openbmctool_execute_command_json(), get_remote_logging_view()

Several enhancements to lib/openbmctool_utils.py

- openbmctool_execute_command_json()
  A wrapper for openbmctool_execute_command that will process
  returned JSON data.
- get_health_check():
  - Change to use new svalid_dict() function.
- remote_logging_view_fields():
  A new function to return a list of all fields expected by the
  'logging remote_logging' command.
- get_remote_logging_view():
  A new function to get the remote_logging view information and return
  as a dictionary.

Change-Id: I3df4bad3712b5c2392b9bc08dd1ac9cc719938e2
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/openbmctool_utils.py b/lib/openbmctool_utils.py
index 4d5f54b..96e7b5f 100755
--- a/lib/openbmctool_utils.py
+++ b/lib/openbmctool_utils.py
@@ -10,6 +10,7 @@
 import gen_valid as gv
 import gen_misc as gm
 import var_funcs as vf
+import utils as utils
 from robot.libraries.BuiltIn import BuiltIn
 import re
 import tempfile
@@ -89,6 +90,35 @@
     return gc.shell_cmd(command_string, *args, **kwargs)
 
 
+def openbmctool_execute_command_json(command_string,
+                                     *args,
+                                     **kwargs):
+    r"""
+    Run the command string as an argument to the openbmctool.py program, parse
+    the JSON output into a dictionary and return the dictionary.
+
+    This function is a wrapper for openbmctool_execute_command (defined
+    above).  The caller may provide any command string where the output will
+    be JSON data.  This function will convert the JSON data to a python
+    object, verify that the 'status' field = "ok" and return the 'data'
+    sub-field to the caller.
+
+    See openbmctool_execute_command (above) for all field descriptions.
+    """
+
+    rc, output = openbmctool_execute_command(command_string,
+                                             *args,
+                                             **kwargs)
+    json_object = utils.to_json_ordered(output)
+    if json_object['status'] != "ok":
+        err_msg = "Error found in JSON data returned by the openbmctool.py "
+        err_msg += "command. Expected a 'status' field value of \"ok\":\n"
+        err_msg += gp.sprint_var(json_object, 1)
+        BuiltIn().fail(gp.sprint_error(err_msg))
+
+    return json_object['data']
+
+
 def get_fru_status():
     r"""
     Get the fru status and return as a list of dictionaries.
@@ -472,16 +502,58 @@
                                              ignore_err=False)
     health_check = vf.key_value_outbuf_to_dict(output, delim=":")
     if int(verify):
-        # Create a list of files by stripping the dir names from the elements
-        # of collect_service_data_file_paths.
-        fields_obtained = health_check.keys()
-        fields_expected = health_check_fields()
-        fields_missing = list(set(fields_expected) - set(fields_obtained))
-        if len(fields_missing) > 0:
-            err_msg = "The following fields are missing from the output of"
-            err_msg += " health_check:\n"
-            err_msg += gp.sprint_var(fields_missing)
-            err_msg += gp.sprint_var(health_check)
+        err_msg = gv.svalid_dict(health_check, health_check_fields())
+        if err_msg != "":
             BuiltIn().fail(gp.sprint_error(err_msg))
 
     return health_check
+
+
+def remote_logging_view_fields():
+    r"""
+    Return a complete list of field names returned by the logging
+    remote_logging view command.
+    """
+
+    return\
+        [
+            "Address",
+            "AddressFamily",
+            "Port"
+        ]
+
+
+def get_remote_logging_view(verify=False):
+    r"""
+    Get the remote_logging view information and return as a dictionary.
+
+    Example robot code:
+
+    ${remote_logging_view}=  Get Remote Logging View
+    Rpvars  1  remote_logging_view
+
+    Example result:
+
+    remote_logging_view:
+      [Address]:                 <blank>
+      [AddressFamily]:           xyz.openbmc_project.Network.Client.IPProtocol.IPv4
+      [Port]:                    0
+
+    Description of argument(s):
+    verify                          If set, verify that all all expected field
+                                    names are generated by the 'logging
+                                    remote_logging view' command.
+    """
+
+    remote_logging_view =\
+        openbmctool_execute_command_json("logging remote_logging view",
+                                         print_output=False,
+                                         ignore_err=False)
+
+    if int(verify):
+        err_msg = gv.svalid_dict(remote_logging_view,
+                                 remote_logging_view_fields())
+        if err_msg != "":
+            BuiltIn().fail(gp.sprint_error(err_msg))
+
+    return remote_logging_view