backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / controller / conf_switch.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2012 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 import logging
18
19 from ryu.controller import event
20 from ryu.lib.dpid import dpid_to_str
21 from ryu.base import app_manager
22
23 LOG = logging.getLogger(__name__)
24
25
26 class EventConfSwitchDelDPID(event.EventBase):
27     def __init__(self, dpid):
28         super(EventConfSwitchDelDPID, self).__init__()
29         self.dpid = dpid
30
31     def __str__(self):
32         return 'EventConfSwitchDelDPID<%s>' % dpid_to_str(self.dpid)
33
34
35 class EventConfSwitchSet(event.EventBase):
36     def __init__(self, dpid, key, value):
37         super(EventConfSwitchSet, self).__init__()
38         self.dpid = dpid
39         self.key = key
40         self.value = value
41
42     def __str__(self):
43         return 'EventConfSwitchSet<%s, %s, %s>' % (
44             dpid_to_str(self.dpid), self.key, self.value)
45
46
47 class EventConfSwitchDel(event.EventBase):
48     def __init__(self, dpid, key):
49         super(EventConfSwitchDel, self).__init__()
50         self.dpid = dpid
51         self.key = key
52
53     def __str__(self):
54         return 'EventConfSwitchDel<%s, %s>' % (dpid_to_str(self.dpid),
55                                                self.key)
56
57
58 class ConfSwitchSet(app_manager.RyuApp):
59     def __init__(self):
60         super(ConfSwitchSet, self).__init__()
61         self.name = 'conf_switch'
62         self.confs = {}
63
64     def dpids(self):
65         return list(self.confs.keys())
66
67     def del_dpid(self, dpid):
68         del self.confs[dpid]
69         self.send_event_to_observers(EventConfSwitchDelDPID(dpid))
70
71     def keys(self, dpid):
72         return list(self.confs[dpid].keys())
73
74     def set_key(self, dpid, key, value):
75         conf = self.confs.setdefault(dpid, {})
76         conf[key] = value
77         self.send_event_to_observers(EventConfSwitchSet(dpid, key, value))
78
79     def get_key(self, dpid, key):
80         return self.confs[dpid][key]
81
82     def del_key(self, dpid, key):
83         del self.confs[dpid][key]
84         self.send_event_to_observers(EventConfSwitchDel(dpid, key))
85
86     # methods for TunnelUpdater
87     def __contains__(self, item):
88         """(dpid, key) in <ConfSwitchSet instance>"""
89         (dpid, key) = item
90         return dpid in self.confs and key in self.confs[dpid]
91
92     def find_dpid(self, key, value):
93         for dpid, conf in self.confs.items():
94             if key in conf:
95                 if conf[key] == value:
96                     return dpid
97
98         return None