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