1 package org.onap.nbi.apis.serviceorder;
3 import org.onap.nbi.OnapComponentsUrlPaths;
4 import org.onap.nbi.apis.serviceorder.model.consumer.CreateServiceInstanceResponse;
5 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
6 import org.onap.nbi.apis.serviceorder.model.consumer.RequestDetails;
7 import org.onap.nbi.exceptions.BackendFunctionalException;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.http.HttpEntity;
13 import org.springframework.http.HttpHeaders;
14 import org.springframework.http.HttpMethod;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.stereotype.Service;
18 import org.springframework.web.client.RestTemplate;
21 public class SoClient {
23 public static final String RESPONSE_STATUS = "response status : ";
24 public static final String RESPONSE_BODY = "response body : ";
25 public static final String RETURNS = " returns ";
26 public static final String ERROR_ON_CALLING = "error on calling ";
28 private RestTemplate restTemplate;
31 private String soHostname;
33 @Value("${so.api.id}")
34 private String soApiId;
36 @Value("${so.header.authorization}")
37 private String soHeaderAuthorization;
39 private static final String HEADER_AUTHORIZATION = "Authorization";
40 private static final String X_FROM_APP_ID = "X-FromAppId";
42 private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
45 public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(RequestDetails requestDetails) {
47 if (LOGGER.isDebugEnabled()) {
48 LOGGER.debug("Calling SO CreateServiceInstance with requestDetails : " + requestDetails.toString());
51 String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
53 HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
56 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
57 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
59 logResponsePost(url, response);
62 } catch (BackendFunctionalException e) {
63 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
68 public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(RequestDetails requestDetails,
71 if (LOGGER.isDebugEnabled()) {
72 LOGGER.debug("Calling SO DeleteServiceInstance with requestDetails : " + requestDetails.toString());
75 String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
77 HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
80 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
81 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
83 logResponsePost(url, response);
86 } catch (BackendFunctionalException e) {
87 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
93 private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
94 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
95 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
97 if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
98 LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
99 + response.getBody().toString());
104 public GetRequestStatusResponse callGetRequestStatus(String requestId) {
105 String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
109 ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
110 new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
111 logResponseGet(url, response);
112 return response.getBody();
114 } catch (BackendFunctionalException e) {
115 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
120 private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
121 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
122 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
123 if (!response.getStatusCode().equals(HttpStatus.OK)) {
124 LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
125 + response.getBody().toString());
129 private HttpHeaders buildRequestHeader() {
130 HttpHeaders httpHeaders = new HttpHeaders();
131 httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
132 httpHeaders.add(X_FROM_APP_ID, soApiId);
133 httpHeaders.add("Accept", "application/json");
134 httpHeaders.add("Content-Type", "application/json");