NBI ACM code refactor

Change-Id: I1957f32b3120903ac978e353c05408fc00b3fe66
Signed-off-by: shrinithi <shrinithi.r@tataelxsi.co.in>
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_nbi/base_topic.py b/osm_nbi/base_topic.py
index 3dbaa3f..b66e6bd 100644
--- a/osm_nbi/base_topic.py
+++ b/osm_nbi/base_topic.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from pyrage import x25519
+
 import logging
 import random
 import string
@@ -21,8 +21,6 @@
 from http import HTTPStatus
 from time import time
 from osm_common.dbbase import deep_update_rfc7396, DbException
-from osm_common.msgbase import MsgException
-from osm_common.fsbase import FsException
 from osm_nbi.validation import validate_input, ValidationError, is_valid_uuid
 from yaml import safe_load, YAMLError
 
@@ -411,27 +409,6 @@
         return None
 
     @staticmethod
-    def format_on_operation(content, operation_type, operation_params=None):
-        op_id = str(uuid4())
-        now = time()
-        if "operationHistory" not in content:
-            content["operationHistory"] = []
-
-        operation = {}
-        operation["operationType"] = operation_type
-        operation["op_id"] = op_id
-        operation["result"] = None
-        operation["creationDate"] = now
-        operation["endDate"] = None
-        operation["workflowState"] = operation["resourceState"] = operation[
-            "operationState"
-        ] = operation["gitOperationInfo"] = None
-        operation["operationParams"] = operation_params
-
-        content["operationHistory"].append(operation)
-        return op_id
-
-    @staticmethod
     def format_on_edit(final_content, edit_content):
         """
         Modifies final_content to admin information upon edition
@@ -643,137 +620,6 @@
             HTTPStatus.INTERNAL_SERVER_ERROR,
         )
 
-    def create_gitname(self, content, session, _id=None):
-        if not self.multiproject:
-            _filter = {}
-        else:
-            _filter = self._get_project_filter(session)
-        _filter["git_name"] = content["name"]
-        if _id:
-            _filter["_id.neq"] = _id
-        if self.db.get_one(
-            self.topic, _filter, fail_on_empty=False, fail_on_more=False
-        ):
-            n = 5
-            # using random.choices()
-            # generating random strings
-            res = "".join(random.choices(string.ascii_lowercase + string.digits, k=n))
-            res1 = content["name"]
-            new_name1 = res1 + res
-            new_name = new_name1.lower()
-            return new_name
-        else:
-            return content["name"]
-
-    def new_profile(self, rollback, session, indata=None, kwargs=None, headers=None):
-        step = "name unique check"
-        try:
-            self.check_unique_name(session, indata["name"])
-
-            step = "validating input parameters"
-            profile_request = self._remove_envelop(indata)
-            self._update_input_with_kwargs(profile_request, kwargs)
-            profile_request = self._validate_input_new(
-                profile_request, session["force"]
-            )
-            operation_params = profile_request
-
-            step = "filling profile details from input data"
-            profile_create = self._create_profile(profile_request, session)
-
-            step = "creating profile at database"
-            self.format_on_new(
-                profile_create, session["project_id"], make_public=session["public"]
-            )
-            profile_create["current_operation"] = None
-            op_id = self.format_on_operation(
-                profile_create,
-                "create",
-                operation_params,
-            )
-
-            _id = self.db.create(self.topic, profile_create)
-            pubkey, privkey = self._generate_age_key()
-            profile_create["age_pubkey"] = self.db.encrypt(
-                pubkey, schema_version="1.11", salt=_id
-            )
-            profile_create["age_privkey"] = self.db.encrypt(
-                privkey, schema_version="1.11", salt=_id
-            )
-            rollback.append({"topic": self.topic, "_id": _id})
-            self.db.set_one(self.topic, {"_id": _id}, profile_create)
-            if op_id:
-                profile_create["op_id"] = op_id
-            self._send_msg("profile_create", {"profile_id": _id, "operation_id": op_id})
-
-            return _id, None
-        except (
-            ValidationError,
-            EngineException,
-            DbException,
-            MsgException,
-            FsException,
-        ) as e:
-            raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code)
-
-    def _create_profile(self, profile_request, session):
-        profile_desc = {
-            "name": profile_request["name"],
-            "description": profile_request["description"],
-            "default": False,
-            "git_name": self.create_gitname(profile_request, session),
-            "state": "IN_CREATION",
-            "operatingState": "IN_PROGRESS",
-            "resourceState": "IN_PROGRESS.REQUEST_RECEIVED",
-        }
-        return profile_desc
-
-    def default_profile(
-        self, rollback, session, indata=None, kwargs=None, headers=None
-    ):
-        step = "validating input parameters"
-        try:
-            profile_request = self._remove_envelop(indata)
-            self._update_input_with_kwargs(profile_request, kwargs)
-            operation_params = profile_request
-
-            step = "filling profile details from input data"
-            profile_create = self._create_default_profile(profile_request, session)
-
-            step = "creating profile at database"
-            self.format_on_new(
-                profile_create, session["project_id"], make_public=session["public"]
-            )
-            profile_create["current_operation"] = None
-            self.format_on_operation(
-                profile_create,
-                "create",
-                operation_params,
-            )
-            _id = self.db.create(self.topic, profile_create)
-            rollback.append({"topic": self.topic, "_id": _id})
-            return _id
-        except (
-            ValidationError,
-            EngineException,
-            DbException,
-            MsgException,
-            FsException,
-        ) as e:
-            raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code)
-
-    def _create_default_profile(self, profile_request, session):
-        profile_desc = {
-            "name": profile_request["name"],
-            "description": f"{self.topic} profile for cluster {profile_request['name']}",
-            "default": True,
-            "git_name": self.create_gitname(profile_request, session),
-            "state": "IN_CREATION",
-            "operatingState": "IN_PROGRESS",
-            "resourceState": "IN_PROGRESS.REQUEST_RECEIVED",
-        }
-        return profile_desc
-
     def delete_list(self, session, filter_q=None):
         """
         Delete a several entries of a topic. This is for internal usage and test only, not exposed to NBI API
