AT&T 1712 and 1802 release code
[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.delegate.DelegateExecution\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.PolicyClient\r
35 import org.openecomp.mso.client.policy.PolicyClientImpl\r
36 import org.openecomp.mso.client.policy.entities.DictionaryData\r
37 import org.openecomp.mso.client.policy.entities.PolicyDecision\r
38 import org.openecomp.mso.client.policy.entities.Treatments\r
39 import org.openecomp.mso.client.policy.PolicyRestClient\r
40 \r
41 \r
42 import com.att.ecomp.mso.bpmn.core.domain.*\r
43 \r
44 import groovy.json.*\r
45 \r
46 /**\r
47  * This groovy class supports the <class>RainyDayHandler.bpmn</class> process.\r
48  *\r
49  * @author\r
50  *\r
51  * Inputs:\r
52  * @param - msoRequestId\r
53  * @param - isDebugLogEnabled\r
54  * @param - serviceType\r
55  * @param - vnfType\r
56  * @param - currentActivity\r
57  * @param - workStep\r
58  * @param - failedActivity\r
59  * @param - errorCode\r
60  * @param - errorText \r
61  * @param - vnfName\r
62  *\r
63  * Outputs:\r
64  * @param - WorkflowException\r
65  * @param - handlingCode\r
66  *\r
67  */\r
68 public class RainyDayHandler extends AbstractServiceTaskProcessor {\r
69 \r
70         String Prefix="RDH_"\r
71         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
72         \r
73         JsonUtils jsonUtils = new JsonUtils()\r
74 \r
75         public void preProcessRequest (DelegateExecution execution) {\r
76                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
77                 String msg = ""\r
78                 utils.log("DEBUG"," ***** preProcessRequest of RainyDayHandler *****",  isDebugLogEnabled)\r
79 \r
80                 try {\r
81                         execution.setVariable("prefix", Prefix)\r
82                         // check for required input\r
83                         String requestId = execution.getVariable("msoRequestId")\r
84                         utils.log("DEBUG", "msoRequestId is: " + requestId, isDebugLogEnabled)          \r
85                         def serviceType = execution.getVariable("serviceType")\r
86                         utils.log("DEBUG", "serviceType is: " + serviceType, isDebugLogEnabled)\r
87                         def vnfType = execution.getVariable("vnfType")\r
88                         utils.log("DEBUG", "vnftype is: " + vnfType, isDebugLogEnabled)\r
89                         def currentActivity = execution.getVariable("currentActivity")\r
90                         utils.log("DEBUG", "currentActivity is: " + currentActivity, isDebugLogEnabled)\r
91                         def workStep = execution.getVariable("workStep")\r
92                         utils.log("DEBUG", "workStep is: " + workStep, isDebugLogEnabled)\r
93                         def failedActivity = execution.getVariable("failedActivity")\r
94                         utils.log("DEBUG", "failedActivity is: " + failedActivity, isDebugLogEnabled)\r
95                         def errorCode = execution.getVariable("errorCode")\r
96                         utils.log("DEBUG", "errorCode is: " + errorCode, isDebugLogEnabled)\r
97                         def errorText = execution.getVariable("errorText")\r
98                         utils.log("DEBUG", "errorText is: " + errorText, isDebugLogEnabled)\r
99                         String defaultPolicyDisposition = (String) execution.getVariable('URN_policy_default_disposition')\r
100                         utils.log("DEBUG", "defaultPolicyDisposition is: " + defaultPolicyDisposition, isDebugLogEnabled)\r
101                         execution.setVariable('defaultPolicyDisposition', defaultPolicyDisposition)\r
102                         \r
103                 } catch (BpmnError e) {\r
104                         throw e;\r
105                 } catch (Exception ex){\r
106                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
107                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
108                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
109                 }\r
110                 utils.log("DEBUG"," ***** Exit preProcessRequest of RainyDayHandler *****",  isDebugLogEnabled)\r
111         }\r
112 \r
113         public void queryPolicy (DelegateExecution execution) {\r
114                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
115                 String msg = ""\r
116                 utils.log("DEBUG"," ***** queryPolicy of RainyDayHandler *****",  isDebugLogEnabled)\r
117 \r
118                 try {\r
119 \r
120                         // check for input\r
121                         String serviceType = execution.getVariable("serviceType")\r
122                         String vnfType = execution.getVariable("vnfType")\r
123                         \r
124                         utils.log("DEBUG", "serviceType: " + serviceType, isDebugLogEnabled)\r
125                         utils.log("DEBUG", "vnfType: " + vnfType, isDebugLogEnabled)\r
126                         \r
127                         def errorCode = execution.getVariable("errorCode")\r
128                         def bbId = execution.getVariable("currentActivity")\r
129                         def workStep = execution.getVariable("workStep")\r
130                         \r
131                         utils.log("DEBUG", "Before querying policy", isDebugLogEnabled)\r
132                         \r
133                         String decision = 'DENY'\r
134                         String disposition = "Abort"\r
135                         String defaultAllowedTreatments = "rollback, skip, manual, abort"\r
136                         \r
137                         String defaultPolicyDisposition = (String) execution.getVariable('defaultPolicyDisposition')\r
138                         if (defaultPolicyDisposition != null) {\r
139                                 utils.log("DEBUG", "Setting disposition to the configured default instead of querying Policy: " + defaultPolicyDisposition, isDebugLogEnabled)\r
140                                 disposition = defaultPolicyDisposition\r
141                                 utils.log("DEBUG", "Setting default allowed treatments: " + defaultAllowedTreatments, isDebugLogEnabled)\r
142                                 execution.setVariable("validResponses", defaultAllowedTreatments)\r
143                         }\r
144                         else {\r
145                         \r
146                                 PolicyDecision decisionObject = null\r
147                         \r
148                                 try {                   \r
149                                         PolicyClient policyClient = new PolicyClientImpl()\r
150                                         utils.log("DEBUG", "Created policy client", isDebugLogEnabled)\r
151                                         decisionObject = policyClient.getDecision(serviceType, vnfType, bbId, workStep, errorCode)\r
152                                         utils.log("DEBUG", "Obtained decision object", isDebugLogEnabled)\r
153                                         DictionaryData dictClient = policyClient.getAllowedTreatments(bbId, workStep)                                   \r
154                                         Treatments treatments = dictClient.getTreatments()\r
155                                         String validResponses = treatments.getString()\r
156                                         if (validResponses != null) {\r
157                                                 validResponses = validResponses.toLowerCase()\r
158                                         }\r
159                                         utils.log("DEBUG", "Obtained validResponses: " + validResponses, isDebugLogEnabled)\r
160                                         execution.setVariable("validResponses", validResponses)\r
161                                 \r
162                                 } catch(Exception e) {\r
163                                         msg = "Exception in queryPolicy " + e.getMessage()\r
164                                         utils.log("DEBUG", msg, isDebugLogEnabled)                              \r
165                                 }\r
166                         \r
167                                                 \r
168                                 if (decisionObject != null) {\r
169                                         decision = decisionObject.getDecision()\r
170                                         disposition = decisionObject.getDetails()\r
171                                         utils.log("DEBUG", "Obtained disposition from policy engine: " + disposition, isDebugLogEnabled)\r
172                                 }\r
173                                 else {\r
174                                         disposition = "Abort"\r
175                                 }\r
176                                 if (disposition == null) {\r
177                                         disposition = "Abort"\r
178                                 }\r
179                         }                       \r
180                         execution.setVariable("handlingCode", disposition)                      \r
181                         \r
182                         utils.log("DEBUG", "Disposition: "+ disposition, isDebugLogEnabled)\r
183 \r
184                 } catch (BpmnError e) {\r
185                         utils.log("DEBUG", "BPMN exception: " + e.errorMessage, isDebugLogEnabled)\r
186                         throw e;\r
187                 } catch (Exception ex){\r
188                         msg = "Exception in queryPolicy " + ex.getMessage()\r
189                         utils.log("DEBUG", msg, isDebugLogEnabled)\r
190                         //exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
191                 }\r
192                 utils.log("DEBUG"," ***** Exit queryPolicy of RainyDayHandler *****",  isDebugLogEnabled)\r
193         }\r
194 \r
195 \r
196 \r
197         \r
198 }\r