2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed 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
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.asdc.local;
24 import com.fasterxml.jackson.core.JsonParseException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import io.joshworks.restclient.http.HttpResponse;
29 import java.io.IOException;
30 import java.io.UnsupportedEncodingException;
31 import java.net.URLDecoder;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.UUID;
35 import org.json.JSONArray;
36 import org.json.JSONObject;
37 import org.onap.vid.asdc.AsdcCatalogException;
38 import org.onap.vid.asdc.AsdcClient;
39 import org.onap.vid.asdc.beans.Service;
40 import org.onap.vid.exceptions.GenericUncheckedException;
43 * The Class LocalAsdcClient.
45 public class LocalAsdcClient implements AsdcClient {
48 public static final String SERVICES = "services";
52 private final JSONObject catalog;
57 private final ObjectMapper mapper;
60 * Instantiates a new in local sdc client.
62 * @param builder the builder
64 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
65 catalog = builder.catalog;
66 mapper = builder.mapper;
74 private JSONObject getCatalog() {
83 private ObjectMapper getMapper() {
90 * @param <T> the generic type
91 * @param json the json
92 * @param clazz the clazz
94 * @throws AsdcCatalogException the sdc catalog exception
96 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
98 return getMapper().readValue(json.toString(), clazz);
99 } catch (JsonParseException e) {
100 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
101 } catch (JsonMappingException e) {
102 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
103 } catch (IOException e) {
104 throw new AsdcCatalogException("Failed to get a response from SDC", e);
109 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
111 public Service getService(UUID uuid) throws AsdcCatalogException {
113 JSONObject serviceJsonObject = null;
114 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
116 for (int i = 0; i < categoryJsonArray.length(); i++) {
117 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
118 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
119 serviceJsonObject = jsonServiceObject;
124 if (serviceJsonObject != null)
125 return convert(serviceJsonObject, Service.class);
130 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
132 public Path getServiceToscaModel(UUID serviceUuid) {
134 String toscaModelURL = null;
136 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
138 for (int i = 0; i < categoryJsonArray.length(); i++) {
140 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
141 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
142 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
145 if (toscaModelURL == null) {
148 ClassLoader classLoader = getClass().getClassLoader();
151 File file = new File(classLoader.getResource(toscaModelURL).getFile());
152 //using URLDecoder.decode to convert special characters from %XX to real character
153 //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
154 return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
155 } catch (RuntimeException | UnsupportedEncodingException e) {
156 throw new GenericUncheckedException("Can't find " + toscaModelURL, e);
161 public HttpResponse<String> checkSDCConnectivity() {
162 return HttpResponse.fallback("");
167 public String getBaseUrl(){
174 public static class Builder {
179 private JSONObject catalog = new JSONObject()
180 .put("resources", new JSONObject())
181 .put(SERVICES, new JSONObject());
186 private ObjectMapper mapper = new ObjectMapper();
189 * Instantiates a new builder.
197 * @param catalog the catalog
198 * @return the builder
200 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
201 this.catalog = catalog;
208 * @param mapper the mapper
209 * @return the builder
211 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
212 this.mapper = mapper;
219 * @return the in local sdc client
221 public org.onap.vid.asdc.local.LocalAsdcClient build() {
222 return new org.onap.vid.asdc.local.LocalAsdcClient(this);