Delete KSU from DB while deleting default and non-default profiles

Change-Id: Ia50335512cb5a39f881cea4c2f49af8acf40662c
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_lcm/k8s.py b/osm_lcm/k8s.py
index 3a0e2d0..58337ac 100644
--- a/osm_lcm/k8s.py
+++ b/osm_lcm/k8s.py
@@ -50,6 +50,13 @@
         "app_profiles": "k8sapp",
     }
 
+    profile_type_mapping = {
+        "infra-controllers": "infra_controller_profiles",
+        "infra-configs": "infra_config_profiles",
+        "managed-resources": "resource_profiles",
+        "applications": "app_profiles",
+    }
+
     def __init__(self, msg, lcm_tasks, config):
         self.logger = logging.getLogger("lcm.gitops")
         self.lcm_tasks = lcm_tasks
@@ -274,6 +281,13 @@
         )
         return content_copy
 
+    def delete_profile_ksu(self, _id, profile_type):
+        filter_q = {"profile": {"_id": _id, "profile_type": profile_type}}
+        ksu_list = self.db.get_list("ksus", filter_q)
+        if ksu_list:
+            self.db.del_list("ksus", filter_q)
+        return
+
     def cluster_kubectl(self, db_cluster):
         cluster_kubeconfig = db_cluster["credentials"]
         kubeconfig_path = f"/tmp/{db_cluster['_id']}_kubeconfig.yaml"
@@ -725,6 +739,7 @@
                         "the db_profile name is :{}".format(db_profile["name"])
                     )
                     if db_cluster["name"] == db_profile["name"]:
+                        self.delete_profile_ksu(profile_id, profile_type)
                         self.db.del_one(db_collection, {"_id": profile_id})
                     else:
                         profile_ids.remove(profile_id)
@@ -1520,6 +1535,8 @@
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(f"App Delete Exit with resource status: {resource_status}")
@@ -1598,6 +1615,8 @@
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(
@@ -1678,6 +1697,8 @@
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(
@@ -1758,6 +1779,8 @@
 
         if resource_status:
             content["state"] = "DELETED"
+            profile_type = self.profile_type_mapping[content["profile_type"]]
+            self.delete_profile_ksu(profile_id, profile_type)
             self.db.set_one(self.db_collection, {"_id": content["_id"]}, content)
             self.db.del_one(self.db_collection, {"_id": content["_id"]})
         self.logger.info(