VAT-to-PAPI: VPPUtils
[csit.git] / resources / libraries / python / PapiExecutor.py
index 09cbf9a..c2f966f 100644 (file)
@@ -365,6 +365,34 @@ class PapiExecutor(object):
             method='dump', process_reply=process_reply,
             ignore_errors=ignore_errors, err_msg=err_msg, timeout=timeout)
 
+    @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):
@@ -407,12 +435,27 @@ class PapiExecutor(object):
         :rtype: list
         """
 
+        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):
+                val_dict = dict()
+                for val_k, val_v in val.iteritems():
+                    val_dict[str(val_k)] = process_value(val_v)
+                return val_dict
+            else:
+                return binascii.hexlify(val) if isinstance(val, str) else val
+
         api_data_processed = list()
         for api in api_d:
             api_args_processed = dict()
             for a_k, a_v in api["api_args"].iteritems():
-                value = binascii.hexlify(a_v) if isinstance(a_v, str) else a_v
-                api_args_processed[str(a_k)] = value
+                api_args_processed[str(a_k)] = process_value(a_v)
             api_data_processed.append(dict(api_name=api["api_name"],
                                            api_args=api_args_processed))
         return api_data_processed
@@ -486,7 +529,7 @@ class PapiExecutor(object):
                    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"],