2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.asdc.rest;
23 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.portalsdk.core.util.SystemProperties;
26 import org.onap.vid.asdc.AsdcCatalogException;
27 import org.onap.vid.asdc.AsdcClient;
28 import org.onap.vid.asdc.beans.Artifact;
29 import org.onap.vid.asdc.beans.Resource;
30 import org.onap.vid.asdc.beans.Service;
31 import org.onap.vid.asdc.parser.ToscaParserImpl;
32 import org.onap.vid.model.ModelConstants;
33 import org.onap.vid.properties.VidProperties;
35 import javax.ws.rs.NotFoundException;
36 import javax.ws.rs.ProcessingException;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.client.Client;
39 import javax.ws.rs.client.ResponseProcessingException;
40 import javax.ws.rs.client.WebTarget;
41 import javax.ws.rs.core.GenericType;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.MultivaluedHashMap;
44 import java.io.IOException;
45 import java.io.InputStream;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.StandardCopyOption;
50 import java.text.DateFormat;
51 import java.text.SimpleDateFormat;
52 import java.util.Collection;
53 import java.util.Collections;
55 import java.util.Map.Entry;
56 import java.util.UUID;
58 import static org.onap.vid.utils.Logging.getHttpServletRequest;
59 import static org.onap.vid.utils.Logging.requestIdHeaderKey;
61 * The Class RestfulAsdcClient.
63 @SuppressWarnings("Duplicates")
64 public class RestfulAsdcClient implements AsdcClient {
70 public static class Builder {
75 private final Client client;
80 private final URI uri;
85 private String auth = null;
88 * Instantiates a new builder.
90 * @param client the client
93 public Builder(Client client, URI uri) {
95 this.client.register(JacksonJsonProvider.class);
102 * @param auth the auth
103 * @return the builder
105 public Builder auth(String auth) {
113 * @return the restful asdc client
115 public RestfulAsdcClient build() {
116 return new RestfulAsdcClient(this);
123 static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class);
126 * The Constant dateFormat.
128 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
133 private final Client client;
138 private final URI uri;
141 * The common headers.
143 private final MultivaluedHashMap<String, Object> commonHeaders;
148 private final String auth;
150 ToscaParserImpl p = new ToscaParserImpl();
153 * Instantiates a new restful asdc client.
155 * @param builder the builder
157 RestfulAsdcClient(Builder builder) {
158 client = builder.client;
162 commonHeaders = new MultivaluedHashMap<String, Object>();
163 commonHeaders.put("Authorization", Collections.singletonList((Object) (auth)));
164 commonHeaders.put("X-ECOMP-InstanceID", Collections.singletonList((Object) (SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME))));
167 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
170 csarFile = Files.createTempFile("csar", ".zip");
171 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
172 } catch (IOException e) {
173 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
183 private Client getClient() {
188 * @see org.onap.vid.asdc.AsdcClient#getResource(java.util.UUID)
190 public Resource getResource(UUID uuid) throws AsdcCatalogException {
192 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
196 .path(path + "/" + uuid.toString() + "/metadata")
197 .request(MediaType.APPLICATION_JSON_TYPE)
198 .headers(commonHeaders)
199 .header("Content-Type", MediaType.APPLICATION_JSON)
200 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
201 .get(Resource.class);
202 } catch (ResponseProcessingException e) {
203 //Couldn't convert response to Java type
204 throw new AsdcCatalogException("SDC response could not be processed", e);
205 } catch (ProcessingException e) {
206 //IO problems during request
207 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
208 } catch (WebApplicationException e) {
209 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
210 throw new AsdcCatalogException(e);
215 * @see org.onap.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
217 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
218 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
222 .path(path + "/" + resourceUuid + "/artifacts/" + artifactUuid)
223 .request(MediaType.APPLICATION_JSON_TYPE)
224 .headers(commonHeaders)
225 .header("Content-Type", MediaType.APPLICATION_JSON)
226 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
227 .get(Artifact.class);
228 } catch (ResponseProcessingException e) {
229 //Couldn't convert response to Java type
230 throw new AsdcCatalogException("SDC response could not be processed", e);
231 } catch (ProcessingException e) {
232 //IO problems during request
233 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
234 } catch (WebApplicationException e) {
235 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
236 throw new AsdcCatalogException(e);
241 * @see org.onap.vid.asdc.AsdcClient#getResources()
243 public Collection<Resource> getResources() throws AsdcCatalogException {
244 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
249 .request(MediaType.APPLICATION_JSON_TYPE)
250 .headers(commonHeaders)
251 .header("Content-Type", MediaType.APPLICATION_JSON)
252 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
253 .get(new GenericType<Collection<Resource>>() {
255 } catch (ResponseProcessingException e) {
256 //Couldn't convert response to Java type
257 throw new AsdcCatalogException("SDC response could not be processed", e);
258 } catch (ProcessingException e) {
259 //IO problems during request
260 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
261 } catch (WebApplicationException e) {
262 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
263 throw new AsdcCatalogException(e);
268 * @see org.onap.vid.asdc.AsdcClient#getResources(java.util.Map)
270 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
271 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
272 WebTarget target = getClient()
276 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
277 target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());
281 return target.request()
282 .accept(MediaType.APPLICATION_JSON_TYPE)
283 .headers(commonHeaders)
284 .header("Content-Type", MediaType.APPLICATION_JSON)
285 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
286 .get(new GenericType<Collection<Resource>>() {
288 } catch (ResponseProcessingException e) {
289 //Couldn't convert response to Java type
290 throw new AsdcCatalogException("SDC response could not be processed", e);
291 } catch (ProcessingException e) {
292 //IO problems during request
293 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
294 } catch (NotFoundException e) {
296 } catch (WebApplicationException e) {
297 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
298 throw new AsdcCatalogException(e);
303 * @see org.onap.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
305 public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
306 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
307 try (final InputStream csarInputStream = (InputStream) getClient()
309 .path(path + "/" + resourceUuid + "/toscaModel")
310 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
311 .headers(commonHeaders)
312 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
313 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
314 .get(InputStream.class)) {
316 return getToscaCsar(csarInputStream);
317 } catch (IOException e) {
318 throw new AsdcCatalogException("Failed to retrieve resource TOSCA model from SDC", e);
323 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
325 public Service getService(UUID uuid) throws AsdcCatalogException {
327 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
331 .path(path + "/" + uuid.toString() + "/metadata")
332 .request(MediaType.APPLICATION_JSON)
333 .headers(commonHeaders)
334 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
336 } catch (ResponseProcessingException e) {
337 //Couldn't convert response to Java type
338 throw new AsdcCatalogException("SDC response could not be processed", e);
339 } catch (ProcessingException e) {
340 //IO problems during request
341 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
342 } catch (WebApplicationException e) {
343 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
344 throw new AsdcCatalogException(e);
349 * @see org.onap.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
351 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
352 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
357 .path(path + "/" + serviceUuid + "/artifacts/" + artifactUuid)
358 .request(MediaType.APPLICATION_JSON_TYPE)
359 .headers(commonHeaders)
360 .header("Content-Type", MediaType.APPLICATION_JSON)
361 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
362 .get(Artifact.class);
363 } catch (ResponseProcessingException e) {
364 //Couldn't convert response to Java type
365 throw new AsdcCatalogException("SDC response could not be processed", e);
366 } catch (ProcessingException e) {
367 //IO problems during request
368 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
369 } catch (WebApplicationException e) {
370 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
371 throw new AsdcCatalogException(e);
376 * @see org.onap.vid.asdc.AsdcClient#getServices()
378 public Collection<Service> getServices() throws AsdcCatalogException {
379 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
385 .accept(MediaType.APPLICATION_JSON_TYPE)
386 .headers(commonHeaders)
387 .header("Content-Type", MediaType.APPLICATION_JSON)
388 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
389 .get(new GenericType<Collection<Service>>() {
391 } catch (ResponseProcessingException e) {
392 //Couldn't convert response to Java type
393 throw new AsdcCatalogException("SDC response could not be processed", e);
394 } catch (ProcessingException e) {
395 //IO problems during request
396 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
397 } catch (WebApplicationException e) {
398 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
399 throw new AsdcCatalogException(e);
404 * @see org.onap.vid.asdc.AsdcClient#getServices(java.util.Map)
406 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
408 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
409 WebTarget target = getClient()
414 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
415 target = target.queryParam(filterEntry.getKey(), (Object[]) filterEntry.getValue());
419 return target.request()
420 .accept(MediaType.APPLICATION_JSON_TYPE)
421 .headers(commonHeaders)
422 .header("Content-Type", MediaType.APPLICATION_JSON)
423 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
424 .get(new GenericType<Collection<Service>>() {
426 } catch (ResponseProcessingException e) {
427 //Couldn't convert response to Java type
428 throw new AsdcCatalogException("SDC response could not be processed", e);
429 } catch (ProcessingException e) {
430 //IO problems during request
431 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
432 } catch (NotFoundException e) {
434 } catch (WebApplicationException e) {
435 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
436 throw new AsdcCatalogException(e);
442 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
444 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
445 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
447 final InputStream csarInputStream = (InputStream) getClient()
449 .path(path + "/" + serviceUuid + "/toscaModel")
450 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
451 .headers(commonHeaders)
452 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
453 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
454 .get(InputStream.class);
457 return getToscaCsar(csarInputStream);
458 } catch (ResponseProcessingException e) {
459 //Couldn't convert response to Java type
460 throw new AsdcCatalogException("SDC response could not be processed", e);
461 } catch (ProcessingException e) {
462 //IO problems during request
463 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
464 } catch (WebApplicationException e) {
465 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
466 throw new AsdcCatalogException(e);
472 * Gets the tosca model.
474 * @param csarInputStream the csar input stream
475 * @return the tosca model
476 * @throws AsdcCatalogException the asdc catalog exception
478 private Path getToscaCsar(InputStream csarInputStream) throws AsdcCatalogException {
479 return createTmpFile(csarInputStream);