Refine access control, peer access
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / CatalogServiceLocalImpl.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 package org.acumos.federation.gateway.service.impl;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.ArrayList;
33 import java.util.LinkedList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.stream.Collectors;
38
39 import javax.annotation.PostConstruct;
40 import javax.annotation.PreDestroy;
41
42 import com.fasterxml.jackson.annotation.JsonIgnore;
43 import com.fasterxml.jackson.annotation.JsonProperty;
44 import com.fasterxml.jackson.databind.MappingIterator;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.databind.ObjectReader;
47
48 import org.apache.commons.io.FileUtils;
49
50 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
51 import org.acumos.federation.gateway.service.CatalogService;
52 import org.acumos.federation.gateway.service.ServiceContext;
53 import org.acumos.federation.gateway.util.Utils;
54 import org.acumos.federation.gateway.util.LocalWatchService;
55 import org.acumos.federation.gateway.common.AdapterCondition;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.beans.factory.BeanInitializationException;
58 import org.springframework.core.env.Environment;
59 import org.springframework.core.io.InputStreamResource;
60 import org.springframework.stereotype.Service;
61 import org.springframework.context.annotation.Conditional;
62 import org.springframework.boot.context.properties.ConfigurationProperties;
63
64 import org.acumos.cds.AccessTypeCode;
65 import org.acumos.cds.ValidationStatusCode;
66 import org.acumos.cds.client.ICommonDataServiceRestClient;
67 import org.acumos.cds.domain.MLPArtifact;
68 import org.acumos.cds.domain.MLPSolution;
69 import org.acumos.cds.domain.MLPSolutionRevision;
70 import org.acumos.cds.transport.RestPageResponse;
71
72
73 /**
74  * 
75  *
76  */
77 @Service
78 @ConfigurationProperties(prefix="catalogLocal")
79 @Conditional(AdapterCondition.class)
80 public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl
81                                                                                                                                                  implements CatalogService {
82
83         private List<FLPSolution>                                       solutions;
84
85
86         @PostConstruct
87         public void initService() {
88
89                 checkResource();
90                 try {
91             watcher.watchOn(this.resource.getURL().toURI(),
92                           (uri) -> { loadSolutionsCatalogInfo(); });
93
94     }
95     catch (IOException | URISyntaxException iox) {
96       log.info(EELFLoggerDelegate.errorLogger, "Catalog watcher registration failed for " + this.resource, iox);
97     }
98
99                 loadSolutionsCatalogInfo();
100
101     // Done
102     log.debug(EELFLoggerDelegate.debugLogger, "Local CatalogService available");
103         }
104
105         @PreDestroy
106         public void cleanupService() {
107         }               
108
109         /** */
110         private void loadSolutionsCatalogInfo() {
111                 synchronized (this) {
112                         try {
113                                 ObjectReader objectReader = 
114                                                                                                                 new ObjectMapper().reader(FLPSolution.class);
115                                 MappingIterator objectIterator =
116                                                                                                                 objectReader.readValues(this.resource.getURL());
117                                 this.solutions = objectIterator.readAll();
118                                 log.info(EELFLoggerDelegate.debugLogger, "loaded " + this.solutions.size() + " solutions");
119                         }
120                         catch (Exception x) {
121                                 throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
122                         }
123                 }
124         }
125
126
127         @Override
128         public List<MLPSolution> getSolutions(
129                 String mlpModelTypes, ServiceContext theContext) {
130
131                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions");
132                 final List<String> modelTypes =
133                         mlpModelTypes == null ? null : Arrays.asList(mlpModelTypes.split(","));
134                 return solutions.stream()
135                                                         .filter(solution -> {
136                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionsList: looking for " + modelTypes + ", has " + solution.getModelTypeCode());
137                                                                                                 return modelTypes == null ||
138                                                                                                                          modelTypes.contains(solution.getModelTypeCode());
139                                                                                         })
140                                                         .collect(Collectors.toList());
141         }
142
143         @Override
144         public MLPSolution getSolution(
145                 final String theSolutionId, ServiceContext theContext) {
146
147                 log.debug(EELFLoggerDelegate.debugLogger, "getSolution");
148                 return solutions.stream()
149                                                         .filter(solution -> {
150                                                                                                 return theSolutionId.equals(solution.getSolutionId());
151                                                                                         })
152                                                         .findFirst()
153                                                         .orElse(null);
154         }
155         
156         @Override
157         public List<MLPSolutionRevision> getSolutionRevisions(
158                 final String theSolutionId, ServiceContext theContext) {
159
160                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
161                 FLPSolution solution =
162                                                 this.solutions.stream()
163                                                         .filter(sol ->
164                                                                                                 sol.getSolutionId().equals(theSolutionId))
165                                                         .findFirst()
166                                                         .orElse(null);
167                 
168                 return (solution == null) ? null : solution.getMLPRevisions();
169         }
170         
171         @Override
172         public MLPSolutionRevision getSolutionRevision(
173                 String theSolutionId, String theRevisionId, ServiceContext theContext) {
174
175                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
176                 List<MLPSolutionRevision> revisions = getSolutionRevisions(theSolutionId, theContext);
177
178                 if (revisions == null)
179                         return null;
180
181                 return revisions.stream()
182                                                         .filter(rev ->
183                                                                                                 rev.getRevisionId().equals(theRevisionId))
184                                                         .findFirst()
185                                                         .orElse(null);
186         }
187
188         @Override
189         public List<MLPArtifact> getSolutionRevisionArtifacts(
190                 final String theSolutionId,     final String theRevisionId, ServiceContext theContext) {
191                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
192
193                 FLPRevision revision = (FLPRevision)
194                         getSolutionRevision(theSolutionId, theRevisionId, theContext);
195
196                 return (revision == null) ? null : revision.getArtifacts();
197         }
198
199         @Override
200         public InputStreamResource getSolutionRevisionArtifactContent(
201                 String theArtifactId, ServiceContext theContext) {
202
203                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifactContent");
204                 //difficult here as we only have the artifact id ..
205         
206                 return null;
207         }
208
209         /** */
210         public static class FLPSolution extends MLPSolution {
211
212     @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
213                 private List<FLPRevision> revisions;
214
215                 //@JsonIgnore
216                 public List<FLPRevision> getRevisions() {
217                         return this.revisions;
218                 }
219                 
220                 //@JsonIgnore
221                 protected List<MLPSolutionRevision> getMLPRevisions() {
222                         return this.revisions == null ? null :
223                                                                 this.revisions.stream()
224                                                                                         .map(rev -> (MLPSolutionRevision)rev)
225                                                                                         .collect(Collectors.toList());
226                 }
227
228                 public void setRevisions(List<FLPRevision> theRevisions) {
229                         this.revisions = theRevisions;
230                 }
231
232         }
233         
234         /** */
235         public static class FLPRevision extends MLPSolutionRevision {
236
237     @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
238                 private List<MLPArtifact> artifacts;
239
240                 //@JsonIgnore
241                 public List<MLPArtifact> getArtifacts() {
242                         return this.artifacts;
243                 }
244
245                 public void setArtifacts(List<MLPArtifact> theArtifacts) {
246                         this.artifacts = theArtifacts;
247                 }
248         }
249
250
251 }