2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.vid.asdc.memory;
\r
23 import java.io.IOException;
\r
24 import java.io.InputStream;
\r
25 import java.nio.file.Files;
\r
26 import java.nio.file.Path;
\r
27 import java.nio.file.StandardCopyOption;
\r
28 import java.util.Collection;
\r
29 import java.util.LinkedList;
\r
30 import java.util.Map;
\r
31 import java.util.Map.Entry;
\r
32 import java.util.UUID;
\r
33 import java.util.zip.ZipFile;
\r
35 import org.codehaus.jackson.JsonParseException;
\r
36 import org.codehaus.jackson.map.JsonMappingException;
\r
37 import org.codehaus.jackson.map.ObjectMapper;
\r
38 import org.json.JSONArray;
\r
39 import org.json.JSONObject;
\r
40 import org.yaml.snakeyaml.Yaml;
\r
42 import org.openecomp.vid.asdc.AsdcCatalogException;
\r
43 import org.openecomp.vid.asdc.AsdcClient;
\r
44 import org.openecomp.vid.asdc.beans.Artifact;
\r
45 import org.openecomp.vid.asdc.beans.Resource;
\r
46 import org.openecomp.vid.asdc.beans.Service;
\r
47 import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
\r
48 import org.openecomp.vid.asdc.beans.tosca.ToscaMeta;
\r
49 import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
\r
52 * The Class InMemoryAsdcClient.
\r
54 public class InMemoryAsdcClient implements AsdcClient {
\r
57 private final JSONObject catalog;
\r
60 private final ObjectMapper mapper;
\r
63 * The Class Builder.
\r
65 public static class Builder {
\r
68 private JSONObject catalog = new JSONObject()
\r
69 .put("resources", new JSONObject())
\r
70 .put("services", new JSONObject());
\r
73 private ObjectMapper mapper = new ObjectMapper();
\r
76 * Instantiates a new builder.
\r
83 * @param catalog the catalog
\r
84 * @return the builder
\r
86 public Builder catalog(JSONObject catalog) {
\r
87 this.catalog = catalog;
\r
94 * @param mapper the mapper
\r
95 * @return the builder
\r
97 public Builder mapper(ObjectMapper mapper) {
\r
98 this.mapper = mapper;
\r
105 * @return the in memory sdc client
\r
107 public InMemoryAsdcClient build() {
\r
108 return new InMemoryAsdcClient(this);
\r
113 * Instantiates a new in memory sdc client.
\r
115 * @param builder the builder
\r
117 private InMemoryAsdcClient(Builder builder) {
\r
118 catalog = builder.catalog;
\r
119 mapper = builder.mapper;
\r
123 * Gets the catalog.
\r
125 * @return the catalog
\r
127 private JSONObject getCatalog() {
\r
134 * @return the mapper
\r
136 private ObjectMapper getMapper() {
\r
143 * @param <T> the generic type
\r
144 * @param json the json
\r
145 * @param clazz the clazz
\r
147 * @throws AsdcCatalogException the sdc catalog exception
\r
149 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
\r
151 return getMapper().readValue(json.toString(), clazz);
\r
152 } catch (JsonParseException e) {
\r
153 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
\r
154 } catch (JsonMappingException e) {
\r
155 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
\r
156 } catch (IOException e) {
\r
157 throw new AsdcCatalogException("Failed to get a response from SDC", e);
\r
162 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
\r
164 public Resource getResource(UUID uuid) throws AsdcCatalogException {
\r
165 final JSONObject resource = getCatalog().getJSONObject("resources")
\r
166 .getJSONObject(uuid.toString());
\r
167 return convert(resource, Resource.class);
\r
171 * @see org.openecomp.vid.asdc.AsdcClient#getResources()
\r
173 public Collection<Resource> getResources() throws AsdcCatalogException {
\r
174 final Collection<Resource> resources = new LinkedList<Resource> ();
\r
176 for (String key : getCatalog().getJSONObject("resources").keySet()) {
\r
177 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
\r
178 final Resource resource = convert(json, Resource.class);
\r
179 resources.add(resource);
\r
186 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
\r
188 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
\r
189 final Collection<Resource> resources = new LinkedList<Resource> ();
\r
191 for (String key : getCatalog().getJSONObject("resources").keySet()) {
\r
192 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
\r
194 boolean filterMatch = true;
\r
196 for (Entry<String, String[]> entry : filter.entrySet()) {
\r
197 for (int i = 0; i < entry.getValue().length; i++) {
\r
198 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
\r
199 filterMatch = false;
\r
205 if (filterMatch) resources.add(convert(json, Resource.class));
\r
212 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
\r
214 public Service getService(UUID uuid) throws AsdcCatalogException {
\r
215 final JSONObject service = getCatalog().getJSONObject("services")
\r
216 .getJSONObject(uuid.toString());
\r
217 return convert(service, Service.class);
\r
221 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
\r
223 public Collection<Service> getServices() throws AsdcCatalogException {
\r
224 final Collection<Service> services = new LinkedList<Service> ();
\r
226 for (String key : getCatalog().getJSONObject("services").keySet()) {
\r
227 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
\r
228 final Service service = convert(json, Service.class);
\r
229 services.add(service);
\r
236 * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
\r
238 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
\r
239 final Collection<Service> services = new LinkedList<Service> ();
\r
241 for (String key : getCatalog().getJSONObject("services").keySet()) {
\r
242 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
\r
244 boolean filterMatch = true;
\r
246 for (Entry<String, String[]> entry : filter.entrySet()) {
\r
247 for (int i = 0; i < entry.getValue().length; i++) {
\r
248 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
\r
249 filterMatch = false;
\r
255 if (filterMatch) services.add(convert(json, Service.class));
\r
262 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
\r
264 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
265 final JSONArray artifacts = getCatalog().getJSONObject("resources")
\r
266 .getJSONObject(resourceUuid.toString())
\r
267 .getJSONArray("artifacts");
\r
269 for (int i = 0; i < artifacts.length(); i++) {
\r
270 final JSONObject artifact = artifacts.getJSONObject(i);
\r
272 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
\r
273 return convert(artifact, Artifact.class);
\r
281 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
\r
283 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
284 final JSONArray artifacts = getCatalog().getJSONObject("services")
\r
285 .getJSONObject(serviceUuid.toString())
\r
286 .getJSONArray("artifacts");
\r
288 for (int i = 0; i < artifacts.length(); i++) {
\r
289 final JSONObject artifact = artifacts.getJSONObject(i);
\r
291 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
\r
292 return convert(artifact, Artifact.class);
\r
300 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
\r
302 public ToscaCsar getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
\r
303 final String toscaModelURL = getCatalog().getJSONObject("resources")
\r
304 .getJSONObject(resourceUuid.toString())
\r
305 .getString("toscaModelURL");
\r
308 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
\r
310 if (toscaModelStream == null) return null;
\r
312 return getToscaModel(toscaModelStream);
\r
316 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
\r
318 public ToscaCsar getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
\r
319 final String toscaModelURL = getCatalog().getJSONObject("services")
\r
320 .getJSONObject(serviceUuid.toString())
\r
321 .getString("toscaModelURL");
\r
323 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
\r
325 if (toscaModelStream == null) return null;
\r
327 return getToscaModel(toscaModelStream);
\r
331 * Gets the tosca model.
\r
333 * @param csarInputStream the csar input stream
\r
334 * @return the tosca model
\r
335 * @throws AsdcCatalogException the asdc catalog exception
\r
337 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
\r
338 final Path csarFile;
\r
341 csarFile = Files.createTempFile("csar", ".zip");
\r
342 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
\r
343 } catch (IOException e) {
\r
344 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
\r
347 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
\r
349 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
\r
350 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
\r
351 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
\r
352 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
\r
354 final Yaml yaml = new Yaml();
\r
355 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
\r
357 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
\r
359 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
\r
360 for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
\r
361 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
\r
362 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
\r
363 csarBuilder.addVnf(childModel);
\r
367 return csarBuilder.build();
\r
368 } catch (IOException e) {
\r
369 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
\r