9258cabe5bf079da2e8e5b325df6dc2c1faf7ac1
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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         private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
40
41         public CamundaClient() {
42                 super(CommonConstants.CAMUNDA);
43         }
44
45
46         @Override
47         public HttpResponse post(String camundaReqXML, String requestId,
48                         String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
49                                         throws ClientProtocolException, IOException{
50                 HttpPost post = new HttpPost(url);
51                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
52                 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);
53
54                 StringEntity input = new StringEntity(jsonReq);
55                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
56
57                 String encryptedCredentials;
58                 if(props!=null){
59                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
60                         if(encryptedCredentials != null){
61                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
62                                 if(userCredentials != null){
63                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
64                         .printBase64Binary(userCredentials.getBytes()));
65                                 }
66                         }
67                 }
68
69                 post.setEntity(input);
70         return client.execute(post);
71         }
72
73         @Override
74         public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException{
75                 HttpPost post = new HttpPost(url);
76                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
77
78                 StringEntity input = new StringEntity(jsonReq);
79                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
80
81                 String encryptedCredentials;
82                 if(props!=null){
83                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
84                         if(encryptedCredentials != null){
85                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
86                                 if(userCredentials != null){
87                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
88                         .printBase64Binary(userCredentials.getBytes()));
89                                 }
90                         }
91                 }
92
93                 post.setEntity(input);
94
95         return client.execute(post);
96         }
97
98         @Override
99         public HttpResponse post(String requestId, boolean isBaseVfModule,
100                         int recipeTimeout, String requestAction, String serviceInstanceId,
101                         String vnfId, String vfModuleId, String volumeGroupId, String networkId,
102                         String serviceType, String vnfType, String vfModuleType, String networkType,
103                         String requestDetails, String recipeParamXsd)
104                                         throws ClientProtocolException, IOException{
105                 HttpPost post = new HttpPost(url);
106                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
107                 String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
108                                 serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId,
109                                 serviceType, vnfType, vfModuleType, networkType, requestDetails, recipeParamXsd);
110
111                 StringEntity input = new StringEntity(jsonReq);
112                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
113
114                 String encryptedCredentials;
115                 if(props!=null){
116                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
117                         if(encryptedCredentials != null){
118                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
119                                 if(userCredentials != null){
120                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
121                         .printBase64Binary(userCredentials.getBytes()));
122                                 }
123                         }
124                 }
125
126                 post.setEntity(input);
127
128         return client.execute(post);
129         }
130         
131         @Override
132     public HttpResponse get() {
133         return null;
134     }
135
136         private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
137                 String jsonReq = null;
138                 if(reqXML == null){
139                         reqXML ="";
140                 }
141                 if(requestTimeout == null){
142                         requestTimeout ="";
143                 }
144                 if(schemaVersion == null){
145                         schemaVersion = "";
146                 }
147
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(reqXML);
158                         host.setValue(parseURL());
159                         schema.setValue(schemaVersion);
160                         reqid.setValue(requestId);
161                         svcid.setValue(serviceInstanceId);
162                         timeout.setValue(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                         mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
171
172                         jsonReq = mapper.writeValueAsString(camundaRequest);
173                         msoLogger.debug("request body is " + jsonReq);
174                 }catch(Exception e){
175                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
176                 }
177                 return jsonReq;
178         }
179
180         private String wrapVIDRequest(String requestId, boolean isBaseVfModule,
181                         int recipeTimeout, String requestAction, String serviceInstanceId,
182                         String vnfId, String vfModuleId, String volumeGroupId, String networkId,
183                         String serviceType, String vnfType, String vfModuleType, String networkType,
184                         String requestDetails, String recipeParams){
185                 String jsonReq = null;
186                 if(requestId == null){
187                         requestId ="";
188                 }
189                 if(requestAction == null){
190                         requestAction ="";
191                 }
192                 if(serviceInstanceId == null){
193                         serviceInstanceId ="";
194                 }
195                 if(vnfId == null){
196                         vnfId ="";
197                 }
198                 if(vfModuleId == null){
199                         vfModuleId ="";
200                 }
201                 if(volumeGroupId == null){
202                         volumeGroupId ="";
203                 }
204                 if(networkId == null){
205                         networkId ="";
206                 }
207                 if(serviceType == null){
208                         serviceType ="";
209                 }
210                 if(vnfType == null){
211                         vnfType ="";
212                 }
213                 if(vfModuleType == null){
214                         vfModuleType ="";
215                 }
216                 if(networkType == null){
217                         networkType ="";
218                 }
219                 if(requestDetails == null){
220                         requestDetails ="";
221                 }
222
223
224
225                 try{
226                         CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
227                         CamundaInput serviceInput = new CamundaInput();
228                         CamundaInput host = new CamundaInput();
229                         CamundaInput requestIdInput= new CamundaInput();
230                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
231                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
232                         CamundaInput requestActionInput = new CamundaInput();
233                         CamundaInput serviceInstanceIdInput = new CamundaInput();
234                         CamundaInput vnfIdInput = new CamundaInput();
235                         CamundaInput vfModuleIdInput = new CamundaInput();
236                         CamundaInput volumeGroupIdInput = new CamundaInput();
237                         CamundaInput networkIdInput = new CamundaInput();
238                         CamundaInput serviceTypeInput = new CamundaInput();
239                         CamundaInput vnfTypeInput = new CamundaInput();
240                         CamundaInput vfModuleTypeInput = new CamundaInput();
241                         CamundaInput networkTypeInput = new CamundaInput();
242                         CamundaInput recipeParamsInput = new CamundaInput();
243                         host.setValue(parseURL());
244                         requestIdInput.setValue(requestId);
245                         isBaseVfModuleInput.setValue(isBaseVfModule);
246                         recipeTimeoutInput.setValue(recipeTimeout);
247                         requestActionInput.setValue(requestAction);
248                         serviceInstanceIdInput.setValue(serviceInstanceId);
249                         vnfIdInput.setValue(vnfId);
250                         vfModuleIdInput.setValue(vfModuleId);
251                         volumeGroupIdInput.setValue(volumeGroupId);
252                         networkIdInput.setValue(networkId);
253                         serviceTypeInput.setValue(serviceType);
254                         vnfTypeInput.setValue(vnfType);
255                         vfModuleTypeInput.setValue(vfModuleType);
256                         networkTypeInput.setValue(networkType);
257                         recipeParamsInput.setValue(recipeParams);
258                         serviceInput.setValue(requestDetails);
259                         camundaRequest.setServiceInput(serviceInput);
260                         camundaRequest.setHost(host);
261                         camundaRequest.setRequestId(requestIdInput);
262                         camundaRequest.setMsoRequestId(requestIdInput);
263                         camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
264                         camundaRequest.setRecipeTimeout(recipeTimeoutInput);
265                         camundaRequest.setRequestAction(requestActionInput);
266                         camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
267                         camundaRequest.setVnfId(vnfIdInput);
268                         camundaRequest.setVfModuleId(vfModuleIdInput);
269                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
270                         camundaRequest.setNetworkId(networkIdInput);
271                         camundaRequest.setServiceType(serviceTypeInput);
272                         camundaRequest.setVnfType(vnfTypeInput);
273                         camundaRequest.setVfModuleType(vfModuleTypeInput);
274                         camundaRequest.setNetworkType(networkTypeInput);
275                         camundaRequest.setRecipeParams(recipeParamsInput);
276                         ObjectMapper mapper = new ObjectMapper();
277                         mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
278
279                         jsonReq = mapper.writeValueAsString(camundaRequest);
280                         msoLogger.debug("request body is " + jsonReq);
281                 }catch(Exception e){
282                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
283                 }
284                 return jsonReq;
285         }
286
287         private String parseURL(){
288                 String[] parts = url.split(":");
289                 String host = "";
290                 if(parts.length>=2){
291                         host = parts[1];
292                         if(host.length()>2){
293                                 host = host.substring(2);
294                         }
295                 }
296                 return host;
297         }
298
299
300 }