From 15db614704fe8bc6a281a92d8e10db3c77794285 Mon Sep 17 00:00:00 2001 From: aticig Date: Mon, 24 Jan 2022 12:51:26 +0300 Subject: [PATCH] Fix Bug 1425 NG-RO unable to pin VNF with vim_account config If vnf vim-accounts has been changed during instantiation, the binding between new vim-account information and ns vld is provided through this fix. Fixing some code format errors Change-Id: I183c01d284047d5e49b7219eb88e6d85c0e2666c Signed-off-by: aticig --- osm_lcm/data_utils/vca.py | 4 +++ osm_lcm/lcm.py | 4 +-- osm_lcm/ns.py | 61 ++++++++++++++++++++++++-------- osm_lcm/prometheus.py | 7 ++-- osm_lcm/tests/test_prometheus.py | 10 ++---- 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/osm_lcm/data_utils/vca.py b/osm_lcm/data_utils/vca.py index 2116549..f4235cb 100644 --- a/osm_lcm/data_utils/vca.py +++ b/osm_lcm/data_utils/vca.py @@ -123,6 +123,7 @@ class EERelation(dict): class Relation(dict): """Represents a relation""" + def __init__(self, name, provider: EERelation, requirer: EERelation) -> NoReturn: """ Args: @@ -152,6 +153,7 @@ class Relation(dict): class DeployedComponent(dict): """Represents a deployed component (nsr["_admin"]["deployed"])""" + def __init__(self, data: Dict[str, Any]): """ Args: @@ -176,6 +178,7 @@ class DeployedComponent(dict): class DeployedK8sResource(DeployedComponent): """Represents a deployed component for a kdu resource""" + def __init__(self, data: Dict[str, Any]): super().__init__(data) @@ -193,6 +196,7 @@ class DeployedK8sResource(DeployedComponent): class DeployedVCA(DeployedComponent): """Represents a VCA deployed component""" + def __init__(self, nsr_id: str, deployed_vca: Dict[str, Any]) -> NoReturn: """ Args: diff --git a/osm_lcm/lcm.py b/osm_lcm/lcm.py index 060aa07..f8b204a 100644 --- a/osm_lcm/lcm.py +++ b/osm_lcm/lcm.py @@ -671,9 +671,7 @@ class Lcm: # check RO version self.loop.run_until_complete(self.check_RO_version()) - self.ns = ns.NsLcm( - self.msg, self.lcm_tasks, self.config, self.loop - ) + self.ns = ns.NsLcm(self.msg, self.lcm_tasks, self.config, self.loop) self.netslice = netslice.NetsliceLcm( self.msg, self.lcm_tasks, self.config, self.loop, self.ns ) diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index 8ec39ad..33e5aa1 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -822,6 +822,37 @@ class NsLcm(LcmBase): if vld_params.get("common_id"): target_vld["common_id"] = vld_params.get("common_id") + # modify target["ns"]["vld"] with instantiation parameters to override vnf vim-account + def update_ns_vld_target(target, ns_params): + for vnf_params in ns_params.get("vnf", ()): + if vnf_params.get("vimAccountId"): + target_vnf = next( + ( + vnfr + for vnfr in db_vnfrs.values() + if vnf_params["member-vnf-index"] + == vnfr["member-vnf-index-ref"] + ), + None, + ) + vdur = next((vdur for vdur in target_vnf.get("vdur", ())), None) + for a_index, a_vld in enumerate(target["ns"]["vld"]): + target_vld = find_in_list( + get_iterable(vdur, "interfaces"), + lambda iface: iface.get("ns-vld-id") == a_vld["name"], + ) + if target_vld: + if vnf_params.get("vimAccountId") not in a_vld.get( + "vim_info", {} + ): + target["ns"]["vld"][a_index].get("vim_info").update( + { + "vim:{}".format(vnf_params["vimAccountId"]): { + "vim_network_name": "" + } + } + ) + nslcmop_id = db_nslcmop["_id"] target = { "name": db_nsr["name"], @@ -937,6 +968,8 @@ class NsLcm(LcmBase): vld_params.update(vld_instantiation_params) parse_vld_instantiation_params(target_vim, target_vld, vld_params, None) target["ns"]["vld"].append(target_vld) + # Update the target ns_vld if vnf vim_account is overriden by instantiation params + update_ns_vld_target(target, ns_params) for vnfr in db_vnfrs.values(): vnfd = find_in_list( @@ -1665,7 +1698,8 @@ class NsLcm(LcmBase): base_folder["folder"], base_folder["pkg-dir"], "charms" - if vca_type in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm") + if vca_type + in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm") else "helm-charts", vca_name, ) @@ -1673,7 +1707,8 @@ class NsLcm(LcmBase): artifact_path = "{}/Scripts/{}/{}/".format( base_folder["folder"], "charms" - if vca_type in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm") + if vca_type + in ("native_charm", "lxc_proxy_charm", "k8s_proxy_charm") else "helm-charts", vca_name, ) @@ -2004,12 +2039,10 @@ class NsLcm(LcmBase): for job in prometheus_jobs: self.db.set_one( "prometheus_jobs", - { - "job_name": job["job_name"] - }, + {"job_name": job["job_name"]}, job, upsert=True, - fail_on_empty=False + fail_on_empty=False, ) step = "instantiated at VCA" @@ -2784,7 +2817,9 @@ class NsLcm(LcmBase): if requirer_id != nsd["id"]: requirer_dict["vnf-profile-id"] = requirer_id else: - raise Exception("provider/requirer or entities must be included in the relation.") + raise Exception( + "provider/requirer or entities must be included in the relation." + ) relation_provider = self._update_ee_relation_data_with_implicit_data( nsr_id, nsd, provider_dict, cached_vnfds ) @@ -2836,7 +2871,9 @@ class NsLcm(LcmBase): if requirer_id != vnfd_id: requirer_dict["vdu-profile-id"] = requirer_id else: - raise Exception("provider/requirer or entities must be included in the relation.") + raise Exception( + "provider/requirer or entities must be included in the relation." + ) relation_provider = self._update_ee_relation_data_with_implicit_data( nsr_id, nsd, provider_dict, cached_vnfds, vnf_profile_id=vnf_profile_id ) @@ -6342,13 +6379,7 @@ class NsLcm(LcmBase): ) async def extract_prometheus_scrape_jobs( - self, - ee_id, - artifact_path, - ee_config_descriptor, - vnfr_id, - nsr_id, - target_ip + self, ee_id, artifact_path, ee_config_descriptor, vnfr_id, nsr_id, target_ip ): # look if exist a file called 'prometheus*.j2' and artifact_content = self.fs.dir_ls(artifact_path) diff --git a/osm_lcm/prometheus.py b/osm_lcm/prometheus.py index 9ea2989..98f5bb1 100644 --- a/osm_lcm/prometheus.py +++ b/osm_lcm/prometheus.py @@ -30,5 +30,8 @@ def parse_job(job_data: str, variables: dict) -> dict: return yaml.safe_load(job_parsed) except (TemplateError, TemplateNotFound, TemplateSyntaxError) as e: # TODO yaml exceptions - raise LcmException("Error parsing Jinja2 to prometheus job. job_data={}, variables={}. Error={}".format( - job_data, variables, e)) + raise LcmException( + "Error parsing Jinja2 to prometheus job. job_data={}, variables={}. Error={}".format( + job_data, variables, e + ) + ) diff --git a/osm_lcm/tests/test_prometheus.py b/osm_lcm/tests/test_prometheus.py index 67b460d..0a76f81 100644 --- a/osm_lcm/tests/test_prometheus.py +++ b/osm_lcm/tests/test_prometheus.py @@ -22,20 +22,16 @@ __author__ = "Alfonso Tierno " class TestPrometheus(asynctest.TestCase): - def test_parse_job(self): text_to_parse = """ # yaml format with jinja2 key1: "parsing var1='{{ var1 }}'" key2: "parsing var2='{{ var2 }}'" """ - vars = {'var1': 'VAR1', 'var2': 'VAR2', 'var3': 'VAR3'} - expected = { - 'key1': "parsing var1='VAR1'", - 'key2': "parsing var2='VAR2'" - } + vars = {"var1": "VAR1", "var2": "VAR2", "var3": "VAR3"} + expected = {"key1": "parsing var1='VAR1'", "key2": "parsing var2='VAR2'"} result = parse_job(text_to_parse, vars) - self.assertEqual(result, expected, 'Error at jinja2 parse') + self.assertEqual(result, expected, "Error at jinja2 parse") if __name__ == "__main__": -- 2.25.1