X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FIPUtil.py;h=901d2ed7f702c1af2365150350e83d7bb149b99c;hb=d7c297494fa96603a90dda7ec50e73c041ca6aa2;hp=0c4356bb6447c9039bce35ac52f4d761c5bb6417;hpb=758d50dd77f6114b2d4fe0ca13d95ae111f7b110;p=csit.git diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py index 0c4356bb64..901d2ed7f7 100644 --- a/resources/libraries/python/IPUtil.py +++ b/resources/libraries/python/IPUtil.py @@ -16,11 +16,11 @@ import re from enum import IntEnum -from ipaddress import ip_address, IPv4Network, IPv6Network +from ipaddress import ip_address from resources.libraries.python.Constants import Constants from resources.libraries.python.InterfaceUtil import InterfaceUtil -from resources.libraries.python.PapiExecutor import PapiExecutor +from resources.libraries.python.PapiExecutor import PapiSocketExecutor from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd from resources.libraries.python.topology import Topology from resources.libraries.python.VatExecutor import VatTerminal @@ -112,34 +112,24 @@ class IPUtil(object): """ sw_if_index = InterfaceUtil.get_interface_index(node, interface) - data = list() - if sw_if_index: - is_ipv6 = 1 if ip_version == 'ipv6' else 0 - - cmd = 'ip_address_dump' - cmd_reply = 'ip_address_details' - args = dict(sw_if_index=sw_if_index, - is_ipv6=is_ipv6) - err_msg = 'Failed to get L2FIB dump on host {host}'.format( - host=node['host']) - - with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_dump(err_msg) - - for item in papi_resp.reply[0]['api_reply']: - item[cmd_reply]['ip'] = item[cmd_reply]['prefix'].split('/')[0] - item[cmd_reply]['prefix_length'] = int( - item[cmd_reply]['prefix'].split('/')[1]) - item[cmd_reply]['is_ipv6'] = is_ipv6 - item[cmd_reply]['netmask'] = \ - str(IPv6Network(unicode('::/{pl}'.format( - pl=item[cmd_reply]['prefix_length']))).netmask) \ - if is_ipv6 \ - else str(IPv4Network(unicode('0.0.0.0/{pl}'.format( - pl=item[cmd_reply]['prefix_length']))).netmask) - data.append(item[cmd_reply]) - - return data + if not sw_if_index: + return list() + + is_ipv6 = 1 if ip_version == 'ipv6' else 0 + + cmd = 'ip_address_dump' + args = dict(sw_if_index=sw_if_index, + is_ipv6=is_ipv6) + err_msg = 'Failed to get L2FIB dump on host {host}'.format( + host=node['host']) + + with PapiSocketExecutor(node) as papi_exec: + details = papi_exec.add(cmd, **args).get_details(err_msg) + + # TODO: CSIT currently looks only whether the list is empty. + # Add proper value processing if values become important. + + return details @staticmethod def vpp_get_ip_tables(node): @@ -149,10 +139,25 @@ class IPUtil(object): :type node: dict """ - PapiExecutor.run_cli_cmd(node, 'show ip fib') - PapiExecutor.run_cli_cmd(node, 'show ip fib summary') - PapiExecutor.run_cli_cmd(node, 'show ip6 fib') - PapiExecutor.run_cli_cmd(node, 'show ip6 fib summary') + PapiSocketExecutor.run_cli_cmd(node, 'show ip fib') + PapiSocketExecutor.run_cli_cmd(node, 'show ip fib summary') + PapiSocketExecutor.run_cli_cmd(node, 'show ip6 fib') + PapiSocketExecutor.run_cli_cmd(node, 'show ip6 fib summary') + + @staticmethod + def vpp_get_ip_tables_prefix(node, address): + """Get dump of all IP FIB tables on a VPP node. + + :param node: VPP node. + :type node: dict + """ + addr = ip_address(unicode(address)) + + PapiSocketExecutor.run_cli_cmd( + node, 'show {ip_ver} fib {addr}/{addr_len}'.format( + ip_ver='ip6' if addr.version == 6 else 'ip', + addr=addr, + addr_len=addr.max_prefixlen)) @staticmethod def get_interface_vrf_table(node, interface, ip_version='ipv4'): @@ -177,11 +182,10 @@ class IPUtil(object): err_msg = 'Failed to get VRF id assigned to interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return papi_resp['vrf_id'] + return reply['vrf_id'] @staticmethod def vpp_ip_source_check_setup(node, if_name): @@ -199,9 +203,8 @@ class IPUtil(object): loose=0) err_msg = 'Failed to enable source check on interface {ifc}'.format( ifc=if_name) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_ip_probe(node, interface, addr): @@ -215,16 +218,14 @@ class IPUtil(object): :type addr: str """ cmd = 'ip_probe_neighbor' - cmd_reply = 'proxy_arp_intfc_enable_disable_reply' args = dict( sw_if_index=InterfaceUtil.get_interface_index(node, interface), dst=str(addr)) err_msg = 'VPP ip probe {dev} {ip} failed on {h}'.format( dev=interface, ip=addr, h=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(cmd_reply=cmd_reply, err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def ip_addresses_should_be_equal(ip1, ip2): @@ -400,9 +401,8 @@ class IPUtil(object): address=ip_addr.packed) err_msg = 'Failed to add IP address on interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address): @@ -430,9 +430,8 @@ class IPUtil(object): neighbor=neighbor) err_msg = 'Failed to add IP neighbor on interface {ifc}'.format( ifc=iface_key) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def union_addr(ip_addr): @@ -446,6 +445,21 @@ class IPUtil(object): return dict(ip6=ip_addr.packed) if ip_addr.version == 6 \ else dict(ip4=ip_addr.packed) + @staticmethod + def create_ip_address_object(ip_addr): + """Create IP address object. + + :param ip_addr: IPv4 or IPv6 address + :type ip_addr: IPv4Address or IPv6Address + :returns: IP address object. + :rtype: dict + """ + return dict( + af=getattr( + AddressFamily, 'ADDRESS_IP6' if ip_addr.version == 6 + else 'ADDRESS_IP4').value, + un=IPUtil.union_addr(ip_addr)) + @staticmethod def compose_vpp_route_structure(node, network, prefix_len, **kwargs): """Create route object for ip_route_add_del api call. @@ -477,11 +491,7 @@ class IPUtil(object): net_addr = ip_address(unicode(network)) - addr = dict( - af=getattr( - AddressFamily, 'ADDRESS_IP6' if net_addr.version == 6 - else 'ADDRESS_IP4').value, - un=None) + addr = IPUtil.create_ip_address_object(net_addr) prefix = dict( len=int(prefix_len), address=addr) @@ -518,7 +528,6 @@ class IPUtil(object): prefix=prefix, n_paths=len(paths), paths=paths) - return route @staticmethod @@ -549,7 +558,7 @@ class IPUtil(object): if count > 100: gateway = kwargs.get("gateway", '') - + interface = kwargs.get("interface", '') vrf = kwargs.get("vrf", None) multipath = kwargs.get("multipath", False) @@ -559,6 +568,9 @@ class IPUtil(object): network=network, prefix_length=prefix_len, via='via {}'.format(gateway) if gateway else '', + sw_if_index='sw_if_index {}'.format( + InterfaceUtil.get_interface_index(node, interface)) + if interface else '', vrf='vrf {}'.format(vrf) if vrf else '', count='count {}'.format(count) if count else '', multipath='multipath' if multipath else '') @@ -575,16 +587,13 @@ class IPUtil(object): err_msg = 'Failed to add route(s) on host {host}'.format( host=node['host']) - with PapiExecutor(node) as papi_exec: + with PapiSocketExecutor(node) as papi_exec: for i in xrange(kwargs.get('count', 1)): args['route']['prefix']['address']['un'] = \ IPUtil.union_addr(net_addr + i) history = False if 1 < i < kwargs.get('count', 1) else True papi_exec.add(cmd, history=history, **args) - if i > 0 and i % Constants.PAPI_MAX_API_BULK == 0: - papi_exec.get_replies(err_msg).verify_replies( - err_msg=err_msg) - papi_exec.get_replies(err_msg).verify_replies(err_msg=err_msg) + papi_exec.get_replies(err_msg) @staticmethod def flush_ip_addresses(node, interface): @@ -601,9 +610,8 @@ class IPUtil(object): del_all=1) err_msg = 'Failed to flush IP address on interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def add_fib_table(node, table_id, ipv6=False): @@ -625,6 +633,5 @@ class IPUtil(object): is_add=1) err_msg = 'Failed to add FIB table on host {host}'.format( host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg)