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