@@ -801,6 +647,12 @@
         """
         pass
 
+    def delete_extra_before(self, session, _id, db_content, not_send_msg=None):
+        """
+        Delete other things apart from database entry of a item _id.
+        """
+        return {}
+
     def delete(self, session, _id, dry_run=False, not_send_msg=None):
         """
         Delete item by its internal _id
@@ -820,26 +672,6 @@
         item_content = self.db.get_one(self.topic, filter_q)
         nsd_id = item_content.get("_id")
 
-        if (
-            self.topic == "k8sinfra_controller"
-            or self.topic == "k8sinfra_config"
-            or self.topic == "k8sapp"
-            or self.topic == "k8sresource"
-            or self.topic == "clusters"
-        ):
-            if "state" in item_content:
-                item_content["state"] = "IN_DELETION"
-                item_content["operatingState"] = "PROCESSING"
-                self.db.set_one(self.topic, {"_id": _id}, item_content)
-
-            item_content_1 = self.db.get_one(self.topic, filter_q)
-            item_content_1["current_operation"] = None
-            op_id = self.format_on_operation(
-                item_content_1,
-                "delete",
-                None,
-            )
-
         self.check_conflict_on_del(session, _id, item_content)
 
         # While deteling ns descriptor associated ns config template should also get deleted.
@@ -897,30 +729,25 @@
                         http_code=HTTPStatus.UNAUTHORIZED,
                     )
         # delete
-        if (
-            self.topic == "k8sinfra_controller"
-            or self.topic == "k8sinfra_config"
-            or self.topic == "k8sapp"
-            or self.topic == "k8sresource"
-        ):
-            self.db.set_one(self.topic, {"_id": _id}, item_content_1)
-            self._send_msg(
-                "delete",
-                {"profile_id": _id, "operation_id": op_id},
-                not_send_msg=not_send_msg,
-            )
-        elif self.topic == "clusters":
-            self.db.set_one("clusters", {"_id": _id}, item_content_1)
-            self._send_msg(
-                "delete",
-                {"cluster_id": _id, "operation_id": op_id},
-                not_send_msg=not_send_msg,
-            )
+        different_message = self.delete_extra_before(
+            session, _id, item_content, not_send_msg=not_send_msg
+        )
+        # self.db.del_one(self.topic, filter_q)
+        # self.delete_extra(session, _id, item_content, not_send_msg=not_send_msg)
+        if different_message:
+            self.delete_extra(session, _id, item_content, not_send_msg=not_send_msg)
+            self._send_msg("delete", different_message, not_send_msg=not_send_msg)
         else:
             self.db.del_one(self.topic, filter_q)
             self.delete_extra(session, _id, item_content, not_send_msg=not_send_msg)
             self._send_msg("deleted", {"_id": _id}, not_send_msg=not_send_msg)
