From: aticig Date: Fri, 10 Dec 2021 09:59:20 +0000 (+0300) Subject: Fix Bug 1781 RO does not create management networks where appropriate X-Git-Tag: v12.0.0rc1~40 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=e9a26f282aa9a82e19484514c69b18b381594ab4;p=osm%2FRO.git Fix Bug 1781 RO does not create management networks where appropriate If there is mgmt-network in the descriptor, there is no mapping of that network to a VIM network in the descriptor, also there is no mapping in the "--config" parameter or at VIM creation; that mgmt-network will be created. Change-Id: Ic3ba8bed0417c49bf7d205b91c04703c6aa1b6a3 Signed-off-by: aticig --- diff --git a/NG-RO/osm_ng_ro/ns_thread.py b/NG-RO/osm_ng_ro/ns_thread.py index 01c894dd..ee62af4f 100644 --- a/NG-RO/osm_ng_ro/ns_thread.py +++ b/NG-RO/osm_ng_ro/ns_thread.py @@ -123,6 +123,8 @@ class VimInteractionNet(VimInteractionBase): created = False created_items = {} target_vim = self.my_vims[ro_task["target_id"]] + mgmtnet = False + mgmtnet_defined_in_vim = False try: # FIND @@ -130,13 +132,15 @@ class VimInteractionNet(VimInteractionBase): # if management, get configuration of VIM if task["find_params"].get("filter_dict"): vim_filter = task["find_params"]["filter_dict"] - # mamagement network + # management network elif task["find_params"].get("mgmt"): + mgmtnet = True if deep_get( self.db_vims[ro_task["target_id"]], "config", "management_network_id", ): + mgmtnet_defined_in_vim = True vim_filter = { "id": self.db_vims[ro_task["target_id"]]["config"][ "management_network_id" @@ -147,6 +151,7 @@ class VimInteractionNet(VimInteractionBase): "config", "management_network_name", ): + mgmtnet_defined_in_vim = True vim_filter = { "name": self.db_vims[ro_task["target_id"]]["config"][ "management_network_name" @@ -161,11 +166,29 @@ class VimInteractionNet(VimInteractionBase): vim_nets = target_vim.get_network_list(vim_filter) if not vim_nets and not task.get("params"): - raise NsWorkerExceptionNotFound( - "Network not found with this criteria: '{}'".format( - task.get("find_params") + # If there is mgmt-network in the descriptor, + # there is no mapping of that network to a VIM network in the descriptor, + # also there is no mapping in the "--config" parameter or at VIM creation; + # that mgmt-network will be created. + if mgmtnet and not mgmtnet_defined_in_vim: + net_name = ( + vim_filter.get("name") + if vim_filter.get("name") + else vim_filter.get("id")[:16] + ) + vim_net_id, created_items = target_vim.new_network( + net_name, None + ) + self.logger.debug( + "Created mgmt network vim_net_id: {}".format(vim_net_id) + ) + created = True + else: + raise NsWorkerExceptionNotFound( + "Network not found with this criteria: '{}'".format( + task.get("find_params") + ) ) - ) elif len(vim_nets) > 1: raise NsWorkerException( "More than one network found with this criteria: '{}'".format( diff --git a/NG-RO/osm_ng_ro/tests/test_ns_thread.py b/NG-RO/osm_ng_ro/tests/test_ns_thread.py index 9aa7c18e..41a6ff1a 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns_thread.py +++ b/NG-RO/osm_ng_ro/tests/test_ns_thread.py @@ -38,14 +38,15 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_id_in_find_params_mgmt_several_vim_nets(self): """ - management_network_id in find_params.get('mgmt') + mgmt network is set in find_params + management_network_id in vim config More than one network found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_2": { + 0: { "config": { "management_network_id": "test_mgmt_id", }, @@ -55,12 +56,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_2": { - "target_id": "vim_openstack_2", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -99,14 +100,15 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_id_in_find_params_mgmt_no_vim_nets(self): """ - management_network_id in find_params.get('mgmt') + mgmt network is set in find_params + management_network_id in vim config The network could not be found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_3": { + 0: { "config": { "management_network_id": "test_mgmt_id", }, @@ -115,13 +117,13 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( - instance, "logger", logging - ): + instance, "db_vims", db_vims + ), patch.object(instance, "logger", logging): ro_task = { "target_id": 0, "tasks": { "task_index_3": { - "target_id": "vim_openstack_3", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -155,16 +157,74 @@ class TestVimInteractionNet(unittest.TestCase): self.assertEqual(result[1].get("created"), False) self.assertEqual(result[1].get("vim_status"), "VIM_ERROR") + def test__mgmt_net_in_find_params_no_vim_config_no_vim_nets(self): + """ + mgmt network is set in find_params + vim config does not have management_network_id or management_network_id + The network could not be found in the VIM + """ + db = "test_db" + logger = "test_logger" + my_vims = "test-vim" + db_vims = { + 0: { + "config": {}, + }, + } + + instance = VimInteractionNet(db, logger, my_vims, db_vims) + with patch.object(instance, "my_vims", [self.target_vim]), patch.object( + instance, "db_vims", db_vims + ), patch.object(instance, "logger", logging): + ro_task = { + "target_id": 0, + "tasks": { + "task_index_3": { + "target_id": 0, + "action_id": "123456", + "nsr_id": "654321", + "task_id": "123456:1", + "status": "SCHEDULED", + "action": "CREATE", + "item": "test_item", + "target_record": "test_target_record", + "target_record_id": "test_target_record_id", + "params": {}, + # values coming from extra_dict + "find_params": { + "mgmt": True, + "name": "some_mgmt_name", + }, + "depends_on": "test_depends_on", + }, + }, + } + + task_index = "task_index_3" + self.target_vim.get_network_list.return_value = [] + self.target_vim.new_network.return_value = "sample_net_id", { + "item1": "sample_created_item" + } + result = instance.new(ro_task, task_index, self.task_depends) + self.assertEqual(result[0], "BUILD") + self.assertEqual(result[1].get("vim_id"), "sample_net_id") + self.assertEqual(result[1].get("created"), True) + self.assertDictEqual( + result[1].get("created_items"), {"item1": "sample_created_item"} + ) + self.assertEqual(result[1].get("vim_status"), "BUILD") + def test__mgmt_net_name_in_find_params_mgmt_several_vim_nets(self): """ - management_network_name in find_params.get('mgmt') + mgmt network is set in find_params + management_network_name in vim config More than one network found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_4": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -174,12 +234,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_4": { - "target_id": "vim_openstack_4", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -218,14 +278,15 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_name_in_find_params_mgmt_no_vim_nets(self): """ - management_network_name in find_params.get('mgmt') + mgmt network is set in find_params + management_network_name in vim config The network could not be found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_5": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -235,12 +296,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_5": { - "target_id": "vim_openstack_5", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -276,14 +337,16 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_name_in_find_params_filterdict_several_vim_nets(self): """ - management_network_name in find_params.get('filterdict') + mgmt network is set in find_params + management_network_name in vim config + network_name is set in find_params.get('filterdict') More than one network found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_6": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -292,12 +355,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_6": { - "target_id": "vim_openstack_6", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -347,7 +410,7 @@ class TestVimInteractionNet(unittest.TestCase): logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_7": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -356,12 +419,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_4": { - "target_id": "vim_openstack_7", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -371,7 +434,7 @@ class TestVimInteractionNet(unittest.TestCase): "target_record": "test_target_record", "target_record_id": "test_target_record_id", # values coming from extra_dict - "params": "", + "params": {}, "find_params": {"wrong_param": "wrong_value"}, "depends_on": "test_depends_on", }, @@ -401,7 +464,7 @@ class TestVimInteractionNet(unittest.TestCase): logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_8": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -410,12 +473,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_8": { - "target_id": "vim_openstack_8", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -451,14 +514,16 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_name_in_find_params_filterdict_no_vim_nets(self): """ - management_network_name in find_params.get('filterdict') + mgmt network is set in find_params + management_network_name in vim config + network_name is set in find_params.get('filterdict') Any network could not be found in the VIM """ db = "test_db" logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_9": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -467,12 +532,72 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_9": { - "target_id": "vim_openstack_9", + "target_id": 0, + "action_id": "123456", + "nsr_id": "654321", + "task_id": "123456:1", + "status": "SCHEDULED", + "action": "CREATE", + "item": "test_item", + "target_record": "test_target_record", + "target_record_id": "test_target_record_id", + # values coming from extra_dict + "params": "", + "find_params": { + "filter_dict": { + "name": "some-network-name", + }, + "mgmt": True, + "name": "some_mgmt_name", + }, + "depends_on": "test_depends_on", + }, + }, + } + + task_index = "task_index_9" + self.target_vim.get_network_list.return_value = [] + with self.assertLogs() as captured: + result = instance.new(ro_task, task_index, self.task_depends) + self.assertEqual(len(captured.records), 1) + self.assertTrue( + "Network not found with this criteria" + in captured.records[0].getMessage() + ) + self.assertEqual(captured.records[0].levelname, "ERROR") + self.assertEqual(result[0], "FAILED") + self.assertEqual(result[1].get("created"), False) + self.assertEqual(result[1].get("vim_status"), "VIM_ERROR") + + def test__mgmt_net_in_find_params_filterdict_no_config_no_vim_nets(self): + """ + mgmt network is set in find_params + vim config is empty + network_name is set in find_params.get('filterdict') + Any network could not be found in the VIM + """ + db = "test_db" + logger = "test_logger" + my_vims = "test-vim" + db_vims = { + 0: { + "config": {}, + }, + } + instance = VimInteractionNet(db, logger, my_vims, db_vims) + with patch.object(instance, "my_vims", [self.target_vim]), patch.object( + instance, "logger", logging + ), patch.object(instance, "db_vims", db_vims): + ro_task = { + "target_id": 0, + "tasks": { + "task_index_9": { + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -511,7 +636,7 @@ class TestVimInteractionNet(unittest.TestCase): def test__mgmt_net_name_in_find_params_mgmt_no_config_one_vim_net(self): """ - name in find_params + mgmt network is set in find_params management_network_name is not in db_vims.get('config') One network found in the VIM """ @@ -519,19 +644,19 @@ class TestVimInteractionNet(unittest.TestCase): logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_10": { + 0: { "config": {}, }, } instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_2": { - "target_id": "vim_openstack_10", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -569,7 +694,7 @@ class TestVimInteractionNet(unittest.TestCase): logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_11": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -578,12 +703,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_11": { - "target_id": "vim_openstack_11", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -624,7 +749,7 @@ class TestVimInteractionNet(unittest.TestCase): logger = "test_logger" my_vims = "test-vim" db_vims = { - "vim_openstack_13": { + 0: { "config": { "management_network_name": "test_mgmt_name", }, @@ -633,12 +758,12 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object(instance, "my_vims", [self.target_vim]), patch.object( instance, "logger", logging - ): + ), patch.object(instance, "db_vims", db_vims): ro_task = { "target_id": 0, "tasks": { "task_index_12": { - "target_id": "vim_openstack_13", + "target_id": 0, "action_id": "123456", "nsr_id": "654321", "task_id": "123456:1", @@ -677,7 +802,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_1": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "122436:1", "locked_by": None, @@ -729,7 +856,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_1": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "122436:1", "locked_by": None, @@ -782,7 +911,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_1": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "122436:1", "locked_by": None, @@ -835,7 +966,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_1": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "122436:1", "locked_by": None, @@ -877,7 +1010,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_1": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "122436:1", "locked_by": None, @@ -931,7 +1066,9 @@ class TestVimInteractionNet(unittest.TestCase): instance = VimInteractionNet(db, logger, my_vims, db_vims) with patch.object( instance, "my_vims", {"vim_openstack_2": self.target_vim} - ), patch.object(instance, "logger", logging): + ), patch.object(instance, "logger", logging), patch.object( + instance, "db_vims", db_vims + ): ro_task = { "_id": "128436:1", "locked_by": None,