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