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);
39 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
41 public CamundaClient() {
42 super(CommonConstants.CAMUNDA);
47 public HttpResponse post(String camundaReqXML, String requestId,
48 String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
49 throws ClientProtocolException, IOException{
50 HttpPost post = new HttpPost(url);
51 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
52 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
54 StringEntity input = new StringEntity(jsonReq);
55 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
57 String encryptedCredentials;
59 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
60 if(encryptedCredentials != null){
61 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
62 if(userCredentials != null){
63 post.addHeader("Authorization", "Basic " + DatatypeConverter
64 .printBase64Binary(userCredentials.getBytes()));
69 post.setEntity(input);
70 return client.execute(post);
74 public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException{
75 HttpPost post = new HttpPost(url);
76 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
78 StringEntity input = new StringEntity(jsonReq);
79 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
81 String encryptedCredentials;
83 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
84 if(encryptedCredentials != null){
85 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
86 if(userCredentials != null){
87 post.addHeader("Authorization", "Basic " + DatatypeConverter
88 .printBase64Binary(userCredentials.getBytes()));
93 post.setEntity(input);
95 return client.execute(post);
99 public HttpResponse post(String requestId, boolean isBaseVfModule,
100 int recipeTimeout, String requestAction, String serviceInstanceId,
101 String vnfId, String vfModuleId, String volumeGroupId, String networkId,
102 String serviceType, String vnfType, String vfModuleType, String networkType,
103 String requestDetails, String recipeParamXsd)
104 throws ClientProtocolException, IOException{
105 HttpPost post = new HttpPost(url);
106 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
107 String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
108 serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId,
109 serviceType, vnfType, vfModuleType, networkType, requestDetails, recipeParamXsd);
111 StringEntity input = new StringEntity(jsonReq);
112 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
114 String encryptedCredentials;
116 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
117 if(encryptedCredentials != null){
118 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
119 if(userCredentials != null){
120 post.addHeader("Authorization", "Basic " + DatatypeConverter
121 .printBase64Binary(userCredentials.getBytes()));
126 post.setEntity(input);
128 return client.execute(post);
132 public HttpResponse get() {
136 private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
137 String jsonReq = null;
141 if(requestTimeout == null){
144 if(schemaVersion == 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(reqXML);
158 host.setValue(parseURL());
159 schema.setValue(schemaVersion);
160 reqid.setValue(requestId);
161 svcid.setValue(serviceInstanceId);
162 timeout.setValue(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();
170 mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
172 jsonReq = mapper.writeValueAsString(camundaRequest);
173 msoLogger.debug("request body is " + jsonReq);
175 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
180 private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
181 int recipeTimeout, String requestAction, String serviceInstanceId,
182 String vnfId, String vfModuleId, String volumeGroupId, String networkId,
183 String serviceType, String vnfType, String vfModuleType, String networkType,
184 String requestDetails, String recipeParams){
185 String jsonReq = null;
186 if(requestId == null){
189 if(requestAction == null){
192 if(serviceInstanceId == null){
193 serviceInstanceId ="";
198 if(vfModuleId == null){
201 if(volumeGroupId == null){
204 if(networkId == null){
207 if(serviceType == null){
213 if(vfModuleType == null){
216 if(networkType == null){
219 if(requestDetails == null){
226 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
227 CamundaInput serviceInput = new CamundaInput();
228 CamundaInput host = new CamundaInput();
229 CamundaInput requestIdInput= new CamundaInput();
230 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
231 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
232 CamundaInput requestActionInput = new CamundaInput();
233 CamundaInput serviceInstanceIdInput = new CamundaInput();
234 CamundaInput vnfIdInput = new CamundaInput();
235 CamundaInput vfModuleIdInput = new CamundaInput();
236 CamundaInput volumeGroupIdInput = new CamundaInput();
237 CamundaInput networkIdInput = new CamundaInput();
238 CamundaInput serviceTypeInput = new CamundaInput();
239 CamundaInput vnfTypeInput = new CamundaInput();
240 CamundaInput vfModuleTypeInput = new CamundaInput();
241 CamundaInput networkTypeInput = new CamundaInput();
242 CamundaInput recipeParamsInput = new CamundaInput();
243 host.setValue(parseURL());
244 requestIdInput.setValue(requestId);
245 isBaseVfModuleInput.setValue(isBaseVfModule);
246 recipeTimeoutInput.setValue(recipeTimeout);
247 requestActionInput.setValue(requestAction);
248 serviceInstanceIdInput.setValue(serviceInstanceId);
249 vnfIdInput.setValue(vnfId);
250 vfModuleIdInput.setValue(vfModuleId);
251 volumeGroupIdInput.setValue(volumeGroupId);
252 networkIdInput.setValue(networkId);
253 serviceTypeInput.setValue(serviceType);
254 vnfTypeInput.setValue(vnfType);
255 vfModuleTypeInput.setValue(vfModuleType);
256 networkTypeInput.setValue(networkType);
257 recipeParamsInput.setValue(recipeParams);
258 serviceInput.setValue(requestDetails);
259 camundaRequest.setServiceInput(serviceInput);
260 camundaRequest.setHost(host);
261 camundaRequest.setRequestId(requestIdInput);
262 camundaRequest.setMsoRequestId(requestIdInput);
263 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
264 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
265 camundaRequest.setRequestAction(requestActionInput);
266 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
267 camundaRequest.setVnfId(vnfIdInput);
268 camundaRequest.setVfModuleId(vfModuleIdInput);
269 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
270 camundaRequest.setNetworkId(networkIdInput);
271 camundaRequest.setServiceType(serviceTypeInput);
272 camundaRequest.setVnfType(vnfTypeInput);
273 camundaRequest.setVfModuleType(vfModuleTypeInput);
274 camundaRequest.setNetworkType(networkTypeInput);
275 camundaRequest.setRecipeParams(recipeParamsInput);
276 ObjectMapper mapper = new ObjectMapper();
277 mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
279 jsonReq = mapper.writeValueAsString(camundaRequest);
280 msoLogger.debug("request body is " + jsonReq);
282 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
287 private String parseURL(){
288 String[] parts = url.split(":");
293 host = host.substring(2);