2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.vid.asdc.rest;
\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
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
58 * The Class RestfulAsdcClient.
\r
60 public class RestfulAsdcClient implements AsdcClient {
\r
63 * The Class Builder.
\r
65 public static class Builder {
\r
70 private final Client client;
\r
75 private final URI uri;
\r
80 private String auth = null;
\r
83 * Instantiates a new builder.
\r
85 * @param client the client
\r
86 * @param uri the uri
\r
88 public Builder(Client client, URI uri) {
\r
89 this.client = client;
\r
90 this.client.register(JacksonJsonProvider.class);
\r
97 * @param auth the auth
\r
98 * @return the builder
\r
100 public Builder auth(String auth) {
\r
108 * @return the restful asdc client
\r
110 public RestfulAsdcClient build() {
\r
111 return new RestfulAsdcClient(this);
\r
116 * The Constant LOG.
\r
118 static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class);
\r
121 * The Constant dateFormat.
\r
123 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
\r
128 private final Client client;
\r
133 private final URI uri;
\r
136 * The common headers.
\r
138 private final MultivaluedHashMap<String, Object> commonHeaders;
\r
143 private final String auth;
\r
145 ToscaParserImpl p = new ToscaParserImpl();
\r
148 * Instantiates a new restful asdc client.
\r
150 * @param builder the builder
\r
152 private RestfulAsdcClient(Builder builder) {
\r
153 client = builder.client;
\r
155 auth = builder.auth;
\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
162 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
\r
163 final Path csarFile;
\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
176 * @return the client
\r
178 private Client getClient() {
\r
183 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
\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
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
208 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
\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
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
233 * @see org.openecomp.vid.asdc.AsdcClient#getResources()
\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
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
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
259 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
\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
267 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
\r
268 target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());
\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
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
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
293 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
\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
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
305 return getToscaCsar(csarInputStream);
\r
306 } catch (IOException e) {
\r
307 throw new AsdcCatalogException("Failed to retrieve resource TOSCA model from ASDC", e);
\r
312 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
\r
314 public Service getService(UUID uuid) throws AsdcCatalogException {
\r
316 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\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
337 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
\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
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
363 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
\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
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
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
390 * @see org.openecomp.vid.asdc.AsdcClient#getServices(java.util.Map)
\r
392 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
\r
394 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
395 WebTarget target = getClient()
\r
400 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
\r
401 target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());
\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
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
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
427 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
\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
432 final InputStream csarInputStream = (InputStream) getClient()
\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
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
456 * Gets the tosca model.
\r
458 * @param csarInputStream the csar input stream
\r
459 * @return the tosca model
\r
460 * @throws AsdcCatalogException the asdc catalog exception
\r
462 private Path getToscaCsar(InputStream csarInputStream) throws AsdcCatalogException {
\r
463 return createTmpFile(csarInputStream);
\r