1710 Rebase - Second Attempt
[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.ExceptionUtil\r
35 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\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                         execution.setVariable("rolledBack", null)\r
69                         execution.setVariable("rollbackError", null)\r
70                         \r
71                         if (execution.getVariable("disableRollback").equals("true" ))\r
72                         {\r
73                                 execution.setVariable("skipRollback", true)\r
74                         }\r
75 \r
76                         String vnfId = rollbackData.get("VNF", "vnfId")\r
77                         utils.log("DEBUG", "Rollback vnfId is: " + vnfId, isDebugEnabled)\r
78                         execution.setVariable("DCVAMR_vnfId", vnfId)\r
79                         \r
80                         execution.setVariable("mso-request-id", execution.getVariable("msoRequestId"))\r
81                         \r
82                         execution.setVariable("DCVAMR_rollbackSDNCAssign", rollbackData.get("VNF", "rollbackSDNCAssign"))\r
83                         execution.setVariable("DCVAMR_rollbackSDNCActivate", rollbackData.get("VNF", "rollbackSDNCActivate"))\r
84                         execution.setVariable("DCVAMR_rollbackVnfCreate", rollbackData.get("VNF", "rollbackVnfCreate"))\r
85                         \r
86                         String sdncCallbackUrl = rollbackData.get("VNF", "sdncCallbackUrl")\r
87                         utils.log("DEBUG", "Rollback sdncCallbackUrl is: " + sdncCallbackUrl, isDebugEnabled)\r
88                         execution.setVariable("DCVAMR_sdncCallbackUrl", sdncCallbackUrl)\r
89                         \r
90                         String tenantId= rollbackData.get("VNF", "tenantId")\r
91                         utils.log("DEBUG", "Rollback tenantId is: " + tenantId, isDebugEnabled)\r
92                         execution.setVariable("DCVAMR_tenantId", tenantId)\r
93                         \r
94                         String source= rollbackData.get("VNF", "source")\r
95                         utils.log("DEBUG", "Rollback source is: " + source, isDebugEnabled)\r
96                         execution.setVariable("DCVAMR_source", source)\r
97                         \r
98                         String serviceInstanceId = rollbackData.get("VNF", "serviceInstanceId")\r
99                         utils.log("DEBUG", "Rollback serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)\r
100                         execution.setVariable("DCVAMR_serviceInstanceId", serviceInstanceId)\r
101                         \r
102                         String cloudSiteId = rollbackData.get("VNF", "cloudSiteId")\r
103                         utils.log("DEBUG", "Rollback cloudSiteId is: " + cloudSiteId, isDebugEnabled)\r
104                         execution.setVariable("DCVAMR_cloudSiteId", cloudSiteId)\r
105                         \r
106                         def numOfAddOnModulesString = rollbackData.get("VNFANDMODULES", "numOfCreatedAddOnModules")\r
107                         int numOfAddOnModules = 0\r
108                         if (numOfAddOnModulesString != null) {\r
109                                 numOfAddOnModules = Integer.parseInt(numOfAddOnModulesString)                           \r
110                         }\r
111                         execution.setVariable("DCVAMR_numOfAddOnModules", numOfAddOnModules)\r
112                         \r
113                         def baseVfModuleRollbackMap = rollbackData.get("VFMODULE_BASE")\r
114                         if (baseVfModuleRollbackMap == null) {\r
115                                 // there are no VF Modules to delete\r
116                                 execution.setVariable("DCVAMR_numOfModulesToDelete", 0)\r
117                         }\r
118                         else {\r
119                                 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfAddOnModules + 1)                             \r
120                         }                       \r
121                         \r
122                 }catch(BpmnError b){\r
123                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
124                         throw b\r
125                 }catch(Exception e){\r
126                         utils.log("DEBUG", " Error Occured in DoCreateVnfAndModulesRollback PreProcessRequest method!" + e.getMessage(), isDebugEnabled)\r
127                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModulesRollback PreProcessRequest")\r
128 \r
129                 }\r
130                 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModulesRollback PreProcessRequest Process ***", isDebugEnabled)\r
131         }\r
132 \r
133         \r
134         \r
135         public void preProcessCreateVfModuleRollback(Execution execution){\r
136                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
137                 execution.setVariable("prefix", Prefix)\r
138                 logDebug(" ======== STARTED preProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
139                 \r
140                 try {\r
141                         \r
142                         def rollbackData = execution.getVariable("rollbackData")\r
143                         \r
144                         def vfModuleRollbackData = new RollbackData()\r
145                         \r
146                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")\r
147                         logDebug("numOfModulesToDelete: " + numOfModulesToDelete, isDebugLogEnabled)\r
148                         def moduleMap = null\r
149                         \r
150                         if (numOfModulesToDelete > 1) {\r
151                                 int addOnModuleIndex = numOfModulesToDelete - 1\r
152                                 moduleMap = rollbackData.get("VFMODULE_ADDON_" + addOnModuleIndex)\r
153                                 logDebug("Removing ADDON VF module # " + addOnModuleIndex, isDebugLogEnabled)\r
154                         }\r
155                         else {\r
156                                 moduleMap = rollbackData.get("VFMODULE_BASE")\r
157                                 logDebug("Removing BASE VF module", isDebugLogEnabled)\r
158                         }\r
159                         moduleMap.each{ k, v -> vfModuleRollbackData.put("VFMODULE", "${k}","${v}") }\r
160                         execution.setVariable("DCVAMR_RollbackData", vfModuleRollbackData)                                                              \r
161                         \r
162                 }catch(Exception e){\r
163                         utils.log("ERROR", "Exception Occured Processing preProcessCreateVfModuleRollback. Exception is:\n" + e, isDebugLogEnabled)\r
164                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessCreateVfModuleRollback Method:\n" + e.getMessage())\r
165                 }\r
166                 logDebug("======== COMPLETED preProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
167         }\r
168         \r
169         \r
170         public void postProcessCreateVfModuleRollback(Execution execution){\r
171                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
172                 execution.setVariable("prefix", Prefix)\r
173                 logDebug(" ======== STARTED postProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
174                 def rolledBack = false\r
175                 \r
176                 try {\r
177                         rolledBack = execution.getVariable("DCVM_rolledBack")\r
178                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")\r
179                         execution.setVariable("DCVAMR_numOfModulesToDelete", numOfModulesToDelete - 1)          \r
180                 }catch(Exception e){\r
181                         utils.log("ERROR", "Exception Occured Processing postProcessCreateVfModuleRollback. Exception is:\n" + e, isDebugLogEnabled)\r
182                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessCreateVfModuleRollback Method:\n" + e.getMessage())\r
183                 }\r
184                 if (rolledBack == false) {\r
185                         logDebug("Failure on DoCreateVfModuleRollback", isDebugLogEnabled)\r
186                         utils.log("ERROR", "Unsuccessful rollback of DoCreateVfModule")\r
187                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during rollback of DoCreateVfModule")\r
188                 }\r
189                 logDebug("======== COMPLETED postProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)\r
190         }\r
191         \r
192         \r
193         public void preProcessSDNCDeactivateRequest(Execution execution){\r
194                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
195                 execution.setVariable("prefix", Prefix)\r
196                 logDebug(" ======== STARTED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)\r
197                 def vnfId = execution.getVariable("vnfId")\r
198                 def serviceInstanceId = execution.getVariable("serviceInstanceId")\r
199 \r
200                 try{\r
201                         //Build SDNC Request\r
202                         \r
203                         String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")\r
204 \r
205                         deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)\r
206                         execution.setVariable(Prefix + "deactivateSDNCRequest", deactivateSDNCRequest)\r
207                         logDebug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest, isDebugLogEnabled)\r
208                         utils.logAudit("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)\r
209 \r
210                 }catch(Exception e){\r
211                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCDeactivateRequest. Exception is:\n" + e, isDebugLogEnabled)\r
212                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())\r
213                 }\r
214                 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)\r
215         }\r
216         \r
217         public void preProcessSDNCUnassignRequest(Execution execution) {\r
218                 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +\r
219                         'execution=' + execution.getId() +\r
220                         ')'\r
221                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
222                 logDebug('Entered ' + method, isDebugLogEnabled)\r
223                 execution.setVariable("prefix", Prefix)\r
224                 logDebug(" ======== STARTED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)\r
225                 try{\r
226                         String vnfId = execution.getVariable("vnfId")\r
227                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
228 \r
229                         String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")\r
230 \r
231                         execution.setVariable(Prefix + "unassignSDNCRequest", unassignSDNCRequest)\r
232                         logDebug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest, isDebugLogEnabled)\r
233                         utils.logAudit("Outgoing UnassignSDNCRequest is: \n"  + unassignSDNCRequest)\r
234 \r
235                 }catch(Exception e){\r
236                         log.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e, isDebugLogEnabled)\r
237                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCUnassignRequest Method:\n" + e.getMessage())\r
238                 }\r
239                 logDebug("======== COMPLETED  preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)\r
240         }\r
241         \r
242         public String buildSDNCRequest(Execution execution, String svcInstId, String action){\r
243                 \r
244                                 String uuid = execution.getVariable('testReqId') // for junits\r
245                                 if(uuid==null){\r
246                                         uuid = execution.getVariable("msoRequestId") + "-" +    System.currentTimeMillis()\r
247                                 }\r
248                                 def callbackURL = execution.getVariable(Prefix + "sdncCallbackUrl")\r
249                                 def requestId = execution.getVariable("msoRequestId")                           \r
250                                 def tenantId = execution.getVariable(Prefix + "tenantId")\r
251                                 def source = execution.getVariable(Prefix + "source")\r
252                                 def vnfId = execution.getVariable(Prefix + "vnfId")\r
253                                 def serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")\r
254                                 def cloudSiteId = execution.getVariable(Prefix + "cloudSiteId")\r
255                                 \r
256                                 String sdncRequest =\r
257                                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"\r
258                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"\r
259                                                                                                         xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1">\r
260            <sdncadapter:RequestHeader>\r
261                                 <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>\r
262                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>\r
263                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>\r
264                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>\r
265                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>\r
266                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>\r
267                 </sdncadapter:RequestHeader>\r
268         <sdncadapterworkflow:SDNCRequestData>\r
269                 <request-information>\r
270                         <request-id>${requestId}</request-id>\r
271                         <request-action>DeleteVnfInstance</request-action>\r
272                         <source>${source}</source>\r
273                         <notification-url/>\r
274                         <order-number/>\r
275                         <order-version/>\r
276                 </request-information>\r
277                 <service-information>\r
278                         <service-id/>\r
279                         <subscription-service-type/>                    \r
280                         <service-instance-id>${serviceInstanceId}</service-instance-id>\r
281                         <global-customer-id/>\r
282                 </service-information>\r
283                 <vnf-information>\r
284                         <vnf-id>${vnfId}</vnf-id>\r
285                         <vnf-type/>                     \r
286                 </vnf-information>\r
287                 <vnf-request-input>                     \r
288                         <vnf-name/>\r
289                         <tenant>${tenantId}</tenant>\r
290                         <aic-cloud-region>${cloudSiteId}</aic-cloud-region>                     \r
291                 </vnf-request-input>\r
292         </sdncadapterworkflow:SDNCRequestData>\r
293         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""\r
294                 \r
295                         utils.logAudit("sdncRequest:  " + sdncRequest)\r
296                         return sdncRequest\r
297         }\r
298                 \r
299         public void validateSDNCResponse(Execution execution, String response, String method){\r
300                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
301                 execution.setVariable("prefix",Prefix)\r
302                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)\r
303 \r
304                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
305                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")\r
306 \r
307                 utils.logAudit("workflowException: " + workflowException)\r
308 \r
309                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)\r
310                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)\r
311 \r
312                 utils.logAudit("SDNCResponse: " + response)\r
313 \r
314                 String sdncResponse = response\r
315                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){\r
316                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)\r
317                 }else{\r
318                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)\r
319                         throw new BpmnError("MSOWorkflowException")\r
320                 }\r
321                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)\r
322         }\r
323         \r
324         public void setSuccessfulRollbackStatus (Execution execution){\r
325                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
326                 execution.setVariable("prefix", Prefix)\r
327                 logDebug(" ======== STARTED setSuccessfulRollbackStatus ======== ", isDebugLogEnabled)\r
328         \r
329                 try{\r
330                         // Set rolledBack to true, rollbackError to null\r
331                         execution.setVariable("rolledBack", true)\r
332                         execution.setVariable("rollbackError", null)\r
333         \r
334                 }catch(Exception e){\r
335                         utils.log("ERROR", "Exception Occured Processing setSuccessfulRollbackStatus. Exception is:\n" + e, isDebugLogEnabled)\r
336                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setSuccessfulRollbackStatus Method:\n" + e.getMessage())\r
337                 }\r
338                 logDebug("======== COMPLETED setSuccessfulRollbackStatus ======== ", isDebugLogEnabled)\r
339         }\r
340         \r
341         public void setFailedRollbackStatus (Execution execution){\r
342                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
343                 execution.setVariable("prefix", Prefix)\r
344                 logDebug(" ======== STARTED setFailedRollbackStatus ======== ", isDebugLogEnabled)\r
345         \r
346                 try{\r
347                         // Set rolledBack to false, rollbackError to actual value, rollbackData to null\r
348                         execution.setVariable("rolledBack", false)\r
349                         def rollbackError = execution.getVariable("rollbackError")\r
350                         if (rollbackError == null) {\r
351                                 execution.setVariable("rollbackError", 'Caught exception in DoCreateVnfAndModulesRollback')\r
352                         }\r
353                         execution.setVariable("rollbackData", null)\r
354         \r
355                 }catch(Exception e){\r
356                         utils.log("ERROR", "Exception Occured Processing setFailedRollbackStatus. Exception is:\n" + e, isDebugLogEnabled)\r
357                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setFailedRollbackStatus Method:\n" + e.getMessage())\r
358                 }\r
359                 logDebug("======== COMPLETED setFailedRollbackStatus ======== ", isDebugLogEnabled)\r
360         }\r
361         \r
362         \r
363 }\r