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