aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java
blob: ab07aaeed78051e2bc8c1958d21b7c9c1d9af344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2018 - 2019 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.asdc.rest;

import com.att.eelf.configuration.EELFLogger;
import com.google.common.collect.ImmutableMap;
import io.vavr.control.Try;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.AsdcClient;
import org.onap.vid.asdc.beans.Service;
import org.onap.vid.client.SyncRestClientInterface;
import org.onap.vid.model.ModelConstants;
import org.onap.vid.properties.VidProperties;
import org.onap.vid.utils.Logging;
import org.springframework.http.HttpMethod;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
import static org.onap.portalsdk.core.util.SystemProperties.APP_DISPLAY_NAME;
import static org.onap.vid.asdc.AsdcClient.URIS.METADATA_URL_TEMPLATE;
import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE;
import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION;
import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE;
import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
import static org.onap.vid.utils.Logging.logRequest;

public class SdcRestClient implements AsdcClient {

    private String baseUrl;
    private String path;
    private String auth;
    private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc");

    private SyncRestClientInterface syncRestClient;


    public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) {
        this.syncRestClient = client;
        this.auth = auth;
        this.baseUrl = baseUrl;
        this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
    }


    @Override
    public Service getService(UUID uuid) throws AsdcCatalogException {
        String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid);
        logRequest(LOGGER, HttpMethod.GET, finalUrl);

        return Try
                .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class))
                .getOrElseThrow(AsdcCatalogException::new)
                .getBody();

    }

    @Override
    public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException {
        String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, uuid);
        logRequest(LOGGER, HttpMethod.GET, finalUrl);

        InputStream inputStream = Try
                .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()))
                .getOrElseThrow(AsdcCatalogException::new)
                .getBody();

        return createTmpFile(inputStream);
    }


    private Map<String, String> prepareHeaders(String auth, String contentType) {
        return ImmutableMap.of(
                X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
                AUTHORIZATION, auth,
                REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
                CONTENT_TYPE, contentType
        );
    }

    private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
        return Try
                .of(() -> tryToCreateTmpFile(csarInputStream))
                .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable));
    }

    private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException {
        Path csarFile = Files.createTempFile("csar", ".zip");
        Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);

        LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());

        return csarFile;
    }
}