adb8546b4bf9b2c408d6799ad1ed7b07e7939a5f
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / service / impl / FederationServiceLocalImpl.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.FederationService;
52 import org.acumos.federation.gateway.util.Utils;
53 import org.acumos.federation.gateway.util.LocalWatchService;
54 import org.acumos.federation.gateway.common.AdapterCondition;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.beans.factory.BeanInitializationException;
57 import org.springframework.core.env.Environment;
58 import org.springframework.core.io.InputStreamResource;
59 import org.springframework.stereotype.Service;
60 import org.springframework.context.annotation.Conditional;
61 import org.springframework.boot.context.properties.ConfigurationProperties;
62
63 import org.acumos.cds.AccessTypeCode;
64 import org.acumos.cds.ValidationStatusCode;
65 import org.acumos.cds.client.ICommonDataServiceRestClient;
66 import org.acumos.cds.domain.MLPArtifact;
67 import org.acumos.cds.domain.MLPSolution;
68 import org.acumos.cds.domain.MLPSolutionRevision;
69 import org.acumos.cds.transport.RestPageResponse;
70
71
72 /**
73  * 
74  *
75  */
76 @Service
77 @ConfigurationProperties(prefix="catalogLocal")
78 @Conditional(AdapterCondition.class)
79 public class FederationServiceLocalImpl extends AbstractServiceLocalImpl
80                                                                                                                                                                  implements FederationService {
81
82         private List<FLPSolution>                                       solutions;
83
84
85         @PostConstruct
86         public void initService() {
87
88                 checkResource();
89                 try {
90             watcher.watchOn(this.resource.getURL().toURI(),
91                           (uri) -> { loadSolutionsCatalogInfo(); });
92
93     }
94     catch (IOException | URISyntaxException iox) {
95       log.info(EELFLoggerDelegate.errorLogger, "Catalog watcher registration failed for " + this.resource, iox);
96     }
97
98                 loadSolutionsCatalogInfo();
99
100     // Done
101     log.debug(EELFLoggerDelegate.debugLogger, "Local FederationService available");
102         }
103
104         @PreDestroy
105         public void cleanupService() {
106         }               
107
108         /** */
109         private void loadSolutionsCatalogInfo() {
110                 synchronized (this) {
111                         try {
112                                 ObjectReader objectReader = 
113                                                                                                                 new ObjectMapper().reader(FLPSolution.class);
114                                 MappingIterator objectIterator =
115                                                                                                                 objectReader.readValues(this.resource.getURL());
116                                 this.solutions = objectIterator.readAll();
117                                 log.info(EELFLoggerDelegate.debugLogger, "loaded " + this.solutions.size() + " solutions");
118                         }
119                         catch (Exception x) {
120                                 throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
121                         }
122                 }
123         }
124
125         /**
126          */
127         @Override
128         public RestPageResponse<MLPSolution>  getPeerCatalogSolutions(Integer pageNumber, Integer maxSize, String sortingOrder,
129                         List<String> mlpModelTypes) {
130                 return null;
131         }
132
133         @Override
134         public List<MLPSolution> getPeerCatalogSolutionsList(String mlpModelTypes) {
135                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionsList");
136                 final List<String> modelTypes =
137                         mlpModelTypes == null ? null : Arrays.asList(mlpModelTypes.split(","));
138                 return solutions.stream()
139                                                         .filter(solution -> {
140                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionsList: looking for " + modelTypes + ", has " + solution.getModelTypeCode());
141                                                                                                 return modelTypes == null ||
142                                                                                                                          modelTypes.contains(solution.getModelTypeCode());
143                                                                                         })
144                                                         .collect(Collectors.toList());
145         }
146
147         
148         @Override
149         public List<MLPSolutionRevision> getPeerCatalogSolutionRevision(
150                                                                                                                                                                         final String theSolutionId) {
151                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerCatalogSolutionRevision`");
152                 FLPSolution solution =
153                                                 this.solutions.stream()
154                                                         .filter(sol ->
155                                                                                                 sol.getSolutionId().equals(theSolutionId))
156                                                         .findFirst()
157                                                         .orElse(null);
158                 
159                 return (solution == null) ? null : solution.getMLPRevisions();
160         }
161
162         @Override
163         public List<MLPArtifact> getPeerSolutionArtifacts(
164                                                                                                                                                                                 final String theSolutionId,
165                                                                                                                                                                                 final String theRevisionId) {
166                 log.debug(EELFLoggerDelegate.debugLogger, "getPeerSolutionArtifacts`");
167                 FLPSolution solution =
168                                                  this.solutions.stream()
169                                                         .filter(sol ->
170                                                                                                 sol.getSolutionId().equals(theSolutionId))
171                                                         .findFirst()
172                                                         .orElse(null);
173
174                 if (solution == null)
175                         return null;
176
177                 FLPRevision revision = 
178                         solution.getRevisions().stream()
179                                                         .filter(rev ->
180                                                                                                 rev.getRevisionId().equals(theRevisionId))
181                                                         .findFirst()
182                                                         .orElse(null);
183
184                 return (revision == null) ? null : revision.getArtifacts();
185         }
186
187         @Override
188         public InputStreamResource getPeerSolutionArtifactFile(
189                                                                                                                 final String theArtifactId) {
190                 return null;
191         }
192
193         /** */
194         public static class FLPSolution extends MLPSolution {
195
196     @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
197                 private List<FLPRevision> revisions;
198
199                 //@JsonIgnore
200                 public List<FLPRevision> getRevisions() {
201                         return this.revisions;
202                 }
203                 
204                 //@JsonIgnore
205                 protected List<MLPSolutionRevision> getMLPRevisions() {
206                         return this.revisions == null ? null :
207                                                                 this.revisions.stream()
208                                                                                         .map(rev -> (MLPSolutionRevision)rev)
209                                                                                         .collect(Collectors.toList());
210                 }
211
212                 public void setRevisions(List<FLPRevision> theRevisions) {
213                         this.revisions = theRevisions;
214                 }
215
216         }
217         
218         /** */
219         public static class FLPRevision extends MLPSolutionRevision {
220
221     @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
222                 private List<MLPArtifact> artifacts;
223
224                 //@JsonIgnore
225                 public List<MLPArtifact> getArtifacts() {
226                         return this.artifacts;
227                 }
228
229                 public void setArtifacts(List<MLPArtifact> theArtifacts) {
230                         this.artifacts = theArtifacts;
231                 }
232         }
233
234
235 }