ec7b0688b23241833ed31eefc6721f880675a401
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / FederationServiceImpl.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.stream.Collectors;
36
37 import javax.annotation.PostConstruct;
38
39 import org.apache.commons.io.FileUtils;
40 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
41 import org.acumos.federation.gateway.service.FederationService;
42 import org.acumos.federation.gateway.util.Utils;
43 import org.acumos.federation.gateway.common.GatewayCondition;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.core.env.Environment;
46 import org.springframework.core.io.InputStreamResource;
47 import org.springframework.stereotype.Service;
48 import org.springframework.context.annotation.Conditional;
49
50 import org.acumos.nexus.client.NexusArtifactClient;
51 import org.acumos.nexus.client.RepositoryLocation;
52
53 import org.acumos.cds.AccessTypeCode;
54 import org.acumos.cds.ValidationStatusCode;
55 import org.acumos.cds.client.ICommonDataServiceRestClient;
56 import org.acumos.cds.domain.MLPArtifact;
57 import org.acumos.cds.domain.MLPSolution;
58 import org.acumos.cds.domain.MLPSolutionRevision;
59 import org.acumos.cds.transport.RestPageResponse;
60
61 /**
62  * 
63  *
64  */
65 @Service
66 @Conditional(GatewayCondition.class)
67 public class FederationServiceImpl extends AbstractServiceImpl implements FederationService {
68
69         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(FederationServiceImpl.class);
70
71         @Autowired
72         private Environment env;
73
74         private Map<String, Object> baseSelector;
75         
76         @PostConstruct
77         public void initService() {
78                 baseSelector = new HashMap<String, Object>();
79 /*
80                 baseSelector.put("active", true); //Fetch all active solutions
81                 baseSelector.put("accessTypeCode", AccessTypeCode.PB.toString()); // Fetch allowed only for Public models
82                 baseSelector.put("validationStatusCode", ValidationStatusCode.PS.toString()); // Validation status should be Passed locally
83                 baseSelector.put("provider", env.getProperty("federated.instance.name"));
84 */      
85         }
86
87         /**
88          * This needs to be implemented for future enhancement where pagination is allowed
89          */
90         @Override
91         public RestPageResponse<MLPSolution>  getPeerCatalogSolutions(Integer pageNumber, Integer maxSize, String sortingOrder,
92                         List<String> mlpModelTypes) {
93                 return null;
94         }
95
96         @Override
97         public List<MLPSolution> getPeerCatalogSolutionsList(String mlpModelTypes) {
98                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionsList");
99                 List<MLPSolution> filteredMLPSolutions = null;
100                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
101                 //TODO: revisit this code to pass query parameters to CCDS Service
102                 Map<String, Object> queryParameters = new HashMap<String, Object>(this.baseSelector);
103                 List<MLPSolution> mlpSolutions = dataServiceRestClient.searchSolutions(queryParameters, false);
104                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionsList: data service provided solutions " + mlpSolutions);
105
106                 if(mlpSolutions != null && mlpSolutions.size() > 0 && !Utils.isEmptyOrNullString(mlpModelTypes)) {
107                         //Filter List using Lamba to get solutions which matches the ML Model Type
108                         filteredMLPSolutions = mlpSolutions.stream()
109                                                                                                                                 .filter(mlpSolution -> { String modelType = mlpSolution.getModelTypeCode();
110                                                                                                                                                                                                                                  return modelType == null || //for testing only
111                                                                                                                                                                                                                                                                 mlpModelTypes.contains(modelType);      
112                                                                                                                                                                                                                          })
113                                                                                                                                 .collect(Collectors.toList());
114                 } else {
115                         filteredMLPSolutions = mlpSolutions;
116                 }
117                 return filteredMLPSolutions;
118         }
119
120         
121         @Override
122         public List<MLPSolutionRevision> getPeerCatalogSolutionRevision(String solutionId) {
123                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionRevision`");
124                 List<MLPSolutionRevision> mlpSolutionRevisions = null;
125                 ICommonDataServiceRestClient dataServiceRestClient = getClient();
126                 mlpSolutionRevisions = dataServiceRestClient.getSolutionRevisions(solutionId);
127                 return mlpSolutionRevisions;
128         }
129
130         @Override
131         public List<MLPArtifact> getPeerSolutionArtifacts(String solutionId, String revisionId) {
132                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerSolutionArtifacts`");
133                 List<MLPArtifact> mlpSolutionRevisions = new ArrayList<>();
134                 FederationDataClient commonDataClient = getCommonDataClient();
135                 Iterable<MLPArtifact> iterable = commonDataClient.getSolutionRevisionArtifacts(solutionId, revisionId);
136                 for (MLPArtifact mlpArtifact : iterable) {
137                         mlpSolutionRevisions.add(mlpArtifact);
138                 }
139                 return mlpSolutionRevisions;
140         }
141
142         @Override
143         public InputStreamResource getPeerSolutionArtifactFile(String artifactId) {
144                 InputStreamResource streamResource = null;
145                 ByteArrayOutputStream byteArrayOutputStream  = null;
146                 try{
147                         ICommonDataServiceRestClient dataServiceRestClient = getClient();
148                         MLPArtifact mlpArtifact = dataServiceRestClient.getArtifact(artifactId);
149                         
150                         
151                         String path = Utils.getTempFolderPath(mlpArtifact.getName(), mlpArtifact.getVersion(), env.getProperty("nexus.tempFolder", ""));
152                         
153                         RepositoryLocation repositoryLocation = new RepositoryLocation();
154                         repositoryLocation.setId("1");
155                         repositoryLocation.setUrl(env.getProperty("nexus.url"));
156                         repositoryLocation.setUsername(env.getProperty("nexus.username"));
157                         repositoryLocation.setPassword(env.getProperty("nexus.password"));
158                         repositoryLocation.setProxy(env.getProperty("nexus.proxy"));
159                         
160                         // if you need a proxy to access the Nexus
161                         NexusArtifactClient artifactClient = new NexusArtifactClient(repositoryLocation);
162                         
163                         byteArrayOutputStream = artifactClient.getArtifact(mlpArtifact.getUri());
164                         InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
165                         
166                         //Plain Old Java. Sprint 3 will use try resource handling
167                         if(inputStream != null) {
168                                 streamResource = new InputStreamResource(inputStream);
169                         }
170                         if(byteArrayOutputStream != null) {
171                                 byteArrayOutputStream.close();
172                         }
173                         Utils.deletetTempFiles(path);
174                         
175                 } catch (Exception e) {
176                         log.error(EELFLoggerDelegate.errorLogger, "getPeerSolutionArtifactFile`", e);
177                 } 
178                 // TODO Auto-generated method stub
179                 return streamResource;
180         }
181
182         
183 }