1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co 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 from ryu.lib.mac import haddr_to_str
20 LOG = logging.getLogger('ryu.controller.mac_to_port')
23 class MacToPortTable(object):
24 """MAC addr <-> (dpid, port name)"""
27 super(MacToPortTable, self).__init__()
30 def dpid_add(self, dpid):
31 LOG.debug('dpid_add: 0x%016x', dpid)
32 self.mac_to_port.setdefault(dpid, {})
34 def port_add(self, dpid, port, mac):
36 :returns: old port if learned. (this may be = port)
39 old_port = self.mac_to_port[dpid].get(mac, None)
40 self.mac_to_port[dpid][mac] = port
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))
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)
52 def mac_list(self, dpid, port):
53 return [mac for (mac, port_) in self.mac_to_port.get(dpid).items()
56 def mac_del(self, dpid, mac):
57 del self.mac_to_port[dpid][mac]