2 * ===============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.acumos.federation.gateway.service.impl;
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;
30 import java.util.function.Predicate;
31 import java.util.stream.Collectors;
33 import javax.annotation.PostConstruct;
34 import javax.annotation.PreDestroy;
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;
54 import com.fasterxml.jackson.databind.MappingIterator;
55 import com.fasterxml.jackson.databind.ObjectReader;
62 @ConfigurationProperties(prefix = "catalog-local")
63 public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements CatalogService {
65 private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67 private List<Solution> solutions;
70 public void initService() {
74 watcher.watchOn(this.resource.getURL().toURI(), (uri) -> {
75 loadSolutionsCatalogInfo();
78 } catch (IOException | URISyntaxException iox) {
79 log.info("Catalog watcher registration failed for " + this.resource, iox);
82 loadSolutionsCatalogInfo();
85 log.debug("Local CatalogService available");
89 public void cleanupService() {
93 private void loadSolutionsCatalogInfo() {
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);
108 public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
110 log.debug("getSolutions, selector {}", theSelector);
111 return(solutions.stream().filter(ServiceImpl.compileSelector(theSelector)).collect(Collectors.toList()));
115 public Solution getSolution(final String theSolutionId, ServiceContext theContext) throws ServiceException {
117 log.debug("getSolution");
118 return solutions.stream().filter(solution -> {
119 return theSolutionId.equals(solution.getSolutionId());
120 }).findFirst().orElse(null);
124 public Solution putSolution(Solution theSolution, ServiceContext theContext) throws ServiceException {
126 log.trace("putSolution {}", theSolution);
131 public List<MLPSolutionRevision> getSolutionRevisions(final String theSolutionId, ServiceContext theContext) throws ServiceException {
133 log.debug("getSolutionRevisions");
135 Solution solution = getSolution(theSolutionId, theContext);
136 return (solution == null) ? Collections.EMPTY_LIST : (List)solution.getRevisions();
140 public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
141 ServiceContext theContext) throws ServiceException {
143 log.debug("getSolutionRevision");
144 return (SolutionRevision)getSolutionRevisions(theSolutionId, theContext).stream()
145 .filter(rev -> rev.getRevisionId().equals(theRevisionId)).findFirst().orElse(null);
149 public SolutionRevision putSolutionRevision(SolutionRevision theRevision, ServiceContext theContext)
150 throws ServiceException {
151 log.trace("putSolutionRevision {}", theRevision);
156 public List<MLPArtifact> getSolutionRevisionArtifacts(final String theSolutionId, final String theRevisionId,
157 ServiceContext theContext) throws ServiceException {
158 log.debug("getSolutionRevisionArtifacts");
160 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
161 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getArtifacts();
165 public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext)
166 throws ServiceException {
167 log.debug("getSolutionRevisionArtifact");
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;
183 public List<MLPDocument> getSolutionRevisionDocuments(String theSolutionId, String theRevisionId, ServiceContext theContext) throws ServiceException {
184 log.debug("getSolutionRevisionDocuments");
186 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
187 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getDocuments();
191 public Document getSolutionRevisionDocument(String theDocumentId, ServiceContext theContext) throws ServiceException {
192 log.debug("getSolutionRevisionDocument");
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;