132e618ba59b1661c87cc38cddfec46823822ef9
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / orchestration / ApiHandlerClient.java
1 package org.onap.so.client.orchestration;
2
3 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
4 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
5 import org.springframework.beans.factory.annotation.Qualifier;
6 import org.springframework.beans.factory.annotation.Value;
7 import org.springframework.http.HttpEntity;
8 import org.springframework.http.HttpHeaders;
9 import org.springframework.http.HttpMethod;
10 import org.springframework.http.MediaType;
11 import org.springframework.stereotype.Component;
12 import org.springframework.web.client.HttpStatusCodeException;
13 import org.springframework.web.client.RestClientException;
14 import org.springframework.web.client.RestTemplate;
15 import static org.onap.so.client.orchestration.RestTemplateApiClientConfig.REST_TEMPLATE_API_HANDLER;
16
17 @Component
18 public class ApiHandlerClient {
19
20     @Value("${mso.adapters.apihandler.serviceInstantiationEndpoint:/onap/so/infra/serviceInstantiation/v7/serviceInstances}")
21     private String serviceInstantiationEndpoint;
22     @Value("${mso.adapters.apihandler.endpoint}")
23     private String baseUri;
24     @Value("${mso.adapters.apihandler.auth}")
25     private String auth;
26
27     private RestTemplate restTemplate;
28
29     public ApiHandlerClient(@Qualifier(REST_TEMPLATE_API_HANDLER) RestTemplate restTemplate) {
30         this.restTemplate = restTemplate;
31     }
32
33     public ServiceInstancesResponse createServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
34             throws ApiHandlerClientException {
35         try {
36             HttpEntity<ServiceInstancesRequest> request = createRequest(serviceInstancesRequest);
37             return restTemplate.exchange(baseUri + serviceInstantiationEndpoint, HttpMethod.POST, request,
38                     ServiceInstancesResponse.class).getBody();
39         } catch (HttpStatusCodeException e) {
40             throw new ApiHandlerClientException("Failed sending service createInstance request to api-handler."
41                     + " Error: " + e.getResponseBodyAsString());
42         } catch (RestClientException e) {
43             throw new ApiHandlerClientException(
44                     "Failed sending service createInstance request to api-handler." + " Error: " + e.getMessage());
45         }
46     }
47
48     public ServiceInstancesResponse deleteServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
49             throws ApiHandlerClientException {
50         try {
51             HttpEntity<ServiceInstancesRequest> request = createRequest(serviceInstancesRequest);
52             return restTemplate.exchange(
53                     baseUri + serviceInstantiationEndpoint
54                             + String.format("/%s", serviceInstancesRequest.getServiceInstanceId()),
55                     HttpMethod.DELETE, request, ServiceInstancesResponse.class).getBody();
56         } catch (HttpStatusCodeException e) {
57             throw new ApiHandlerClientException("Failed sending service deleteInstance request to api-handler."
58                     + " Error: " + e.getResponseBodyAsString());
59         } catch (RestClientException e) {
60             throw new ApiHandlerClientException(
61                     "Failed sending service deleteInstance request to api-handler." + " Error: " + e.getMessage());
62         }
63     }
64
65     private HttpEntity<ServiceInstancesRequest> createRequest(ServiceInstancesRequest serviceInstancesRequest) {
66         HttpHeaders headers = new HttpHeaders();
67         headers.add(HttpHeaders.AUTHORIZATION, auth);
68         headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
69         headers.set(HttpHeaders.ACCEPT, String.valueOf(MediaType.APPLICATION_JSON));
70
71         return new HttpEntity<>(serviceInstancesRequest, headers);
72     }
73 }