backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / zebra / event.py
1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Events for Zebra protocol service.
18 """
19
20 import inspect
21 import logging
22
23 from ryu import utils
24 from ryu.controller import event
25 from ryu.lib.packet import zebra
26
27
28 LOG = logging.getLogger(__name__)
29 MOD = utils.import_module(__name__)
30
31 ZEBRA_EVENTS = []
32
33
34 class EventZebraBase(event.EventBase):
35     """
36     The base class for Zebra protocol service event class.
37
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`.
43
44     The subclasses are named as::
45
46         ``"Event" + <Zebra message body class name>``
47
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::
52
53         "Event" + "ZebraInterfaceAdd" = "EventZebraInterfaceAdd"
54
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.
58     """
59
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
65
66     def __repr__(self):
67         m = ', '.join(
68             ['%s=%r' % (k, v)
69              for k, v in self.__dict__.items() if not k.startswith('_')])
70         return "%s(%s)" % (self.__class__.__name__, m)
71
72     __str__ = __repr__
73
74
75 def _event_name(body_cls):
76     return 'Event%s' % body_cls.__name__
77
78
79 def message_to_event(zclient, msg):
80     """
81     Converts Zebra protocol message instance to Zebra protocol service
82     event instance.
83
84     If corresponding event class is not defined, returns None.
85
86     :param zclient: Zebra client instance.
87     :param msg: Zebra protocol message.
88     :return: Zebra protocol service event.
89     """
90     if not isinstance(msg, zebra.ZebraMessage):
91         return None
92
93     body_cls = msg.get_body_class(msg.version, msg.command)
94     ev_cls = getattr(MOD, _event_name(body_cls), None)
95     if ev_cls is None:
96         return None
97
98     return ev_cls(zclient, msg)
99
100
101 def _define_event_class(body_cls):
102     name = _event_name(body_cls)
103
104     event_cls = type(name, (EventZebraBase,), {})
105     globals()[name] = event_cls
106
107     return event_cls
108
109
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('_')):
115             continue
116
117         ev = _define_event_class(zebra_cls)
118         # LOG.debug('Generated Zebra event: %s' % ev)
119         ZEBRA_EVENTS.append(ev)
120
121
122 _generate_event_classes()