1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 YAMAMOTO Takashi <yamamoto 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.
20 class AddressConverter(object):
21 def __init__(self, addr, strat, fallback=None, **kwargs):
24 self._fallback = fallback
25 self._addr_kwargs = kwargs
27 def text_to_bin(self, text):
29 return self._addr(text, **self._addr_kwargs).packed
30 except Exception as e:
31 if self._fallback is None:
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
39 def bin_to_text(self, bin):
40 return str(self._addr(self._strat.packed_to_int(bin),
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)
50 class mac_mydialect(netaddr.mac_unix):
54 mac = AddressConverter(netaddr.EUI, netaddr.strategy.eui48, version=48,
55 dialect=mac_mydialect)