diff --git a/vmware_nsx/plugins/nsx_v/plugin.py b/vmware_nsx/plugins/nsx_v/plugin.py index 6621556a2e..2ceb2a0474 100644 --- a/vmware_nsx/plugins/nsx_v/plugin.py +++ b/vmware_nsx/plugins/nsx_v/plugin.py @@ -928,6 +928,8 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, if backend_network: # Update the QOS restrictions of the backend network self._update_network_qos(context, net_data, dvs_net_ids, net_moref) + new_net[qos_consts.QOS_POLICY_ID] = ( + qos_com_utils.get_network_policy_id(context, new_net['id'])) # this extra lookup is necessary to get the # latest db model for the extension functions @@ -1039,6 +1041,11 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, for mapping in mappings: self._delete_backend_network(mapping) + def _extend_get_network_dict_provider(self, context, net): + self._extend_network_dict_provider(context, net) + net[qos_consts.QOS_POLICY_ID] = qos_com_utils.get_network_policy_id( + context, net['id']) + def get_network(self, context, id, fields=None): with context.session.begin(subtransactions=True): # goto to the plugin DB and fetch the network @@ -1047,7 +1054,7 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, # to add provider networks fields net_result = self._make_network_dict(network, context=context) - self._extend_network_dict_provider(context, net_result) + self._extend_get_network_dict_provider(context, net_result) return self._fields(net_result, fields) def get_networks(self, context, filters=None, fields=None, @@ -1060,7 +1067,7 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, context, filters, fields, sorts, limit, marker, page_reverse)) for net in networks: - self._extend_network_dict_provider(context, net) + self._extend_get_network_dict_provider(context, net) return (networks if not fields else [self._fields(network, fields) for network in networks]) @@ -1130,6 +1137,9 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, qos_com_utils.update_network_policy_binding( context, id, net_attrs[qos_consts.QOS_POLICY_ID]) + net_res[qos_consts.QOS_POLICY_ID] = ( + qos_com_utils.get_network_policy_id(context, id)) + return net_res def _validate_address_pairs(self, attrs, db_port): diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index 45c6cf8776..20c172f86a 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -597,7 +597,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, net_data[qos_consts.QOS_POLICY_ID]) created_net[qos_consts.QOS_POLICY_ID] = ( - qos_utils.get_network_policy_id(context, created_net['id'])) + qos_com_utils.get_network_policy_id(context, created_net['id'])) return created_net @@ -758,8 +758,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _extend_get_network_dict_provider(self, context, network): self._extend_network_dict_provider(context, network) - network[qos_consts.QOS_POLICY_ID] = qos_utils.get_network_policy_id( - context, network['id']) + network[qos_consts.QOS_POLICY_ID] = (qos_com_utils. + get_network_policy_id(context, network['id'])) def get_network(self, context, id, fields=None): with context.session.begin(subtransactions=True): @@ -899,7 +899,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, qos_policy_id = port_data[qos_consts.QOS_POLICY_ID] elif device_owner.startswith(const.DEVICE_OWNER_COMPUTE_PREFIX): # check if the network of this port has a policy - qos_policy_id = qos_utils.get_network_policy_id( + qos_policy_id = qos_com_utils.get_network_policy_id( context, port_data['network_id']) if qos_policy_id: qos_profile_id = self._get_qos_profile_id(context, qos_policy_id) @@ -1262,14 +1262,14 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, policy_id = updated_port[qos_consts.QOS_POLICY_ID] else: # Look for the previous QoS policy - policy_id = qos_utils.get_port_policy_id( + policy_id = qos_com_utils.get_port_policy_id( context, updated_port['id']) # If the port is now a 'compute' port (attached to a vm) and # Qos policy was not configured on the port directly, # try to take it from the ports network if policy_id is None and is_new_compute: # check if the network of this port has a policy - policy_id = qos_utils.get_network_policy_id( + policy_id = qos_com_utils.get_network_policy_id( context, updated_port.get('network_id')) if policy_id is not None: @@ -1362,7 +1362,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, self._extend_port_dict_binding(context, port) # add the qos policy id from the DB - port[qos_consts.QOS_POLICY_ID] = qos_utils.get_port_policy_id( + port[qos_consts.QOS_POLICY_ID] = qos_com_utils.get_port_policy_id( context, port['id']) def get_port(self, context, id, fields=None): diff --git a/vmware_nsx/services/qos/common/utils.py b/vmware_nsx/services/qos/common/utils.py index 9384070307..c857d2971c 100644 --- a/vmware_nsx/services/qos/common/utils.py +++ b/vmware_nsx/services/qos/common/utils.py @@ -49,3 +49,17 @@ def update_port_policy_binding(context, port_id, new_policy_id): context, id=new_policy_id) if new_policy: new_policy.attach_port(port_id) + + +def get_port_policy_id(context, port_id): + policy = qos_policy.QosPolicy.get_port_policy( + context, port_id) + if policy: + return policy.id + + +def get_network_policy_id(context, net_id): + policy = qos_policy.QosPolicy.get_network_policy( + context, net_id) + if policy: + return policy.id diff --git a/vmware_nsx/services/qos/nsx_v3/utils.py b/vmware_nsx/services/qos/nsx_v3/utils.py index 21d55e4821..072cf2488f 100644 --- a/vmware_nsx/services/qos/nsx_v3/utils.py +++ b/vmware_nsx/services/qos/nsx_v3/utils.py @@ -31,20 +31,6 @@ LOG = logging.getLogger(__name__) MAX_KBPS_MIN_VALUE = 1024 -def get_port_policy_id(context, port_id): - policy = qos_policy.QosPolicy.get_port_policy( - context, port_id) - if policy: - return policy.id - - -def get_network_policy_id(context, net_id): - policy = qos_policy.QosPolicy.get_network_policy( - context, net_id) - if policy: - return policy.id - - def handle_qos_notification(policy_obj, event_type): handler = QosNotificationsHandler() context = n_context.get_admin_context() diff --git a/vmware_nsx/tests/unit/nsx_v/test_plugin.py b/vmware_nsx/tests/unit/nsx_v/test_plugin.py index 17919fb0af..1b1086c429 100644 --- a/vmware_nsx/tests/unit/nsx_v/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v/test_plugin.py @@ -31,6 +31,7 @@ from neutron.extensions import securitygroup as secgrp from neutron import manager from neutron.objects.qos import policy as qos_pol from neutron.plugins.common import constants as plugin_const +from neutron.services.qos import qos_consts from neutron.tests.unit import _test_extension_portbindings as test_bindings import neutron.tests.unit.db.test_allowedaddresspairs_db as test_addr_pair import neutron.tests.unit.db.test_db_base_plugin_v2 as test_plugin @@ -529,10 +530,18 @@ class TestNetworksV2(test_plugin.TestNetworksV2, NsxVPluginV2TestCase): 'admin_state_up': False, 'shared': False }} - # create the network - should succeed and translate the policy id - plugin.create_network(ctx, data) - fake_init_from_policy.assert_called_once_with(ctx, policy_id) - self.assertTrue(fake_dvs_update.called) + with mock.patch('vmware_nsx.services.qos.common.utils.' + 'get_network_policy_id', + return_value=policy_id): + # create the network - should succeed and translate the policy id + net = plugin.create_network(ctx, data) + self.assertEqual(policy_id, net[qos_consts.QOS_POLICY_ID]) + fake_init_from_policy.assert_called_once_with(ctx, policy_id) + self.assertTrue(fake_dvs_update.called) + + # Get network should also return the qos policy id + net2 = plugin.get_network(ctx, net['id']) + self.assertEqual(policy_id, net2[qos_consts.QOS_POLICY_ID]) @mock.patch.object(dvs.DvsManager, 'update_port_groups_config') @mock.patch.object(qos_utils.NsxVQosRule, '_init_from_policy_id') @@ -557,9 +566,17 @@ class TestNetworksV2(test_plugin.TestNetworksV2, NsxVPluginV2TestCase): policy_id = _uuid() data['network']['qos_policy_id'] = policy_id # update the network - should succeed and translate the policy id - plugin.update_network(ctx, net['id'], data) - fake_init_from_policy.assert_called_once_with(ctx, policy_id) - self.assertTrue(fake_dvs_update.called) + with mock.patch('vmware_nsx.services.qos.common.utils.' + 'get_network_policy_id', + return_value=policy_id): + res = plugin.update_network(ctx, net['id'], data) + self.assertEqual(policy_id, res[qos_consts.QOS_POLICY_ID]) + fake_init_from_policy.assert_called_once_with(ctx, policy_id) + self.assertTrue(fake_dvs_update.called) + + # Get network should also return the qos policy id + net2 = plugin.get_network(ctx, net['id']) + self.assertEqual(policy_id, net2[qos_consts.QOS_POLICY_ID]) @mock.patch.object(dvs.DvsManager, 'update_port_groups_config') @mock.patch.object(qos_utils.NsxVQosRule, '_init_from_policy_id') diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 19d9694b2d..e493f02d9f 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -280,7 +280,7 @@ class TestPortsV2(test_plugin.TestPortsV2, NsxV3PluginTestCaseMixin, port = self.plugin.create_port(self.ctx, data) self.assertEqual(policy_id, port['qos_policy_id']) # Get port should also return the qos policy id - with mock.patch('vmware_nsx.services.qos.nsx_v3.utils.' + with mock.patch('vmware_nsx.services.qos.common.utils.' 'get_port_policy_id', return_value=policy_id): port = self.plugin.get_port(self.ctx, port['id']) @@ -305,7 +305,7 @@ class TestPortsV2(test_plugin.TestPortsV2, NsxV3PluginTestCaseMixin, res = self.plugin.update_port(self.ctx, port['id'], data) self.assertEqual(policy_id, res['qos_policy_id']) # Get port should also return the qos policy id - with mock.patch('vmware_nsx.services.qos.nsx_v3.utils.' + with mock.patch('vmware_nsx.services.qos.common.utils.' 'get_port_policy_id', return_value=policy_id): res = self.plugin.get_port(self.ctx, port['id']) @@ -338,7 +338,7 @@ class TestPortsV2(test_plugin.TestPortsV2, NsxV3PluginTestCaseMixin, } with mock.patch.object(self.plugin, '_get_qos_profile_id') as get_profile: - with mock.patch('vmware_nsx.services.qos.nsx_v3.utils.' + with mock.patch('vmware_nsx.services.qos.common.utils.' 'get_network_policy_id', return_value=policy_id): self.plugin.create_port(self.ctx, data) get_profile.assert_called_once_with(self.ctx, policy_id) @@ -361,7 +361,7 @@ class TestPortsV2(test_plugin.TestPortsV2, NsxV3PluginTestCaseMixin, data['port']['device_owner'] = device_owner with mock.patch.object(self.plugin, '_get_qos_profile_id') as get_profile: - with mock.patch('vmware_nsx.services.qos.nsx_v3.utils.' + with mock.patch('vmware_nsx.services.qos.common.utils.' 'get_network_policy_id', return_value=policy_id): self.plugin.update_port(self.ctx, port['id'], data) get_profile.assert_called_once_with(self.ctx, policy_id) diff --git a/vmware_nsx/tests/unit/services/qos/test_nsxv_notification.py b/vmware_nsx/tests/unit/services/qos/test_nsxv_notification.py index 996a70a0df..20f3818db8 100644 --- a/vmware_nsx/tests/unit/services/qos/test_nsxv_notification.py +++ b/vmware_nsx/tests/unit/services/qos/test_nsxv_notification.py @@ -111,8 +111,17 @@ class TestQosNsxVNotification(test_plugin.NsxVPluginV2TestCase, cfg.CONF.set_override('dvs_name', 'fake_dvs', group='dvs') cfg.CONF.set_default('use_dvs_features', True, 'nsxv') + def test_create_port_anticipating_allocation(self): + with mock.patch('vmware_nsx.services.qos.common.utils.' + 'get_network_policy_id'): + super(TestQosNsxVNotification, + self).test_create_port_anticipating_allocation() + def _create_net(self): - return self._core_plugin.create_network(self.ctxt, self._net_data) + with mock.patch('vmware_nsx.services.qos.common.utils.' + 'get_network_policy_id', + return_value=self.policy.id): + return self._core_plugin.create_network(self.ctxt, self._net_data) @mock.patch.object(qos_com_utils, 'update_network_policy_binding') @mock.patch.object(dvs.DvsManager, 'update_port_groups_config')