1 package org.onap.so.client.orchestration;
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;
18 public class ApiHandlerClient {
20 @Value("${mso.adapters.apihandler.serviceInstantiationEndpoint:/onap/so/infra/serviceInstantiation/v7/serviceInstances}")
21 private String serviceInstantiationEndpoint;
22 @Value("${mso.adapters.apihandler.endpoint:http://localhost:8080}")
23 private String baseUri;
24 @Value("${mso.adapters.apihandler.auth:Basic dGVzdDp0ZXN0Cg==}")
27 private RestTemplate restTemplate;
29 public ApiHandlerClient(@Qualifier(REST_TEMPLATE_API_HANDLER) RestTemplate restTemplate) {
30 this.restTemplate = restTemplate;
33 public ServiceInstancesResponse createServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
34 throws ApiHandlerClientException {
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());
48 public ServiceInstancesResponse deleteServiceInstance(ServiceInstancesRequest serviceInstancesRequest)
49 throws ApiHandlerClientException {
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());
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));
71 return new HttpEntity<>(serviceInstancesRequest, headers);