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