[VID-55] Upgrade Tosca Parser (merge)
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / asdc / rest / RestfulAsdcClient.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.rest;\r
22 \r
23 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;\r
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
25 import org.openecomp.vid.asdc.AsdcCatalogException;\r
26 import org.openecomp.vid.asdc.AsdcClient;\r
27 import org.openecomp.vid.asdc.beans.Artifact;\r
28 import org.openecomp.vid.asdc.beans.Resource;\r
29 import org.openecomp.vid.asdc.beans.Service;\r
30 import org.openecomp.vid.asdc.parser.ToscaParserImpl;\r
31 import org.openecomp.vid.model.ModelConstants;\r
32 import org.openecomp.vid.properties.VidProperties;\r
33 \r
34 import javax.ws.rs.NotFoundException;\r
35 import javax.ws.rs.ProcessingException;\r
36 import javax.ws.rs.WebApplicationException;\r
37 import javax.ws.rs.client.Client;\r
38 import javax.ws.rs.client.ResponseProcessingException;\r
39 import javax.ws.rs.client.WebTarget;\r
40 import javax.ws.rs.core.GenericType;\r
41 import javax.ws.rs.core.MediaType;\r
42 import javax.ws.rs.core.MultivaluedHashMap;\r
43 import java.io.IOException;\r
44 import java.io.InputStream;\r
45 import java.net.URI;\r
46 import java.nio.file.Files;\r
47 import java.nio.file.Path;\r
48 import java.nio.file.StandardCopyOption;\r
49 import java.text.DateFormat;\r
50 import java.text.SimpleDateFormat;\r
51 import java.util.Collection;\r
52 import java.util.Collections;\r
53 import java.util.Map;\r
54 import java.util.Map.Entry;\r
55 import java.util.UUID;\r
56 \r
57 /**\r
58  * The Class RestfulAsdcClient.\r
59  */\r
60 public class RestfulAsdcClient implements AsdcClient {\r
61 \r
62     /**\r
63      * The Class Builder.\r
64      */\r
65     public static class Builder {\r
66 \r
67         /**\r
68          * The client.\r
69          */\r
70         private final Client client;\r
71 \r
72         /**\r
73          * The uri.\r
74          */\r
75         private final URI uri;\r
76 \r
77         /**\r
78          * The auth.\r
79          */\r
80         private String auth = null;\r
81 \r
82         /**\r
83          * Instantiates a new builder.\r
84          *\r
85          * @param client the client\r
86          * @param uri    the uri\r
87          */\r
88         public Builder(Client client, URI uri) {\r
89             this.client = client;\r
90             this.client.register(JacksonJsonProvider.class);\r
91             this.uri = uri;\r
92         }\r
93 \r
94         /**\r
95          * Auth.\r
96          *\r
97          * @param auth the auth\r
98          * @return the builder\r
99          */\r
100         public Builder auth(String auth) {\r
101             this.auth = auth;\r
102             return this;\r
103         }\r
104 \r
105         /**\r
106          * Builds the.\r
107          *\r
108          * @return the restful asdc client\r
109          */\r
110         public RestfulAsdcClient build() {\r
111             return new RestfulAsdcClient(this);\r
112         }\r
113     }\r
114 \r
115     /**\r
116      * The Constant LOG.\r
117      */\r
118     static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class);\r
119 \r
120     /**\r
121      * The Constant dateFormat.\r
122      */\r
123     final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
124 \r
125     /**\r
126      * The client.\r
127      */\r
128     private final Client client;\r
129 \r
130     /**\r
131      * The uri.\r
132      */\r
133     private final URI uri;\r
134 \r
135     /**\r
136      * The common headers.\r
137      */\r
138     private final MultivaluedHashMap<String, Object> commonHeaders;\r
139 \r
140     /**\r
141      * The auth.\r
142      */\r
143     private final String auth;\r
144 \r
145     ToscaParserImpl p = new ToscaParserImpl();\r
146 \r
147     /**\r
148      * Instantiates a new restful asdc client.\r
149      *\r
150      * @param builder the builder\r
151      */\r
152     private RestfulAsdcClient(Builder builder) {\r
153         client = builder.client;\r
154         uri = builder.uri;\r
155         auth = builder.auth;\r
156 \r
157         commonHeaders = new MultivaluedHashMap<String, Object>();\r
158         commonHeaders.put("X-ECOMP-InstanceID", Collections.singletonList((Object) "VID"));\r
159         commonHeaders.put("Authorization", Collections.singletonList((Object) (auth)));\r
160     }\r
161 \r
162     private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {\r
163         final Path csarFile;\r
164         try {\r
165             csarFile = Files.createTempFile("csar", ".zip");\r
166             Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);\r
167         } catch (IOException e) {\r
168             throw new AsdcCatalogException("Caught IOException while creating CSAR", e);\r
169         }\r
170         return csarFile;\r
171     }\r
172 \r
173     /**\r
174      * Gets the client.\r
175      *\r
176      * @return the client\r
177      */\r
178     private Client getClient() {\r
179         return client;\r
180     }\r
181 \r
182     /* (non-Javadoc)\r
183      * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)\r
184      */\r
185     public Resource getResource(UUID uuid) throws AsdcCatalogException {\r
186         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);\r
187         try {\r
188             return getClient()\r
189                     .target(uri)\r
190                     .path(path + "/" + uuid.toString() + "/metadata")\r
191                     .request(MediaType.APPLICATION_JSON_TYPE)\r
192                     .headers(commonHeaders)\r
193                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
194                     .get(Resource.class);\r
195         } catch (ResponseProcessingException e) {\r
196             //Couldn't convert response to Java type\r
197             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
198         } catch (ProcessingException e) {\r
199             //IO problems during request\r
200             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
201         } catch (WebApplicationException e) {\r
202             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
203             throw new AsdcCatalogException(e);\r
204         }\r
205     }\r
206 \r
207     /* (non-Javadoc)\r
208      * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)\r
209      */\r
210     public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {\r
211         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);\r
212         try {\r
213             return getClient()\r
214                     .target(uri)\r
215                     .path(path + "/" + resourceUuid + "/artifacts/" + artifactUuid)\r
216                     .request(MediaType.APPLICATION_JSON_TYPE)\r
217                     .headers(commonHeaders)\r
218                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
219                     .get(Artifact.class);\r
220         } catch (ResponseProcessingException e) {\r
221             //Couldn't convert response to Java type\r
222             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
223         } catch (ProcessingException e) {\r
224             //IO problems during request\r
225             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
226         } catch (WebApplicationException e) {\r
227             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
228             throw new AsdcCatalogException(e);\r
229         }\r
230     }\r
231 \r
232     /* (non-Javadoc)\r
233      * @see org.openecomp.vid.asdc.AsdcClient#getResources()\r
234      */\r
235     public Collection<Resource> getResources() throws AsdcCatalogException {\r
236         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);\r
237         try {\r
238             return getClient()\r
239                     .target(uri)\r
240                     .path(path)\r
241                     .request(MediaType.APPLICATION_JSON_TYPE)\r
242                     .headers(commonHeaders)\r
243                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
244                     .get(new GenericType<Collection<Resource>>() {\r
245                     });\r
246         } catch (ResponseProcessingException e) {\r
247             //Couldn't convert response to Java type\r
248             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
249         } catch (ProcessingException e) {\r
250             //IO problems during request\r
251             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
252         } catch (WebApplicationException e) {\r
253             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
254             throw new AsdcCatalogException(e);\r
255         }\r
256     }\r
257 \r
258     /* (non-Javadoc)\r
259      * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)\r
260      */\r
261     public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {\r
262         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);\r
263         WebTarget target = getClient()\r
264                 .target(uri)\r
265                 .path(path);\r
266 \r
267         for (Entry<String, String[]> filterEntry : filter.entrySet()) {\r
268             target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());\r
269         }\r
270 \r
271         try {\r
272             return target.request()\r
273                     .accept(MediaType.APPLICATION_JSON_TYPE)\r
274                     .headers(commonHeaders)\r
275                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
276                     .get(new GenericType<Collection<Resource>>() {\r
277                     });\r
278         } catch (ResponseProcessingException e) {\r
279             //Couldn't convert response to Java type\r
280             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
281         } catch (ProcessingException e) {\r
282             //IO problems during request\r
283             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
284         } catch (NotFoundException e) {\r
285             throw e;\r
286         } catch (WebApplicationException e) {\r
287             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
288             throw new AsdcCatalogException(e);\r
289         }\r
290     }\r
291 \r
292     /* (non-Javadoc)\r
293      * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)\r
294      */\r
295     public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {\r
296         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);\r
297         try (final InputStream csarInputStream = (InputStream) getClient()\r
298                 .target(uri)\r
299                 .path(path + "/" + resourceUuid + "/toscaModel")\r
300                 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)\r
301                 .headers(commonHeaders)\r
302                 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)\r
303                 .get(InputStream.class)) {\r
304 \r
305             return getToscaCsar(csarInputStream);\r
306         } catch (IOException e) {\r
307             throw new AsdcCatalogException("Failed to retrieve resource TOSCA model from ASDC", e);\r
308         }\r
309     }\r
310 \r
311     /* (non-Javadoc)\r
312      * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)\r
313      */\r
314     public Service getService(UUID uuid) throws AsdcCatalogException {\r
315 \r
316         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);\r
317         try {\r
318             return getClient()\r
319                     .target(uri)\r
320                     .path(path + "/" + uuid.toString() + "/metadata")\r
321                     .request(MediaType.APPLICATION_JSON)\r
322                     .headers(commonHeaders)\r
323                     .get(Service.class);\r
324         } catch (ResponseProcessingException e) {\r
325             //Couldn't convert response to Java type\r
326             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
327         } catch (ProcessingException e) {\r
328             //IO problems during request\r
329             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
330         } catch (WebApplicationException e) {\r
331             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
332             throw new AsdcCatalogException(e);\r
333         }\r
334     }\r
335 \r
336     /* (non-Javadoc)\r
337      * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)\r
338      */\r
339     public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {\r
340         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);\r
341                 \r
342         try {\r
343             return getClient()\r
344                     .target(uri)\r
345                     .path(path + "/" + serviceUuid + "/artifacts/" + artifactUuid)\r
346                     .request(MediaType.APPLICATION_JSON_TYPE)\r
347                     .headers(commonHeaders)\r
348                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
349                     .get(Artifact.class);\r
350         } catch (ResponseProcessingException e) {\r
351             //Couldn't convert response to Java type\r
352             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
353         } catch (ProcessingException e) {\r
354             //IO problems during request\r
355             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
356         } catch (WebApplicationException e) {\r
357             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
358             throw new AsdcCatalogException(e);\r
359         }\r
360     }\r
361 \r
362     /* (non-Javadoc)\r
363      * @see org.openecomp.vid.asdc.AsdcClient#getServices()\r
364      */\r
365     public Collection<Service> getServices() throws AsdcCatalogException {\r
366         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);\r
367         try {\r
368             return getClient()\r
369                     .target(uri)\r
370                     .path(path)\r
371                     .request()\r
372                     .accept(MediaType.APPLICATION_JSON_TYPE)\r
373                     .headers(commonHeaders)\r
374                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
375                     .get(new GenericType<Collection<Service>>() {\r
376                     });\r
377         } catch (ResponseProcessingException e) {\r
378             //Couldn't convert response to Java type\r
379             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
380         } catch (ProcessingException e) {\r
381             //IO problems during request\r
382             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
383         } catch (WebApplicationException e) {\r
384             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
385             throw new AsdcCatalogException(e);\r
386         }\r
387     }\r
388 \r
389     /* (non-Javadoc)\r
390      * @see org.openecomp.vid.asdc.AsdcClient#getServices(java.util.Map)\r
391      */\r
392     public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {\r
393 \r
394         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);\r
395         WebTarget target = getClient()\r
396                 .target(uri)\r
397                 .path(path);\r
398 \r
399 \r
400         for (Entry<String, String[]> filterEntry : filter.entrySet()) {\r
401             target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());\r
402         }\r
403 \r
404         try {\r
405             return target.request()\r
406                     .accept(MediaType.APPLICATION_JSON_TYPE)\r
407                     .headers(commonHeaders)\r
408                     .header("Content-Type", MediaType.APPLICATION_JSON)\r
409                     .get(new GenericType<Collection<Service>>() {\r
410                     });\r
411         } catch (ResponseProcessingException e) {\r
412             //Couldn't convert response to Java type\r
413             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
414         } catch (ProcessingException e) {\r
415             //IO problems during request\r
416             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
417         } catch (NotFoundException e) {\r
418             throw e;\r
419         } catch (WebApplicationException e) {\r
420             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
421             throw new AsdcCatalogException(e);\r
422         }\r
423     }\r
424 \r
425 \r
426     /* (non-Javadoc)\r
427      * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)\r
428      */\r
429     public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {\r
430         String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);\r
431         try {\r
432             final InputStream csarInputStream = (InputStream) getClient()\r
433                     .target(uri)\r
434                     .path(path + "/" + serviceUuid + "/toscaModel")\r
435                     .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)\r
436                     .headers(commonHeaders)\r
437                     .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)\r
438                     .get(InputStream.class);\r
439 \r
440 \r
441             return getToscaCsar(csarInputStream);\r
442         } catch (ResponseProcessingException e) {\r
443             //Couldn't convert response to Java type\r
444             throw new AsdcCatalogException("ASDC response could not be processed", e);\r
445         } catch (ProcessingException e) {\r
446             //IO problems during request\r
447             throw new AsdcCatalogException("Failed to get a response from ASDC service", e);\r
448         } catch (WebApplicationException e) {\r
449             //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)\r
450             throw new AsdcCatalogException(e);\r
451         }\r
452     }\r
453 \r
454 \r
455     /**\r
456      * Gets the tosca model.\r
457      *\r
458      * @param csarInputStream the csar input stream\r
459      * @return the tosca model\r
460      * @throws AsdcCatalogException the asdc catalog exception\r
461      */\r
462     private Path getToscaCsar(InputStream csarInputStream) throws AsdcCatalogException {\r
463         return createTmpFile(csarInputStream);\r
464     }\r
465 }\r