neutron/neutron/callbacks/registry.py
armando-migliaccio ad6a3ef5f6 Add callbacks-based system to Neutron
This system helps Neutron entities to cooperate in a loose manner.

This is especially important in face of the advanced service split, where each
service gets to live independently from the core. This is also useful to clean
up some of the tight coupling between ML2 and L3, or L3 and VPN/FW.

This work was touted to be part of the effort on the v3 plugin API design.
(aka perestroika), however that is not going to bear the expected fruits
in time for the Kilo release. This framework is instead pretty crucial to
cleaning up the split between the various Neutron components and some of
the coupling between ML2 and DVR.

Subsequent patches will show how this framework is put into practice in relation
to cleaning up/decoupling the various Neutron components, however, a devref
how-to is added in the context of this patch to further clarify how the proposed
mechanism works and can be used.

Related-blueprint: services-split
Related-blueprint: plugin-interface-perestroika

Change-Id: I498aeb0773822707e82763f1f0022c580308bde0
2015-02-25 06:11:59 -08:00

49 lines
1.4 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutron.callbacks import manager
# TODO(armax): consider adding locking
CALLBACK_MANAGER = None
def _get_callback_manager():
global CALLBACK_MANAGER
if CALLBACK_MANAGER is None:
CALLBACK_MANAGER = manager.CallbacksManager()
return CALLBACK_MANAGER
def subscribe(callback, resource, event):
_get_callback_manager().subscribe(callback, resource, event)
def unsubscribe(callback, resource, event):
_get_callback_manager().unsubscribe(callback, resource, event)
def unsubscribe_by_resource(callback, resource):
_get_callback_manager().unsubscribe_by_resource(callback, resource)
def unsubscribe_all(callback):
_get_callback_manager().unsubscribe_all(callback)
def notify(resource, event, trigger, **kwargs):
_get_callback_manager().notify(resource, event, trigger, **kwargs)
def clear():
_get_callback_manager().clear()