2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License 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.onap.vid.asdc.memory;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.StandardCopyOption;
28 import java.util.Collection;
29 import java.util.LinkedList;
31 import java.util.Map.Entry;
32 import java.util.UUID;
33 import java.util.zip.ZipFile;
35 import org.codehaus.jackson.JsonParseException;
36 import org.codehaus.jackson.map.JsonMappingException;
37 import org.codehaus.jackson.map.ObjectMapper;
38 import org.json.JSONArray;
39 import org.json.JSONObject;
40 import org.yaml.snakeyaml.Yaml;
42 import org.onap.vid.asdc.AsdcCatalogException;
43 import org.onap.vid.asdc.AsdcClient;
44 import org.onap.vid.asdc.beans.Artifact;
45 import org.onap.vid.asdc.beans.Resource;
46 import org.onap.vid.asdc.beans.Service;
47 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
48 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
49 import org.onap.vid.asdc.beans.tosca.ToscaModel;
52 * The Class InMemoryAsdcClient.
54 public class InMemoryAsdcClient implements AsdcClient {
57 private final JSONObject catalog;
60 private final ObjectMapper mapper;
65 public static class Builder {
68 private JSONObject catalog = new JSONObject()
69 .put("resources", new JSONObject())
70 .put("services", new JSONObject());
73 private ObjectMapper mapper = new ObjectMapper();
76 * Instantiates a new builder.
83 * @param catalog the catalog
86 public Builder catalog(JSONObject catalog) {
87 this.catalog = catalog;
94 * @param mapper the mapper
97 public Builder mapper(ObjectMapper mapper) {
105 * @return the in memory sdc client
107 public InMemoryAsdcClient build() {
108 return new InMemoryAsdcClient(this);
113 * Instantiates a new in memory sdc client.
115 * @param builder the builder
117 private InMemoryAsdcClient(Builder builder) {
118 catalog = builder.catalog;
119 mapper = builder.mapper;
125 * @return the catalog
127 private JSONObject getCatalog() {
136 private ObjectMapper getMapper() {
143 * @param <T> the generic type
144 * @param json the json
145 * @param clazz the clazz
147 * @throws AsdcCatalogException the sdc catalog exception
149 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
151 return getMapper().readValue(json.toString(), clazz);
152 } catch (JsonParseException e) {
153 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
154 } catch (JsonMappingException e) {
155 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
156 } catch (IOException e) {
157 throw new AsdcCatalogException("Failed to get a response from SDC", e);
162 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
164 public Resource getResource(UUID uuid) throws AsdcCatalogException {
165 final JSONObject resource = getCatalog().getJSONObject("resources")
166 .getJSONObject(uuid.toString());
167 return convert(resource, Resource.class);
172 public Collection<Resource> getResources() throws AsdcCatalogException {
173 final Collection<Resource> resources = new LinkedList<Resource> ();
175 for (String key : getCatalog().getJSONObject("resources").keySet()) {
176 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
177 final Resource resource = convert(json, Resource.class);
178 resources.add(resource);
185 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
187 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
188 final Collection<Resource> resources = new LinkedList<Resource> ();
190 for (String key : getCatalog().getJSONObject("resources").keySet()) {
191 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
193 boolean filterMatch = true;
195 for (Entry<String, String[]> entry : filter.entrySet()) {
196 for (int i = 0; i < entry.getValue().length; i++) {
197 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
204 if (filterMatch) resources.add(convert(json, Resource.class));
211 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
213 public Service getService(UUID uuid) throws AsdcCatalogException {
214 final JSONObject service = getCatalog().getJSONObject("services")
215 .getJSONObject(uuid.toString());
216 return convert(service, Service.class);
220 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
222 public Collection<Service> getServices() throws AsdcCatalogException {
223 final Collection<Service> services = new LinkedList<Service> ();
225 for (String key : getCatalog().getJSONObject("services").keySet()) {
226 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
227 final Service service = convert(json, Service.class);
228 services.add(service);
235 * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
237 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
238 final Collection<Service> services = new LinkedList<Service> ();
240 for (String key : getCatalog().getJSONObject("services").keySet()) {
241 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
243 boolean filterMatch = true;
245 for (Entry<String, String[]> entry : filter.entrySet()) {
246 for (int i = 0; i < entry.getValue().length; i++) {
247 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
254 if (filterMatch) services.add(convert(json, Service.class));
261 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
263 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
264 final JSONArray artifacts = getCatalog().getJSONObject("resources")
265 .getJSONObject(resourceUuid.toString())
266 .getJSONArray("artifacts");
268 for (int i = 0; i < artifacts.length(); i++) {
269 final JSONObject artifact = artifacts.getJSONObject(i);
271 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
272 return convert(artifact, Artifact.class);
280 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
282 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
283 final JSONArray artifacts = getCatalog().getJSONObject("services")
284 .getJSONObject(serviceUuid.toString())
285 .getJSONArray("artifacts");
287 for (int i = 0; i < artifacts.length(); i++) {
288 final JSONObject artifact = artifacts.getJSONObject(i);
290 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
291 return convert(artifact, Artifact.class);
299 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
301 public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
302 final String toscaModelURL = getCatalog().getJSONObject("resources")
303 .getJSONObject(resourceUuid.toString())
304 .getString("toscaModelURL");
307 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
309 if (toscaModelStream == null) return null;
311 return null;//getToscaModel(toscaModelStream);
315 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
317 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
318 final String toscaModelURL = getCatalog().getJSONObject("services")
319 .getJSONObject(serviceUuid.toString())
320 .getString("toscaModelURL");
322 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
324 if (toscaModelStream == null) return null;
326 return null;//getToscaModel(toscaModelStream);
330 * Gets the tosca model.
332 * @param csarInputStream the csar input stream
333 * @return the tosca model
334 * @throws AsdcCatalogException the asdc catalog exception
336 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
340 csarFile = Files.createTempFile("csar", ".zip");
341 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
342 } catch (IOException e) {
343 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
346 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
348 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
349 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
350 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
351 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
353 final Yaml yaml = new Yaml();
354 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
356 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
358 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
359 for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
360 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
361 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
362 csarBuilder.addVnf(childModel);
366 return csarBuilder.build();
367 } catch (IOException e) {
368 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);