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.InputStream;
31 import java.io.UnsupportedEncodingException;
32 import java.net.URLDecoder;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.UUID;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.onap.vid.aai.HttpResponseWithRequestInfo;
39 import org.onap.vid.asdc.AsdcCatalogException;
40 import org.onap.vid.asdc.AsdcClient;
41 import org.onap.vid.asdc.beans.Service;
42 import org.onap.vid.exceptions.GenericUncheckedException;
45 * The Class LocalAsdcClient.
47 public class LocalAsdcClient implements AsdcClient {
50 public static final String SERVICES = "services";
54 private final JSONObject catalog;
59 private final ObjectMapper mapper;
62 * Instantiates a new in local sdc client.
64 * @param builder the builder
66 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
67 catalog = builder.catalog;
68 mapper = builder.mapper;
76 private JSONObject getCatalog() {
85 private ObjectMapper getMapper() {
92 * @param <T> the generic type
93 * @param json the json
94 * @param clazz the clazz
96 * @throws AsdcCatalogException the sdc catalog exception
98 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
100 return getMapper().readValue(json.toString(), clazz);
101 } catch (JsonParseException e) {
102 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
103 } catch (JsonMappingException e) {
104 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
105 } catch (IOException e) {
106 throw new AsdcCatalogException("Failed to get a response from SDC", e);
111 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
113 public Service getService(UUID uuid) throws AsdcCatalogException {
115 JSONObject serviceJsonObject = null;
116 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
118 for (int i = 0; i < categoryJsonArray.length(); i++) {
119 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
120 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
121 serviceJsonObject = jsonServiceObject;
126 if (serviceJsonObject != null)
127 return convert(serviceJsonObject, Service.class);
132 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
134 public Path getServiceToscaModel(UUID serviceUuid) {
136 String toscaModelURL = null;
138 final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
140 for (int i = 0; i < categoryJsonArray.length(); i++) {
142 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
143 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
144 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
147 if (toscaModelURL == null) {
150 ClassLoader classLoader = getClass().getClassLoader();
153 File file = new File(classLoader.getResource(toscaModelURL).getFile());
154 //using URLDecoder.decode to convert special characters from %XX to real character
155 //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
156 return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
157 } catch (RuntimeException | UnsupportedEncodingException e) {
158 throw new GenericUncheckedException("Can't find " + toscaModelURL, e);
163 public HttpResponse<String> checkSDCConnectivity() {
164 return HttpResponse.fallback("");
168 public HttpResponseWithRequestInfo<InputStream> getServiceInputStream(UUID serviceUuid, boolean warpException) {
173 public String getBaseUrl(){
180 public static class Builder {
185 private JSONObject catalog = new JSONObject()
186 .put("resources", new JSONObject())
187 .put(SERVICES, new JSONObject());
192 private ObjectMapper mapper = new ObjectMapper();
195 * Instantiates a new builder.
203 * @param catalog the catalog
204 * @return the builder
206 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
207 this.catalog = catalog;
214 * @param mapper the mapper
215 * @return the builder
217 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
218 this.mapper = mapper;
225 * @return the in local sdc client
227 public org.onap.vid.asdc.local.LocalAsdcClient build() {
228 return new org.onap.vid.asdc.local.LocalAsdcClient(this);