Pin black version in tox.ini to 23.12.1
[osm/N2VC.git] / n2vc / n2vc_juju_conn.py
index fb36809..f28a9bd 100644 (file)
@@ -37,11 +37,12 @@ from n2vc.exceptions import (
 )
 from n2vc.n2vc_conn import N2VCConnector
 from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml
-from n2vc.libjuju import Libjuju
+from n2vc.libjuju import Libjuju, retry_callback
 from n2vc.store import MotorStore
 from n2vc.utils import get_ee_id_components, generate_random_alfanum_string
 from n2vc.vca.connection import get_connection
 from retrying_async import retry
+from typing import Tuple
 
 
 class N2VCJujuConnector(N2VCConnector):
@@ -60,7 +61,6 @@ class N2VCJujuConnector(N2VCConnector):
         db: object,
         fs: object,
         log: object = None,
-        loop: object = None,
         on_update_db=None,
     ):
         """
@@ -69,19 +69,11 @@ class N2VCJujuConnector(N2VCConnector):
         :param: db: Database object from osm_common
         :param: fs: Filesystem object from osm_common
         :param: log: Logger
-        :param: loop: Asyncio loop
         :param: on_update_db: Callback function to be called for updating the database.
         """
 
         # parent class constructor
-        N2VCConnector.__init__(
-            self,
-            db=db,
-            fs=fs,
-            log=log,
-            loop=loop,
-            on_update_db=on_update_db,
-        )
+        N2VCConnector.__init__(self, db=db, fs=fs, log=log, on_update_db=on_update_db)
 
         # silence websocket traffic log
         logging.getLogger("websockets.protocol").setLevel(logging.INFO)
@@ -92,7 +84,7 @@ class N2VCJujuConnector(N2VCConnector):
 
         db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri")
         self._store = MotorStore(db_uri)
-        self.loading_libjuju = asyncio.Lock(loop=self.loop)
+        self.loading_libjuju = asyncio.Lock()
         self.delete_namespace_locks = {}
         self.log.info("N2VC juju connector initialized")
 
@@ -226,10 +218,7 @@ class N2VCJujuConnector(N2VCConnector):
         # create or reuse a new juju machine
         try:
             if not await libjuju.model_exists(model_name):
