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;
69 import org.acumos.federation.gateway.cds.Artifact;
72 * CDS based implementation of the CatalogService.
76 public class CatalogServiceImpl extends AbstractServiceImpl
77 implements CatalogService {
79 private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogServiceImpl.class.getName());
82 private Environment env;
84 private Map<String, Object> baseSelector;
87 public void initService() {
88 baseSelector = new HashMap<String, Object>();
90 // Fetch all active solutions
91 baseSelector.put(Solution.Fields.active, true);
92 // Fetch allowed only for Public models
93 baseSelector.put(Solution.Fields.accessTypeCode, AccessTypeCode.PB.toString());
94 // Validation status should be passed locally
95 baseSelector.put(Solution.Fields.validationStatusCode, ValidationStatusCode.PS.toString());
101 public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
102 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with selector {}", theSelector);
104 Map<String, Object> selector = new HashMap<String, Object>();
105 if (theSelector != null)
106 selector.putAll(theSelector);
107 //it is essential that this gets done at the end as to force all baseSelector criteria (otherwise a submitted accessTypeCode
108 //could overwrite the basic one end expose non public solutions ..).
109 selector.putAll(this.baseSelector);
110 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with full selector {}", selector);
112 RestPageRequest pageRequest = new RestPageRequest(0, 5);
113 RestPageResponse<MLPSolution> pageResponse = null;
114 List<MLPSolution> solutions = new ArrayList<MLPSolution>(),
115 pageSolutions = null;
116 ICommonDataServiceRestClient cdsClient = getClient();
119 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page {}", pageResponse);
120 if (selector.containsKey(Solution.Fields.modified)) {
121 //Use the dedicated api: this is a 'deep' application of the 'modified' criteria as it will look into revisions
122 //and artifacts for related information modified since.
124 cdsClient.findSolutionsByDate(
125 (Boolean)baseSelector.get(Solution.Fields.active),
126 new String[] {selector.get(Solution.Fields.accessTypeCode).toString()},
127 new String[] {selector.get(Solution.Fields.validationStatusCode).toString()},
128 new Date((Long)selector.get(Solution.Fields.modified)),
131 //we need to post-process all other selection criteria
132 pageSolutions = pageResponse.getContent().stream()
133 .filter(solution -> ServiceImpl.isSelectable(solution, theSelector))
134 .collect(Collectors.toList());
138 cdsClient.searchSolutions(selector, false, pageRequest);
139 pageSolutions = pageResponse.getContent();
141 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page response {}", pageResponse);
143 pageRequest.setPage(pageResponse.getNumber() + 1);
144 solutions.addAll(pageSolutions);
146 while (!pageResponse.isLast());
148 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions: solutions count {}", solutions.size());
153 public Solution getSolution(String theSolutionId, ServiceContext theContext) throws ServiceException {
155 log.trace(EELFLoggerDelegate.debugLogger, "getSolution {}", theSolutionId);
156 ICommonDataServiceRestClient cdsClient = getClient();
158 Solution solution = (Solution)cdsClient.getSolution(theSolutionId);
159 solution.setRevisions(cdsClient.getSolutionRevisions(theSolutionId));
162 catch (HttpStatusCodeException restx) {
163 if (Errors.isCDSNotFound(restx))
166 throw new ServiceException("Failed to retrieve solution information", restx);
171 public List<MLPSolutionRevision> getSolutionRevisions(String theSolutionId, ServiceContext theContext) throws ServiceException {
173 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
175 return getClient().getSolutionRevisions(theSolutionId);
177 catch (HttpStatusCodeException restx) {
178 if (Errors.isCDSNotFound(restx))
181 throw new ServiceException("Failed to retrieve solution revision information", restx);
186 public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
187 ServiceContext theContext) throws ServiceException {
189 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
190 ICommonDataServiceRestClient cdsClient = getClient();
192 SolutionRevision revision =
193 (SolutionRevision)cdsClient.getSolutionRevision(theSolutionId, theRevisionId);
194 revision.setArtifacts(cdsClient.getSolutionRevisionArtifacts(theSolutionId, theRevisionId));
197 catch (HttpStatusCodeException restx) {
198 if (Errors.isCDSNotFound(restx))
201 throw new ServiceException("Failed to retrieve solution revision information", restx);
206 public List<MLPArtifact> getSolutionRevisionArtifacts(String theSolutionId, String theRevisionId,
207 ServiceContext theContext) throws ServiceException {
209 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
211 return getClient().getSolutionRevisionArtifacts(theSolutionId, theRevisionId);
213 catch (HttpStatusCodeException restx) {
214 if (Errors.isCDSNotFound(restx))
217 throw new ServiceException("Failed to retrieve solution information", restx);
222 * @return catalog artifact representation
223 * @throws ServiceException if failing to retrieve artifact information or retrieve content
226 public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) throws ServiceException {
228 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifact");
230 return (Artifact)getClient().getArtifact(theArtifactId);
232 catch (HttpStatusCodeException restx) {
233 if (Errors.isCDSNotFound(restx))
236 throw new ServiceException("Failed to retrieve solution revision artifact information", restx);