e6e81671cf3b062cf10e521f740bd0e869670d6d
[so.git] /
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     private static final String CAMUNDA_RESPONSE = "Response is: {}";
53     private static final String AUTHORIZATION = "Authorization";
54     private static final String BASIC = "Basic ";
55
56     public CamundaClient() {
57         super(CommonConstants.CAMUNDA);
58     }
59
60
61     @Override
62     public HttpResponse post(String camundaReqXML, String requestId, String requestTimeout, String schemaVersion,
63             String serviceInstanceId, String action) throws ClientProtocolException, IOException {
64         HttpPost post = new HttpPost(url);
65         logger.debug(CAMUNDA_URL_MESAGE + url);
66         String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion);
67
68         StringEntity input = new StringEntity(jsonReq);
69         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
70         logger.info("Camunda Request Content: {}", jsonReq);
71
72
73         post.setEntity(input);
74         setupHeaders(post);
75
76         HttpResponse response = client.execute(post);
77         logger.debug(CAMUNDA_RESPONSE, response);
78
79         return response;
80     }
81
82
83     private void setupHeaders(HttpPost post) {
84         post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
85         post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
86
87         String encryptedCredentials = null;
88         if (props != null) {
89             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
90             if (encryptedCredentials != null) {
91                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
92                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
93                 if (userCredentials != null) {
94                     post.addHeader(AUTHORIZATION,
95                             BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
96                 }
97             }
98         }
99     }
100
101     @Override
102     public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException {
103         HttpPost post = new HttpPost(url);
104         logger.debug(CAMUNDA_URL_MESAGE + url);
105
106         StringEntity input = new StringEntity(jsonReq);
107         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
108         setupHeaders(post);
109
110         String encryptedCredentials = null;
111         if (props != null) {
112             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
113             if (encryptedCredentials != null) {
114                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
115                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
116                 if (userCredentials != null) {
117                     post.addHeader(AUTHORIZATION,
118                             BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
119                 }
120             }
121         }
122
123
124         post.setEntity(input);
125         HttpResponse response = client.execute(post);
126         logger.debug(CAMUNDA_RESPONSE, response);
127
128         return response;
129     }
130
131     public HttpResponse post(RequestClientParameter parameterObject) throws ClientProtocolException, IOException {
132         HttpPost post = new HttpPost(url);
133         logger.debug(CAMUNDA_URL_MESAGE + url);
134         String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
135                 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
136                 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
137                 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
138                 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
139                 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
140                 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
141                 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
142                 parameterObject.getInstanceGroupId());
143
144         StringEntity input = new StringEntity(jsonReq);
145         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
146
147
148         setupHeaders(post);
149
150         String encryptedCredentials = null;
151         if (props != null) {
152             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
153             if (encryptedCredentials != null) {
154                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
155                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
156                 if (userCredentials != null) {
157                     post.addHeader(AUTHORIZATION,
158                             BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
159                 }
160             }
161         }
162
163         post.setEntity(input);
164         HttpResponse response = client.execute(post);
165         logger.debug(CAMUNDA_RESPONSE, response);
166
167         return response;
168     }
169
170     @Override
171     public HttpResponse get() {
172         return null;
173     }
174
175     protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
176             String schemaVersion) {
177         String jsonReq = null;
178
179         try {
180             CamundaRequest camundaRequest = new CamundaRequest();
181             CamundaInput camundaInput = new CamundaInput();
182             CamundaInput host = new CamundaInput();
183             CamundaInput schema = new CamundaInput();
184             CamundaInput reqid = new CamundaInput();
185             CamundaInput svcid = new CamundaInput();
186             CamundaInput timeout = new CamundaInput();
187             camundaInput.setValue(StringUtils.defaultString(reqXML));
188             host.setValue(parseURL());
189             schema.setValue(StringUtils.defaultString(schemaVersion));
190             reqid.setValue(requestId);
191             svcid.setValue(serviceInstanceId);
192             timeout.setValue(StringUtils.defaultString(requestTimeout));
193             camundaRequest.setServiceInput(camundaInput);
194             camundaRequest.setHost(host);
195             camundaRequest.setReqid(reqid);
196             camundaRequest.setSvcid(svcid);
197             camundaRequest.setSchema(schema);
198             camundaRequest.setTimeout(timeout);
199             ObjectMapper mapper = new ObjectMapper();
200
201             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
202
203             jsonReq = mapper.writeValueAsString(camundaRequest);
204             logger.trace("request body is {}", jsonReq);
205         } catch (Exception e) {
206             logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapRequest",
207                     ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
208         }
209         return jsonReq;
210     }
211
212
213     protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
214             String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
215             String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
216             String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
217             String paramXsd, String instanceGroupId) {
218         String jsonReq = null;
219
220         try {
221             CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
222             CamundaInput serviceInput = new CamundaInput();
223             CamundaInput host = new CamundaInput();
224             CamundaInput requestIdInput = new CamundaInput();
225             CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
226             CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
227             CamundaInput requestActionInput = new CamundaInput();
228             CamundaInput serviceInstanceIdInput = new CamundaInput();
229             CamundaInput pnfCorrelationIdInput = new CamundaInput();
230             CamundaInput vnfIdInput = new CamundaInput();
231             CamundaInput vfModuleIdInput = new CamundaInput();
232             CamundaInput volumeGroupIdInput = new CamundaInput();
233             CamundaInput networkIdInput = new CamundaInput();
234             CamundaInput configurationIdInput = new CamundaInput();
235             CamundaInput serviceTypeInput = new CamundaInput();
236             CamundaInput vnfTypeInput = new CamundaInput();
237             CamundaInput vfModuleTypeInput = new CamundaInput();
238             CamundaInput networkTypeInput = new CamundaInput();
239             CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
240             CamundaInput apiVersionInput = new CamundaInput();
241             CamundaInput requestUriInput = new CamundaInput();
242             CamundaInput recipeParamsInput = new CamundaInput();
243             CamundaInput instanceGroupIdInput = new CamundaInput();
244
245             // host.setValue(parseURL());
246             requestIdInput.setValue(StringUtils.defaultString(requestId));
247             isBaseVfModuleInput.setValue(isBaseVfModule);
248             recipeTimeoutInput.setValue(recipeTimeout);
249             requestActionInput.setValue(StringUtils.defaultString(requestAction));
250             serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
251             pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
252             vnfIdInput.setValue(StringUtils.defaultString(vnfId));
253             vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
254             volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
255             networkIdInput.setValue(StringUtils.defaultString(networkId));
256             configurationIdInput.setValue(StringUtils.defaultString(configurationId));
257             serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
258             vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
259             vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
260             networkTypeInput.setValue(StringUtils.defaultString(networkType));
261             aLaCarteInput.setValue(aLaCarte);
262             apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
263             requestUriInput.setValue(StringUtils.defaultString(requestUri));
264             recipeParamsInput.setValue(paramXsd);
265             instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
266
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.setPnfCorrelationId(pnfCorrelationIdInput);
277             camundaRequest.setVnfId(vnfIdInput);
278             camundaRequest.setVfModuleId(vfModuleIdInput);
279             camundaRequest.setVolumeGroupId(volumeGroupIdInput);
280             camundaRequest.setNetworkId(networkIdInput);
281             camundaRequest.setConfigurationId(configurationIdInput);
282             camundaRequest.setServiceType(serviceTypeInput);
283             camundaRequest.setVnfType(vnfTypeInput);
284             camundaRequest.setVfModuleType(vfModuleTypeInput);
285             camundaRequest.setNetworkType(networkTypeInput);
286             camundaRequest.setaLaCarte(aLaCarteInput);
287             camundaRequest.setApiVersion(apiVersionInput);
288             camundaRequest.setRequestUri(requestUriInput);
289             camundaRequest.setRecipeParams(recipeParamsInput);
290             camundaRequest.setInstanceGroupId(instanceGroupIdInput);
291
292             ObjectMapper mapper = new ObjectMapper();
293             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
294
295             jsonReq = mapper.writeValueAsString(camundaRequest);
296             logger.trace("request body is {}", jsonReq);
297         } catch (Exception e) {
298             logger.error("{} {} {} {} {}", MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapVIDRequest",
299                     ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
300         }
301         return jsonReq;
302     }
303
304     private String parseURL() {
305         String[] parts = url.split(":");
306         String host = "";
307         if (parts.length >= 2) {
308             host = parts[1];
309             if (host.length() > 2) {
310                 host = host.substring(2);
311             }
312         }
313         return host;
314     }
315 }