summaryrefslogtreecommitdiffstats
path: root/networking-odl/networking_odl/common/callback.py
diff options
context:
space:
mode:
Diffstat (limited to 'networking-odl/networking_odl/common/callback.py')
-rw-r--r--networking-odl/networking_odl/common/callback.py94
1 files changed, 61 insertions, 33 deletions
diff --git a/networking-odl/networking_odl/common/callback.py b/networking-odl/networking_odl/common/callback.py
index d9d168b..fe09037 100644
--- a/networking-odl/networking_odl/common/callback.py
+++ b/networking-odl/networking_odl/common/callback.py
@@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
-import collections
-
from oslo_log import log as logging
from neutron.callbacks import events
@@ -25,49 +23,79 @@ from networking_odl.common import constants as odl_const
LOG = logging.getLogger(__name__)
-ODLResource = collections.namedtuple('ODLResource', ('singular', 'plural'))
-_RESOURCE_MAPPING = {
- resources.SECURITY_GROUP: ODLResource(odl_const.ODL_SG, odl_const.ODL_SGS),
- resources.SECURITY_GROUP_RULE: ODLResource(odl_const.ODL_SG_RULE,
- odl_const.ODL_SG_RULES),
-}
-_OPERATION_MAPPING = {
- events.AFTER_CREATE: odl_const.ODL_CREATE,
- events.AFTER_UPDATE: odl_const.ODL_UPDATE,
- events.AFTER_DELETE: odl_const.ODL_DELETE,
-}
-
class OdlSecurityGroupsHandler(object):
- def __init__(self, odl_driver):
- self.odl_driver = odl_driver
- self._subscribe()
+ def __init__(self, odl_client, event_type="AFTER"):
+ self.odl_client = odl_client
+ self.subscribe(event_type)
- def _subscribe(self):
- for event in (events.AFTER_CREATE, events.AFTER_DELETE):
- registry.subscribe(self.sg_callback, resources.SECURITY_GROUP,
- event)
- registry.subscribe(self.sg_callback, resources.SECURITY_GROUP_RULE,
- event)
+ def sg_callback(self, resource, event, trigger, **kwargs):
- registry.subscribe(self.sg_callback, resources.SECURITY_GROUP,
- events.AFTER_UPDATE)
+ res_key_mapping = {
+ odl_const.ODL_SGS: odl_const.ODL_SG,
+ odl_const.ODL_SG_RULES: odl_const.ODL_SG_RULE,
+ }
+ res_name_mapping = {
+ resources.SECURITY_GROUP: odl_const.ODL_SGS,
+ resources.SECURITY_GROUP_RULE: odl_const.ODL_SG_RULES,
+ }
+ ops_mapping = {
+ events.AFTER_CREATE: odl_const.ODL_CREATE,
+ events.AFTER_UPDATE: odl_const.ODL_UPDATE,
+ events.AFTER_DELETE: odl_const.ODL_DELETE,
+ events.PRECOMMIT_CREATE: odl_const.ODL_CREATE,
+ events.PRECOMMIT_UPDATE: odl_const.ODL_UPDATE,
+ events.PRECOMMIT_DELETE: odl_const.ODL_DELETE,
+ }
- def sg_callback(self, resource, event, trigger, **kwargs):
+ # Loop up the ODL's counterpart resource label
+ # e.g. resources.SECURITY_GROUP -> odl_const.ODL_SGS
+ # Note: 1) url will use dashes instead of underscore;
+ # 2) when res is a list, append 's' to odl_res_key
+ # Ref: https://github.com/opendaylight/neutron/blob/master
+ # /northbound-api/src/main/java/org/opendaylight
+ # /neutron/northbound/api
+ # /NeutronSecurityGroupRequest.java#L33
res = kwargs.get(resource)
res_id = kwargs.get("%s_id" % resource)
- odl_res_type = _RESOURCE_MAPPING[resource]
+ odl_res_type = res_name_mapping[resource]
+ odl_res_key = res_key_mapping[odl_res_type]
+ odl_ops = ops_mapping[event]
+ odl_res_type_uri = odl_res_type.replace('_', '-')
+
+ if type(res) is list:
+ odl_res_key += "s"
- odl_ops = _OPERATION_MAPPING[event]
- odl_res_dict = None if res is None else {odl_res_type.singular: res}
+ if res is None:
+ odl_res_dict = None
+ else:
+ odl_res_dict = {odl_res_key: res}
LOG.debug("Calling sync_from_callback with ODL_OPS (%(odl_ops)s) "
"ODL_RES_TYPE (%(odl_res_type)s) RES_ID (%(res_id)s) "
- "ODL_RES_DICT (%(odl_res_dict)s) KWARGS (%(kwargs)s)",
+ "ODL_RES_KEY (%(odl_res_key)s) RES (%(res)s) "
+ "KWARGS (%(kwargs)s)",
{'odl_ops': odl_ops, 'odl_res_type': odl_res_type,
- 'res_id': res_id, 'odl_res_dict': odl_res_dict,
+ 'res_id': res_id, 'odl_res_key': odl_res_key, 'res': res,
'kwargs': kwargs})
- self.odl_driver.sync_from_callback(odl_ops, odl_res_type,
- res_id, odl_res_dict)
+ self.odl_client.sync_from_callback(odl_ops, odl_res_type_uri, res_id,
+ odl_res_dict)
+
+ def subscribe(self, event_type):
+ registry.subscribe(
+ self.sg_callback, resources.SECURITY_GROUP,
+ getattr(events, "%s_CREATE" % event_type))
+ registry.subscribe(
+ self.sg_callback, resources.SECURITY_GROUP,
+ getattr(events, "%s_UPDATE" % event_type))
+ registry.subscribe(
+ self.sg_callback, resources.SECURITY_GROUP,
+ getattr(events, "%s_DELETE" % event_type))
+ registry.subscribe(
+ self.sg_callback, resources.SECURITY_GROUP_RULE,
+ getattr(events, "%s_CREATE" % event_type))
+ registry.subscribe(
+ self.sg_callback, resources.SECURITY_GROUP_RULE,
+ getattr(events, "%s_DELETE" % event_type))