2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import org.onap.so.bpmn.core.UrnPropertiesReader
26 import org.onap.so.logger.ErrorCode
28 import java.text.SimpleDateFormat
30 import org.apache.commons.lang3.*
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.onap.so.logger.MessageEnum
33 import org.slf4j.Logger
34 import org.slf4j.LoggerFactory
36 public class FalloutHandler extends AbstractServiceTaskProcessor {
37 private static final Logger logger = LoggerFactory.getLogger( FalloutHandler.class);
40 ExceptionUtil exceptionUtil = new ExceptionUtil()
42 public initializeProcessVariables(DelegateExecution execution){
43 def method = getClass().getSimpleName() + '.initializeProcessVariables(' +'execution=' + execution.getId() +')'
44 logger.trace('Entered ' + method)
47 execution.setVariable("prefix",Prefix)
49 //These variables are form the input Message to the BPMN
50 execution.setVariable("FH_request_id","")
51 execution.setVariable("FH_request_action","")
52 execution.setVariable("FH_notification-url","")
53 execution.setVariable("FH_mso-bpel-name","")
54 execution.setVariable("FH_ErrorCode", "")
55 execution.setVariable("FH_ErrorMessage", "")
57 execution.setVariable("FH_notification-url-Ok", false)
58 execution.setVariable("FH_request_id-Ok", false)
60 //These variables are for Get Mso Aai Password Adapter
61 execution.setVariable("FH_deliveryStatus", true)
63 //update Response Status to pending ...Adapter variables
64 execution.setVariable("FH_updateResponseStatusPayload", null)
65 execution.setVariable("FH_updateResponseStatusResponse", null)
67 //update Request Gamma ...Adapter variables
68 execution.setVariable("FH_updateRequestGammaPayload", "")
69 execution.setVariable("FH_updateRequestGammaResponse", null)
70 execution.setVariable("FH_updateRequestGammaResponseCode", null)
72 //update Request Infra ...Adapter variables
73 execution.setVariable("FH_updateRequestInfraPayload", "")
74 execution.setVariable("FH_updateRequestInfraResponse", null)
75 execution.setVariable("FH_updateRequestInfraResponseCode", null)
77 //assign True to success variable
78 execution.setVariable("FH_success", true)
80 //Set notify status to Failed variable
81 execution.setVariable("FH_NOTIFY_STATUS", "SUCCESS")
83 //Set DB update variable
84 execution.setVariable("FH_updateRequestPayload", "")
85 execution.setVariable("FH_updateRequestResponse", null)
86 execution.setVariable("FH_updateRequestResponseCode", null)
89 execution.setVariable("BasicAuthHeaderValue","")
92 execution.setVariable("FH_parameterList", "")
95 execution.setVariable("FalloutHandlerResponse","")
96 execution.setVariable("FH_ErrorResponse", null)
97 execution.setVariable("FH_ResponseCode", "")
99 } catch (Exception e) {
100 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
101 'Caught exception in ' + method, "BPMN",
102 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
103 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
107 public void preProcessRequest (DelegateExecution execution) {
108 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
109 logger.trace('Entered ' + method)
111 // Initialize flow variables
112 initializeProcessVariables(execution)
113 setSuccessIndicator(execution, false)
115 setBasicDBAuthHeader(execution, execution.getVariable('isDebugLogEnabled'))
118 def xml = execution.getVariable("FalloutHandlerRequest")
119 logger.debug(" XML --> " + xml)
120 logger.debug("FalloutHandler request: " + xml)
122 //Check the incoming request type
123 //Incoming request can be ACTIVE_REQUESTS (request-information node) or INFRA_ACTIVE_REQUESTS (request-info node)
124 if (utils.nodeExists(xml, "request-information")) {
125 execution.setVariable("FH_request_id-Ok", true) // Incoming request is for ACTIVE_REQUESTS
128 //Check notification-url for the incoming request type
129 //ACTIVE_REQUESTS may have notificationurl node
130 //INFRA_ACTIVE_REQUESTS notificationurl node does not exist
131 def notificationurl = ""
132 if (utils.nodeExists(xml, "notification-url")) {
133 notificationurl = utils.getNodeText(xml,"notification-url")
134 if(notificationurl != null && !notificationurl.isEmpty()) {
135 logger.debug("********** Incoming notification Url is: " + notificationurl);
136 execution.setVariable("FH_notification-url-Ok", true)
137 execution.setVariable("FH_notification-url",notificationurl)
141 //Check request_id for the incoming request type
142 //For INFRA_ACTIVE_REQUESTS payload request-id IS optional (Not sure why this is option since req id is primary key ... also tried exe through SOAP UI to check if MSO code handles null like auto generated seq not it does not)
143 //For ACTIVE_REQUESTS payload request-id is NOT optional
145 if (utils.nodeExists(xml, "request-id")) {
146 execution.setVariable("FH_request_id",utils.getNodeText(xml,"request-id"))
148 logger.debug("FH_request_id: " + execution.getVariable("FH_request_id"))
150 // INFRA_ACTIVE_REQUESTS have "action" element ... mandatory
151 // ACTIVE_REQUEST have "request-action" ... mandatory
152 if (utils.nodeExists(xml, "request-action")) {
153 execution.setVariable("FH_request_action",utils.getNodeText(xml,"request-action"))
154 } else if (utils.nodeExists(xml, "action")) {
155 execution.setVariable("FH_request_action",utils.getNodeText(xml,"action"))
159 //Check source for the incoming request type
160 //For INFRA_ACTIVE_REQUESTS payload source IS optional
161 //For ACTIVE_REQUESTS payload source is NOT optional
163 if (utils.nodeExists(xml, "source")) {
164 execution.setVariable("FH_source",utils.getNodeText(xml,"source"))
167 //Check if ErrorCode node exists. If yes, initialize it from request xml, if no, it will stay with defaulf value already set in initializeProcessVariables() method above.
169 if (utils.nodeExists(xml, "ErrorCode")) {
170 errorCode = utils.getNodeText(xml,"ErrorCode")
171 if(errorCode != null && !errorCode.isEmpty()) {
172 execution.setVariable("FH_ErrorCode", errorCode)
175 logger.debug("FH_ErrorCode: " + errorCode)
177 //Check if ErrorMessage node exists. If yes, initialize it from request xml, if no, it will stay with defaulf value already set in initializeProcessVariables() method above.
178 def errorMessage = ""
179 if (utils.nodeExists(xml, "ErrorMessage")) {
180 errorCode = utils.getNodeText(xml,"ErrorMessage")
181 if(errorCode != null && !errorCode.isEmpty()) {
182 errorCode = errorCode
183 execution.setVariable("FH_ErrorMessage", errorCode)
187 //Check for Parameter List
188 if (utils.nodeExists(xml, "parameter-list")) {
189 def parameterList = utils.getNodeXml(xml, "parameter-list", false)
190 execution.setVariable("FH_parameterList", parameterList)
193 logger.trace("--> " + execution.getVariable(""))
194 logger.debug("FH_request_id-OK --> " + execution.getVariable("FH_request_id-Ok"))
196 // set the DHV/Service Instantiation values if specified in the request
197 execution.setVariable("FH_is_srv_inst_req", String.valueOf("true".equals(utils.getNodeText(xml, "is-srv-inst-req"))))
198 logger.trace("--> " + execution.getVariable(""))
199 execution.setVariable("FH_is_json_content", String.valueOf("JSON".equals(utils.getNodeText(xml, "resp-content-type"))))
200 logger.trace("--> " + execution.getVariable(""))
201 execution.setVariable("FH_service_inst_id", utils.getNodeText(xml, "service-instance-id"))
202 logger.trace("--> " + execution.getVariable(""))
203 execution.setVariable("FH_start_time", utils.getNodeText(xml, "start-time"))
204 logger.trace("--> " + execution.getVariable(""))
205 // this variable is used by the camunda flow to set the Content-Type for the async response
206 if (execution.getVariable("FH_is_srv_inst_req").equals("true") &&
207 execution.getVariable("FH_is_json_content").equals("true")) {
208 execution.setVariable("FH_content_type", "application/json")
210 execution.setVariable("FH_content_type", "text/xml")
212 } catch (Exception e) {
213 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
214 'Caught exception in ' + method, "BPMN",
215 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
216 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in" + method)
219 logger.debug("OUTOF --> Initialize Variables Fallout Handler #########");
222 public String updateRequestPayload (DelegateExecution execution){
223 def method = getClass().getSimpleName() + '.updateRequestPayload(' +'execution=' + execution.getId() +')'
224 logger.trace('Entered ' + method)
228 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
232 <requestId>${MsoUtils.xmlEscape(execution.getVariable("FH_request_id"))}</requestId>
233 <lastModifiedBy>BPEL</lastModifiedBy>
234 <finalErrorMessage>${MsoUtils.xmlEscape(execution.getVariable("FH_ErrorMessage"))}</finalErrorMessage>
235 <finalErrorCode>${MsoUtils.xmlEscape(execution.getVariable("FH_ErrorCode"))}</finalErrorCode>
236 <status>FAILED</status>
237 <responseStatus>${MsoUtils.xmlEscape(execution.getVariable("FH_NOTIFY_STATUS"))}</responseStatus>
243 logger.debug("updateRequestPayload: " + payload)
244 execution.setVariable("FH_updateRequestPayload", payload)
245 return execution.getVariable("FH_updateRequestPayload")
246 } catch (Exception e) {
247 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
248 'Caught exception in ' + method, "BPMN",
249 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
250 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
254 public String updateRequestInfraPayload (DelegateExecution execution){
255 def method = getClass().getSimpleName() + '.updateRequestInfraPayload(' +'execution=' + execution.getId() +')'
256 logger.trace('Entered ' + method)
260 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
263 <req:updateInfraRequest>
264 <requestId>${MsoUtils.xmlEscape(execution.getVariable("FH_request_id"))}</requestId>
265 <lastModifiedBy>BPEL</lastModifiedBy>
266 <statusMessage>${MsoUtils.xmlEscape(execution.getVariable("FH_ErrorMessage"))}</statusMessage>
267 <requestStatus>FAILED</requestStatus>
268 <progress>100</progress>
269 </req:updateInfraRequest>
274 execution.setVariable("FH_updateRequestInfraPayload", payload)
275 logger.debug("updateRequestInfraPayload: " + payload)
276 return execution.getVariable("FH_updateRequestInfraPayload")
277 } catch (Exception e) {
278 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
279 'Caught exception in ' + method, "BPMN",
280 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
281 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
285 public String updateRequestGammaPayload (DelegateExecution execution){
286 def method = getClass().getSimpleName() + '.updateRequestGammaPayload(' +'execution=' + execution.getId() +')'
287 logger.trace('Entered ' + method)
291 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="${UrnPropertiesReader.getVariable("mso.default.adapter.namespace", execution)}/requestsdb">
295 <requestId>${MsoUtils.xmlEscape(execution.getVariable("FH_request_id"))}</requestId>
296 <lastModifiedBy>BPEL</lastModifiedBy>
297 <finalErrorMessage>${MsoUtils.xmlEscape(execution.getVariable("FH_ErrorMessage"))}</finalErrorMessage>
298 <finalErrorCode>${MsoUtils.xmlEscape(execution.getVariable("FH_ErrorCode"))}</finalErrorCode>
299 <status>FAILED</status>
305 execution.setVariable("FH_updateRequestGammaPayload", payload)
306 logger.debug("updateRequestGammaPayload: " + payload)
307 return execution.getVariable("FH_updateRequestGammaPayload")
308 } catch (Exception e) {
309 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
310 'Caught exception in ' + method, "BPMN",
311 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
312 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
316 public String updateResponseStatusPayload (DelegateExecution execution){
317 def method = getClass().getSimpleName() + '.updateResponseStatusPayload(' +'execution=' + execution.getId() +')'
318 logger.trace('Entered ' + method)
322 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://org.onap.so/requestsdb">
325 <req:updateResponseStatus>
326 <requestId>${MsoUtils.xmlEscape(execution.getVariable("FH_request_id"))}</requestId>
327 <lastModifiedBy>BPEL</lastModifiedBy>
328 <responseStatus>SENDING_FINAL_NOTIFY</responseStatus>
329 </req:updateResponseStatus>
334 execution.setVariable("FH_updateResponseStatusPayload", payload)
335 logger.debug("updateResponseStatusPayload: " + payload)
336 return execution.getVariable("FH_updateResponseStatusPayload")
337 } catch (Exception e) {
338 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
339 'Caught exception in ' + method, "BPMN",
340 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
341 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
345 public void buildDBWorkflowException(DelegateExecution execution, String responseCodeVariable) {
346 def method = getClass().getSimpleName() + '.buildDBWorkflowException(' +
347 'execution=' + execution.getId() +
348 ', responseCodeVariable=' + responseCodeVariable + ')'
349 logger.trace('Entered ' + method)
352 def responseCode = execution.getVariable(responseCodeVariable)
353 // If the HTTP response code was null, it means a connection fault occurred (a java exception)
354 def errorMessage = responseCode == null ? "Could not connect to DB Adapter" : "DB Adapter returned ${responseCode} response"
355 def errorCode = responseCode == null ? 7000 : 7020
356 // exceptionUtil.buildWorkflowException(execution, errorCode, errorMessage)
357 } catch (Exception e) {
358 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
359 'Caught exception in ' + method, "BPMN",
360 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
361 // exceptionUtil.buildWorkflowException(execution, 2000, "Internal Error - Occured in " + method)
366 * Used to create a workflow response in success and failure cases.
368 public void postProcessResponse (DelegateExecution execution) {
369 def method = getClass().getSimpleName() + '.postProcessResponse(' +'execution=' + execution.getId() +')'
370 logger.trace('Entered ' + method)
373 Boolean success = (Boolean) execution.getVariable("FH_success")
374 String out = success ? "Fallout Handler Succeeded" : "Fallout Handler Failed";
376 String falloutHandlerResponse = """
377 <workflow:FalloutHandlerResponse xmlns:workflow="http://org.onap/so/workflow/schema/v1">
378 <workflow:out>${MsoUtils.xmlEscape(out)}</workflow:out>
379 </workflow:FalloutHandlerResponse>
382 falloutHandlerResponse = utils.formatXml(falloutHandlerResponse)
383 logger.debug("FalloutHandler Response: " + falloutHandlerResponse);
385 execution.setVariable("FalloutHandlerResponse", falloutHandlerResponse)
386 execution.setVariable("WorkflowResponse", falloutHandlerResponse)
387 execution.setVariable("FH_ResponseCode", success ? "200" : "500")
388 setSuccessIndicator(execution, success)
390 logger.debug("FalloutHandlerResponse =\n" + falloutHandlerResponse)
391 } catch (Exception e) {
392 // Do NOT throw WorkflowException!
393 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
394 'Caught exception in ' + method, "BPMN",
395 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);