summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/client/SdcRestClient.java
blob: 058d9c7b7b7b2a771bb345191d139fcd6d628222 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package org.onap.sdc.dcae.client;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
import org.onap.sdc.dcae.composition.restmodels.ReferenceUUID;
import org.onap.sdc.dcae.composition.restmodels.sdc.*;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.composition.util.SystemProperties;
import org.onap.sdc.dcae.enums.AssetType;
import org.onap.sdc.dcae.enums.SdcConsumerInfo;
import org.onap.sdc.dcae.utils.Normalizers;
import org.onap.sdc.dcae.utils.SDCResponseErrorHandler;
import org.onap.sdc.dcae.utils.SdcRestClientUtils;
import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.*;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component("sdcrestclient")
public class SdcRestClient implements ISdcClient {

    @Autowired
    private SystemProperties systemProperties;

    private static final String SLASH = "/";
    private static final String ECOMP_INSTANCE_ID_HEADER = "X-ECOMP-InstanceID";
    private static final String ECOMP_REQUEST_ID_HEADER = "X-ECOMP-RequestID";
    private static final String USER_ID_HEADER = "USER_ID";
    private static final String RESOURCES_PATH = "resources";
    private static final String SERVICES_PATH = "services";
    private static final String ARTIFACTS_PATH = "artifacts";
    private static final String CONTENT_MD5_HEADER = "Content-MD5";
    private static final String RESOURCE_INSTANCES_PATH = "resourceInstances";
    private static final String LIFECYCLE_STATE_PATH = "lifecycleState/{lifecycleOperation}";
    private static final String METADATA_PATH = "metadata";
    private static final String VERSION_PATH = "version";
    private static final String MONITORING_REFERENCES_PATH = "externalReferences/monitoring";

    private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

    private String uri;

    private RestTemplate client;

    @PostConstruct
    private void init() {
        URI configUri = URI.create(systemProperties.getProperties().getProperty(DcaeBeConstants.Config.URI));
        EnumMap<SdcConsumerInfo, String> userInfo = SdcRestClientUtils.extractConsumerInfoFromUri(configUri);
        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultHeaders(defaultHeaders(userInfo)).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        client = new RestTemplate(requestFactory);
        client.setErrorHandler(new SDCResponseErrorHandler());
        uri = userInfo.get(SdcConsumerInfo.CATALOG_URL);
    }

