24941e9ed9810ab87614933f35d7ad5db6685f04
[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.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.net.URISyntaxException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30
31 import javax.annotation.PostConstruct;
32 import javax.annotation.PreDestroy;
33
34 import org.acumos.cds.domain.MLPArtifact;
35 import org.acumos.cds.domain.MLPDocument;
36 import org.acumos.cds.domain.MLPSolution;
37 import org.acumos.cds.domain.MLPSolutionRevision;
38 import org.acumos.federation.gateway.cds.Artifact;
39 import org.acumos.federation.gateway.cds.Document;
40 import org.acumos.federation.gateway.cds.Mapper;
41 import org.acumos.federation.gateway.cds.Solution;
42 import org.acumos.federation.gateway.cds.SolutionRevision;
43 import org.acumos.federation.gateway.service.CatalogService;
44 import org.acumos.federation.gateway.service.ServiceContext;
45 import org.acumos.federation.gateway.service.ServiceException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.BeanInitializationException;
49 import org.springframework.boot.context.properties.ConfigurationProperties;
50 import org.springframework.stereotype.Service;
51
52 import com.fasterxml.jackson.databind.MappingIterator;
53 import com.fasterxml.jackson.databind.ObjectReader;
54
55 /**
56  * 
57  *
58  */
59 @Service
60 @ConfigurationProperties(prefix = "catalog-local")
61 public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements CatalogService {
62
63         private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64
65         private List<Solution> solutions;
66
67         @PostConstruct
68         public void initService() {
69
70                 checkResource();
71                 try {
72                         watcher.watchOn(this.resource.getURL().toURI(), (uri) -> {
73                                 loadSolutionsCatalogInfo();
74                         });
75
76                 } catch (IOException | URISyntaxException iox) {
77                         log.info("Catalog watcher registration failed for " + this.resource, iox);
78                 }
79
80                 loadSolutionsCatalogInfo();
81
82                 // Done
83                 log.debug("Local CatalogService available");
84         }
85
86         @PreDestroy
87         public void cleanupService() {
88         }
89
90         /** */
91         private void loadSolutionsCatalogInfo() {
92                 synchronized (this) {
93                         try {
94                                 ObjectReader objectReader = Mapper.build()
95                                                                                                                                                         .reader(Solution.class);
96                                 MappingIterator objectIterator = objectReader.readValues(this.resource.getURL());
97                                 this.solutions = objectIterator.readAll();
98                                 log.info("loaded " + this.solutions.size() + " solutions");
99                         } catch (Exception x) {
100                                 throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
101                         }
102                 }
103         }
104
105         @Override
106         public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
107
108                 log.debug("getSolutions, selector {}", theSelector);
109
110                 return solutions.stream()
111                         .filter(solution -> ServiceImpl.isSelectable(solution, theSelector))
112                         .collect(Collectors.toList());
113         }
114
115         @Override
116         public Solution getSolution(final String theSolutionId, ServiceContext theContext) throws ServiceException {
117
118                 log.debug("getSolution");
119                 return solutions.stream().filter(solution -> {
120                         return theSolutionId.equals(solution.getSolutionId());
121                 }).findFirst().orElse(null);
122         }
123
124         @Override       
125         public Solution putSolution(Solution theSolution, ServiceContext theContext) throws ServiceException {
126                 
127                 log.trace("putSolution {}", theSolution);
128                 return theSolution;
129         }
130
131         @Override
132         public List<MLPSolutionRevision> getSolutionRevisions(final String theSolutionId, ServiceContext theContext) throws ServiceException {
133
134                 log.debug("getSolutionRevisions");
135
136                 Solution solution = getSolution(theSolutionId, theContext);
137                 return (solution == null) ? Collections.EMPTY_LIST : (List)solution.getRevisions();
138         }
139
140         @Override
141         public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
142                         ServiceContext theContext) throws ServiceException  {
143
144                 log.debug("getSolutionRevision");
145                 return (SolutionRevision)getSolutionRevisions(theSolutionId, theContext).stream()
146                                                 .filter(rev -> rev.getRevisionId().equals(theRevisionId)).findFirst().orElse(null);
147         }
148
149         @Override
150   public SolutionRevision putSolutionRevision(SolutionRevision theRevision, ServiceContext theContext)
151                                                                                                                                                                                                                                                                                                                                                                                                                                 throws ServiceException {
152                 log.trace("putSolutionRevision {}", theRevision);
153                 return theRevision;     
154         }
155
156         @Override
157         public List<MLPArtifact> getSolutionRevisionArtifacts(final String theSolutionId, final String theRevisionId,
158                         ServiceContext theContext) throws ServiceException {
159                 log.debug("getSolutionRevisionArtifacts");
160
161                 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
162                 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getArtifacts();
163         }
164
165         @Override
166         public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) 
167                                                                                                                                                                                                                                                                                                                                                                                                 throws ServiceException {
168                 log.debug("getSolutionRevisionArtifact");
169                 // cumbersome
170                 for (Solution solution : this.solutions) {
171                         for (MLPSolutionRevision revision : solution.getRevisions()) {
172                                 for (MLPArtifact artifact : ((SolutionRevision)revision).getArtifacts()) {
173                                         if (artifact.getArtifactId().equals(theArtifactId)) {
174                                                 return (Artifact)artifact;
175                                         }
176                                 }
177                         }
178                 }
179
180                 return null;
181         }
182         
183         @Override
184         public List<MLPDocument> getSolutionRevisionDocuments(String theSolutionId, String theRevisionId, ServiceContext theContext) throws ServiceException {
185                 log.debug("getSolutionRevisionDocuments");
186
187                 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
188                 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getDocuments();
189         }
190
191         @Override
192         public Document getSolutionRevisionDocument(String theDocumentId, ServiceContext theContext) throws ServiceException {
193                 log.debug("getSolutionRevisionDocument");
194                 // cumbersome
195                 for (Solution solution : this.solutions) {
196                         for (MLPSolutionRevision revision : solution.getRevisions()) {
197                                 for (MLPDocument document : ((SolutionRevision)revision).getDocuments()) {
198                                         if (document.getDocumentId().equals(theDocumentId)) {
199                                                 return (Document)document;
200                                         }
201                                 }
202                         }
203                 }
204
205                 return null;
206         }
207 }