Automate 9 TC to verify BIOS Attribute Types using pldmtool

      - Verify Set BIOS Integer Attribute Type
      - Verify Set BIOS String Attribute Type
      - Verify Restore BIOS Attribute Values
      - Verify Get BIOS Attribute With Invalid Attribute Name
      - Verify Set BIOS Attribute With Invalid Attribute Name
      - Verify Set Invalid Optional Value For BIOS Enumeration Attribute Type
      - Verify Set Out Of Range Integer Value For BIOS Integer Attribute Type
      - Verify Set Out Of Range String Value For BIOS String Attribute Type

Change-Id: I2db9fea8acd097a33a52f6cd5d03ed8427c12a4f
Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
diff --git a/lib/pldm_utils.py b/lib/pldm_utils.py
old mode 100644
new mode 100755
index e22af25..661e8ab
--- a/lib/pldm_utils.py
+++ b/lib/pldm_utils.py
@@ -9,9 +9,12 @@
 import func_args as fa
 import bmc_ssh_utils as bsu
 import json
+import random
+import string
 
 
 def pldmtool(option_string, **bsu_options):
+
     r"""
     Run pldmtool on the BMC with the caller's option string and return the result.
 
@@ -26,11 +29,6 @@
           "Response": "1.0.0"
       }
 
-     pldmtool base GetTID
-     {
-         "Response": 1
-     }
-
     Description of argument(s):
     option_string                   A string of options which are to be processed by the pldmtool command.
     parse_results                   Parse the pldmtool results and return a dictionary rather than the raw
@@ -43,17 +41,23 @@
     bsu_options = fa.args_to_objects(bsu_options)
 
     stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
-    return json.loads(stdout)
+    if stderr:
+        return stderr
+    try:
+        return json.loads(stdout)
+    except ValueError:
+        return stdout
 
 
-def GenerateBIOSAttrHandleValueDict(attr_val_table_data):
+def GetBIOSEnumAttributeOptionalValues(attr_val_table_data):
+
     """
-    From pldmtool bios GetBIOSTable of AttributeValueTable generate dictionary of
-    for BIOS attribute and its value.
+    From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
+    attribute handle and its optional values for BIOS Enumeration type.
 
     Description of argument(s):
     attr_val_table_data     pldmtool output from GetBIOSTable table type AttributeValueTable
-                             e.g.
+                            e.g.
                             [{
                                   "AttributeHandle": 20,
                                   "AttributeNameHandle": "23(pvm-pcie-error-inject)",
@@ -64,24 +68,9 @@
                                   "NumberOfDefaultValues": 1,
                                   "DefaultValueStringHandleIndex[0]": 1,
                                   "StringHandle": "4(Enabled)"
-                             },
-                             {
-                                  "AttributeHandle": 26,
-                                  "AttributeNameHandle": "28(pvm_fw_boot_side)",
-                                  "AttributeType": "BIOSEnumeration",
-                                  "NumberOfPossibleValues": 2,
-                                  "PossibleValueStringHandle[0]": "11(Perm)",
-                                  "PossibleValueStringHandle[1]": "15(Temp)",
-                                  "NumberOfDefaultValues": 1,
-                                  "DefaultValueStringHandleIndex[0]": 1,
-                                  "StringHandle": "15(Temp)"
                              }]
-
     @return                  Dictionary of BIOS attribute and its value.
