2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.apihandler.common;
24 import org.openecomp.mso.apihandler.camundabeans.*;
25 import org.openecomp.mso.logger.MessageEnum;
26 import org.openecomp.mso.logger.MsoLogger;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.entity.StringEntity;
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.codehaus.jackson.map.SerializationConfig;
34 import javax.xml.bind.DatatypeConverter;
35 import java.io.IOException;
37 public class CamundaClient extends RequestClient{
38 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
40 public CamundaClient() {
41 super(CommonConstants.CAMUNDA);
46 public HttpResponse post(String camundaReqXML, String requestId,
47 String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
48 throws ClientProtocolException, IOException{
49 HttpPost post = new HttpPost(url);
50 msoLogger.debug("Camunda url is: "+ url);
51 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
53 StringEntity input = new StringEntity(jsonReq);
54 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
56 String encryptedCredentials = null;
58 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
59 if(encryptedCredentials != null){
60 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
61 if(userCredentials != null){
62 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
67 post.setEntity(input);
68 HttpResponse response = client.execute(post);
74 public HttpResponse post(String jsonReq)
75 throws ClientProtocolException, IOException{
76 HttpPost post = new HttpPost(url);
77 msoLogger.debug("Camunda url is: "+ url);
78 //String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
80 StringEntity input = new StringEntity(jsonReq);
81 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
83 String encryptedCredentials = null;
85 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
86 if(encryptedCredentials != null){
87 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
88 if(userCredentials != null){
89 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
94 post.setEntity(input);
95 HttpResponse response = client.execute(post);
101 public HttpResponse post(String requestId, boolean isBaseVfModule,
102 int recipeTimeout, String requestAction, String serviceInstanceId,
103 String vnfId, String vfModuleId, String volumeGroupId, String networkId,
104 String serviceType, String vnfType, String vfModuleType, String networkType,
105 String requestDetails)
106 throws ClientProtocolException, IOException{
107 HttpPost post = new HttpPost(url);
108 msoLogger.debug("Camunda url is: "+ url);
109 String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
110 serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId,
111 serviceType, vnfType, vfModuleType, networkType, requestDetails);
113 StringEntity input = new StringEntity(jsonReq);
114 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
116 String encryptedCredentials = null;
118 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
119 if(encryptedCredentials != null){
120 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
121 if(userCredentials != null){
122 post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
127 post.setEntity(input);
128 HttpResponse response = client.execute(post);
134 public HttpResponse get() {
138 private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
139 String jsonReq = null;
143 if(requestTimeout == null){
146 if(schemaVersion == null){
152 CamundaRequest camundaRequest = new CamundaRequest();
153 CamundaInput camundaInput = new CamundaInput();
154 CamundaInput host = new CamundaInput();
155 CamundaInput schema = new CamundaInput();
156 CamundaInput reqid = new CamundaInput();
157 CamundaInput svcid = new CamundaInput();
158 CamundaInput timeout = new CamundaInput();
159 camundaInput.setValue(reqXML);
160 host.setValue(parseURL());
161 schema.setValue(schemaVersion);
162 reqid.setValue(requestId);
163 svcid.setValue(serviceInstanceId);
164 timeout.setValue(requestTimeout);
165 camundaRequest.setServiceInput(camundaInput);
166 camundaRequest.setHost(host);
167 camundaRequest.setReqid(reqid);
168 camundaRequest.setSvcid(svcid);
169 camundaRequest.setSchema(schema);
170 camundaRequest.setTimeout(timeout);
171 ObjectMapper mapper = new ObjectMapper();
172 mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
174 jsonReq = mapper.writeValueAsString(camundaRequest);
175 msoLogger.debug("request body is " + jsonReq);
177 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
182 private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
183 int recipeTimeout, String requestAction, String serviceInstanceId,
184 String vnfId, String vfModuleId, String volumeGroupId, String networkId,
185 String serviceType, String vnfType, String vfModuleType, String networkType,
186 String requestDetails){
187 String jsonReq = null;
188 if(requestId == null){
191 if(requestAction == null){
194 if(serviceInstanceId == null){
195 serviceInstanceId ="";
200 if(vfModuleId == null){
203 if(volumeGroupId == null){
206 if(networkId == null){
209 if(serviceType == null){
215 if(vfModuleType == null){
218 if(networkType == null){
221 if(requestDetails == null){
228 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
229 CamundaInput serviceInput = new CamundaInput();
230 CamundaInput host = new CamundaInput();
231 CamundaInput requestIdInput= new CamundaInput();
232 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
233 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
234 CamundaInput requestActionInput = new CamundaInput();
235 CamundaInput serviceInstanceIdInput = new CamundaInput();
236 CamundaInput vnfIdInput = new CamundaInput();
237 CamundaInput vfModuleIdInput = new CamundaInput();
238 CamundaInput volumeGroupIdInput = new CamundaInput();
239 CamundaInput networkIdInput = new CamundaInput();
240 CamundaInput serviceTypeInput = new CamundaInput();
241 CamundaInput vnfTypeInput = new CamundaInput();
242 CamundaInput vfModuleTypeInput = new CamundaInput();
243 CamundaInput networkTypeInput = new CamundaInput();
245 host.setValue(parseURL());
246 requestIdInput.setValue(requestId);
247 isBaseVfModuleInput.setValue(isBaseVfModule);
248 recipeTimeoutInput.setValue(recipeTimeout);
249 requestActionInput.setValue(requestAction);
250 serviceInstanceIdInput.setValue(serviceInstanceId);
251 vnfIdInput.setValue(vnfId);
252 vfModuleIdInput.setValue(vfModuleId);
253 volumeGroupIdInput.setValue(volumeGroupId);
254 networkIdInput.setValue(networkId);
255 serviceTypeInput.setValue(serviceType);
256 vnfTypeInput.setValue(vnfType);
257 vfModuleTypeInput.setValue(vfModuleType);
258 networkTypeInput.setValue(networkType);
260 serviceInput.setValue(requestDetails);
261 camundaRequest.setServiceInput(serviceInput);
262 camundaRequest.setHost(host);
263 camundaRequest.setRequestId(requestIdInput);
264 camundaRequest.setMsoRequestId(requestIdInput);
265 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
266 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
267 camundaRequest.setRequestAction(requestActionInput);
268 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
269 camundaRequest.setVnfId(vnfIdInput);
270 camundaRequest.setVfModuleId(vfModuleIdInput);
271 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
272 camundaRequest.setNetworkId(networkIdInput);
273 camundaRequest.setServiceType(serviceTypeInput);
274 camundaRequest.setVnfType(vnfTypeInput);
275 camundaRequest.setVfModuleType(vfModuleTypeInput);
276 camundaRequest.setNetworkType(networkTypeInput);
278 ObjectMapper mapper = new ObjectMapper();
279 mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
281 jsonReq = mapper.writeValueAsString(camundaRequest);
282 msoLogger.debug("request body is " + jsonReq);
284 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
289 private String parseURL(){
290 String[] parts = url.split(":");
295 host = host.substring(2);