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