-                await libjuju.add_model(
-                    model_name,
-                    libjuju.vca_connection.lxd_cloud,
-                )
+                await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud)
             machine, new = await libjuju.create_machine(
                 model_name=model_name,
                 machine_id=machine_id,
@@ -255,9 +244,7 @@ class N2VCJujuConnector(N2VCConnector):
             raise N2VCException(message=message)
 
         # new machine credentials
-        credentials = {
-            "hostname": machine.dns_name,
-        }
+        credentials = {"hostname": machine.dns_name}
 
         self.log.info(
             "Execution environment created. ee_id: {}, credentials: {}".format(
@@ -337,10 +324,7 @@ class N2VCJujuConnector(N2VCConnector):
         # register machine on juju
         try:
             if not await libjuju.model_exists(model_name):
-                await libjuju.add_model(
-                    model_name,
-                    libjuju.vca_connection.lxd_cloud,
-                )
+                await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud)
             machine_id = await libjuju.provision_machine(
                 model_name=model_name,
                 hostname=hostname,
@@ -371,7 +355,13 @@ class N2VCJujuConnector(N2VCConnector):
 
     # In case of native_charm is being deployed, if JujuApplicationExists error happens
     # it will try to add_unit
-    @retry(attempts=3, delay=5, retry_exceptions=(N2VCApplicationExists,), timeout=None)
+    @retry(
+        attempts=3,
+        delay=5,
+        retry_exceptions=(N2VCApplicationExists,),
+        timeout=None,
+        callback=retry_callback,
+    )
     async def install_configuration_sw(
         self,
         ee_id: str,
@@ -564,10 +554,7 @@ class N2VCJujuConnector(N2VCConnector):
         _, ns_id, _, _, _ = self._get_namespace_components(namespace=namespace)
         model_name = "{}-k8s".format(ns_id)
         if not await libjuju.model_exists(model_name):
-            await libjuju.add_model(
-                model_name,
-                libjuju.vca_connection.k8s_cloud,
-            )
+            await libjuju.add_model(model_name, libjuju.vca_connection.k8s_cloud)
         application_name = self._get_application_name(namespace)
 
         try:
@@ -586,9 +573,7 @@ class N2VCJujuConnector(N2VCConnector):
 
         self.log.info("K8s proxy charm installed")
         ee_id = N2VCJujuConnector._build_ee_id(
-            model_name=model_name,
-            application_name=application_name,
-            machine_id="k8s",
+            model_name=model_name, application_name=application_name, machine_id="k8s"
         )
 
         self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id)
@@ -717,9 +702,7 @@ class N2VCJujuConnector(N2VCConnector):
         return await libjuju.get_metrics(model_name, application_name)
 
     async def add_relation(
-        self,
-        provider: RelationEndpoint,
-        requirer: RelationEndpoint,
+        self, provider: RelationEndpoint, requirer: RelationEndpoint
     ):
         """
         Add relation between two charmed endpoints
@@ -730,7 +713,7 @@ class N2VCJujuConnector(N2VCConnector):
         self.log.debug(f"adding new relation between {provider} and {requirer}")
         cross_model_relation = (
             provider.model_name != requirer.model_name
-            or requirer.vca_id != requirer.vca_id
+            or provider.vca_id != requirer.vca_id
         )
         try:
             if cross_model_relation:
@@ -743,9 +726,7 @@ class N2VCJujuConnector(N2VCConnector):
                         requirer.model_name, offer, provider_libjuju
                     )
                     await requirer_libjuju.add_relation(
-                        requirer.model_name,
-                        requirer.endpoint,
-                        saas_name,
+                        requirer.model_name, requirer.endpoint, saas_name
                     )
             else:
                 # Standard relation
@@ -793,7 +774,7 @@ class N2VCJujuConnector(N2VCConnector):
         self.log.info("Deleting namespace={}".format(namespace))
         will_not_delete = False
         if namespace not in self.delete_namespace_locks:
-            self.delete_namespace_locks[namespace] = asyncio.Lock(loop=self.loop)
+            self.delete_namespace_locks[namespace] = asyncio.Lock()
         delete_lock = self.delete_namespace_locks[namespace]
 
         while delete_lock.locked():
@@ -829,6 +810,7 @@ class N2VCJujuConnector(N2VCConnector):
                                 model_name=model, total_timeout=total_timeout
                             )
                     except Exception as e:
+                        self.log.error(f"Error deleting namespace {namespace} : {e}")
                         raise N2VCException(
                             message="Error deleting namespace {} : {}".format(
                                 namespace, e
@@ -839,6 +821,9 @@ class N2VCJujuConnector(N2VCConnector):
                         message="only ns_id is permitted to delete yet",
                         bad_args=["namespace"],
                     )
+        except Exception as e:
+            self.log.error(f"Error deleting namespace {namespace} : {e}")
+            raise e
         finally:
             self.delete_namespace_locks.pop(namespace)
         self.log.info("Namespace {} deleted".format(namespace))
@@ -851,6 +836,7 @@ class N2VCJujuConnector(N2VCConnector):
         scaling_in: bool = False,
         vca_type: str = None,
         vca_id: str = None,
+        application_to_delete: str = None,
     ):
         """
         Delete an execution environment
@@ -860,10 +846,11 @@ class N2VCJujuConnector(N2VCConnector):
                             {collection: <str>, filter: {},  path: <str>},
                             e.g. {collection: "nsrs", filter:
                                 {_id: <nsd-id>, path: "_admin.deployed.VCA.3"}
-        :param: total_timeout: Total timeout
-        :param: scaling_in: Boolean to indicate if it is a scaling in operation
-        :param: vca_type: VCA type
-        :param: vca_id: VCA ID
+        :param total_timeout: Total timeout
+        :param scaling_in: Boolean to indicate if it is a scaling in operation
+        :param vca_type: VCA type
+        :param vca_id: VCA ID
+        :param application_to_delete: name of the single application to be deleted
         """
         self.log.info("Deleting execution environment ee_id={}".format(ee_id))
         libjuju = await self._get_libjuju(vca_id)
@@ -878,12 +865,30 @@ class N2VCJujuConnector(N2VCConnector):
             ee_id=ee_id
         )
         try:
-            if not scaling_in:
-                # destroy the model
-                await libjuju.destroy_model(
+            if application_to_delete == application_name:
+                # destroy the application
+                await libjuju.destroy_application(
                     model_name=model_name,
+                    application_name=application_name,
                     total_timeout=total_timeout,
                 )
+                # if model is empty delete it
+                controller = await libjuju.get_controller()
+                model = await libjuju.get_model(
+                    controller=controller,
+                    model_name=model_name,
+                )
+                if not model.applications:
+                    self.log.info("Model {} is empty, deleting it".format(model_name))
+                    await libjuju.destroy_model(
+                        model_name=model_name,
+                        total_timeout=total_timeout,
+                    )
+            elif not scaling_in:
+                # destroy the model
+                await libjuju.destroy_model(
+                    model_name=model_name, total_timeout=total_timeout
+                )
             elif vca_type == "native_charm" and scaling_in:
                 # destroy the unit in the application
                 await libjuju.destroy_unit(
@@ -986,8 +991,7 @@ class N2VCJujuConnector(N2VCConnector):
                     config=params_dict,
                 )
                 actions = await libjuju.get_actions(
-                    application_name=application_name,
-                    model_name=model_name,
+                    application_name=application_name, model_name=model_name
                 )
                 self.log.debug(
                     "Application {} has these actions: {}".format(
@@ -1056,18 +1060,81 @@ class N2VCJujuConnector(N2VCConnector):
                 if status == "completed":
                     return output
                 else:
-                    raise Exception("status is not completed: {}".format(status))
+                    if "output" in output:
+                        raise Exception(f'{status}: {output["output"]}')
+                    else:
+                        raise Exception(
+                            f"{status}: No further information received from action"
+                        )
+
             except Exception as e:
-                self.log.error(
-                    "Error executing primitive {}: {}".format(primitive_name, e)
-                )
+                self.log.error(f"Error executing primitive {primitive_name}: {e}")
                 raise N2VCExecutionException(
-                    message="Error executing primitive {} into ee={} : {}".format(
-                        primitive_name, ee_id, e
-                    ),
+                    message=f"Error executing primitive {primitive_name} in ee={ee_id}: {e}",
                     primitive_name=primitive_name,
                 )
 
+    async def upgrade_charm(
+        self,
+        ee_id: str = None,
+        path: str = None,
+        charm_id: str = None,
+        charm_type: str = None,
+        timeout: float = None,
+    ) -> str:
+        """This method upgrade charms in VNFs
+
+        Args:
+            ee_id:  Execution environment id
+            path:   Local path to the charm
+            charm_id:   charm-id
+            charm_type: Charm type can be lxc-proxy-charm, native-charm or k8s-proxy-charm
+            timeout: (Float)    Timeout for the ns update operation
+
+        Returns:
+            The output of the update operation if status equals to "completed"
+
+        """
+        self.log.info("Upgrading charm: {} on ee: {}".format(path, ee_id))
+        libjuju = await self._get_libjuju(charm_id)
+
+        # check arguments
+        if ee_id is None or len(ee_id) == 0:
+            raise N2VCBadArgumentsException(
+                message="ee_id is mandatory", bad_args=["ee_id"]
+            )
+        try:
+            (
+                model_name,
+                application_name,
+                machine_id,
+            ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id)
+
+        except Exception:
+            raise N2VCBadArgumentsException(
+                message="ee_id={} is not a valid execution environment id".format(
+                    ee_id
+                ),
+                bad_args=["ee_id"],
+            )
+
+        try:
+            await libjuju.upgrade_charm(
+                application_name=application_name,
+                path=path,
+                model_name=model_name,
+                total_timeout=timeout,
+            )
+
+            return f"Charm upgraded with application name {application_name}"
+
+        except Exception as e:
+            self.log.error("Error upgrading charm {}: {}".format(path, e))
+
+            raise N2VCException(
+                message="Error upgrading charm {} in ee={} : {}".format(path, ee_id, e)
+            )
+
     async def disconnect(self, vca_id: str = None):
         """
         Disconnect from VCA
@@ -1104,19 +1171,13 @@ class N2VCJujuConnector(N2VCConnector):
             if not self.libjuju:
                 async with self.loading_libjuju:
                     vca_connection = await get_connection(self._store)
-                    self.libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log)
+                    self.libjuju = Libjuju(vca_connection, log=self.log)
             return self.libjuju
         else:
             vca_connection = await get_connection(self._store, vca_id)
-            return Libjuju(
-                vca_connection,
-                loop=self.loop,
-                log=self.log,
-                n2vc=self,
-            )
+            return Libjuju(vca_connection, log=self.log, n2vc=self)
 
     def _write_ee_id_db(self, db_dict: dict, ee_id: str):
-
         # write ee_id to database: _admin.deployed.VCA.x
         try:
             the_table = db_dict["collection"]
@@ -1159,20 +1220,41 @@ class N2VCJujuConnector(N2VCConnector):
 
         return get_ee_id_components(ee_id)
 
-    def _get_application_name(self, namespace: str) -> str:
-        """
-        Build application name from namespace
-        :param namespace:
-        :return: app-vnf-<vnf id>-vdu-<vdu-id>-cnt-<vdu-count>-<random_value>
+    @staticmethod
+    def _find_charm_level(vnf_id: str, vdu_id: str) -> str:
+        """Decides the charm level.
+        Args:
+            vnf_id  (str):  VNF id
+            vdu_id  (str):  VDU id
+
+        Returns:
+            charm_level (str):  ns-level or vnf-level or vdu-level
         """
+        if vdu_id and not vnf_id:
+            raise N2VCException(message="If vdu-id exists, vnf-id should be provided.")
+        if vnf_id and vdu_id:
+            return "vdu-level"
+        if vnf_id and not vdu_id:
+            return "vnf-level"
+        if not vnf_id and not vdu_id:
+            return "ns-level"
 
-        # TODO: Enforce the Juju 50-character application limit
+    @staticmethod
+    def _generate_backward_compatible_application_name(
+        vnf_id: str, vdu_id: str, vdu_count: str
+    ) -> str:
+        """Generate backward compatible application name
+         by limiting the app name to 50 characters.
 
-        # split namespace components
-        _, _, vnf_id, vdu_id, vdu_count = self._get_namespace_components(
-            namespace=namespace
-        )
+        Args:
+            vnf_id  (str):  VNF ID
+            vdu_id  (str):  VDU ID
+            vdu_count   (str):  vdu-count-index
+
+        Returns:
+            application_name (str): generated application name
 
+        """
         if vnf_id is None or len(vnf_id) == 0:
             vnf_id = ""
         else:
@@ -1196,6 +1278,227 @@ class N2VCJujuConnector(N2VCConnector):
         application_name = "app-{}{}{}-{}".format(
             vnf_id, vdu_id, vdu_count, random_suffix
         )
+        return application_name
+
+    @staticmethod
+    def _get_vca_record(search_key: str, vca_records: list, vdu_id: str) -> dict:
+        """Get the correct VCA record dict depending on the search key
+
+        Args:
+            search_key  (str):      keyword to find the correct VCA record
+            vca_records (list):     All VCA records as list
+            vdu_id  (str):          VDU ID
+
+        Returns:
+            vca_record  (dict):     Dictionary which includes the correct VCA record
+
+        """
+        return next(
+            filter(lambda record: record[search_key] == vdu_id, vca_records), {}
+        )
+
+    @staticmethod
+    def _generate_application_name(
+        charm_level: str,
+        vnfrs: dict,
+        vca_records: list,
+        vnf_count: str = None,
+        vdu_id: str = None,
+        vdu_count: str = None,
+    ) -> str:
+        """Generate application name to make the relevant charm of VDU/KDU
+        in the VNFD descriptor become clearly visible.
+        Limiting the app name to 50 characters.
+
+        Args:
+            charm_level  (str):  level of charm
+            vnfrs  (dict):  vnf record dict
+            vca_records   (list):   db_nsr["_admin"]["deployed"]["VCA"] as list
+            vnf_count   (str): vnf count index
+            vdu_id   (str):  VDU ID
+            vdu_count   (str):  vdu count index
+
+        Returns:
+            application_name (str): generated application name
+
+        """
+        application_name = ""
+        if charm_level == "ns-level":
+            if len(vca_records) != 1:
+                raise N2VCException(message="One VCA record is expected.")
+            # Only one VCA record is expected if it's ns-level charm.
+            # Shorten the charm name to its first 40 characters.
+            charm_name = vca_records[0]["charm_name"][:40]
+            if not charm_name:
+                raise N2VCException(message="Charm name should be provided.")
+            application_name = charm_name + "-ns"
+
+        elif charm_level == "vnf-level":
+            if len(vca_records) < 1:
+                raise N2VCException(message="One or more VCA record is expected.")
+            # If VNF is scaled, more than one VCA record may be included in vca_records
+            # but ee_descriptor_id is same.
+            # Shorten the ee_descriptor_id and member-vnf-index-ref
+            # to first 12 characters.
+            application_name = (
+                vca_records[0]["ee_descriptor_id"][:12]
+                + "-"
+                + vnf_count
+                + "-"
+                + vnfrs["member-vnf-index-ref"][:12]
+                + "-vnf"
+            )
+        elif charm_level == "vdu-level":
+            if len(vca_records) < 1:
+                raise N2VCException(message="One or more VCA record is expected.")
+
+            # Charms are also used for deployments with Helm charts.
+            # If deployment unit is a Helm chart/KDU,
+            # vdu_profile_id and vdu_count will be empty string.
+            if vdu_count is None:
+                vdu_count = ""
+
+            # If vnf/vdu is scaled, more than one VCA record may be included in vca_records
+            # but ee_descriptor_id is same.
+            # Shorten the ee_descriptor_id, member-vnf-index-ref and vdu_profile_id
+            # to first 12 characters.
+            if not vdu_id:
+                raise N2VCException(message="vdu-id should be provided.")
+
+            vca_record = N2VCJujuConnector._get_vca_record(
+                "vdu_id", vca_records, vdu_id
+            )
+
+            if not vca_record:
+                vca_record = N2VCJujuConnector._get_vca_record(
+                    "kdu_name", vca_records, vdu_id
+                )
+
+            application_name = (
+                vca_record["ee_descriptor_id"][:12]
+                + "-"
+                + vnf_count
+                + "-"
+                + vnfrs["member-vnf-index-ref"][:12]
+                + "-"
+                + vdu_id[:12]
+                + "-"
+                + vdu_count
+                + "-vdu"
+            )
+
+        return application_name
+
+    def _get_vnf_count_and_record(
+        self, charm_level: str, vnf_id_and_count: str
+    ) -> Tuple[str, dict]:
+        """Get the vnf count and VNF record depend on charm level
+
+        Args:
+            charm_level  (str)
+            vnf_id_and_count (str)
+
+        Returns:
+            (vnf_count  (str), db_vnfr(dict)) as Tuple
+
+        """
+        vnf_count = ""
+        db_vnfr = {}
+
+        if charm_level in ("vnf-level", "vdu-level"):
+            vnf_id = "-".join(vnf_id_and_count.split("-")[:-1])
+            vnf_count = vnf_id_and_count.split("-")[-1]
+            db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_id})
+
+        # If the charm is ns level, it returns empty vnf_count and db_vnfr
+        return vnf_count, db_vnfr
+
+    @staticmethod
+    def _get_vca_records(charm_level: str, db_nsr: dict, db_vnfr: dict) -> list:
+        """Get the VCA records from db_nsr dict
+
+        Args:
+            charm_level (str):  level of charm
+            db_nsr  (dict):     NS record from database
+            db_vnfr (dict):     VNF record from database
+
+        Returns:
+            vca_records (list):  List of VCA record dictionaries
+
+        """
+        vca_records = {}
+        if charm_level == "ns-level":
+            vca_records = list(
+                filter(
+                    lambda vca_record: vca_record["target_element"] == "ns",
+                    db_nsr["_admin"]["deployed"]["VCA"],
+                )
+            )
+        elif charm_level in ["vnf-level", "vdu-level"]:
+            vca_records = list(
+                filter(
+                    lambda vca_record: vca_record["member-vnf-index"]
+                    == db_vnfr["member-vnf-index-ref"],
+                    db_nsr["_admin"]["deployed"]["VCA"],
+                )
+            )
+
+        return vca_records
+
+    def _get_application_name(self, namespace: str) -> str:
+        """Build application name from namespace
+
+        Application name structure:
+            NS level: <charm-name>-ns
+            VNF level: <ee-name>-z<vnf-ordinal-scale-number>-<vnf-profile-id>-vnf
+            VDU level: <ee-name>-z<vnf-ordinal-scale-number>-<vnf-profile-id>-
+            <vdu-profile-id>-z<vdu-ordinal-scale-number>-vdu
+
+        Application naming for backward compatibility (old structure):
+            NS level: app-<random_value>
+            VNF level: app-vnf-<vnf-id>-z<ordinal-scale-number>-<random_value>
+            VDU level: app-vnf-<vnf-id>-z<vnf-ordinal-scale-number>-vdu-
+            <vdu-id>-cnt-<vdu-count>-z<vdu-ordinal-scale-number>-<random_value>
+
+        Args:
+            namespace   (str)
+
+        Returns:
+            application_name    (str)
+
+        """
+        # split namespace components
+        (
+            nsi_id,
+            ns_id,
+            vnf_id_and_count,
+            vdu_id,
+            vdu_count,
+        ) = self._get_namespace_components(namespace=namespace)
+
+        if not ns_id:
+            raise N2VCException(message="ns-id should be provided.")
+
+        charm_level = self._find_charm_level(vnf_id_and_count, vdu_id)
+        db_nsr = self.db.get_one("nsrs", {"_id": ns_id})
+        vnf_count, db_vnfr = self._get_vnf_count_and_record(
+            charm_level, vnf_id_and_count
+        )
+        vca_records = self._get_vca_records(charm_level, db_nsr, db_vnfr)
+
+        if all("charm_name" in vca_record.keys() for vca_record in vca_records):
+            application_name = self._generate_application_name(
+                charm_level,
+                db_vnfr,
+                vca_records,
+                vnf_count=vnf_count,
+                vdu_id=vdu_id,
+                vdu_count=vdu_count,
+            )
+        else:
+            application_name = self._generate_backward_compatible_application_name(
+                vnf_id_and_count, vdu_id, vdu_count
+            )
 
         return N2VCJujuConnector._format_app_name(application_name)
 
@@ -1256,6 +1559,6 @@ class N2VCJujuConnector(N2VCConnector):
         :param: vca_id: VCA ID
         """
         vca_connection = await get_connection(self._store, vca_id=vca_id)
-        libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log, n2vc=self)
+        libjuju = Libjuju(vca_connection, log=self.log, n2vc=self)
         controller = await libjuju.get_controller()
         await libjuju.disconnect_controller(controller)