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