papi: fix copy/paste error
[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 def verify_enum_hint(e):
32     return (e.ADDRESS_IP4.value == ADDRESS_IP4) and\
33            (e.ADDRESS_IP6.value == ADDRESS_IP6)
34
35 #
36 # Type conversion for input arguments and return values
37 #
38
39
40 def format_vl_api_address_t(args):
41     try:
42         return {'un': {'ip6': inet_pton(AF_INET6, args)},
43                 'af': ADDRESS_IP6}
44     # PY2: raises socket.error
45     # PY3: raises OSError
46     except (socket.error, OSError):
47         return {'un': {'ip4': inet_pton(AF_INET, args)},
48                 'af': ADDRESS_IP4}
49
50
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),
58             'len': int(length)}
59
60
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),
68             'len': int(length)}
69
70
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),
77             'len': int(length)}
78
79
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),
86             'len': int(length)}
87
88
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),
95             'len': int(length)}
96
97
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),
104             'len': int(length)}
105
106
107 conversion_table = {
108     'vl_api_ip6_address_t':
109     {
110         'IPv6Address': lambda o: o.packed,
111         'str': lambda s: inet_pton(AF_INET6, s)
112     },
113     'vl_api_ip4_address_t':
114     {
115         'IPv4Address': lambda o: o.packed,
116         'str': lambda s: inet_pton(AF_INET, s)
117     },
118     'vl_api_ip6_prefix_t':
119     {
120         'IPv6Network': lambda o: {'address': o.network_address.packed,
121                                   'len': o.prefixlen},
122         'str': lambda s: format_vl_api_ip6_prefix_t(s)
123     },
124     'vl_api_ip4_prefix_t':
125     {
126         'IPv4Network': lambda o: {'address': o.network_address.packed,
127                                   'len': o.prefixlen},
128         'str': lambda s: format_vl_api_ip4_prefix_t(s)
129     },
130     'vl_api_address_t':
131     {
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)
135     },
136     'vl_api_prefix_t':
137     {
138         'IPv4Network': lambda o: {'address':
139                                   {'af': ADDRESS_IP4, 'un':
140                                    {'ip4': o.network_address.packed}},
141                                   'len': o.prefixlen},
142         'IPv6Network': lambda o: {'address':
143                                   {'af': ADDRESS_IP6, 'un':
144                                    {'ip6': o.network_address.packed}},
145                                   'len': o.prefixlen},
146         'str': lambda s: format_vl_api_prefix_t(s)
147     },
148     'vl_api_address_with_prefix_t':
149     {
150         'IPv4Interface': lambda o: {'address':
151                                     {'af': ADDRESS_IP4, 'un':
152                                      {'ip4': o.packed}},
153                                     'len': o.network.prefixlen},
154         'IPv6Interface': lambda o: {'address':
155                                     {'af': ADDRESS_IP6, 'un':
156                                      {'ip6': o.packed}},
157                                     'len': o.network.prefixlen},
158         'str': lambda s: format_vl_api_address_with_prefix_t(s)
159     },
160     'vl_api_ip4_address_with_prefix_t':
161     {
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)
165     },
166     'vl_api_ip6_address_with_prefix_t':
167     {
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)
171     },
172     'vl_api_mac_address_t':
173     {
174         'MACAddress': lambda o: o.packed,
175         'str': lambda s: macaddress.mac_pton(s)
176     },
177     'vl_api_timestamp_t':
178     {
179         'datetime.datetime': lambda o:
180         (o - datetime.datetime(1970, 1, 1)).total_seconds()
181     }
182 }
183
184
185 def unformat_api_address_t(o):
186     if o.af == 1:
187         return ipaddress.IPv6Address(o.un.ip6)
188     if o.af == 0:
189         return ipaddress.IPv4Address(o.un.ip4)
190     return None
191
192
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)
198     return None
199
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))
205
206
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))
212     return None
213
214
215 def unformat_api_ip4_address_with_prefix_t(o):
216     return ipaddress.IPv4Interface((o.address, o.len))
217
218
219 def unformat_api_ip6_address_with_prefix_t(o):
220     return ipaddress.IPv6Interface((o.address, o.len))
221
222
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),
236 }