Add precommit notifications for create/update port/network in ML2

In some cases (like for validation of QoS rules in QoS
service plugin) it is necessary to send notifications
before commit create/update port and before commit
updated network.
This patch adds such notifications to be send from
ML2 plugin.

Change-Id: I89c59ad51ec59458c69a7363fc75190c6e4c9551
This commit is contained in:
Sławek Kapłoński 2017-03-20 20:31:34 +00:00
parent b6628df0df
commit 4a76c9a895
2 changed files with 30 additions and 5 deletions

View File

@ -813,6 +813,11 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
updated_network = self.get_network(context, id)
kwargs = {'context': context, 'network': updated_network,
'original_network': original_network}
registry.notify(
resources.NETWORK, events.PRECOMMIT_UPDATE, self, **kwargs)
# TODO(QoS): Move out to the extension framework somehow.
need_network_update_notify = (
qos_consts.QOS_POLICY_ID in net_data and
@ -1063,6 +1068,9 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
attrs.get(addr_pair.ADDRESS_PAIRS)))
self._process_port_create_extra_dhcp_opts(context, result,
dhcp_opts)
kwargs = {'context': context, 'port': result}
registry.notify(
resources.PORT, events.PRECOMMIT_CREATE, self, **kwargs)
self.mechanism_manager.create_port_precommit(mech_context)
self._setup_dhcp_agent_provisioning_component(context, result)
@ -1229,6 +1237,15 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
original_port=original_port)
need_port_update_notify |= self._process_port_binding(
mech_context, attrs)
kwargs = {
'context': context,
'port': updated_port,
'original_port': original_port,
}
registry.notify(
resources.PORT, events.PRECOMMIT_UPDATE, self, **kwargs)
# For DVR router interface ports we need to retrieve the
# DVRPortbinding context instead of the normal port context.
# The normal Portbinding context does not have the status

View File

@ -13,12 +13,14 @@
# under the License.
#
import mock
import netaddr
from neutron_lib import constants
from neutron_lib import context as nctx
from neutron_lib.plugins import directory
from oslo_utils import uuidutils
from neutron.callbacks import registry
from neutron.db import models_v2
from neutron.plugins.ml2 import config
from neutron.tests.unit.plugins.ml2 import test_plugin
@ -47,7 +49,8 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase):
self.l3p = directory.get_plugin(constants.L3)
self.ctx = nctx.get_admin_context()
def test_handle_expired_object(self):
@mock.patch.object(registry, 'notify')
def test_handle_expired_object(self, notify_mock):
rp = directory.get_plugin('revision_plugin')
with self.port():
with self.ctx.session.begin():
@ -64,7 +67,8 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase):
self.ctx.session.expire(port_obj)
rp._bump_related_revisions(self.ctx.session, ipal_obj)
def test_port_name_update_revises(self):
@mock.patch.object(registry, 'notify')
def test_port_name_update_revises(self, notify_mock):
with self.port() as port:
rev = port['port']['revision_number']
new = {'port': {'name': 'seaweed'}}
@ -72,7 +76,8 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase):
new_rev = response['port']['revision_number']
self.assertGreater(new_rev, rev)
def test_port_ip_update_revises(self):
@mock.patch.object(registry, 'notify')
def test_port_ip_update_revises(self, notify_mock):
with self.port() as port:
rev = port['port']['revision_number']
new = {'port': {'fixed_ips': port['port']['fixed_ips']}}
@ -128,7 +133,9 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase):
# add an intf and make sure it bumps rev
with self.subnet(tenant_id='some_tenant', cidr='10.0.1.0/24') as s:
interface_info = {'subnet_id': s['subnet']['id']}
self.l3p.add_router_interface(self.ctx, router['id'], interface_info)
with mock.patch.object(registry, 'notify'):
self.l3p.add_router_interface(self.ctx, router['id'],
interface_info)
router = updated
updated = self.l3p.get_router(self.ctx, router['id'])
self.assertGreater(updated['revision_number'],
@ -154,7 +161,8 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase):
self.assertGreater(updated['revision_number'],
router['revision_number'])
def test_qos_policy_bump_port_revision(self):
@mock.patch.object(registry, 'notify')
def test_qos_policy_bump_port_revision(self, notify_mock):
with self.port() as port:
rev = port['port']['revision_number']
qos_plugin = directory.get_plugin('QOS')