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