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