2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.apihandler.common;
27 import java.io.IOException;
28 import java.security.GeneralSecurityException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import javax.xml.bind.DatatypeConverter;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.http.HttpStatus;
34 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
35 import org.onap.so.apihandler.camundabeans.CamundaInput;
36 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
37 import org.onap.so.apihandler.camundabeans.CamundaRequest;
38 import org.onap.so.apihandler.camundabeans.CamundaResponse;
39 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
40 import org.onap.so.apihandlerinfra.exceptions.ApiException;
41 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
42 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
43 import org.onap.so.utils.CryptoUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.core.env.Environment;
48 import org.springframework.http.HttpEntity;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.stereotype.Component;
53 import org.springframework.web.client.HttpStatusCodeException;
54 import org.springframework.web.client.ResourceAccessException;
55 import org.springframework.web.client.RestTemplate;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.fasterxml.jackson.databind.SerializationFeature;
60 public class CamundaClient {
61 private static Logger logger = LoggerFactory.getLogger(CamundaClient.class);
62 private static final String BASIC = "Basic ";
65 private RestTemplate restTemplate;
68 private Environment env;
71 private ResponseHandler responseHandler;
73 public ResponseEntity<String> post(String camundaReqXML, String requestId, String requestTimeout,
74 String schemaVersion, String serviceInstanceId, String action, String orchestrationURI)
76 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion,
78 logger.info("Camunda Request Content: {}", jsonReq);
80 return post(jsonReq, orchestrationURI);
83 public ResponseEntity<String> post(RequestClientParameter parameterObject, String orchestrationURI)
85 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
86 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
87 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
88 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
89 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
90 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
91 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
92 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
93 parameterObject.getInstanceGroupId(), parameterObject.isGenerateIdsOnly());
95 return post(jsonReq, orchestrationURI);
98 public ResponseEntity<String> get(String url) throws ApiException {
99 url = env.getRequiredProperty(CommonConstants.CAMUNDA_URL) + url;
100 HttpEntity<?> requestEntity = new HttpEntity<>(setHeaders());
102 return restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
103 } catch (HttpStatusCodeException e) {
104 logger.error("Error returned from sending GET request to BPMN", e);
105 throw createBPMNFailureException(e);
106 } catch (ResourceAccessException e) {
107 logger.error("Error sending GET to BPMN", e);
108 ClientConnectionException clientException = new ClientConnectionException.Builder(url,
109 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).build();
110 throw clientException;
114 public ResponseEntity<String> post(String jsonReq, String url) throws ApiException {
115 url = env.getRequiredProperty(CommonConstants.CAMUNDA_URL) + url;
116 HttpEntity<String> request = new HttpEntity<String>(jsonReq, setHeaders());
118 return restTemplate.postForEntity(url, request, String.class);
119 } catch (HttpStatusCodeException e) {
120 logger.error("Error returned after sending POST request to BPMN", e);
121 throw createBPMNFailureException(e);
122 } catch (ResourceAccessException e) {
123 logger.error("Error sending POST to BPMN", e);
124 ClientConnectionException clientException = new ClientConnectionException.Builder(url,
125 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).build();
126 throw clientException;
130 protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
131 String schemaVersion, String url) {
132 String jsonReq = null;
134 CamundaRequest camundaRequest = new CamundaRequest();
135 CamundaInput camundaInput = new CamundaInput();
136 CamundaInput host = new CamundaInput();
137 CamundaInput schema = new CamundaInput();
138 CamundaInput reqid = new CamundaInput();
139 CamundaInput svcid = new CamundaInput();
140 CamundaInput timeout = new CamundaInput();
141 camundaInput.setValue(StringUtils.defaultString(reqXML));
142 host.setValue(CommonConstants.CAMUNDA_URL);
143 schema.setValue(StringUtils.defaultString(schemaVersion));
144 reqid.setValue(requestId);
145 svcid.setValue(serviceInstanceId);
146 timeout.setValue(StringUtils.defaultString(requestTimeout));
147 camundaRequest.setServiceInput(camundaInput);
148 camundaRequest.setHost(host);
149 camundaRequest.setReqid(reqid);
150 camundaRequest.setSvcid(svcid);
151 camundaRequest.setSchema(schema);
152 camundaRequest.setTimeout(timeout);
153 ObjectMapper mapper = new ObjectMapper();
155 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
157 jsonReq = mapper.writeValueAsString(camundaRequest);
158 logger.trace("request body is {}", jsonReq);
159 } catch (Exception e) {
160 logger.error("Error in APIH Wrap request", e);
166 protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
167 String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
168 String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
169 String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
170 String paramXsd, String instanceGroupId, boolean generateIdsOnly) {
171 String jsonReq = null;
174 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
175 CamundaInput serviceInput = new CamundaInput();
176 CamundaInput host = new CamundaInput();
177 CamundaInput requestIdInput = new CamundaInput();
178 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
179 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
180 CamundaInput requestActionInput = new CamundaInput();
181 CamundaInput serviceInstanceIdInput = new CamundaInput();
182 CamundaInput pnfCorrelationIdInput = new CamundaInput();
183 CamundaInput vnfIdInput = new CamundaInput();
184 CamundaInput vfModuleIdInput = new CamundaInput();
185 CamundaInput volumeGroupIdInput = new CamundaInput();
186 CamundaInput networkIdInput = new CamundaInput();
187 CamundaInput configurationIdInput = new CamundaInput();
188 CamundaInput serviceTypeInput = new CamundaInput();
189 CamundaInput vnfTypeInput = new CamundaInput();
190 CamundaInput vfModuleTypeInput = new CamundaInput();
191 CamundaInput networkTypeInput = new CamundaInput();
192 CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
193 CamundaInput apiVersionInput = new CamundaInput();
194 CamundaInput requestUriInput = new CamundaInput();
195 CamundaInput recipeParamsInput = new CamundaInput();
196 CamundaInput instanceGroupIdInput = new CamundaInput();
197 CamundaBooleanInput generateIds = new CamundaBooleanInput();
200 requestIdInput.setValue(StringUtils.defaultString(requestId));
201 isBaseVfModuleInput.setValue(isBaseVfModule);
202 recipeTimeoutInput.setValue(recipeTimeout);
203 requestActionInput.setValue(StringUtils.defaultString(requestAction));
204 serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
205 pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
206 vnfIdInput.setValue(StringUtils.defaultString(vnfId));
207 vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
208 volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
209 networkIdInput.setValue(StringUtils.defaultString(networkId));
210 configurationIdInput.setValue(StringUtils.defaultString(configurationId));
211 serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
212 vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
213 vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
214 networkTypeInput.setValue(StringUtils.defaultString(networkType));
215 aLaCarteInput.setValue(aLaCarte);
216 apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
217 requestUriInput.setValue(StringUtils.defaultString(requestUri));
218 recipeParamsInput.setValue(paramXsd);
219 instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
220 generateIds.setValue(generateIdsOnly);
222 serviceInput.setValue(requestDetails);
223 camundaRequest.setServiceInput(serviceInput);
224 camundaRequest.setHost(host);
225 camundaRequest.setRequestId(requestIdInput);
226 camundaRequest.setMsoRequestId(requestIdInput);
227 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
228 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
229 camundaRequest.setRequestAction(requestActionInput);
230 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
231 camundaRequest.setPnfCorrelationId(pnfCorrelationIdInput);
232 camundaRequest.setVnfId(vnfIdInput);
233 camundaRequest.setVfModuleId(vfModuleIdInput);
234 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
235 camundaRequest.setNetworkId(networkIdInput);
236 camundaRequest.setConfigurationId(configurationIdInput);
237 camundaRequest.setServiceType(serviceTypeInput);
238 camundaRequest.setVnfType(vnfTypeInput);
239 camundaRequest.setVfModuleType(vfModuleTypeInput);
240 camundaRequest.setNetworkType(networkTypeInput);
241 camundaRequest.setaLaCarte(aLaCarteInput);
242 camundaRequest.setApiVersion(apiVersionInput);
243 camundaRequest.setRequestUri(requestUriInput);
244 camundaRequest.setRecipeParams(recipeParamsInput);
245 camundaRequest.setInstanceGroupId(instanceGroupIdInput);
246 camundaRequest.setGenerateIds(generateIds);
248 ObjectMapper mapper = new ObjectMapper();
249 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
251 jsonReq = mapper.writeValueAsString(camundaRequest);
252 logger.trace("request body is {}", jsonReq);
253 } catch (Exception e) {
254 logger.error("Error in wrapVIDRequest", e);
259 protected HttpHeaders setHeaders() {
260 HttpHeaders headers = new HttpHeaders();
261 List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>();
262 acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
263 headers.setAccept(acceptableMediaTypes);
264 headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
265 headers.add(HttpHeaders.AUTHORIZATION, addAuthorizationHeader(env.getRequiredProperty("mso.camundaAuth"),
266 env.getRequiredProperty("mso.msoKey")));
270 protected String addAuthorizationHeader(String auth, String msoKey) {
271 String basicAuth = null;
273 String userCredentials = CryptoUtils.decrypt(auth, msoKey);
274 if (userCredentials != null) {
275 basicAuth = BASIC + DatatypeConverter.printBase64Binary(userCredentials.getBytes());
277 } catch (GeneralSecurityException e) {
278 logger.error("Security exception", e);
283 protected BPMNFailureException createBPMNFailureException(HttpStatusCodeException e) {
284 ObjectMapper mapper = new ObjectMapper();
285 String responseText = null;
286 String message = null;
288 CamundaResponse response = mapper.readValue(e.getResponseBodyAsString(), CamundaResponse.class);
289 responseText = response.getResponse();
290 } catch (IOException ex) {
291 responseText = e.getResponseBodyAsString();
293 message = String.valueOf(e.getStatusCode());
294 if (responseText != null && !responseText.isEmpty()) {
295 message = message + " " + responseText;
297 BPMNFailureException bpmnException =
298 new BPMNFailureException.Builder(message, responseHandler.setStatus(e.getStatusCode().value()),
299 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR, e.getStatusCode()).build();
300 return bpmnException;