2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from socket import inet_pton, AF_INET6, AF_INET
19 from . import macaddress
26 # Copies from vl_api_address_t definition
31 def verify_enum_hint(e):
32 return (e.ADDRESS_IP4.value == ADDRESS_IP4) and\
33 (e.ADDRESS_IP6.value == ADDRESS_IP6)
36 # Type conversion for input arguments and return values
40 def format_vl_api_address_t(args):
42 return {'un': {'ip6': inet_pton(AF_INET6, args)},
44 # PY2: raises socket.error
46 except (socket.error, OSError):
47 return {'un': {'ip4': inet_pton(AF_INET, args)},
51 def format_vl_api_prefix_t(args):
52 if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
53 return {'address': format_vl_api_address_t(
54 text_type(args.network_address)),
55 'len': int(args.prefixlen)}
56 p, length = args.split('/')
57 return {'address': format_vl_api_address_t(p),
61 def format_vl_api_address_with_prefix_t(args):
62 if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)):
63 return {'address': format_vl_api_address_t(
64 text_type(args.network_address)),
65 'len': int(args.prefixlen)}
66 p, length = args.split('/')
67 return {'address': format_vl_api_address_t(p),
71 def format_vl_api_ip6_prefix_t(args):
72 if isinstance(args, ipaddress.IPv6Network):
73 return {'address': args.network_address.packed,
74 'len': int(args.prefixlen)}
75 p, length = args.split('/')
76 return {'address': inet_pton(AF_INET6, p),
80 def format_vl_api_ip6_address_with_prefix_t(args):
81 if isinstance(args, ipaddress.IPv6Interface):
82 return {'address': args.network_address.packed,
83 'len': int(args.prefixlen)}
84 p, length = args.split('/')
85 return {'address': inet_pton(AF_INET6, p),
89 def format_vl_api_ip4_prefix_t(args):
90 if isinstance(args, ipaddress.IPv4Network):
91 return {'address': args.network_address.packed,
92 'len': int(args.prefixlen)}
93 p, length = args.split('/')
94 return {'address': inet_pton(AF_INET, p),
98 def format_vl_api_ip4_address_with_prefix_t(args):
99 if isinstance(args, ipaddress.IPv4Interface):
100 return {'address': args.network_address.packed,
101 'len': int(args.prefixlen)}
102 p, length = args.split('/')
103 return {'address': inet_pton(AF_INET, p),
108 'vl_api_ip6_address_t':
110 'IPv6Address': lambda o: o.packed,
111 'str': lambda s: inet_pton(AF_INET6, s)
113 'vl_api_ip4_address_t':
115 'IPv4Address': lambda o: o.packed,
116 'str': lambda s: inet_pton(AF_INET, s)
118 'vl_api_ip6_prefix_t':
120 'IPv6Network': lambda o: {'address': o.network_address.packed,
122 'str': lambda s: format_vl_api_ip6_prefix_t(s)
124 'vl_api_ip4_prefix_t':
126 'IPv4Network': lambda o: {'address': o.network_address.packed,
128 'str': lambda s: format_vl_api_ip4_prefix_t(s)
132 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
133 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
134 'str': lambda s: format_vl_api_address_t(s)
138 'IPv4Network': lambda o: {'address':
139 {'af': ADDRESS_IP4, 'un':
140 {'ip4': o.network_address.packed}},
142 'IPv6Network': lambda o: {'address':
143 {'af': ADDRESS_IP6, 'un':
144 {'ip6': o.network_address.packed}},
146 'str': lambda s: format_vl_api_prefix_t(s)
148 'vl_api_address_with_prefix_t':
150 'IPv4Interface': lambda o: {'address':
151 {'af': ADDRESS_IP4, 'un':
153 'len': o.network.prefixlen},
154 'IPv6Interface': lambda o: {'address':
155 {'af': ADDRESS_IP6, 'un':
157 'len': o.network.prefixlen},
158 'str': lambda s: format_vl_api_address_with_prefix_t(s)
160 'vl_api_ip4_address_with_prefix_t':
162 'IPv4Interface': lambda o: {'address': o.packed,
163 'len': o.network.prefixlen},
164 'str': lambda s: format_vl_api_ip4_address_with_prefix_t(s)
166 'vl_api_ip6_address_with_prefix_t':
168 'IPv6Interface': lambda o: {'address': o.packed,
169 'len': o.network.prefixlen},
170 'str': lambda s: format_vl_api_ip6_address_with_prefix_t(s)
172 'vl_api_mac_address_t':
174 'MACAddress': lambda o: o.packed,
175 'str': lambda s: macaddress.mac_pton(s)
177 'vl_api_timestamp_t':
179 'datetime.datetime': lambda o:
180 (o - datetime.datetime(1970, 1, 1)).total_seconds()
185 def unformat_api_address_t(o):
187 return ipaddress.IPv6Address(o.un.ip6)
189 return ipaddress.IPv4Address(o.un.ip4)
193 def unformat_api_prefix_t(o):
194 if o.address.af == 1:
195 return ipaddress.IPv6Network((o.address.un.ip6, o.len), False)
196 if o.address.af == 0:
197 return ipaddress.IPv4Network((o.address.un.ip4, o.len), False)
200 if isinstance(o.address, ipaddress.IPv4Address):
201 return ipaddress.IPv4Network((o.address, o.len), False)
202 if isinstance(o.address, ipaddress.IPv6Address):
203 return ipaddress.IPv6Network((o.address, o.len), False)
204 raise ValueError('Unknown instance {}', format(o))
207 def unformat_api_address_with_prefix_t(o):
208 if o.address.af == 1:
209 return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
210 if o.address.af == 0:
211 return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
215 def unformat_api_ip4_address_with_prefix_t(o):
216 return ipaddress.IPv4Interface((o.address, o.len))
219 def unformat_api_ip6_address_with_prefix_t(o):
220 return ipaddress.IPv6Interface((o.address, o.len))
223 conversion_unpacker_table = {
224 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
225 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)),
226 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
227 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)),
228 'vl_api_address_t': lambda o: unformat_api_address_t(o),
229 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
230 'vl_api_address_with_prefix_t': lambda o: unformat_api_address_with_prefix_t(o),
231 'vl_api_ip4_address_with_prefix_t': lambda o: unformat_api_ip4_address_with_prefix_t(o),
232 'vl_api_ip6_address_with_prefix_t': lambda o: unformat_api_ip6_address_with_prefix_t(o),
233 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),
234 'vl_api_timestamp_t': lambda o: datetime.datetime.fromtimestamp(o),
235 'vl_api_timedelta_t': lambda o: datetime.timedelta(seconds=o),