    private List<BasicHeader> defaultHeaders(EnumMap<SdcConsumerInfo, String> userInfo) {
        List<BasicHeader> headers = new ArrayList<>();
        headers.add(new BasicHeader(HttpHeaders.AUTHORIZATION, userInfo.get(SdcConsumerInfo.AUTH)));
        headers.add(new BasicHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE));
        headers.add(new BasicHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE));
        headers.add(new BasicHeader(ECOMP_INSTANCE_ID_HEADER, userInfo.get(SdcConsumerInfo.INSTANCE_ID)));
        return headers;
    }

    public ResourceDetailed getResource(String uuid, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH, uuid, METADATA_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get resource from SDC. URL={}", url);
        return getObject(url, requestId, ResourceDetailed.class);
    }

    public ServiceDetailed getService(String uuid, String requestId) {
        String url = buildRequestPath(SERVICES_PATH, uuid, METADATA_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get service from SDC. URL={}", url);
        return getObject(url, requestId, ServiceDetailed.class);
    }

    public List<Resource> getResources(String resourceType, String category, String subcategory, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH, SdcRestClientUtils.buildResourceFilterQuery(resourceType, category, subcategory));
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get resources from SDC. URL={}", url);
        return Arrays.asList(getObject(url, requestId, Resource[].class));
    }

    public List<Service> getServices(String requestId) {
        String url = buildRequestPath(SERVICES_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get services from SDC. URL={}", url);
        return Arrays.asList(getObject(url, requestId, Service[].class));
    }

    public String addExternalMonitoringReference(String userId, CreateVFCMTRequest resource, ReferenceUUID vfcmtUuid, String requestId) {
        String url = buildRequestPath(resource.getContextType(), resource.getServiceUuid(), RESOURCE_INSTANCES_PATH,
                Normalizers.normalizeComponentInstanceName(resource.getVfiName()), MONITORING_REFERENCES_PATH);

        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Connecting service id {} name {} to vfcmt {} URL={}",
                resource.getServiceUuid(), resource.getVfiName(), vfcmtUuid.getReferenceUUID(), url);

        return client.postForObject(url, new HttpEntity<>(vfcmtUuid, postResourceHeaders(userId, requestId)),
                String.class);
    }

    public void deleteExternalMonitoringReference(String userId, String context, String uuid, String normalizeVfiName, String vfcmtUuid, String requestId) {
        String url = buildRequestPath(context, uuid, RESOURCE_INSTANCES_PATH,
                normalizeVfiName, MONITORING_REFERENCES_PATH, vfcmtUuid);
        client.exchange(url, HttpMethod.DELETE, new HttpEntity(postResourceHeaders(userId, requestId)), String.class);
    }

    public ResourceDetailed createResource(String userId, CreateVFCMTRequest resource, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Create SDC resource with name {} URL={}", resource.getName(), url);
        return client.postForObject(url, new HttpEntity<>(resource, postResourceHeaders(userId, requestId)), ResourceDetailed.class);
    }

    public ResourceDetailed changeResourceLifecycleState(String userId, String uuid, String lifecycleOperation, String userRemarks, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH, uuid, LIFECYCLE_STATE_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Change SDC resource lifecycle state ({}). URL={}", lifecycleOperation, url);
        return client.postForObject(url, new HttpEntity<>(SdcRestClientUtils.buildUserRemarksObject(userRemarks), postResourceHeaders(userId, requestId)), ResourceDetailed.class, lifecycleOperation);
    }

    public ServiceDetailed changeServiceLifecycleState(String userId, String uuid, String lifecycleOperation, String userRemarks, String requestId) {
        String url = buildRequestPath(SERVICES_PATH, uuid, LIFECYCLE_STATE_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Change SDC service lifecycle state ({}). URL={}", lifecycleOperation, url);
        return client.postForObject(url, new HttpEntity<>(SdcRestClientUtils.buildUserRemarksObject(userRemarks), postResourceHeaders(userId, requestId)), ServiceDetailed.class, lifecycleOperation);
    }

    public Asset changeAssetLifecycleState(String userId, String uuid, String lifecycleOperation, String userRemarks, AssetType assetType, String requestId) {
        return AssetType.RESOURCE == assetType ? changeResourceLifecycleState(userId, uuid, lifecycleOperation, userRemarks, requestId) : changeServiceLifecycleState(userId, uuid, lifecycleOperation, userRemarks, requestId);
    }

    public String getResourceArtifact(String resourceUuid, String artifactUuid, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH, resourceUuid, ARTIFACTS_PATH, artifactUuid);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get resource artifact from SDC. URL={}", url);
        return getObject(url, requestId, String.class);
    }

    public Artifact createResourceArtifact(String userId, String resourceUuid, Artifact artifact, String requestId) throws Exception {
        String url = buildRequestPath(RESOURCES_PATH, resourceUuid, ARTIFACTS_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Create SDC resource artifact. URL={}", url);
        String artifactData = SdcRestClientUtils.artifactToString(artifact);
        return client.postForObject(url, new HttpEntity<>(artifactData, postArtifactHeaders(userId, artifactData, requestId)), Artifact.class);
    }

    public Artifact updateResourceArtifact(String userId, String resourceUuid, Artifact artifact, String requestId) throws Exception {
        String url = buildRequestPath(RESOURCES_PATH, resourceUuid, ARTIFACTS_PATH, artifact.getArtifactUUID());
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Update SDC resource artifact. URL={}", url);
        String artifactData = SdcRestClientUtils.artifactToString(artifact);
        return client.postForObject(url, new HttpEntity<>(artifactData, postArtifactHeaders(userId, artifactData, requestId)), Artifact.class);
    }

    public void deleteResourceArtifact(String userId, String resourceUuid, String artifactId, String requestId) {
        String url = buildRequestPath(RESOURCES_PATH, resourceUuid, ARTIFACTS_PATH, artifactId);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Delete SDC resource artifact. URL={}", url);
        client.exchange(url, HttpMethod.DELETE, new HttpEntity(postResourceHeaders(userId, requestId)), Artifact.class);
    }

    public Artifact createVfInstanceArtifact(String userId, String serviceUuid, String normalizedInstanceName, Artifact artifact, String requestId) throws Exception {
        String url = buildRequestPath(SERVICES_PATH, serviceUuid, RESOURCE_INSTANCES_PATH, normalizedInstanceName, ARTIFACTS_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Create SDC resource instance artifact. URL={}", url);
        String artifactData = SdcRestClientUtils.artifactToString(artifact);
        return client.postForObject(url, new HttpEntity<>(artifactData, postArtifactHeaders(userId, artifactData, requestId)), Artifact.class);
    }

    public Artifact updateVfInstanceArtifact(String userId, String serviceUuid, String normalizedInstanceName, Artifact artifact, String requestId) throws Exception {
        String url = buildRequestPath(SERVICES_PATH, serviceUuid, RESOURCE_INSTANCES_PATH, normalizedInstanceName, ARTIFACTS_PATH, artifact.getArtifactUUID());
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Update SDC resource instance artifact. URL={}", url);
        String artifactData = SdcRestClientUtils.artifactToString(artifact);
        return client.postForObject(url, new HttpEntity<>(artifactData, postArtifactHeaders(userId, artifactData, requestId)), Artifact.class);
    }

    public ExternalReferencesMap getMonitoringReferences(String context, String uuid, String version, String requestId) {
        String url = buildRequestPath(context, uuid, VERSION_PATH, version, MONITORING_REFERENCES_PATH);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Get SDC service monitoring references. URL={}", url);
        return getObject(url, requestId, ExternalReferencesMap.class);
    }

    public void deleteInstanceResourceArtifact(String userId, String context, String serviceUuid, String normalizedVfiName, String artifactUuid, String requestId) {
        String url = buildRequestPath(context, serviceUuid, RESOURCE_INSTANCES_PATH, normalizedVfiName, ARTIFACTS_PATH, artifactUuid);
        debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Delete SDC instance resource artifact. URL={}", url);
        client.exchange(url, HttpMethod.DELETE, new HttpEntity(postResourceHeaders(userId, requestId)), Artifact.class);
    }

    private HttpHeaders postResourceHeaders(String userId, String requestId) {
        HttpHeaders headers = requestHeader(requestId);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add(USER_ID_HEADER, userId);
        return headers;
    }

    private HttpHeaders postArtifactHeaders(String userId, String artifact, String requestId) {
        HttpHeaders headers = postResourceHeaders(userId, requestId);
        String md5 = Base64Utils.encodeToString(DigestUtils.md5Hex(artifact).getBytes());
        headers.add(CONTENT_MD5_HEADER, md5);
        return headers;
    }

    private HttpHeaders requestHeader(String requestId){
        HttpHeaders headers = new HttpHeaders();
        headers.add(ECOMP_REQUEST_ID_HEADER, requestId);
        return headers;
    }

    private <T> T getObject(String url, String requestId, Class<T> clazz) {
        return client.exchange(url, HttpMethod.GET, new HttpEntity<>(requestHeader(requestId)), clazz).getBody();
    }

    private String buildRequestPath(String... args){
        return uri + Stream.of(args).collect(Collectors.joining(SLASH));
    }
}