org.onap migration
[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 org.codehaus.jackson.JsonParseException;
4 import org.codehaus.jackson.map.JsonMappingException;
5 import org.codehaus.jackson.map.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.Artifact;
11 import org.onap.vid.asdc.beans.Resource;
12 import org.onap.vid.asdc.beans.Service;
13 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
14 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
15 import org.onap.vid.asdc.beans.tosca.ToscaModel;
16 import org.yaml.snakeyaml.Yaml;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.nio.file.StandardCopyOption;
25 import java.util.Collection;
26 import java.util.LinkedList;
27 import java.util.Map;
28 import java.util.UUID;
29 import java.util.zip.ZipFile;
30
31 /**
32  * The Class LocalAsdcClient.
33  */
34 public class LocalAsdcClient implements AsdcClient {
35
36
37     /**
38      * The catalog.
39      */
40     private final JSONObject catalog;
41
42     /**
43      * The mapper.
44      */
45     private final ObjectMapper mapper;
46
47     /**
48      * Instantiates a new in local sdc client.
49      *
50      * @param builder the builder
51      */
52     public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
53         catalog = builder.catalog;
54         mapper = builder.mapper;
55     }
56
57     /**
58      * Gets the catalog.
59      *
60      * @return the catalog
61      */
62     private JSONObject getCatalog() {
63         return catalog;
64     }
65
66     /**
67      * Gets the mapper.
68      *
69      * @return the mapper
70      */
71     private ObjectMapper getMapper() {
72         return mapper;
73     }
74
75     /**
76      * Convert.
77      *
78      * @param <T>   the generic type
79      * @param json  the json
80      * @param clazz the clazz
81      * @return the t
82      * @throws AsdcCatalogException the sdc catalog exception
83      */
84     private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
85         try {
86             return getMapper().readValue(json.toString(), clazz);
87         } catch (JsonParseException e) {
88             throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
89         } catch (JsonMappingException e) {
90             throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
91         } catch (IOException e) {
92             throw new AsdcCatalogException("Failed to get a response from SDC", e);
93         }
94     }
95
96     /* (non-Javadoc)
97      * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
98      */
99     public Resource getResource(UUID uuid) throws AsdcCatalogException {
100         final JSONObject resource = getCatalog().getJSONObject("resources")
101                 .getJSONObject(uuid.toString());
102         return convert(resource, Resource.class);
103     }
104
105     /* (non-Javadoc)
106      * @see org.openecomp.vid.asdc.AsdcClient#getResources()
107      */
108     public Collection<Resource> getResources() throws AsdcCatalogException {
109         final Collection<Resource> resources = new LinkedList<Resource>();
110
111         for (String key : getCatalog().getJSONObject("resources").keySet()) {
112             final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
113             final Resource resource = convert(json, Resource.class);
114             resources.add(resource);
115         }
116
117         return resources;
118     }
119
120     /* (non-Javadoc)
121      * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
122      */
123     public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
124         final Collection<Resource> resources = new LinkedList<Resource>();
125
126         for (String key : getCatalog().getJSONObject("resources").keySet()) {
127             final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
128
129             boolean filterMatch = true;
130
131             for (Map.Entry<String, String[]> entry : filter.entrySet()) {
132                 for (int i = 0; i < entry.getValue().length; i++) {
133                     if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
134                         filterMatch = false;
135                         break;
136                     }
137                 }
138             }
139
140             if (filterMatch) resources.add(convert(json, Resource.class));
141         }
142
143         return resources;
144     }
145
146     /* (non-Javadoc)
147      * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
148      */
149     public Service getService(UUID uuid) throws AsdcCatalogException {
150
151         JSONObject serviceJsonObject = null;
152         final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
153
154         for (int i = 0; i < categoryJsonArray.length(); i++) {
155             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
156             if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
157                 serviceJsonObject = jsonServiceObject;
158                 break;
159             }
160         }
161
162         if (serviceJsonObject != null)
163             return convert(serviceJsonObject, Service.class);
164         else return null;
165     }
166
167     /* (non-Javadoc)
168      * @see org.openecomp.vid.asdc.AsdcClient#getServices()
169      */
170     public Collection<Service> getServices() throws AsdcCatalogException {
171         final Collection<Service> services = new LinkedList<Service>();
172
173         JSONArray servicesArr = getCatalog().getJSONArray("services");
174
175         for (Object objService : servicesArr) {
176             JSONObject jsonServiceItem = (JSONObject) objService;
177             final Service service = convert(jsonServiceItem, Service.class);
178             services.add(service);
179         }
180
181         return services;
182     }
183
184     /* (non-Javadoc)
185      * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
186      */
187     public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
188         final Collection<Service> services = new LinkedList<Service>();
189
190         JSONArray catalogServices = catalog.getJSONArray("services");
191
192         for (int i = 0; i < catalogServices.length(); i++) {
193
194             JSONObject serviceJson = catalogServices.getJSONObject(i);
195
196             boolean filterMatch = true;
197
198             for (Map.Entry<String, String[]> entry : filter.entrySet()) {
199                 for (int j = 0; j < entry.getValue().length; j++) {
200                     if (!serviceJson.getString(entry.getKey()).equals(entry.getValue()[j])) {
201                         filterMatch = false;
202                         break;
203                     }
204                 }
205             }
206             if (filterMatch) services.add(convert(serviceJson, Service.class));
207         }
208         return services;
209     }
210
211     /* (non-Javadoc)
212      * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
213      */
214     public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
215         final JSONArray artifacts = getCatalog().getJSONObject("resources")
216                 .getJSONObject(resourceUuid.toString())
217                 .getJSONArray("artifacts");
218
219         for (int i = 0; i < artifacts.length(); i++) {
220             final JSONObject artifact = artifacts.getJSONObject(i);
221
222             if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
223                 return convert(artifact, Artifact.class);
224             }
225         }
226
227         return null;
228     }
229
230     /* (non-Javadoc)
231      * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
232      */
233     public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
234         final JSONArray artifacts = getCatalog().getJSONObject("services")
235                 .getJSONObject(serviceUuid.toString())
236                 .getJSONArray("artifacts");
237
238         for (int i = 0; i < artifacts.length(); i++) {
239             final JSONObject artifact = artifacts.getJSONObject(i);
240
241             if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
242                 return convert(artifact, Artifact.class);
243             }
244         }
245
246         return null;
247     }
248
249     /* (non-Javadoc)
250      * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
251      */
252     public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
253         final String toscaModelURL = getCatalog().getJSONObject("resources")
254                 .getJSONObject(resourceUuid.toString())
255                 .getString("toscaModelURL");
256
257
258         final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
259
260         if (toscaModelStream == null) return null;
261
262         return null;//getToscaModel(toscaModelStream);
263     }
264
265     /* (non-Javadoc)
266      * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
267      */
268     public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
269
270         String toscaModelURL = null;
271
272         final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
273
274         for (int i = 0; i < categoryJsonArray.length(); i++) {
275
276             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
277             if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
278                 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
279                 break;
280             }
281         }
282         if (toscaModelURL == null) {
283             return null;
284         }
285         final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
286
287         ClassLoader classLoader = getClass().getClassLoader();
288         File file = new File(classLoader.getResource(toscaModelURL).getFile());
289         Path path = Paths.get(file.getPath());
290
291         if (toscaModelStream == null) return null;
292
293         return path;
294     }
295
296     /**
297      * Gets the tosca model.
298      *
299      * @param csarInputStream the csar input stream
300      * @return the tosca model
301      * @throws AsdcCatalogException the asdc catalog exception
302      */
303     private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
304         final Path csarFile;
305
306         try {
307             csarFile = Files.createTempFile("csar", ".zip");
308             Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
309         } catch (IOException e) {
310             throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
311         }
312
313         try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
314
315             final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
316             final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
317             final String entryDefinitions = toscaMeta.get("Entry-Definitions");
318             final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
319
320             final Yaml yaml = new Yaml();
321             final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
322
323             final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
324
325             for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
326                 for (Map.Entry<String, Map<String, String>> entry : imports.entrySet()) {
327                     final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
328                     final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
329                     csarBuilder.addVnf(childModel);
330                 }
331             }
332
333             return csarBuilder.build();
334         } catch (IOException e) {
335             throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
336         }
337     }
338
339     /**
340      * The Class Builder.
341      */
342     public static class Builder {
343
344         /**
345          * The catalog.
346          */
347         private JSONObject catalog = new JSONObject()
348                 .put("resources", new JSONObject())
349                 .put("services", new JSONObject());
350
351         /**
352          * The mapper.
353          */
354         private ObjectMapper mapper = new ObjectMapper();
355
356         /**
357          * Instantiates a new builder.
358          */
359         public Builder() {
360         }
361
362         /**
363          * Catalog.
364          *
365          * @param catalog the catalog
366          * @return the builder
367          */
368         public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
369             this.catalog = catalog;
370             return this;
371         }
372
373         /**
374          * Mapper.
375          *
376          * @param mapper the mapper
377          * @return the builder
378          */
379         public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
380             this.mapper = mapper;
381             return this;
382         }
383
384         /**
385          * Builds the.
386          *
387          * @return the in local sdc client
388          */
389         public org.onap.vid.asdc.local.LocalAsdcClient build() {
390             return new org.onap.vid.asdc.local.LocalAsdcClient(this);
391         }
392     }
393
394 }