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