0ca85f0a065346e7c8925f02cc663db1dce071ae
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
26 import java.io.IOException;
27 import java.util.UUID;
28
29 import javax.xml.bind.DatatypeConverter;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.ClientProtocolException;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.StringEntity;
36 import org.onap.logging.ref.slf4j.ONAPLogConstants;
37 import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
38 import org.onap.so.apihandler.camundabeans.CamundaInput;
39 import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
40 import org.onap.so.apihandler.camundabeans.CamundaRequest;
41 import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
42 import org.onap.so.logger.MessageEnum;
43 import org.onap.so.logger.MsoLogger;
44 import org.slf4j.MDC;
45
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
48
49 public class CamundaClient extends RequestClient{
50         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, CamundaClient.class);
51         private static final String CAMUNDA_URL_MESAGE = "Camunda url is: ";
52
53         public CamundaClient() {
54                 super(CommonConstants.CAMUNDA);
55         }
56
57
58         @Override
59         public HttpResponse post(String camundaReqXML, String requestId,
60                         String requestTimeout, String schemaVersion, String serviceInstanceId, String action)
61                                         throws ClientProtocolException, IOException{
62                 HttpPost post = new HttpPost(url);
63                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
64                 String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout,  schemaVersion);
65
66                 StringEntity input = new StringEntity(jsonReq);
67                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
68                 msoLogger.info("Camunda Request Content: " + jsonReq);
69                 
70
71                 post.setEntity(input);
72                 setupHeaders(post);
73
74                 HttpResponse response = client.execute(post);
75                 msoLogger.debug("Response is: " + response);
76                 
77                 return response;
78         }
79
80
81         private void setupHeaders(HttpPost post) {
82                 post.addHeader(ONAPLogConstants.Headers.REQUEST_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
83                 post.addHeader(ONAPLogConstants.Headers.INVOCATION_ID, UUID.randomUUID().toString());
84                 
85                 String encryptedCredentials = null;
86                 if(props!=null){
87                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
88                         if(encryptedCredentials != null){
89                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
90                                 if(userCredentials != null){
91                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
92                                 }
93                         }
94                 }
95         }
96
97         @Override
98         public HttpResponse post(String jsonReq)
99                                         throws ClientProtocolException, IOException{
100                 HttpPost post = new HttpPost(url);
101                 msoLogger.debug(CAMUNDA_URL_MESAGE + url);
102
103                 StringEntity input = new StringEntity(jsonReq);
104                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
105                 setupHeaders(post);
106
107                 String encryptedCredentials = null;
108                 if(props!=null){
109                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
110                         if(encryptedCredentials != null){
111                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
112                                 if(userCredentials != null){
113                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
114                                 }
115                         }
116                 }
117
118
119                 post.setEntity(input);
120                 HttpResponse response = client.execute(post);
121                 msoLogger.debug("Response is: " + response);
122
123                 return response;
124         }
125
126         public HttpResponse post(RequestClientParameter parameterObject)
127                                         throws ClientProtocolException, IOException{
128                 HttpPost post = new HttpPost(url);
129                 msoLogger.debug(CAMUNDA_URL_MESAGE+ url);
130                 String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(), parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
131                                 parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(), parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(), parameterObject.getNetworkId(), parameterObject.getConfigurationId(),
132                                 parameterObject.getServiceType(), parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(), parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(), parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
133                                 parameterObject.getInstanceGroupId());
134
135                 StringEntity input = new StringEntity(jsonReq);
136                 input.setContentType(CommonConstants.CONTENT_TYPE_JSON);
137
138
139                 setupHeaders(post);
140
141                 String encryptedCredentials = null;
142                 if(props!=null){
143                         encryptedCredentials = props.getProperty(CommonConstants.CAMUNDA_AUTH);
144                         if(encryptedCredentials != null){
145                                 String userCredentials = getEncryptedPropValue(encryptedCredentials, CommonConstants.DEFAULT_BPEL_AUTH, props.getProperty(CommonConstants.ENCRYPTION_KEY_PROP));
146                                 if(userCredentials != null){
147                                         post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));
148                                 }
149                         }
150                 }
151
152                 post.setEntity(input);
153                 HttpResponse response = client.execute(post);
154                 msoLogger.debug("Response is: " + response);
155
156                 return response;
157         }
158         
159         @Override
160     public HttpResponse get() {
161         return null;
162     }
163
164         protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout, String schemaVersion){
165                 String jsonReq = null;
166
167                 try{
168                         CamundaRequest camundaRequest = new CamundaRequest();
169                         CamundaInput camundaInput = new CamundaInput();
170                         CamundaInput host = new CamundaInput();
171                         CamundaInput schema = new CamundaInput();
172                         CamundaInput reqid = new CamundaInput();
173                         CamundaInput svcid = new CamundaInput();
174                         CamundaInput timeout = new CamundaInput();
175                         camundaInput.setValue(StringUtils.defaultString(reqXML));
176                         host.setValue(parseURL());
177                         schema.setValue(StringUtils.defaultString(schemaVersion));
178                         reqid.setValue(requestId);
179                         svcid.setValue(serviceInstanceId);
180                         timeout.setValue(StringUtils.defaultString(requestTimeout));
181                         camundaRequest.setServiceInput(camundaInput);
182                         camundaRequest.setHost(host);
183                         camundaRequest.setReqid(reqid);
184                         camundaRequest.setSvcid(svcid);
185                         camundaRequest.setSchema(schema);
186                         camundaRequest.setTimeout(timeout);
187                         ObjectMapper mapper = new ObjectMapper();
188
189                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
190
191                         jsonReq = mapper.writeValueAsString(camundaRequest);
192                         msoLogger.trace("request body is " + jsonReq);
193                 }catch(Exception e){
194                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
195                 }
196                 return jsonReq;
197         }
198         
199
200         protected String wrapVIDRequest(String requestId, boolean isBaseVfModule,
201                         int recipeTimeout, String requestAction, String serviceInstanceId, String pnfCorrelationId,
202                         String vnfId, String vfModuleId, String volumeGroupId, String networkId, String configurationId,
203                         String serviceType, String vnfType, String vfModuleType, String networkType,
204                         String requestDetails, String apiVersion, boolean aLaCarte, String requestUri, String paramXsd,
205                         String instanceGroupId){
206                 String jsonReq = null;
207
208                 try{
209                         CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
210                         CamundaInput serviceInput = new CamundaInput();
211                         CamundaInput host = new CamundaInput();
212                         CamundaInput requestIdInput= new CamundaInput();
213                         CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
214                         CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
215                         CamundaInput requestActionInput = new CamundaInput();
216                         CamundaInput serviceInstanceIdInput = new CamundaInput();
217                         CamundaInput pnfCorrelationIdInput = new CamundaInput();
218                         CamundaInput vnfIdInput = new CamundaInput();
219                         CamundaInput vfModuleIdInput = new CamundaInput();
220                         CamundaInput volumeGroupIdInput = new CamundaInput();
221                         CamundaInput networkIdInput = new CamundaInput();
222                         CamundaInput configurationIdInput = new CamundaInput();
223                         CamundaInput serviceTypeInput = new CamundaInput();
224                         CamundaInput vnfTypeInput = new CamundaInput();
225                         CamundaInput vfModuleTypeInput = new CamundaInput();
226                         CamundaInput networkTypeInput = new CamundaInput();
227                         CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
228                         CamundaInput apiVersionInput = new CamundaInput();
229                         CamundaInput requestUriInput = new CamundaInput();
230                         CamundaInput recipeParamsInput = new CamundaInput();
231                         CamundaInput instanceGroupIdInput = new CamundaInput();
232                         
233                         //host.setValue(parseURL());
234                         requestIdInput.setValue(StringUtils.defaultString(requestId));
235                         isBaseVfModuleInput.setValue(isBaseVfModule);
236                         recipeTimeoutInput.setValue(recipeTimeout);
237                         requestActionInput.setValue(StringUtils.defaultString(requestAction));
238                         serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
239                         pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
240                         vnfIdInput.setValue(StringUtils.defaultString(vnfId));
241                         vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
242                         volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
243                         networkIdInput.setValue(StringUtils.defaultString(networkId));
244                         configurationIdInput.setValue(StringUtils.defaultString(configurationId));
245                         serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
246                         vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
247                         vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
248                         networkTypeInput.setValue(StringUtils.defaultString(networkType));
249                         aLaCarteInput.setValue(aLaCarte);
250                         apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
251                         requestUriInput.setValue(StringUtils.defaultString(requestUri));
252                         recipeParamsInput.setValue(paramXsd);
253                         instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
254
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.setPnfCorrelationId(pnfCorrelationIdInput);
265                         camundaRequest.setVnfId(vnfIdInput);
266                         camundaRequest.setVfModuleId(vfModuleIdInput);
267                         camundaRequest.setVolumeGroupId(volumeGroupIdInput);
268                         camundaRequest.setNetworkId(networkIdInput);
269                         camundaRequest.setConfigurationId(configurationIdInput);
270                         camundaRequest.setServiceType(serviceTypeInput);
271                         camundaRequest.setVnfType(vnfTypeInput);
272                         camundaRequest.setVfModuleType(vfModuleTypeInput);
273                         camundaRequest.setNetworkType(networkTypeInput);
274                         camundaRequest.setaLaCarte(aLaCarteInput);
275                         camundaRequest.setApiVersion(apiVersionInput);
276                         camundaRequest.setRequestUri(requestUriInput);
277                         camundaRequest.setRecipeParams(recipeParamsInput);
278                         camundaRequest.setInstanceGroupId(instanceGroupIdInput);
279                         
280                         ObjectMapper mapper = new ObjectMapper();
281                         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
282
283                         jsonReq = mapper.writeValueAsString(camundaRequest);
284                         msoLogger.trace("request body is " + jsonReq);
285                 }catch(Exception e){
286                         msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request", e);
287                 }
288                 return jsonReq;
289         }
290
291         private String parseURL(){
292                 String[] parts = url.split(":");
293                 String host = "";
294                 if(parts.length>=2){
295                         host = parts[1];
296                         if(host.length()>2){
297                                 host = host.substring(2);
298                         }
299                 }
300                 return host;
301         }
302 }