Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / local / LocalAsdcClient.java
1 package org.onap.vid.asdc.local;
2
3 import com.fasterxml.jackson.core.JsonParseException;
4 import com.fasterxml.jackson.databind.JsonMappingException;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import org.json.JSONArray;
7 import org.json.JSONObject;
8 import org.onap.vid.asdc.AsdcCatalogException;
9 import org.onap.vid.asdc.AsdcClient;
10 import org.onap.vid.asdc.beans.Service;
11 import org.onap.vid.exceptions.GenericUncheckedException;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URLDecoder;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.UUID;
20
21 /**
22  * The Class LocalAsdcClient.
23  */
24 public class LocalAsdcClient implements AsdcClient {
25
26
27     public static final String SERVICES = "services";
28     /**
29      * The catalog.
30      */
31     private final JSONObject catalog;
32
33     /**
34      * The mapper.
35      */
36     private final ObjectMapper mapper;
37
38     /**
39      * Instantiates a new in local sdc client.
40      *
41      * @param builder the builder
42      */
43     public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
44         catalog = builder.catalog;
45         mapper = builder.mapper;
46     }
47
48     /**
49      * Gets the catalog.
50      *
51      * @return the catalog
52      */
53     private JSONObject getCatalog() {
54         return catalog;
55     }
56
57     /**
58      * Gets the mapper.
59      *
60      * @return the mapper
61      */
62     private ObjectMapper getMapper() {
63         return mapper;
64     }
65
66     /**
67      * Convert.
68      *
69      * @param <T>   the generic type
70      * @param json  the json
71      * @param clazz the clazz
72      * @return the t
73      * @throws AsdcCatalogException the sdc catalog exception
74      */
75     private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
76         try {
77             return getMapper().readValue(json.toString(), clazz);
78         } catch (JsonParseException e) {
79             throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
80         } catch (JsonMappingException e) {
81             throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
82         } catch (IOException e) {
83             throw new AsdcCatalogException("Failed to get a response from SDC", e);
84         }
85     }
86
87     /* (non-Javadoc)
88      * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
89      */
90     public Service getService(UUID uuid) throws AsdcCatalogException {
91
92         JSONObject serviceJsonObject = null;
93         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
94
95         for (int i = 0; i < categoryJsonArray.length(); i++) {
96             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
97             if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
98                 serviceJsonObject = jsonServiceObject;
99                 break;
100             }
101         }
102
103         if (serviceJsonObject != null)
104             return convert(serviceJsonObject, Service.class);
105         else return null;
106     }
107
108     /* (non-Javadoc)
109      * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
110      */
111     public Path getServiceToscaModel(UUID serviceUuid) {
112
113         String toscaModelURL = null;
114
115         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
116
117         for (int i = 0; i < categoryJsonArray.length(); i++) {
118
119             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
120             if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
121                 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
122             }
123         }
124         if (toscaModelURL == null) {
125             return null;
126         }
127         ClassLoader classLoader = getClass().getClassLoader();
128         File file = new File(classLoader.getResource(toscaModelURL).getFile());
129
130         try {
131             //using URLDecoder.decode to convert special characters from %XX to real character
132             //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
133             return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
134         } catch (UnsupportedEncodingException e) {
135             throw new GenericUncheckedException(e);
136         }
137     }
138
139     /**
140      * The Class Builder.
141      */
142     public static class Builder {
143
144         /**
145          * The catalog.
146          */
147         private JSONObject catalog = new JSONObject()
148                 .put("resources", new JSONObject())
149                 .put(SERVICES, new JSONObject());
150
151         /**
152          * The mapper.
153          */
154         private ObjectMapper mapper = new ObjectMapper();
155
156         /**
157          * Instantiates a new builder.
158          */
159         public Builder() {
160         }
161
162         /**
163          * Catalog.
164          *
165          * @param catalog the catalog
166          * @return the builder
167          */
168         public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
169             this.catalog = catalog;
170             return this;
171         }
172
173         /**
174          * Mapper.
175          *
176          * @param mapper the mapper
177          * @return the builder
178          */
179         public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
180             this.mapper = mapper;
181             return this;
182         }
183
184         /**
185          * Builds the.
186          *
187          * @return the in local sdc client
188          */
189         public org.onap.vid.asdc.local.LocalAsdcClient build() {
190             return new org.onap.vid.asdc.local.LocalAsdcClient(this);
191         }
192     }
193
194 }