-                             e.g. {
-                                   'pvm_pcie_error_inject': ['Disabled', 'Enabled'],
-                                   'pvm-fw-boot-side': ['Perm', 'Temp']
-                                  }
+                             e.g. {'pvm_pcie_error_inject': ['Disabled', 'Enabled']}
     """
 
     attr_val_data_dict = {}
@@ -104,3 +93,132 @@
                 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
                 attr_val_data_dict[attr_handle[0][1:-1]] = value_list
     return attr_val_data_dict
+
+
+def GetBIOSStrAndIntAttributeHandles(attr_type, attr_val_table_data):
+
+    """
+    From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
+    attribute handle and its values based on the attribute type.
+
+    Description of argument(s):
+    attr_type               "BIOSInteger" or "BIOSString".
+    attr_val_table_data     pldmtool output from GetBIOSTable table type AttributeValueTable.
+
+    @return                 Dict of BIOS attribute and its value based on attribute type.
+
+    """
+    attr_val_int_dict = {}
+    attr_val_str_dict = {}
+    for item in attr_val_table_data:
+        value_dict = {}
+        attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
+        # Example:
+        # {'vmi_if0_ipv4_prefix_length': {'UpperBound': 32, 'LowerBound': 0}
+        if (item["AttributeType"] == "BIOSInteger"):
+            value_dict["LowerBound"] = item["LowerBound"]
+            value_dict["UpperBound"] = item["UpperBound"]
+            attr_val_int_dict[attr_handle[0][1:-1]] = value_dict
+        # Example:
+        # {'vmi_if1_ipv4_ipaddr': {'MaximumStringLength': 15, 'MinimumStringLength': 7}}
+        elif (item["AttributeType"] == "BIOSString"):
+            value_dict["MinimumStringLength"] = item["MinimumStringLength"]
+            value_dict["MaximumStringLength"] = item["MaximumStringLength"]
+            attr_val_str_dict[attr_handle[0][1:-1]] = value_dict
+
+    if (attr_type == "BIOSInteger"):
+        return attr_val_int_dict
+    elif (attr_type == "BIOSString"):
+        return attr_val_str_dict
+
+
+def GetRandomBIOSIntAndStrValues(attr_name, count):
+
+    """
+    Get random integer or string values for BIOS attribute values based on the count.
+
+    Description of argument(s):
+    attr_name               Attribute name of BIOS attribute type Integer or string.
+    count                   Max length for BIOS attribute type Integer or string.
+
+    @return                 Random attribute value based on BIOS attribute type Integer
+                            or string.
+
+    """
+    attr_random_value = ''
+
+    # Example
+    # 12.13.14.15
+    if 'gateway' in attr_name:
+        attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
+    # Example
+    # 11.11.11.11
+    elif 'ipaddr' in attr_name:
+        attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
+    # Example
+    # E5YWEDWJJ
+    elif 'name' in attr_name:
+        data = string.ascii_uppercase + string.digits
+        attr_random_value = ''.join(random.choice(data) for _ in range(int(count)))
+
+    else:
+        attr_random_value = random.randint(0, int(count))
+    return attr_random_value
+
+
+def GetBIOSAttrOriginalValues(attr_val_table_data):
+
+    """
+    From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
+    attribute handle and its values.
+
+    Description of argument(s):
+    attr_val_table_data     pldmtool output from GetBIOSTable table type AttributeValueTable.
+
+    @return                 Dict of BIOS attribute and its value.
+
+    """
+    attr_val_data_dict = {}
+    for item in attr_val_table_data:
+        attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
+        attr_name = attr_handle[0][1:-1]
+
+        command = "bios GetBIOSAttributeCurrentValueByHandle -a " + attr_name
+        value = pldmtool(command)
+        attr_val_data_dict[attr_name] = value["CurrentValue"]
+        if not value["CurrentValue"]:
+            if 'name' in attr_name:
+                attr_val_data_dict[attr_name] = '""'
+
+    return attr_val_data_dict
+
+
+def GetBIOSAttrDefaultValues(attr_val_table_data):
+
+    """
+    From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
+    attribute handle and its default attribute values.
+
+    Description of argument(s):
+    attr_val_table_data     pldmtool output from GetBIOSTable table type AttributeValueTable.
+
+    @return                 Dict of BIOS attribute and its default attribute value.
+
+    """
+    attr_val_data_dict = {}
+    for item in attr_val_table_data:
+        attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
+        attr_name = attr_handle[0][1:-1]
+
+        if "DefaultString" in item:
+            attr_val_data_dict[attr_name] = item["DefaultString"]
+            if not item["DefaultString"]:
+                if 'name' in attr_name:
+                    attr_val_data_dict[attr_name] = '""'
+        elif "DefaultValue" in item:
+            attr_val_data_dict[attr_name] = item["DefaultValue"]
+        elif "StringHandle" in item:
+            attr_default_value = re.findall(r'\(.*?\)', item["StringHandle"])
+            attr_val_data_dict[attr_name] = attr_default_value[0][1:-1]
+
+    return attr_val_data_dict