PAPI: Allow ipaddress object as argument and return values from API calls
[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
16 from socket import inet_pton, inet_ntop, AF_INET6, AF_INET
17 import socket
18 import ipaddress
19
20 # Copies from vl_api_address_t definition
21 ADDRESS_IP4 = 0
22 ADDRESS_IP6 = 1
23
24 #
25 # Type conversion for input arguments and return values
26 #
27
28
29 def format_vl_api_address_t(args):
30     try:
31         return {'un': {'ip6': inet_pton(AF_INET6, args)},
32                 'af': ADDRESS_IP6}
33     except socket.error as e:
34         return {'un': {'ip4': inet_pton(AF_INET, args)},
35                 'af': ADDRESS_IP4}
36
37
38 def format_vl_api_prefix_t(args):
39     p, length = args.split('/')
40     return {'address': format_vl_api_address_t(p),
41             'address_length': int(length)}
42
43
44 def format_vl_api_ip6_prefix_t(args):
45     p, length = args.split('/')
46     return {'prefix': inet_pton(AF_INET6, p),
47             'len': int(length)}
48
49
50 def format_vl_api_ip4_prefix_t(args):
51     p, length = args.split('/')
52     return {'prefix': inet_pton(AF_INET, p),
53             'len': int(length)}
54
55
56 conversion_table = {
57     'vl_api_ip6_address_t':
58     {
59         'IPv6Address': lambda o: o.packed,
60         'str': lambda s: inet_pton(AF_INET6, s)
61     },
62     'vl_api_ip4_address_t':
63     {
64         'IPv4Address': lambda o: o.packed,
65         'str': lambda s: inet_pton(AF_INET, s)
66     },
67     'vl_api_ip6_prefix_t':
68     {
69         'IPv6Network': lambda o: {'prefix': o.network_address.packed,
70                                   'len': o.prefixlen},
71         'str': lambda s: format_vl_api_ip6_prefix_t(s)
72     },
73     'vl_api_ip4_prefix_t':
74     {
75         'IPv4Network': lambda o: {'prefix': o.network_address.packed,
76                                   'len': o.prefixlen},
77         'str': lambda s: format_vl_api_ip4_prefix_t(s)
78     },
79     'vl_api_address_t':
80     {
81         'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
82         'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
83         'str': lambda s: format_vl_api_address_t(s)
84     },
85     'vl_api_prefix_t':
86     {
87         'IPv4Network': lambda o: {'prefix':
88                                   {'af': ADDRESS_IP4, 'un':
89                                    {'ip4': o.network_address.packed}},
90                                   'len': o.prefixlen},
91         'IPv6Network': lambda o: {'prefix':
92                                   {'af': ADDRESS_IP6, 'un':
93                                    {'ip6': o.network_address.packed}},
94                                   'len': o.prefixlen},
95         'str': lambda s: format_vl_api_prefix_t(s)
96     },
97 }
98
99
100 def unformat_api_address_t(o):
101     if o.af == 1:
102         return ipaddress.IPv6Address(o.un.ip6)
103     if o.af == 0:
104         return ipaddress.IPv4Address(o.un.ip4)
105
106
107 def unformat_api_prefix_t(o):
108     if isinstance(o.address, ipaddress.IPv4Address):
109         return ipaddress.IPv4Network((o.address, o.address_length), False)
110     if isinstance(o.address, ipaddress.IPv6Address):
111         return ipaddress.IPv6Network((o.address, o.address_length), False)
112
113
114 conversion_unpacker_table = {
115     'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
116     'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.prefix, o.len)),
117     'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
118     'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.prefix, o.len)),
119     'vl_api_address_t': lambda o: unformat_api_address_t(o),
120     'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
121 }