diff options
author | Jozsef Csongvai <jozsef.csongvai@bell.ca> | 2021-11-26 15:31:40 -0500 |
---|---|---|
committer | Jozsef Csongvai <jozsef.csongvai@bell.ca> | 2021-11-26 16:52:57 -0500 |
commit | 7861185045a47e86ea34afb22038cce3ab4196b7 (patch) | |
tree | 15c3cf56b112184a1faebec08b88b7ab54121d20 /bpmn/so-bpmn-tasks/src/main/java/org/onap | |
parent | 634473225cfa557fde97ba507805db7621462742 (diff) |
Add REST client for Api-handler
This enables calling api-handler from bpmn-infra
Issue-ID: SO-3811
Signed-off-by: Jozsef Csongvai <jozsef.csongvai@bell.ca>
Change-Id: I59a6c7859505f9fca4729089914f43834ceb6323
Diffstat (limited to 'bpmn/so-bpmn-tasks/src/main/java/org/onap')
3 files changed, 104 insertions, 0 deletions
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClient.java new file mode 100644 index 0000000000..132e618ba5 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClient.java @@ -0,0 +1,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); + } +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClientException.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClientException.java new file mode 100644 index 0000000000..0a4c60cdf7 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/ApiHandlerClientException.java @@ -0,0 +1,8 @@ +package org.onap.so.client.orchestration; + +public class ApiHandlerClientException extends Exception { + + public ApiHandlerClientException(String errorMessage) { + super(errorMessage); + } +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/RestTemplateApiClientConfig.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/RestTemplateApiClientConfig.java new file mode 100644 index 0000000000..a01fbe18f8 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/RestTemplateApiClientConfig.java @@ -0,0 +1,23 @@ +package org.onap.so.client.orchestration; + +import org.onap.logging.filter.spring.SpringClientPayloadFilter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateApiClientConfig { + public static final String REST_TEMPLATE_API_HANDLER = "restTemplateApiHandler"; + + @Bean(REST_TEMPLATE_API_HANDLER) + public RestTemplate restTemplate() { + final RestTemplate restTemplate = new RestTemplate(); + restTemplate + .setRequestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory())); + restTemplate.getInterceptors().add((new SpringClientPayloadFilter())); + return restTemplate; + } + +} |