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.vavr.control.Try;
26 import org.onap.portalsdk.core.util.SystemProperties;
27 import org.onap.vid.asdc.AsdcCatalogException;
28 import org.onap.vid.asdc.AsdcClient;
29 import org.onap.vid.asdc.beans.Service;
30 import org.onap.vid.client.SyncRestClientInterface;
31 import org.onap.vid.model.ModelConstants;
32 import org.onap.vid.properties.VidProperties;
33 import org.onap.vid.utils.Logging;
34 import org.springframework.http.HttpMethod;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.StandardCopyOption;
41 import java.util.Collections;
43 import java.util.UUID;
45 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
46 import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
47 import static org.onap.portalsdk.core.util.SystemProperties.APP_DISPLAY_NAME;
48 import static org.onap.vid.asdc.AsdcClient.URIS.METADATA_URL_TEMPLATE;
49 import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE;
50 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION;
51 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE;
52 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
53 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
54 import static org.onap.vid.utils.Logging.logRequest;
56 public class SdcRestClient implements AsdcClient {
58 private String baseUrl;
61 private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");
63 private SyncRestClientInterface syncRestClient;
66 public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
67 this.syncRestClient = client;
69 this.baseUrl = baseUrl;
70 this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
75 public Service getService(UUID uuid) throws AsdcCatalogException {
76 String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid);
77 logRequest(LOGGER, HttpMethod.GET, finalUrl);
80 .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
81 .getOrElseThrow(AsdcCatalogException::new)
87 public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException {
88 String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, uuid);
89 logRequest(LOGGER, HttpMethod.GET, finalUrl);
91 InputStream inputStream = Try
92 .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()))
93 .getOrElseThrow(AsdcCatalogException::new)
96 return createTmpFile(inputStream);
100 private Map<String, String> prepareHeaders(String auth, String contentType) {
101 return ImmutableMap.of(
102 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
104 REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
105 CONTENT_TYPE, contentType
109 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
111 .of(() -> tryToCreateTmpFile(csarInputStream))
112 .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
115 private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
116 Path csarFile = Files.createTempFile("csar", ".zip");
117 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
119 LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());