Merge "Adding Delete AS"
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java
new file mode 100644
index 0000000..50ffa47
--- /dev/null
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java
@@ -0,0 +1,139 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
+
+import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_INSTANCE_ID_PARAM_NAME;
+import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
+import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.FINISHED;
+import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
+import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.STARTED;
+import java.util.Optional;
+import org.camunda.bpm.engine.delegate.DelegateExecution;
+import org.onap.so.cnfm.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
+import org.onap.so.cnfm.lcm.database.beans.AsInst;
+import org.onap.so.cnfm.lcm.database.beans.State;
+import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
+import org.onap.so.cnfm.lcm.model.ErrorDetails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ *
+ * @author Waqas Ikram (waqas.ikram@est.tech)
+ *
+ */
+@Component
+public class DeleteAsTask extends AbstractServiceTask {
+    private static final Logger logger = LoggerFactory.getLogger(DeleteAsTask.class);
+
+    private static final String AS_INSTANCE_EXISTS_PARAM_NAME = "asInstanceExists";
+    private static final String AS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME = "isInNotInstantiatedState";
+
+    private final AaiServiceProvider aaiServiceProvider;
+
+    protected DeleteAsTask(final DatabaseServiceProvider databaseServiceProvider,
+            final AaiServiceProvider aaiServiceProvider) {
+        super(databaseServiceProvider);
+        this.aaiServiceProvider = aaiServiceProvider;
+    }
+
+    public void setJobStatusToStarted(final DelegateExecution execution) {
+        setJobStatus(execution, STARTED, "Delete AS workflow process started");
+    }
+
+    public void setJobStatusToFinished(final DelegateExecution execution) {
+        setJobStatus(execution, FINISHED, "Delete AS workflow process finished");
+    }
+
+    public void setJobStatusInProgress(final DelegateExecution execution, final String message) {
+        setJobStatus(execution, IN_PROGRESS, message);
+    }
+
+    public void setJobStatusToError(final DelegateExecution execution) {
+        setJobStatusToError(execution, "Delete AS workflow process failed");
+    }
+
+    public void checkIfAsInstanceExistsInDb(final DelegateExecution execution) {
+        logger.info("Executing checkIfAsInstanceExistsInDb  ...");
+        setJobStatusInProgress(execution, "Checking that AS Instance Exists in DB");
+
+        final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
+        final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstId);
+        final boolean asInstanceExists = optionalAsInst.isPresent();
+        logger.info("AS Instance entry with id: {} {} exist in database", asInstId,
+                asInstanceExists ? "does" : "doesn't");
+        execution.setVariable(AS_INSTANCE_EXISTS_PARAM_NAME, asInstanceExists);
+
+        if (!asInstanceExists) {
+            final String message =
+                    "AS Instance with id: " + asInstId + " does not exist in database, so will not be deleted.";
+            logger.info(message);
+            execution.setVariable(AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new ErrorDetails().detail(message));
+        }
+
+        logger.info("Finished executing checkIfAsInstanceExistsInDb ...");
+    }
+
+    public void checkifAsInstanceInDbIsInNotInstantiatedState(final DelegateExecution execution) {
+        logger.info("Executing checkifAsInstanceInDbIsInNotInstantiatedState ...");
+        setJobStatusInProgress(execution, "Checking if AS Instance in database is in NOT_INSTANTIATED state");
+
+        final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
+        final AsInst asInst = getAsInst(execution, asInstId);
+        final State state = asInst.getStatus();
+        final boolean isInNotInstantiatedState = State.NOT_INSTANTIATED.equals(state);
+        logger.info("As Instance entry with asInstId: {} is in state: {}", asInstId, state);
+        execution.setVariable(AS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME, isInNotInstantiatedState);
+
+        if (!isInNotInstantiatedState) {
+            final String message = "Cannot Delete AS Instance with id: " + asInstId + " in the state: " + state;
+            logger.info(message);
+            execution.setVariable(AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new ErrorDetails().detail(message));
+        }
+
+        logger.info("Finished executing checkifAsInstanceInDbIsInNotInstantiatedState ...");
+    }
+
+    public void deleteGenericVnfFromAai(final DelegateExecution execution) {
+        logger.info("Executing deleteGenericVnfFromAai ...");
+        try {
+            final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
+            aaiServiceProvider.deleteGenericVnf(asInstId);
+        } catch (final Exception exception) {
+            final String message = "Unable to Delete GenericVnf from AAI ";
+            logger.error(message, exception);
+            abortOperation(execution, message);
+        }
+
+
+        logger.info("Finished executing deleteGenericVnfFromAai ...");
+    }
+
+    public void deleteAsInstanceFromDb(final DelegateExecution execution) {
+        logger.info("Executing deleteAsInstanceFromDb ...");
+        setJobStatusInProgress(execution, "Deleting AS Instance from Db");
+
+        final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
+        databaseServiceProvider.deleteAsInst(asInstId);
+
+        logger.info("Finished executing deleteAsInstanceFromDb ...");
+    }
+}
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java
index 789f1be..9fa3100 100644
--- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java
@@ -161,7 +161,7 @@
 
                     if (!Files.exists(path.getParent())) {
                         final File parentDir = path.getParent().toFile();
-                        logger.debug("Creating sub directories to download helm chart file {}", parentDir.toString());
+                        logger.debug("Creating sub directories to download helm chart file {}", parentDir);
                         parentDir.mkdirs();
                     }
 
