2 * ===============LICENSE_START=======================================================
4 * ===================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6 * ===================================================================================
7 * This Acumos software file is distributed by AT&T and Tech Mahindra
8 * under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * This file is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ===============LICENSE_END=========================================================
24 package org.acumos.federation.gateway.service.impl;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Date;
36 import java.util.stream.Collectors;
38 import javax.annotation.PostConstruct;
40 import org.apache.commons.io.FileUtils;
42 import org.acumos.federation.gateway.util.Utils;
43 import org.acumos.federation.gateway.util.Errors;
44 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
45 import org.acumos.federation.gateway.service.CatalogService;
46 import org.acumos.federation.gateway.service.ServiceContext;
47 import org.acumos.federation.gateway.service.ServiceException;
49 import org.acumos.nexus.client.NexusArtifactClient;
51 import org.acumos.cds.AccessTypeCode;
52 import org.acumos.cds.ValidationStatusCode;
53 import org.acumos.cds.client.ICommonDataServiceRestClient;
54 import org.acumos.cds.domain.MLPArtifact;
55 import org.acumos.cds.domain.MLPSolution;
56 import org.acumos.cds.domain.MLPSolutionRevision;
57 import org.acumos.cds.transport.RestPageResponse;
58 import org.acumos.cds.transport.RestPageRequest;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.core.env.Environment;
62 import org.springframework.core.io.InputStreamResource;
63 import org.springframework.stereotype.Service;
64 import org.springframework.context.annotation.Conditional;
65 import org.springframework.web.client.HttpStatusCodeException;
67 import org.acumos.federation.gateway.cds.Solution;
68 import org.acumos.federation.gateway.cds.SolutionRevision;
71 * CDS based implementation of the CatalogService.
75 public class CatalogServiceImpl extends AbstractServiceImpl
76 implements CatalogService {
78 private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogServiceImpl.class.getName());
81 private Environment env;
83 private Map<String, Object> baseSelector;
86 public void initService() {
87 baseSelector = new HashMap<String, Object>();
89 // Fetch all active solutions
90 baseSelector.put(Solution.Fields.active, true);
91 // Fetch allowed only for Public models
92 baseSelector.put(Solution.Fields.accessTypeCode, AccessTypeCode.PB.toString());
93 // Validation status should be passed locally
94 baseSelector.put(Solution.Fields.validationStatusCode, ValidationStatusCode.PS.toString());
100 public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
101 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with selector {}", theSelector);
103 Map<String, Object> selector = new HashMap<String, Object>();
104 if (theSelector != null)
105 selector.putAll(theSelector);
106 //it is essential that this gets done at the end as to force all baseSelector criteria (otherwise a submitted accessTypeCode
107 //could overwrite the basic one end expose non public solutions ..).
108 selector.putAll(this.baseSelector);
109 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with full selector {}", selector);
111 RestPageRequest pageRequest = new RestPageRequest(0, 5);
112 RestPageResponse<MLPSolution> pageResponse = null;
113 List<MLPSolution> solutions = new ArrayList<MLPSolution>(),
114 pageSolutions = null;
115 ICommonDataServiceRestClient cdsClient = getClient();
118 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page {}", pageResponse);
119 if (selector.containsKey(Solution.Fields.modified)) {
120 //Use the dedicated api: this is a 'deep' application of the 'modified' criteria as it will look into revisions
121 //and artifacts for related information modified since.
123 cdsClient.findSolutionsByDate(
124 (Boolean)baseSelector.get(Solution.Fields.active),
125 new String[] {selector.get(Solution.Fields.accessTypeCode).toString()},
126 new String[] {selector.get(Solution.Fields.validationStatusCode).toString()},
127 new Date((Long)selector.get(Solution.Fields.modified)),
130 //we need to post-process all other selection criteria
131 pageSolutions = pageResponse.getContent().stream()
132 .filter(solution -> ServiceImpl.isSelectable(solution, theSelector))
133 .collect(Collectors.toList());
137 cdsClient.searchSolutions(selector, false, pageRequest);
138 pageSolutions = pageResponse.getContent();
140 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page response {}", pageResponse);
142 pageRequest.setPage(pageResponse.getNumber() + 1);
143 solutions.addAll(pageSolutions);
145 while (!pageResponse.isLast());
147 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions: cds solutions count {}", solutions.size());
152 public MLPSolution getSolution(String theSolutionId, ServiceContext theContext) throws ServiceException {
154 log.trace(EELFLoggerDelegate.debugLogger, "getSolution {}", theSolutionId);
155 ICommonDataServiceRestClient cdsClient = getClient();
157 Solution solution = (Solution)cdsClient.getSolution(theSolutionId);
158 solution.setRevisions(cdsClient.getSolutionRevisions(theSolutionId));
161 catch (HttpStatusCodeException restx) {
162 if (Errors.isCDSNotFound(restx))
165 throw new ServiceException("Failed to retrieve solution information", restx);
170 public List<MLPSolutionRevision> getSolutionRevisions(String theSolutionId, ServiceContext theContext) throws ServiceException {
172 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
174 return getClient().getSolutionRevisions(theSolutionId);
176 catch (HttpStatusCodeException restx) {
177 if (Errors.isCDSNotFound(restx))
180 throw new ServiceException("Failed to retrieve solution revision information", restx);
185 public MLPSolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
186 ServiceContext theContext) throws ServiceException {
188 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
189 ICommonDataServiceRestClient cdsClient = getClient();
191 SolutionRevision revision =
192 (SolutionRevision)cdsClient.getSolutionRevision(theSolutionId, theRevisionId);
193 revision.setArtifacts(cdsClient.getSolutionRevisionArtifacts(theSolutionId, theRevisionId));
196 catch (HttpStatusCodeException restx) {
197 if (Errors.isCDSNotFound(restx))
200 throw new ServiceException("Failed to retrieve solution revision information", restx);
205 public List<MLPArtifact> getSolutionRevisionArtifacts(String theSolutionId, String theRevisionId,
206 ServiceContext theContext) throws ServiceException {
208 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
210 return getClient().getSolutionRevisionArtifacts(theSolutionId, theRevisionId);
212 catch (HttpStatusCodeException restx) {
213 if (Errors.isCDSNotFound(restx))
216 throw new ServiceException("Failed to retrieve solution information", restx);
221 * @return catalog artifact representation
222 * @throws ServiceException if failing to retrieve artifact information or retrieve content
225 public MLPArtifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) throws ServiceException {
227 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifact");
229 return getClient().getArtifact(theArtifactId);
231 catch (HttpStatusCodeException restx) {
232 if (Errors.isCDSNotFound(restx))
235 throw new ServiceException("Failed to retrieve solution revision artifact information", restx);