Merge "Reorder modifiers"
[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 import java.io.IOException;
24
25 import javax.xml.bind.DatatypeConverter;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.entity.StringEntity;
31 import org.openecomp.mso.apihandler.camundabeans.CamundaBooleanInput;
32 import org.openecomp.mso.apihandler.camundabeans.CamundaInput;
33 import org.openecomp.mso.apihandler.camundabeans.CamundaIntegerInput;
34 import org.openecomp.mso.apihandler.camundabeans.CamundaRequest;
35 import org.openecomp.mso.apihandler.camundabeans.CamundaVIDRequest;
36 import org.openecomp.mso.logger.MessageEnum;
37 import org.openecomp.mso.logger.MsoLogger;
38
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.databind.SerializationFeature;
41
42 public class CamundaClient extends RequestClient{
43         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
44         private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
45
46         public CamundaClient() {
47                 super(CommonConstants.CAMUNDA);
48         }
49
50
51         @Override
52         public HttpResponse post(String camundaReqXML, String requestId,
53                         String requestTimeout, String schemaVersion, String serviceInstanceId, String action) throws IOException {
54                 HttpPost post = new HttpPost(url);
55                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
56                 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);
57
58                 StringEntity input = new StringEntity(jsonReq);
59                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
60
61                 String encryptedCredentials;
62                 if(props!=null){
63                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
64                         if(encryptedCredentials != null){
65                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
66                                 if(userCredentials != null){
67                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
68                         .printBase64Binary(userCredentials.getBytes()));
69                                 }
70                         }
71                 }
72
73                 post.setEntity(input);
74         return client.execute(post);
75         }
76
77         @Override
78         public HttpResponse post(String jsonReq) throws IOException {
79                 HttpPost post = new HttpPost(url);
80                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
81
82                 StringEntity input = new StringEntity(jsonReq);
83                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
84
85                 String encryptedCredentials;
86                 if(props!=null){
87                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
88                         if(encryptedCredentials != null){
89                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
90                                 if(userCredentials != null){
91                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
92                         .printBase64Binary(userCredentials.getBytes()));
93                                 }
94                         }
95                 }
96
97                 post.setEntity(input);
98
99         return client.execute(post);
100         }
101
102         @Override
103         public HttpResponse post(RequestClientParamater params) throws IOException {
104                 HttpPost post = new HttpPost(url);
105                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
106                 String jsonReq = wrapVIDRequest(params);
107
108                 StringEntity input = new StringEntity(jsonReq);
109                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
110                 String encryptedCredentials;
111                 if(props!=null){
112                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH,null);
113                         if(encryptedCredentials != null){
114                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, CommonConstants.ENCRYPTION_KEY);
115                                 if(userCredentials != null){
116                                         post.addHeader("Authorization", "Basic " + DatatypeConverter
117                         .printBase64Binary(userCredentials.getBytes()));
118                                 }
119                         }
120                 }
121                 post.setEntity(input);
122         return client.execute(post);
123         }
124
125         @Override
126     public HttpResponse get() {
127         return null;
128     }
129
130         private String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
131                 String jsonReq = null;
132                 if(reqXML == null){
133                         reqXML ="";
134                 }
135                 if(requestTimeout == null){
136                         requestTimeout ="";
137                 }
138                 if(schemaVersion == null){
139                         schemaVersion = "";
140                 }
141                 try{
142                         CamundaRequest camundaRequest = new CamundaRequest();
143                         CamundaInput camundaInput = new CamundaInput();
144                         CamundaInput host = new CamundaInput();
145                         CamundaInput schema = new CamundaInput();
146                         CamundaInput reqid = new CamundaInput();
147                         CamundaInput svcid = new CamundaInput();
148                         CamundaInput timeout = new CamundaInput();
149                         camundaInput.setValue(reqXML);
150                         host.setValue(parseURL());
151                         schema.setValue(schemaVersion);
152                         reqid.setValue(requestId);
153                         svcid.setValue(serviceInstanceId);
154                         timeout.setValue(requestTimeout);
155                         camundaRequest.setServiceInput(camundaInput);
156                         camundaRequest.setHost(host);
157                         camundaRequest.setReqid(reqid);
158                         camundaRequest.setSvcid(svcid);
159                         camundaRequest.setSchema(schema);
160                         camundaRequest.setTimeout(timeout);
161                         ObjectMapper mapper = new ObjectMapper();
162                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
163
164                         jsonReq = mapper.writeValueAsString(camundaRequest);
165                         msoLogger.debug("request body is " + jsonReq);
166                 }catch(Exception e){
167                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
168                 }
169                 return jsonReq;
170         }
171
172         private String wrapVIDRequest(RequestClientParamater requestClientParamater) {
173                 String jsonReq = null;
174                 try{
175                         CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
176                         CamundaInput serviceInput = new CamundaInput();
177                         CamundaInput host = new CamundaInput();
178                         CamundaInput requestIdInput= new CamundaInput();
179                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
180                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
181                         CamundaInput requestActionInput = new CamundaInput();
182                         CamundaInput serviceInstanceIdInput = new CamundaInput();
183                         CamundaInput correlationIdInput = new CamundaInput();
184                         CamundaInput vnfIdInput = new CamundaInput();
185                         CamundaInput vfModuleIdInput = new CamundaInput();
186                         CamundaInput volumeGroupIdInput = new CamundaInput();
187                         CamundaInput networkIdInput = new CamundaInput();
188                         CamundaInput configurationIdInput = new CamundaInput();
189                         CamundaInput serviceTypeInput = new CamundaInput();
190                         CamundaInput vnfTypeInput = new CamundaInput();
191                         CamundaInput vfModuleTypeInput = new CamundaInput();
192                         CamundaInput networkTypeInput = new CamundaInput();
193                         CamundaInput recipeParamsInput = new CamundaInput();
194                         host.setValue(parseURL());
195                         requestIdInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestId()));
196                         isBaseVfModuleInput.setValue(requestClientParamater.isBaseVfModule());
197                         recipeTimeoutInput.setValue(requestClientParamater.getRecipeTimeout());
198                         requestActionInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestAction()));
199                         serviceInstanceIdInput.setValue(StringUtils.defaultString(requestClientParamater.getServiceInstanceId()));
200                         correlationIdInput.setValue(StringUtils.defaultString(requestClientParamater.getCorrelationId()));
201                         vnfIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVnfId()));
202                         vfModuleIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVfModuleId()));
203                         volumeGroupIdInput.setValue(StringUtils.defaultString(requestClientParamater.getVolumeGroupId()));
204                         networkIdInput.setValue(StringUtils.defaultString(requestClientParamater.getNetworkId()));
205                         configurationIdInput.setValue(StringUtils.defaultString(requestClientParamater.getConfigurationId()));
206                         serviceTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getServiceType()));
207                         vnfTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getVnfType()));
208                         vfModuleTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getVfModuleType()));
209                         networkTypeInput.setValue(StringUtils.defaultString(requestClientParamater.getNetworkType()));
210                         recipeParamsInput.setValue(requestClientParamater.getRecipeParamXsd());
211                         serviceInput.setValue(StringUtils.defaultString(requestClientParamater.getRequestDetails()));
212                         camundaRequest.setServiceInput(serviceInput);
213                         camundaRequest.setHost(host);
214                         camundaRequest.setRequestId(requestIdInput);
215                         camundaRequest.setMsoRequestId(requestIdInput);
216                         camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
217                         camundaRequest.setRecipeTimeout(recipeTimeoutInput);
218                         camundaRequest.setRequestAction(requestActionInput);
219                         camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
220                         camundaRequest.setCorrelationId(correlationIdInput);
221                         camundaRequest.setVnfId(vnfIdInput);
222                         camundaRequest.setVfModuleId(vfModuleIdInput);
223                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
224                         camundaRequest.setNetworkId(networkIdInput);
225                         camundaRequest.setConfigurationId(configurationIdInput);
226                         camundaRequest.setServiceType(serviceTypeInput);
227                         camundaRequest.setVnfType(vnfTypeInput);
228                         camundaRequest.setVfModuleType(vfModuleTypeInput);
229                         camundaRequest.setNetworkType(networkTypeInput);
230                         camundaRequest.setRecipeParams(recipeParamsInput);
231                         ObjectMapper mapper = new ObjectMapper();
232                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
233
234                         jsonReq = mapper.writeValueAsString(camundaRequest);
235                         msoLogger.debug("request body is " + jsonReq);
236                 }catch(Exception e){
237                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
238                 }
239                 return jsonReq;
240         }
241
242         private String parseURL(){
243                 String[] parts = url.split(":");
244                 String host = "";
245                 if(parts.length>=2){
246                         host = parts[1];
247                         if(host.length()>2){
248                                 host = host.substring(2);
249                         }
250                 }
251                 return host;
252         }
253
254 }