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;
23 import java.io.IOException;
25 import javax.xml.bind.DatatypeConverter;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.entity.StringEntity;
31 import org.openecomp.mso.apihandler.camundabeans.CamundaBooleanInput;
32 import org.openecomp.mso.apihandler.camundabeans.CamundaInput;
33 import org.openecomp.mso.apihandler.camundabeans.CamundaIntegerInput;
34 import org.openecomp.mso.apihandler.camundabeans.CamundaRequest;
35 import org.openecomp.mso.apihandler.camundabeans.CamundaVIDRequest;
36 import org.openecomp.mso.logger.MessageEnum;
37 import org.openecomp.mso.logger.MsoLogger;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.databind.SerializationFeature;
42 public class CamundaClient extends RequestClient{
43 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
44 private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
46 public CamundaClient() {
47 super(CommonConstants.CAMUNDA);
52 public HttpResponse post(String camundaReqXML, String requestId,
53 String requestTimeout, String schemaVersion, String serviceInstanceId, String action) throws IOException {
54 HttpPost post = new HttpPost(url);
55 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
56 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
58 StringEntity input = new StringEntity(jsonReq);
59 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
61 String encryptedCredentials;
63 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
64 if(encryptedCredentials != null){
65 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
66 if(userCredentials != null){
67 post.addHeader("Authorization", "Basic " + DatatypeConverter
68 .printBase64Binary(userCredentials.getBytes()));
73 post.setEntity(input);
74 return client.execute(post);
78 public HttpResponse post(String jsonReq) throws IOException {
79 HttpPost post = new HttpPost(url);
80 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
82 StringEntity input = new StringEntity(jsonReq);
83 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
85 String encryptedCredentials;
87 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
88 if(encryptedCredentials != null){
89 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
90 if(userCredentials != null){
91 post.addHeader("Authorization", "Basic " + DatatypeConverter
92 .printBase64Binary(userCredentials.getBytes()));
97 post.setEntity(input);
99 return client.execute(post);
103 public HttpResponse post(RequestClientParamater params) throws IOException {
104 HttpPost post = new HttpPost(url);
105 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
106 String jsonReq = wrapVIDRequest(params);
108 StringEntity input = new StringEntity(jsonReq);
109 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
110 String encryptedCredentials;
112 encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
113 if(encryptedCredentials != null){
114 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
115 if(userCredentials != null){
116 post.addHeader("Authorization", "Basic " + DatatypeConverter
117 .printBase64Binary(userCredentials.getBytes()));
121 post.setEntity(input);
122 return client.execute(post);
126 public HttpResponse get() {
130 private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
131 String jsonReq = null;
135 if(requestTimeout == null){
138 if(schemaVersion == null){
142 CamundaRequest camundaRequest = new CamundaRequest();
143 CamundaInput camundaInput = new CamundaInput();
144 CamundaInput host = new CamundaInput();
145 CamundaInput schema = new CamundaInput();
146 CamundaInput reqid = new CamundaInput();
147 CamundaInput svcid = new CamundaInput();
148 CamundaInput timeout = new CamundaInput();
149 camundaInput.setValue(reqXML);
150 host.setValue(parseURL());
151 schema.setValue(schemaVersion);
152 reqid.setValue(requestId);
153 svcid.setValue(serviceInstanceId);
154 timeout.setValue(requestTimeout);
155 camundaRequest.setServiceInput(camundaInput);
156 camundaRequest.setHost(host);
157 camundaRequest.setReqid(reqid);
158 camundaRequest.setSvcid(svcid);
159 camundaRequest.setSchema(schema);
160 camundaRequest.setTimeout(timeout);
161 ObjectMapper mapper = new ObjectMapper();
162 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
164 jsonReq = mapper.writeValueAsString(camundaRequest);
165 msoLogger.debug("request body is " + jsonReq);
167 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
172 private String wrapVIDRequest(RequestClientParamater requestClientParamater) {
173 String jsonReq = null;
175 CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
176 CamundaInput serviceInput = new CamundaInput();
177 CamundaInput host = new CamundaInput();
178 CamundaInput requestIdInput= new CamundaInput();
179 CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
180 CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
181 CamundaInput requestActionInput = new CamundaInput();
182 CamundaInput serviceInstanceIdInput = new CamundaInput();
183 CamundaInput correlationIdInput = new CamundaInput();
184 CamundaInput vnfIdInput = new CamundaInput();
185 CamundaInput vfModuleIdInput = new CamundaInput();
186 CamundaInput volumeGroupIdInput = new CamundaInput();
187 CamundaInput networkIdInput = new CamundaInput();
188 CamundaInput configurationIdInput = new CamundaInput();
189 CamundaInput serviceTypeInput = new CamundaInput();
190 CamundaInput vnfTypeInput = new CamundaInput();
191 CamundaInput vfModuleTypeInput = new CamundaInput();
192 CamundaInput networkTypeInput = new CamundaInput();
193 CamundaInput recipeParamsInput = new CamundaInput();
194 host.setValue(parseURL());
195 requestIdInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestId()));
196 isBaseVfModuleInput.setValue(requestClientParamater.isBaseVfModule());
197 recipeTimeoutInput.setValue(requestClientParamater.getRecipeTimeout());
198 requestActionInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestAction()));
199 serviceInstanceIdInput.setValue(StringUtils.defaultString(requestClientParamater.getServiceInstanceId()));
200 correlationIdInput.setValue(StringUtils.defaultString(requestClientParamater.getCorrelationId()));
201 vnfIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVnfId()));
202 vfModuleIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVfModuleId()));
203 volumeGroupIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVolumeGroupId()));
204 networkIdInput.setValue(StringUtils.defaultString(requestClientParamater.getNetworkId()));
205 configurationIdInput.setValue(StringUtils.defaultString(requestClientParamater.getConfigurationId()));
206 serviceTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getServiceType()));
207 vnfTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getVnfType()));
208 vfModuleTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getVfModuleType()));
209 networkTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getNetworkType()));
210 recipeParamsInput.setValue(requestClientParamater.getRecipeParamXsd());
211 serviceInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestDetails()));
212 camundaRequest.setServiceInput(serviceInput);
213 camundaRequest.setHost(host);
214 camundaRequest.setRequestId(requestIdInput);
215 camundaRequest.setMsoRequestId(requestIdInput);
216 camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
217 camundaRequest.setRecipeTimeout(recipeTimeoutInput);
218 camundaRequest.setRequestAction(requestActionInput);
219 camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
220 camundaRequest.setCorrelationId(correlationIdInput);
221 camundaRequest.setVnfId(vnfIdInput);
222 camundaRequest.setVfModuleId(vfModuleIdInput);
223 camundaRequest.setVolumeGroupId(volumeGroupIdInput);
224 camundaRequest.setNetworkId(networkIdInput);
225 camundaRequest.setConfigurationId(configurationIdInput);
226 camundaRequest.setServiceType(serviceTypeInput);
227 camundaRequest.setVnfType(vnfTypeInput);
228 camundaRequest.setVfModuleType(vfModuleTypeInput);
229 camundaRequest.setNetworkType(networkTypeInput);
230 camundaRequest.setRecipeParams(recipeParamsInput);
231 ObjectMapper mapper = new ObjectMapper();
232 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
234 jsonReq = mapper.writeValueAsString(camundaRequest);
235 msoLogger.debug("request body is " + jsonReq);
237 msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
242 private String parseURL(){
243 String[] parts = url.split(":");
248 host = host.substring(2);