2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.openecomp.mso.bpmn.infrastructure.scripts
 
  24 import javax.xml.parsers.DocumentBuilder
 
  25 import javax.xml.parsers.DocumentBuilderFactory
 
  27 import org.apache.commons.lang3.*
 
  28 import org.camunda.bpm.engine.delegate.BpmnError
 
  29 import org.camunda.bpm.engine.runtime.Execution
 
  30 import org.w3c.dom.Document
 
  31 import org.w3c.dom.Element
 
  32 import org.w3c.dom.Node
 
  33 import org.w3c.dom.NodeList
 
  34 import org.xml.sax.InputSource
 
  37 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
 
  38 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
 
  39 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
 
  40 import org.openecomp.mso.bpmn.core.WorkflowException
 
  41 import org.openecomp.mso.bpmn.core.json.JsonUtils;
 
  45  * This class supports the DeleteVnfInfra Flow
 
  46  * with the Deletion of a generic vnf for
 
  49 class DeleteVnfInfra extends AbstractServiceTaskProcessor {
 
  51         String Prefix="DELVI_"
 
  52         ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  53         JsonUtils jsonUtil = new JsonUtils()
 
  54         VidUtils vidUtils = new VidUtils(this)
 
  57          * This method gets and validates the incoming
 
  62         public void preProcessRequest(Execution execution) {
 
  63                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
 
  64                 execution.setVariable("prefix",Prefix)
 
  65                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PreProcessRequest Process*** ", isDebugEnabled)
 
  67                 execution.setVariable("DELVI_SuccessIndicator", false)
 
  68                 execution.setVariable("DELVI_vnfInUse", false)
 
  72                         String deleteVnfRequest = execution.getVariable("bpmnRequest")
 
  73                         execution.setVariable("DELVI_DeleteVnfRequest", deleteVnfRequest)
 
  74                         utils.logAudit("Incoming DeleteVnfInfra Request is: \n" + deleteVnfRequest)
 
  76                         if(deleteVnfRequest != null){
 
  78                                 String requestId = execution.getVariable("mso-request-id")
 
  79                                 execution.setVariable("DELVI_requestId", requestId)
 
  81                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
 
  82                                 execution.setVariable("DELVI_serviceInstanceId", serviceInstanceId)
 
  83                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
 
  85                                 String vnfId = execution.getVariable("vnfId")
 
  86                                 execution.setVariable("DELVI_vnfId", vnfId)
 
  87                                 utils.log("DEBUG", "Incoming Vnf(Instance) Id is: " + vnfId, isDebugEnabled)
 
  89                                 String source = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.requestInfo.source")
 
  90                                 execution.setVariable("DELVI_source", source)
 
  91                                 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
 
  93                                 def cloudConfiguration = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.cloudConfiguration")
 
  94                                 execution.setVariable("DELVI_cloudConfiguration", cloudConfiguration)
 
  96                                 boolean cascadeDelete = false
 
  97                                 Boolean cascadeDeleteObj = jsonUtil.getJsonRawValue(deleteVnfRequest, "requestDetails.requestParameters.cascadeDelete")
 
  98                                 if(cascadeDeleteObj!=null){
 
  99                                         cascadeDelete = cascadeDeleteObj.booleanValue()
 
 101                                 execution.setVariable("DELVI_cascadeDelete", cascadeDelete)
 
 102                                 utils.log("DEBUG", "Incoming cascadeDelete is: " + cascadeDelete, isDebugEnabled)
 
 104                                 //For Completion Handler & Fallout Handler
 
 106                                 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
 
 107                                         <request-id>${requestId}</request-id>
 
 108                                         <action>DELETE</action>
 
 109                                         <source>${source}</source>
 
 112                                 execution.setVariable("DELVI_requestInfo", requestInfo)
 
 114                                 // Setting for sub flow calls
 
 115                                 execution.setVariable("DELVI_type", "generic-vnf")
 
 118                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
 
 122                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
 
 125                         utils.log("DEBUG", " Error Occured in DeleteVnfInfra PreProcessRequest method!" + e, isDebugEnabled)
 
 126                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PreProcessRequest")
 
 129                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PreProcessRequest Process ***", isDebugEnabled)
 
 132         public void sendSyncResponse (Execution execution) {
 
 133                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
 
 134                 execution.setVariable("prefix",Prefix)
 
 136                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra SendSyncResponse Process *** ", isDebugEnabled)
 
 139                         String requestId = execution.getVariable("DELVI_requestId")
 
 140                         String vnfId = execution.getVariable("DELVI_vnfId")
 
 142                         String DeleteVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
 
 144                         utils.log("DEBUG", " DeleteVnfInfra Sync Response is: \n"  + DeleteVnfResponse, isDebugEnabled)
 
 145                         execution.setVariable("DELVI_sentSyncResponse", true)
 
 147                         sendWorkflowResponse(execution, 202, DeleteVnfResponse)
 
 149                 } catch (Exception ex) {
 
 150                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra SendSyncResponse Process " + ex.getMessage(), isDebugEnabled)
 
 151                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra SendSyncResponse Process")
 
 154                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra SendSyncResponse Process ***", isDebugEnabled)
 
 157         public void prepareCompletionHandlerRequest(Execution execution){
 
 158                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
 
 159                 execution.setVariable("prefix",Prefix)
 
 161                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PrepareCompletionHandlerRequest Process *** ", isDebugEnabled)
 
 164                         String requestInfo = execution.getVariable("DELVI_requestInfo")
 
 165                         requestInfo = utils.removeXmlPreamble(requestInfo)
 
 166                         String vnfId = execution.getVariable("DELVI_vnfId")
 
 169                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
 
 170                                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">
 
 172                                                         <status-message>Vnf has been deleted successfully.</status-message>
 
 173                                                         <vnfId>${vnfId}</vnfId>
 
 174                                                         <mso-bpel-name>DeleteVnfInfra</mso-bpel-name>
 
 175                                                 </aetgt:MsoCompletionRequest>"""
 
 177                         execution.setVariable("DELVI_completionHandlerRequest", request)
 
 178                         utils.log("DEBUG", "Completion Handler Request is: " + request, isDebugEnabled)
 
 180                         execution.setVariable("WorkflowResponse", "Success") // for junits
 
 182                 } catch (Exception ex) {
 
 183                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage(), isDebugEnabled)
 
 184                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process")
 
 187                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PrepareCompletionHandlerRequest Process ***", isDebugEnabled)
 
 190         public void sendErrorResponse(Execution execution){
 
 191                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
 
 192                 execution.setVariable("prefix",Prefix)
 
 194                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra sendErrorResponse Process *** ", isDebugEnabled)
 
 196                         def sentSyncResponse = execution.getVariable("DELVI_sentSyncResponse")
 
 197                         if(sentSyncResponse == false){
 
 198                                 utils.log("DEBUG", "Sending a Sync Error Response", isDebugEnabled)
 
 199                                 WorkflowException wfex = execution.getVariable("WorkflowException")
 
 200                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
 
 202                                 utils.logAudit(response)
 
 203                                 sendWorkflowResponse(execution, 500, response)
 
 205                                 utils.log("DEBUG", "A Sync Response has already been sent. Skipping Send Sync Error Response.", isDebugEnabled)
 
 208                 } catch(Exception ex) {
 
 209                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
 
 210                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra sendErrorResponse Process")
 
 212                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra sendErrorResponse Process ***", isDebugEnabled)