-        return _id
+        return None
+
+    def edit_extra_before(self, session, _id, indata=None, kwargs=None, content=None):
+        """
+        edit other things apart from database entry of a item _id.
+        """
+        return {}
 
     def edit(self, session, _id, indata=None, kwargs=None, content=None):
         """
@@ -938,25 +765,6 @@
         if kwargs:
             self._update_input_with_kwargs(indata, kwargs)
         try:
-            if (
-                self.topic == "k8sinfra_controller"
-                or self.topic == "k8sinfra_config"
-                or self.topic == "k8sapp"
-                or self.topic == "k8sresource"
-                or self.topic == "clusters"
-            ):
-                check = self.db.get_one(self.topic, {"_id": _id})
-                if self.topic != "clusters":
-                    if check["default"] is True:
-                        raise EngineException(
-                            "Cannot edit default profiles",
-                            HTTPStatus.UNPROCESSABLE_ENTITY,
-                        )
-                if "name" in indata:
-                    if check["name"] == indata["name"]:
-                        pass
-                    else:
-                        self.check_unique_name(session, indata["name"])
             if indata and session.get("set_project"):
                 raise EngineException(
                     "Cannot edit content and set to project (query string SET_PROJECT) at same time",
@@ -974,104 +782,48 @@
             content = self.check_conflict_on_edit(session, content, indata, _id=_id)
             op_id = self.format_on_edit(content, indata)
 
+            self.logger.info(f"indata is : {indata}")
+
+            different_message = self.edit_extra_before(
+                session, _id, indata, kwargs=None, content=None
+            )
+            self.logger.info(f"different msg is : {different_message}")
+
             self.db.replace(self.topic, _id, content)
 
             indata.pop("_admin", None)
             if op_id:
                 indata["op_id"] = op_id
             indata["_id"] = _id
-            if (
-                self.topic == "k8sinfra_controller"
-                or self.topic == "k8sinfra_config"
-                or self.topic == "k8sapp"
-                or self.topic == "k8sresource"
-                or self.topic == "clusters"
-            ):
+
+            if different_message:
+                self.logger.info("It is getting into if")
                 pass
             else:
+                self.logger.info("It is getting into else")
                 self._send_msg("edited", indata)
             return op_id
         except ValidationError as e:
             raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
 
-    def detach(self, session, _id, profile_type):
-        # To detach the profiles from every cluster
-        filter_q = {}
-        existing_clusters = self.db.get_list("clusters", filter_q)
-        existing_clusters_profiles = [
-            profile["_id"]
-            for profile in existing_clusters
-            if profile.get("profile_type", _id)
-        ]
-        update_dict = None
-        for profile in existing_clusters_profiles:
-            filter_q = {"_id": profile}
-            data = self.db.get_one("clusters", filter_q)
-            if profile_type in data:
-                profile_ids = data[profile_type]
-                if _id in profile_ids:
-                    profile_ids.remove(_id)
-                    update_dict = {profile_type: profile_ids}
-                    self.db.set_one("clusters", filter_q, update_dict)
-
-    def _generate_age_key(self):
-        ident = x25519.Identity.generate()
-        # gets the public key
-        pubkey = str(ident.to_public())
-        # gets the private key
-        privkey = str(ident)
-        # return both public and private key
-        return pubkey, privkey
-
-    def add_to_old_collection(self, content, session):
-        self.logger.info(f"content is : {content}")
-        item = {}
-        item["name"] = content["name"]
-        item["credentials"] = {}
-        # item["k8s_version"] = content["k8s_version"]
-        if "k8s_version" in content:
-            item["k8s_version"] = content["k8s_version"]
+    def create_gitname(self, content, session, _id=None):
+        if not self.multiproject:
+            _filter = {}
         else:
-            item["k8s_version"] = None
-        vim_account_details = self.db.get_one(
-            "vim_accounts", {"name": content["vim_account"]}
-        )
-        item["vim_account"] = vim_account_details["_id"]
-        item["nets"] = {"k8s_net1": None}
-        item["deployment_methods"] = {"juju-bundle": False, "helm-chart-v3": True}
-        # item["description"] = content["description"]
-        if "description" in content:
-            item["description"] = content["description"]
+            _filter = self._get_project_filter(session)
+        _filter["git_name"] = content["name"]
+        if _id:
+            _filter["_id.neq"] = _id
+        if self.db.get_one(
+            self.topic, _filter, fail_on_empty=False, fail_on_more=False
+        ):
+            n = 5
+            # using random.choices()
+            # generating random strings
+            res = "".join(random.choices(string.ascii_lowercase + string.digits, k=n))
+            res1 = content["name"]
+            new_name1 = res1 + res
+            new_name = new_name1.lower()
+            return new_name
         else:
-            item["description"] = None
-        item["namespace"] = "kube-system"
-        item["osm_acm"] = True
-        item["schema_version"] = "1.11"
-        self.logger.info(f"item is : {item}")
-        self.format_on_new(item, session["project_id"], make_public=session["public"])
-        _id = self.db.create("k8sclusters", item)
-        self.logger.info(f"_id is : {_id}")
-
-        item_1 = self.db.get_one("k8sclusters", {"name": item["name"]})
-
-        item_1["_admin"]["operationalState"] = "PROCESSING"
-        self.logger.info(f"content is : {item_1}")
-
-        # Create operation data
-        now = time()
-        operation_data = {
-            "lcmOperationType": "create",  # Assuming 'create' operation here
-            "operationState": "PROCESSING",
-            "startTime": now,
-            "statusEnteredTime": now,
-            "detailed-status": "",
-            "operationParams": None,  # Add parameters as needed
-        }
-
-        # create operation
-        item_1["_admin"]["operations"] = [operation_data]
-        item_1["_admin"]["current_operation"] = None
-        self.logger.info(f"content is : {item_1}")
-        self.db.set_one("k8sclusters", {"_id": item_1["_id"]}, item_1)
-
-        return
+            return content["name"]