Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / RainyDayHandler.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.common.scripts;\r
21 \r
22 import static org.apache.commons.lang3.StringUtils.*;\r
23 \r
24 \r
25 \r
26 import org.apache.commons.lang3.*\r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.runtime.Execution\r
29 import org.json.JSONObject;\r
30 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
31 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
32 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition\r
33 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
34 import org.openecomp.mso.client.policy.PolicyDecision\r
35 import org.openecomp.mso.client.policy.PolicyRestClient\r
36 \r
37 \r
38 import com.att.ecomp.mso.bpmn.core.domain.*\r
39 \r
40 import groovy.json.*\r
41 \r
42 /**\r
43  * This groovy class supports the <class>RainyDayHandler.bpmn</class> process.\r
44  *\r
45  * @author\r
46  *\r
47  * Inputs:\r
48  * @param - msoRequestId\r
49  * @param - isDebugLogEnabled\r
50  * @param - serviceType\r
51  * @param - vnfType\r
52  * @param - currentActivity\r
53  * @param - workStep\r
54  * @param - failedActivity\r
55  * @param - errorCode\r
56  * @param - errorText \r
57  *\r
58  * Outputs:\r
59  * @param - WorkflowException\r
60  * @param - handlingCode\r
61  *\r
62  */\r
63 public class RainyDayHandler extends AbstractServiceTaskProcessor {\r
64 \r
65         String Prefix="RDH_"\r
66         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
67         \r
68         JsonUtils jsonUtils = new JsonUtils()\r
69 \r
70         public void preProcessRequest (Execution execution) {\r
71                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
72                 String msg = ""\r
73                 utils.log("DEBUG"," ***** preProcessRequest of RainyDayHandler *****",  isDebugLogEnabled)\r
74 \r
75                 try {\r
76                         execution.setVariable("prefix", Prefix)\r
77                         // check for required input\r
78                         String requestId = execution.getVariable("msoRequestId")\r
79                         utils.log("DEBUG", "msoRequestId is: " + requestId, isDebugLogEnabled)          \r
80                         def serviceType = execution.getVariable("serviceType")\r
81                         utils.log("DEBUG", "serviceType is: " + serviceType, isDebugLogEnabled)\r
82                         def vnfType = execution.getVariable("vnfType")\r
83                         utils.log("DEBUG", "vnftype is: " + vnfType, isDebugLogEnabled)\r
84                         def currentActivity = execution.getVariable("currentActivity")\r
85                         utils.log("DEBUG", "currentActivity is: " + currentActivity, isDebugLogEnabled)\r
86                         def workStep = execution.getVariable("workStep")\r
87                         utils.log("DEBUG", "workStep is: " + workStep, isDebugLogEnabled)\r
88                         def failedActivity = execution.getVariable("failedActivity")\r
89                         utils.log("DEBUG", "failedActivity is: " + failedActivity, isDebugLogEnabled)\r
90                         def errorCode = execution.getVariable("errorCode")\r
91                         utils.log("DEBUG", "errorCode is: " + errorCode, isDebugLogEnabled)\r
92                         def errorText = execution.getVariable("errorText")\r
93                         utils.log("DEBUG", "errorText is: " + errorText, isDebugLogEnabled)\r
94                         \r
95                 } catch (BpmnError e) {\r
96                         throw e;\r
97                 } catch (Exception ex){\r
98                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
99                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
100                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
101                 }\r
102                 utils.log("DEBUG"," ***** Exit preProcessRequest of RainyDayHandler *****",  isDebugLogEnabled)\r
103         }\r
104 \r
105         public void queryPolicy (Execution execution) {\r
106                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
107                 String msg = ""\r
108                 utils.log("DEBUG"," ***** queryPolicy of RainyDayHandler *****",  isDebugLogEnabled)\r
109 \r
110                 try {\r
111 \r
112                         // check for input\r
113                         String serviceType = execution.getVariable("serviceType")\r
114                         String vnfType = execution.getVariable("vnfType")\r
115                         \r
116                         utils.log("DEBUG", "serviceType: " + serviceType, isDebugLogEnabled)\r
117                         utils.log("DEBUG", "vnfType: " + vnfType, isDebugLogEnabled)\r
118                         \r
119                         def errorCode = execution.getVariable("errorCode")\r
120                         def bbId = execution.getVariable("currentActivity")\r
121                         def workStep = execution.getVariable("workStep")\r
122                         \r
123                         utils.log("DEBUG", "Before querying policy", isDebugLogEnabled)\r
124                         \r
125                         PolicyDecision decisionObject = null\r
126                         \r
127                         try {                   \r
128                                 PolicyRestClient policyClient = new PolicyRestClient()\r
129                                 utils.log("DEBUG", "Created policy client", isDebugLogEnabled)\r
130                                 decisionObject = policyClient.getDecision(serviceType, vnfType, bbId, workStep, errorCode)\r
131                                 utils.log("DEBUG", "Obtained decision object", isDebugLogEnabled)\r
132                         } catch(Exception e) {\r
133                                 msg = "Exception in queryPolicy " + e.getMessage()\r
134                                 utils.log("DEBUG", msg, isDebugLogEnabled)\r
135                                 \r
136                         }\r
137                         \r
138                         String decision = 'DENY'\r
139                         String disposition = "Abort"                    \r
140                         if (decisionObject != null) {\r
141                                 decision = decisionObject.getDecision()\r
142                                 disposition = decisionObject.getDetails()\r
143                                 utils.log("DEBUG", "Obtained disposition from policy engine: " + disposition, isDebugLogEnabled)\r
144                         }\r
145                         else {\r
146                                 disposition = "Manual"\r
147                         }\r
148                         if (disposition == null) {\r
149                                 disposition = "Manual"\r
150                         }                       \r
151                         execution.setVariable("handlingCode", disposition)\r
152                         execution.setVariable("validResponses", "rollback, abort, skip, retry")\r
153                         utils.log("DEBUG", "Disposition: "+ disposition, isDebugLogEnabled)\r
154 \r
155                 } catch (BpmnError e) {\r
156                         utils.log("DEBUG", "BPMN exception: " + e.errorMessage, isDebugLogEnabled)\r
157                         throw e;\r
158                 } catch (Exception ex){\r
159                         msg = "Exception in queryPolicy " + ex.getMessage()\r
160                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
161                         //exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
162                 }\r
163                 utils.log("DEBUG"," ***** Exit queryPolicy of RainyDayHandler *****",  isDebugLogEnabled)\r
164         }\r
165 \r
166 \r
167 \r
168         \r
169 }\r