c3e41f48ca4ba285a2d8a9bc1d0c3968ace60f66
[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.RequestDetails;
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.RestTemplate;
34
35 @Service
36 public class SoClient {
37
38     public static final String RESPONSE_STATUS = "response status : ";
39     public static final String RESPONSE_BODY = "response body : ";
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(RequestDetails requestDetails) {
61
62         if (LOGGER.isDebugEnabled()) {
63             LOGGER.debug("Calling SO CreateServiceInstance with requestDetails : " + requestDetails.toString());
64         }
65
66         String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
67
68         HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
69
70         try {
71             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
72                     new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
73
74             logResponsePost(url, response);
75             return response;
76
77         } catch (BackendFunctionalException e) {
78             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
79             return null;
80         }
81     }
82
83     public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(RequestDetails requestDetails,
84             String serviceId) {
85
86         if (LOGGER.isDebugEnabled()) {
87             LOGGER.debug("Calling SO DeleteServiceInstance with requestDetails : " + requestDetails.toString());
88         }
89
90         String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
91
92         HttpEntity<RequestDetails> requestDetailEntity = new HttpEntity<>(requestDetails, buildRequestHeader());
93
94         try {
95             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
96                     new HttpEntity<>(requestDetailEntity, buildRequestHeader()), CreateServiceInstanceResponse.class);
97
98             logResponsePost(url, response);
99             return response;
100
101         } catch (BackendFunctionalException e) {
102             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
103             return null;
104         }
105
106     }
107
108     private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
109         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
110         LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
111
112         if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
113             LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
114                     + response.getBody().toString());
115         }
116     }
117
118
119     public GetRequestStatusResponse callGetRequestStatus(String requestId) {
120         String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
121
122         try {
123
124             ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
125                     new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
126             logResponseGet(url, response);
127             return response.getBody();
128
129         } catch (BackendFunctionalException e) {
130             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
131             return null;
132         }
133     }
134
135     private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
136         LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
137         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
138         if (!response.getStatusCode().equals(HttpStatus.OK)) {
139             LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
140                     + response.getBody().toString());
141         }
142     }
143
144     private HttpHeaders buildRequestHeader() {
145         HttpHeaders httpHeaders = new HttpHeaders();
146         httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
147         httpHeaders.add(X_FROM_APP_ID, soApiId);
148         httpHeaders.add("Accept", "application/json");
149         httpHeaders.add("Content-Type", "application/json");
150         return httpHeaders;
151     }
152
153 }