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;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.onap.vid.asdc.AsdcCatalogException;
31 import org.onap.vid.asdc.AsdcClient;
32 import org.onap.vid.asdc.beans.Service;
33 import org.onap.vid.exceptions.GenericUncheckedException;
36 import java.io.IOException;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URLDecoder;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.UUID;
44 * The Class LocalAsdcClient.
46 public class LocalAsdcClient implements AsdcClient {
49 public static final String SERVICES = "services";
53 private final JSONObject catalog;
58 private final ObjectMapper mapper;
61 * Instantiates a new in local sdc client.
63 * @param builder the builder
65 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
66 catalog = builder.catalog;
67 mapper = builder.mapper;
75 private JSONObject getCatalog() {
84 private ObjectMapper getMapper() {
91 * @param <T> the generic type
92 * @param json the json
93 * @param clazz the clazz
95 * @throws AsdcCatalogException the sdc catalog exception
97 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
99 return getMapper().readValue(json.toString(), clazz);
100 } catch (JsonParseException e) {
101 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
102 } catch (JsonMappingException e) {
103 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
104 } catch (IOException e) {
105 throw new AsdcCatalogException("Failed to get a response from SDC", e);
110 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
112 public Service getService(UUID uuid) throws AsdcCatalogException {
114 JSONObject serviceJsonObject = null;
115 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
117 for (int i = 0; i < categoryJsonArray.length(); i++) {
118 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
119 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
120 serviceJsonObject = jsonServiceObject;
125 if (serviceJsonObject != null)
126 return convert(serviceJsonObject, Service.class);
131 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
133 public Path getServiceToscaModel(UUID serviceUuid) {
135 String toscaModelURL = null;
137 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
139 for (int i = 0; i < categoryJsonArray.length(); i++) {
141 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
142 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
143 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
146 if (toscaModelURL == null) {
149 ClassLoader classLoader = getClass().getClassLoader();
150 File file = new File(classLoader.getResource(toscaModelURL).getFile());
153 //using URLDecoder.decode to convert special characters from %XX to real character
154 //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
155 return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
156 } catch (UnsupportedEncodingException e) {
157 throw new GenericUncheckedException(e);
162 public HttpResponse<String> checkSDCConnectivity() {
163 return HttpResponse.fallback("");
168 public String getBaseUrl(){
175 public static class Builder {
180 private JSONObject catalog = new JSONObject()
181 .put("resources", new JSONObject())
182 .put(SERVICES, new JSONObject());
187 private ObjectMapper mapper = new ObjectMapper();
190 * Instantiates a new builder.
198 * @param catalog the catalog
199 * @return the builder
201 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
202 this.catalog = catalog;
209 * @param mapper the mapper
210 * @return the builder
212 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
213 this.mapper = mapper;
220 * @return the in local sdc client
222 public org.onap.vid.asdc.local.LocalAsdcClient build() {
223 return new org.onap.vid.asdc.local.LocalAsdcClient(this);