7cfd094df4d258902cafd5d87ed6e64bba11c8ad
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / local / LocalAsdcClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.vid.asdc.local;
23
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;
34
35 import java.io.File;
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;
42
43 /**
44  * The Class LocalAsdcClient.
45  */
46 public class LocalAsdcClient implements AsdcClient {
47
48
49     public static final String SERVICES = "services";
50     /**
51      * The catalog.
52      */
53     private final JSONObject catalog;
54
55     /**
56      * The mapper.
57      */
58     private final ObjectMapper mapper;
59
60     /**
61      * Instantiates a new in local sdc client.
62      *
63      * @param builder the builder
64      */
65     public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
66         catalog = builder.catalog;
67         mapper = builder.mapper;
68     }
69
70     /**
71      * Gets the catalog.
72      *
73      * @return the catalog
74      */
75     private JSONObject getCatalog() {
76         return catalog;
77     }
78
79     /**
80      * Gets the mapper.
81      *
82      * @return the mapper
83      */
84     private ObjectMapper getMapper() {
85         return mapper;
86     }
87
88     /**
89      * Convert.
90      *
91      * @param <T>   the generic type
92      * @param json  the json
93      * @param clazz the clazz
94      * @return the t
95      * @throws AsdcCatalogException the sdc catalog exception
96      */
97     private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
98         try {
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);
106         }
107     }
108
109     /* (non-Javadoc)
110      * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
111      */
112     public Service getService(UUID uuid) throws AsdcCatalogException {
113
114         JSONObject serviceJsonObject = null;
115         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
116
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;
121                 break;
122             }
123         }
124
125         if (serviceJsonObject != null)
126             return convert(serviceJsonObject, Service.class);
127         else return null;
128     }
129
130     /* (non-Javadoc)
131      * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
132      */
133     public Path getServiceToscaModel(UUID serviceUuid) {
134
135         String toscaModelURL = null;
136
137         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
138
139         for (int i = 0; i < categoryJsonArray.length(); i++) {
140
141             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
142             if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
143                 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
144             }
145         }
146         if (toscaModelURL == null) {
147             return null;
148         }
149         ClassLoader classLoader = getClass().getClassLoader();
150         File file = new File(classLoader.getResource(toscaModelURL).getFile());
151
152         try {
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);
158         }
159     }
160
161     @Override
162     public HttpResponse<String> checkSDCConnectivity() {
163         return HttpResponse.fallback("");
164     }
165
166
167     @Override
168     public String getBaseUrl(){
169         return "";
170     }
171
172     /**
173      * The Class Builder.
174      */
175     public static class Builder {
176
177         /**
178          * The catalog.
179          */
180         private JSONObject catalog = new JSONObject()
181                 .put("resources", new JSONObject())
182                 .put(SERVICES, new JSONObject());
183
184         /**
185          * The mapper.
186          */
187         private ObjectMapper mapper = new ObjectMapper();
188
189         /**
190          * Instantiates a new builder.
191          */
192         public Builder() {
193         }
194
195         /**
196          * Catalog.
197          *
198          * @param catalog the catalog
199          * @return the builder
200          */
201         public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
202             this.catalog = catalog;
203             return this;
204         }
205
206         /**
207          * Mapper.
208          *
209          * @param mapper the mapper
210          * @return the builder
211          */
212         public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
213             this.mapper = mapper;
214             return this;
215         }
216
217         /**
218          * Builds the.
219          *
220          * @return the in local sdc client
221          */
222         public org.onap.vid.asdc.local.LocalAsdcClient build() {
223             return new org.onap.vid.asdc.local.LocalAsdcClient(this);
224         }
225     }
226
227 }