@@ -381,10 +381,9 @@
         final List<AsDeploymentItem> asDeploymentItems =
                 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
         if (asDeploymentItems != null) {
-            asDeploymentItems.stream().forEach(asDeploymentItem -> {
-                logger.info("Current status {} of asDeploymentItem: {}", asDeploymentItem.getStatus(),
-                        asDeploymentItem.getName());
-            });
+            asDeploymentItems.stream()
+                    .forEach(asDeploymentItem -> logger.info("Current status {} of asDeploymentItem: {}",
+                            asDeploymentItem.getStatus(), asDeploymentItem.getName()));
         }
     }
 
@@ -396,7 +395,7 @@
         final Path dirPath = Paths.get(parentDir, dirname);
         final File dir = dirPath.toFile();
         if (!dir.exists()) {
-            logger.debug("Creating directory to download helm chart file {}", dir.toString());
+            logger.debug("Creating directory to download helm chart file {}", dir);
             dir.mkdir();
         }
         return dir;
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java
index a2cf976..4017946 100644
--- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java
@@ -65,7 +65,6 @@
  * @author Waqas Ikram (waqas.ikram@est.tech)
  *
  */
-
 @Component
 public class InstantiateDeploymentItemTask extends AbstractServiceTask {
 
@@ -200,9 +199,7 @@
             execution.setVariable(KUBE_KINDS_PARAM_NAME, kubeKinds);
 
             final Map<String, Boolean> result = new HashMap<>();
-            kubeKinds.forEach(kind -> {
-                result.put(kind, false);
-            });
+            kubeKinds.forEach(kind -> result.put(kind, false));
 
             execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, result);
         } catch (final BpmnError bpmnError) {
@@ -303,7 +300,7 @@
                             resources.addAll(kubernetesClient.getStatefulSetResources(apiClient, labelSelector));
                             break;
                         default:
-                            logger.warn("Unknown resource type {} setting {} skipping it", kind);
+                            logger.warn("Unknown resource type {} found skipping it ...", kind);
                             break;
                     }
                 } catch (final Exception exception) {
@@ -373,7 +370,6 @@
             }
         });
 
-
         logger.info("Finished executing createK8sResourcesInAai  ...");
 
     }
@@ -386,20 +382,17 @@
                 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
 
         if (kubeKindResult != null) {
-            kubeKindResult.entrySet().forEach(entry -> {
-                logger.info("Current status {} of resource type: {}", entry.getValue(), entry.getKey());
-            });
+            kubeKindResult.entrySet().forEach(
+                    entry -> logger.info("Current status {} of resource type: {}", entry.getValue(), entry.getKey()));
         }
 
