Update Logging
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandler / common / CamundaClient.java
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  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
26 import java.io.IOException;
27 import java.util.UUID;
28
29 import javax.xml.bind.DatatypeConverter;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.StringEntity;
36 import org.onap.logging.ref.slf4j.ONAPLogConstants;
37 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
38 import org.onap.so.apihandler.camundabeans.CamundaInput;
39 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
40 import org.onap.so.apihandler.camundabeans.CamundaRequest;
41 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
42 import org.onap.so.logger.MessageEnum;
43 import org.onap.so.logger.MsoLogger;
44 import org.slf4j.MDC;
45
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
48
49 public class CamundaClient extends RequestClient{
50         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, CamundaClient.class);
51         private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
52
53         public CamundaClient() {
54                 super(CommonConstants.CAMUNDA);
55         }
56
57
58         @Override
59         public HttpResponse post(String camundaReqXML, String requestId,
60                         String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
61                                         throws ClientProtocolException, IOException{
62                 HttpPost post = new HttpPost(url);
63                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
64                 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);
65
66                 StringEntity input = new StringEntity(jsonReq);
67                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
68                 msoLogger.info("Camunda Request Content: " + jsonReq);
69                 
70
71                 post.setEntity(input);
72                 setupHeaders(post);
73
74                 HttpResponse response = client.execute(post);
75                 msoLogger.debug("Response is: " + response);
76                 
77                 return response;
78         }
79
80
81         private void setupHeaders(HttpPost post) {
82                 post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
83                 post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
84                 
85                 String encryptedCredentials = null;
86                 if(props!=null){
87                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
88                         if(encryptedCredentials != null){
89                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
90                                 if(userCredentials != null){
91                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
92                                 }
93                         }
94                 }
95         }
96
97         @Override
98         public HttpResponse post(String jsonReq)
99                                         throws ClientProtocolException, IOException{
100                 HttpPost post = new HttpPost(url);
101                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
102
103                 StringEntity input = new StringEntity(jsonReq);
104                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
105                 setupHeaders(post);
106
107                 String encryptedCredentials = null;
108                 if(props!=null){
109                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
110                         if(encryptedCredentials != null){
111                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
112                                 if(userCredentials != null){
113                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
114                                 }
115                         }
116                 }
117
118
119                 post.setEntity(input);
120                 HttpResponse response = client.execute(post);
121                 msoLogger.debug("Response is: " + response);
122
123                 return response;
124         }
125
126         public HttpResponse post(RequestClientParameter parameterObject)
127                                         throws ClientProtocolException, IOException{
128                 HttpPost post = new HttpPost(url);
129                 msoLogger.debug(CAMUNDA_URL_MESAGE+ url);
130                 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(), parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
131                                 parameterObject.getServiceInstanceId(), parameterObject.getCorrelationId(), parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(), parameterObject.getNetworkId(), parameterObject.getConfigurationId(),
132                                 parameterObject.getServiceType(), parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(), parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(), parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd());
133
134                 StringEntity input = new StringEntity(jsonReq);
135                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
136
137
138                 setupHeaders(post);
139
140                 String encryptedCredentials = null;
141                 if(props!=null){
142                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
143                         if(encryptedCredentials != null){
144                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
145                                 if(userCredentials != null){
146                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
147                                 }
148                         }
149                 }
150
151                 post.setEntity(input);
152                 HttpResponse response = client.execute(post);
153                 msoLogger.debug("Response is: " + response);
154
155                 return response;
156         }
157         
158         @Override
159     public HttpResponse get() {
160         return null;
161     }
162
163         protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
164                 String jsonReq = null;
165
166                 try{
167                         CamundaRequest camundaRequest = new CamundaRequest();
168                         CamundaInput camundaInput = new CamundaInput();
169                         CamundaInput host = new CamundaInput();
170                         CamundaInput schema = new CamundaInput();
171                         CamundaInput reqid = new CamundaInput();
172                         CamundaInput svcid = new CamundaInput();
173                         CamundaInput timeout = new CamundaInput();
174                         camundaInput.setValue(StringUtils.defaultString(reqXML));
175                         host.setValue(parseURL());
176                         schema.setValue(StringUtils.defaultString(schemaVersion));
177                         reqid.setValue(requestId);
178                         svcid.setValue(serviceInstanceId);
179                         timeout.setValue(StringUtils.defaultString(requestTimeout));
180                         camundaRequest.setServiceInput(camundaInput);
181                         camundaRequest.setHost(host);
182                         camundaRequest.setReqid(reqid);
183                         camundaRequest.setSvcid(svcid);
184                         camundaRequest.setSchema(schema);
185                         camundaRequest.setTimeout(timeout);
186                         ObjectMapper mapper = new ObjectMapper();
187
188                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
189
190                         jsonReq = mapper.writeValueAsString(camundaRequest);
191                         msoLogger.trace("request body is " + jsonReq);
192                 }catch(Exception e){
193                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
194                 }
195                 return jsonReq;
196         }
197         
198
199         protected String wrapVIDRequest(String requestId, boolean isBaseVfModule,
200                         int recipeTimeout, String requestAction, String serviceInstanceId, String correlationId,
201                         String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
202                         String serviceType, String vnfType, String vfModuleType, String networkType,
203                         String requestDetails, String apiVersion, boolean aLaCarte, String requestUri, String paramXsd){
204                 String jsonReq = null;
205
206                 try{
207                         CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
208                         CamundaInput serviceInput = new CamundaInput();
209                         CamundaInput host = new CamundaInput();
210                         CamundaInput requestIdInput= new CamundaInput();
211                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
212                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
213                         CamundaInput requestActionInput = new CamundaInput();
214                         CamundaInput serviceInstanceIdInput = new CamundaInput();
215                         CamundaInput correlationIdInput = new CamundaInput();
216                         CamundaInput vnfIdInput = new CamundaInput();
217                         CamundaInput vfModuleIdInput = new CamundaInput();
218                         CamundaInput volumeGroupIdInput = new CamundaInput();
219                         CamundaInput networkIdInput = new CamundaInput();
220                         CamundaInput configurationIdInput = new CamundaInput();
221                         CamundaInput serviceTypeInput = new CamundaInput();
222                         CamundaInput vnfTypeInput = new CamundaInput();
223                         CamundaInput vfModuleTypeInput = new CamundaInput();
224                         CamundaInput networkTypeInput = new CamundaInput();
225                         CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
226                         CamundaInput apiVersionInput = new CamundaInput();
227                         CamundaInput requestUriInput = new CamundaInput();
228                         CamundaInput recipeParamsInput = new CamundaInput();
229                         
230                         //host.setValue(parseURL());
231                         requestIdInput.setValue(StringUtils.defaultString(requestId));
232                         isBaseVfModuleInput.setValue(isBaseVfModule);
233                         recipeTimeoutInput.setValue(recipeTimeout);
234                         requestActionInput.setValue(StringUtils.defaultString(requestAction));
235                         serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
236                         correlationIdInput.setValue(StringUtils.defaultString(correlationId));
237                         vnfIdInput.setValue(StringUtils.defaultString(vnfId));
238                         vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
239                         volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
240                         networkIdInput.setValue(StringUtils.defaultString(networkId));
241                         configurationIdInput.setValue(StringUtils.defaultString(configurationId));
242                         serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
243                         vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
244                         vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
245                         networkTypeInput.setValue(StringUtils.defaultString(networkType));
246                         aLaCarteInput.setValue(aLaCarte);
247                         apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
248                         requestUriInput.setValue(StringUtils.defaultString(requestUri));
249                         recipeParamsInput.setValue(paramXsd);
250
251                         serviceInput.setValue(requestDetails);
252                         camundaRequest.setServiceInput(serviceInput);
253                         camundaRequest.setHost(host);
254                         camundaRequest.setRequestId(requestIdInput);
255                         camundaRequest.setMsoRequestId(requestIdInput);
256                         camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
257                         camundaRequest.setRecipeTimeout(recipeTimeoutInput);
258                         camundaRequest.setRequestAction(requestActionInput);
259                         camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
260                         camundaRequest.setCorrelationId(correlationIdInput);
261                         camundaRequest.setVnfId(vnfIdInput);
262                         camundaRequest.setVfModuleId(vfModuleIdInput);
263                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
264                         camundaRequest.setNetworkId(networkIdInput);
265                         camundaRequest.setConfigurationId(configurationIdInput);
266                         camundaRequest.setServiceType(serviceTypeInput);
267                         camundaRequest.setVnfType(vnfTypeInput);
268                         camundaRequest.setVfModuleType(vfModuleTypeInput);
269                         camundaRequest.setNetworkType(networkTypeInput);
270                         camundaRequest.setaLaCarte(aLaCarteInput);
271                         camundaRequest.setApiVersion(apiVersionInput);
272                         camundaRequest.setRequestUri(requestUriInput);
273                         camundaRequest.setRecipeParams(recipeParamsInput);
274                         
275                         ObjectMapper mapper = new ObjectMapper();
276                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
277
278                         jsonReq = mapper.writeValueAsString(camundaRequest);
279                         msoLogger.trace("request body is " + jsonReq);
280                 }catch(Exception e){
281                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
282                 }
283                 return jsonReq;
284         }
285
286         private String parseURL(){
287                 String[] parts = url.split(":");
288                 String host = "";
289                 if(parts.length>=2){
290                         host = parts[1];
291                         if(host.length()>2){
292                                 host = host.substring(2);
293                         }
294                 }
295                 return host;
296         }
297 }