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 com.att.eelf.configuration.EELFLogger;
24 import com.google.common.collect.ImmutableMap;
25 import io.joshworks.restclient.http.HttpResponse;
26 import io.vavr.control.Try;
27 import org.onap.portalsdk.core.util.SystemProperties;
28 import org.onap.vid.asdc.AsdcCatalogException;
29 import org.onap.vid.asdc.AsdcClient;
30 import org.onap.vid.asdc.beans.Service;
31 import org.onap.vid.client.SyncRestClientInterface;
32 import org.onap.vid.model.ModelConstants;
33 import org.onap.vid.properties.VidProperties;
34 import org.onap.vid.utils.Logging;
35 import org.springframework.http.HttpMethod;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.StandardCopyOption;
42 import java.util.Collections;
44 import java.util.UUID;
46 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
47 import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
48 import static org.onap.portalsdk.core.util.SystemProperties.APP_DISPLAY_NAME;
49 import static org.onap.vid.asdc.AsdcClient.URIS.METADATA_URL_TEMPLATE;
50 import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE;
51 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION;
52 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE;
53 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
54 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
55 import static org.onap.vid.utils.Logging.logRequest;
57 public class SdcRestClient implements AsdcClient {
59 private String baseUrl;
62 private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");
64 private SyncRestClientInterface syncRestClient;
67 public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
68 this.syncRestClient = client;
70 this.baseUrl = baseUrl;
71 this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
76 public Service getService(UUID uuid) throws AsdcCatalogException {
77 String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid);
78 logRequest(LOGGER, HttpMethod.GET, finalUrl);
81 .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
82 .getOrElseThrow(AsdcCatalogException::new)
88 public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException {
89 String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, uuid);
90 logRequest(LOGGER, HttpMethod.GET, finalUrl);
92 InputStream inputStream = Try
93 .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()))
94 .getOrElseThrow(AsdcCatalogException::new)
97 return createTmpFile(inputStream);
102 public HttpResponse<String> checkSDCConnectivity() {
103 String finalUrl = baseUrl + URIS.HEALTH_CHECK_ENDPOINT;
105 return syncRestClient
106 .get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), String.class);
110 public String getBaseUrl() {
114 private Map<String, String> prepareHeaders(String auth, String contentType) {
115 return ImmutableMap.of(
116 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
118 REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
119 CONTENT_TYPE, contentType
123 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
125 .of(() -> tryToCreateTmpFile(csarInputStream))
126 .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
129 private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
130 Path csarFile = Files.createTempFile("csar", ".zip");
131 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
133 LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());