fix sonar alert
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / SoClient.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
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
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nbi.apis.serviceorder;
17
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;
35
36 @Service
37 public class SoClient {
38
39     public static final String RESPONSE_STATUS = "response status : ";
40     public static final String RETURNS = " returns ";
41     public static final String ERROR_ON_CALLING = "error on calling ";
42     @Autowired
43     private RestTemplate restTemplate;
44
45     @Value("${so.host}")
46     private String soHostname;
47
48     @Value("${so.api.id}")
49     private String soApiId;
50
51     @Value("${so.header.authorization}")
52     private String soHeaderAuthorization;
53
54     private static final String HEADER_AUTHORIZATION = "Authorization";
55     private static final String X_FROM_APP_ID = "X-FromAppId";
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
58
59
60     public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(MSOPayload msoPayload) {
61
62         if (LOGGER.isDebugEnabled()) {
63             LOGGER.debug("Calling SO CreateServiceInstance with msoPayload : " + msoPayload.toString());
64         }
65
66         String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
67
68         try {
69             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
70                     new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
71
72             logResponsePost(url, response);
73             return response;
74
75         } catch (BackendFunctionalException|ResourceAccessException e) {
76             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
77             return null;
78         }
79     }
80
81     public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(MSOPayload msoPayload,
82             String serviceId) {
83
84         if (LOGGER.isDebugEnabled()) {
85             LOGGER.debug("Calling SO DeleteServiceInstance with msoPayload : " + msoPayload.toString());
86         }
87
88         String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
89
90         try {
91             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
92                     new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
93
94             logResponsePost(url, response);
95             return response;
96
97         } catch (BackendFunctionalException|ResourceAccessException e) {
98             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
99             return null;
100         }
101
102     }
103
104     private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
105         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
106         if(LOGGER.isDebugEnabled()){
107             LOGGER.debug("response body : {}", response.getBody().toString());
108         }
109
110         if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.CREATED)) {
111             LOGGER.warn("HTTP call SO on {} returns {} , {}",url ,response.getStatusCodeValue(), response.getBody().toString());
112         }
113     }
114
115
116     public GetRequestStatusResponse callGetRequestStatus(String requestId) {
117         String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
118
119         try {
120
121             ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
122                     new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
123             logResponseGet(url, response);
124             return response.getBody();
125
126         } catch (BackendFunctionalException|ResourceAccessException e) {
127             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
128             return null;
129         }
130     }
131
132     private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
133         if(response!=null){
134             if(LOGGER.isDebugEnabled()){
135                 LOGGER.debug("response body : {}", response.getBody().toString());
136             }
137             LOGGER.info("response status : {}", response.getStatusCodeValue());
138             if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
139                 LOGGER.warn("HTTP call SO on {} returns {} , {}",url ,response.getStatusCodeValue(), response.getBody().toString());
140             }
141         } else {
142             LOGGER.info("no response calling url {}",url);
143         }
144     }
145
146     private HttpHeaders buildRequestHeader() {
147         HttpHeaders httpHeaders = new HttpHeaders();
148         httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
149         httpHeaders.add(X_FROM_APP_ID, soApiId);
150         httpHeaders.add("Accept", "application/json");
151         httpHeaders.add("Content-Type", "application/json");
152         return httpHeaders;
153     }
154
155 }