[MSO-8] Second step of the rebase for MSO
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVnfAndModulesRollback.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\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.infrastructure.scripts\r
21 \r
22 import java.util.UUID;\r
23 \r
24 import org.json.JSONObject;\r
25 import org.json.JSONArray;\r
26 \r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.runtime.Execution;\r
29 \r
30 import static org.apache.commons.lang3.StringUtils.*;\r
31 \r
32 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
33 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
34 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils\r
35 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
36 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
37 import org.openecomp.mso.bpmn.core.RollbackData\r
38 import org.openecomp.mso.bpmn.core.WorkflowException\r
39 \r
40 /**\r
41  * This class supports the macro VID Flow\r
42  * with the rollback of a creation of a generic vnf and related VF modules.\r
43  */\r
44 class DoCreateVnfAndModulesRollback extends AbstractServiceTaskProcessor {\r
45 \r
46         String Prefix="DCVAMR_"\r
47         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
48         JsonUtils jsonUtil = new JsonUtils()\r
49         \r
50 \r
51         /**\r
52          * This method gets and validates the incoming\r
53          * request.\r
54          *\r
55          * @param - execution\r
56          *\r
57          */\r
58         public void preProcessRequest(Execution execution) {\r
59                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
60                 execution.setVariable("prefix",Prefix)\r
61                 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModulesRollback PreProcessRequest Process*** ", isDebugEnabled)\r
62 \r
63                 try{\r
64                         // Get Rollback Variables\r
65                         \r
66                         def rollbackData = execution.getVariable("RollbackData")\r
67                         utils.log("DEBUG", "Incoming RollbackData is: " + rollbackData.toString(), isDebugEnabled)\r
68                         String vnfId = rollbackData.get("VNF", "vnfId")\r
69                         utils.log("DEBUG", "Rollback vnfId is: " + vnfId, isDebugEnabled)\r
70                         execution.setVariable("DCVAMR_vnfId", vnfId)\r
71                         \r
72                         def numOfAddOnModulesString = rollbackData.get("VNFANDMODULES", "numOfCreatedAddOnModules")\r
73                         int numOfAddOnModules = 0\r
74                         if (numOfAddOnModulesString != null) {\r
75                                 numOfAddOnModules = Integer.parseInt(numOfAddOnModulesString)                           \r
76                         }\r
77                         execution.setVariable("DCVAMR_numOfAddOnModules", numOfAddOnModules)\r
78                         \r
79                         def baseVfModuleRollbackMap = rollbackData.get("VFMODULE_BASE")\r
80                         if (baseVfModuleRollbackMap == null) {\r
81                                 // there are no VF Modules to delete\r
82                                 execution.setVariable("DCVAMR_numOfModulesToDelete", 0)\r
83                         }\r
84                         else {\r
85                                 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfAddOnModules + 1)                             \r
86                         }                       \r
87                         \r
88                 }catch(BpmnError b){\r
89                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
90                         throw b\r
91                 }catch(Exception e){\r
92                         utils.log("DEBUG", " Error Occured in DoCreateVnfAndModulesRollback PreProcessRequest method!" + e.getMessage(), isDebugEnabled)\r
93                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModulesRollback PreProcessRequest")\r
94 \r
95                 }\r
96                 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModulesRollback PreProcessRequest Process ***", isDebugEnabled)\r
97         }\r
98 \r
99         \r
100         \r
101         public void preProcessCreateVfModuleRollback(Execution execution){\r
102                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
103                 execution.setVariable("prefix", Prefix)\r
104                 logDebug(" ======== STARTED preProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
105                 \r
106                 try {\r
107                         \r
108                         def rollbackData = execution.getVariable("RollbackData")\r
109                         \r
110                         def vfModuleRollbackData = new RollbackData()\r
111                         \r
112                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")\r
113                         logDebug("numOfModulesToDelete: " + numOfModulesToDelete, isDebugLogEnabled)\r
114                         def moduleMap = null\r
115                         \r
116                         if (numOfModulesToDelete > 1) {\r
117                                 int addOnModuleIndex = numOfModulesToDelete - 1\r
118                                 moduleMap = rollbackData.get("VFMODULE_ADDON_" + addOnModuleIndex)\r
119                                 logDebug("Removing ADDON VF module # " + addOnModuleIndex, isDebugLogEnabled)\r
120                         }\r
121                         else {\r
122                                 moduleMap = rollbackData.get("VFMODULE_BASE")\r
123                                 logDebug("Removing BASE VF module", isDebugLogEnabled)\r
124                         }\r
125                         moduleMap.each{ k, v -> vfModuleRollbackData.put("VFMODULE", "${k}","${v}") }\r
126                         execution.setVariable("DCVAMR_RollbackData", vfModuleRollbackData)                                                              \r
127                         \r
128                 }catch(Exception e){\r
129                         utils.log("ERROR", "Exception Occured Processing preProcessCreateVfModuleRollback. Exception is:\n" + e, isDebugLogEnabled)\r
130                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessCreateVfModuleRollback Method:\n" + e.getMessage())\r
131                 }\r
132                 logDebug("======== COMPLETED preProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
133         }\r
134         \r
135         \r
136         public void postProcessCreateVfModuleRollback(Execution execution){\r
137                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
138                 execution.setVariable("prefix", Prefix)\r
139                 logDebug(" ======== STARTED postProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
140                 \r
141                 try {                   \r
142                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")\r
143                         execution.setVariable("DCVAMR_numOfModulesToDelete", numOfModulesToDelete - 1)          \r
144                 }catch(Exception e){\r
145                         utils.log("ERROR", "Exception Occured Processing postProcessCreateVfModuleRollback. Exception is:\n" + e, isDebugLogEnabled)\r
146                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessCreateVfModuleRollback Method:\n" + e.getMessage())\r
147                 }\r
148                 logDebug("======== COMPLETED postProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
149         }\r
150         \r
151         \r
152         \r
153 }\r