Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandler / common / CamundaClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.apihandler.common;
25
26
27 import java.io.IOException;
28 import java.util.UUID;
29 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.logging.ref.slf4j.ONAPLogConstants;
36 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
37 import org.onap.so.apihandler.camundabeans.CamundaInput;
38 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
39 import org.onap.so.apihandler.camundabeans.CamundaRequest;
40 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
41 import org.onap.so.logger.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.slf4j.MDC;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
48
49 public class CamundaClient extends RequestClient {
50     private static Logger logger = LoggerFactory.getLogger(CamundaClient.class);
51     private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
52
53     public CamundaClient() {
54         super(CommonConstants.CAMUNDA);
55     }
56
57
58     @Override
59     public HttpResponse post(String camundaReqXML, String requestId, String requestTimeout, String schemaVersion,
60             String serviceInstanceId, String action) throws ClientProtocolException, IOException {
61         HttpPost post = new HttpPost(url);
62         logger.debug(CAMUNDA_URL_MESAGE + url);
63         String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
64
65         StringEntity input = new StringEntity(jsonReq);
66         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
67         logger.info("Camunda Request Content: {}", jsonReq);
68
69
70         post.setEntity(input);
71         setupHeaders(post);
72
73         HttpResponse response = client.execute(post);
74         logger.debug("Response is: {}", response);
75
76         return response;
77     }
78
79
80     private void setupHeaders(HttpPost post) {
81         post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
82         post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
83
84         String encryptedCredentials = null;
85         if (props != null) {
86             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
87             if (encryptedCredentials != null) {
88                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
89                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
90                 if (userCredentials != null) {
91                     post.addHeader("Authorization",
92                             "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
93                 }
94             }
95         }
96     }
97
98     @Override
99     public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException {
100         HttpPost post = new HttpPost(url);
101         logger.debug(CAMUNDA_URL_MESAGE + url);
102
103         StringEntity input = new StringEntity(jsonReq);
104         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
105         setupHeaders(post);
106
107         String encryptedCredentials = null;
108         if (props != null) {
109             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
110             if (encryptedCredentials != null) {
111                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
112                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
113                 if (userCredentials != null) {
114                     post.addHeader("Authorization",
115                             "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
116                 }
117             }
118         }
119
120
121         post.setEntity(input);
122         HttpResponse response = client.execute(post);
123         logger.debug("Response is: {}", response);
124
125         return response;
126     }
127
128     public HttpResponse post(RequestClientParameter parameterObject) throws ClientProtocolException, IOException {
129         HttpPost post = new HttpPost(url);
130         logger.debug(CAMUNDA_URL_MESAGE + url);
131         String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
132                 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
133                 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
134                 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
135                 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
136                 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
137                 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
138                 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
139                 parameterObject.getInstanceGroupId());
140
141         StringEntity input = new StringEntity(jsonReq);
142         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
143
144
145         setupHeaders(post);
146
147         String encryptedCredentials = null;
148         if (props != null) {
149             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
150             if (encryptedCredentials != null) {
151                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
152                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
153                 if (userCredentials != null) {
154                     post.addHeader("Authorization",
155                             "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
156                 }
157             }
158         }
159
160         post.setEntity(input);
161         HttpResponse response = client.execute(post);
162         logger.debug("Response is: {}", response);
163
164         return response;
165     }
166
167     @Override
168     public HttpResponse get() {
169         return null;
170     }
171
172     protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
173             String schemaVersion) {
174         String jsonReq = null;
175
176         try {
177             CamundaRequest camundaRequest = new CamundaRequest();
178             CamundaInput camundaInput = new CamundaInput();
179             CamundaInput host = new CamundaInput();
180             CamundaInput schema = new CamundaInput();
181             CamundaInput reqid = new CamundaInput();
182             CamundaInput svcid = new CamundaInput();
183             CamundaInput timeout = new CamundaInput();
184             camundaInput.setValue(StringUtils.defaultString(reqXML));
185             host.setValue(parseURL());
186             schema.setValue(StringUtils.defaultString(schemaVersion));
187             reqid.setValue(requestId);
188             svcid.setValue(serviceInstanceId);
189             timeout.setValue(StringUtils.defaultString(requestTimeout));
190             camundaRequest.setServiceInput(camundaInput);
191             camundaRequest.setHost(host);
192             camundaRequest.setReqid(reqid);
193             camundaRequest.setSvcid(svcid);
194             camundaRequest.setSchema(schema);
195             camundaRequest.setTimeout(timeout);
196             ObjectMapper mapper = new ObjectMapper();
197
198             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
199
200             jsonReq = mapper.writeValueAsString(camundaRequest);
201             logger.trace("request body is {}", jsonReq);
202         } catch (Exception e) {
203             logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapRequest",
204                     ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
205         }
206         return jsonReq;
207     }
208
209
210     protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
211             String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
212             String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
213             String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
214             String paramXsd, String instanceGroupId) {
215         String jsonReq = null;
216
217         try {
218             CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
219             CamundaInput serviceInput = new CamundaInput();
220             CamundaInput host = new CamundaInput();
221             CamundaInput requestIdInput = new CamundaInput();
222             CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
223             CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
224             CamundaInput requestActionInput = new CamundaInput();
225             CamundaInput serviceInstanceIdInput = new CamundaInput();
226             CamundaInput pnfCorrelationIdInput = new CamundaInput();
227             CamundaInput vnfIdInput = new CamundaInput();
228             CamundaInput vfModuleIdInput = new CamundaInput();
229             CamundaInput volumeGroupIdInput = new CamundaInput();
230             CamundaInput networkIdInput = new CamundaInput();
231             CamundaInput configurationIdInput = new CamundaInput();
232             CamundaInput serviceTypeInput = new CamundaInput();
233             CamundaInput vnfTypeInput = new CamundaInput();
234             CamundaInput vfModuleTypeInput = new CamundaInput();
235             CamundaInput networkTypeInput = new CamundaInput();
236             CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
237             CamundaInput apiVersionInput = new CamundaInput();
238             CamundaInput requestUriInput = new CamundaInput();
239             CamundaInput recipeParamsInput = new CamundaInput();
240             CamundaInput instanceGroupIdInput = new CamundaInput();
241
242             // host.setValue(parseURL());
243             requestIdInput.setValue(StringUtils.defaultString(requestId));
244             isBaseVfModuleInput.setValue(isBaseVfModule);
245             recipeTimeoutInput.setValue(recipeTimeout);
246             requestActionInput.setValue(StringUtils.defaultString(requestAction));
247             serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
248             pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
249             vnfIdInput.setValue(StringUtils.defaultString(vnfId));
250             vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
251             volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
252             networkIdInput.setValue(StringUtils.defaultString(networkId));
253             configurationIdInput.setValue(StringUtils.defaultString(configurationId));
254             serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
255             vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
256             vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
257             networkTypeInput.setValue(StringUtils.defaultString(networkType));
258             aLaCarteInput.setValue(aLaCarte);
259             apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
260             requestUriInput.setValue(StringUtils.defaultString(requestUri));
261             recipeParamsInput.setValue(paramXsd);
262             instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
263
264             serviceInput.setValue(requestDetails);
265             camundaRequest.setServiceInput(serviceInput);
266             camundaRequest.setHost(host);
267             camundaRequest.setRequestId(requestIdInput);
268             camundaRequest.setMsoRequestId(requestIdInput);
269             camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
270             camundaRequest.setRecipeTimeout(recipeTimeoutInput);
271             camundaRequest.setRequestAction(requestActionInput);
272             camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
273             camundaRequest.setPnfCorrelationId(pnfCorrelationIdInput);
274             camundaRequest.setVnfId(vnfIdInput);
275             camundaRequest.setVfModuleId(vfModuleIdInput);
276             camundaRequest.setVolumeGroupId(volumeGroupIdInput);
277             camundaRequest.setNetworkId(networkIdInput);
278             camundaRequest.setConfigurationId(configurationIdInput);
279             camundaRequest.setServiceType(serviceTypeInput);
280             camundaRequest.setVnfType(vnfTypeInput);
281             camundaRequest.setVfModuleType(vfModuleTypeInput);
282             camundaRequest.setNetworkType(networkTypeInput);
283             camundaRequest.setaLaCarte(aLaCarteInput);
284             camundaRequest.setApiVersion(apiVersionInput);
285             camundaRequest.setRequestUri(requestUriInput);
286             camundaRequest.setRecipeParams(recipeParamsInput);
287             camundaRequest.setInstanceGroupId(instanceGroupIdInput);
288
289             ObjectMapper mapper = new ObjectMapper();
290             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
291
292             jsonReq = mapper.writeValueAsString(camundaRequest);
293             logger.trace("request body is {}", jsonReq);
294         } catch (Exception e) {
295             logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapVIDRequest",
296                     ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
297         }
298         return jsonReq;
299     }
300
301     private String parseURL() {
302         String[] parts = url.split(":");
303         String host = "";
304         if (parts.length >= 2) {
305             host = parts[1];
306             if (host.length() > 2) {
307                 host = host.substring(2);
308             }
309         }
310         return host;
311     }
312 }