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