backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / controller / mac_to_port.py
1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co 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 from ryu.lib.mac import haddr_to_str
19
20 LOG = logging.getLogger('ryu.controller.mac_to_port')
21
22
23 class MacToPortTable(object):
24     """MAC addr <-> (dpid, port name)"""
25
26     def __init__(self):
27         super(MacToPortTable, self).__init__()
28         self.mac_to_port = {}
29
30     def dpid_add(self, dpid):
31         LOG.debug('dpid_add: 0x%016x', dpid)
32         self.mac_to_port.setdefault(dpid, {})
33
34     def port_add(self, dpid, port, mac):
35         """
36         :returns: old port if learned. (this may be = port)
37                   None otherwise
38         """
39         old_port = self.mac_to_port[dpid].get(mac, None)
40         self.mac_to_port[dpid][mac] = port
41
42         if old_port is not None and old_port != port:
43             LOG.debug('port_add: 0x%016x 0x%04x %s',
44                       dpid, port, haddr_to_str(mac))
45
46         return old_port
47
48     def port_get(self, dpid, mac):
49         # LOG.debug('dpid 0x%016x mac %s', dpid, haddr_to_str(mac))
50         return self.mac_to_port[dpid].get(mac)
51
52     def mac_list(self, dpid, port):
53         return [mac for (mac, port_) in self.mac_to_port.get(dpid).items()
54                 if port_ == port]
55
56     def mac_del(self, dpid, mac):
57         del self.mac_to_port[dpid][mac]