Skip to content

Commit

Permalink
Move Nova notification logic out of API controller
Browse files Browse the repository at this point in the history
Once the decoupling started with 60c05a6, this cleanup
patch continues the effort by improving the decoupling
between the API layer and Nova.

Change-Id: I1d7d4b80ee77deefce18df22f76cab81750c0397
  • Loading branch information
armando-migliaccio committed Jun 28, 2016
1 parent e162ce1 commit a4df99f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
21 changes: 6 additions & 15 deletions neutron/api/v2/base.py
Expand Up @@ -18,7 +18,6 @@

import netaddr
from neutron_lib import exceptions
from oslo_config import cfg
from oslo_log import log as logging
from oslo_policy import policy as oslo_policy
from oslo_utils import excutils
Expand Down Expand Up @@ -90,9 +89,6 @@ def __init__(self, plugin, collection, resource, attr_info,
self._policy_attrs = [name for (name, info) in self._attr_info.items()
if info.get('required_by_policy')]
self._notifier = n_rpc.get_notifier('network')
if cfg.CONF.notify_nova_on_port_data_changes:
from neutron.notifiers import nova
self._nova_notifier = nova.Notifier()
self._member_actions = member_actions
self._primary_key = self._get_primary_key()
if self._allow_pagination and self._native_pagination:
Expand Down Expand Up @@ -327,10 +323,6 @@ def _item(self, request, id, do_authz=False, field_list=None,
pluralized=self._collection)
return obj

def _send_nova_notification(self, action, orig, returned):
if hasattr(self, '_nova_notifier'):
self._nova_notifier.send_network_change(action, orig, returned)

@db_api.retry_db_errors
def index(self, request, **kwargs):
"""Returns a list of the requested entity."""
Expand Down Expand Up @@ -473,7 +465,8 @@ def notify(create_result):
registry.notify(self._resource, events.BEFORE_RESPONSE, self,
context=request.context, data=create_result,
method_name=notifier_method,
collection=self._collection)
collection=self._collection,
action=action, original={})
return create_result

def do_create(body, bulk=False, emulated=False):
Expand Down Expand Up @@ -519,8 +512,6 @@ def do_create(body, bulk=False, emulated=False):
return notify({self._collection: objs})
else:
obj = do_create(body)
self._send_nova_notification(action, {},
{self._resource: obj})
return notify({self._resource: self._view(request.context,
obj)})

Expand Down Expand Up @@ -563,10 +554,10 @@ def _delete(self, request, id, **kwargs):
notifier_method,
{self._resource + '_id': id})
result = {self._resource: self._view(request.context, obj)}
self._send_nova_notification(action, {}, result)
registry.notify(self._resource, events.BEFORE_RESPONSE, self,
context=request.context, data=result,
method_name=notifier_method)
method_name=notifier_method, action=action,
original={})

def update(self, request, id, body=None, **kwargs):
"""Updates the specified entity's attributes."""
Expand Down Expand Up @@ -637,8 +628,8 @@ def _update(self, request, id, body, **kwargs):
self._notifier.info(request.context, notifier_method, result)
registry.notify(self._resource, events.BEFORE_RESPONSE, self,
context=request.context, data=result,
method_name=notifier_method)
self._send_nova_notification(action, orig_object_copy, result)
method_name=notifier_method, action=action,
original=orig_object_copy)
return result

@staticmethod
Expand Down
17 changes: 17 additions & 0 deletions neutron/notifiers/nova.py
Expand Up @@ -23,6 +23,9 @@
from sqlalchemy.orm import attributes as sql_attr

from neutron._i18n import _LE, _LI, _LW
from neutron.callbacks import events
from neutron.callbacks import registry
from neutron.callbacks import resources
from neutron import context
from neutron import manager
from neutron.notifiers import batch_notifier
Expand Down Expand Up @@ -68,6 +71,15 @@ def __init__(self):
self.batch_notifier = batch_notifier.BatchNotifier(
cfg.CONF.send_events_interval, self.send_events)

# register callbacks for events pertaining resources affecting Nova
callback_resources = (
resources.FLOATING_IP,
resources.PORT,
)
for resource in callback_resources:
registry.subscribe(self._send_nova_notification,
resource, events.BEFORE_RESPONSE)

def _is_compute_port(self, port):
try:
if (port['device_id'] and uuidutils.is_uuid_like(port['device_id'])
Expand Down Expand Up @@ -96,6 +108,11 @@ def _plugin(self):
self._plugin_ref = manager.NeutronManager.get_plugin()
return self._plugin_ref

def _send_nova_notification(self, resource, event, trigger,
action=None, original=None, data=None,
**kwargs):
self.send_network_change(action, original, data)

def send_network_change(self, action, original_obj,
returned_obj):
"""Called when a network change is made that nova cares about.
Expand Down

0 comments on commit a4df99f

Please sign in to comment.