backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / controller / mac_to_network.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
19 from ryu.exception import MacAddressDuplicated
20 from ryu.lib.mac import haddr_to_str
21
22 LOG = logging.getLogger('ryu.controller.mac_to_network')
23
24
25 class MacToNetwork(object):
26     def __init__(self, nw):
27         super(MacToNetwork, self).__init__()
28         self.mac_to_net = {}
29         self.dpid = {}
30         self.nw = nw
31
32     def get_network(self, mac, default=None):
33         return self.mac_to_net.get(mac, default)
34
35     def add_mac(self, mac, nw_id, nw_id_external=None):
36         _nw_id = self.mac_to_net.get(mac)
37         if _nw_id == nw_id:
38             return
39
40         # allow changing from nw_id_external to known nw id
41         if _nw_id is None or _nw_id == nw_id_external:
42             self.mac_to_net[mac] = nw_id
43             LOG.debug('overwrite nw_id: mac %s nw old %s new %s',
44                       haddr_to_str(mac), _nw_id, nw_id)
45             return
46
47         if nw_id == nw_id_external:
48             # this can happens when the packet traverses
49             # VM-> tap-> ovs-> ext-port-> wire-> ext-port-> ovs-> tap-> VM
50             return
51
52         LOG.warning('duplicated nw_id: mac %s nw old %s new %s',
53                     haddr_to_str(mac), _nw_id, nw_id)
54
55         raise MacAddressDuplicated(mac=mac)
56
57     def del_mac(self, mac):
58         del self.mac_to_net[mac]