-
         final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
         final List<AsDeploymentItem> asDeploymentItems =
                 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
         if (asDeploymentItems != null) {
-            asDeploymentItems.stream().forEach(asDeploymentItem -> {
-                logger.info("Current status {} of asDeploymentItem: {}", asDeploymentItem.getStatus(),
-                        asDeploymentItem.getName());
-            });
+            asDeploymentItems.stream()
+                    .forEach(asDeploymentItem -> logger.info("Current status {} of asDeploymentItem: {}",
+                            asDeploymentItem.getStatus(), asDeploymentItem.getName()));
         }
     }
 
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java
index d834c4d..4ffcb2d 100644
--- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java
@@ -59,7 +59,7 @@
 
     @Autowired
     protected TerminateAsTask(final DatabaseServiceProvider databaseServiceProvider,
-                              final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) {
+            final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) {
         super(databaseServiceProvider);
         this.kubConfigProvider = kubConfigProvider;
         this.aaiServiceProvider = aaiServiceProvider;
@@ -97,10 +97,9 @@
         final List<AsDeploymentItem> asDeploymentItems =
                 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
         if (asDeploymentItems != null) {
-            asDeploymentItems.stream().forEach(asDeploymentItem -> {
-                logger.info("Current status {} of terminating asDeploymentItem: {}", asDeploymentItem.getStatus(),
-                        asDeploymentItem.getName());
-            });
+            asDeploymentItems.stream()
+                    .forEach(asDeploymentItem -> logger.info("Current status {} of terminating asDeploymentItem: {}",
+                            asDeploymentItem.getStatus(), asDeploymentItem.getName()));
         }
     }
 
@@ -196,8 +195,7 @@
 
         logger.debug("Executing updateGenericVnfStatustoDeActivated");
         final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
