3cf60229e52d499a13041fee7fa7126f161d4365
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / CatalogServiceImpl.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
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
11  *  
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *  
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=========================================================
19  */
20
21 /**
22  * 
23  */
24 package org.acumos.federation.gateway.service.impl;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
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;
34 import java.util.Map;
35 import java.util.Date;
36 import java.util.stream.Collectors;
37
38 import javax.annotation.PostConstruct;
39
40 import org.apache.commons.io.FileUtils;
41
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;
48
49 import org.acumos.nexus.client.NexusArtifactClient;
50
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;
59
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;
66
67 import org.acumos.federation.gateway.cds.Solution;
68 import org.acumos.federation.gateway.cds.SolutionRevision;
69 import org.acumos.federation.gateway.cds.Artifact;
70
71 /**
72  * CDS based implementation of the CatalogService.
73  *
74  */
75 @Service
76 public class CatalogServiceImpl extends AbstractServiceImpl
77                                                                                                                                 implements CatalogService {
78
79         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogServiceImpl.class.getName());
80
81         @Autowired
82         private Environment env;
83
84         private Map<String, Object> baseSelector;
85
86         @PostConstruct
87         public void initService() {
88                 baseSelector = new HashMap<String, Object>();
89
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());
96         }
97
98         @Override
99         /*
100          */
101         public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
102                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with selector {}", theSelector);
103
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);
111
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();
117
118                 do {
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.
123                                 pageResponse =
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)),
129                                                 pageRequest);
130                         
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());
135                         }
136                         else {
137                                 pageResponse =
138                                         cdsClient.searchSolutions(selector, false, pageRequest);
139                                 pageSolutions = pageResponse.getContent();
140                         }
141                         log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page response {}", pageResponse);
142                 
143                         pageRequest.setPage(pageResponse.getNumber() + 1);
144                         solutions.addAll(pageSolutions);
145                 }
146                 while (!pageResponse.isLast());
147
148                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions: solutions count {}", solutions.size());
149                 return solutions;
150         }
151
152         @Override
153         public Solution getSolution(String theSolutionId, ServiceContext theContext) throws ServiceException {
154
155                 log.trace(EELFLoggerDelegate.debugLogger, "getSolution {}", theSolutionId);
156                 ICommonDataServiceRestClient cdsClient = getClient();
157                 try {
158                         Solution solution = (Solution)cdsClient.getSolution(theSolutionId);
159                         solution.setRevisions(cdsClient.getSolutionRevisions(theSolutionId));
160                         return solution;
161                 }
162                 catch (HttpStatusCodeException restx) {
163                         if (Errors.isCDSNotFound(restx))
164                                 return null;
165                         else
166                                 throw new ServiceException("Failed to retrieve solution information", restx);
167                 }
168         }
169
170         @Override
171         public List<MLPSolutionRevision> getSolutionRevisions(String theSolutionId, ServiceContext theContext) throws ServiceException {
172
173                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
174                 try {
175                         return getClient().getSolutionRevisions(theSolutionId);
176                 }
177                 catch (HttpStatusCodeException restx) {
178                         if (Errors.isCDSNotFound(restx))
179                                 return null;
180                         else
181                                 throw new ServiceException("Failed to retrieve solution revision information", restx);
182                 }
183         }
184
185         @Override
186         public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
187                         ServiceContext theContext) throws ServiceException {
188
189                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
190                 ICommonDataServiceRestClient cdsClient = getClient();
191                 try {
192                         SolutionRevision revision =
193                                         (SolutionRevision)cdsClient.getSolutionRevision(theSolutionId, theRevisionId);
194                         revision.setArtifacts(cdsClient.getSolutionRevisionArtifacts(theSolutionId, theRevisionId));
195                         return revision;
196                 }       
197                 catch (HttpStatusCodeException restx) {
198                         if (Errors.isCDSNotFound(restx))
199                                 return null;
200                         else
201                                 throw new ServiceException("Failed to retrieve solution revision information", restx);
202                 }
203         }
204
205         @Override
206         public List<MLPArtifact> getSolutionRevisionArtifacts(String theSolutionId, String theRevisionId,
207                         ServiceContext theContext) throws ServiceException {
208
209                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
210                 try {
211                         return getClient().getSolutionRevisionArtifacts(theSolutionId, theRevisionId);
212                 }
213                 catch (HttpStatusCodeException restx) {
214                         if (Errors.isCDSNotFound(restx))
215                                 return null;
216                         else
217                                 throw new ServiceException("Failed to retrieve solution information", restx);
218                 }
219         }
220
221         /**
222          * @return catalog artifact representation
223          * @throws ServiceException if failing to retrieve artifact information or retrieve content 
224          */
225         @Override
226         public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) throws ServiceException {
227
228                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifact");
229                 try {
230                         return (Artifact)getClient().getArtifact(theArtifactId);
231                 }       
232                 catch (HttpStatusCodeException restx) {
233                         if (Errors.isCDSNotFound(restx))
234                                 return null;
235                         else
236                                 throw new ServiceException("Failed to retrieve solution revision artifact information", restx);
237                 }
238         }
239
240 }