blob: 1d70b36f26115ee449602f827a9a8734a79075f0 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Rahul Maheshwari4d488572019-12-10 23:53:05 -06002
3r"""
4PLDM functions.
5"""
6
Sridevi Ramesh961050b2020-11-12 11:04:30 -06007import json
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -05008import random
Patrick Williams20f38712022-12-08 06:18:26 -06009import re
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050010import string
Patrick Williams20f38712022-12-08 06:18:26 -060011
12import bmc_ssh_utils as bsu
13import func_args as fa
14import var_funcs as vf
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -050015from robot.api import logger
Rahul Maheshwari4d488572019-12-10 23:53:05 -060016
17
Sridevi Ramesh961050b2020-11-12 11:04:30 -060018def pldmtool(option_string, **bsu_options):
Rahul Maheshwari4d488572019-12-10 23:53:05 -060019 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 Ramesh961050b2020-11-12 11:04:30 -060028 pldmtool base GetPLDMVersion -t 0
29 {
30 "Response": "1.0.0"
31 }
32
Rahul Maheshwari4d488572019-12-10 23:53:05 -060033 Description of argument(s):
George Keishingb9080fc2023-02-17 12:31:31 -060034 option_string A string of options which are to be processed by the
35 pldmtool command.
36 parse_results Parse the pldmtool results and return a dictionary
37 rather than the raw
38 pldmtool output.
39 bsu_options Options to be passed directly to bmc_execute_command.
40 See its prolog for details.
Rahul Maheshwari4d488572019-12-10 23:53:05 -060041 """
42
George Keishingb9080fc2023-02-17 12:31:31 -060043 # This allows callers to specify arguments in python style
44 # (e.g. print_out=1 vs. print_out=${1}).
Rahul Maheshwari4d488572019-12-10 23:53:05 -060045 bsu_options = fa.args_to_objects(bsu_options)
46
Patrick Williams20f38712022-12-08 06:18:26 -060047 stdout, stderr, rc = bsu.bmc_execute_command(
Sridevi Ramesha4ab9f32023-12-13 02:58:53 -060048 "pldmtool " + option_string, **bsu_options, ignore_err=1
Patrick Williams20f38712022-12-08 06:18:26 -060049 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050050 if stderr:
51 return stderr
52 try:
53 return json.loads(stdout)
54 except ValueError:
55 return stdout
Sridevi Ramesh57537452021-01-18 03:25:05 -060056
57
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050058def GetBIOSEnumAttributeOptionalValues(attr_val_table_data):
Sridevi Ramesh57537452021-01-18 03:25:05 -060059 """
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050060 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
61 attribute handle and its optional values for BIOS Enumeration type.
Sridevi Ramesh57537452021-01-18 03:25:05 -060062
63 Description of argument(s):
64 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050065 e.g.
Sridevi Ramesh57537452021-01-18 03:25:05 -060066 [{
67 "AttributeHandle": 20,
68 "AttributeNameHandle": "23(pvm-pcie-error-inject)",
69 "AttributeType": "BIOSEnumeration",
70 "NumberOfPossibleValues": 2,
71 "PossibleValueStringHandle[0]": "3(Disabled)",
72 "PossibleValueStringHandle[1]": "4(Enabled)",
73 "NumberOfDefaultValues": 1,
74 "DefaultValueStringHandleIndex[0]": 1,
75 "StringHandle": "4(Enabled)"
Sridevi Ramesh57537452021-01-18 03:25:05 -060076 }]
Sridevi Ramesh57537452021-01-18 03:25:05 -060077 @return Dictionary of BIOS attribute and its value.
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050078 e.g. {'pvm_pcie_error_inject': ['Disabled', 'Enabled']}
Sridevi Ramesh57537452021-01-18 03:25:05 -060079 """
80
81 attr_val_data_dict = {}
82 for item in attr_val_table_data:
83 for attr in item:
Patrick Williams20f38712022-12-08 06:18:26 -060084 if attr == "NumberOfPossibleValues":
Sridevi Ramesh57537452021-01-18 03:25:05 -060085 value_list = []
86 for i in range(0, int(item[attr])):
Patrick Williams20f38712022-12-08 06:18:26 -060087 attr_values = item[
88 "PossibleValueStringHandle[" + str(i) + "]"
89 ]
90 value = re.search(r"\((.*?)\)", attr_values).group(1)
Sridevi Ramesh57537452021-01-18 03:25:05 -060091 if value:
92 # Example:
93 # value = '"Power Off"'
Patrick Williams20f38712022-12-08 06:18:26 -060094 if " " in value:
Sridevi Ramesh57537452021-01-18 03:25:05 -060095 value = '"' + value + '"'
96 value_list.append(value)
97 else:
Patrick Williams20f38712022-12-08 06:18:26 -060098 value_list.append("")
Sridevi Ramesh57537452021-01-18 03:25:05 -060099
Patrick Williams20f38712022-12-08 06:18:26 -0600100 attr_handle = re.findall(
101 r"\(.*?\)", item["AttributeNameHandle"]
102 )
Sridevi Ramesh57537452021-01-18 03:25:05 -0600103 attr_val_data_dict[attr_handle[0][1:-1]] = value_list
104 return attr_val_data_dict
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500105
106
107def GetBIOSStrAndIntAttributeHandles(attr_type, attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500108 """
109 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
110 attribute handle and its values based on the attribute type.
111
112 Description of argument(s):
113 attr_type "BIOSInteger" or "BIOSString".
114 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
115
116 @return Dict of BIOS attribute and its value based on attribute type.
117
118 """
119 attr_val_int_dict = {}
120 attr_val_str_dict = {}
121 for item in attr_val_table_data:
122 value_dict = {}
Patrick Williams20f38712022-12-08 06:18:26 -0600123 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500124 # Example:
125 # {'vmi_if0_ipv4_prefix_length': {'UpperBound': 32, 'LowerBound': 0}
Patrick Williams20f38712022-12-08 06:18:26 -0600126 if item["AttributeType"] == "BIOSInteger":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500127 value_dict["LowerBound"] = item["LowerBound"]
128 value_dict["UpperBound"] = item["UpperBound"]
129 attr_val_int_dict[attr_handle[0][1:-1]] = value_dict
130 # Example:
131 # {'vmi_if1_ipv4_ipaddr': {'MaximumStringLength': 15, 'MinimumStringLength': 7}}
Patrick Williams20f38712022-12-08 06:18:26 -0600132 elif item["AttributeType"] == "BIOSString":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500133 value_dict["MinimumStringLength"] = item["MinimumStringLength"]
134 value_dict["MaximumStringLength"] = item["MaximumStringLength"]
135 attr_val_str_dict[attr_handle[0][1:-1]] = value_dict
136
Patrick Williams20f38712022-12-08 06:18:26 -0600137 if attr_type == "BIOSInteger":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500138 return attr_val_int_dict
George Keishingb9080fc2023-02-17 12:31:31 -0600139 if attr_type == "BIOSString":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500140 return attr_val_str_dict
141
George Keishingb9080fc2023-02-17 12:31:31 -0600142 return None
143
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500144
145def GetRandomBIOSIntAndStrValues(attr_name, count):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500146 """
147 Get random integer or string values for BIOS attribute values based on the count.
148
149 Description of argument(s):
150 attr_name Attribute name of BIOS attribute type Integer or string.
151 count Max length for BIOS attribute type Integer or string.
152
153 @return Random attribute value based on BIOS attribute type Integer
154 or string.
155
156 """
Patrick Williams20f38712022-12-08 06:18:26 -0600157 attr_random_value = ""
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500158
159 # Example
160 # 12.13.14.15
Patrick Williams20f38712022-12-08 06:18:26 -0600161 if "gateway" in attr_name:
162 attr_random_value = ".".join(
163 map(str, (random.randint(0, 255) for _ in range(4)))
164 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500165 # Example
166 # 11.11.11.11
Patrick Williams20f38712022-12-08 06:18:26 -0600167 elif "ipaddr" in attr_name:
168 attr_random_value = ".".join(
169 map(str, (random.randint(0, 255) for _ in range(4)))
170 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500171 # Example
172 # E5YWEDWJJ
Patrick Williams20f38712022-12-08 06:18:26 -0600173 elif "name" in attr_name:
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500174 data = string.ascii_uppercase + string.digits
Patrick Williams20f38712022-12-08 06:18:26 -0600175 attr_random_value = "".join(
176 random.choice(data) for _ in range(int(count))
177 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500178
Patrick Williams20f38712022-12-08 06:18:26 -0600179 elif "mfg_flags" in attr_name:
Sridevi Ramesh74291d42021-04-28 07:52:35 -0500180 data = string.ascii_uppercase + string.digits
Patrick Williams20f38712022-12-08 06:18:26 -0600181 attr_random_value = "".join(
182 random.choice(data) for _ in range(int(count))
183 )
Sridevi Ramesh74291d42021-04-28 07:52:35 -0500184
Patrick Williams20f38712022-12-08 06:18:26 -0600185 elif "hb_lid_ids" in attr_name:
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500186 attr_random_value = str(random.randint(0, int(count)))
187
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500188 else:
189 attr_random_value = random.randint(0, int(count))
190 return attr_random_value
191
192
193def GetBIOSAttrOriginalValues(attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500194 """
195 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
196 attribute handle and its values.
197
198 Description of argument(s):
199 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
200
201 @return Dict of BIOS attribute and its value.
202
203 """
204 attr_val_data_dict = {}
205 for item in attr_val_table_data:
Patrick Williams20f38712022-12-08 06:18:26 -0600206 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500207 attr_name = attr_handle[0][1:-1]
208
Sridevi Ramesha4ab9f32023-12-13 02:58:53 -0600209 # Exclude BIOS attribute which are ReadOnly.
210 if "ReadOnly" not in item["AttributeType"]:
211 command = (
212 "bios GetBIOSAttributeCurrentValueByHandle -a " + attr_name
213 )
214 value = pldmtool(command)
215 if "error" in value:
216 print("Ignore BIOS attribute which throws error...")
217 pass
218 elif not value["CurrentValue"]:
219 if "name" in attr_name:
220 attr_val_data_dict[attr_name] = '""'
221 elif "hb_lid_ids" in attr_name:
222 attr_val_data_dict[attr_name] = '""'
223 else:
224 attr_val_data_dict[attr_name] = value["CurrentValue"]
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500225
226 return attr_val_data_dict
227
228
229def GetBIOSAttrDefaultValues(attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500230 """
231 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
232 attribute handle and its default attribute values.
233
234 Description of argument(s):
235 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
236
237 @return Dict of BIOS attribute and its default attribute value.
238
239 """
240 attr_val_data_dict = {}
241 for item in attr_val_table_data:
Patrick Williams20f38712022-12-08 06:18:26 -0600242 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500243 attr_name = attr_handle[0][1:-1]
244
245 if "DefaultString" in item:
246 attr_val_data_dict[attr_name] = item["DefaultString"]
247 if not item["DefaultString"]:
Patrick Williams20f38712022-12-08 06:18:26 -0600248 if "name" in attr_name:
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500249 attr_val_data_dict[attr_name] = '""'
Patrick Williams20f38712022-12-08 06:18:26 -0600250 elif "hb_lid_ids" in attr_name:
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500251 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500252 elif "DefaultValue" in item:
253 attr_val_data_dict[attr_name] = item["DefaultValue"]
254 elif "StringHandle" in item:
Patrick Williams20f38712022-12-08 06:18:26 -0600255 attr_default_value = re.findall(r"\(.*?\)", item["StringHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500256 attr_val_data_dict[attr_name] = attr_default_value[0][1:-1]
257
258 return attr_val_data_dict
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500259
260
261def GetNewValuesForAllBIOSAttrs(attr_table_data):
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500262 """
263 Get a new set of values for all attributes in Attribute Table.
264
265 Description of argument(s):
266 attr_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
267
268 @return Dict of BIOS attribute and new attribute value.
269
270 """
271 existing_data = GetBIOSAttrOriginalValues(attr_table_data)
272 logger.info(existing_data)
Patrick Williams20f38712022-12-08 06:18:26 -0600273 string_attr_data = GetBIOSStrAndIntAttributeHandles(
274 "BIOSString", attr_table_data
275 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500276 logger.info(string_attr_data)
Patrick Williams20f38712022-12-08 06:18:26 -0600277 int_attr_data = GetBIOSStrAndIntAttributeHandles(
278 "BIOSInteger", attr_table_data
279 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500280 logger.info(int_attr_data)
281 enum_attr_data = GetBIOSEnumAttributeOptionalValues(attr_table_data)
282 logger.info(enum_attr_data)
283
284 attr_random_data = {}
285 temp_list = enum_attr_data.copy()
286 for attr in enum_attr_data:
287 try:
288 temp_list[attr].remove(existing_data[attr])
289 except ValueError:
290 try:
291 # The data values have a double quote in them.
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500292 data = '"' + str(existing_data[attr]) + '"'
293 temp_list[attr].remove(data)
294 except ValueError:
Patrick Williams20f38712022-12-08 06:18:26 -0600295 logger.info(
296 "Unable to remove the existing value "
297 + str(data)
298 + " from list "
299 + str(temp_list[attr])
300 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500301 valid_values = temp_list[attr][:]
302 value = random.choice(valid_values)
303 attr_random_data[attr] = value.strip('"')
304 logger.info("Values generated for enumeration type attributes")
305
306 for attr in string_attr_data:
307 # Iterating to make sure we have a different value
308 # other than the existing value.
309 for iter in range(5):
Patrick Williams20f38712022-12-08 06:18:26 -0600310 random_val = GetRandomBIOSIntAndStrValues(
311 attr, string_attr_data[attr]["MaximumStringLength"]
312 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500313 if random_val != existing_data[attr]:
314 break
Sridevi Ramesha4ab9f32023-12-13 02:58:53 -0600315 if isinstance(random_val, str):
316 attr_random_data[attr] = random_val.strip('"')
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500317 logger.info("Values generated for string type attributes")
318
319 for attr in int_attr_data:
320 for iter in range(5):
Patrick Williams20f38712022-12-08 06:18:26 -0600321 random_val = GetRandomBIOSIntAndStrValues(
322 attr, int_attr_data[attr]["UpperBound"]
323 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500324 if random_val != existing_data[attr]:
325 break
326 attr_random_data[attr] = random_val
327 logger.info("Values generated for integer type attributes")
328
329 return attr_random_data