New network function in openbmctool_utils.py

Created new network() function in openbmctool_utils.py:

Run an openbmctool.py network command and return the results as a
dictionary.

Description of argument(s):
sub_command                     The sub-command accepted by the network
                                command (e.g. "view-config", "getIP",
                                etc.).
options                         Any options accepted by the network
                                command.

Also, improved openbmctool_execute_command_json handling of
json.JSONDecodeError.

Change-Id: I2e014ea45fc70d86aa2dd757f614665afff75ad0
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/openbmctool_utils.py b/lib/openbmctool_utils.py
index e845f3a..b49fa28 100755
--- a/lib/openbmctool_utils.py
+++ b/lib/openbmctool_utils.py
@@ -14,6 +14,8 @@
 from robot.libraries.BuiltIn import BuiltIn
 import re
 import tempfile
+import collections
+import json
 
 
 def openbmctool_execute_command(command_string,
@@ -110,7 +112,11 @@
     rc, output = openbmctool_execute_command(command_string,
                                              *args,
                                              **kwargs)
-    json_object = utils.to_json_ordered(output)
+    try:
+        json_object = utils.to_json_ordered(output)
+    except json.JSONDecodeError:
+        BuiltIn().fail(gp.sprint_error(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"
@@ -558,3 +564,46 @@
             BuiltIn().fail(gp.sprint_error(err_msg))
 
     return remote_logging_view
+
+
+def network(sub_command, **options):
+    r"""
+    Run an openbmctool.py network command and return the results as a dictionary.
+
+    Note that any valid network argument may be specified as a function argument.
+
+    Example robot code:
+
+    ${ip_records}=  Network  getIP  I=eth0
+    Rprint Vars  ip_records
+
+    Resulting output:
+
+    ip_records:
+      [/xyz/openbmc_project/network/eth0/ipv4/23d41d48]:
+        [Address]:                  n.n.n.n
+        [Gateway]:
+        [Origin]:                   xyz.openbmc_project.Network.IP.AddressOrigin.Static
+        [PrefixLength]:             24
+        [Type]:                     xyz.openbmc_project.Network.IP.Protocol.IPv4
+      [/xyz/openbmc_project/network/eth0/ipv4/24ba5feb]:
+        [Address]:                  n.n.n.n
+      (etc.)
+
+    Description of argument(s):
+    sub_command                     The sub-command accepted by the network
+                                    command (e.g. "view-config", "getIP",
+                                    etc.).
+    options                         Zero or more options accepted by the network command.
+    """
+
+    if gm.python_version < gm.ordered_dict_version:
+        new_options = collections.OrderedDict(options)
+    else:
+        new_options = options
+
+    command_string = gc.create_command_string('network ' + sub_command,
+                                              new_options)
+    return openbmctool_execute_command_json(command_string,
+                                            print_output=False,
+                                            ignore_err=False)