+ 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.
+ :type err_msg: str
+ :returns: Details, dict objects with fields due to API without "retval".
+ :rtype: list of dict
+ """
+ return self._execute(err_msg)
+
+ @staticmethod
+ def run_cli_cmd(
+ node, cli_cmd, log=True, remote_vpp_socket=Constants.SOCKSVR_PATH):
+ """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 cli_cmd: The CLI command to be run on the node.
+ :param remote_vpp_socket: Path to remote socket to tunnel to.
+ :param log: If True, the response is logged.
+ :type node: dict
+ :type remote_vpp_socket: str
+ :type cli_cmd: str
+ :type log: bool
+ :returns: CLI output.
+ :rtype: str
+ """
+ cmd = u"cli_inband"
+ args = dict(
+ cmd=cli_cmd
+ )
+ err_msg = f"Failed to run 'cli_inband {cli_cmd}' PAPI command " \
+ f"on host {node[u'host']}"
+
+ with PapiSocketExecutor(node, remote_vpp_socket) as papi_exec:
+ reply = papi_exec.add(cmd, **args).get_reply(err_msg)["reply"]
+ if log:
+ logger.info(
+ f"{cli_cmd} ({node[u'host']} - {remote_vpp_socket}):\n"
+ f"{reply.strip()}"
+ )
+ return reply
+
+ @staticmethod
+ def run_cli_cmd_on_all_sockets(node, cli_cmd, log=True):
+ """Run a CLI command as cli_inband, on all sockets in topology file.
+
+ :param node: Node to run command on.
+ :param cli_cmd: The CLI command to be run on the node.
+ :param log: If True, the response is logged.
+ :type node: dict
+ :type cli_cmd: str
+ :type log: bool
+ """
+ sockets = Topology.get_node_sockets(node, socket_type=SocketType.PAPI)
+ if sockets:
+ for socket in sockets.values():
+ PapiSocketExecutor.run_cli_cmd(
+ node, cli_cmd, log=log, remote_vpp_socket=socket
+ )
+
+ @staticmethod
+ def dump_and_log(node, cmds):
+ """Dump and log requested information, return None.
+
+ :param node: DUT node.
+ :param cmds: Dump commands to be executed.
+ :type node: dict
+ :type cmds: list of str
+ """
+ with PapiSocketExecutor(node) as papi_exec:
+ for cmd in cmds:
+ dump = papi_exec.add(cmd).get_details()
+ logger.debug(f"{cmd}:\n{pformat(dump)}")
+
+ def _execute(self, err_msg=u"Undefined error message", exp_rv=0):
+ """Turn internal command list into data and execute; return replies.
+
+ This method also clears the internal command list.
+
+ IMPORTANT!
+ Do not use this method in L1 keywords. Use:
+ - get_replies()
+ - get_reply()
+ - get_sw_if_index()
+ - get_details()
+
+ :param err_msg: The message used if the PAPI command(s) execution fails.
+ :type err_msg: str
+ :returns: Papi responses parsed into a dict-like object,
+ with fields due to API (possibly including retval).
+ :rtype: list of dict
+ :raises RuntimeError: If the replies are not all correct.
+ """
+ vpp_instance = self.get_connected_client()
+ local_list = self._api_command_list
+ # Clear first as execution may fail.
+ self._api_command_list = list()
+ replies = list()
+ for command in local_list:
+ api_name = command[u"api_name"]
+ papi_fn = getattr(vpp_instance.api, api_name)
+ try:
+ try:
+ reply = papi_fn(**command[u"api_args"])
+ except (IOError, struct.error) as err:
+ # Occasionally an error happens, try reconnect.
+ logger.warn(f"Reconnect after error: {err!r}")
+ vpp_instance.disconnect()
+ # Testing shows immediate reconnect fails.
+ time.sleep(1)
+ vpp_instance.connect_sync(u"csit_socket")
+ logger.trace(u"Reconnected.")
+ reply = papi_fn(**command[u"api_args"])
+ except (AttributeError, IOError, struct.error) as err:
+ raise AssertionError(err_msg) from err
+ # *_dump commands return list of objects, convert, ordinary reply.
+ if not isinstance(reply, list):
+ reply = [reply]
+ for item in reply:
+ self.crc_checker.check_api_name(item.__class__.__name__)
+ dict_item = dictize(item)
+ if u"retval" in dict_item.keys():
+ # *_details messages do not contain retval.
+ retval = dict_item[u"retval"]
+ if retval != exp_rv:
+ # TODO: What exactly to log and raise here?
+ raise AssertionError(
+ f"Retval {retval!r} does not match expected "
+ f"retval {exp_rv!r}"
+ )
+ replies.append(dict_item)
+ return replies
+
+
+class Disconnector:
+ """Class for holding a single keyword."""
+
+ @staticmethod
+ def disconnect_all_papi_connections():
+ """Disconnect all connected client instances, tear down the SSH tunnels.
+
+ Also remove the local sockets by deleting the temporary directory.
+ Put disconnected client instances to the reuse list.
+ The added attributes are not cleaned up,
+ as their values will get overwritten on next connect.
+
+ Call this method just before killing/restarting all VPP instances.
+
+ This could be a class method of PapiSocketExecutor.
+ But Robot calls methods on instances, and it would be weird
+ to give node argument for constructor in import.
+ Also, as we have a class of the same name as the module,
+ the keywords defined on module level are not accessible.
+ """
+ cls = PapiSocketExecutor
+ # Iterate over copy of entries so deletions do not mess with iterator.
+ keys_copy = list(cls.conn_cache.keys())
+ for key in keys_copy:
+ cls.disconnect_by_key(key)
+
+
+class PapiExecutor:
+ """Contains methods for executing VPP Python API commands on DUTs.
+
+ TODO: Remove .add step, make get_stats accept paths directly.
+
+ This class processes only one type of VPP PAPI methods: vpp-stats.
+
+ The recommended ways of use are (examples):
+
+ path = ['^/if', '/err/ip4-input', '/sys/node/ip4-input']
+ with PapiExecutor(node) as papi_exec:
+ stats = papi_exec.add(api_name='vpp-stats', path=path).get_stats()
+
+ print('RX interface core 0, sw_if_index 0:\n{0}'.\
+ format(stats[0]['/if/rx'][0][0]))
+
+ or
+
+ path_1 = ['^/if', ]
+ path_2 = ['^/if', '/err/ip4-input', '/sys/node/ip4-input']
+ with PapiExecutor(node) as papi_exec:
+ 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(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
+ that vpp-stats are requested. It is not further processed but it is
+ included in the PAPI history this way:
+ vpp-stats(path=['^/if', '/err/ip4-input', '/sys/node/ip4-input'])
+ Always use csit_papi_command="vpp-stats" if the VPP PAPI method
+ is "stats".
+ - the second parameter must be 'path' as it is used by PapiExecutor
+ method 'add'.
+ - even if the parameter contains multiple paths, there is only one
+ reply item (for each .add).
+ """
+
+ def __init__(self, node):
+ """Initialization.
+
+ :param node: Node to run command(s) on.
+ :type node: dict
+ """
+ # Node to run command(s) on.
+ self._node = node