2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandler.common;
26 import java.io.IOException;
27 import java.util.UUID;
29 import javax.xml.bind.DatatypeConverter;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.StringEntity;
36 import org.onap.logging.ref.slf4j.ONAPLogConstants;
37 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
38 import org.onap.so.apihandler.camundabeans.CamundaInput;
39 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
40 import org.onap.so.apihandler.camundabeans.CamundaRequest;
41 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
42 import org.onap.so.logger.MessageEnum;
43 import org.onap.so.logger.MsoLogger;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
49 public class CamundaClient extends RequestClient{
50 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, CamundaClient.class);
51 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
53 public CamundaClient() {
54 super(CommonConstants.CAMUNDA);
59 public HttpResponse post(String camundaReqXML, String requestId,
60 String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
61 throws ClientProtocolException, IOException{
62 HttpPost post = new HttpPost(url);
63 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
64 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
66 StringEntity input = new StringEntity(jsonReq);
67 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
68 msoLogger.info("Camunda Request Content: " + jsonReq);
71 post.setEntity(input);
74 HttpResponse response = client.execute(post);
75 msoLogger.debug("Response is: " + response);
81 private void setupHeaders(HttpPost post) {
82 post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
83 post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
85 String encryptedCredentials = null;
87 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
88 if(encryptedCredentials != null){
89 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
90 if(userCredentials != null){
91 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
98 public HttpResponse post(String jsonReq)
99 throws ClientProtocolException, IOException{
100 HttpPost post = new HttpPost(url);
101 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
103 StringEntity input = new StringEntity(jsonReq);
104 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
107 String encryptedCredentials = null;
109 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
110 if(encryptedCredentials != null){
111 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
112 if(userCredentials != null){
113 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
119 post.setEntity(input);
120 HttpResponse response = client.execute(post);
121 msoLogger.debug("Response is: " + response);
126 public HttpResponse post(RequestClientParameter parameterObject)
127 throws ClientProtocolException, IOException{
128 HttpPost post = new HttpPost(url);
129 msoLogger.debug(CAMUNDA_URL_MESAGE+ url);
130 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(), parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
131 parameterObject.getServiceInstanceId(), parameterObject.getCorrelationId(), parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(), parameterObject.getNetworkId(), parameterObject.getConfigurationId(),
132 parameterObject.getServiceType(), parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(), parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(), parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
133 parameterObject.getInstanceGroupId());
135 StringEntity input = new StringEntity(jsonReq);
136 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
141 String encryptedCredentials = null;
143 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
144 if(encryptedCredentials != null){
145 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
146 if(userCredentials != null){
147 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
152 post.setEntity(input);
153 HttpResponse response = client.execute(post);
154 msoLogger.debug("Response is: " + response);
160 public HttpResponse get() {
164 protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
165 String jsonReq = null;
168 CamundaRequest camundaRequest = new CamundaRequest();
169 CamundaInput camundaInput = new CamundaInput();
170 CamundaInput host = new CamundaInput();
171 CamundaInput schema = new CamundaInput();
172 CamundaInput reqid = new CamundaInput();
173 CamundaInput svcid = new CamundaInput();
174 CamundaInput timeout = new CamundaInput();
175 camundaInput.setValue(StringUtils.defaultString(reqXML));
176 host.setValue(parseURL());
177 schema.setValue(StringUtils.defaultString(schemaVersion));
178 reqid.setValue(requestId);
179 svcid.setValue(serviceInstanceId);
180 timeout.setValue(StringUtils.defaultString(requestTimeout));
181 camundaRequest.setServiceInput(camundaInput);
182 camundaRequest.setHost(host);
183 camundaRequest.setReqid(reqid);
184 camundaRequest.setSvcid(svcid);
185 camundaRequest.setSchema(schema);
186 camundaRequest.setTimeout(timeout);
187 ObjectMapper mapper = new ObjectMapper();
189 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
191 jsonReq = mapper.writeValueAsString(camundaRequest);
192 msoLogger.trace("request body is " + jsonReq);
194 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
200 protected String wrapVIDRequest(String requestId, boolean isBaseVfModule,
201 int recipeTimeout, String requestAction, String serviceInstanceId, String correlationId,
202 String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
203 String serviceType, String vnfType, String vfModuleType, String networkType,
204 String requestDetails, String apiVersion, boolean aLaCarte, String requestUri, String paramXsd,
205 String instanceGroupId){
206 String jsonReq = null;
209 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
210 CamundaInput serviceInput = new CamundaInput();
211 CamundaInput host = new CamundaInput();
212 CamundaInput requestIdInput= new CamundaInput();
213 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
214 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
215 CamundaInput requestActionInput = new CamundaInput();
216 CamundaInput serviceInstanceIdInput = new CamundaInput();
217 CamundaInput correlationIdInput = new CamundaInput();
218 CamundaInput vnfIdInput = new CamundaInput();
219 CamundaInput vfModuleIdInput = new CamundaInput();
220 CamundaInput volumeGroupIdInput = new CamundaInput();
221 CamundaInput networkIdInput = new CamundaInput();
222 CamundaInput configurationIdInput = new CamundaInput();
223 CamundaInput serviceTypeInput = new CamundaInput();
224 CamundaInput vnfTypeInput = new CamundaInput();
225 CamundaInput vfModuleTypeInput = new CamundaInput();
226 CamundaInput networkTypeInput = new CamundaInput();
227 CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
228 CamundaInput apiVersionInput = new CamundaInput();
229 CamundaInput requestUriInput = new CamundaInput();
230 CamundaInput recipeParamsInput = new CamundaInput();
231 CamundaInput instanceGroupIdInput = new CamundaInput();
233 //host.setValue(parseURL());
234 requestIdInput.setValue(StringUtils.defaultString(requestId));
235 isBaseVfModuleInput.setValue(isBaseVfModule);
236 recipeTimeoutInput.setValue(recipeTimeout);
237 requestActionInput.setValue(StringUtils.defaultString(requestAction));
238 serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
239 correlationIdInput.setValue(StringUtils.defaultString(correlationId));
240 vnfIdInput.setValue(StringUtils.defaultString(vnfId));
241 vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
242 volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
243 networkIdInput.setValue(StringUtils.defaultString(networkId));
244 configurationIdInput.setValue(StringUtils.defaultString(configurationId));
245 serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
246 vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
247 vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
248 networkTypeInput.setValue(StringUtils.defaultString(networkType));
249 aLaCarteInput.setValue(aLaCarte);
250 apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
251 requestUriInput.setValue(StringUtils.defaultString(requestUri));
252 recipeParamsInput.setValue(paramXsd);
253 instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
255 serviceInput.setValue(requestDetails);
256 camundaRequest.setServiceInput(serviceInput);
257 camundaRequest.setHost(host);
258 camundaRequest.setRequestId(requestIdInput);
259 camundaRequest.setMsoRequestId(requestIdInput);
260 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
261 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
262 camundaRequest.setRequestAction(requestActionInput);
263 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
264 camundaRequest.setCorrelationId(correlationIdInput);
265 camundaRequest.setVnfId(vnfIdInput);
266 camundaRequest.setVfModuleId(vfModuleIdInput);
267 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
268 camundaRequest.setNetworkId(networkIdInput);
269 camundaRequest.setConfigurationId(configurationIdInput);
270 camundaRequest.setServiceType(serviceTypeInput);
271 camundaRequest.setVnfType(vnfTypeInput);
272 camundaRequest.setVfModuleType(vfModuleTypeInput);
273 camundaRequest.setNetworkType(networkTypeInput);
274 camundaRequest.setaLaCarte(aLaCarteInput);
275 camundaRequest.setApiVersion(apiVersionInput);
276 camundaRequest.setRequestUri(requestUriInput);
277 camundaRequest.setRecipeParams(recipeParamsInput);
278 camundaRequest.setInstanceGroupId(instanceGroupIdInput);
280 ObjectMapper mapper = new ObjectMapper();
281 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
283 jsonReq = mapper.writeValueAsString(camundaRequest);
284 msoLogger.trace("request body is " + jsonReq);
286 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
291 private String parseURL(){
292 String[] parts = url.split(":");
297 host = host.substring(2);