c5134bfca79625c66bac481b5dc1dbce6071fe41
[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         private 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          * @see org.openecomp.vid.asdc.AsdcClient#getResources()\r
172          */\r
173         public Collection<Resource> getResources() throws AsdcCatalogException {\r
174                 final Collection<Resource> resources = new LinkedList<Resource> ();\r
175                 \r
176                 for (String key : getCatalog().getJSONObject("resources").keySet()) {\r
177                         final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);\r
178                         final Resource resource = convert(json, Resource.class);\r
179                         resources.add(resource);\r
180                 }\r
181                 \r
182                 return resources;\r
183         }\r
184 \r
185         /* (non-Javadoc)\r
186          * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)\r
187          */\r
188         public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {\r
189                 final Collection<Resource> resources = new LinkedList<Resource> ();\r
190                 \r
191                 for (String key : getCatalog().getJSONObject("resources").keySet()) {\r
192                         final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);\r
193                         \r
194                         boolean filterMatch = true;\r
195                         \r
196                         for (Entry<String, String[]> entry : filter.entrySet()) {\r
197                                 for (int i = 0; i < entry.getValue().length; i++) {\r
198                                         if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {\r
199                                                 filterMatch = false;\r
200                                                 break;\r
201                                         }\r
202                                 }\r
203                         }\r
204                         \r
205                         if (filterMatch) resources.add(convert(json, Resource.class));\r
206                 }\r
207                 \r
208                 return resources;\r
209         }\r
210 \r
211         /* (non-Javadoc)\r
212          * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)\r
213          */\r
214         public Service getService(UUID uuid) throws AsdcCatalogException {\r
215                 final JSONObject service = getCatalog().getJSONObject("services")\r
216                                 .getJSONObject(uuid.toString());\r
217                 return convert(service, Service.class);\r
218         }\r
219 \r
220         /* (non-Javadoc)\r
221          * @see org.openecomp.vid.asdc.AsdcClient#getServices()\r
222          */\r
223         public Collection<Service> getServices() throws AsdcCatalogException {\r
224                 final Collection<Service> services = new LinkedList<Service> ();\r
225                 \r
226                 for (String key : getCatalog().getJSONObject("services").keySet()) {\r
227                         final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);\r
228                         final Service service = convert(json, Service.class);\r
229                         services.add(service);\r
230                 }\r
231                 \r
232                 return services;\r
233         }\r
234 \r
235         /* (non-Javadoc)\r
236          * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)\r
237          */\r
238         public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {\r
239                 final Collection<Service> services = new LinkedList<Service> ();\r
240                 \r
241                 for (String key : getCatalog().getJSONObject("services").keySet()) {\r
242                         final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);\r
243                         \r
244                         boolean filterMatch = true;\r
245                         \r
246                         for (Entry<String, String[]> entry : filter.entrySet()) {\r
247                                 for (int i = 0; i < entry.getValue().length; i++) {\r
248                                         if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {\r
249                                                 filterMatch = false;\r
250                                                 break;\r
251                                         }\r
252                                 }\r
253                         }\r
254                         \r
255                         if (filterMatch) services.add(convert(json, Service.class));\r
256                 }\r
257                 \r
258                 return services;\r
259         }\r
260 \r
261         /* (non-Javadoc)\r
262          * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)\r
263          */\r
264         public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {\r
265                 final JSONArray artifacts = getCatalog().getJSONObject("resources")\r
266                                 .getJSONObject(resourceUuid.toString())\r
267                                 .getJSONArray("artifacts");\r
268                 \r
269                 for (int i = 0; i < artifacts.length(); i++) {\r
270                         final JSONObject artifact = artifacts.getJSONObject(i);\r
271                         \r
272                         if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {\r
273                                 return convert(artifact, Artifact.class);\r
274                         }\r
275                 }\r
276                 \r
277                 return null;\r
278         }\r
279 \r
280         /* (non-Javadoc)\r
281          * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)\r
282          */\r
283         public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {\r
284                 final JSONArray artifacts = getCatalog().getJSONObject("services")\r
285                                 .getJSONObject(serviceUuid.toString())\r
286                                 .getJSONArray("artifacts");\r
287                 \r
288                 for (int i = 0; i < artifacts.length(); i++) {\r
289                         final JSONObject artifact = artifacts.getJSONObject(i);\r
290                         \r
291                         if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {\r
292                                 return convert(artifact, Artifact.class);\r
293                         }\r
294                 }\r
295                 \r
296                 return null;\r
297         }\r
298 \r
299         /* (non-Javadoc)\r
300          * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)\r
301          */\r
302         public ToscaCsar getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {\r
303                 final String toscaModelURL = getCatalog().getJSONObject("resources")\r
304                                 .getJSONObject(resourceUuid.toString())\r
305                                 .getString("toscaModelURL");\r
306 \r
307 \r
308                 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);\r
309                 \r
310                 if (toscaModelStream == null) return null;\r
311                 \r
312                 return getToscaModel(toscaModelStream);\r
313         }\r
314 \r
315         /* (non-Javadoc)\r
316          * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)\r
317          */\r
318         public ToscaCsar getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {\r
319                 final String toscaModelURL = getCatalog().getJSONObject("services")\r
320                                                 .getJSONObject(serviceUuid.toString())\r
321                                                 .getString("toscaModelURL");\r
322                 \r
323                 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);\r
324                 \r
325                 if (toscaModelStream == null) return null;\r
326                 \r
327                 return getToscaModel(toscaModelStream);\r
328         }\r
329 \r
330         /**\r
331          * Gets the tosca model.\r
332          *\r
333          * @param csarInputStream the csar input stream\r
334          * @return the tosca model\r
335          * @throws AsdcCatalogException the asdc catalog exception\r
336          */\r
337         private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {\r
338                 final Path csarFile;\r
339                 \r
340                 try {\r
341                         csarFile = Files.createTempFile("csar", ".zip");\r
342                         Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);\r
343                 } catch (IOException e) {\r
344                         throw new AsdcCatalogException("Caught IOException while creating CSAR", e);\r
345                 }\r
346                 \r
347                 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {\r
348                         \r
349                         final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));\r
350                         final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();\r
351                         final String entryDefinitions = toscaMeta.get("Entry-Definitions");\r
352                         final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));\r
353                         \r
354                         final Yaml yaml = new Yaml();\r
355                         final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);\r
356 \r
357                         final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);\r
358                         \r
359                         for (Map<String, Map<String, String>> imports : parentModel.getImports()) {\r
360                                 for (Entry<String, Map<String, String>> entry : imports.entrySet()) {\r
361                                         final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));\r
362                                         final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);\r
363                                         csarBuilder.addVnf(childModel);\r
364                                 }\r
365                         }\r
366                         \r
367                         return csarBuilder.build();\r
368                 } catch (IOException e) {\r
369                         throw new AsdcCatalogException("Caught IOException while processing CSAR", e);\r
370                 }\r
371         }\r
372 }\r