1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2012 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.
19 from ryu.controller import event
20 from ryu.lib.dpid import dpid_to_str
21 from ryu.base import app_manager
23 LOG = logging.getLogger(__name__)
26 class EventConfSwitchDelDPID(event.EventBase):
27 def __init__(self, dpid):
28 super(EventConfSwitchDelDPID, self).__init__()
32 return 'EventConfSwitchDelDPID<%s>' % dpid_to_str(self.dpid)
35 class EventConfSwitchSet(event.EventBase):
36 def __init__(self, dpid, key, value):
37 super(EventConfSwitchSet, self).__init__()
43 return 'EventConfSwitchSet<%s, %s, %s>' % (
44 dpid_to_str(self.dpid), self.key, self.value)
47 class EventConfSwitchDel(event.EventBase):
48 def __init__(self, dpid, key):
49 super(EventConfSwitchDel, self).__init__()
54 return 'EventConfSwitchDel<%s, %s>' % (dpid_to_str(self.dpid),
58 class ConfSwitchSet(app_manager.RyuApp):
60 super(ConfSwitchSet, self).__init__()
61 self.name = 'conf_switch'
65 return list(self.confs.keys())
67 def del_dpid(self, dpid):
69 self.send_event_to_observers(EventConfSwitchDelDPID(dpid))
72 return list(self.confs[dpid].keys())
74 def set_key(self, dpid, key, value):
75 conf = self.confs.setdefault(dpid, {})
77 self.send_event_to_observers(EventConfSwitchSet(dpid, key, value))
79 def get_key(self, dpid, key):
80 return self.confs[dpid][key]
82 def del_key(self, dpid, key):
83 del self.confs[dpid][key]
84 self.send_event_to_observers(EventConfSwitchDel(dpid, key))
86 # methods for TunnelUpdater
87 def __contains__(self, item):
88 """(dpid, key) in <ConfSwitchSet instance>"""
90 return dpid in self.confs and key in self.confs[dpid]
92 def find_dpid(self, key, value):
93 for dpid, conf in self.confs.items():
95 if conf[key] == value: