Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / ReceiveWorkflowMessage.groovy
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.bpmn.common.scripts;
22
23 import groovy.json.*
24
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
32
33
34
35
36 class ReceiveWorkflowMessage extends AbstractServiceTaskProcessor {
37         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ReceiveWorkflowMessage.class);
38
39
40         ExceptionUtil exceptionUtil = new ExceptionUtil()
41
42         /**
43          * Process the incoming variables.
44          *
45          * @param execution The flow's execution instance.
46          */
47 public void preProcessRequest (DelegateExecution execution) {
48                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
49                         'execution=' + execution.getId() +
50                         ')'
51                 msoLogger.trace('Entered ' + method)
52
53                 def prefix="RCVWFMSG_"
54                 execution.setVariable("prefix", prefix)
55                 setSuccessIndicator(execution, false)
56
57                 try {
58
59                         // Confirm that timeout value has been provided in 'RCVWFMSG_timeout'.
60                         def timeout = execution.getVariable('RCVWFMSG_timeout')
61                         msoLogger.debug('Timeout value is \'' + timeout + '\'')
62                         if ((timeout == null) || (timeout.isEmpty())) {
63                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_timeout\''
64                                 msoLogger.debug(msg)
65                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
66                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
67                         }
68
69                         // Confirm that message type has been provided in 'RCVWFMSG_messageType'
70                         def messageType = execution.getVariable('RCVWFMSG_messageType')
71                         msoLogger.debug('Message type is \'' + messageType + '\'')
72                         if ((messageType == null) || (messageType.isEmpty())) {
73                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_messageType\''
74                                 msoLogger.debug(msg)
75                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
76                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
77                         }
78
79                         // Confirm that correlator value has been provided in 'RCVWFMSG_correlator'
80                         def correlator = execution.getVariable('RCVWFMSG_correlator')
81                         msoLogger.debug('Correlator value is \'' + correlator + '\'')
82                         if ((correlator == null) || (correlator.isEmpty())) {
83                                 String msg = getProcessKey(execution) + ': Missing or empty input variable \'RCVWFMSG_correlator\''
84                                 msoLogger.debug(msg)
85                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
86                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
87                         }
88                         execution.setVariable(messageType + '_CORRELATOR', correlator)
89
90                         msoLogger.trace('Exited ' + method)
91                 } catch (BpmnError e) {
92                         throw e
93                 } catch (Exception e) {
94                         String msg = 'Caught exception in ' + method + ": " + e
95                         msoLogger.debug(msg)
96                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
97                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
98                 }
99         }
100
101         /**
102          * Process a received message.
103          *
104          * @param execution The flow's execution instance.
105          */
106         public void processReceivedMessage(DelegateExecution execution){
107                 def method = getClass().getSimpleName() + '.processReceivedMessage(' +
108                         'execution=' + execution.getId() +
109                         ')'
110                 msoLogger.trace('Entered ' + method)
111
112                 String messageType = null;
113                 String receivedMessage = null;
114
115                 try {
116                         messageType = execution.getVariable('RCVWFMSG_messageType')
117                         receivedMessage = execution.getVariable(messageType + '_MESSAGE')
118                         msoLogger.debug(getProcessKey(execution) + ": received message:\n" + receivedMessage)
119
120                         // The received message is made available to the calling flow in WorkflowResponse
121                         execution.setVariable("WorkflowResponse", receivedMessage)
122
123                         setSuccessIndicator(execution, true)
124
125                         msoLogger.trace('Exited ' + method)
126                 } catch (Exception e) {
127                         receivedMessage = receivedMessage == null || String.valueOf(receivedMessage).isEmpty() ? "NONE" : receivedMessage
128                         String msg = "Error processing received workflow message: " + receivedMessage
129                         msoLogger.debug(getProcessKey(execution) + ': ' + msg)
130                         exceptionUtil.buildWorkflowException(execution, 7020, msg)
131                 }
132         }
133 }