backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / addrconv.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 YAMAMOTO Takashi <yamamoto 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 netaddr
18
19
20 class AddressConverter(object):
21     def __init__(self, addr, strat, fallback=None, **kwargs):
22         self._addr = addr
23         self._strat = strat
24         self._fallback = fallback
25         self._addr_kwargs = kwargs
26
27     def text_to_bin(self, text):
28         try:
29             return self._addr(text, **self._addr_kwargs).packed
30         except Exception as e:
31             if self._fallback is None:
32                 raise e
33
34             # text_to_bin is expected to return binary string under
35             # normal circumstances. See ofproto.oxx_fields._from_user.
36             ip = self._fallback(text, **self._addr_kwargs)
37             return ip.ip.packed, ip.netmask.packed
38
39     def bin_to_text(self, bin):
40         return str(self._addr(self._strat.packed_to_int(bin),
41                               **self._addr_kwargs))
42
43
44 ipv4 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv4,
45                         fallback=netaddr.IPNetwork, version=4)
46 ipv6 = AddressConverter(netaddr.IPAddress, netaddr.strategy.ipv6,
47                         fallback=netaddr.IPNetwork, version=6)
48
49
50 class mac_mydialect(netaddr.mac_unix):
51     word_fmt = '%.2x'
52
53
54 mac = AddressConverter(netaddr.EUI, netaddr.strategy.eui48, version=48,
55                        dialect=mac_mydialect)