+ self._ssh.disconnect(self._node)
+
+ def add(self, csit_papi_command="vpp-stats", history=True, **kwargs):
+ """Add next command to internal command list; return self.
+
+ The argument name 'csit_papi_command' must be unique enough as it cannot
+ be repeated in kwargs.
+
+ :param csit_papi_command: VPP API command.
+ :param history: Enable/disable adding command to PAPI command history.
+ :param kwargs: Optional key-value arguments.
+ :type csit_papi_command: str
+ :type history: bool
+ :type kwargs: dict
+ :returns: self, so that method chaining is possible.
+ :rtype: PapiExecutor
+ """
+ if history:
+ PapiHistory.add_to_papi_history(
+ self._node, csit_papi_command, **kwargs)
+ self._api_command_list.append(dict(api_name=csit_papi_command,
+ api_args=kwargs))
+ return self
+
+ def get_stats(self, err_msg="Failed to get statistics.", timeout=120):
+ """Get VPP Stats from VPP Python API.
+
+ :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 of dict
+ """
+
+ paths = [cmd['api_args']['path'] for cmd in self._api_command_list]
+ self._api_command_list = list()
+
+ stdout = self._execute_papi(
+ paths, method='stats', err_msg=err_msg, timeout=timeout)
+
+ return json.loads(stdout)
+
+ 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: 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)
+
+ def get_reply(self, err_msg="Failed to get reply.", timeout=120):
+ """Get reply from VPP Python API.
+
+ The reply is parsed into dict-like object,
+ "retval" field is guaranteed to be zero on success.
+
+ 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_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 timeout: Timeout in seconds.
+ :type err_msg: str
+ :type timeout: int
+ :returns: Response, sw_if_index value of the reply.
+ :rtype: int
+ :raises AssertionError: If retval is nonzero, parsing or ssh error.
+ """
+ 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.
+
+ 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 timeout: Timeout in seconds.
+ :type err_msg: str
+ :type timeout: int
+ :returns: Details, dict objects with fields due to API without "retval".
+ :rtype: list of dict
+ """
+ return self._execute(method='dump', err_msg=err_msg, timeout=timeout)
+
+ @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 PapiExecutor(node) as papi_exec:
+ for cmd in cmds:
+ 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 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.
+ :param log: If True, the response is logged.
+ :type node: dict
+ :type cmd: str
+ :type log: bool
+ :returns: CLI output.
+ :rtype: str
+ """
+
+ 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:
+ reply = papi_exec.add(cli, **args).get_reply(err_msg)["reply"]
+
+ if log:
+ logger.info("{cmd}:\n{reply}".format(cmd=cmd, reply=reply))
+
+ return reply