Feature 9952: Distributed Proxy Charms
[osm/LCM.git] / osm_lcm / ns.py
index 02dde15..198def1 100644 (file)
@@ -49,35 +49,6 @@ from random import randint
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 
 
-class N2VCJujuConnectorLCM(N2VCJujuConnector):
-
-    async def create_execution_environment(self, namespace: str, db_dict: dict, reuse_ee_id: str = None,
-                                           progress_timeout: float = None, total_timeout: float = None,
-                                           config: dict = None, artifact_path: str = None,
-                                           vca_type: str = None) -> (str, dict):
-        # admit two new parameters, artifact_path and vca_type
-        if vca_type == "k8s_proxy_charm":
-            ee_id = await self.install_k8s_proxy_charm(
-                charm_name=artifact_path[artifact_path.rfind("/") + 1:],
-                namespace=namespace,
-                artifact_path=artifact_path,
-                db_dict=db_dict)
-            return ee_id, None
-        else:
-            return await super().create_execution_environment(
-                namespace=namespace, db_dict=db_dict, reuse_ee_id=reuse_ee_id,
-                progress_timeout=progress_timeout, total_timeout=total_timeout)
-
-    async def install_configuration_sw(self, ee_id: str, artifact_path: str, db_dict: dict,
-                                       progress_timeout: float = None, total_timeout: float = None,
-                                       config: dict = None, num_units: int = 1, vca_type: str = "lxc_proxy_charm"):
-        if vca_type == "k8s_proxy_charm":
-            return
-        return await super().install_configuration_sw(
-            ee_id=ee_id, artifact_path=artifact_path, db_dict=db_dict, progress_timeout=progress_timeout,
-            total_timeout=total_timeout, config=config, num_units=num_units)
-
-
 class NsLcm(LcmBase):
     timeout_vca_on_error = 5 * 60   # Time for charm from first time at blocked,error status to mark as failed
     timeout_ns_deploy = 2 * 3600   # default global timeout for deployment a ns
@@ -112,7 +83,7 @@ class NsLcm(LcmBase):
         self.vca_config = config["VCA"].copy()
 
         # create N2VC connector
-        self.n2vc = N2VCJujuConnectorLCM(
+        self.n2vc = N2VCJujuConnector(
             db=self.db,
             fs=self.fs,
             log=self.logger,
@@ -1545,6 +1516,12 @@ class NsLcm(LcmBase):
             # find old ee_id if exists
             ee_id = vca_deployed.get("ee_id")
 
+            vim_account_id = (
+                deep_get(db_vnfr, ("vim-account-id",)) or
+                deep_get(deploy_params, ("OSM", "vim_account_id"))
+            )
+            vca_cloud, vca_cloud_credential = self.get_vca_cloud_and_credentials(vim_account_id)
+            vca_k8s_cloud, vca_k8s_cloud_credential = self.get_vca_k8s_cloud_and_credentials(vim_account_id)
             # create or register execution environment in VCA
             if vca_type in ("lxc_proxy_charm", "k8s_proxy_charm", "helm", "helm-v3"):
 
@@ -1558,13 +1535,27 @@ class NsLcm(LcmBase):
 
                 step = "create execution environment"
                 self.logger.debug(logging_text + step)
-                ee_id, credentials = await self.vca_map[vca_type].create_execution_environment(
-                    namespace=namespace,
-                    reuse_ee_id=ee_id,
-                    db_dict=db_dict,
-                    config=osm_config,
-                    artifact_path=artifact_path,
-                    vca_type=vca_type)
+
+                ee_id = None
+                credentials = None
+                if vca_type == "k8s_proxy_charm":
+                    ee_id = await self.vca_map[vca_type].install_k8s_proxy_charm(
+                        charm_name=artifact_path[artifact_path.rfind("/") + 1:],
+                        namespace=namespace,
+                        artifact_path=artifact_path,
+                        db_dict=db_dict,
+                        cloud_name=vca_k8s_cloud,
+                        credential_name=vca_k8s_cloud_credential,
+                    )
+                else:
+                    ee_id, credentials = await self.vca_map[vca_type].create_execution_environment(
+                        namespace=namespace,
+                        reuse_ee_id=ee_id,
+                        db_dict=db_dict,
+                        config=osm_config,
+                        cloud_name=vca_cloud,
+                        credential_name=vca_cloud_credential,
+                    )
 
             elif vca_type == "native_charm":
                 step = "Waiting to VM being up and getting IP address"
@@ -1599,7 +1590,12 @@ class NsLcm(LcmBase):
                 step = "register execution environment {}".format(credentials)
                 self.logger.debug(logging_text + step)
                 ee_id = await self.vca_map[vca_type].register_execution_environment(
-                    credentials=credentials, namespace=namespace, db_dict=db_dict)
+                    credentials=credentials,
+                    namespace=namespace,
+                    db_dict=db_dict,
+                    cloud_name=vca_cloud,
+                    credential_name=vca_cloud_credential,
+                )
 
             # for compatibility with MON/POL modules, the need model and application name at database
             # TODO ask MON/POL if needed to not assuming anymore the format "model_name.application_name"
@@ -1645,15 +1641,14 @@ class NsLcm(LcmBase):
                         if vdu_id == v["vdu-id-ref"]:
                             num_units = v.get("config-units") or 1
                             break
-
-            await self.vca_map[vca_type].install_configuration_sw(
-                ee_id=ee_id,
-                artifact_path=artifact_path,
-                db_dict=db_dict,
-                config=config,
-                num_units=num_units,
-                vca_type=vca_type
-            )
+            if vca_type != "k8s_proxy_charm":
+                await self.vca_map[vca_type].install_configuration_sw(
+                    ee_id=ee_id,
+                    artifact_path=artifact_path,
+                    db_dict=db_dict,
+                    config=config,
+                    num_units=num_units,
+                )
 
             # write in db flag of configuration_sw already installed
             self.update_db_2("nsrs", nsr_id, {db_update_entry + "config_sw_installed": True})
@@ -4476,3 +4471,36 @@ class NsLcm(LcmBase):
         job_dict = {jl["job_name"]: jl for jl in job_list}
         if await self.prometheus.update(job_dict):
             return list(job_dict.keys())
+
+    def get_vca_cloud_and_credentials(self, vim_account_id: str) -> (str, str):
+        """
+        Get VCA Cloud and VCA Cloud Credentials for the VIM account
+
+        :param: vim_account_id:     VIM Account ID
+
+        :return: (cloud_name, cloud_credential)
+        """
+        config = self.get_vim_account_config(vim_account_id)
+        return config.get("vca_cloud"), config.get("vca_cloud_credential")
+
+    def get_vca_k8s_cloud_and_credentials(self, vim_account_id: str) -> (str, str):
+        """
+        Get VCA K8s Cloud and VCA K8s Cloud Credentials for the VIM account
+
+        :param: vim_account_id:     VIM Account ID
+
+        :return: (cloud_name, cloud_credential)
+        """
+        config = self.get_vim_account_config(vim_account_id)
+        return config.get("vca_k8s_cloud"), config.get("vca_k8s_cloud_credential")
+
+    def get_vim_account_config(self, vim_account_id: str) -> dict:
+        """
+        Get VIM Account config from the OSM Database
+
+        :param: vim_account_id:     VIM Account ID
+
+        :return: Dictionary with the config of the vim account
+        """
+        vim_account = self.db.get_one(table="vim_accounts", q_filter={"_id": vim_account_id}, fail_on_empty=False)
+        return vim_account.get("config", {}) if vim_account else {}