96d98939aa97a578623fa40e13f45e3b644a7d5c
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / SoClient.java
1 package org.onap.nbi.apis.serviceorder;
2
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;
19
20 @Service
21 public class SoClient {
22
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 ";
27     @Autowired
28     private RestTemplate restTemplate;
29
30     @Value("${so.host}")
31     private String soHostname;
32
33     @Value("${so.api.id}")
34     private String soApiId;
35
36     @Value("${so.header.authorization}")
37     private String soHeaderAuthorization;
38
39     private static final String HEADER_AUTHORIZATION = "Authorization";
40     private static final String X_FROM_APP_ID = "X-FromAppId";
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
43
44
45     public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(RequestDetails requestDetails) {
46
47         if (LOGGER.isDebugEnabled()) {
48             LOGGER.debug("Calling SO CreateServiceInstance with requestDetails : " + requestDetails.toString());
49         }
50
51         String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
52
53         HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
54
55         try {
56             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
57                     new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
58
59             logResponsePost(url, response);
60             return response;
61
62         } catch (BackendFunctionalException e) {
63             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
64             return null;
65         }
66     }
67
68     public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(RequestDetails requestDetails,
69             String serviceId) {
70
71         if (LOGGER.isDebugEnabled()) {
72             LOGGER.debug("Calling SO DeleteServiceInstance with requestDetails : " + requestDetails.toString());
73         }
74
75         String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
76
77         HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
78
79         try {
80             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
81                     new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
82
83             logResponsePost(url, response);
84             return response;
85
86         } catch (BackendFunctionalException e) {
87             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
88             return null;
89         }
90
91     }
92
93     private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
94         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
95         LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
96
97         if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
98             LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
99                     + response.getBody().toString());
100         }
101     }
102
103
104     public GetRequestStatusResponse callGetRequestStatus(String requestId) {
105         String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
106
107         try {
108
109             ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
110                     new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
111             logResponseGet(url, response);
112             return response.getBody();
113
114         } catch (BackendFunctionalException e) {
115             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
116             return null;
117         }
118     }
119
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());
126         }
127     }
128
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");
135         return httpHeaders;
136     }
137
138 }