api: refactor format_vl_api_prefix_t return keys
[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, 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
56 def format_vl_api_ip6_prefix_t(args):
57     if isinstance(args, ipaddress.IPv6Network):
58         return {'address': args.network_address.packed,
59                 'len': int(args.prefixlen)}
60     p, length = args.split('/')
61     return {'address': inet_pton(AF_INET6, p),
62             'len': int(length)}
63
64
65 def format_vl_api_ip4_prefix_t(args):
66     if isinstance(args, ipaddress.IPv4Network):
67         return {'address': args.network_address.packed,
68                 'len': int(args.prefixlen)}
69     p, length = args.split('/')
70     return {'address': inet_pton(AF_INET, p),
71             'len': int(length)}
72
73
74 conversion_table = {
75     'vl_api_ip6_address_t':
76     {
77         'IPv6Address': lambda o: o.packed,
78         'str': lambda s: inet_pton(AF_INET6, s)
79     },
80     'vl_api_ip4_address_t':
81     {
82         'IPv4Address': lambda o: o.packed,
83         'str': lambda s: inet_pton(AF_INET, s)
84     },
85     'vl_api_ip6_prefix_t':
86     {
87         'IPv6Network': lambda o: {'address': o.network_address.packed,
88                                   'len': o.prefixlen},
89         'str': lambda s: format_vl_api_ip6_prefix_t(s)
90     },
91     'vl_api_ip4_prefix_t':
92     {
93         'IPv4Network': lambda o: {'address': o.network_address.packed,
94                                   'len': o.prefixlen},
95         'str': lambda s: format_vl_api_ip4_prefix_t(s)
96     },
97     'vl_api_address_t':
98     {
99         'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}},
100         'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}},
101         'str': lambda s: format_vl_api_address_t(s)
102     },
103     'vl_api_prefix_t':
104     {
105         'IPv4Network': lambda o: {'prefix':
106                                   {'af': ADDRESS_IP4, 'un':
107                                    {'ip4': o.network_address.packed}},
108                                   'len': o.prefixlen},
109         'IPv6Network': lambda o: {'prefix':
110                                   {'af': ADDRESS_IP6, 'un':
111                                    {'ip6': o.network_address.packed}},
112                                   'len': o.prefixlen},
113         'str': lambda s: format_vl_api_prefix_t(s)
114     },
115     'vl_api_mac_address_t':
116     {
117         'MACAddress': lambda o: o.packed,
118         'str': lambda s: macaddress.mac_pton(s)
119     },
120 }
121
122
123 def unformat_api_address_t(o):
124     if o.af == 1:
125         return ipaddress.IPv6Address(o.un.ip6)
126     if o.af == 0:
127         return ipaddress.IPv4Address(o.un.ip4)
128
129
130 def unformat_api_prefix_t(o):
131     if isinstance(o.address, ipaddress.IPv4Address):
132         return ipaddress.IPv4Network((o.address, o.len), False)
133     if isinstance(o.address, ipaddress.IPv6Address):
134         return ipaddress.IPv6Network((o.address, o.len), False)
135
136
137 conversion_unpacker_table = {
138     'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o),
139     'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)),
140     'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o),
141     'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)),
142     'vl_api_address_t': lambda o: unformat_api_address_t(o),
143     'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o),
144     'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o),
145 }