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 java.io.IOException;
26 import javax.xml.bind.DatatypeConverter;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.ClientProtocolException;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.entity.StringEntity;
32 import org.openecomp.mso.apihandler.camundabeans.CamundaBooleanInput;
33 import org.openecomp.mso.apihandler.camundabeans.CamundaInput;
34 import org.openecomp.mso.apihandler.camundabeans.CamundaIntegerInput;
35 import org.openecomp.mso.apihandler.camundabeans.CamundaRequest;
36 import org.openecomp.mso.apihandler.camundabeans.CamundaVIDRequest;
37 import org.openecomp.mso.logger.MessageEnum;
38 import org.openecomp.mso.logger.MsoLogger;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.SerializationFeature;
43 public class CamundaClient extends RequestClient{
44 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
45 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
47 public CamundaClient() {
48 super(CommonConstants.CAMUNDA);
53 public HttpResponse post(String camundaReqXML, String requestId,
54 String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
55 throws ClientProtocolException, IOException{
56 HttpPost post = new HttpPost(url);
57 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
58 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
60 StringEntity input = new StringEntity(jsonReq);
61 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
63 String encryptedCredentials;
65 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
66 if(encryptedCredentials != null){
67 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
68 if(userCredentials != null){
69 post.addHeader("Authorization", "Basic " + DatatypeConverter
70 .printBase64Binary(userCredentials.getBytes()));
75 post.setEntity(input);
76 return client.execute(post);
80 public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException{
81 HttpPost post = new HttpPost(url);
82 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
84 StringEntity input = new StringEntity(jsonReq);
85 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
87 String encryptedCredentials;
89 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
90 if(encryptedCredentials != null){
91 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
92 if(userCredentials != null){
93 post.addHeader("Authorization", "Basic " + DatatypeConverter
94 .printBase64Binary(userCredentials.getBytes()));
99 post.setEntity(input);
101 return client.execute(post);
105 public HttpResponse post(String requestId, boolean isBaseVfModule,
106 int recipeTimeout, String requestAction, String serviceInstanceId,
107 String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
108 String serviceType, String vnfType, String vfModuleType, String networkType,
109 String requestDetails, String recipeParamXsd)
110 throws ClientProtocolException, IOException{
111 HttpPost post = new HttpPost(url);
112 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
113 String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
114 serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId, configurationId,
115 serviceType, vnfType, vfModuleType, networkType, requestDetails, recipeParamXsd);
117 StringEntity input = new StringEntity(jsonReq);
118 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
120 String encryptedCredentials;
122 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
123 if(encryptedCredentials != null){
124 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
125 if(userCredentials != null){
126 post.addHeader("Authorization", "Basic " + DatatypeConverter
127 .printBase64Binary(userCredentials.getBytes()));
132 post.setEntity(input);
134 return client.execute(post);
138 public HttpResponse get() {
142 private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
143 String jsonReq = null;
147 if(requestTimeout == null){
150 if(schemaVersion == null){
156 CamundaRequest camundaRequest = new CamundaRequest();
157 CamundaInput camundaInput = new CamundaInput();
158 CamundaInput host = new CamundaInput();
159 CamundaInput schema = new CamundaInput();
160 CamundaInput reqid = new CamundaInput();
161 CamundaInput svcid = new CamundaInput();
162 CamundaInput timeout = new CamundaInput();
163 camundaInput.setValue(reqXML);
164 host.setValue(parseURL());
165 schema.setValue(schemaVersion);
166 reqid.setValue(requestId);
167 svcid.setValue(serviceInstanceId);
168 timeout.setValue(requestTimeout);
169 camundaRequest.setServiceInput(camundaInput);
170 camundaRequest.setHost(host);
171 camundaRequest.setReqid(reqid);
172 camundaRequest.setSvcid(svcid);
173 camundaRequest.setSchema(schema);
174 camundaRequest.setTimeout(timeout);
175 ObjectMapper mapper = new ObjectMapper();
176 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
178 jsonReq = mapper.writeValueAsString(camundaRequest);
179 msoLogger.debug("request body is " + jsonReq);
181 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
186 private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
187 int recipeTimeout, String requestAction, String serviceInstanceId,
188 String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
189 String serviceType, String vnfType, String vfModuleType, String networkType,
190 String requestDetails, String recipeParams){
191 String jsonReq = null;
192 if(requestId == null){
195 if(requestAction == null){
198 if(serviceInstanceId == null){
199 serviceInstanceId ="";
204 if(vfModuleId == null){
207 if(volumeGroupId == null){
210 if(networkId == null){
213 if(configurationId == null){
216 if(serviceType == null){
222 if(vfModuleType == null){
225 if(networkType == null){
228 if(requestDetails == null){
233 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
234 CamundaInput serviceInput = new CamundaInput();
235 CamundaInput host = new CamundaInput();
236 CamundaInput requestIdInput= new CamundaInput();
237 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
238 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
239 CamundaInput requestActionInput = new CamundaInput();
240 CamundaInput serviceInstanceIdInput = new CamundaInput();
241 CamundaInput vnfIdInput = new CamundaInput();
242 CamundaInput vfModuleIdInput = new CamundaInput();
243 CamundaInput volumeGroupIdInput = new CamundaInput();
244 CamundaInput networkIdInput = new CamundaInput();
245 CamundaInput configurationIdInput = new CamundaInput();
246 CamundaInput serviceTypeInput = new CamundaInput();
247 CamundaInput vnfTypeInput = new CamundaInput();
248 CamundaInput vfModuleTypeInput = new CamundaInput();
249 CamundaInput networkTypeInput = new CamundaInput();
250 CamundaInput recipeParamsInput = new CamundaInput();
251 host.setValue(parseURL());
252 requestIdInput.setValue(requestId);
253 isBaseVfModuleInput.setValue(isBaseVfModule);
254 recipeTimeoutInput.setValue(recipeTimeout);
255 requestActionInput.setValue(requestAction);
256 serviceInstanceIdInput.setValue(serviceInstanceId);
257 vnfIdInput.setValue(vnfId);
258 vfModuleIdInput.setValue(vfModuleId);
259 volumeGroupIdInput.setValue(volumeGroupId);
260 networkIdInput.setValue(networkId);
261 configurationIdInput.setValue(configurationId);
262 serviceTypeInput.setValue(serviceType);
263 vnfTypeInput.setValue(vnfType);
264 vfModuleTypeInput.setValue(vfModuleType);
265 networkTypeInput.setValue(networkType);
266 recipeParamsInput.setValue(recipeParams);
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.setVnfId(vnfIdInput);
277 camundaRequest.setVfModuleId(vfModuleIdInput);
278 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
279 camundaRequest.setNetworkId(networkIdInput);
280 camundaRequest.setConfigurationId(configurationIdInput);
281 camundaRequest.setServiceType(serviceTypeInput);
282 camundaRequest.setVnfType(vnfTypeInput);
283 camundaRequest.setVfModuleType(vfModuleTypeInput);
284 camundaRequest.setNetworkType(networkTypeInput);
285 camundaRequest.setRecipeParams(recipeParamsInput);
286 ObjectMapper mapper = new ObjectMapper();
287 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
289 jsonReq = mapper.writeValueAsString(camundaRequest);
290 msoLogger.debug("request body is " + jsonReq);
292 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
297 private String parseURL(){
298 String[] parts = url.split(":");
303 host = host.substring(2);