bc8af6e69093409acad4c69ec812465b3fff9718
[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     @Override
103     public HttpResponse post(RequestClientParameter parameterObject) throws IOException {
104         HttpPost post = new HttpPost(url);
105         logger.debug(CAMUNDA_URL_MESAGE + url);
106         String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
107                 parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
108                 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
109                 parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
110                 parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
111                 parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
112                 parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
113                 parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
114                 parameterObject.getInstanceGroupId(), parameterObject.isGenerateIdsOnly());
115
116         StringEntity input = new StringEntity(jsonReq);
117         input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
118         setupHeaders(post);
119         addAuthorizationHeader(post);
120         post.setEntity(input);
121         HttpResponse response = client.execute(post);
122         logger.debug(CAMUNDA_RESPONSE, response);
123
124         return response;
125     }
126
127     @Override
128     public HttpResponse get() {
129         return null;
130     }
131
132     protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
133             String schemaVersion) {
134         String jsonReq = null;
135
136         try {
137             CamundaRequest camundaRequest = new CamundaRequest();
138             CamundaInput camundaInput = new CamundaInput();
139             CamundaInput host = new CamundaInput();
140             CamundaInput schema = new CamundaInput();
141             CamundaInput reqid = new CamundaInput();
142             CamundaInput svcid = new CamundaInput();
143             CamundaInput timeout = new CamundaInput();
144             camundaInput.setValue(StringUtils.defaultString(reqXML));
145             host.setValue(parseURL());
146             schema.setValue(StringUtils.defaultString(schemaVersion));
147             reqid.setValue(requestId);
148             svcid.setValue(serviceInstanceId);
149             timeout.setValue(StringUtils.defaultString(requestTimeout));
150             camundaRequest.setServiceInput(camundaInput);
151             camundaRequest.setHost(host);
152             camundaRequest.setReqid(reqid);
153             camundaRequest.setSvcid(svcid);
154             camundaRequest.setSchema(schema);
155             camundaRequest.setTimeout(timeout);
156             ObjectMapper mapper = new ObjectMapper();
157
158             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
159
160             jsonReq = mapper.writeValueAsString(camundaRequest);
161             logger.trace("request body is {}", jsonReq);
162         } catch (Exception e) {
163             logger.error("Error in APIH Warp request", e);
164         }
165         return jsonReq;
166     }
167
168
169     protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
170             String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
171             String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
172             String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
173             String paramXsd, String instanceGroupId, boolean generateIdsOnly) {
174         String jsonReq = null;
175
176         try {
177             CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
178             CamundaInput serviceInput = new CamundaInput();
179             CamundaInput host = new CamundaInput();
180             CamundaInput requestIdInput = new CamundaInput();
181             CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
182             CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
183             CamundaInput requestActionInput = new CamundaInput();
184             CamundaInput serviceInstanceIdInput = new CamundaInput();
185             CamundaInput pnfCorrelationIdInput = new CamundaInput();
186             CamundaInput vnfIdInput = new CamundaInput();
187             CamundaInput vfModuleIdInput = new CamundaInput();
188             CamundaInput volumeGroupIdInput = new CamundaInput();
189             CamundaInput networkIdInput = new CamundaInput();
190             CamundaInput configurationIdInput = new CamundaInput();
191             CamundaInput serviceTypeInput = new CamundaInput();
192             CamundaInput vnfTypeInput = new CamundaInput();
193             CamundaInput vfModuleTypeInput = new CamundaInput();
194             CamundaInput networkTypeInput = new CamundaInput();
195             CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
196             CamundaInput apiVersionInput = new CamundaInput();
197             CamundaInput requestUriInput = new CamundaInput();
198             CamundaInput recipeParamsInput = new CamundaInput();
199             CamundaInput instanceGroupIdInput = new CamundaInput();
200             CamundaBooleanInput generateIds = new CamundaBooleanInput();
201
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             generateIds.setValue(generateIdsOnly);
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             camundaRequest.setGenerateIds(generateIds);
250
251             ObjectMapper mapper = new ObjectMapper();
252             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
253
254             jsonReq = mapper.writeValueAsString(camundaRequest);
255             logger.trace("request body is {}", jsonReq);
256         } catch (Exception e) {
257             logger.error("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 }