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