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