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