ef4d8c33a9a24635a058dac018bd577b2c2eaaf7
[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.*;
28 import org.springframework.stereotype.Service;
29 import org.springframework.web.client.RestTemplate;
30
31 @Service
32 public class SoClient {
33
34     public static final String RESPONSE_STATUS = "response status : ";
35     public static final String RESPONSE_BODY = "response body : ";
36     public static final String RETURNS = " returns ";
37     public static final String ERROR_ON_CALLING = "error on calling ";
38     @Autowired
39     private RestTemplate restTemplate;
40
41     @Value("${so.host}")
42     private String soHostname;
43
44     @Value("${so.api.id}")
45     private String soApiId;
46
47     @Value("${so.header.authorization}")
48     private String soHeaderAuthorization;
49
50     private static final String HEADER_AUTHORIZATION = "Authorization";
51     private static final String X_FROM_APP_ID = "X-FromAppId";
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(SoClient.class);
54
55
56     public ResponseEntity<CreateServiceInstanceResponse> callCreateServiceInstance(MSOPayload msoPayload) {
57
58         if (LOGGER.isDebugEnabled()) {
59             LOGGER.debug("Calling SO CreateServiceInstance with msoPayload : " + msoPayload.toString());
60         }
61
62         String url = soHostname + OnapComponentsUrlPaths.MSO_CREATE_SERVICE_INSTANCE_PATH;
63
64         try {
65             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.POST,
66                     new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
67
68             logResponsePost(url, response);
69             return response;
70
71         } catch (BackendFunctionalException e) {
72             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
73             return null;
74         }
75     }
76
77     public ResponseEntity<CreateServiceInstanceResponse> callDeleteServiceInstance(MSOPayload msoPayload,
78             String serviceId) {
79
80         if (LOGGER.isDebugEnabled()) {
81             LOGGER.debug("Calling SO DeleteServiceInstance with msoPayload : " + msoPayload.toString());
82         }
83
84         String url = soHostname + OnapComponentsUrlPaths.MSO_DELETE_REQUEST_STATUS_PATH + serviceId;
85
86         try {
87             ResponseEntity<CreateServiceInstanceResponse> response = restTemplate.exchange(url, HttpMethod.DELETE,
88                     new HttpEntity<>(msoPayload, buildRequestHeader()), CreateServiceInstanceResponse.class);
89
90             logResponsePost(url, response);
91             return response;
92
93         } catch (BackendFunctionalException e) {
94             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
95             return null;
96         }
97
98     }
99
100     private void logResponsePost(String url, ResponseEntity<CreateServiceInstanceResponse> response) {
101         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
102         LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
103
104         if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
105             LOGGER.warn("HTTP call SO on " + url + RETURNS + response.getStatusCodeValue() + ", "
106                     + response.getBody().toString());
107         }
108     }
109
110
111     public GetRequestStatusResponse callGetRequestStatus(String requestId) {
112         String url = soHostname + OnapComponentsUrlPaths.MSO_GET_REQUEST_STATUS_PATH + requestId;
113
114         try {
115
116             ResponseEntity<GetRequestStatusResponse> response = restTemplate.exchange(url, HttpMethod.GET,
117                     new HttpEntity<>(buildRequestHeader()), GetRequestStatusResponse.class);
118             logResponseGet(url, response);
119             return response.getBody();
120
121         } catch (BackendFunctionalException e) {
122             LOGGER.error(ERROR_ON_CALLING + url + " ," + e);
123             return null;
124         }
125     }
126
127     private void logResponseGet(String url, ResponseEntity<GetRequestStatusResponse> response) {
128         LOGGER.debug(RESPONSE_BODY + response.getBody().toString());
129         LOGGER.info(RESPONSE_STATUS + response.getStatusCodeValue());
130         if (!response.getStatusCode().equals(HttpStatus.OK)) {
131             LOGGER.warn("HTTP call on " + url + RETURNS + response.getStatusCodeValue() + ", "
132                     + response.getBody().toString());
133         }
134     }
135
136     private HttpHeaders buildRequestHeader() {
137         HttpHeaders httpHeaders = new HttpHeaders();
138         httpHeaders.add(HEADER_AUTHORIZATION, soHeaderAuthorization);
139         httpHeaders.add(X_FROM_APP_ID, soApiId);
140         httpHeaders.add("Accept", "application/json");
141         httpHeaders.add("Content-Type", "application/json");
142         return httpHeaders;
143     }
144
145 }