Merge "Replace SO client"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / rest / SdcRestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 Nokia 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.asdc.rest;
22
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;
35
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;
42 import java.util.Map;
43 import java.util.UUID;
44
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;
55
56 public class SdcRestClient implements AsdcClient {
57
58     private String baseUrl;
59     private String path;
60     private String auth;
61     private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");
62
63     private SyncRestClientInterface syncRestClient;
64
65
66     public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
67         this.syncRestClient = client;
68         this.auth = auth;
69         this.baseUrl = baseUrl;
70         this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
71     }
72
73
74     @Override
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);
78
79         return Try
80                 .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
81                 .getOrElseThrow(AsdcCatalogException::new)
82                 .getBody();
83
84     }
85
86     @Override
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);
90
91         InputStream inputStream = Try
92                 .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()))
93                 .getOrElseThrow(AsdcCatalogException::new)
94                 .getBody();
95
96         return createTmpFile(inputStream);
97     }
98
99
100     private Map<String, String> prepareHeaders(String auth, String contentType) {
101         return ImmutableMap.of(
102                 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
103                 AUTHORIZATION, auth,
104                 REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
105                 CONTENT_TYPE, contentType
106         );
107     }
108
109     private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
110         return Try
111                 .of(() -> tryToCreateTmpFile(csarInputStream))
112                 .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
113     }
114
115     private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
116         Path csarFile = Files.createTempFile("csar", ".zip");
117         Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
118
119         LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());
120
121         return csarFile;
122     }
123 }