From ed3bcb00b31b2ae2e43219557c62c5bd1b31ceb4 Mon Sep 17 00:00:00 2001 From: Paul Vinciguerra Date: Thu, 6 Jun 2019 15:25:44 -0400 Subject: vpp_papi: Accept ipaddress.Network objects on prefix_t methods. vl_api_prefix_t returns ipaddress.Network objects. Accept Network objects as well as existing string format. Type: refactor. Change-Id: Iba5403724a7b3e1da3b3740027fccd43631bf31e Signed-off-by: Paul Vinciguerra --- src/vpp-api/python/vpp_papi/vpp_format.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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)} -- cgit 1.2.3-korg