diff options
author | Paul Vinciguerra <pvinci@vinciconsulting.com> | 2019-06-06 15:25:44 -0400 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2019-06-07 07:45:50 +0000 |
commit | ed3bcb00b31b2ae2e43219557c62c5bd1b31ceb4 (patch) | |
tree | 9c95a67a84f4d8f8ae55f916ee1536217739e4f6 /src/vpp-api | |
parent | 34ce57486106f8a64b54f32122645a8df9be79eb (diff) |
vpp_papi: Accept ipaddress.Network objects on prefix_t methods.
vl_api_prefix_t returns ipaddress.<IPv[46]>Network objects.
Accept Network objects as well as existing string format.
Type: refactor.
Change-Id: Iba5403724a7b3e1da3b3740027fccd43631bf31e
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
Diffstat (limited to 'src/vpp-api')
-rw-r--r-- | src/vpp-api/python/vpp_papi/vpp_format.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/vpp-api/python/vpp_papi/vpp_format.py b/src/vpp-api/python/vpp_papi/vpp_format.py index fec0667862c..2c2345a3f76 100644 --- a/src/vpp-api/python/vpp_papi/vpp_format.py +++ b/src/vpp-api/python/vpp_papi/vpp_format.py @@ -13,11 +13,16 @@ # limitations under the License. # -from socket import inet_pton, inet_ntop, AF_INET6, AF_INET +from socket import inet_pton, AF_INET6, AF_INET import socket import ipaddress from . import macaddress +try: + text_type = unicode +except NameError: + text_type = str + # Copies from vl_api_address_t definition ADDRESS_IP4 = 0 ADDRESS_IP6 = 1 @@ -37,18 +42,28 @@ def format_vl_api_address_t(args): def format_vl_api_prefix_t(args): + if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)): + return {'prefix': format_vl_api_address_t( + text_type(args.network_address)), + 'len': int(args.prefixlen)} p, length = args.split('/') return {'address': format_vl_api_address_t(p), 'address_length': int(length)} def format_vl_api_ip6_prefix_t(args): + if isinstance(args, ipaddress.IPv6Network): + return {'prefix': args.network_address.packed, + 'len': int(args.prefixlen)} p, length = args.split('/') return {'prefix': inet_pton(AF_INET6, p), 'len': int(length)} def format_vl_api_ip4_prefix_t(args): + if isinstance(args, ipaddress.IPv4Network): + return {'prefix': args.network_address.packed, + 'len': int(args.prefixlen)} p, length = args.split('/') return {'prefix': inet_pton(AF_INET, p), 'len': int(length)} |