api: split vl_api_prefix into two
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_format.py
1 #
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:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 #
15 import datetime
16 from socket import inet_pton, AF_INET6, AF_INET
17 import socket
18 import ipaddress
19 from . import macaddress
20
21 try:
22     text_type = unicode
23 except NameError:
24     text_type = str
25
26 # Copies from vl_api_address_t definition
27 ADDRESS_IP4 = 0
28 ADDRESS_IP6 = 1
29
30 #
31 # Type conversion for input arguments and return values
32 #
33
34
35 def format_vl_api_address_t(args):
36     try:
37         return {'un': {'ip6': inet_pton(AF_INET6, args)},
38                 'af': ADDRESS_IP6}
39     # PY2: raises socket.error
40     # PY3: raises OSError
41     except (socket.error, OSError):
42         return {'un': {'ip4': inet_pton(AF_INET, args)},
43                 'af': ADDRESS_IP4}
44
45
46 def format_vl_api_prefix_t(args):
47     if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)):
48         return {'address': format_vl_api_address_t(
49             text_type(args.network_address)),
50                 'len': int(args.prefixlen)}
51     p, length = args.split('/')
52     return {'address': format_vl_api_address_t(p),
53             'len': int(length)}
54
55 def format_vl_api_address_with_prefix_t(args):
56     if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)):
57         return {'address': format_vl_api_address_t(
58             text_type(args.network_address)),
59                 'len': int(args.prefixlen)}
60     p, length = args.split('/')
61     return {'address': format_vl_api_address_t(p),
62             'len': int(length)}
63
64
65 def format_vl_api_ip6_prefix_t(args):
66     if isinstance(args, ipaddress.IPv6Network):
67         return {'address': args.network_address.packed,
68                 'len': int(args.prefixlen)}
69     p, length = args.split('/')
70     return {'address': inet_pton(AF_INET6, p),
71             'len': int(length)}
72
73 def format_vl_api_ip6_address_with_prefix_t(args):
74     if isinstance(args, ipaddress.IPv6Interface):
75         return {'address': args.network_address.packed,
76                 'len': int(args.prefixlen)}
77     p, length = args.split('/')
78     return {'address': inet_pton(AF_INET6, p),
79             'len': int(length)}
80
81 def format_vl_api_ip4_prefix_t(args):
82     if isinstance(args, ipaddress.IPv4Network):
83         return {'address': args.network_address.packed,
84                 'len': int(args.prefixlen)}
85     p, length = args.split('/')
86     return {'address': inet_pton(AF_INET, p),
87             'len': int(length)}
88
89 def format_vl_api_ip4_address_with_prefix_t(args):
90     if isinstance(args, ipaddress.IPv4Interface):
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),
95             'len': int(length)}
96
97
98 conversion_table = {
99     'vl_api_ip6_address_t':
100     {
101         'IPv6Address': lambda o: o.packed,
102         'str': lambda s: inet_pton(AF_INET6, s)
103     },
104     'vl_api_ip4_address_t':
105     {
106         'IPv4Address': lambda o: o.packed,
107         'str': lambda s: inet_pton(AF_INET, s)
108     },
109     'vl_api_ip6_prefix_t':
110     {
111         'IPv6Network': lambda o: {'address': o.network_address.packed,
112                                   'len': o.prefixlen},
113         'str': lambda s: format_vl_api_ip6_prefix_t(s)
114     },
115     'vl_api_ip4_prefix_t':
116     {
117         'IPv4Network': lambda o: {'address': o.network_address.packed,
118                                   'len': o.prefixlen},
119         'str': lambda s: format_vl_api_ip4_prefix_t(s)
120     },
121     'vl_api_address_t':
122     {
123         'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
124         'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
125         'str': lambda s: format_vl_api_address_t(s)
126     },
127     'vl_api_prefix_t':
128     {
129         'IPv4Network': lambda o: {'address':
130                                   {'af': ADDRESS_IP4, 'un':
131                                    {'ip4': o.network_address.packed}},
132                                   'len': o.prefixlen},
133         'IPv6Network': lambda o: {'address':
134                                   {'af': ADDRESS_IP6, 'un':
135                                    {'ip6': o.network_address.packed}},
136                                   'len': o.prefixlen},
137         'str': lambda s: format_vl_api_prefix_t(s)
138     },
139     'vl_api_address_with_prefix_t':
140     {
141         'IPv4Interface': lambda o: {'address':
142                                   {'af': ADDRESS_IP4, 'un':
143                                    {'ip4': o.packed}},
144                                   'len': o.network.prefixlen},
145         'IPv6Interface': lambda o: {'address':
146                                   {'af': ADDRESS_IP6, 'un':
147                                    {'ip6': o.packed}},
148                                   'len': o.network.prefixlen},
149         'str': lambda s: format_vl_api_address_with_prefix_t(s)
150     },
151     'vl_api_ip4_address_with_prefix_t':
152     {
153         'IPv4Interface': lambda o: {'address': o.packed,
154                                     'len': o.network.prefixlen},
155         'str': lambda s: format_vl_api_ip4_address_with_prefix_t(s)
156     },
157     'vl_api_ip6_address_with_prefix_t':
158     {
159         'IPv6Interface': lambda o: {'address': o.packed,
160                                   'len': o.network.prefixlen},
161         'str': lambda s: format_vl_api_ip6_address_with_prefix_t(s)
162     },
163     'vl_api_mac_address_t':
164     {
165         'MACAddress': lambda o: o.packed,
166         'str': lambda s: macaddress.mac_pton(s)
167     },
168     'vl_api_timestamp_t':
169     {
170         'datetime.datetime': lambda o:
171         (o - datetime.datetime(1970, 1, 1)).total_seconds()
172     }
173 }
174
175
176 def unformat_api_address_t(o):
177     if o.af == 1:
178         return ipaddress.IPv6Address(o.un.ip6)
179     if o.af == 0:
180         return ipaddress.IPv4Address(o.un.ip4)
181     raise ValueError('Unknown address family {}'.format(o))
182
183 def unformat_api_prefix_t(o):
184     if o.address.af == 1:
185         return ipaddress.IPv6Network((o.address.un.ip6, o.len), False)
186     if o.address.af == 0:
187         return ipaddress.IPv4Network((o.address.un.ip4, o.len), False)
188     raise ValueError('Unknown address family {}'.format(o))
189
190     if isinstance(o.address, ipaddress.IPv4Address):
191         return ipaddress.IPv4Network((o.address, o.len), False)
192     if isinstance(o.address, ipaddress.IPv6Address):
193         return ipaddress.IPv6Network((o.address, o.len), False)
194     raise ValueError('Unknown instance {}', format(o))
195
196 def unformat_api_address_with_prefix_t(o):
197     if o.address.af == 1:
198         return ipaddress.IPv6Interface((o.address.un.ip6, o.len))
199     if o.address.af == 0:
200         return ipaddress.IPv4Interface((o.address.un.ip4, o.len))
201     raise ValueError('Unknown address family {}'.format(o))
202
203 def unformat_api_ip4_address_with_prefix_t(o):
204     return ipaddress.IPv4Interface((o.address, o.len))
205
206 def unformat_api_ip6_address_with_prefix_t(o):
207     return ipaddress.IPv6Interface((o.address, o.len))
208
209 conversion_unpacker_table = {
210     'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
211     'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)),
212     'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
213     'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)),
214     'vl_api_address_t': lambda o: unformat_api_address_t(o),
215     'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
216     'vl_api_address_with_prefix_t': lambda o: unformat_api_address_with_prefix_t(o),
217     'vl_api_ip4_address_with_prefix_t': lambda o: unformat_api_ip4_address_with_prefix_t(o),
218     'vl_api_ip6_address_with_prefix_t': lambda o: unformat_api_ip6_address_with_prefix_t(o),
219     'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),
220     'vl_api_timestamp_t': lambda o: datetime.datetime.fromtimestamp(o),
221     'vl_api_timedelta_t': lambda o: datetime.timedelta(seconds=o),
222 }