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.
# 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)}
