e4591f861fb825905866fd4b851f1b069c4f276f
[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.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.Collections;
38 import java.util.stream.Collectors;
39
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42
43 import com.fasterxml.jackson.annotation.JsonIgnore;
44 import com.fasterxml.jackson.annotation.JsonProperty;
45 import com.fasterxml.jackson.databind.MappingIterator;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.ObjectReader;
48
49 import org.apache.commons.io.FileUtils;
50
51 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
52 import org.acumos.federation.gateway.service.CatalogService;
53 import org.acumos.federation.gateway.service.ServiceContext;
54 import org.acumos.federation.gateway.service.ServiceException;
55 import org.acumos.federation.gateway.util.Utils;
56
57 import org.acumos.federation.gateway.cds.Solution;
58 import org.acumos.federation.gateway.cds.SolutionRevision;
59 import org.acumos.federation.gateway.cds.Artifact;
60 import org.acumos.federation.gateway.cds.Mapper;
61
62 import org.acumos.cds.AccessTypeCode;
63 import org.acumos.cds.ValidationStatusCode;
64 import org.acumos.cds.client.ICommonDataServiceRestClient;
65 import org.acumos.cds.domain.MLPArtifact;
66 import org.acumos.cds.domain.MLPSolution;
67 import org.acumos.cds.domain.MLPSolutionRevision;
68 import org.acumos.cds.transport.RestPageResponse;
69
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.beans.factory.BeanInitializationException;
72 import org.springframework.core.env.Environment;
73 import org.springframework.core.io.InputStreamResource;
74 import org.springframework.stereotype.Service;
75 import org.springframework.context.annotation.Conditional;
76 import org.springframework.boot.context.properties.ConfigurationProperties;
77
78
79 /**
80  * 
81  *
82  */
83 @Service
84 @ConfigurationProperties(prefix = "catalogLocal")
85 public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements CatalogService {
86
87         private static final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(CatalogServiceLocalImpl.class.getName());
88
89         private List<Solution> solutions;
90
91         @PostConstruct
92         public void initService() {
93
94                 checkResource();
95                 try {
96                         watcher.watchOn(this.resource.getURL().toURI(), (uri) -> {
97                                 loadSolutionsCatalogInfo();
98                         });
99
100                 } catch (IOException | URISyntaxException iox) {
101                         log.info(EELFLoggerDelegate.errorLogger, "Catalog watcher registration failed for " + this.resource, iox);
102                 }
103
104                 loadSolutionsCatalogInfo();
105
106                 // Done
107                 log.debug(EELFLoggerDelegate.debugLogger, "Local CatalogService available");
108         }
109
110         @PreDestroy
111         public void cleanupService() {
112         }
113
114         /** */
115         private void loadSolutionsCatalogInfo() {
116                 synchronized (this) {
117                         try {
118                                 ObjectReader objectReader = Mapper.build().reader(Solution.class);
119                                 MappingIterator objectIterator = objectReader.readValues(this.resource.getURL());
120                                 this.solutions = objectIterator.readAll();
121                                 log.info(EELFLoggerDelegate.debugLogger, "loaded " + this.solutions.size() + " solutions");
122                         } catch (Exception x) {
123                                 throw new BeanInitializationException("Failed to load solutions catalog from " + this.resource, x);
124                         }
125                 }
126         }
127
128         @Override
129         public List<MLPSolution> getSolutions(Map<String, ?> theSelector, ServiceContext theContext) throws ServiceException {
130
131                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutions, selector {}", theSelector);
132
133                 return solutions.stream()
134                         .filter(solution -> ServiceImpl.isSelectable(solution, theSelector))
135                         .collect(Collectors.toList());
136         }
137
138         @Override
139         public Solution getSolution(final String theSolutionId, ServiceContext theContext) throws ServiceException {
140
141                 log.debug(EELFLoggerDelegate.debugLogger, "getSolution");
142                 return solutions.stream().filter(solution -> {
143                         return theSolutionId.equals(solution.getSolutionId());
144                 }).findFirst().orElse(null);
145         }
146
147         @Override
148         public List<MLPSolutionRevision> getSolutionRevisions(final String theSolutionId, ServiceContext theContext) throws ServiceException {
149
150                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisions");
151
152                 Solution solution = getSolution(theSolutionId, theContext);
153                 return (solution == null) ? Collections.EMPTY_LIST : (List)solution.getRevisions();
154         }
155
156         @Override
157         public SolutionRevision getSolutionRevision(String theSolutionId, String theRevisionId,
158                         ServiceContext theContext) throws ServiceException  {
159
160                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevision");
161                 return (SolutionRevision)getSolutionRevisions(theSolutionId, theContext).stream()
162                                                 .filter(rev -> rev.getRevisionId().equals(theRevisionId)).findFirst().orElse(null);
163         }
164
165         @Override
166         public List<MLPArtifact> getSolutionRevisionArtifacts(final String theSolutionId, final String theRevisionId,
167                         ServiceContext theContext) throws ServiceException {
168                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifacts");
169
170                 SolutionRevision revision = getSolutionRevision(theSolutionId, theRevisionId, theContext);
171                 return (revision == null) ? Collections.EMPTY_LIST : (List)revision.getArtifacts();
172         }
173
174         @Override
175         public Artifact getSolutionRevisionArtifact(String theArtifactId, ServiceContext theContext) 
176                                                                                                                                                                                                                                                                                                                                                                                                 throws ServiceException {
177                 log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifact");
178                 // cumbersome
179                 for (Solution solution : this.solutions) {
180                         for (MLPSolutionRevision revision : solution.getRevisions()) {
181                                 for (MLPArtifact artifact : ((SolutionRevision)revision).getArtifacts()) {
182                                         if (artifact.getArtifactId().equals(theArtifactId)) {
183                                                 return (Artifact)artifact;
184                                         }
185                                 }
186                         }
187                 }
188
189                 return null;
190         }
191 }