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