1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Events for Zebra protocol service.
24 from ryu.controller import event
25 from ryu.lib.packet import zebra
28 LOG = logging.getLogger(__name__)
29 MOD = utils.import_module(__name__)
34 class EventZebraBase(event.EventBase):
36 The base class for Zebra protocol service event class.
38 The subclasses have at least ``zclient`` and the same attributes with
39 :py:class: `ryu.lib.packet.zebra.ZebraMessage`.
40 ``zclient`` is an instance of Zebra client class. See
41 :py:class: `ryu.services.protocols.zebra.client.zclient.ZClient` or
42 :py:class: `ryu.services.protocols.zebra.server.zserver.ZClient`.
44 The subclasses are named as::
46 ``"Event" + <Zebra message body class name>``
48 For Example, if the service received ZEBRA_INTERFACE_ADD message,
49 the body class should be
50 :py:class: `ryu.lib.packet.zebra.ZebraInterfaceAdd`, then the event
51 class will be named as::
53 "Event" + "ZebraInterfaceAdd" = "EventZebraInterfaceAdd"
55 ``msg`` argument must be an instance of
56 :py:class: `ryu.lib.packet.zebra.ZebraMessage` and used to extract the
57 attributes for the event classes.
60 def __init__(self, zclient, msg):
61 super(EventZebraBase, self).__init__()
62 assert isinstance(msg, zebra.ZebraMessage)
63 self.__dict__ = msg.__dict__
64 self.zclient = zclient
69 for k, v in self.__dict__.items() if not k.startswith('_')])
70 return "%s(%s)" % (self.__class__.__name__, m)
75 def _event_name(body_cls):
76 return 'Event%s' % body_cls.__name__
79 def message_to_event(zclient, msg):
81 Converts Zebra protocol message instance to Zebra protocol service
84 If corresponding event class is not defined, returns None.
86 :param zclient: Zebra client instance.
87 :param msg: Zebra protocol message.
88 :return: Zebra protocol service event.
90 if not isinstance(msg, zebra.ZebraMessage):
93 body_cls = msg.get_body_class(msg.version, msg.command)
94 ev_cls = getattr(MOD, _event_name(body_cls), None)
98 return ev_cls(zclient, msg)
101 def _define_event_class(body_cls):
102 name = _event_name(body_cls)
104 event_cls = type(name, (EventZebraBase,), {})
105 globals()[name] = event_cls
110 def _generate_event_classes():
111 for zebra_cls in zebra.__dict__.values():
112 if (not inspect.isclass(zebra_cls)
113 or not issubclass(zebra_cls, zebra._ZebraMessageBody)
114 or zebra_cls.__name__.startswith('_')):
117 ev = _define_event_class(zebra_cls)
118 # LOG.debug('Generated Zebra event: %s' % ev)
119 ZEBRA_EVENTS.append(ev)
122 _generate_event_classes()