Extend probe mechanism
[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 - 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
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.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;
36
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;
43 import java.util.Map;
44 import java.util.UUID;
45
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;
56
57 public class SdcRestClient implements AsdcClient {
58
59     private String baseUrl;
60     private String path;
61     private String auth;
62     private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");
63
64     private SyncRestClientInterface syncRestClient;
65
66
67     public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
68         this.syncRestClient = client;
69         this.auth = auth;
70         this.baseUrl = baseUrl;
71         this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
72     }
73
74
75     @Override
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);
79
80         return Try
81                 .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
82                 .getOrElseThrow(AsdcCatalogException::new)
83                 .getBody();
84
85     }
86
87     @Override
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);
91
92         InputStream inputStream = Try
93                 .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()))
94                 .getOrElseThrow(AsdcCatalogException::new)
95                 .getBody();
96
97         return createTmpFile(inputStream);
98     }
99
100
101     public HttpResponse<String> checkSDCConnectivity() {
102         String finalUrl = baseUrl + URIS.HEALTH_CHECK_ENDPOINT;
103
104         return syncRestClient
105                 .get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), String.class);
106     }
107
108     private Map<String, String> prepareHeaders(String auth, String contentType) {
109         return ImmutableMap.of(
110                 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
111                 AUTHORIZATION, auth,
112                 REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
113                 CONTENT_TYPE, contentType
114         );
115     }
116
117     private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
118         return Try
119                 .of(() -> tryToCreateTmpFile(csarInputStream))
120                 .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
121     }
122
123     private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
124         Path csarFile = Files.createTempFile("csar", ".zip");
125         Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
126
127         LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());
128
129         return csarFile;
130     }
131 }