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