Check dependency between NBI operations

Change-Id: I77e1e5c78cba1deb3f8fe8ba5b4c2fed4ff36cff
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_nbi/acm_topic.py b/osm_nbi/acm_topic.py
index 8d529aa..eb335da 100644
--- a/osm_nbi/acm_topic.py
+++ b/osm_nbi/acm_topic.py
@@ -26,7 +26,8 @@
 from osm_nbi.base_topic import BaseTopic, EngineException
 from osm_nbi.validation import ValidationError
 
-# import logging
+import logging
+
 # import random
 # import string
 # from yaml import safe_load, YAMLError
@@ -35,6 +36,11 @@
 class ACMOperationTopic:
     def __init__(self, db, fs, msg, auth):
         self.multiproject = None  # Declare the attribute here
+        self.db = db
+        self.fs = fs
+        self.msg = msg
+        self.logger = logging.getLogger("nbi.base")
+        self.auth = auth
 
     @staticmethod
     def format_on_operation(content, operation_type, operation_params=None):
@@ -57,6 +63,48 @@
         content["operationHistory"].append(operation)
         return op_id
 
+    def check_dependency(self, check, operation_type=None):
+        topic_to_db_mapping = {
+            "cluster": "clusters",
+            "ksu": "ksus",
+            "infra_controller_profiles": "k8sinfra_controller",
+            "infra_config_profiles": "k8sinfra_config",
+            "resource_profiles": "k8sresource",
+            "app_profiles": "k8sapp",
+            "oka": "okas",
+        }
+        for topic, _id in check.items():
+            filter_q = {
+                "_id": _id,
+            }
+            if topic == "okas":
+                for oka_element in check[topic]:
+                    self.check_dependency({"oka": oka_element})
+            if topic not in ("okas"):
+                element_content = self.db.get_one(topic_to_db_mapping[topic], filter_q)
+                element_name = element_content.get("name")
+                state = element_content["state"]
+                if (
+                    operation_type == "delete"
+                    and state == "FAILED_CREATION"
+                    and element_content["operatingState"] == "IDLE"
+                ):
+                    self.logger.info(f"Delete operation is allowed in {state} state")
+                    return
+                elif element_content["state"] != "CREATED":
+                    raise EngineException(
+                        f"State of the {element_name} {topic} is {state}",
+                        HTTPStatus.UNPROCESSABLE_ENTITY,
+                    )
+                elif (
+                    state == "CREATED"
+                    and element_content["operatingState"] == "PROCESSING"
+                ):
+                    raise EngineException(
+                        f"operatingState of the {element_name} {topic} is not IDLE",
+                        HTTPStatus.UNPROCESSABLE_ENTITY,
+                    )
+
 
 class ACMTopic(BaseTopic, ACMOperationTopic):
     def __init__(self, db, fs, msg, auth):