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