1 package org.onap.vid.asdc.local;
3 import com.fasterxml.jackson.core.JsonParseException;
4 import com.fasterxml.jackson.databind.JsonMappingException;
5 import com.fasterxml.jackson.databind.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.exceptions.GenericUncheckedException;
14 import java.io.IOException;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLDecoder;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.UUID;
22 * The Class LocalAsdcClient.
24 public class LocalAsdcClient implements AsdcClient {
27 public static final String SERVICES = "services";
31 private final JSONObject catalog;
36 private final ObjectMapper mapper;
39 * Instantiates a new in local sdc client.
41 * @param builder the builder
43 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
44 catalog = builder.catalog;
45 mapper = builder.mapper;
53 private JSONObject getCatalog() {
62 private ObjectMapper getMapper() {
69 * @param <T> the generic type
70 * @param json the json
71 * @param clazz the clazz
73 * @throws AsdcCatalogException the sdc catalog exception
75 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
77 return getMapper().readValue(json.toString(), clazz);
78 } catch (JsonParseException e) {
79 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
80 } catch (JsonMappingException e) {
81 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
82 } catch (IOException e) {
83 throw new AsdcCatalogException("Failed to get a response from SDC", e);
88 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
90 public Service getService(UUID uuid) throws AsdcCatalogException {
92 JSONObject serviceJsonObject = null;
93 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
95 for (int i = 0; i < categoryJsonArray.length(); i++) {
96 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
97 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
98 serviceJsonObject = jsonServiceObject;
103 if (serviceJsonObject != null)
104 return convert(serviceJsonObject, Service.class);
109 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
111 public Path getServiceToscaModel(UUID serviceUuid) {
113 String toscaModelURL = null;
115 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
117 for (int i = 0; i < categoryJsonArray.length(); i++) {
119 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
120 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
121 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
124 if (toscaModelURL == null) {
127 ClassLoader classLoader = getClass().getClassLoader();
128 File file = new File(classLoader.getResource(toscaModelURL).getFile());
131 //using URLDecoder.decode to convert special characters from %XX to real character
132 //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
133 return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
134 } catch (UnsupportedEncodingException e) {
135 throw new GenericUncheckedException(e);
142 public static class Builder {
147 private JSONObject catalog = new JSONObject()
148 .put("resources", new JSONObject())
149 .put(SERVICES, new JSONObject());
154 private ObjectMapper mapper = new ObjectMapper();
157 * Instantiates a new builder.
165 * @param catalog the catalog
166 * @return the builder
168 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
169 this.catalog = catalog;
176 * @param mapper the mapper
177 * @return the builder
179 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
180 this.mapper = mapper;
187 * @return the in local sdc client
189 public org.onap.vid.asdc.local.LocalAsdcClient build() {
190 return new org.onap.vid.asdc.local.LocalAsdcClient(this);