9256c0be4f6012685ce181d01ebe4c652a0dd00b
[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
70 /**
71  * CDS based implementation of the CatalogService.
72  *
73  */
74 @Service
75 public class CatalogServiceImpl extends AbstractServiceImpl
76                                                                                                                                 implements CatalogService {
77
78         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogServiceImpl.class.getName());
79
80         @Autowired
81         private Environment env;
82
83         private Map<String, Object> baseSelector;
84
85         @PostConstruct
86         public void initService() {
87                 baseSelector = new HashMap<String, Object>();
88
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());
95         }
96
97         @Override
98         /*
99          */
100         public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
101                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions with selector {}", theSelector);
102
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);
110
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();
116
117                 do {
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.
122                                 pageResponse =
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)),
128                                                 pageRequest);
129                         
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());
134                         }
135                         else {
136                                 pageResponse =
137                                         cdsClient.searchSolutions(selector, false, pageRequest);
138                                 pageSolutions = pageResponse.getContent();
139                         }
140                         log.debug(EELFLoggerDelegate.debugLogger, "getSolutions page response {}", pageResponse);
141                 
142                         pageRequest.setPage(pageResponse.getNumber() + 1);
143                         solutions.addAll(pageSolutions);
144                 }
145                 while (!pageResponse.isLast());
146
147                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions: cds solutions count {}", solutions.size());
148                 return solutions;
149         }
150
151         @Override
152         public MLPSolution getSolution(String theSolutionId, ServiceContext theContext) throws ServiceException {
153
154                 log.trace(EELFLoggerDelegate.debugLogger, "getSolution {}", theSolutionId);
155                 ICommonDataServiceRestClient cdsClient = getClient();
156                 try {
157                         Solution solution = (Solution)cdsClient.getSolution(theSolutionId);
158                         solution.setRevisions(cdsClient.getSolutionRevisions(theSolutionId));
159                         return solution;
160                 }
161                 catch (HttpStatusCodeException restx) {
162                         if (Errors.isCDSNotFound(restx))
163                                 return null;
164                         else
165                                 throw new ServiceException("Failed to retrieve solution information", restx);
166                 }
167         }
168
169         @Override
170         public List<MLPSolutionRevision> getSolutionRevisions(String theSolutionId, ServiceContext theContext) throws ServiceException {
171
172                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
173                 try {
174                         return getClient().getSolutionRevisions(theSolutionId);
175                 }
176                 catch (HttpStatusCodeException restx) {
177                         if (Errors.isCDSNotFound(restx))
178                                 return null;
179                         else
180                                 throw new ServiceException("Failed to retrieve solution revision information", restx);
181                 }
182         }
183
184         @Override
185         public MLPSolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
186                         ServiceContext theContext) throws ServiceException {
187
188                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
189                 ICommonDataServiceRestClient cdsClient = getClient();
190                 try {
191                         SolutionRevision revision =
192                                         (SolutionRevision)cdsClient.getSolutionRevision(theSolutionId, theRevisionId);
193                         revision.setArtifacts(cdsClient.getSolutionRevisionArtifacts(theSolutionId, theRevisionId));
194                         return revision;
195                 }       
196                 catch (HttpStatusCodeException restx) {
197                         if (Errors.isCDSNotFound(restx))
198                                 return null;
199                         else
200                                 throw new ServiceException("Failed to retrieve solution revision information", restx);
201                 }
202         }
203
204         @Override
205         public List<MLPArtifact> getSolutionRevisionArtifacts(String theSolutionId, String theRevisionId,
206                         ServiceContext theContext) throws ServiceException {
207
208                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
209                 try {
210                         return getClient().getSolutionRevisionArtifacts(theSolutionId, theRevisionId);
211                 }
212                 catch (HttpStatusCodeException restx) {
213                         if (Errors.isCDSNotFound(restx))
214                                 return null;
215                         else
216                                 throw new ServiceException("Failed to retrieve solution information", restx);
217                 }
218         }
219
220         /**
221          * @return catalog artifact representation
222          * @throws ServiceException if failing to retrieve artifact information or retrieve content 
223          */
224         @Override
225         public MLPArtifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) throws ServiceException {
226
227                 log.trace(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifact");
228                 try {
229                         return getClient().getArtifact(theArtifactId);
230                 }       
231                 catch (HttpStatusCodeException restx) {
232                         if (Errors.isCDSNotFound(restx))
233                                 return null;
234                         else
235                                 throw new ServiceException("Failed to retrieve solution revision artifact information", restx);
236                 }
237         }
238
239 }