George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Rahul Maheshwari | 4d48857 | 2019-12-10 23:53:05 -0600 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | PLDM functions. |
| 5 | """ |
| 6 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import re |
| 8 | import var_funcs as vf |
| 9 | import func_args as fa |
| 10 | import bmc_ssh_utils as bsu |
Sridevi Ramesh | 961050b | 2020-11-12 11:04:30 -0600 | [diff] [blame] | 11 | import json |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 12 | import random |
| 13 | import string |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 14 | from robot.api import logger |
Rahul Maheshwari | 4d48857 | 2019-12-10 23:53:05 -0600 | [diff] [blame] | 15 | |
| 16 | |
Sridevi Ramesh | 961050b | 2020-11-12 11:04:30 -0600 | [diff] [blame] | 17 | def pldmtool(option_string, **bsu_options): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 18 | |
Rahul Maheshwari | 4d48857 | 2019-12-10 23:53:05 -0600 | [diff] [blame] | 19 | r""" |
| 20 | Run pldmtool on the BMC with the caller's option string and return the result. |
| 21 | |
| 22 | Example: |
| 23 | |
| 24 | ${pldm_results}= Pldmtool base GetPLDMTypes |
| 25 | Rprint Vars pldm_results |
| 26 | |
| 27 | pldm_results: |
Sridevi Ramesh | 961050b | 2020-11-12 11:04:30 -0600 | [diff] [blame] | 28 | pldmtool base GetPLDMVersion -t 0 |
| 29 | { |
| 30 | "Response": "1.0.0" |
| 31 | } |
| 32 | |
Rahul Maheshwari | 4d48857 | 2019-12-10 23:53:05 -0600 | [diff] [blame] | 33 | Description of argument(s): |
| 34 | option_string A string of options which are to be processed by the pldmtool command. |
| 35 | parse_results Parse the pldmtool results and return a dictionary rather than the raw |
| 36 | pldmtool output. |
| 37 | bsu_options Options to be passed directly to bmc_execute_command. See its prolog for |
| 38 | details. |
| 39 | """ |
| 40 | |
| 41 | # This allows callers to specify arguments in python style (e.g. print_out=1 vs. print_out=${1}). |
| 42 | bsu_options = fa.args_to_objects(bsu_options) |
| 43 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 44 | stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 45 | if stderr: |
| 46 | return stderr |
| 47 | try: |
| 48 | return json.loads(stdout) |
| 49 | except ValueError: |
| 50 | return stdout |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 51 | |
| 52 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 53 | def GetBIOSEnumAttributeOptionalValues(attr_val_table_data): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 54 | |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 55 | """ |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 56 | From pldmtool GetBIOSTable of type AttributeValueTable get the dict of |
| 57 | attribute handle and its optional values for BIOS Enumeration type. |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 58 | |
| 59 | Description of argument(s): |
| 60 | attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 61 | e.g. |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 62 | [{ |
| 63 | "AttributeHandle": 20, |
| 64 | "AttributeNameHandle": "23(pvm-pcie-error-inject)", |
| 65 | "AttributeType": "BIOSEnumeration", |
| 66 | "NumberOfPossibleValues": 2, |
| 67 | "PossibleValueStringHandle[0]": "3(Disabled)", |
| 68 | "PossibleValueStringHandle[1]": "4(Enabled)", |
| 69 | "NumberOfDefaultValues": 1, |
| 70 | "DefaultValueStringHandleIndex[0]": 1, |
| 71 | "StringHandle": "4(Enabled)" |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 72 | }] |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 73 | @return Dictionary of BIOS attribute and its value. |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 74 | e.g. {'pvm_pcie_error_inject': ['Disabled', 'Enabled']} |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 75 | """ |
| 76 | |
| 77 | attr_val_data_dict = {} |
| 78 | for item in attr_val_table_data: |
| 79 | for attr in item: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 80 | if (attr == "NumberOfPossibleValues"): |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 81 | value_list = [] |
| 82 | for i in range(0, int(item[attr])): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 83 | attr_values = item["PossibleValueStringHandle[" + str(i) + "]"] |
| 84 | value = re.search(r'\((.*?)\)', attr_values).group(1) |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 85 | if value: |
| 86 | # Example: |
| 87 | # value = '"Power Off"' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 88 | if ' ' in value: |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 89 | value = '"' + value + '"' |
| 90 | value_list.append(value) |
| 91 | else: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 92 | value_list.append('') |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 93 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 94 | attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"]) |
Sridevi Ramesh | 5753745 | 2021-01-18 03:25:05 -0600 | [diff] [blame] | 95 | attr_val_data_dict[attr_handle[0][1:-1]] = value_list |
| 96 | return attr_val_data_dict |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def GetBIOSStrAndIntAttributeHandles(attr_type, attr_val_table_data): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 100 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 101 | """ |
| 102 | From pldmtool GetBIOSTable of type AttributeValueTable get the dict of |
| 103 | attribute handle and its values based on the attribute type. |
| 104 | |
| 105 | Description of argument(s): |
| 106 | attr_type "BIOSInteger" or "BIOSString". |
| 107 | attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable. |
| 108 | |
| 109 | @return Dict of BIOS attribute and its value based on attribute type. |
| 110 | |
| 111 | """ |
| 112 | attr_val_int_dict = {} |
| 113 | attr_val_str_dict = {} |
| 114 | for item in attr_val_table_data: |
| 115 | value_dict = {} |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 116 | attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"]) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 117 | # Example: |
| 118 | # {'vmi_if0_ipv4_prefix_length': {'UpperBound': 32, 'LowerBound': 0} |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 119 | if (item["AttributeType"] == "BIOSInteger"): |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 120 | value_dict["LowerBound"] = item["LowerBound"] |
| 121 | value_dict["UpperBound"] = item["UpperBound"] |
| 122 | attr_val_int_dict[attr_handle[0][1:-1]] = value_dict |
| 123 | # Example: |
| 124 | # {'vmi_if1_ipv4_ipaddr': {'MaximumStringLength': 15, 'MinimumStringLength': 7}} |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 125 | elif (item["AttributeType"] == "BIOSString"): |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 126 | value_dict["MinimumStringLength"] = item["MinimumStringLength"] |
| 127 | value_dict["MaximumStringLength"] = item["MaximumStringLength"] |
| 128 | attr_val_str_dict[attr_handle[0][1:-1]] = value_dict |
| 129 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 130 | if (attr_type == "BIOSInteger"): |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 131 | return attr_val_int_dict |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 132 | elif (attr_type == "BIOSString"): |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 133 | return attr_val_str_dict |
| 134 | |
| 135 | |
| 136 | def GetRandomBIOSIntAndStrValues(attr_name, count): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 137 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 138 | """ |
| 139 | Get random integer or string values for BIOS attribute values based on the count. |
| 140 | |
| 141 | Description of argument(s): |
| 142 | attr_name Attribute name of BIOS attribute type Integer or string. |
| 143 | count Max length for BIOS attribute type Integer or string. |
| 144 | |
| 145 | @return Random attribute value based on BIOS attribute type Integer |
| 146 | or string. |
| 147 | |
| 148 | """ |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 149 | attr_random_value = '' |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 150 | |
| 151 | # Example |
| 152 | # 12.13.14.15 |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 153 | if 'gateway' in attr_name: |
| 154 | attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4)))) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 155 | # Example |
| 156 | # 11.11.11.11 |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 157 | elif 'ipaddr' in attr_name: |
| 158 | attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4)))) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 159 | # Example |
| 160 | # E5YWEDWJJ |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 161 | elif 'name' in attr_name: |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 162 | data = string.ascii_uppercase + string.digits |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 163 | attr_random_value = ''.join(random.choice(data) for _ in range(int(count))) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 164 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 165 | elif 'mfg_flags' in attr_name: |
Sridevi Ramesh | 74291d4 | 2021-04-28 07:52:35 -0500 | [diff] [blame] | 166 | data = string.ascii_uppercase + string.digits |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 167 | attr_random_value = ''.join(random.choice(data) for _ in range(int(count))) |
Sridevi Ramesh | 74291d4 | 2021-04-28 07:52:35 -0500 | [diff] [blame] | 168 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 169 | elif 'hb_lid_ids' in attr_name: |
Sridevi Ramesh | 48615cd | 2021-06-15 07:21:22 -0500 | [diff] [blame] | 170 | attr_random_value = str(random.randint(0, int(count))) |
| 171 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 172 | else: |
| 173 | attr_random_value = random.randint(0, int(count)) |
| 174 | return attr_random_value |
| 175 | |
| 176 | |
| 177 | def GetBIOSAttrOriginalValues(attr_val_table_data): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 178 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 179 | """ |
| 180 | From pldmtool GetBIOSTable of type AttributeValueTable get the dict of |
| 181 | attribute handle and its values. |
| 182 | |
| 183 | Description of argument(s): |
| 184 | attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable. |
| 185 | |
| 186 | @return Dict of BIOS attribute and its value. |
| 187 | |
| 188 | """ |
| 189 | attr_val_data_dict = {} |
| 190 | for item in attr_val_table_data: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 191 | attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"]) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 192 | attr_name = attr_handle[0][1:-1] |
| 193 | |
| 194 | command = "bios GetBIOSAttributeCurrentValueByHandle -a " + attr_name |
| 195 | value = pldmtool(command) |
| 196 | attr_val_data_dict[attr_name] = value["CurrentValue"] |
| 197 | if not value["CurrentValue"]: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 198 | if 'name' in attr_name: |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 199 | attr_val_data_dict[attr_name] = '""' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 200 | elif 'hb_lid_ids' in attr_name: |
Sridevi Ramesh | 48615cd | 2021-06-15 07:21:22 -0500 | [diff] [blame] | 201 | attr_val_data_dict[attr_name] = '""' |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 202 | |
| 203 | return attr_val_data_dict |
| 204 | |
| 205 | |
| 206 | def GetBIOSAttrDefaultValues(attr_val_table_data): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 207 | |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 208 | """ |
| 209 | From pldmtool GetBIOSTable of type AttributeValueTable get the dict of |
| 210 | attribute handle and its default attribute values. |
| 211 | |
| 212 | Description of argument(s): |
| 213 | attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable. |
| 214 | |
| 215 | @return Dict of BIOS attribute and its default attribute value. |
| 216 | |
| 217 | """ |
| 218 | attr_val_data_dict = {} |
| 219 | for item in attr_val_table_data: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 220 | attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"]) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 221 | attr_name = attr_handle[0][1:-1] |
| 222 | |
| 223 | if "DefaultString" in item: |
| 224 | attr_val_data_dict[attr_name] = item["DefaultString"] |
| 225 | if not item["DefaultString"]: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 226 | if 'name' in attr_name: |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 227 | attr_val_data_dict[attr_name] = '""' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 228 | elif 'hb_lid_ids' in attr_name: |
Sridevi Ramesh | 48615cd | 2021-06-15 07:21:22 -0500 | [diff] [blame] | 229 | attr_val_data_dict[attr_name] = '""' |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 230 | elif "DefaultValue" in item: |
| 231 | attr_val_data_dict[attr_name] = item["DefaultValue"] |
| 232 | elif "StringHandle" in item: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 233 | attr_default_value = re.findall(r'\(.*?\)', item["StringHandle"]) |
Sridevi Ramesh | 2ab3d38 | 2021-03-29 04:16:01 -0500 | [diff] [blame] | 234 | attr_val_data_dict[attr_name] = attr_default_value[0][1:-1] |
| 235 | |
| 236 | return attr_val_data_dict |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def GetNewValuesForAllBIOSAttrs(attr_table_data): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 240 | |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 241 | """ |
| 242 | Get a new set of values for all attributes in Attribute Table. |
| 243 | |
| 244 | Description of argument(s): |
| 245 | attr_table_data pldmtool output from GetBIOSTable table type AttributeValueTable. |
| 246 | |
| 247 | @return Dict of BIOS attribute and new attribute value. |
| 248 | |
| 249 | """ |
| 250 | existing_data = GetBIOSAttrOriginalValues(attr_table_data) |
| 251 | logger.info(existing_data) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 252 | string_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSString", attr_table_data) |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 253 | logger.info(string_attr_data) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 254 | int_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSInteger", attr_table_data) |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 255 | logger.info(int_attr_data) |
| 256 | enum_attr_data = GetBIOSEnumAttributeOptionalValues(attr_table_data) |
| 257 | logger.info(enum_attr_data) |
| 258 | |
| 259 | attr_random_data = {} |
| 260 | temp_list = enum_attr_data.copy() |
| 261 | for attr in enum_attr_data: |
| 262 | try: |
| 263 | temp_list[attr].remove(existing_data[attr]) |
| 264 | except ValueError: |
| 265 | try: |
| 266 | # The data values have a double quote in them. |
| 267 | # Eg: '"IBM I"' instead of just 'IBM I' |
| 268 | data = '"' + str(existing_data[attr]) + '"' |
| 269 | temp_list[attr].remove(data) |
| 270 | except ValueError: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 271 | logger.info("Unable to remove the existing value " |
| 272 | + str(data) + " from list " + str(temp_list[attr])) |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 273 | valid_values = temp_list[attr][:] |
| 274 | value = random.choice(valid_values) |
| 275 | attr_random_data[attr] = value.strip('"') |
| 276 | logger.info("Values generated for enumeration type attributes") |
| 277 | |
| 278 | for attr in string_attr_data: |
| 279 | # Iterating to make sure we have a different value |
| 280 | # other than the existing value. |
| 281 | for iter in range(5): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 282 | random_val = GetRandomBIOSIntAndStrValues(attr, string_attr_data[attr]["MaximumStringLength"]) |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 283 | if random_val != existing_data[attr]: |
| 284 | break |
| 285 | attr_random_data[attr] = random_val.strip('"') |
| 286 | logger.info("Values generated for string type attributes") |
| 287 | |
| 288 | for attr in int_attr_data: |
| 289 | for iter in range(5): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 290 | random_val = GetRandomBIOSIntAndStrValues(attr, int_attr_data[attr]["UpperBound"]) |
Anusha Dathatri | ba3cb8c | 2021-05-25 21:22:02 -0500 | [diff] [blame] | 291 | if random_val != existing_data[attr]: |
| 292 | break |
| 293 | attr_random_data[attr] = random_val |
| 294 | logger.info("Values generated for integer type attributes") |
| 295 | |
| 296 | return attr_random_data |