2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia. 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 static javax.ws.rs.core.MediaType.APPLICATION_JSON;
24 import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
25 import static org.onap.portalsdk.core.util.SystemProperties.APP_DISPLAY_NAME;
26 import static org.onap.vid.asdc.AsdcClient.URIS.METADATA_URL_TEMPLATE;
27 import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE;
28 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION;
29 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE;
30 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
31 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
32 import static org.onap.vid.utils.Logging.logRequest;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.google.common.collect.ImmutableMap;
36 import io.joshworks.restclient.http.HttpResponse;
37 import io.vavr.control.Try;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.StandardCopyOption;
43 import java.util.Collections;
45 import java.util.UUID;
46 import org.onap.portalsdk.core.util.SystemProperties;
47 import org.onap.vid.aai.ExceptionWithRequestInfo;
48 import org.onap.vid.aai.HttpResponseWithRequestInfo;
49 import org.onap.vid.asdc.AsdcCatalogException;
50 import org.onap.vid.asdc.AsdcClient;
51 import org.onap.vid.asdc.beans.Service;
52 import org.onap.vid.client.SyncRestClientInterface;
53 import org.onap.vid.model.ModelConstants;
54 import org.onap.vid.properties.VidProperties;
55 import org.onap.vid.utils.Logging;
56 import org.springframework.http.HttpMethod;
58 public class SdcRestClient implements AsdcClient {
60 private String baseUrl;
63 private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");
65 private SyncRestClientInterface syncRestClient;
68 public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
69 this.syncRestClient = client;
71 this.baseUrl = baseUrl;
72 this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
77 public Service getService(UUID uuid) throws AsdcCatalogException {
78 String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid);
79 logRequest(LOGGER, HttpMethod.GET, finalUrl);
82 .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
83 .getOrElseThrow(AsdcCatalogException::new)
89 public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException {
90 InputStream inputStream = Try
91 .of(() -> getServiceInputStream(uuid, false))
92 .getOrElseThrow(AsdcCatalogException::new)
96 return createTmpFile(inputStream);
100 public HttpResponseWithRequestInfo<InputStream> getServiceInputStream(UUID serviceUuid, boolean warpException) {
101 String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, serviceUuid);
102 logRequest(LOGGER, HttpMethod.GET, finalUrl);
104 HttpResponse<InputStream> httpResponse = syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap());
105 return new HttpResponseWithRequestInfo<>(httpResponse, finalUrl, HttpMethod.GET);
107 catch (RuntimeException exception) {
108 throw warpException ? new ExceptionWithRequestInfo(HttpMethod.GET, finalUrl, exception) : exception;
114 public HttpResponse<String> checkSDCConnectivity() {
115 String finalUrl = baseUrl + URIS.HEALTH_CHECK_ENDPOINT;
117 return syncRestClient
118 .get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), String.class);
122 public String getBaseUrl() {
126 private Map<String, String> prepareHeaders(String auth, String contentType) {
127 return ImmutableMap.of(
128 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
130 REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
131 CONTENT_TYPE, contentType
135 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
137 .of(() -> tryToCreateTmpFile(csarInputStream))
138 .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
141 private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
142 Path csarFile = Files.createTempFile("csar", ".zip");
143 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
145 LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());