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