Add SDNC Resource Create Call Common Recipe
[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 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 " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
64                                 }
65                         }
66                 }
67
68                 post.setEntity(input);
69         return client.execute(post);
70         }
71
72         @Override
73         public HttpResponse post(String jsonReq) throws ClientProtocolException, IOException{
74                 HttpPost post = new HttpPost(url);
75                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
76
77                 StringEntity input = new StringEntity(jsonReq);
78                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
79
80                 String encryptedCredentials;
81                 if(props!=null){
82                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
83                         if(encryptedCredentials != null){
84                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
85                                 if(userCredentials != null){
86                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
87                                 }
88                         }
89                 }
90
91                 post.setEntity(input);
92
93         return client.execute(post);
94         }
95
96         @Override
97         public HttpResponse post(String requestId, boolean isBaseVfModule,
98                         int recipeTimeout, String requestAction, String serviceInstanceId,
99                         String vnfId, String vfModuleId, String volumeGroupId, String networkId,
100                         String serviceType, String vnfType, String vfModuleType, String networkType,
101                         String requestDetails, String recipeParamXsd)
102                                         throws ClientProtocolException, IOException{
103                 HttpPost post = new HttpPost(url);
104                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
105                 String jsonReq = wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
106                                 serviceInstanceId, vnfId, vfModuleId, volumeGroupId, networkId,
107                                 serviceType, vnfType, vfModuleType, networkType, requestDetails, recipeParamXsd);
108
109                 StringEntity input = new StringEntity(jsonReq);
110                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
111
112                 String encryptedCredentials;
113                 if(props!=null){
114                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
115                         if(encryptedCredentials != null){
116                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
117                                 if(userCredentials != null){
118                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
119                                 }
120                         }
121                 }
122
123                 post.setEntity(input);
124
125         return client.execute(post);
126         }
127         
128         @Override
129     public HttpResponse get() {
130         return null;
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, String recipeParams){
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                         CamundaInput serviceInput = new CamundaInput();
225                         CamundaInput host = new CamundaInput();
226                         CamundaInput requestIdInput= new CamundaInput();
227                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
228                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
229                         CamundaInput requestActionInput = new CamundaInput();
230                         CamundaInput serviceInstanceIdInput = new CamundaInput();
231                         CamundaInput vnfIdInput = new CamundaInput();
232                         CamundaInput vfModuleIdInput = new CamundaInput();
233                         CamundaInput volumeGroupIdInput = new CamundaInput();
234                         CamundaInput networkIdInput = new CamundaInput();
235                         CamundaInput serviceTypeInput = new CamundaInput();
236                         CamundaInput vnfTypeInput = new CamundaInput();
237                         CamundaInput vfModuleTypeInput = new CamundaInput();
238                         CamundaInput networkTypeInput = new CamundaInput();
239                         CamundaInput recipeParamsInput = new CamundaInput();
240                         host.setValue(parseURL());
241                         requestIdInput.setValue(requestId);
242                         isBaseVfModuleInput.setValue(isBaseVfModule);
243                         recipeTimeoutInput.setValue(recipeTimeout);
244                         requestActionInput.setValue(requestAction);
245                         serviceInstanceIdInput.setValue(serviceInstanceId);
246                         vnfIdInput.setValue(vnfId);
247                         vfModuleIdInput.setValue(vfModuleId);
248                         volumeGroupIdInput.setValue(volumeGroupId);
249                         networkIdInput.setValue(networkId);
250                         serviceTypeInput.setValue(serviceType);
251                         vnfTypeInput.setValue(vnfType);
252                         vfModuleTypeInput.setValue(vfModuleType);
253                         networkTypeInput.setValue(networkType);
254                         recipeParamsInput.setValue(recipeParams);
255                         serviceInput.setValue(requestDetails);
256                         camundaRequest.setServiceInput(serviceInput);
257                         camundaRequest.setHost(host);
258                         camundaRequest.setRequestId(requestIdInput);
259                         camundaRequest.setMsoRequestId(requestIdInput);
260                         camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
261                         camundaRequest.setRecipeTimeout(recipeTimeoutInput);
262                         camundaRequest.setRequestAction(requestActionInput);
263                         camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
264                         camundaRequest.setVnfId(vnfIdInput);
265                         camundaRequest.setVfModuleId(vfModuleIdInput);
266                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
267                         camundaRequest.setNetworkId(networkIdInput);
268                         camundaRequest.setServiceType(serviceTypeInput);
269                         camundaRequest.setVnfType(vnfTypeInput);
270                         camundaRequest.setVfModuleType(vfModuleTypeInput);
271                         camundaRequest.setNetworkType(networkTypeInput);
272                         camundaRequest.setRecipeParams(recipeParamsInput);
273                         ObjectMapper mapper = new ObjectMapper();
274                         mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
275
276                         jsonReq = mapper.writeValueAsString(camundaRequest);
277                         msoLogger.debug("request body is " + jsonReq);
278                 }catch(Exception e){
279                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
280                 }
281                 return jsonReq;
282         }
283
284         private String parseURL(){
285                 String[] parts = url.split(":");
286                 String host = "";
287                 if(parts.length>=2){
288                         host = parts[1];
289                         if(host.length()>2){
290                                 host = host.substring(2);
291                         }
292                 }
293                 return host;
294         }
295
296
297 }