2  * ============LICENSE_START=======================================================
\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 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\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 
  20 package org.openecomp.mso.bpmn.infrastructure.scripts
\r 
  22 import java.util.UUID;
\r 
  24 import org.json.JSONObject;
\r 
  25 import org.json.JSONArray;
\r 
  27 import org.camunda.bpm.engine.delegate.BpmnError
\r 
  28 import org.camunda.bpm.engine.runtime.Execution;
\r 
  30 import static org.apache.commons.lang3.StringUtils.*;
\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 
  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 
  44 class DoCreateVnfAndModulesRollback extends AbstractServiceTaskProcessor {
\r 
  46         String Prefix="DCVAMR_"
\r 
  47         ExceptionUtil exceptionUtil = new ExceptionUtil()
\r 
  48         JsonUtils jsonUtil = new JsonUtils()
\r 
  52          * This method gets and validates the incoming
\r 
  55          * @param - execution
\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 
  64                         // Get Rollback Variables
\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 
  71                         if (execution.getVariable("disableRollback").equals("true" ))
\r 
  73                                 execution.setVariable("skipRollback", true)
\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 
  80                         execution.setVariable("mso-request-id", execution.getVariable("msoRequestId"))
\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 
  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 
  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 
  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 
  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 
 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 
 106                         def numOfAddOnModulesString = rollbackData.get("VNFANDMODULES", "numOfCreatedAddOnModules")
\r 
 107                         int numOfAddOnModules = 0
\r 
 108                         if (numOfAddOnModulesString != null) {
\r 
 109                                 numOfAddOnModules = Integer.parseInt(numOfAddOnModulesString)                           
\r 
 111                         execution.setVariable("DCVAMR_numOfAddOnModules", numOfAddOnModules)
\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 
 119                                 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfAddOnModules + 1)                             
\r 
 122                 }catch(BpmnError b){
\r 
 123                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
\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 
 130                 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModulesRollback PreProcessRequest Process ***", isDebugEnabled)
\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 
 142                         def rollbackData = execution.getVariable("rollbackData")
\r 
 144                         def vfModuleRollbackData = new RollbackData()
\r 
 146                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")
\r 
 147                         logDebug("numOfModulesToDelete: " + numOfModulesToDelete, isDebugLogEnabled)
\r 
 148                         def moduleMap = null
\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 
 156                                 moduleMap = rollbackData.get("VFMODULE_BASE")
\r 
 157                                 logDebug("Removing BASE VF module", isDebugLogEnabled)
\r 
 159                         moduleMap.each{ k, v -> vfModuleRollbackData.put("VFMODULE", "${k}","${v}") }
\r 
 160                         execution.setVariable("DCVAMR_RollbackData", vfModuleRollbackData)                                                              
\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 
 166                 logDebug("======== COMPLETED preProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)
\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 
 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 
 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 
 189                 logDebug("======== COMPLETED postProcessCreateVfModuleRollback ======== ", isDebugLogEnabled)
\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 
 201                         //Build SDNC Request
\r 
 203                         String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
\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 
 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 
 214                 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
\r 
 217         public void preProcessSDNCUnassignRequest(Execution execution) {
\r 
 218                 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
\r 
 219                         'execution=' + execution.getId() +
\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 
 226                         String vnfId = execution.getVariable("vnfId")
\r 
 227                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
\r 
 229                         String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
\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 
 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 
 239                 logDebug("======== COMPLETED  preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
\r 
 242         public String buildSDNCRequest(Execution execution, String svcInstId, String action){
\r 
 244                                 String uuid = execution.getVariable('testReqId') // for junits
\r 
 246                                         uuid = execution.getVariable("msoRequestId") + "-" +    System.currentTimeMillis()
\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 
 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 
 276                 </request-information>
\r 
 277                 <service-information>
\r 
 279                         <subscription-service-type/>                    
\r 
 280                         <service-instance-id>${serviceInstanceId}</service-instance-id>
\r 
 281                         <global-customer-id/>
\r 
 282                 </service-information>
\r 
 284                         <vnf-id>${vnfId}</vnf-id>
\r 
 287                 <vnf-request-input>                     
\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 
 295                         utils.logAudit("sdncRequest:  " + sdncRequest)
\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 
 304                 WorkflowException workflowException = execution.getVariable("WorkflowException")
\r 
 305                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
\r 
 307                 utils.logAudit("workflowException: " + workflowException)
\r 
 309                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
\r 
 310                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
\r 
 312                 utils.logAudit("SDNCResponse: " + response)
\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 
 318                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
\r 
 319                         throw new BpmnError("MSOWorkflowException")
\r 
 321                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
\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 
 330                         // Set rolledBack to true, rollbackError to null
\r 
 331                         execution.setVariable("rolledBack", true)
\r 
 332                         execution.setVariable("rollbackError", null)
\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 
 338                 logDebug("======== COMPLETED setSuccessfulRollbackStatus ======== ", isDebugLogEnabled)
\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 
 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 
 353                         execution.setVariable("rollbackData", null)
\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 
 359                 logDebug("======== COMPLETED setFailedRollbackStatus ======== ", isDebugLogEnabled)
\r