aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClient.java
blob: 132e618ba59b1661c87cc38cddfec46823822ef9 (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
package org.onap.so.client.orchestration;

import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import static org.onap.so.client.orchestration.RestTemplateApiClientConfig.REST_TEMPLATE_API_HANDLER;

@Component
public class ApiHandlerClient {

    @Value("${mso.adapters.apihandler.serviceInstantiationEndpoint:/onap/so/infra/serviceInstantiation/v7/serviceInstances}")
    private String serviceInstantiationEndpoint;
    @Value("${mso.adapters.apihandler.endpoint}")
    private String baseUri;
    @Value("${mso.adapters.apihandler.auth}")
    private String auth;

    private RestTemplate restTemplate;

    public ApiHandlerClient(@Qualifier(REST_TEMPLATE_API_HANDLER) RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ServiceInstancesResponse createServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
            throws ApiHandlerClientException {
        try {
            HttpEntity<ServiceInstancesRequest> request = createRequest(serviceInstancesRequest);
            return restTemplate.exchange(baseUri + serviceInstantiationEndpoint, HttpMethod.POST, request,
                    ServiceInstancesResponse.class).getBody();
        } catch (HttpStatusCodeException e) {
            throw new ApiHandlerClientException("Failed sending service createInstance request to api-handler."
                    + " Error: " + e.getResponseBodyAsString());
        } catch (RestClientException e) {
            throw new ApiHandlerClientException(
                    "Failed sending service createInstance request to api-handler." + " Error: " + e.getMessage());
        }
    }

    public ServiceInstancesResponse deleteServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
            throws ApiHandlerClientException {
        try {
            HttpEntity<ServiceInstancesRequest> request = createRequest(serviceInstancesRequest);
            return restTemplate.exchange(
                    baseUri + serviceInstantiationEndpoint
                            + String.format("/%s", serviceInstancesRequest.getServiceInstanceId()),
                    HttpMethod.DELETE, request, ServiceInstancesResponse.class).getBody();
        } catch (HttpStatusCodeException e) {
            throw new ApiHandlerClientException("Failed sending service deleteInstance request to api-handler."
                    + " Error: " + e.getResponseBodyAsString());
        } catch (RestClientException e) {
            throw new ApiHandlerClientException(
                    "Failed sending service deleteInstance request to api-handler." + " Error: " + e.getMessage());
        }
    }

    private HttpEntity<ServiceInstancesRequest> createRequest(ServiceInstancesRequest serviceInstancesRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.AUTHORIZATION, auth);
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        headers.set(HttpHeaders.ACCEPT, String.valueOf(MediaType.APPLICATION_JSON));

        return new HttpEntity<>(serviceInstancesRequest, headers);
    }
}