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