From 8177c8486e2ed981960ec1166fe225d33150598e Mon Sep 17 00:00:00 2001 From: kvrshenoy Date: Fri, 14 Sep 2018 22:41:43 -0700 Subject: [PATCH] Multi-DVS Support When Hypervisor is vCenter This patcset supports multi DVS when network type is portgroup Change-Id: Ic983182f5039ba67f12aa545ff670be7500ba6da Partial-Bug:#1714428 --- vmware_nsx/dvs/dvs.py | 60 ++++++++++++++++-------- vmware_nsx/plugins/dvs/plugin.py | 7 ++- vmware_nsx/tests/unit/dvs/test_plugin.py | 10 ++-- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/vmware_nsx/dvs/dvs.py b/vmware_nsx/dvs/dvs.py index 78de5c64d5..b611015e47 100644 --- a/vmware_nsx/dvs/dvs.py +++ b/vmware_nsx/dvs/dvs.py @@ -167,29 +167,49 @@ class DvsManager(VCManagerBase): 'vlan_tag': vlan_tag, 'dvs': dvs_moref.value}) + def _get_portgroup(self, net_id): + """Get the port group moref of the net_id.""" + results = self._session.invoke_api(vim_util, + 'get_objects', + self._session.vim, + 'DistributedVirtualPortgroup', + 100) + while results: + for pg in results.objects: + for prop in pg.propSet: + if net_id == prop.val: + vim_util.cancel_retrieval(self._session.vim, results) + return pg.obj + results = vim_util.continue_retrieval(self._session.vim, results) + raise exceptions.NetworkNotFound(net_id=net_id) + def _net_id_to_moref(self, dvs_moref, net_id): """Gets the moref for the specific neutron network.""" # NOTE(garyk): return this from a cache if not found then invoke # code below. - port_groups = self._session.invoke_api(vim_util, - 'get_object_properties', - self._session.vim, - dvs_moref, - ['portgroup']) - if len(port_groups) and hasattr(port_groups[0], 'propSet'): - for prop in port_groups[0].propSet: - for val in prop.val[0]: - props = self._session.invoke_api(vim_util, - 'get_object_properties', - self._session.vim, - val, ['name']) - if len(props) and hasattr(props[0], 'propSet'): - for prop in props[0].propSet: - # match name or mor id - if net_id == prop.val or net_id == val.value: - # NOTE(garyk): update cache - return val - raise exceptions.NetworkNotFound(net_id=net_id) + if dvs_moref: + port_groups = self._session.invoke_api(vim_util, + 'get_object_properties', + self._session.vim, + dvs_moref, + ['portgroup']) + if len(port_groups) and hasattr(port_groups[0], 'propSet'): + for prop in port_groups[0].propSet: + for val in prop.val[0]: + props = self._session.invoke_api( + vim_util, + 'get_object_properties', + self._session.vim, + val, ['name']) + if len(props) and hasattr(props[0], 'propSet'): + for prop in props[0].propSet: + # match name or mor id + if net_id == prop.val or net_id == val.value: + # NOTE(garyk): update cache + return val + raise exceptions.NetworkNotFound(net_id=net_id) + else: + return self._get_portgroup(net_id) def _is_vlan_network_by_moref(self, moref): """ @@ -409,7 +429,7 @@ class DvsManager(VCManagerBase): 'get_object_properties_dict', self._session.vim, pg_moref, properties) - return pg_info + return pg_info, pg_moref def _get_dvs_moref_from_teaming_data(self, teaming_data): """Get the moref dvs that belongs to the teaming data""" diff --git a/vmware_nsx/plugins/dvs/plugin.py b/vmware_nsx/plugins/dvs/plugin.py index 91e289f569..de5774929d 100644 --- a/vmware_nsx/plugins/dvs/plugin.py +++ b/vmware_nsx/plugins/dvs/plugin.py @@ -188,6 +188,10 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin, trunk_mode=trunk_mode) return dvs_name + def _get_portgroup_info(self, net_id): + pg_info, dvpg_moref = self._dvs.dvs.get_port_group_info(None, net_id) + return pg_info, dvpg_moref + def _dvs_create_network(self, context, network): net_data = network['network'] if net_data['admin_state_up'] is False: @@ -207,13 +211,12 @@ class NsxDvsV2(addr_pair_db.AllowedAddressPairsMixin, net_id = dvs_name = None if net_data.get(pnet.NETWORK_TYPE) == c_utils.NetworkTypes.PORTGROUP: net_id = net_data.get(pnet.PHYSICAL_NETWORK) - pg_info = self._dvs.get_port_group_info(net_id) + pg_info, dvpg_moref = self._get_portgroup_info(net_id) if pg_info.get('name') != net_data.get('name'): err_msg = (_("Portgroup name %(dvpg)s must match network " "name %(network)s") % {'dvpg': pg_info.get('name'), 'network': net_data.get('name')}) raise n_exc.InvalidInput(error_message=err_msg) - dvpg_moref = self._dvs.net_id_to_moref(net_id) dvs_id = dvpg_moref.value else: dvs_id = self._dvs_get_id(net_data) diff --git a/vmware_nsx/tests/unit/dvs/test_plugin.py b/vmware_nsx/tests/unit/dvs/test_plugin.py index db7e5957a0..6cb4d93025 100644 --- a/vmware_nsx/tests/unit/dvs/test_plugin.py +++ b/vmware_nsx/tests/unit/dvs/test_plugin.py @@ -221,11 +221,10 @@ class NeutronSimpleDvsTest(NeutronSimpleDvsTestCase): @mock.patch.object(dvs.DvsManager, 'get_port_group_info') @mock.patch.object(dvs.DvsManager, '_net_id_to_moref') - def test_create_and_delete_dvs_network_portgroup(self, fake_get_moref, + def test_create_and_delete_dvs_network_portgroup(self, fake_moref, fake_pg_info): - fake_pg_info.return_value = {'name': 'fake-name'} + fake_pg_info.return_value = {'name': 'fake-name'}, fake_moref self._create_and_delete_dvs_network(network_type='portgroup') - self.assertTrue(fake_get_moref.call_count) self.assertTrue(fake_pg_info.call_count) @mock.patch.object(dvs.DvsManager, 'get_port_group_info') @@ -233,10 +232,9 @@ class NeutronSimpleDvsTest(NeutronSimpleDvsTestCase): def test_create_and_delete_dvs_network_portgroup_vlan(self, fake_get_moref, fake_pg_info): - fake_pg_info.return_value = {'name': 'fake-name'} + fake_pg_info.return_value = {'name': 'fake-name'}, fake_get_moref self._create_and_delete_dvs_network(network_type='portgroup', vlan_tag=7) - self.assertTrue(fake_get_moref.call_count) self.assertTrue(fake_pg_info.call_count) def test_create_and_delete_dvs_port(self): @@ -333,7 +331,7 @@ class NeutronSimpleDvsTest(NeutronSimpleDvsTestCase): def test_create_and_delete_portgroup_network_invalid_name(self, fake_get_moref, fake_pg_info): - fake_pg_info.return_value = {'name': 'fake-different-name'} + fake_pg_info.return_value = {'name': 'invalid-name'}, fake_get_moref data = {'network': {'provider:network_type': 'portgroup', 'name': 'fake-name', 'admin_state_up': True}}