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