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.HttpEntity;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Service;
33 import org.springframework.web.client.ResourceAccessException;
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(MSOPayload msoPayload) {
63 if (LOGGER.isDebugEnabled()) {
64 LOGGER.debug("Calling SO CreateServiceInstance with msoPayload : " + msoPayload.toString());
67 String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
70 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
71 new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
73 logResponsePost(url, response);
76 } catch (BackendFunctionalException|ResourceAccessException e) {
77 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
82 public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(MSOPayload msoPayload,
85 if (LOGGER.isDebugEnabled()) {
86 LOGGER.debug("Calling SO DeleteServiceInstance with msoPayload : " + msoPayload.toString());
89 String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
92 ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
93 new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
95 logResponsePost(url, response);
98 } catch (BackendFunctionalException|ResourceAccessException e) {
99 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
105 private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
106 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
107 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
109 if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
110 LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
111 + response.getBody().toString());
116 public GetRequestStatusResponse callGetRequestStatus(String requestId) {
117 String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
121 ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
122 new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
123 logResponseGet(url, response);
124 return response.getBody();
126 } catch (BackendFunctionalException|ResourceAccessException e) {
127 LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
132 private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
134 LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
135 LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
136 if (!response.getStatusCode().equals(HttpStatus.OK)) {
137 LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
138 + response.getBody().toString());
141 LOGGER.info("no response calling url {}",url);
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");