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