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