eb4ba7653080c3ed807ddfdb2d1f27e9049eecaf
[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 com.google.common.base.Strings;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.http.HttpResponse;
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 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         addAuthorizationHeader(post);
87     }
88
89     @Override
90     public HttpResponse post(String jsonReq) throws IOException {
91         HttpPost post = new HttpPost(url);
92         logger.debug(CAMUNDA_URL_MESAGE + url);
93
94         StringEntity input = new StringEntity(jsonReq);
95         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
96         setupHeaders(post);
97         addAuthorizationHeader(post);
98         post.setEntity(input);
99         HttpResponse response = client.execute(post);
100         logger.debug(CAMUNDA_RESPONSE, response);
101
102         return response;
103     }
104
105     public HttpResponse post(RequestClientParameter parameterObject) throws IOException {
106         HttpPost post = new HttpPost(url);
107         logger.debug(CAMUNDA_URL_MESAGE + url);
108         String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
109                 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
110                 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
111                 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
112                 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
113                 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
114                 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
115                 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
116                 parameterObject.getInstanceGroupId());
117
118         StringEntity input = new StringEntity(jsonReq);
119         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
120         setupHeaders(post);
121         addAuthorizationHeader(post);
122         post.setEntity(input);
123         HttpResponse response = client.execute(post);
124         logger.debug(CAMUNDA_RESPONSE, response);
125
126         return response;
127     }
128
129     @Override
130     public HttpResponse get() {
131         return null;
132     }
133
134     protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
135             String schemaVersion) {
136         String jsonReq = null;
137
138         try {
139             CamundaRequest camundaRequest = new CamundaRequest();
140             CamundaInput camundaInput = new CamundaInput();
141             CamundaInput host = new CamundaInput();
142             CamundaInput schema = new CamundaInput();
143             CamundaInput reqid = new CamundaInput();
144             CamundaInput svcid = new CamundaInput();
145             CamundaInput timeout = new CamundaInput();
146             camundaInput.setValue(StringUtils.defaultString(reqXML));
147             host.setValue(parseURL());
148             schema.setValue(StringUtils.defaultString(schemaVersion));
149             reqid.setValue(requestId);
150             svcid.setValue(serviceInstanceId);
151             timeout.setValue(StringUtils.defaultString(requestTimeout));
152             camundaRequest.setServiceInput(camundaInput);
153             camundaRequest.setHost(host);
154             camundaRequest.setReqid(reqid);
155             camundaRequest.setSvcid(svcid);
156             camundaRequest.setSchema(schema);
157             camundaRequest.setTimeout(timeout);
158             ObjectMapper mapper = new ObjectMapper();
159
160             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
161
162             jsonReq = mapper.writeValueAsString(camundaRequest);
163             logger.trace("request body is {}", jsonReq);
164         } catch (Exception e) {
165             logger.error(Strings.repeat("{} ", 5), MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda", "wrapRequest",
166                     ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
167         }
168         return jsonReq;
169     }
170
171
172     protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
173             String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
174             String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
175             String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
176             String paramXsd, String instanceGroupId) {
177         String jsonReq = null;
178
179         try {
180             CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
181             CamundaInput serviceInput = new CamundaInput();
182             CamundaInput host = new CamundaInput();
183             CamundaInput requestIdInput = new CamundaInput();
184             CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
185             CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
186             CamundaInput requestActionInput = new CamundaInput();
187             CamundaInput serviceInstanceIdInput = new CamundaInput();
188             CamundaInput pnfCorrelationIdInput = new CamundaInput();
189             CamundaInput vnfIdInput = new CamundaInput();
190             CamundaInput vfModuleIdInput = new CamundaInput();
191             CamundaInput volumeGroupIdInput = new CamundaInput();
192             CamundaInput networkIdInput = new CamundaInput();
193             CamundaInput configurationIdInput = new CamundaInput();
194             CamundaInput serviceTypeInput = new CamundaInput();
195             CamundaInput vnfTypeInput = new CamundaInput();
196             CamundaInput vfModuleTypeInput = new CamundaInput();
197             CamundaInput networkTypeInput = new CamundaInput();
198             CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
199             CamundaInput apiVersionInput = new CamundaInput();
200             CamundaInput requestUriInput = new CamundaInput();
201             CamundaInput recipeParamsInput = new CamundaInput();
202             CamundaInput instanceGroupIdInput = new CamundaInput();
203
204             requestIdInput.setValue(StringUtils.defaultString(requestId));
205             isBaseVfModuleInput.setValue(isBaseVfModule);
206             recipeTimeoutInput.setValue(recipeTimeout);
207             requestActionInput.setValue(StringUtils.defaultString(requestAction));
208             serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
209             pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
210             vnfIdInput.setValue(StringUtils.defaultString(vnfId));
211             vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
212             volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
213             networkIdInput.setValue(StringUtils.defaultString(networkId));
214             configurationIdInput.setValue(StringUtils.defaultString(configurationId));
215             serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
216             vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
217             vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
218             networkTypeInput.setValue(StringUtils.defaultString(networkType));
219             aLaCarteInput.setValue(aLaCarte);
220             apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
221             requestUriInput.setValue(StringUtils.defaultString(requestUri));
222             recipeParamsInput.setValue(paramXsd);
223             instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
224
225             serviceInput.setValue(requestDetails);
226             camundaRequest.setServiceInput(serviceInput);
227             camundaRequest.setHost(host);
228             camundaRequest.setRequestId(requestIdInput);
229             camundaRequest.setMsoRequestId(requestIdInput);
230             camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
231             camundaRequest.setRecipeTimeout(recipeTimeoutInput);
232             camundaRequest.setRequestAction(requestActionInput);
233             camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
234             camundaRequest.setPnfCorrelationId(pnfCorrelationIdInput);
235             camundaRequest.setVnfId(vnfIdInput);
236             camundaRequest.setVfModuleId(vfModuleIdInput);
237             camundaRequest.setVolumeGroupId(volumeGroupIdInput);
238             camundaRequest.setNetworkId(networkIdInput);
239             camundaRequest.setConfigurationId(configurationIdInput);
240             camundaRequest.setServiceType(serviceTypeInput);
241             camundaRequest.setVnfType(vnfTypeInput);
242             camundaRequest.setVfModuleType(vfModuleTypeInput);
243             camundaRequest.setNetworkType(networkTypeInput);
244             camundaRequest.setaLaCarte(aLaCarteInput);
245             camundaRequest.setApiVersion(apiVersionInput);
246             camundaRequest.setRequestUri(requestUriInput);
247             camundaRequest.setRecipeParams(recipeParamsInput);
248             camundaRequest.setInstanceGroupId(instanceGroupIdInput);
249
250             ObjectMapper mapper = new ObjectMapper();
251             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
252
253             jsonReq = mapper.writeValueAsString(camundaRequest);
254             logger.trace("request body is {}", jsonReq);
255         } catch (Exception e) {
256             logger.error(Strings.repeat("{} ", 5), MessageEnum.APIH_WARP_REQUEST.toString(), "Camunda",
257                     "wrapVIDRequest", ErrorCode.BusinessProcesssError.getValue(), "Error in APIH Warp request", e);
258         }
259         return jsonReq;
260     }
261
262     private String parseURL() {
263         String[] parts = url.split(":");
264         String host = "";
265         if (parts.length >= 2) {
266             host = parts[1];
267             if (host.length() > 2) {
268                 host = host.substring(2);
269             }
270         }
271         return host;
272     }
273
274     private void addAuthorizationHeader(HttpPost post) {
275         String encryptedCredentials;
276         if (props != null) {
277             encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
278             if (encryptedCredentials != null) {
279                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH,
280                         props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
281                 if (userCredentials != null) {
282                     post.addHeader(AUTHORIZATION,
283                             BASIC + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
284                 }
285             }
286         }
287     }
288 }