0a307f3ce0b401a754acf3a8a7ff4e11eb588a65
[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         @Override
134     public HttpResponse get() {
135         return null;
136     }
137
138         private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
139                 String jsonReq = null;
140                 if(reqXML == null){
141                         reqXML ="";
142                 }
143                 if(requestTimeout == null){
144                         requestTimeout ="";
145                 }
146                 if(schemaVersion == null){
147                         schemaVersion = "";
148                 }
149
150
151                 try{
152                         CamundaRequest camundaRequest = new CamundaRequest();
153                         CamundaInput camundaInput = new CamundaInput();
154                         CamundaInput host = new CamundaInput();
155                         CamundaInput schema = new CamundaInput();
156                         CamundaInput reqid = new CamundaInput();
157                         CamundaInput svcid = new CamundaInput();
158                         CamundaInput timeout = new CamundaInput();
159                         camundaInput.setValue(reqXML);
160                         host.setValue(parseURL());
161                         schema.setValue(schemaVersion);
162                         reqid.setValue(requestId);
163                         svcid.setValue(serviceInstanceId);
164                         timeout.setValue(requestTimeout);
165                         camundaRequest.setServiceInput(camundaInput);
166                         camundaRequest.setHost(host);
167                         camundaRequest.setReqid(reqid);
168                         camundaRequest.setSvcid(svcid);
169                         camundaRequest.setSchema(schema);
170                         camundaRequest.setTimeout(timeout);
171                         ObjectMapper mapper = new ObjectMapper();
172                         mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
173
174                         jsonReq = mapper.writeValueAsString(camundaRequest);
175                         msoLogger.debug("request body is " + jsonReq);
176                 }catch(Exception e){
177                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
178                 }
179                 return jsonReq;
180         }
181
182         private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
183                         int recipeTimeout, String requestAction, String serviceInstanceId,
184                         String vnfId, String vfModuleId, String volumeGroupId, String networkId,
185                         String serviceType, String vnfType, String vfModuleType, String networkType,
186                         String requestDetails){
187                 String jsonReq = null;
188                 if(requestId == null){
189                         requestId ="";
190                 }
191                 if(requestAction == null){
192                         requestAction ="";
193                 }
194                 if(serviceInstanceId == null){
195                         serviceInstanceId ="";
196                 }
197                 if(vnfId == null){
198                         vnfId ="";
199                 }
200                 if(vfModuleId == null){
201                         vfModuleId ="";
202                 }
203                 if(volumeGroupId == null){
204                         volumeGroupId ="";
205                 }
206                 if(networkId == null){
207                         networkId ="";
208                 }
209                 if(serviceType == null){
210                         serviceType ="";
211                 }
212                 if(vnfType == null){
213                         vnfType ="";
214                 }
215                 if(vfModuleType == null){
216                         vfModuleType ="";
217                 }
218                 if(networkType == null){
219                         networkType ="";
220                 }
221                 if(requestDetails == null){
222                         requestDetails ="";
223                 }
224
225
226
227                 try{
228                         CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
229                         CamundaInput serviceInput = new CamundaInput();
230                         CamundaInput host = new CamundaInput();
231                         CamundaInput requestIdInput= new CamundaInput();
232                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
233                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
234                         CamundaInput requestActionInput = new CamundaInput();
235                         CamundaInput serviceInstanceIdInput = new CamundaInput();
236                         CamundaInput vnfIdInput = new CamundaInput();
237                         CamundaInput vfModuleIdInput = new CamundaInput();
238                         CamundaInput volumeGroupIdInput = new CamundaInput();
239                         CamundaInput networkIdInput = new CamundaInput();
240                         CamundaInput serviceTypeInput = new CamundaInput();
241                         CamundaInput vnfTypeInput = new CamundaInput();
242                         CamundaInput vfModuleTypeInput = new CamundaInput();
243                         CamundaInput networkTypeInput = new CamundaInput();
244
245                         host.setValue(parseURL());
246                         requestIdInput.setValue(requestId);
247                         isBaseVfModuleInput.setValue(isBaseVfModule);
248                         recipeTimeoutInput.setValue(recipeTimeout);
249                         requestActionInput.setValue(requestAction);
250                         serviceInstanceIdInput.setValue(serviceInstanceId);
251                         vnfIdInput.setValue(vnfId);
252                         vfModuleIdInput.setValue(vfModuleId);
253                         volumeGroupIdInput.setValue(volumeGroupId);
254                         networkIdInput.setValue(networkId);
255                         serviceTypeInput.setValue(serviceType);
256                         vnfTypeInput.setValue(vnfType);
257                         vfModuleTypeInput.setValue(vfModuleType);
258                         networkTypeInput.setValue(networkType);
259
260                         serviceInput.setValue(requestDetails);
261                         camundaRequest.setServiceInput(serviceInput);
262                         camundaRequest.setHost(host);
263                         camundaRequest.setRequestId(requestIdInput);
264                         camundaRequest.setMsoRequestId(requestIdInput);
265                         camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
266                         camundaRequest.setRecipeTimeout(recipeTimeoutInput);
267                         camundaRequest.setRequestAction(requestActionInput);
268                         camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
269                         camundaRequest.setVnfId(vnfIdInput);
270                         camundaRequest.setVfModuleId(vfModuleIdInput);
271                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
272                         camundaRequest.setNetworkId(networkIdInput);
273                         camundaRequest.setServiceType(serviceTypeInput);
274                         camundaRequest.setVnfType(vnfTypeInput);
275                         camundaRequest.setVfModuleType(vfModuleTypeInput);
276                         camundaRequest.setNetworkType(networkTypeInput);
277
278                         ObjectMapper mapper = new ObjectMapper();
279                         mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
280
281                         jsonReq = mapper.writeValueAsString(camundaRequest);
282                         msoLogger.debug("request body is " + jsonReq);
283                 }catch(Exception e){
284                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
285                 }
286                 return jsonReq;
287         }
288
289         private String parseURL(){
290                 String[] parts = url.split(":");
291                 String host = "";
292                 if(parts.length>=2){
293                         host = parts[1];
294                         if(host.length()>2){
295                                 host = host.substring(2);
296                         }
297                 }
298                 return host;
299         }
300
301
302 }