diff options
author | Vratko Polak <vrpolak@cisco.com> | 2019-07-09 12:17:09 +0200 |
---|---|---|
committer | Dave Wallace <dwallacelf@gmail.com> | 2019-07-12 13:00:49 +0000 |
commit | 33fb34665214bbbd0a4b3154169b21c2da01f69b (patch) | |
tree | 9ebb70889824451cf8411875159a6fafd70b60ac /resources | |
parent | ccfe499e2a27f2caf234ecbb2ec948120810eab6 (diff) |
PapiExecutor always verifies
Do not support returning unverified replies anymore.
Basically, ".get_replies().verify_replies()" is now just ".get_replies()".
This allows fairly large simplifications both at call sites
and in PapiExecutor.py
+ Rename get_dumps to get_details.
+ Introduce get_reply and get_sw_if_index.
+ Rename variables holding get_*() value,
+ e.g. get_stats() value is stored to variable named "stats".
+ Rename "item" of subsequent loop to hint the type instead.
+ Rename "details" function argument to "verbose".
+ Process reply details in place, instead of building new list.
- Except hybrid blocks which can return both list or single item.
- Except human readable text building blocks.
+ Rename most similar names to sw_if_index.
- Except "vpp_sw_index" and some function names.
+ Use single run_cli_cmd from PapiExecutor.
+ Do not chain methods over multiple lines.
+ Small space gain is not worth readability loss.
+ Include minor code and docstrings improvement.
+ Add some TODOs.
Change-Id: Ib2110a3d2101a74d5837baab3a58dc46aafc6ce3
Signed-off-by: Vratko Polak <vrpolak@cisco.com>
Diffstat (limited to 'resources')
20 files changed, 457 insertions, 765 deletions
diff --git a/resources/libraries/python/Classify.py b/resources/libraries/python/Classify.py index 2b9ab3d11c..b2cc3a6420 100644 --- a/resources/libraries/python/Classify.py +++ b/resources/libraries/python/Classify.py @@ -290,11 +290,10 @@ class Classify(object): host=node['host']) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return int(data["new_table_index"]), int(data["skip_n_vectors"]),\ - int(data["match_n_vectors"]) + return int(reply["new_table_index"]), int(reply["skip_n_vectors"]),\ + int(reply["match_n_vectors"]) @staticmethod def _classify_add_del_session(node, is_add, table_index, match, @@ -357,8 +356,7 @@ class Classify(object): host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _macip_acl_add(node, rules, tag=""): @@ -382,8 +380,7 @@ class Classify(object): host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _acl_interface_set_acl_list(node, sw_if_index, acl_type, acls): @@ -411,8 +408,7 @@ class Classify(object): format(idx=sw_if_index, host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _acl_add_replace(node, acl_idx, rules, tag=""): @@ -439,8 +435,7 @@ class Classify(object): host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_creates_classify_table_l3(node, ip_version, direction, ip_addr): @@ -738,10 +733,8 @@ class Classify(object): table_id=int(table_index) ) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - - return data + reply = papi_exec.add(cmd, **args).get_reply(err_msg) + return reply @staticmethod def get_classify_session_data(node, table_index): @@ -754,14 +747,14 @@ class Classify(object): :returns: List of classify session settings. :rtype: list or dict """ + cmd = "classify_session_dump" args = dict( table_id=int(table_index) ) with PapiExecutor(node) as papi_exec: - dump = papi_exec.add("classify_session_dump", **args).\ - get_dump().reply[0]["api_reply"]["classify_session_details"] + details = papi_exec.add(cmd, **args).get_details() - return dump + return details @staticmethod def vpp_log_plugin_acl_settings(node): @@ -941,8 +934,7 @@ class Classify(object): acl_index=int(acl_idx) ) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_log_macip_acl_interface_assignment(node): @@ -955,5 +947,5 @@ class Classify(object): err_msg = "Failed to get 'macip_acl_interface' on host {host}".format( host=node['host']) with PapiExecutor(node) as papi_exec: - rpl = papi_exec.add(cmd).get_replies(err_msg).reply[0]["api_reply"] - logger.info(rpl["macip_acl_interface_get_reply"]) + reply = papi_exec.add(cmd).get_reply(err_msg) + logger.info(reply) diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py index 2dafebf5f6..6a8e1a2401 100644 --- a/resources/libraries/python/IPUtil.py +++ b/resources/libraries/python/IPUtil.py @@ -112,34 +112,30 @@ 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'] = \ + details = papi_exec.add(cmd, **args).get_details(err_msg) + + for item in details: + item['ip'] = item['prefix'].split('/')[0] + item['prefix_length'] = int(item['prefix'].split('/')[1]) + item['is_ipv6'] = is_ipv6 + item['netmask'] = \ str(IPv6Network(unicode('::/{pl}'.format( - pl=item[cmd_reply]['prefix_length']))).netmask) \ + pl=item['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]) + pl=item['prefix_length']))).netmask) - return data + return details @staticmethod def vpp_get_ip_tables(node): @@ -193,10 +189,9 @@ class IPUtil(object): 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) + 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): @@ -215,8 +210,7 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_ip_probe(node, interface, addr): @@ -230,7 +224,6 @@ 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)) @@ -238,8 +231,7 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def ip_addresses_should_be_equal(ip1, ip2): @@ -416,8 +408,7 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address): @@ -446,8 +437,7 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def union_addr(ip_addr): @@ -600,9 +590,8 @@ class IPUtil(object): 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) + papi_exec.get_replies(err_msg) @staticmethod def flush_ip_addresses(node, interface): @@ -620,8 +609,7 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def add_fib_table(node, table_id, ipv6=False): @@ -644,5 +632,4 @@ class IPUtil(object): 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) + papi_exec.add(cmd, **args).get_reply(err_msg) diff --git a/resources/libraries/python/IPsecUtil.py b/resources/libraries/python/IPsecUtil.py index 1a59172958..78239f9950 100644 --- a/resources/libraries/python/IPsecUtil.py +++ b/resources/libraries/python/IPsecUtil.py @@ -257,16 +257,11 @@ class IPsecUtil(object): """ cmd = 'ipsec_select_backend' - cmd_reply = 'ipsec_select_backend_reply' err_msg = 'Failed to select IPsec backend on host {host}'.format( host=node['host']) args = dict(protocol=protocol, index=index) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).execute_should_pass(err_msg) - data = papi_resp.reply[0]['api_reply'][cmd_reply] - if data['retval'] != 0: - raise RuntimeError('Failed to select IPsec backend on host {host}'. - format(host=node['host'])) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_ipsec_backend_dump(node): @@ -279,8 +274,7 @@ class IPsecUtil(object): err_msg = 'Failed to dump IPsec backends on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add('ipsec_backend_dump').execute_should_pass( - err_msg, process_reply=False) + papi_exec.add('ipsec_backend_dump').get_details(err_msg) @staticmethod def vpp_ipsec_add_sad_entry(node, sad_id, spi, crypto_alg, crypto_key, diff --git a/resources/libraries/python/IPv6Util.py b/resources/libraries/python/IPv6Util.py index 28e5f7d2fb..9138c09a20 100644 --- a/resources/libraries/python/IPv6Util.py +++ b/resources/libraries/python/IPv6Util.py @@ -39,8 +39,7 @@ class IPv6Util(object): '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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_ra_send_after_interval(node, interface, interval=2): @@ -62,8 +61,7 @@ class IPv6Util(object): '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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_all_ra_suppress_link_layer(nodes): diff --git a/resources/libraries/python/InterfaceUtil.py b/resources/libraries/python/InterfaceUtil.py index 7144e0adef..0b1f06f9bf 100644 --- a/resources/libraries/python/InterfaceUtil.py +++ b/resources/libraries/python/InterfaceUtil.py @@ -139,8 +139,7 @@ class InterfaceUtil(object): args = dict(sw_if_index=sw_if_index, admin_up_down=admin_up_down) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) elif node['type'] == NodeType.TG or node['type'] == NodeType.VM: cmd = 'ip link set {ifc} {state}'.format( ifc=iface_name, state=state) @@ -212,8 +211,7 @@ class InterfaceUtil(object): mtu=int(mtu)) try: with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) except AssertionError as err: # TODO: Make failure tolerance optional. logger.debug("Setting MTU failed. Expected?\n{err}".format( @@ -319,15 +317,12 @@ class InterfaceUtil(object): param = '' cmd = 'sw_interface_dump' - cmd_reply = 'sw_interface_details' args = dict(name_filter_valid=0, name_filter='') err_msg = 'Failed to get interface 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) - - papi_if_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) def process_if_dump(if_dump): """Process interface dump. @@ -345,12 +340,11 @@ class InterfaceUtil(object): return if_dump data = list() if interface is None else dict() - for item in papi_if_dump: + for if_dump in details: if interface is None: - data.append(process_if_dump(item[cmd_reply])) - elif str(item[cmd_reply].get(param)).rstrip('\x00') == \ - str(interface): - data = process_if_dump(item[cmd_reply]) + data.append(process_if_dump(if_dump)) + elif str(if_dump.get(param)).rstrip('\x00') == str(interface): + data = process_if_dump(if_dump) break logger.debug('Interface data:\n{if_data}'.format(if_data=data)) @@ -736,16 +730,14 @@ class InterfaceUtil(object): err_msg = 'Failed to create VLAN sub-interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] if_key = Topology.add_new_port(node, 'vlan_subif') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - return '{ifc}.{vlan}'.format(ifc=interface, vlan=vlan), sw_if_idx + return '{ifc}.{vlan}'.format(ifc=interface, vlan=vlan), sw_if_index @staticmethod def create_vxlan_interface(node, vni, source_ip, destination_ip): @@ -780,16 +772,14 @@ class InterfaceUtil(object): err_msg = 'Failed to create VXLAN tunnel interface on host {host}'.\ format(host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] if_key = Topology.add_new_port(node, 'vxlan_tunnel') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - return sw_if_idx + return sw_if_index @staticmethod def vxlan_dump(node, interface=None): @@ -812,14 +802,11 @@ class InterfaceUtil(object): sw_if_index = int(Constants.BITWISE_NON_ZERO) cmd = 'vxlan_tunnel_dump' - cmd_reply = 'vxlan_tunnel_details' args = dict(sw_if_index=sw_if_index) err_msg = 'Failed to get VXLAN 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) - - papi_vxlan_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) def process_vxlan_dump(vxlan_dump): """Process vxlan dump. @@ -842,11 +829,11 @@ class InterfaceUtil(object): return vxlan_dump data = list() if interface is None else dict() - for item in papi_vxlan_dump: + for vxlan_dump in details: if interface is None: - data.append(process_vxlan_dump(item[cmd_reply])) - elif item[cmd_reply]['sw_if_index'] == sw_if_index: - data = process_vxlan_dump(item[cmd_reply]) + data.append(process_vxlan_dump(vxlan_dump)) + elif vxlan_dump['sw_if_index'] == sw_if_index: + data = process_vxlan_dump(vxlan_dump) break logger.debug('VXLAN data:\n{vxlan_data}'.format(vxlan_data=data)) @@ -864,13 +851,10 @@ class InterfaceUtil(object): :rtype: list """ cmd = 'sw_interface_vhost_user_dump' - cmd_reply = 'sw_interface_vhost_user_details' err_msg = 'Failed to get vhost-user dump on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd).get_dump(err_msg) - - papi_vxlan_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd).get_details(err_msg) def process_vhost_dump(vhost_dump): """Process vhost dump. @@ -886,12 +870,13 @@ class InterfaceUtil(object): vhost_dump['sock_filename'].rstrip('\x00') return vhost_dump - data = list() - for item in papi_vxlan_dump: - data.append(process_vhost_dump(item[cmd_reply])) + for vhost_dump in details: + # In-place edits. + process_vhost_dump(vhost_dump) - logger.debug('Vhost-user data:\n{vhost_data}'.format(vhost_data=data)) - return data + logger.debug('Vhost-user details:\n{vhost_details}'.format( + vhost_details=details)) + return details @staticmethod def tap_dump(node, name=None): @@ -909,13 +894,10 @@ class InterfaceUtil(object): :rtype: dict or list """ cmd = 'sw_interface_tap_v2_dump' - cmd_reply = 'sw_interface_tap_v2_details' err_msg = 'Failed to get TAP dump on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd).get_dump(err_msg) - - papi_tap_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd).get_details(err_msg) def process_tap_dump(tap_dump): """Process tap dump. @@ -938,11 +920,11 @@ class InterfaceUtil(object): return tap_dump data = list() if name is None else dict() - for item in papi_tap_dump: + for tap_dump in details: if name is None: - data.append(process_tap_dump(item[cmd_reply])) - elif item[cmd_reply].get('dev_name').rstrip('\x00') == name: - data = process_tap_dump(item[cmd_reply]) + data.append(process_tap_dump(tap_dump)) + elif tap_dump.get('dev_name').rstrip('\x00') == name: + data = process_tap_dump(tap_dump) break logger.debug('TAP data:\n{tap_data}'.format(tap_data=data)) @@ -991,16 +973,14 @@ class InterfaceUtil(object): err_msg = 'Failed to create sub-interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_subif_idx = papi_resp['sw_if_index'] if_key = Topology.add_new_port(node, 'subinterface') - Topology.update_interface_sw_if_index(node, if_key, sw_subif_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_subif_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - return '{ifc}.{s_id}'.format(ifc=interface, s_id=sub_id), sw_subif_idx + return '{ifc}.{s_id}'.format(ifc=interface, s_id=sub_id), sw_if_index @staticmethod def create_gre_tunnel_interface(node, source_ip, destination_ip): @@ -1028,16 +1008,14 @@ class InterfaceUtil(object): err_msg = 'Failed to create GRE tunnel interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] if_key = Topology.add_new_port(node, 'gre_tunnel') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - return ifc_name, sw_if_idx + return ifc_name, sw_if_index @staticmethod def vpp_create_loopback(node): @@ -1055,16 +1033,14 @@ class InterfaceUtil(object): err_msg = 'Failed to create loopback interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] if_key = Topology.add_new_port(node, 'loopback') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - return sw_if_idx + return sw_if_index @staticmethod def vpp_create_bond_interface(node, mode, load_balance=None, mac=None): @@ -1096,38 +1072,37 @@ class InterfaceUtil(object): err_msg = 'Failed to create bond interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] - InterfaceUtil.add_eth_interface(node, sw_if_idx=sw_if_idx, + InterfaceUtil.add_eth_interface(node, sw_if_index=sw_if_index, ifc_pfx='eth_bond') - if_key = Topology.get_interface_by_sw_index(node, sw_if_idx) + if_key = Topology.get_interface_by_sw_index(node, sw_if_index) return if_key @staticmethod - def add_eth_interface(node, ifc_name=None, sw_if_idx=None, ifc_pfx=None): + def add_eth_interface(node, ifc_name=None, sw_if_index=None, ifc_pfx=None): """Add ethernet interface to current topology. :param node: DUT node from topology. :param ifc_name: Name of the interface. - :param sw_if_idx: SW interface index. + :param sw_if_index: SW interface index. :param ifc_pfx: Interface key prefix. :type node: dict :type ifc_name: str - :type sw_if_idx: int + :type sw_if_index: int :type ifc_pfx: str """ if_key = Topology.add_new_port(node, ifc_pfx) - if ifc_name and sw_if_idx is None: - sw_if_idx = InterfaceUtil.vpp_get_interface_sw_index(node, ifc_name) - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - if sw_if_idx and ifc_name is None: - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + if ifc_name and sw_if_index is None: + sw_if_index = InterfaceUtil.vpp_get_interface_sw_index( + node, ifc_name) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + if sw_if_index and ifc_name is None: + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx) + ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index) Topology.update_interface_mac_address(node, if_key, ifc_mac) @staticmethod @@ -1154,13 +1129,11 @@ class InterfaceUtil(object): err_msg = 'Failed to create AVF interface on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) - sw_if_idx = papi_resp['sw_if_index'] - InterfaceUtil.add_eth_interface(node, sw_if_idx=sw_if_idx, + InterfaceUtil.add_eth_interface(node, sw_if_index=sw_if_index, ifc_pfx='eth_avf') - if_key = Topology.get_interface_by_sw_index(node, sw_if_idx) + if_key = Topology.get_interface_by_sw_index(node, sw_if_index) return if_key @@ -1188,55 +1161,46 @@ class InterfaceUtil(object): bond=bond_if, host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod - def vpp_show_bond_data_on_node(node, details=False): + def vpp_show_bond_data_on_node(node, verbose=False): """Show (detailed) bond information on VPP node. :param node: DUT node from topology. - :param details: If detailed information is required or not. + :param verbose: If detailed information is required or not. :type node: dict - :type details: bool + :type verbose: bool """ cmd = 'sw_interface_bond_dump' - cmd_reply = 'sw_interface_bond_details' err_msg = 'Failed to get bond interface dump on host {host}'.format( host=node['host']) data = ('Bond data on node {host}:\n'.format(host=node['host'])) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd).get_dump(err_msg) - - papi_dump = papi_resp.reply[0]['api_reply'] - for item in papi_dump: - data += ('{b}\n'.format(b=item[cmd_reply]['interface_name']. - rstrip('\x00'))) - data += (' mode: {m}\n'. - format(m=LinkBondMode(item[cmd_reply]['mode']).name. - lower())) - data += (' load balance: {lb}\n'. - format(lb=LinkBondLoadBalance(item[cmd_reply]['lb']).name. - lower())) - data += (' number of active slaves: {n}\n'. - format(n=item[cmd_reply]['active_slaves'])) - if details: + details = papi_exec.add(cmd).get_details(err_msg) + + for bond in details: + data += ('{b}\n'.format(b=bond['interface_name'].rstrip('\x00'))) + data += (' mode: {m}\n'.format(m=LinkBondMode( + bond['mode']).name.lower())) + data += (' load balance: {lb}\n'.format(lb=LinkBondLoadBalance( + bond['lb']).name.lower())) + data += (' number of active slaves: {n}\n'.format( + n=bond['active_slaves'])) + if verbose: slave_data = InterfaceUtil.vpp_bond_slave_dump( node, Topology.get_interface_by_sw_index( - node, item[cmd_reply]['sw_if_index'])) + node, bond['sw_if_index'])) for slave in slave_data: if not slave['is_passive']: data += (' {s}\n'.format(s=slave['interface_name'])) - data += (' number of slaves: {n}\n'. - format(n=item[cmd_reply]['slaves'])) - if details: + data += (' number of slaves: {n}\n'.format(n=bond['slaves'])) + if verbose: for slave in slave_data: data += (' {s}\n'.format(s=slave['interface_name'])) - data += (' interface id: {i}\n'. - format(i=item[cmd_reply]['id'])) - data += (' sw_if_index: {i}\n'. - format(i=item[cmd_reply]['sw_if_index'])) + data += (' interface id: {i}\n'.format(i=bond['id'])) + data += (' sw_if_index: {i}\n'.format(i=bond['sw_if_index'])) logger.info(data) @staticmethod @@ -1251,16 +1215,13 @@ class InterfaceUtil(object): :rtype: dict """ cmd = 'sw_interface_slave_dump' - cmd_reply = 'sw_interface_slave_details' args = dict(sw_if_index=Topology.get_interface_sw_index( node, interface)) err_msg = 'Failed to get slave 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) - - papi_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) def process_slave_dump(slave_dump): """Process slave dump. @@ -1274,25 +1235,25 @@ class InterfaceUtil(object): rstrip('\x00') return slave_dump - data = list() - for item in papi_dump: - data.append(process_slave_dump(item[cmd_reply])) + for slave_dump in details: + # In-place edits. + process_slave_dump(slave_dump) - logger.debug('Slave data:\n{slave_data}'.format(slave_data=data)) - return data + logger.debug('Slave data:\n{slave_data}'.format(slave_data=details)) + return details @staticmethod - def vpp_show_bond_data_on_all_nodes(nodes, details=False): + def vpp_show_bond_data_on_all_nodes(nodes, verbose=False): """Show (detailed) bond information on all VPP nodes in DICT__nodes. :param nodes: Nodes in the topology. - :param details: If detailed information is required or not. + :param verbose: If detailed information is required or not. :type nodes: dict - :type details: bool + :type verbose: bool """ for node_data in nodes.values(): if node_data['type'] == NodeType.DUT: - InterfaceUtil.vpp_show_bond_data_on_node(node_data, details) + InterfaceUtil.vpp_show_bond_data_on_node(node_data, verbose) @staticmethod def vpp_enable_input_acl_interface(node, interface, ip_version, @@ -1321,8 +1282,7 @@ class InterfaceUtil(object): err_msg = 'Failed to enable input acl 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def get_interface_classify_table(node, interface): @@ -1347,10 +1307,9 @@ class InterfaceUtil(object): err_msg = 'Failed to get classify table name by 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) + reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return papi_resp + return reply @staticmethod def get_sw_if_index(node, interface_name): @@ -1388,14 +1347,11 @@ class InterfaceUtil(object): sw_if_index = int(Constants.BITWISE_NON_ZERO) cmd = 'vxlan_gpe_tunnel_dump' - cmd_reply = 'vxlan_gpe_tunnel_details' args = dict(sw_if_index=sw_if_index) err_msg = 'Failed to get VXLAN-GPE 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) - - papi_vxlan_dump = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) def process_vxlan_gpe_dump(vxlan_dump): """Process vxlan_gpe dump. @@ -1418,11 +1374,11 @@ class InterfaceUtil(object): return vxlan_dump data = list() if interface_name is None else dict() - for item in papi_vxlan_dump: + for vxlan_dump in details: if interface_name is None: - data.append(process_vxlan_gpe_dump(item[cmd_reply])) - elif item[cmd_reply]['sw_if_index'] == sw_if_index: - data = process_vxlan_gpe_dump(item[cmd_reply]) + data.append(process_vxlan_gpe_dump(vxlan_dump)) + elif vxlan_dump['sw_if_index'] == sw_if_index: + data = process_vxlan_gpe_dump(vxlan_dump) break logger.debug('VXLAN-GPE data:\n{vxlan_gpe_data}'.format( @@ -1450,8 +1406,7 @@ class InterfaceUtil(object): err_msg = 'Failed to assign interface {ifc} to FIB table'.format( ifc=interface) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def set_linux_interface_mac(node, interface, mac, namespace=None, @@ -1606,17 +1561,14 @@ class InterfaceUtil(object): :rtype: list """ cmd = 'sw_interface_rx_placement_dump' - cmd_reply = 'sw_interface_rx_placement_details' err_msg = "Failed to run '{cmd}' PAPI command on host {host}!".format( cmd=cmd, host=node['host']) with PapiExecutor(node) as papi_exec: for ifc in node['interfaces'].values(): if ifc['vpp_sw_index'] is not None: papi_exec.add(cmd, sw_if_index=ifc['vpp_sw_index']) - papi_resp = papi_exec.get_dump(err_msg) - thr_mapping = [s[cmd_reply] for r in papi_resp.reply - for s in r['api_reply']] - return sorted(thr_mapping, key=lambda k: k['sw_if_index']) + details = papi_exec.get_details(err_msg) + return sorted(details, key=lambda k: k['sw_if_index']) @staticmethod def vpp_sw_interface_set_rx_placement(node, sw_if_index, queue_id, @@ -1640,8 +1592,7 @@ class InterfaceUtil(object): args = dict(sw_if_index=sw_if_index, queue_id=queue_id, worker_id=worker_id) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_round_robin_rx_placement(node, prefix): diff --git a/resources/libraries/python/L2Util.py b/resources/libraries/python/L2Util.py index f1476044a6..7c575a290e 100644 --- a/resources/libraries/python/L2Util.py +++ b/resources/libraries/python/L2Util.py @@ -130,8 +130,7 @@ class L2Util(object): filter_mac=int(filter_mac), bvi_mac=int(bvi_mac)) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def create_l2_bd(node, bd_id, flood=1, uu_flood=1, forward=1, learn=1, @@ -170,8 +169,7 @@ class L2Util(object): arp_term=int(arp_term), is_add=1) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def add_interface_to_l2_bd(node, interface, bd_id, shg=0, port_type=0): @@ -203,8 +201,7 @@ class L2Util(object): port_type=int(port_type), enable=1) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_add_l2_bridge_domain(node, bd_id, port_1, port_2, learn=True): @@ -252,8 +249,8 @@ class L2Util(object): ' {host}'.format(host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd1, **args1).add(cmd2, **args2).add(cmd2, **args3).\ - get_replies(err_msg).verify_replies(err_msg=err_msg) + papi_exec.add(cmd1, **args1).add(cmd2, **args2).add(cmd2, **args3) + papi_exec.get_replies(err_msg) @staticmethod def vpp_setup_bidirectional_cross_connect(node, interface1, interface2): @@ -289,8 +286,7 @@ class L2Util(object): ' host {host}'.format(host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args1).add(cmd, **args2).get_replies(err_msg).\ - verify_replies(err_msg=err_msg) + papi_exec.add(cmd, **args1).add(cmd, **args2).get_replies(err_msg) @staticmethod def vpp_setup_bidirectional_l2_patch(node, interface1, interface2): @@ -326,8 +322,7 @@ class L2Util(object): ' host {host}'.format(host=node['host']) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args1).add(cmd, **args2).get_replies(err_msg).\ - verify_replies(err_msg=err_msg) + papi_exec.add(cmd, **args1).add(cmd, **args2).get_replies(err_msg) @staticmethod def linux_add_bridge(node, br_name, if_1, if_2, set_up=True): @@ -393,22 +388,19 @@ class L2Util(object): """ cmd = 'bridge_domain_dump' - cmd_reply = 'bridge_domain_details' args = dict(bd_id=int(bd_id)) 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) - - data = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) bd_data = list() if bd_id == Constants.BITWISE_NON_ZERO else dict() - for bridge_domain in data: + for bridge_domain in details: if bd_id == Constants.BITWISE_NON_ZERO: - bd_data.append(bridge_domain[cmd_reply]) + bd_data.append(bridge_domain) else: - if bridge_domain[cmd_reply]['bd_id'] == bd_id: - return bridge_domain[cmd_reply] + if bridge_domain['bd_id'] == bd_id: + return bridge_domain return bd_data @@ -453,8 +445,7 @@ class L2Util(object): err_msg = 'Failed to set VLAN TAG rewrite 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) + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def get_l2_fib_table(node, bd_id): @@ -469,22 +460,16 @@ class L2Util(object): """ cmd = 'l2_fib_table_dump' - cmd_reply = 'l2_fib_table_details' args = dict(bd_id=int(bd_id)) 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) - - data = papi_resp.reply[0]['api_reply'] + details = papi_exec.add(cmd, **args).get_details(err_msg) - fib_data = list() - for fib in data: - fib_item = fib[cmd_reply] + for fib_item in details: fib_item['mac'] = L2Util.bin_to_mac(fib_item['mac']) - fib_data.append(fib_item) - return fib_data + return details @staticmethod def get_l2_fib_entry_by_mac(node, bd_index, mac): diff --git a/resources/libraries/python/Memif.py b/resources/libraries/python/Memif.py index 53fbcef404..5c38ec46f5 100644 --- a/resources/libraries/python/Memif.py +++ b/resources/libraries/python/Memif.py @@ -35,8 +35,8 @@ class Memif(object): pass @staticmethod - def _memif_dump(node): - """Get the memif dump on the given node. + def _memif_details(node): + """Get the memif dump details on the given node. :param node: Given node to get Memif dump from. :type node: dict @@ -44,19 +44,15 @@ class Memif(object): :rtype: list """ with PapiExecutor(node) as papi_exec: - dump = papi_exec.add("memif_dump").get_dump() + details = papi_exec.add("memif_dump").get_details() - data = list() - for item in dump.reply[0]["api_reply"]: - item["memif_details"]["if_name"] = \ - item["memif_details"]["if_name"].rstrip('\x00') - item["memif_details"]["hw_addr"] = \ - L2Util.bin_to_mac(item["memif_details"]["hw_addr"]) - data.append(item) + for memif in details: + memif["if_name"] = memif["if_name"].rstrip('\x00') + memif["hw_addr"] = L2Util.bin_to_mac(memif["hw_addr"]) - logger.debug("MEMIF data:\n{data}".format(data=data)) + logger.debug("MEMIF details:\n{details}".format(details=details)) - return data + return details @staticmethod def _memif_socket_filename_add_del(node, is_add, filename, sid): @@ -83,13 +79,11 @@ class Memif(object): socket_filename=str('/tmp/' + filename) ) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - return data + return papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _memif_create(node, mid, sid, rxq=1, txq=1, role=1): - """Create Memif interface on the given node. + """Create Memif interface on the given node, return its sw_if_index. :param node: Given node to create Memif interface on. :param mid: Memif interface ID. @@ -103,8 +97,8 @@ class Memif(object): :type rxq: int :type txq: int :type role: int - :returns: Verified data from PAPI response. - :rtype: dict + :returns: sw_if_index + :rtype: int """ cmd = 'memif_create' err_msg = 'Failed to create memif interface on host {host}'.format( @@ -117,9 +111,7 @@ class Memif(object): id=int(mid) ) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - return data + return papi_exec.add(cmd, **args).get_sw_if_index(err_msg) @staticmethod def create_memif_interface(node, filename, mid, sid, rxq=1, txq=1, @@ -151,23 +143,24 @@ class Memif(object): Memif._memif_socket_filename_add_del(node, True, filename, sid) # Create memif - rsp = Memif._memif_create(node, mid, sid, rxq=rxq, txq=txq, role=role) + sw_if_index = Memif._memif_create( + node, mid, sid, rxq=rxq, txq=txq, role=role) # Update Topology if_key = Topology.add_new_port(node, 'memif') - Topology.update_interface_sw_if_index(node, if_key, rsp["sw_if_index"]) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) - ifc_name = Memif.vpp_get_memif_interface_name(node, rsp["sw_if_index"]) + ifc_name = Memif.vpp_get_memif_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - ifc_mac = Memif.vpp_get_memif_interface_mac(node, rsp["sw_if_index"]) + ifc_mac = Memif.vpp_get_memif_interface_mac(node, sw_if_index) Topology.update_interface_mac_address(node, if_key, ifc_mac) Topology.update_interface_memif_socket(node, if_key, '/tmp/' + filename) Topology.update_interface_memif_id(node, if_key, mid) Topology.update_interface_memif_role(node, if_key, str(role)) - return rsp["sw_if_index"] + return sw_if_index @staticmethod def show_memif(node): @@ -177,7 +170,7 @@ class Memif(object): :type node: dict """ - Memif._memif_dump(node) + Memif._memif_details(node) @staticmethod def show_memif_on_all_duts(nodes): @@ -191,39 +184,39 @@ class Memif(object): Memif.show_memif(node) @staticmethod - def vpp_get_memif_interface_name(node, sw_if_idx): + def vpp_get_memif_interface_name(node, sw_if_index): """Get Memif interface name from Memif interfaces dump. :param node: DUT node. - :param sw_if_idx: DUT node. + :param sw_if_index: DUT node. :type node: dict - :type sw_if_idx: int + :type sw_if_index: int :returns: Memif interface name, or None if not found. :rtype: str """ - dump = Memif._memif_dump(node) + details = Memif._memif_details(node) - for item in dump: - if item["memif_details"]["sw_if_index"] == sw_if_idx: - return item["memif_details"]["if_name"] + for memif in details: + if memif["sw_if_index"] == sw_if_index: + return memif["if_name"] return None @staticmethod - def vpp_get_memif_interface_mac(node, sw_if_idx): + def vpp_get_memif_interface_mac(node, sw_if_index): """Get Memif interface MAC address from Memif interfaces dump. :param node: DUT node. - :param sw_if_idx: DUT node. + :param sw_if_index: DUT node. :type node: dict - :type sw_if_idx: int + :type sw_if_index: int :returns: Memif interface MAC address, or None if not found. :rtype: str """ - dump = Memif._memif_dump(node) + details = Memif._memif_details(node) - for item in dump: - if item["memif_details"]["sw_if_index"] == sw_if_idx: - return item["memif_details"]["hw_addr"] + for memif in details: + if memif["sw_if_index"] == sw_if_index: + return memif["hw_addr"] return None diff --git a/resources/libraries/python/NATUtil.py b/resources/libraries/python/NATUtil.py index aeeb3bc1a5..5c0278db90 100644 --- a/resources/libraries/python/NATUtil.py +++ b/resources/libraries/python/NATUtil.py @@ -66,8 +66,7 @@ class NATUtil(object): flags=getattr(NATConfigFlags, "NAT_IS_INSIDE").value ) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args_in).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args_in).get_reply(err_msg) int_out_idx = InterfaceUtil.get_sw_if_index(node, int_out) err_msg = 'Failed to set outside interface {int} for NAT44 on host ' \ @@ -78,8 +77,7 @@ class NATUtil(object): flags=getattr(NATConfigFlags, "NAT_IS_OUTSIDE").value ) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args_in).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args_in).get_reply(err_msg) @staticmethod def set_nat44_deterministic(node, ip_in, subnet_in, ip_out, subnet_out): @@ -108,8 +106,7 @@ class NATUtil(object): out_plen=int(subnet_out) ) with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args_in).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + papi_exec.add(cmd, **args_in).get_reply(err_msg) @staticmethod def show_nat(node): @@ -135,9 +132,8 @@ class NATUtil(object): err_msg = 'Failed to get NAT configuration on host {host}'.\ format(host=node['host']) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - logger.debug("NAT Configuration:\n{data}".format(data=pformat(data))) + reply = papi_exec.add(cmd).get_reply(err_msg) + logger.debug("NAT Configuration:\n{reply}".format(reply=pformat(reply))) cmds = [ "nat_worker_dump", diff --git a/resources/libraries/python/PapiExecutor.py b/resources/libraries/python/PapiExecutor.py index 30a90473f5..9ca34d88ae 100644 --- a/resources/libraries/python/PapiExecutor.py +++ b/resources/libraries/python/PapiExecutor.py @@ -18,179 +18,15 @@ import binascii import json from pprint import pformat - from robot.api import logger from resources.libraries.python.Constants import Constants -from resources.libraries.python.ssh import SSH, SSHTimeout from resources.libraries.python.PapiHistory import PapiHistory +from resources.libraries.python.PythonThree import raise_from +from resources.libraries.python.ssh import SSH, SSHTimeout -__all__ = ["PapiExecutor", "PapiResponse"] - - -class PapiResponse(object): - """Class for metadata specifying the Papi reply, stdout, stderr and return - code. - """ - - def __init__(self, papi_reply=None, stdout="", stderr="", requests=None): - """Construct the Papi response by setting the values needed. - - TODO: - Implement 'dump' analogue of verify_replies that would concatenate - the values, so that call sites do not have to do that themselves. - - :param papi_reply: API reply from last executed PAPI command(s). - :param stdout: stdout from last executed PAPI command(s). - :param stderr: stderr from last executed PAPI command(s). - :param requests: List of used PAPI requests. It is used while verifying - replies. If None, expected replies must be provided for verify_reply - and verify_replies methods. - :type papi_reply: list or None - :type stdout: str - :type stderr: str - :type requests: list - """ - - # API reply from last executed PAPI command(s). - self.reply = papi_reply - - # stdout from last executed PAPI command(s). - self.stdout = stdout - - # stderr from last executed PAPI command(s). - self.stderr = stderr - - # List of used PAPI requests. - self.requests = requests - - # List of expected PAPI replies. It is used while verifying replies. - if self.requests: - self.expected_replies = \ - ["{rqst}_reply".format(rqst=rqst) for rqst in self.requests] - - def __str__(self): - """Return string with human readable description of the PapiResponse. - - :returns: Readable description. - :rtype: str - """ - return ( - "papi_reply={papi_reply},stdout={stdout},stderr={stderr}," - "requests={requests}").format( - papi_reply=self.reply, stdout=self.stdout, stderr=self.stderr, - requests=self.requests) - - def __repr__(self): - """Return string executable as Python constructor call. - - :returns: Executable constructor call. - :rtype: str - """ - return "PapiResponse({str})".format(str=str(self)) - - def verify_reply(self, cmd_reply=None, idx=0, - err_msg="Failed to verify PAPI reply."): - """Verify and return data from the PAPI response. - - Note: Use only with a simple request / reply command. In this case the - PAPI reply includes 'retval' which is checked in this method. - - Do not use with 'dump' and 'vpp-stats' methods. - - Use if PAPI response includes only one command reply. - - Use it this way (preferred): - - with PapiExecutor(node) as papi_exec: - data = papi_exec.add('show_version').get_replies().verify_reply() - - or if you must provide the expected reply (not recommended): - - with PapiExecutor(node) as papi_exec: - data = papi_exec.add('show_version').get_replies().\ - verify_reply('show_version_reply') - - :param cmd_reply: PAPI reply. If None, list of 'requests' should have - been provided to the __init__ method as pre-generated list of - replies is used in this method in this case. - The PapiExecutor._execute() method provides the requests - automatically. - :param idx: Index to PapiResponse.reply list. - :param err_msg: The message used if the verification fails. - :type cmd_reply: str - :type idx: int - :type err_msg: str or None - :returns: Verified data from PAPI response. - :rtype: dict - :raises AssertionError: If the PAPI return value is not 0, so the reply - is not valid. - :raises KeyError, IndexError: If the reply does not have expected - structure. - """ - cmd_rpl = self.expected_replies[idx] if cmd_reply is None else cmd_reply - - data = self.reply[idx]['api_reply'][cmd_rpl] - if data['retval'] != 0: - raise AssertionError("{msg}\nidx={idx}, cmd_reply={reply}". - format(msg=err_msg, idx=idx, reply=cmd_rpl)) - - return data - - def verify_replies(self, cmd_replies=None, - err_msg="Failed to verify PAPI reply."): - """Verify and return data from the PAPI response. - - Note: Use only with request / reply commands. In this case each - PAPI reply includes 'retval' which is checked. - - Do not use with 'dump' and 'vpp-stats' methods. - - Use if PAPI response includes more than one command reply. - - Use it this way: - - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd1, **args1).add(cmd2, **args2).add(cmd2, **args3).\ - get_replies(err_msg).verify_replies() - - or if you need the data from the PAPI response: - - with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\ - add(cmd2, **args3).get_replies(err_msg).verify_replies() - - or if you must provide the list of expected replies (not recommended): - - with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\ - add(cmd2, **args3).get_replies(err_msg).\ - verify_replies(cmd_replies=cmd_replies) - - :param cmd_replies: List of PAPI command replies. If None, list of - 'requests' should have been provided to the __init__ method as - pre-generated list of replies is used in this method in this case. - The PapiExecutor._execute() method provides the requests - automatically. - :param err_msg: The message used if the verification fails. - :type cmd_replies: list of str or None - :type err_msg: str - :returns: List of verified data from PAPI response. - :rtype list - :raises AssertionError: If the PAPI response does not include at least - one of specified command replies. - """ - data = list() - - cmd_rpls = self.expected_replies if cmd_replies is None else cmd_replies - - if len(self.reply) != len(cmd_rpls): - raise AssertionError(err_msg) - for idx, cmd_reply in enumerate(cmd_rpls): - data.append(self.verify_reply(cmd_reply, idx, err_msg)) - - return data +__all__ = ["PapiExecutor"] class PapiExecutor(object): @@ -199,7 +35,7 @@ class PapiExecutor(object): Note: Use only with "with" statement, e.g.: with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add('show_version').get_replies(err_msg) + replies = papi_exec.add('show_version').get_replies(err_msg) This class processes three classes of VPP PAPI methods: 1. simple request / reply: method='request', @@ -213,32 +49,31 @@ class PapiExecutor(object): a. One request with no arguments: with PapiExecutor(node) as papi_exec: - data = papi_exec.add('show_version').get_replies().\ - verify_reply() + reply = papi_exec.add('show_version').get_reply() b. Three requests with arguments, the second and the third ones are the same but with different arguments. with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\ - add(cmd2, **args3).get_replies(err_msg).verify_replies() + replies = papi_exec.add(cmd1, **args1).add(cmd2, **args2).\ + add(cmd2, **args3).get_replies(err_msg) 2. Dump functions cmd = 'sw_interface_rx_placement_dump' with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, sw_if_index=ifc['vpp_sw_index']).\ - get_dump(err_msg) + details = papi_exec.add(cmd, sw_if_index=ifc['vpp_sw_index']).\ + get_details(err_msg) 3. vpp-stats path = ['^/if', '/err/ip4-input', '/sys/node/ip4-input'] with PapiExecutor(node) as papi_exec: - data = papi_exec.add(api_name='vpp-stats', path=path).get_stats() + stats = papi_exec.add(api_name='vpp-stats', path=path).get_stats() print('RX interface core 0, sw_if_index 0:\n{0}'.\ - format(data[0]['/if/rx'][0][0])) + format(stats[0]['/if/rx'][0][0])) or @@ -246,11 +81,11 @@ class PapiExecutor(object): path_2 = ['^/if', '/err/ip4-input', '/sys/node/ip4-input'] with PapiExecutor(node) as papi_exec: - data = papi_exec.add('vpp-stats', path=path_1).\ + stats = papi_exec.add('vpp-stats', path=path_1).\ add('vpp-stats', path=path_2).get_stats() print('RX interface core 0, sw_if_index 0:\n{0}'.\ - format(data[1]['/if/rx'][0][0])) + format(stats[1]['/if/rx'][0][0])) Note: In this case, when PapiExecutor method 'add' is used: - its parameter 'csit_papi_command' is used only to keep information @@ -320,94 +155,112 @@ class PapiExecutor(object): :type err_msg: str :type timeout: int :returns: Requested VPP statistics. - :rtype: list + :rtype: list of dict """ paths = [cmd['api_args']['path'] for cmd in self._api_command_list] self._api_command_list = list() - stdout, _ = self._execute_papi( + stdout = self._execute_papi( paths, method='stats', err_msg=err_msg, timeout=timeout) return json.loads(stdout) - def get_stats_reply(self, err_msg="Failed to get statistics.", timeout=120): - """Get VPP Stats reply from VPP Python API. + def get_replies(self, err_msg="Failed to get replies.", timeout=120): + """Get replies from VPP Python API. + + The replies are parsed into dict-like objects, + "retval" field is guaranteed to be zero on success. :param err_msg: The message used if the PAPI command(s) execution fails. :param timeout: Timeout in seconds. :type err_msg: str :type timeout: int - :returns: Requested VPP statistics. - :rtype: list + :returns: Responses, dict objects with fields due to API and "retval". + :rtype: list of dict + :raises RuntimeError: If retval is nonzero, parsing or ssh error. """ + return self._execute(method='request', err_msg=err_msg, timeout=timeout) - args = self._api_command_list[0]['api_args'] - self._api_command_list = list() + def get_reply(self, err_msg="Failed to get reply.", timeout=120): + """Get reply from VPP Python API. - stdout, _ = self._execute_papi( - args, method='stats_request', err_msg=err_msg, timeout=timeout) + The reply is parsed into dict-like object, + "retval" field is guaranteed to be zero on success. - return json.loads(stdout) + TODO: Discuss exception types to raise, unify with inner methods. + + :param err_msg: The message used if the PAPI command(s) execution fails. + :param timeout: Timeout in seconds. + :type err_msg: str + :type timeout: int + :returns: Response, dict object with fields due to API and "retval". + :rtype: dict + :raises AssertionError: If retval is nonzero, parsing or ssh error. + """ + replies = self.get_replies(err_msg=err_msg, timeout=timeout) + if len(replies) != 1: + raise RuntimeError("Expected single reply, got {replies!r}".format( + replies=replies)) + return replies[0] - def get_replies(self, err_msg="Failed to get replies.", - process_reply=True, ignore_errors=False, timeout=120): - """Get reply/replies from VPP Python API. + def get_sw_if_index(self, err_msg="Failed to get reply.", timeout=120): + """Get sw_if_index from reply from VPP Python API. + + Frequently, the caller is only interested in sw_if_index field + of the reply, this wrapper makes such call sites shorter. + + TODO: Discuss exception types to raise, unify with inner methods. :param err_msg: The message used if the PAPI command(s) execution fails. - :param process_reply: Process PAPI reply if True. - :param ignore_errors: If true, the errors in the reply are ignored. :param timeout: Timeout in seconds. :type err_msg: str - :type process_reply: bool - :type ignore_errors: bool :type timeout: int - :returns: Papi response including: papi reply, stdout, stderr and - return code. - :rtype: PapiResponse + :returns: Response, sw_if_index value of the reply. + :rtype: int + :raises AssertionError: If retval is nonzero, parsing or ssh error. """ - return self._execute( - method='request', process_reply=process_reply, - ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout) + return self.get_reply(err_msg=err_msg, timeout=timeout)["sw_if_index"] + + def get_details(self, err_msg="Failed to get dump details.", timeout=120): + """Get dump details from VPP Python API. - def get_dump(self, err_msg="Failed to get dump.", - process_reply=True, ignore_errors=False, timeout=120): - """Get dump from VPP Python API. + The details are parsed into dict-like objects. + The number of details per single dump command can vary, + and all association between details and dumps is lost, + so if you care about the association (as opposed to + logging everything at once for debugging purposes), + it is recommended to call get_details for each dump (type) separately. :param err_msg: The message used if the PAPI command(s) execution fails. - :param process_reply: Process PAPI reply if True. - :param ignore_errors: If true, the errors in the reply are ignored. :param timeout: Timeout in seconds. :type err_msg: str - :type process_reply: bool - :type ignore_errors: bool :type timeout: int - :returns: Papi response including: papi reply, stdout, stderr and - return code. - :rtype: PapiResponse + :returns: Details, dict objects with fields due to API without "retval". + :rtype: list of dict """ - return self._execute( - method='dump', process_reply=process_reply, - ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout) + return self._execute(method='dump', err_msg=err_msg, timeout=timeout) @staticmethod def dump_and_log(node, cmds): - """Dump and log requested information. + """Dump and log requested information, return None. :param node: DUT node. :param cmds: Dump commands to be executed. :type node: dict - :type cmds: list + :type cmds: list of str """ with PapiExecutor(node) as papi_exec: for cmd in cmds: - dump = papi_exec.add(cmd).get_dump() - logger.debug("{cmd}:\n{data}".format( - cmd=cmd, data=pformat(dump.reply[0]["api_reply"]))) + details = papi_exec.add(cmd).get_details() + logger.debug("{cmd}:\n{details}".format( + cmd=cmd, details=pformat(details))) @staticmethod def run_cli_cmd(node, cmd, log=True): - """Run a CLI command. + """Run a CLI command as cli_inband, return the "reply" field of reply. + + Optionally, log the field value. :param node: Node to run command on. :param cmd: The CLI command to be run on the node. @@ -415,8 +268,8 @@ class PapiExecutor(object): :type node: dict :type cmd: str :type log: bool - :returns: Verified data from PAPI response. - :rtype: dict + :returns: CLI output. + :rtype: str """ cli = 'cli_inband' @@ -425,43 +278,12 @@ class PapiExecutor(object): "{host}".format(host=node['host'], cmd=cmd) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cli, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + reply = papi_exec.add(cli, **args).get_reply(err_msg)["reply"] if log: - logger.info("{cmd}:\n{data}".format(cmd=cmd, data=data["reply"])) - - return data - - def execute_should_pass(self, err_msg="Failed to execute PAPI command.", - process_reply=True, ignore_errors=False, - timeout=120): - """Execute the PAPI commands and check the return code. - Raise exception if the PAPI command(s) failed. - - IMPORTANT! - Do not use this method in L1 keywords. Use: - - get_replies() - - get_dump() - This method will be removed soon. + logger.info("{cmd}:\n{reply}".format(cmd=cmd, reply=reply)) - :param err_msg: The message used if the PAPI command(s) execution fails. - :param process_reply: Indicate whether or not to process PAPI reply. - :param ignore_errors: If true, the errors in the reply are ignored. - :param timeout: Timeout in seconds. - :type err_msg: str - :type process_reply: bool - :type ignore_errors: bool - :type timeout: int - :returns: Papi response including: papi reply, stdout, stderr and - return code. - :rtype: PapiResponse - :raises AssertionError: If PAPI command(s) execution failed. - """ - # TODO: Migrate callers to get_replies and delete this method. - return self.get_replies( - process_reply=process_reply, ignore_errors=ignore_errors, - err_msg=err_msg, timeout=timeout) + return reply @staticmethod def _process_api_data(api_d): @@ -572,46 +394,45 @@ class PapiExecutor(object): :type method: str :type err_msg: str :type timeout: int - :returns: Stdout and stderr. - :rtype: 2-tuple of str + :returns: Stdout from remote python utility, to be parsed by caller. + :rtype: str :raises SSHTimeout: If PAPI command(s) execution has timed out. :raises RuntimeError: If PAPI executor failed due to another reason. :raises AssertionError: If PAPI command(s) execution has failed. """ if not api_data: - RuntimeError("No API data provided.") + raise RuntimeError("No API data provided.") json_data = json.dumps(api_data) \ if method in ("stats", "stats_request") \ else json.dumps(self._process_api_data(api_data)) cmd = "{fw_dir}/{papi_provider} --method {method} --data '{json}'".\ - format(fw_dir=Constants.REMOTE_FW_DIR, - papi_provider=Constants.RESOURCES_PAPI_PROVIDER, - method=method, - json=json_data) + format( + fw_dir=Constants.REMOTE_FW_DIR, method=method, json=json_data, + papi_provider=Constants.RESOURCES_PAPI_PROVIDER) try: - ret_code, stdout, stderr = self._ssh.exec_command_sudo( + ret_code, stdout, _ = self._ssh.exec_command_sudo( cmd=cmd, timeout=timeout, log_stdout_err=False) + # TODO: Fail on non-empty stderr? except SSHTimeout: logger.error("PAPI command(s) execution timeout on host {host}:" "\n{apis}".format(host=self._node["host"], apis=api_data)) raise - except Exception: - raise RuntimeError("PAPI command(s) execution on host {host} " - "failed: {apis}".format(host=self._node["host"], - apis=api_data)) + except Exception as exc: + raise_from(RuntimeError( + "PAPI command(s) execution on host {host} " + "failed: {apis}".format( + host=self._node["host"], apis=api_data)), exc) if ret_code != 0: raise AssertionError(err_msg) - return stdout, stderr + return stdout - def _execute(self, method='request', process_reply=True, - ignore_errors=False, err_msg="", timeout=120): - """Turn internal command list into proper data and execute; return - PAPI response. + def _execute(self, method='request', err_msg="", timeout=120): + """Turn internal command list into data and execute; return replies. This method also clears the internal command list. @@ -619,22 +440,18 @@ class PapiExecutor(object): Do not use this method in L1 keywords. Use: - get_stats() - get_replies() - - get_dump() + - get_details() :param method: VPP Python API method. Supported methods are: 'request', 'dump' and 'stats'. - :param process_reply: Process PAPI reply if True. - :param ignore_errors: If true, the errors in the reply are ignored. :param err_msg: The message used if the PAPI command(s) execution fails. :param timeout: Timeout in seconds. :type method: str - :type process_reply: bool - :type ignore_errors: bool :type err_msg: str :type timeout: int - :returns: Papi response including: papi reply, stdout, stderr and - return code. - :rtype: PapiResponse + :returns: Papi responses parsed into a dict-like object, + with field due to API or stats hierarchy. + :rtype: list of dict :raises KeyError: If the reply is not correct. """ @@ -643,31 +460,36 @@ class PapiExecutor(object): # Clear first as execution may fail. self._api_command_list = list() - stdout, stderr = self._execute_papi( + stdout = self._execute_papi( local_list, method=method, err_msg=err_msg, timeout=timeout) - papi_reply = list() - if process_reply: - try: - json_data = json.loads(stdout) - except ValueError: - logger.error("An error occured while processing the PAPI " - "request:\n{rqst}".format(rqst=local_list)) - raise - for data in json_data: - try: - api_reply_processed = dict( - api_name=data["api_name"], - api_reply=self._process_reply(data["api_reply"])) - except KeyError: - if ignore_errors: - continue - else: - raise - papi_reply.append(api_reply_processed) - - # Log processed papi reply to be able to check API replies changes - logger.debug("Processed PAPI reply: {reply}".format(reply=papi_reply)) - - return PapiResponse( - papi_reply=papi_reply, stdout=stdout, stderr=stderr, - requests=[rqst["api_name"] for rqst in local_list]) + replies = list() + try: + json_data = json.loads(stdout) + except ValueError as err: + raise_from(RuntimeError(err_msg), err) + for data in json_data: + if method == "request": + api_reply = self._process_reply(data["api_reply"]) + # api_reply contains single key, *_reply. + obj = api_reply.values()[0] + retval = obj["retval"] + if retval != 0: + # TODO: What exactly to log and raise here? + err = AssertionError("Got retval {rv!r}".format(rv=retval)) + raise_from(AssertionError(err_msg), err, level="INFO") + replies.append(obj) + elif method == "dump": + api_reply = self._process_reply(data["api_reply"]) + # api_reply is a list where item contas single key, *_details. + for item in api_reply: + obj = item.values()[0] + replies.append(obj) + else: + # TODO: Implement support for stats. + raise RuntimeError("Unsuported method {method}".format( + method=method)) + + # TODO: Make logging optional? + logger.debug("PAPI replies: {replies}".format(replies=replies)) + + return replies diff --git a/resources/libraries/python/ProxyArp.py b/resources/libraries/python/ProxyArp.py index 6cd723d327..a0a54fb103 100644 --- a/resources/libraries/python/ProxyArp.py +++ b/resources/libraries/python/ProxyArp.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016 Cisco and/or its affiliates. +# Copyright (c) 2019 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -54,5 +54,4 @@ class ProxyArp(object): err_msg = 'Failed to enable proxy ARP 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) + papi_exec.add(cmd, **args).get_reply(err_msg) diff --git a/resources/libraries/python/PythonThree.py b/resources/libraries/python/PythonThree.py new file mode 100644 index 0000000000..6ecea80173 --- /dev/null +++ b/resources/libraries/python/PythonThree.py @@ -0,0 +1,39 @@ +# Copyright (c) 2019 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Library holding utility functions to be replaced by later Python builtins.""" + +from robot.api import logger + + +def raise_from(raising, excepted, level="WARN"): + """Function to be replaced by "raise from" in Python 3. + + Neither "six" nor "future" offer good enough implementation right now. + chezsoi.org/lucas/blog/displaying-chained-exceptions-stacktraces-in-python-2 + + Current implementation just logs the excepted error, and raises the new one. + For allower log level values, see: + robot-framework.readthedocs.io/en/latest/autodoc/robot.api.html#log-levels + + :param raising: The exception to raise. + :param excepted: The exception we excepted and want to log. + :param level: Robot logger logging level to log with. + :type raising: BaseException + :type excepted: BaseException + :type level: str + :raises: raising + """ + logger.write("Excepted: {exc!r}\nRaising: {rai!r}".format( + exc=excepted, rai=raising), level) + raise raising diff --git a/resources/libraries/python/Tap.py b/resources/libraries/python/Tap.py index 4e04a4ab33..2a1d7efbc8 100644 --- a/resources/libraries/python/Tap.py +++ b/resources/libraries/python/Tap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 Cisco and/or its affiliates. +# Copyright (c) 2019 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -43,28 +43,28 @@ class Tap(object): resp = vat.vat_terminal_exec_cmd_from_template('tap.vat', tap_command=command, tap_arguments=args) - sw_if_idx = resp[0]['sw_if_index'] + sw_if_index = resp[0]['sw_if_index'] if_key = Topology.add_new_port(node, 'tap') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) - ifc_name = Tap.vpp_get_tap_interface_name(node, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) + ifc_name = Tap.vpp_get_tap_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) if mac is None: - mac = Tap.vpp_get_tap_interface_mac(node, sw_if_idx) + mac = Tap.vpp_get_tap_interface_mac(node, sw_if_index) Topology.update_interface_mac_address(node, if_key, mac) Topology.update_interface_tap_dev_name(node, if_key, tap_name) - return sw_if_idx + return sw_if_index @staticmethod - def modify_tap_interface(node, if_index, tap_name, mac=None): + def modify_tap_interface(node, sw_if_index, tap_name, mac=None): """Modify tap interface like linux interface name or VPP MAC. :param node: Node to modify tap on. - :param if_index: Index of tap interface to be modified. + :param sw_if_index: Index of tap interface to be modified. :param tap_name: Tap interface name for linux tap. :param mac: Optional MAC address for VPP tap. :type node: dict - :type if_index: int + :type sw_if_index: int :type tap_name: str :type mac: str :returns: Returns a interface index. @@ -73,14 +73,14 @@ class Tap(object): command = 'modify' if mac is not None: args = 'sw_if_index {} tapname {} mac {}'.format( - if_index, tap_name, mac) + sw_if_index, tap_name, mac) else: - args = 'sw_if_index {} tapname {}'.format(if_index, tap_name) + args = 'sw_if_index {} tapname {}'.format(sw_if_index, tap_name) with VatTerminal(node) as vat: resp = vat.vat_terminal_exec_cmd_from_template('tap.vat', tap_command=command, tap_arguments=args) - if_key = Topology.get_interface_by_sw_index(node, if_index) + if_key = Topology.get_interface_by_sw_index(node, sw_if_index) Topology.update_interface_tap_dev_name(node, if_key, tap_name) if mac: Topology.update_interface_mac_address(node, if_key, mac) @@ -88,17 +88,17 @@ class Tap(object): return resp[0]['sw_if_index'] @staticmethod - def delete_tap_interface(node, if_index): + def delete_tap_interface(node, sw_if_index): """Delete tap interface. :param node: Node to delete tap on. - :param if_index: Index of tap interface to be deleted. + :param sw_if_index: Index of tap interface to be deleted. :type node: dict - :type if_index: int + :type sw_if_index: int :raises RuntimeError: Deletion was not successful. """ command = 'delete' - args = 'sw_if_index {}'.format(if_index) + args = 'sw_if_index {}'.format(sw_if_index) with VatTerminal(node) as vat: resp = vat.vat_terminal_exec_cmd_from_template('tap.vat', tap_command=command, @@ -106,7 +106,7 @@ class Tap(object): if int(resp[0]['retval']) != 0: raise RuntimeError( 'Could not remove tap interface: {}'.format(resp)) - if_key = Topology.get_interface_sw_index(node, if_index) + if_key = Topology.get_interface_sw_index(node, sw_if_index) Topology.remove_port(node, if_key) @staticmethod @@ -125,13 +125,13 @@ class Tap(object): 'Tap interface :{} does not exist'.format(tap_name)) @staticmethod - def vpp_get_tap_interface_name(node, sw_if_idx): + def vpp_get_tap_interface_name(node, sw_if_index): """Get VPP tap interface name from hardware interfaces dump. :param node: DUT node. - :param sw_if_idx: DUT node. + :param sw_if_index: DUT node. :type node: dict - :type sw_if_idx: int + :type sw_if_index: int :returns: VPP tap interface name. :rtype: str """ @@ -142,19 +142,19 @@ class Tap(object): for line in str(response[0]).splitlines(): if line.startswith('tap-'): line_split = line.split() - if line_split[1] == sw_if_idx: + if line_split[1] == sw_if_index: return line_split[0] return None @staticmethod - def vpp_get_tap_interface_mac(node, sw_if_idx): + def vpp_get_tap_interface_mac(node, sw_if_index): """Get tap interface MAC address from hardware interfaces dump. :param node: DUT node. - :param sw_if_idx: DUT node. + :param sw_if_index: DUT node. :type node: dict - :type sw_if_idx: int + :type sw_if_index: int :returns: Tap interface MAC address. :rtype: str """ @@ -169,7 +169,7 @@ class Tap(object): return line_split[-1] if line.startswith('tap-'): line_split = line.split() - if line_split[1] == sw_if_idx: + if line_split[1] == sw_if_index: tap_if_match = True return None diff --git a/resources/libraries/python/TestConfig.py b/resources/libraries/python/TestConfig.py index a5bd5650a2..284b2e8cf4 100644 --- a/resources/libraries/python/TestConfig.py +++ b/resources/libraries/python/TestConfig.py @@ -189,9 +189,8 @@ class TestConfig(object): add(cmd2, history=history, **args2).\ add(cmd3, history=history, **args3) if i > 0 and i % (Constants.PAPI_MAX_API_BULK / 3) == 0: - papi_exec.get_replies(err_msg).verify_replies( - err_msg=err_msg) - papi_exec.get_replies().verify_replies() + papi_exec.get_replies(err_msg) + papi_exec.get_replies() return vxlan_count @@ -298,10 +297,9 @@ class TestConfig(object): papi_exec.add(cmd, history=history, **args1). \ add(cmd, history=history, **args2) if i > 0 and i % (Constants.PAPI_MAX_API_BULK / 2) == 0: - papi_exec.get_replies(err_msg).verify_replies( - err_msg=err_msg) + papi_exec.get_replies(err_msg) papi_exec.add(cmd, **args1).add(cmd, **args2) - papi_exec.get_replies().verify_replies() + papi_exec.get_replies() @staticmethod def vpp_put_vxlan_and_vlan_interfaces_to_bridge_domain( @@ -423,6 +421,5 @@ class TestConfig(object): add(cmd3, history=history, **args3). \ add(cmd3, history=history, **args4) if i > 0 and i % (Constants.PAPI_MAX_API_BULK / 4) == 0: - papi_exec.get_replies(err_msg).verify_replies( - err_msg=err_msg) - papi_exec.get_replies().verify_replies() + papi_exec.get_replies(err_msg) + papi_exec.get_replies() diff --git a/resources/libraries/python/VPPUtil.py b/resources/libraries/python/VPPUtil.py index 03d39d3e23..676671f15e 100644 --- a/resources/libraries/python/VPPUtil.py +++ b/resources/libraries/python/VPPUtil.py @@ -19,6 +19,7 @@ from robot.api import logger from resources.libraries.python.Constants import Constants from resources.libraries.python.DUTSetup import DUTSetup +from resources.libraries.python.L2Util import L2Util from resources.libraries.python.PapiExecutor import PapiExecutor from resources.libraries.python.ssh import exec_cmd_no_error from resources.libraries.python.topology import NodeType @@ -162,16 +163,16 @@ class VPPUtil(object): :rtype: str """ with PapiExecutor(node) as papi_exec: - data = papi_exec.add('show_version').get_replies().verify_reply() - version = ('VPP version: {ver}\n'. - format(ver=data['version'].rstrip('\0x00'))) + reply = papi_exec.add('show_version').get_reply() + return_version = reply['version'].rstrip('\0x00') + version = 'VPP version: {ver}\n'.format(ver=return_version) if verbose: version += ('Compile date: {date}\n' - 'Compile location: {cl}\n '. - format(date=data['build_date'].rstrip('\0x00'), - cl=data['build_directory'].rstrip('\0x00'))) + 'Compile location: {cl}\n'. + format(date=reply['build_date'].rstrip('\0x00'), + cl=reply['build_directory'].rstrip('\0x00'))) logger.info(version) - return data['version'].rstrip('\0x00') + return return_version @staticmethod def show_vpp_version_on_all_duts(nodes): @@ -193,27 +194,19 @@ class VPPUtil(object): """ cmd = 'sw_interface_dump' - cmd_reply = 'sw_interface_details' args = dict(name_filter_valid=0, name_filter='') err_msg = 'Failed to get interface dump on host {host}'.format( host=node['host']) with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg) - - papi_if_dump = papi_resp.reply[0]['api_reply'] - - if_data = list() - for item in papi_if_dump: - data = item[cmd_reply] - data['interface_name'] = data['interface_name'].rstrip('\x00') - data['tag'] = data['tag'].rstrip('\x00') - data['l2_address'] = str(':'.join(binascii.hexlify( - data['l2_address'])[i:i + 2] for i in range(0, 12, 2)). - decode('ascii')) - if_data.append(data) + details = papi_exec.add(cmd, **args).get_details(err_msg) + + for if_dump in details: + if_dump['interface_name'] = if_dump['interface_name'].rstrip('\x00') + if_dump['tag'] = if_dump['tag'].rstrip('\x00') + if_dump['l2_address'] = L2Util.bin_to_mac(if_dump['l2_address']) # TODO: return only base data - logger.trace('Interface data of host {host}:\n{if_data}'.format( - host=node['host'], if_data=if_data)) + logger.trace('Interface data of host {host}:\n{details}'.format( + host=node['host'], details=details)) @staticmethod def vpp_enable_traces_on_dut(node, fail_on_error=False): @@ -301,7 +294,7 @@ class VPPUtil(object): :returns: VPP log data. :rtype: list """ - return PapiExecutor.run_cli_cmd(node, "show log")["reply"] + return PapiExecutor.run_cli_cmd(node, "show log") @staticmethod def vpp_show_threads(node): @@ -313,11 +306,10 @@ class VPPUtil(object): :rtype: list """ with PapiExecutor(node) as papi_exec: - data = papi_exec.add('show_threads').get_replies().\ - verify_reply()["thread_data"] + reply = papi_exec.add('show_threads').get_reply() threads_data = list() - for thread in data: + for thread in reply["thread_data"]: thread_data = list() for item in thread: if isinstance(item, unicode): diff --git a/resources/libraries/python/VhostUser.py b/resources/libraries/python/VhostUser.py index 0c7a79e266..916d0829ee 100644 --- a/resources/libraries/python/VhostUser.py +++ b/resources/libraries/python/VhostUser.py @@ -33,21 +33,17 @@ class VhostUser(object): response. :rtype: list """ + cmd = "sw_interface_vhost_user_dump" with PapiExecutor(node) as papi_exec: - dump = papi_exec.add("sw_interface_vhost_user_dump").get_dump() + details = papi_exec.add(cmd).get_details() - key = "sw_interface_vhost_user_details" - data = list() - for item in dump.reply[0]["api_reply"]: - item[key]["interface_name"] = \ - item[key]["interface_name"].rstrip('\x00') - item[key]["sock_filename"] = \ - item[key]["sock_filename"].rstrip('\x00') - data.append(item) + for vhost in details: + vhost["interface_name"] = vhost["interface_name"].rstrip('\x00') + vhost["sock_filename"] = vhost["sock_filename"].rstrip('\x00') - logger.debug("VhostUser data:\n{data}".format(data=data)) + logger.debug("VhostUser details:\n{details}".format(details=details)) - return data + return details @staticmethod def vpp_create_vhost_user_interface(node, socket): @@ -67,25 +63,21 @@ class VhostUser(object): sock_filename=str(socket) ) with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - - # Extract sw_if_idx: - sw_if_idx = data["sw_if_index"] + sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg) # Update the Topology: if_key = Topology.add_new_port(node, 'vhost') - Topology.update_interface_sw_if_index(node, if_key, sw_if_idx) + Topology.update_interface_sw_if_index(node, if_key, sw_if_index) - ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx) + ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) - ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx) + ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index) Topology.update_interface_mac_address(node, if_key, ifc_mac) Topology.update_interface_vhost_socket(node, if_key, socket) - return sw_if_idx + return sw_if_index @staticmethod def get_vhost_user_if_name_by_sock(node, socket): diff --git a/resources/libraries/python/VppCounters.py b/resources/libraries/python/VppCounters.py index cc3554829e..dd1553538d 100644 --- a/resources/libraries/python/VppCounters.py +++ b/resources/libraries/python/VppCounters.py @@ -29,33 +29,6 @@ class VppCounters(object): self._stats_table = None @staticmethod - def _run_cli_cmd(node, cmd, log=True): - """Run a CLI command. - - :param node: Node to run command on. - :param cmd: The CLI command to be run on the node. - :param log: If True, the response is logged. - :type node: dict - :type cmd: str - :type log: bool - :returns: Verified data from PAPI response. - :rtype: dict - """ - cli = 'cli_inband' - args = dict(cmd=cmd) - err_msg = "Failed to run 'cli_inband {cmd}' PAPI command on host " \ - "{host}".format(host=node['host'], cmd=cmd) - - with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cli, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) - - if log: - logger.info("{cmd}:\n{data}".format(cmd=cmd, data=data["reply"])) - - return data - - @staticmethod def _get_non_zero_items(data): """Extract and return non-zero items from the input data. @@ -73,7 +46,7 @@ class VppCounters(object): :param node: Node to run command on. :type node: dict """ - VppCounters._run_cli_cmd(node, 'show errors') + PapiExecutor.run_cli_cmd(node, 'show errors') @staticmethod def vpp_show_errors_verbose(node): @@ -82,7 +55,7 @@ class VppCounters(object): :param node: Node to run command on. :type node: dict """ - VppCounters._run_cli_cmd(node, 'show errors verbose') + PapiExecutor.run_cli_cmd(node, 'show errors verbose') @staticmethod def vpp_show_errors_on_all_duts(nodes, verbose=False): @@ -112,6 +85,7 @@ class VppCounters(object): args = dict(path='^/sys/node') with PapiExecutor(node) as papi_exec: stats = papi_exec.add("vpp-stats", **args).get_stats()[0] + # TODO: Introduce get_stat? names = stats['/sys/node/names'] @@ -187,7 +161,7 @@ class VppCounters(object): :param node: Node to run command on. :type node: dict """ - VppCounters._run_cli_cmd(node, 'show hardware detail') + PapiExecutor.run_cli_cmd(node, 'show hardware detail') @staticmethod def vpp_clear_runtime(node): @@ -198,7 +172,7 @@ class VppCounters(object): :returns: Verified data from PAPI response. :rtype: dict """ - return VppCounters._run_cli_cmd(node, 'clear runtime', log=False) + return PapiExecutor.run_cli_cmd(node, 'clear runtime', log=False) @staticmethod def clear_runtime_counters_on_all_duts(nodes): @@ -220,7 +194,7 @@ class VppCounters(object): :returns: Verified data from PAPI response. :rtype: dict """ - return VppCounters._run_cli_cmd(node, 'clear interfaces', log=False) + return PapiExecutor.run_cli_cmd(node, 'clear interfaces', log=False) @staticmethod def clear_interface_counters_on_all_duts(nodes): @@ -242,7 +216,7 @@ class VppCounters(object): :returns: Verified data from PAPI response. :rtype: dict """ - return VppCounters._run_cli_cmd(node, 'clear hardware', log=False) + return PapiExecutor.run_cli_cmd(node, 'clear hardware', log=False) @staticmethod def clear_hardware_counters_on_all_duts(nodes): @@ -264,7 +238,7 @@ class VppCounters(object): :returns: Verified data from PAPI response. :rtype: dict """ - return VppCounters._run_cli_cmd(node, 'clear errors', log=False) + return PapiExecutor.run_cli_cmd(node, 'clear errors', log=False) @staticmethod def clear_error_counters_on_all_duts(nodes): @@ -315,9 +289,9 @@ class VppCounters(object): """ version = 'ip6' if is_ipv6 else 'ip4' topo = Topology() - if_index = topo.get_interface_sw_index(node, interface) - if if_index is None: - logger.trace('{i} sw_index not found.'.format(i=interface)) + sw_if_index = topo.get_interface_sw_index(node, interface) + if sw_if_index is None: + logger.trace('{i} sw_if_index not found.'.format(i=interface)) return 0 if_counters = self._stats_table.get('interface_counters') @@ -327,7 +301,7 @@ class VppCounters(object): for counter in if_counters: if counter['vnet_counter_type'] == version: data = counter['data'] - return data[if_index] + return data[sw_if_index] logger.trace('{i} {v} counter not found.'.format( i=interface, v=version)) return 0 diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index e4ac93fb1b..cee35868e4 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -24,31 +24,13 @@ from robot.api import logger from scp import SCPClient, SCPException from resources.libraries.python.OptionString import OptionString +from resources.libraries.python.PythonThree import raise_from __all__ = ["exec_cmd", "exec_cmd_no_error"] # TODO: load priv key -def raise_from(raising, excepted): - """Function to be replaced by "raise from" in Python 3. - - Neither "six" nor "future" offer good enough implementation right now. - chezsoi.org/lucas/blog/displaying-chained-exceptions-stacktraces-in-python-2 - - Current implementation just logs excepted error, and raises the new one. - - :param raising: The exception to raise. - :param excepted: The exception we excepted and want to log. - :type raising: BaseException - :type excepted: BaseException - :raises: raising - """ - logger.error("Excepted: {exc!r}\nRaising: {rai!r}".format( - exc=excepted, rai=raising)) - raise raising - - class SSHTimeout(Exception): """This exception is raised when a timeout occurs.""" pass diff --git a/resources/libraries/python/telemetry/SPAN.py b/resources/libraries/python/telemetry/SPAN.py index c282c6160b..2033525f55 100644 --- a/resources/libraries/python/telemetry/SPAN.py +++ b/resources/libraries/python/telemetry/SPAN.py @@ -37,14 +37,14 @@ class SPAN(object): source/destination interface pair. :rtype: list of dict """ + cmd = "sw_interface_span_dump" args = dict( is_l2=1 if is_l2 else 0 ) with PapiExecutor(node) as papi_exec: - dump = papi_exec.add("sw_interface_span_dump", **args). \ - get_dump().reply[0]["api_reply"] + details = papi_exec.add(cmd, **args).get_details() - return dump + return details @staticmethod def vpp_get_span_configuration_by_interface(node, dst_interface, @@ -71,9 +71,8 @@ class SPAN(object): node, dst_interface, "sw_if_index") src_interfaces = [] for item in data: - if item["sw_interface_span_details"]["sw_if_index_to"] == dst_int: - src_interfaces.append( - item["sw_interface_span_details"]["sw_if_index_from"]) + if item["sw_if_index_to"] == dst_int: + src_interfaces.append(item["sw_if_index_from"]) if ret_format != "sw_if_index": src_interfaces = [ diff --git a/resources/libraries/python/topology.py b/resources/libraries/python/topology.py index d31d17830c..b06cf7dc4e 100644 --- a/resources/libraries/python/topology.py +++ b/resources/libraries/python/topology.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 Cisco and/or its affiliates. +# Copyright (c) 2019 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -382,28 +382,28 @@ class Topology(object): return retval @staticmethod - def get_interface_by_sw_index(node, sw_index): + def get_interface_by_sw_index(node, sw_if_index): """Return interface name of link on node. This method returns the interface name associated with a software interface index assigned to the interface by vpp for a given node. :param node: The node topology dictionary. - :param sw_index: Sw_index of the link that a interface is connected to. + :param sw_if_index: sw_if_index of the link that a interface is connected to. :type node: dict - :type sw_index: int + :type sw_if_index: int :returns: Interface name of the interface connected to the given link. :rtype: str """ return Topology._get_interface_by_key_value(node, "vpp_sw_index", - sw_index) + sw_if_index) @staticmethod def get_interface_sw_index(node, iface_key): """Get VPP sw_if_index for the interface using interface key. :param node: Node to get interface sw_if_index on. - :param iface_key: Interface key from topology file, or sw_index. + :param iface_key: Interface key from topology file, or sw_if_index. :type node: dict :type iface_key: str/int :returns: Return sw_if_index or None if not found. @@ -710,7 +710,7 @@ class Topology(object): def get_node_link_mac(node, link_name): """Return interface mac address by link name. - :param node: Node to get interface sw_index on. + :param node: Node to get interface sw_if_index on. :param link_name: Link name. :type node: dict :type link_name: str diff --git a/resources/libraries/robot/performance/performance_configuration.robot b/resources/libraries/robot/performance/performance_configuration.robot index fee12959da..2b2c505658 100644 --- a/resources/libraries/robot/performance/performance_configuration.robot +++ b/resources/libraries/robot/performance/performance_configuration.robot @@ -1270,7 +1270,7 @@ | | Run Keyword Unless | '${if2_status}' == 'PASS' | | ... | VPP Enslave Physical Interface | ${dut2} | ${dut2_if1_2} | | ... | ${dut2_eth_bond_if1} -| | VPP Show Bond Data On All Nodes | ${nodes} | details=${TRUE} +| | VPP Show Bond Data On All Nodes | ${nodes} | verbose=${TRUE} | | Initialize VLAN dot1q sub-interfaces in circular topology | | ... | ${dut1} | ${dut1_eth_bond_if1} | ${dut2} | ${dut2_eth_bond_if1} | | ... | ${subid} @@ -2116,7 +2116,7 @@ | | Run Keyword Unless | '${if2_status}' == 'PASS' | | ... | VPP Enslave Physical Interface | ${dut2} | ${dut2_if1_2} | | ... | ${dut2_eth_bond_if1} -| | VPP Show Bond Data On All Nodes | ${nodes} | details=${TRUE} +| | VPP Show Bond Data On All Nodes | ${nodes} | verbose=${TRUE} | | Initialize VLAN dot1q sub-interfaces in circular topology | | ... | ${dut1} | ${dut1_eth_bond_if1} | ${dut2} | ${dut2_eth_bond_if1} | | ... | ${subid} |