3 * Copyright (c) 2017 Orange. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.onap.nbi.apis.serviceorder;
19 import org.onap.nbi.OnapComponentsUrlPaths;
20 import org.onap.nbi.apis.serviceorder.model.consumer.CreateServiceInstanceResponse;
21 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
22 import org.onap.nbi.apis.serviceorder.model.consumer.RequestDetails;
23 import org.onap.nbi.exceptions.BackendFunctionalException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpMethod;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.client.RestTemplate;
37 public class SoClient {
39 public static final String RESPONSE_STATUS = "response status : ";
40 public static final String RESPONSE_BODY = "response body : ";
41 public static final String RETURNS = " returns ";
42 public static final String ERROR_ON_CALLING = "error on calling ";
44 private RestTemplate restTemplate;
47 private String soHostname;
49 @Value("${so.api.id}")
50 private String soApiId;
52 @Value("${so.header.authorization}")
53 private String soHeaderAuthorization;
55 private static final String HEADER_AUTHORIZATION = "Authorization";
56 private static final String X_FROM_APP_ID = "X-FromAppId";
58 private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
61 public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(RequestDetails requestDetails) {
63 if (LOGGER.isDebugEnabled()) {
64 LOGGER.debug("Calling SO CreateServiceInstance with requestDetails : " + requestDetails.toString());
67 String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
69 HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
72 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
73 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
75 logResponsePost(url, response);
78 } catch (BackendFunctionalException e) {
79 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
84 public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(RequestDetails requestDetails,
87 if (LOGGER.isDebugEnabled()) {
88 LOGGER.debug("Calling SO DeleteServiceInstance with requestDetails : " + requestDetails.toString());
91 String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
93 HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
96 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
97 new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
99 logResponsePost(url, response);
102 } catch (BackendFunctionalException e) {
103 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
109 private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
110 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
111 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
113 if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
114 LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
115 + response.getBody().toString());
120 public GetRequestStatusResponse callGetRequestStatus(String requestId) {
121 String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
125 ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
126 new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
127 logResponseGet(url, response);
128 return response.getBody();
130 } catch (BackendFunctionalException e) {
131 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
136 private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
137 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
138 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
139 if (!response.getStatusCode().equals(HttpStatus.OK)) {
140 LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
141 + response.getBody().toString());
145 private HttpHeaders buildRequestHeader() {
146 HttpHeaders httpHeaders = new HttpHeaders();
147 httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
148 httpHeaders.add(X_FROM_APP_ID, soApiId);
149 httpHeaders.add("Accept", "application/json");
150 httpHeaders.add("Content-Type", "application/json");