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.Collections;
27 import java.util.List;
29 import java.util.stream.Collectors;
31 import javax.annotation.PostConstruct;
32 import javax.annotation.PreDestroy;
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;
52 import com.fasterxml.jackson.databind.MappingIterator;
53 import com.fasterxml.jackson.databind.ObjectReader;
60 @ConfigurationProperties(prefix = "catalog-local")
61 public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements CatalogService {
63 private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65 private List<Solution> solutions;
68 public void initService() {
72 watcher.watchOn(this.resource.getURL().toURI(), (uri) -> {
73 loadSolutionsCatalogInfo();
76 } catch (IOException | URISyntaxException iox) {
77 log.info("Catalog watcher registration failed for " + this.resource, iox);
80 loadSolutionsCatalogInfo();
83 log.debug("Local CatalogService available");
87 public void cleanupService() {
91 private void loadSolutionsCatalogInfo() {
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);
106 public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
108 log.debug("getSolutions, selector {}", theSelector);
110 return solutions.stream()
111 .filter(solution -> ServiceImpl.isSelectable(solution, theSelector))
112 .collect(Collectors.toList());
116 public Solution getSolution(final String theSolutionId, ServiceContext theContext) throws ServiceException {
118 log.debug("getSolution");
119 return solutions.stream().filter(solution -> {
120 return theSolutionId.equals(solution.getSolutionId());
121 }).findFirst().orElse(null);
125 public Solution putSolution(Solution theSolution, ServiceContext theContext) throws ServiceException {
127 log.trace("putSolution {}", theSolution);
132 public List<MLPSolutionRevision> getSolutionRevisions(final String theSolutionId, ServiceContext theContext) throws ServiceException {
134 log.debug("getSolutionRevisions");
136 Solution solution = getSolution(theSolutionId, theContext);
137 return (solution == null) ? Collections.EMPTY_LIST : (List)solution.getRevisions();
141 public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
142 ServiceContext theContext) throws ServiceException {
144 log.debug("getSolutionRevision");
145 return (SolutionRevision)getSolutionRevisions(theSolutionId, theContext).stream()
146 .filter(rev -> rev.getRevisionId().equals(theRevisionId)).findFirst().orElse(null);
150 public SolutionRevision putSolutionRevision(SolutionRevision theRevision, ServiceContext theContext)
151 throws ServiceException {
152 log.trace("putSolutionRevision {}", theRevision);
157 public List<MLPArtifact> getSolutionRevisionArtifacts(final String theSolutionId, final String theRevisionId,
158 ServiceContext theContext) throws ServiceException {
159 log.debug("getSolutionRevisionArtifacts");
161 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
162 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getArtifacts();
166 public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext)
167 throws ServiceException {
168 log.debug("getSolutionRevisionArtifact");
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;
184 public List<MLPDocument> getSolutionRevisionDocuments(String theSolutionId, String theRevisionId, ServiceContext theContext) throws ServiceException {
185 log.debug("getSolutionRevisionDocuments");
187 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
188 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getDocuments();
192 public Document getSolutionRevisionDocument(String theDocumentId, ServiceContext theContext) throws ServiceException {
193 log.debug("getSolutionRevisionDocument");
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;