-        final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId,
-                OrchestrationStatusEnum.DEACTIVATED);
+        final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId, OrchestrationStatusEnum.DEACTIVATED);
         if (!result) {
             abortOperation(execution, "Failed to update GenericVnf status to Deactivated as there"
                     + "is no GenericVnf Found in AAI of ID: " + asInstId);
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn
new file mode 100644
index 0000000..4352eba
--- /dev/null
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn
@@ -0,0 +1,251 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1tc9uti" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.0.0">
+  <bpmn:process id="DeleteAs" name="DeleteAs" isExecutable="true">
+    <bpmn:startEvent id="StartEvent_1">
+      <bpmn:outgoing>Flow_09vhzc2</bpmn:outgoing>
+    </bpmn:startEvent>
+    <bpmn:endEvent id="Event_0nug1lm">
+      <bpmn:incoming>Flow_0ld9hr8</bpmn:incoming>
+    </bpmn:endEvent>
+    <bpmn:sequenceFlow id="Flow_09vhzc2" sourceRef="StartEvent_1" targetRef="Activity_0taaax6" />
+    <bpmn:serviceTask id="Activity_0taaax6" name="Set Job Status to STARTED" camunda:asyncBefore="true" camunda:expression="${DeleteAsTask.setJobStatusToStarted(execution)}">
+      <bpmn:incoming>Flow_09vhzc2</bpmn:incoming>
+      <bpmn:outgoing>Flow_1687i7q</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:sequenceFlow id="Flow_1687i7q" sourceRef="Activity_0taaax6" targetRef="Activity_1jfsdb2" />
+    <bpmn:serviceTask id="Activity_1jfsdb2" name="Check AS Instance exists In DB " camunda:expression="${DeleteAsTask.checkIfAsInstanceExistsInDb(execution)}">
+      <bpmn:incoming>Flow_1687i7q</bpmn:incoming>
+      <bpmn:outgoing>Flow_0qqc31o</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:serviceTask id="Activity_1g62uqk" name="Set Job Status to FINISHED" camunda:expression="${DeleteAsTask.setJobStatusToFinished(execution)}">
+      <bpmn:incoming>Flow_172kwvj</bpmn:incoming>
+      <bpmn:outgoing>Flow_0ld9hr8</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:sequenceFlow id="Flow_0ld9hr8" sourceRef="Activity_1g62uqk" targetRef="Event_0nug1lm" />
+    <bpmn:subProcess id="Activity_03dmnkf" name="Error Handling" triggeredByEvent="true">
+      <bpmn:startEvent id="Event_02rz7as" name="error">
+        <bpmn:outgoing>Flow_0ifwuxp</bpmn:outgoing>
+        <bpmn:errorEventDefinition id="ErrorEventDefinition_1r3pem1" />
+      </bpmn:startEvent>
+      <bpmn:endEvent id="Event_1gc07h6" name="end">
+        <bpmn:incoming>Flow_1kk2y0h</bpmn:incoming>
+      </bpmn:endEvent>
+      <bpmn:serviceTask id="Activity_0uuvi1d" name="Set Job Status to ERROR" camunda:asyncBefore="true" camunda:expression="${DeleteAsTask.setJobStatusToError(execution)}">
+        <bpmn:incoming>Flow_0ifwuxp</bpmn:incoming>
+        <bpmn:outgoing>Flow_1kk2y0h</bpmn:outgoing>
+      </bpmn:serviceTask>
+      <bpmn:sequenceFlow id="Flow_1kk2y0h" sourceRef="Activity_0uuvi1d" targetRef="Event_1gc07h6" />
+      <bpmn:sequenceFlow id="Flow_0ifwuxp" sourceRef="Event_02rz7as" targetRef="Activity_0uuvi1d" />
+    </bpmn:subProcess>
+    <bpmn:subProcess id="Activity_10mnj1m" name="Java Exception Handling" triggeredByEvent="true">
+      <bpmn:startEvent id="Event_1m8nqlt" name="error">
+        <bpmn:outgoing>Flow_053knln</bpmn:outgoing>
+        <bpmn:errorEventDefinition id="ErrorEventDefinition_171hvzy" errorRef="Error_1rc8vx6" camunda:errorCodeVariable="BPMN_javaExpCode" camunda:errorMessageVariable="BPMN_javaExpMsg" />
+      </bpmn:startEvent>
+      <bpmn:endEvent id="Event_1v4252g">
+        <bpmn:incoming>Flow_1gu1dnf</bpmn:incoming>
+      </bpmn:endEvent>
+      <bpmn:serviceTask id="Activity_052yqhq" name="Set Job Status to ERROR" camunda:asyncBefore="true" camunda:expression="${DeleteAsTask.setJobStatusToError(execution)}">
+        <bpmn:incoming>Flow_053knln</bpmn:incoming>
+        <bpmn:outgoing>Flow_1gu1dnf</bpmn:outgoing>
+      </bpmn:serviceTask>
+      <bpmn:sequenceFlow id="Flow_1gu1dnf" sourceRef="Activity_052yqhq" targetRef="Event_1v4252g" />
+      <bpmn:sequenceFlow id="Flow_053knln" sourceRef="Event_1m8nqlt" targetRef="Activity_052yqhq" />
+    </bpmn:subProcess>
+    <bpmn:serviceTask id="Activity_0uxlyqj" name="Check if AS Instance state in DB is NOT_INSTANTIATED" camunda:expression="${DeleteAsTask.checkifAsInstanceInDbIsInNotInstantiatedState(execution)}">
+      <bpmn:incoming>Flow_0zhc29v</bpmn:incoming>
+      <bpmn:outgoing>Flow_0rnc1yr</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:exclusiveGateway id="Gateway_13t56cm" name="Does AS Instance Exist?">
+      <bpmn:incoming>Flow_0qqc31o</bpmn:incoming>
+      <bpmn:outgoing>Flow_0zhc29v</bpmn:outgoing>
+      <bpmn:outgoing>Flow_0tb94ui</bpmn:outgoing>
+    </bpmn:exclusiveGateway>
+    <bpmn:exclusiveGateway id="Gateway_0jvzfpt" name="Is AS Instance in NOT INSTANTIATED State?">
+      <bpmn:incoming>Flow_0rnc1yr</bpmn:incoming>
+      <bpmn:outgoing>Flow_0blv3cy</bpmn:outgoing>
+      <bpmn:outgoing>Flow_11ap3gp</bpmn:outgoing>
+    </bpmn:exclusiveGateway>
+    <bpmn:endEvent id="Event_1xhfq2h">
+      <bpmn:incoming>Flow_0tb94ui</bpmn:incoming>
+      <bpmn:incoming>Flow_0blv3cy</bpmn:incoming>
+      <bpmn:errorEventDefinition id="ErrorEventDefinition_1hclsfi" errorRef="Error_1rc8vx6" />
+    </bpmn:endEvent>
+    <bpmn:sequenceFlow id="Flow_0zhc29v" name="Yes" sourceRef="Gateway_13t56cm" targetRef="Activity_0uxlyqj">
+      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">#{asInstanceExists}</bpmn:conditionExpression>
+    </bpmn:sequenceFlow>
+    <bpmn:sequenceFlow id="Flow_0rnc1yr" sourceRef="Activity_0uxlyqj" targetRef="Gateway_0jvzfpt" />
+    <bpmn:sequenceFlow id="Flow_0tb94ui" name="No" sourceRef="Gateway_13t56cm" targetRef="Event_1xhfq2h">
+      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">#{not nsInstanceExists}</bpmn:conditionExpression>
+    </bpmn:sequenceFlow>
+    <bpmn:sequenceFlow id="Flow_0blv3cy" name="No" sourceRef="Gateway_0jvzfpt" targetRef="Event_1xhfq2h">
+      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">#{not isInNotInstantiatedState}</bpmn:conditionExpression>
+    </bpmn:sequenceFlow>
+    <bpmn:sequenceFlow id="Flow_0qqc31o" sourceRef="Activity_1jfsdb2" targetRef="Gateway_13t56cm" />
+    <bpmn:sequenceFlow id="Flow_11ap3gp" name="Yes" sourceRef="Gateway_0jvzfpt" targetRef="Activity_0mupxpq">
+      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">#{isInNotInstantiatedState}</bpmn:conditionExpression>
+    </bpmn:sequenceFlow>
+    <bpmn:serviceTask id="Activity_15dtekb" name="Delete AS Instance from DB" camunda:expression="${DeleteAsTask.deleteAsInstanceFromDb(execution)}">
+      <bpmn:incoming>Flow_1t4zqk5</bpmn:incoming>
+      <bpmn:outgoing>Flow_172kwvj</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:sequenceFlow id="Flow_172kwvj" sourceRef="Activity_15dtekb" targetRef="Activity_1g62uqk" />
+    <bpmn:serviceTask id="Activity_0mupxpq" name="Delete AS (GenericVnf) Instance from AAI" camunda:expression="${DeleteAsTask.deleteGenericVnfFromAai(execution)}">
+      <bpmn:incoming>Flow_11ap3gp</bpmn:incoming>
+      <bpmn:outgoing>Flow_1t4zqk5</bpmn:outgoing>
+    </bpmn:serviceTask>
+    <bpmn:sequenceFlow id="Flow_1t4zqk5" sourceRef="Activity_0mupxpq" targetRef="Activity_15dtekb" />
+  </bpmn:process>
+  <bpmn:error id="Error_0fg93db" name="AsWorkflowProcessingException" errorCode="DELETE_AS_WORKFLOW_PROCESSING_EXCEPTION" />
+  <bpmn:error id="Error_1rc8vx6" name="AsWorkflowProcessingException" errorCode="CREATE_AS_WORKFLOW_PROCESSING_EXCEPTION" />
+  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
+    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="DeleteAs">
+      <bpmndi:BPMNEdge id="Flow_172kwvj_di" bpmnElement="Flow_172kwvj">
+        <di:waypoint x="1220" y="167" />
+        <di:waypoint x="1280" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_11ap3gp_di" bpmnElement="Flow_11ap3gp">
+        <di:waypoint x="915" y="167" />
+        <di:waypoint x="970" y="167" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="930" y="140" width="19" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0qqc31o_di" bpmnElement="Flow_0qqc31o">
+        <di:waypoint x="530" y="167" />
+        <di:waypoint x="585" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0blv3cy_di" bpmnElement="Flow_0blv3cy">
+        <di:waypoint x="890" y="192" />
+        <di:waypoint x="890" y="270" />
+        <di:waypoint x="768" y="270" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="903" y="228" width="14" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0tb94ui_di" bpmnElement="Flow_0tb94ui">
+        <di:waypoint x="610" y="192" />
+        <di:waypoint x="610" y="270" />
+        <di:waypoint x="732" y="270" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="618" y="228" width="14" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0rnc1yr_di" bpmnElement="Flow_0rnc1yr">
+        <di:waypoint x="800" y="167" />
+        <di:waypoint x="865" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0zhc29v_di" bpmnElement="Flow_0zhc29v">
+        <di:waypoint x="635" y="167" />
+        <di:waypoint x="700" y="167" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="658" y="149" width="19" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_0ld9hr8_di" bpmnElement="Flow_0ld9hr8">
+        <di:waypoint x="1380" y="167" />
+        <di:waypoint x="1442" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_1687i7q_di" bpmnElement="Flow_1687i7q">
+        <di:waypoint x="370" y="167" />
+        <di:waypoint x="430" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_09vhzc2_di" bpmnElement="Flow_09vhzc2">
+        <di:waypoint x="215" y="167" />
+        <di:waypoint x="270" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_1t4zqk5_di" bpmnElement="Flow_1t4zqk5">
+        <di:waypoint x="1070" y="167" />
+        <di:waypoint x="1120" y="167" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
+        <dc:Bounds x="179" y="149" width="36" height="36" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_0taaax6_di" bpmnElement="Activity_0taaax6">
+        <dc:Bounds x="270" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_1jfsdb2_di" bpmnElement="Activity_1jfsdb2">
+        <dc:Bounds x="430" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Event_0nug1lm_di" bpmnElement="Event_0nug1lm">
+        <dc:Bounds x="1442" y="149" width="36" height="36" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_15dtekb_di" bpmnElement="Activity_15dtekb">
+        <dc:Bounds x="1120" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_1g62uqk_di" bpmnElement="Activity_1g62uqk">
+        <dc:Bounds x="1280" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_0mupxpq_di" bpmnElement="Activity_0mupxpq">
+        <dc:Bounds x="970" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_03dmnkf_di" bpmnElement="Activity_03dmnkf" isExpanded="true">
+        <dc:Bounds x="310" y="330" width="438" height="130" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNEdge id="Flow_0ifwuxp_di" bpmnElement="Flow_0ifwuxp">
+        <di:waypoint x="368" y="393" />
+        <di:waypoint x="460" y="393" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_1kk2y0h_di" bpmnElement="Flow_1kk2y0h">
+        <di:waypoint x="560" y="393" />
+        <di:waypoint x="692" y="393" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNShape id="Event_02rz7as_di" bpmnElement="Event_02rz7as">
+        <dc:Bounds x="332" y="375" width="36" height="36" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="338" y="418" width="24" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Event_1gc07h6_di" bpmnElement="Event_1gc07h6">
+        <dc:Bounds x="692" y="375" width="36" height="36" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="702" y="417" width="19" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_0uuvi1d_di" bpmnElement="Activity_0uuvi1d">
+        <dc:Bounds x="460" y="353" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_10mnj1m_di" bpmnElement="Activity_10mnj1m" isExpanded="true">
+        <dc:Bounds x="310" y="490" width="438" height="130" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNEdge id="Flow_053knln_di" bpmnElement="Flow_053knln">
+        <di:waypoint x="401" y="553" />
+        <di:waypoint x="459" y="553" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNEdge id="Flow_1gu1dnf_di" bpmnElement="Flow_1gu1dnf">
+        <di:waypoint x="559" y="553" />
+        <di:waypoint x="651" y="553" />
+      </bpmndi:BPMNEdge>
+      <bpmndi:BPMNShape id="Event_1m8nqlt_di" bpmnElement="Event_1m8nqlt">
+        <dc:Bounds x="365" y="535" width="36" height="36" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="372" y="578" width="24" height="14" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Event_1v4252g_di" bpmnElement="Event_1v4252g">
+        <dc:Bounds x="651" y="535" width="36" height="36" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_052yqhq_di" bpmnElement="Activity_052yqhq">
+        <dc:Bounds x="459" y="513" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Activity_0uxlyqj_di" bpmnElement="Activity_0uxlyqj">
+        <dc:Bounds x="700" y="127" width="100" height="80" />
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Gateway_13t56cm_di" bpmnElement="Gateway_13t56cm" isMarkerVisible="true">
+        <dc:Bounds x="585" y="142" width="50" height="50" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="566" y="106" width="88" height="27" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Gateway_0jvzfpt_di" bpmnElement="Gateway_0jvzfpt" isMarkerVisible="true">
+        <dc:Bounds x="865" y="142" width="50" height="50" />
+        <bpmndi:BPMNLabel>
+          <dc:Bounds x="851" y="83" width="79" height="53" />
+        </bpmndi:BPMNLabel>
+      </bpmndi:BPMNShape>
+      <bpmndi:BPMNShape id="Event_1xhfq2h_di" bpmnElement="Event_1xhfq2h">
+        <dc:Bounds x="732" y="252" width="36" height="36" />
+      </bpmndi:BPMNShape>
+    </bpmndi:BPMNPlane>
+  </bpmndi:BPMNDiagram>
+</bpmn:definitions>
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java
new file mode 100644
index 0000000..e90e64b
--- /dev/null
+++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java
@@ -0,0 +1,111 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.delete;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.ok;
+import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.onap.aaiclient.client.aai.AAIVersion.V19;
+import java.time.LocalDateTime;
+import java.util.Optional;
+import java.util.UUID;
+import org.camunda.bpm.engine.history.HistoricProcessInstance;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.so.cnfm.lcm.bpmn.flows.BaseTest;
+import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService;
+import org.onap.so.cnfm.lcm.database.beans.AsInst;
+import org.onap.so.cnfm.lcm.database.beans.Job;
+import org.onap.so.cnfm.lcm.database.beans.State;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ *
+ * @author Waqas Ikram (waqas.ikram@est.tech)
+ *
+ */
+public class DeleteAsTaskTest extends BaseTest {
+
+    @Autowired
+    private JobExecutorService objUnderTest;
+
+    @Before
+    public void before() {
+        wireMockServer.resetAll();
+    }
+
+    @After
+    public void after() {
+        wireMockServer.resetAll();
+    }
+
+    @Test
+    public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
+        final String asInstanceId = UUID.randomUUID().toString();
+        addDummyAsToDatabase(asInstanceId);
+        mockAaiEndpoints(asInstanceId);
+
+        objUnderTest.runDeleteAsJob(asInstanceId);
+
+        final Optional<Job> optional = getJobByResourceId(asInstanceId);
+        assertTrue(optional.isPresent());
+        final Job job = optional.get();
+
+        assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
+
+        final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
+        assertNotNull(historicProcessInstance);
+        assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
+
+        final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
+        assertTrue(optionalAsInst.isEmpty());
+
+    }
+
+    private void mockAaiEndpoints(final String asInstanceId) {
+        final String modelEndpoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + asInstanceId;
+        final String resourceVersion = UUID.randomUUID().toString();
+
+        final String body =
+                "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
+        wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
+        wireMockServer.stubFor(
+                delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));
+    }
+
+    private void addDummyAsToDatabase(final String asInstanceId) {
+        final String asdId = UUID.randomUUID().toString();
+
+        final AsInst asInst = new AsInst().asInstId(asInstanceId).name("asName").asdId(asdId)
+                .asdInvariantId(asInstanceId).status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now())
+                .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion")
+                .asProvider("asProvider").serviceInstanceId(SERVICE_INSTANCE_ID)
+                .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
+                .tenantId("tenantId");
+        databaseServiceProvider.saveAsInst(asInst);
+    }
+
+}