1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 sample router manager.
19 (un-)instantiate routers
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
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
33 class RouterManager(app_manager.RyuApp):
35 vrrp_event.VRRPInterfaceNetworkDevice: {
36 4: sample_router.RouterIPV4Linux,
37 6: sample_router.RouterIPV6Linux,
39 vrrp_event.VRRPInterfaceOpenFlow: {
40 4: sample_router.RouterIPV4OpenFlow,
41 6: sample_router.RouterIPV6OpenFlow,
45 def __init__(self, *args, **kwargs):
46 super(RouterManager, self).__init__(*args, **kwargs)
49 self.routers = {} # instance name -> router name
51 def _router_factory(self, instance_name, monitor_name, interface, config):
53 for interface_cls, router_clses in self._ROUTER_CLASSES.items():
54 if isinstance(interface, interface_cls):
61 self.logger.debug('interface %s %s', type(interface), interface)
62 self.logger.debug('cls %s', cls)
64 raise ValueError('Unknown interface type %s %s' % (type(interface),
66 kwargs = self._kwargs.copy()
68 'name': instance_name,
69 'monitor_name': monitor_name,
71 'interface': interface,
73 app_mgr = app_manager.AppManager.get_instance()
74 return app_mgr.instantiate(cls, *self._args, **kwargs)
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:
85 router_name = self.routers.get(ev.instance_name)
86 self.send_event(router_name, ev)
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)
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)