import binascii
import json
+from pprint import pformat
+
from robot.api import logger
from resources.libraries.python.Constants import Constants
def __exit__(self, exc_type, exc_val, exc_tb):
self._ssh.disconnect(self._node)
- def add(self, csit_papi_command="vpp-stats", **kwargs):
+ 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
"""
- PapiHistory.add_to_papi_history(self._node, csit_papi_command, **kwargs)
+ 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
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.
+
+ :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
+ """
+
+ args = self._api_command_list[0]['api_args']
+ self._api_command_list = list()
+
+ stdout, _ = self._execute_papi(
+ args, method='stats_request', err_msg=err_msg, timeout=timeout)
+
+ return json.loads(stdout)
+
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.
method='dump', process_reply=process_reply,
ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout)
+ @staticmethod
+ def dump_and_log(node, cmds):
+ """Dump and log requested information.
+
+ :param node: DUT node.
+ :param cmds: Dump commands to be executed.
+ :type node: dict
+ :type cmds: list
+ """
+ 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"])))
+
+ @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
+
def execute_should_pass(self, err_msg="Failed to execute PAPI command.",
process_reply=True, ignore_errors=False,
timeout=120):
:rtype: dict or str or int
"""
if isinstance(val, dict):
- val_dict = dict()
for val_k, val_v in val.iteritems():
- val_dict[str(val_k)] = process_value(val_v)
- return val_dict
+ val[str(val_k)] = process_value(val_v)
+ return val
+ elif isinstance(val, list):
+ for idx, val_l in enumerate(val):
+ val[idx] = process_value(val_l)
+ return val
else:
return binascii.hexlify(val) if isinstance(val, str) else val
:returns: Processed API reply / a part of API reply.
:rtype: dict
"""
+ def process_value(val):
+ """Process value.
+
+ :param val: Value to be processed.
+ :type val: object
+ :returns: Processed value.
+ :rtype: dict or str or int
+ """
+ if isinstance(val, dict):
+ for val_k, val_v in val.iteritems():
+ val[str(val_k)] = process_value(val_v)
+ return val
+ elif isinstance(val, list):
+ for idx, val_l in enumerate(val):
+ val[idx] = process_value(val_l)
+ return val
+ elif isinstance(val, unicode):
+ return binascii.unhexlify(val)
+ else:
+ return val
+
reply_dict = dict()
reply_value = dict()
for reply_key, reply_v in api_r.iteritems():
for a_k, a_v in reply_v.iteritems():
- reply_value[a_k] = binascii.unhexlify(a_v) \
- if isinstance(a_v, unicode) else a_v
+ reply_value[a_k] = process_value(a_v)
reply_dict[reply_key] = reply_value
return reply_dict
if not api_data:
RuntimeError("No API data provided.")
- json_data = json.dumps(api_data) if method == "stats" \
+ 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}'".\
json=json_data)
try:
ret_code, stdout, stderr = self._ssh.exec_command_sudo(
- cmd=cmd, timeout=timeout)
+ cmd=cmd, timeout=timeout, log_stdout_err=False)
except SSHTimeout:
logger.error("PAPI command(s) execution timeout on host {host}:"
"\n{apis}".format(host=self._node["host"],