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