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;
28 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.so.apihandler.camundabeans.CamundaBooleanInput;
36 import org.onap.so.apihandler.camundabeans.CamundaInput;
37 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
38 import org.onap.so.apihandler.camundabeans.CamundaRequest;
39 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
40 import org.onap.so.logger.MessageEnum;
41 import org.onap.so.logger.MsoLogger;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.fasterxml.jackson.databind.SerializationFeature;
46 public class CamundaClient extends RequestClient{
47 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, CamundaClient.class);
48 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
50 public CamundaClient() {
51 super(CommonConstants.CAMUNDA);
56 public HttpResponse post(String camundaReqXML, String requestId,
57 String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
58 throws ClientProtocolException, IOException{
59 HttpPost post = new HttpPost(url);
60 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
61 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
63 StringEntity input = new StringEntity(jsonReq);
64 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
65 msoLogger.info("Camunda Request Content: " + jsonReq);
66 String encryptedCredentials = null;
68 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
69 if(encryptedCredentials != null){
70 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
71 if(userCredentials != null){
72 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
77 post.setEntity(input);
78 HttpResponse response = client.execute(post);
79 msoLogger.debug("Response is: " + response);
85 public HttpResponse post(String jsonReq)
86 throws ClientProtocolException, IOException{
87 HttpPost post = new HttpPost(url);
88 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
89 //String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
91 StringEntity input = new StringEntity(jsonReq);
92 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
94 String encryptedCredentials = null;
96 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
97 if(encryptedCredentials != null){
98 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
99 if(userCredentials != null){
100 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
105 post.setEntity(input);
106 HttpResponse response = client.execute(post);
107 msoLogger.debug("Response is: " + response);
112 public HttpResponse post(RequestClientParameter parameterObject)
113 throws ClientProtocolException, IOException{
114 HttpPost post = new HttpPost(url);
115 msoLogger.debug(CAMUNDA_URL_MESAGE+ url);
116 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(), parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
117 parameterObject.getServiceInstanceId(), parameterObject.getCorrelationId(), parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(), parameterObject.getNetworkId(), parameterObject.getConfigurationId(),
118 parameterObject.getServiceType(), parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(), parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(), parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd());
120 StringEntity input = new StringEntity(jsonReq);
121 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
123 String encryptedCredentials = null;
125 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
126 if(encryptedCredentials != null){
127 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
128 if(userCredentials != null){
129 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
134 post.setEntity(input);
135 HttpResponse response = client.execute(post);
136 msoLogger.debug("Response is: " + response);
142 public HttpResponse get() {
146 protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
147 String jsonReq = null;
150 CamundaRequest camundaRequest = new CamundaRequest();
151 CamundaInput camundaInput = new CamundaInput();
152 CamundaInput host = new CamundaInput();
153 CamundaInput schema = new CamundaInput();
154 CamundaInput reqid = new CamundaInput();
155 CamundaInput svcid = new CamundaInput();
156 CamundaInput timeout = new CamundaInput();
157 camundaInput.setValue(StringUtils.defaultString(reqXML));
158 host.setValue(parseURL());
159 schema.setValue(StringUtils.defaultString(schemaVersion));
160 reqid.setValue(requestId);
161 svcid.setValue(serviceInstanceId);
162 timeout.setValue(StringUtils.defaultString(requestTimeout));
163 camundaRequest.setServiceInput(camundaInput);
164 camundaRequest.setHost(host);
165 camundaRequest.setReqid(reqid);
166 camundaRequest.setSvcid(svcid);
167 camundaRequest.setSchema(schema);
168 camundaRequest.setTimeout(timeout);
169 ObjectMapper mapper = new ObjectMapper();
171 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
173 jsonReq = mapper.writeValueAsString(camundaRequest);
174 msoLogger.trace("request body is " + jsonReq);
176 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
182 protected String wrapVIDRequest(String requestId, boolean isBaseVfModule,
183 int recipeTimeout, String requestAction, String serviceInstanceId, String correlationId,
184 String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
185 String serviceType, String vnfType, String vfModuleType, String networkType,
186 String requestDetails, String apiVersion, boolean aLaCarte, String requestUri, String paramXsd){
187 String jsonReq = null;
190 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
191 CamundaInput serviceInput = new CamundaInput();
192 CamundaInput host = new CamundaInput();
193 CamundaInput requestIdInput= new CamundaInput();
194 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
195 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
196 CamundaInput requestActionInput = new CamundaInput();
197 CamundaInput serviceInstanceIdInput = new CamundaInput();
198 CamundaInput correlationIdInput = new CamundaInput();
199 CamundaInput vnfIdInput = new CamundaInput();
200 CamundaInput vfModuleIdInput = new CamundaInput();
201 CamundaInput volumeGroupIdInput = new CamundaInput();
202 CamundaInput networkIdInput = new CamundaInput();
203 CamundaInput configurationIdInput = new CamundaInput();
204 CamundaInput serviceTypeInput = new CamundaInput();
205 CamundaInput vnfTypeInput = new CamundaInput();
206 CamundaInput vfModuleTypeInput = new CamundaInput();
207 CamundaInput networkTypeInput = new CamundaInput();
208 CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
209 CamundaInput apiVersionInput = new CamundaInput();
210 CamundaInput requestUriInput = new CamundaInput();
211 CamundaInput recipeParamsInput = new CamundaInput();
213 //host.setValue(parseURL());
214 requestIdInput.setValue(StringUtils.defaultString(requestId));
215 isBaseVfModuleInput.setValue(isBaseVfModule);
216 recipeTimeoutInput.setValue(recipeTimeout);
217 requestActionInput.setValue(StringUtils.defaultString(requestAction));
218 serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
219 correlationIdInput.setValue(StringUtils.defaultString(correlationId));
220 vnfIdInput.setValue(StringUtils.defaultString(vnfId));
221 vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
222 volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
223 networkIdInput.setValue(StringUtils.defaultString(networkId));
224 configurationIdInput.setValue(StringUtils.defaultString(configurationId));
225 serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
226 vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
227 vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
228 networkTypeInput.setValue(StringUtils.defaultString(networkType));
229 aLaCarteInput.setValue(aLaCarte);
230 apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
231 requestUriInput.setValue(StringUtils.defaultString(requestUri));
232 recipeParamsInput.setValue(paramXsd);
234 serviceInput.setValue(requestDetails);
235 camundaRequest.setServiceInput(serviceInput);
236 camundaRequest.setHost(host);
237 camundaRequest.setRequestId(requestIdInput);
238 camundaRequest.setMsoRequestId(requestIdInput);
239 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
240 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
241 camundaRequest.setRequestAction(requestActionInput);
242 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
243 camundaRequest.setCorrelationId(correlationIdInput);
244 camundaRequest.setVnfId(vnfIdInput);
245 camundaRequest.setVfModuleId(vfModuleIdInput);
246 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
247 camundaRequest.setNetworkId(networkIdInput);
248 camundaRequest.setConfigurationId(configurationIdInput);
249 camundaRequest.setServiceType(serviceTypeInput);
250 camundaRequest.setVnfType(vnfTypeInput);
251 camundaRequest.setVfModuleType(vfModuleTypeInput);
252 camundaRequest.setNetworkType(networkTypeInput);
253 camundaRequest.setaLaCarte(aLaCarteInput);
254 camundaRequest.setApiVersion(apiVersionInput);
255 camundaRequest.setRequestUri(requestUriInput);
256 camundaRequest.setRecipeParams(recipeParamsInput);
258 ObjectMapper mapper = new ObjectMapper();
259 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
261 jsonReq = mapper.writeValueAsString(camundaRequest);
262 msoLogger.trace("request body is " + jsonReq);
264 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
269 private String parseURL(){
270 String[] parts = url.split(":");
275 host = host.substring(2);