backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / vrrp / sample_manager.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """
18 sample router manager.
19 (un-)instantiate routers
20 Usage example:
21 PYTHONPATH=. ./bin/ryu-manager --verbose \
22              ryu.services.protocols.vrrp.manager \
23              ryu.services.protocols.vrrp.dumper \
24              ryu.services.protocols.vrrp.sample_manager
25 """
26
27 from ryu.base import app_manager
28 from ryu.controller import handler
29 from ryu.services.protocols.vrrp import event as vrrp_event
30 from ryu.services.protocols.vrrp import sample_router
31
32
33 class RouterManager(app_manager.RyuApp):
34     _ROUTER_CLASSES = {
35         vrrp_event.VRRPInterfaceNetworkDevice: {
36             4: sample_router.RouterIPV4Linux,
37             6: sample_router.RouterIPV6Linux,
38         },
39         vrrp_event.VRRPInterfaceOpenFlow: {
40             4: sample_router.RouterIPV4OpenFlow,
41             6: sample_router.RouterIPV6OpenFlow,
42         },
43     }
44
45     def __init__(self, *args, **kwargs):
46         super(RouterManager, self).__init__(*args, **kwargs)
47         self._args = args
48         self._kwargs = kwargs
49         self.routers = {}  # instance name -> router name
50
51     def _router_factory(self, instance_name, monitor_name, interface, config):
52         cls = None
53         for interface_cls, router_clses in self._ROUTER_CLASSES.items():
54             if isinstance(interface, interface_cls):
55                 if config.is_ipv6:
56                     cls = router_clses[6]
57                 else:
58                     cls = router_clses[4]
59                 break
60
61         self.logger.debug('interface %s %s', type(interface), interface)
62         self.logger.debug('cls %s', cls)
63         if cls is None:
64             raise ValueError('Unknown interface type %s %s' % (type(interface),
65                                                                interface))
66         kwargs = self._kwargs.copy()
67         kwargs.update({
68             'name': instance_name,
69             'monitor_name': monitor_name,
70             'config': config,
71             'interface': interface,
72         })
73         app_mgr = app_manager.AppManager.get_instance()
74         return app_mgr.instantiate(cls, *self._args, **kwargs)
75
76     @handler.set_ev_cls(vrrp_event.EventVRRPStateChanged)
77     def vrrp_state_changed_handler(self, ev):
78         if ev.new_state == vrrp_event.VRRP_STATE_INITIALIZE:
79             if ev.old_state:
80                 self._shutdown(ev)
81             else:
82                 self._initialize(ev)
83             return
84
85         router_name = self.routers.get(ev.instance_name)
86         self.send_event(router_name, ev)
87
88     def _initialize(self, ev):
89         router = self._router_factory(ev.instance_name, ev.monitor_name,
90                                       ev.interface, ev.config)
91         self.routers[ev.instance_name] = router.name
92         self.send_event(router.name, ev)
93         router.start()
94
95     def _shutdown(self, ev):
96         router_name = self.routers.pop(ev.instance_name)
97         self.send_event(router_name, ev)
98         app_mgr = app_manager.AppManager.get_instance()
99         app_mgr.uninstantiate(router_name)