Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ReceiveWorkflowMessage.groovy
1 package org.openecomp.mso.bpmn.common.scripts;\r
2 \r
3 import groovy.json.*\r
4 \r
5 import org.apache.commons.lang3.*\r
6 import org.camunda.bpm.engine.delegate.BpmnError\r
7 import org.camunda.bpm.engine.delegate.DelegateExecution\r
8 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
9 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
10 \r
11 \r
12 class ReceiveWorkflowMessage extends AbstractServiceTaskProcessor {\r
13 \r
14         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
15 \r
16         /**\r
17          * Process the incoming variables.\r
18          *\r
19          * @param execution The flow's execution instance.\r
20          */\r
21 public void preProcessRequest (DelegateExecution execution) {\r
22                 def method = getClass().getSimpleName() + '.preProcessRequest(' +\r
23                         'execution=' + execution.getId() +\r
24                         ')'\r
25                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
26                 logDebug('Entered ' + method, isDebugLogEnabled)\r
27 \r
28                 def prefix="RCVWFMSG_"\r
29                 execution.setVariable("prefix", prefix)\r
30                 setSuccessIndicator(execution, false)\r
31 \r
32                 try {\r
33 \r
34                         // Confirm that timeout value has been provided in 'RCVWFMSG_timeout'.\r
35                         def timeout = execution.getVariable('RCVWFMSG_timeout')\r
36                         logDebug('Timeout value is \'' + timeout + '\'', isDebugLogEnabled)\r
37                         if ((timeout == null) || (timeout.isEmpty())) {\r
38                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_timeout\''\r
39                                 logDebug(msg, isDebugLogEnabled)\r
40                                 logError(msg)\r
41                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)\r
42                         }\r
43 \r
44                         // Confirm that message type has been provided in 'RCVWFMSG_messageType'\r
45                         def messageType = execution.getVariable('RCVWFMSG_messageType')\r
46                         logDebug('Message type is \'' + messageType + '\'', isDebugLogEnabled)\r
47                         if ((messageType == null) || (messageType.isEmpty())) {\r
48                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_messageType\''\r
49                                 logDebug(msg, isDebugLogEnabled)\r
50                                 logError(msg)\r
51                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)\r
52                         }\r
53 \r
54                         // Confirm that correlator value has been provided in 'RCVWFMSG_correlator'\r
55                         def correlator = execution.getVariable('RCVWFMSG_correlator')\r
56                         logDebug('Correlator value is \'' + correlator + '\'', isDebugLogEnabled)\r
57                         if ((correlator == null) || (correlator.isEmpty())) {\r
58                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_correlator\''\r
59                                 logDebug(msg, isDebugLogEnabled)\r
60                                 logError(msg)\r
61                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)\r
62                         }\r
63                         execution.setVariable(messageType + '_CORRELATOR', correlator)\r
64 \r
65                         logDebug('Exited ' + method, isDebugLogEnabled)\r
66                 } catch (BpmnError e) {\r
67                         throw e\r
68                 } catch (Exception e) {\r
69                         String msg = 'Caught exception in ' + method + ": " + e\r
70                         logDebug(msg, isDebugLogEnabled)\r
71                         logError(msg)\r
72                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)\r
73                 }\r
74         }\r
75 \r
76         /**\r
77          * Process a received message.\r
78          *\r
79          * @param execution The flow's execution instance.\r
80          */\r
81         public void processReceivedMessage(DelegateExecution execution){\r
82                 def method = getClass().getSimpleName() + '.processReceivedMessage(' +\r
83                         'execution=' + execution.getId() +\r
84                         ')'\r
85                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
86                 logDebug('Entered ' + method, isDebugLogEnabled)\r
87 \r
88                 String messageType = null;\r
89                 String receivedMessage = null;\r
90 \r
91                 try {\r
92                         messageType = execution.getVariable('RCVWFMSG_messageType')\r
93                         receivedMessage = execution.getVariable(messageType + '_MESSAGE')\r
94                         logDebug(getProcessKey(execution) + ": received message:\n" + receivedMessage, isDebugLogEnabled)\r
95 \r
96                         // The received message is made available to the calling flow in WorkflowResponse\r
97                         execution.setVariable("WorkflowResponse", receivedMessage)\r
98 \r
99                         setSuccessIndicator(execution, true)\r
100 \r
101                         logDebug('Exited ' + method, isDebugLogEnabled)\r
102                 } catch (Exception e) {\r
103                         receivedMessage = receivedMessage == null || String.valueOf(receivedMessage).isEmpty() ? "NONE" : receivedMessage\r
104                         String msg = "Error processing received workflow message: " + receivedMessage\r
105                         logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)\r
106                         exceptionUtil.buildWorkflowException(execution, 7020, msg)\r
107                 }\r
108         }\r
109 }\r