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