2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.nbi.apis.serviceorder;
18 import org.onap.nbi.OnapComponentsUrlPaths;
19 import org.onap.nbi.apis.serviceorder.model.consumer.CreateServiceInstanceResponse;
20 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
21 import org.onap.nbi.apis.serviceorder.model.consumer.MSOPayload;
22 import org.onap.nbi.exceptions.BackendFunctionalException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.http.*;
28 import org.springframework.stereotype.Service;
29 import org.springframework.web.client.RestTemplate;
32 public class SoClient {
34 public static final String RESPONSE_STATUS = "response status : ";
35 public static final String RESPONSE_BODY = "response body : ";
36 public static final String RETURNS = " returns ";
37 public static final String ERROR_ON_CALLING = "error on calling ";
39 private RestTemplate restTemplate;
42 private String soHostname;
44 @Value("${so.api.id}")
45 private String soApiId;
47 @Value("${so.header.authorization}")
48 private String soHeaderAuthorization;
50 private static final String HEADER_AUTHORIZATION = "Authorization";
51 private static final String X_FROM_APP_ID = "X-FromAppId";
53 private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
56 public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(MSOPayload msoPayload) {
58 if (LOGGER.isDebugEnabled()) {
59 LOGGER.debug("Calling SO CreateServiceInstance with msoPayload : " + msoPayload.toString());
62 String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
64 HttpEntity<MSOPayload> requestDetailEntity = new HttpEntity<>(msoPayload, buildRequestHeader());
67 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
68 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
70 logResponsePost(url, response);
73 } catch (BackendFunctionalException e) {
74 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
79 public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(MSOPayload msoPayload,
82 if (LOGGER.isDebugEnabled()) {
83 LOGGER.debug("Calling SO DeleteServiceInstance with msoPayload : " + msoPayload.toString());
86 String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
88 HttpEntity<MSOPayload> requestDetailEntity = new HttpEntity<>(msoPayload, buildRequestHeader());
91 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
92 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
94 logResponsePost(url, response);
97 } catch (BackendFunctionalException e) {
98 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
104 private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
105 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
106 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
108 if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
109 LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
110 + response.getBody().toString());
115 public GetRequestStatusResponse callGetRequestStatus(String requestId) {
116 String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
120 ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
121 new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
122 logResponseGet(url, response);
123 return response.getBody();
125 } catch (BackendFunctionalException e) {
126 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
131 private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
132 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
133 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
134 if (!response.getStatusCode().equals(HttpStatus.OK)) {
135 LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
136 + response.getBody().toString());
140 private HttpHeaders buildRequestHeader() {
141 HttpHeaders httpHeaders = new HttpHeaders();
142 httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
143 httpHeaders.add(X_FROM_APP_ID, soApiId);
144 httpHeaders.add("Accept", "application/json");
145 httpHeaders.add("Content-Type", "application/json");