1 package org.onap.vid.asdc.local;
3 import org.codehaus.jackson.JsonParseException;
4 import org.codehaus.jackson.map.JsonMappingException;
5 import org.codehaus.jackson.map.ObjectMapper;
6 import org.json.JSONArray;
7 import org.json.JSONObject;
8 import org.onap.vid.asdc.AsdcCatalogException;
9 import org.onap.vid.asdc.AsdcClient;
10 import org.onap.vid.asdc.beans.Service;
11 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
12 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
13 import org.onap.vid.asdc.beans.tosca.ToscaModel;
14 import org.onap.vid.exceptions.GenericUncheckedException;
15 import org.yaml.snakeyaml.Yaml;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLDecoder;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.nio.file.StandardCopyOption;
27 import java.util.UUID;
28 import java.util.zip.ZipFile;
31 * The Class LocalAsdcClient.
33 public class LocalAsdcClient implements AsdcClient {
36 public static final String SERVICES = "services";
40 private final JSONObject catalog;
45 private final ObjectMapper mapper;
48 * Instantiates a new in local sdc client.
50 * @param builder the builder
52 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
53 catalog = builder.catalog;
54 mapper = builder.mapper;
62 private JSONObject getCatalog() {
71 private ObjectMapper getMapper() {
78 * @param <T> the generic type
79 * @param json the json
80 * @param clazz the clazz
82 * @throws AsdcCatalogException the sdc catalog exception
84 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
86 return getMapper().readValue(json.toString(), clazz);
87 } catch (JsonParseException e) {
88 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
89 } catch (JsonMappingException e) {
90 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
91 } catch (IOException e) {
92 throw new AsdcCatalogException("Failed to get a response from SDC", e);
97 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
99 public Service getService(UUID uuid) throws AsdcCatalogException {
101 JSONObject serviceJsonObject = null;
102 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
104 for (int i = 0; i < categoryJsonArray.length(); i++) {
105 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
106 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
107 serviceJsonObject = jsonServiceObject;
112 if (serviceJsonObject != null)
113 return convert(serviceJsonObject, Service.class);
118 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
120 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
122 String toscaModelURL = null;
124 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
126 for (int i = 0; i < categoryJsonArray.length(); i++) {
128 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
129 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
130 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
133 if (toscaModelURL == null) {
136 ClassLoader classLoader = getClass().getClassLoader();
137 File file = new File(classLoader.getResource(toscaModelURL).getFile());
140 //using URLDecoder.decode to convert special characters from %XX to real character
141 //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
142 return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
143 } catch (UnsupportedEncodingException e) {
144 throw new GenericUncheckedException(e);
149 * Gets the tosca model.
151 * @param csarInputStream the csar input stream
152 * @return the tosca model
153 * @throws AsdcCatalogException the asdc catalog exception
155 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
159 csarFile = Files.createTempFile("csar", ".zip");
160 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
161 } catch (IOException e) {
162 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
165 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
167 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
168 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
169 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
170 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
172 final Yaml yaml = new Yaml();
173 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
175 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
177 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
178 for (Map.Entry<String, Map<String, String>> entry : imports.entrySet()) {
179 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
180 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
181 csarBuilder.addVnf(childModel);
185 return csarBuilder.build();
186 } catch (IOException e) {
187 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
194 public static class Builder {
199 private JSONObject catalog = new JSONObject()
200 .put("resources", new JSONObject())
201 .put(SERVICES, new JSONObject());
206 private ObjectMapper mapper = new ObjectMapper();
209 * Instantiates a new builder.
217 * @param catalog the catalog
218 * @return the builder
220 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
221 this.catalog = catalog;
228 * @param mapper the mapper
229 * @return the builder
231 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
232 this.mapper = mapper;
239 * @return the in local sdc client
241 public org.onap.vid.asdc.local.LocalAsdcClient build() {
242 return new org.onap.vid.asdc.local.LocalAsdcClient(this);