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.util.UUID;
29 import javax.xml.bind.DatatypeConverter;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.client.ClientProtocolException;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.entity.StringEntity;
35 import org.onap.logging.ref.slf4j.ONAPLogConstants;
36 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
37 import org.onap.so.apihandler.camundabeans.CamundaInput;
38 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
39 import org.onap.so.apihandler.camundabeans.CamundaRequest;
40 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
41 import org.onap.so.logger.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
49 public class CamundaClient extends RequestClient {
50 private static Logger logger = LoggerFactory.getLogger(CamundaClient.class);
51 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
52 private static final String CAMUNDA_RESPONSE = "Response is: {}";
53 private static final String AUTHORIZATION = "Authorization";
54 private static final String BASIC = "Basic ";
56 public CamundaClient() {
57 super(CommonConstants.CAMUNDA);
62 public HttpResponse post(String camundaReqXML, String requestId, String requestTimeout, String schemaVersion,
63 String serviceInstanceId, String action) throws ClientProtocolException, IOException {
64 HttpPost post = new HttpPost(url);
65 logger.debug(CAMUNDA_URL_MESAGE + url);
66 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
68 StringEntity input = new StringEntity(jsonReq);
69 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
70 logger.info("Camunda Request Content: {}", jsonReq);
73 post.setEntity(input);
76 HttpResponse response = client.execute(post);
77 logger.debug(CAMUNDA_RESPONSE, response);
83 private void setupHeaders(HttpPost post) {
84 post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
85 post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
87 String encryptedCredentials = null;
89 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
90 if (encryptedCredentials != null) {
91 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
92 props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
93 if (userCredentials != null) {
94 post.addHeader(AUTHORIZATION,
95 BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
102 public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException {
103 HttpPost post = new HttpPost(url);
104 logger.debug(CAMUNDA_URL_MESAGE + url);
106 StringEntity input = new StringEntity(jsonReq);
107 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
110 String encryptedCredentials = null;
112 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
113 if (encryptedCredentials != null) {
114 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
115 props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
116 if (userCredentials != null) {
117 post.addHeader(AUTHORIZATION,
118 BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
124 post.setEntity(input);
125 HttpResponse response = client.execute(post);
126 logger.debug(CAMUNDA_RESPONSE, response);
131 public HttpResponse post(RequestClientParameter parameterObject) throws ClientProtocolException, IOException {
132 HttpPost post = new HttpPost(url);
133 logger.debug(CAMUNDA_URL_MESAGE + url);
134 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
135 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
136 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
137 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
138 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
139 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
140 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
141 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
142 parameterObject.getInstanceGroupId());
144 StringEntity input = new StringEntity(jsonReq);
145 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
150 String encryptedCredentials = null;
152 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
153 if (encryptedCredentials != null) {
154 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
155 props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
156 if (userCredentials != null) {
157 post.addHeader(AUTHORIZATION,
158 BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
163 post.setEntity(input);
164 HttpResponse response = client.execute(post);
165 logger.debug(CAMUNDA_RESPONSE, response);
171 public HttpResponse get() {
175 protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
176 String schemaVersion) {
177 String jsonReq = null;
180 CamundaRequest camundaRequest = new CamundaRequest();
181 CamundaInput camundaInput = new CamundaInput();
182 CamundaInput host = new CamundaInput();
183 CamundaInput schema = new CamundaInput();
184 CamundaInput reqid = new CamundaInput();
185 CamundaInput svcid = new CamundaInput();
186 CamundaInput timeout = new CamundaInput();
187 camundaInput.setValue(StringUtils.defaultString(reqXML));
188 host.setValue(parseURL());
189 schema.setValue(StringUtils.defaultString(schemaVersion));
190 reqid.setValue(requestId);
191 svcid.setValue(serviceInstanceId);
192 timeout.setValue(StringUtils.defaultString(requestTimeout));
193 camundaRequest.setServiceInput(camundaInput);
194 camundaRequest.setHost(host);
195 camundaRequest.setReqid(reqid);
196 camundaRequest.setSvcid(svcid);
197 camundaRequest.setSchema(schema);
198 camundaRequest.setTimeout(timeout);
199 ObjectMapper mapper = new ObjectMapper();
201 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
203 jsonReq = mapper.writeValueAsString(camundaRequest);
204 logger.trace("request body is {}", jsonReq);
205 } catch (Exception e) {
206 logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapRequest",
207 ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
213 protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
214 String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
215 String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
216 String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
217 String paramXsd, String instanceGroupId) {
218 String jsonReq = null;
221 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
222 CamundaInput serviceInput = new CamundaInput();
223 CamundaInput host = new CamundaInput();
224 CamundaInput requestIdInput = new CamundaInput();
225 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
226 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
227 CamundaInput requestActionInput = new CamundaInput();
228 CamundaInput serviceInstanceIdInput = new CamundaInput();
229 CamundaInput pnfCorrelationIdInput = new CamundaInput();
230 CamundaInput vnfIdInput = new CamundaInput();
231 CamundaInput vfModuleIdInput = new CamundaInput();
232 CamundaInput volumeGroupIdInput = new CamundaInput();
233 CamundaInput networkIdInput = new CamundaInput();
234 CamundaInput configurationIdInput = new CamundaInput();
235 CamundaInput serviceTypeInput = new CamundaInput();
236 CamundaInput vnfTypeInput = new CamundaInput();
237 CamundaInput vfModuleTypeInput = new CamundaInput();
238 CamundaInput networkTypeInput = new CamundaInput();
239 CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
240 CamundaInput apiVersionInput = new CamundaInput();
241 CamundaInput requestUriInput = new CamundaInput();
242 CamundaInput recipeParamsInput = new CamundaInput();
243 CamundaInput instanceGroupIdInput = new CamundaInput();
245 // host.setValue(parseURL());
246 requestIdInput.setValue(StringUtils.defaultString(requestId));
247 isBaseVfModuleInput.setValue(isBaseVfModule);
248 recipeTimeoutInput.setValue(recipeTimeout);
249 requestActionInput.setValue(StringUtils.defaultString(requestAction));
250 serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
251 pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
252 vnfIdInput.setValue(StringUtils.defaultString(vnfId));
253 vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
254 volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
255 networkIdInput.setValue(StringUtils.defaultString(networkId));
256 configurationIdInput.setValue(StringUtils.defaultString(configurationId));
257 serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
258 vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
259 vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
260 networkTypeInput.setValue(StringUtils.defaultString(networkType));
261 aLaCarteInput.setValue(aLaCarte);
262 apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
263 requestUriInput.setValue(StringUtils.defaultString(requestUri));
264 recipeParamsInput.setValue(paramXsd);
265 instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
267 serviceInput.setValue(requestDetails);
268 camundaRequest.setServiceInput(serviceInput);
269 camundaRequest.setHost(host);
270 camundaRequest.setRequestId(requestIdInput);
271 camundaRequest.setMsoRequestId(requestIdInput);
272 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
273 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
274 camundaRequest.setRequestAction(requestActionInput);
275 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
276 camundaRequest.setPnfCorrelationId(pnfCorrelationIdInput);
277 camundaRequest.setVnfId(vnfIdInput);
278 camundaRequest.setVfModuleId(vfModuleIdInput);
279 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
280 camundaRequest.setNetworkId(networkIdInput);
281 camundaRequest.setConfigurationId(configurationIdInput);
282 camundaRequest.setServiceType(serviceTypeInput);
283 camundaRequest.setVnfType(vnfTypeInput);
284 camundaRequest.setVfModuleType(vfModuleTypeInput);
285 camundaRequest.setNetworkType(networkTypeInput);
286 camundaRequest.setaLaCarte(aLaCarteInput);
287 camundaRequest.setApiVersion(apiVersionInput);
288 camundaRequest.setRequestUri(requestUriInput);
289 camundaRequest.setRecipeParams(recipeParamsInput);
290 camundaRequest.setInstanceGroupId(instanceGroupIdInput);
292 ObjectMapper mapper = new ObjectMapper();
293 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
295 jsonReq = mapper.writeValueAsString(camundaRequest);
296 logger.trace("request body is {}", jsonReq);
297 } catch (Exception e) {
298 logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapVIDRequest",
299 ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
304 private String parseURL() {
305 String[] parts = url.split(":");
307 if (parts.length >= 2) {
309 if (host.length() > 2) {
310 host = host.substring(2);