blob: 81e53fe2c8d2f9f7d6b32f6d0d0030729da06ccb [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):
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 Williams20f38712022-12-08 06:18:26 -060044 stdout, stderr, rc = bsu.bmc_execute_command(
45 "pldmtool " + option_string, **bsu_options
46 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050047 if stderr:
48 return stderr
49 try:
50 return json.loads(stdout)
51 except ValueError:
52 return stdout
Sridevi Ramesh57537452021-01-18 03:25:05 -060053
54
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050055def GetBIOSEnumAttributeOptionalValues(attr_val_table_data):
Sridevi Ramesh57537452021-01-18 03:25:05 -060056 """
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050057 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
58 attribute handle and its optional values for BIOS Enumeration type.
Sridevi Ramesh57537452021-01-18 03:25:05 -060059
60 Description of argument(s):
61 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050062 e.g.
Sridevi Ramesh57537452021-01-18 03:25:05 -060063 [{
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 Ramesh57537452021-01-18 03:25:05 -060073 }]
Sridevi Ramesh57537452021-01-18 03:25:05 -060074 @return Dictionary of BIOS attribute and its value.
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050075 e.g. {'pvm_pcie_error_inject': ['Disabled', 'Enabled']}
Sridevi Ramesh57537452021-01-18 03:25:05 -060076 """
77
78 attr_val_data_dict = {}
79 for item in attr_val_table_data:
80 for attr in item:
Patrick Williams20f38712022-12-08 06:18:26 -060081 if attr == "NumberOfPossibleValues":
Sridevi Ramesh57537452021-01-18 03:25:05 -060082 value_list = []
83 for i in range(0, int(item[attr])):
Patrick Williams20f38712022-12-08 06:18:26 -060084 attr_values = item[
85 "PossibleValueStringHandle[" + str(i) + "]"
86 ]
87 value = re.search(r"\((.*?)\)", attr_values).group(1)
Sridevi Ramesh57537452021-01-18 03:25:05 -060088 if value:
89 # Example:
90 # value = '"Power Off"'
Patrick Williams20f38712022-12-08 06:18:26 -060091 if " " in value:
Sridevi Ramesh57537452021-01-18 03:25:05 -060092 value = '"' + value + '"'
93 value_list.append(value)
94 else:
Patrick Williams20f38712022-12-08 06:18:26 -060095 value_list.append("")
Sridevi Ramesh57537452021-01-18 03:25:05 -060096
Patrick Williams20f38712022-12-08 06:18:26 -060097 attr_handle = re.findall(
98 r"\(.*?\)", item["AttributeNameHandle"]
99 )
Sridevi Ramesh57537452021-01-18 03:25:05 -0600100 attr_val_data_dict[attr_handle[0][1:-1]] = value_list
101 return attr_val_data_dict
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500102
103
104def GetBIOSStrAndIntAttributeHandles(attr_type, attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500105 """
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 Williams20f38712022-12-08 06:18:26 -0600120 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500121 # Example:
122 # {'vmi_if0_ipv4_prefix_length': {'UpperBound': 32, 'LowerBound': 0}
Patrick Williams20f38712022-12-08 06:18:26 -0600123 if item["AttributeType"] == "BIOSInteger":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500124 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 Williams20f38712022-12-08 06:18:26 -0600129 elif item["AttributeType"] == "BIOSString":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500130 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 Williams20f38712022-12-08 06:18:26 -0600134 if attr_type == "BIOSInteger":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500135 return attr_val_int_dict
Patrick Williams20f38712022-12-08 06:18:26 -0600136 elif attr_type == "BIOSString":
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500137 return attr_val_str_dict
138
139
140def GetRandomBIOSIntAndStrValues(attr_name, count):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500141 """
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 Williams20f38712022-12-08 06:18:26 -0600152 attr_random_value = ""
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500153
154 # Example
155 # 12.13.14.15
Patrick Williams20f38712022-12-08 06:18:26 -0600156 if "gateway" in attr_name:
157 attr_random_value = ".".join(
158 map(str, (random.randint(0, 255) for _ in range(4)))
159 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500160 # Example
161 # 11.11.11.11
Patrick Williams20f38712022-12-08 06:18:26 -0600162 elif "ipaddr" in attr_name:
163 attr_random_value = ".".join(
164 map(str, (random.randint(0, 255) for _ in range(4)))
165 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500166 # Example
167 # E5YWEDWJJ
Patrick Williams20f38712022-12-08 06:18:26 -0600168 elif "name" in attr_name:
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500169 data = string.ascii_uppercase + string.digits
Patrick Williams20f38712022-12-08 06:18:26 -0600170 attr_random_value = "".join(
171 random.choice(data) for _ in range(int(count))
172 )
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500173
Patrick Williams20f38712022-12-08 06:18:26 -0600174 elif "mfg_flags" in attr_name:
Sridevi Ramesh74291d42021-04-28 07:52:35 -0500175 data = string.ascii_uppercase + string.digits
Patrick Williams20f38712022-12-08 06:18:26 -0600176 attr_random_value = "".join(
177 random.choice(data) for _ in range(int(count))
178 )
Sridevi Ramesh74291d42021-04-28 07:52:35 -0500179
Patrick Williams20f38712022-12-08 06:18:26 -0600180 elif "hb_lid_ids" in attr_name:
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500181 attr_random_value = str(random.randint(0, int(count)))
182
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500183 else:
184 attr_random_value = random.randint(0, int(count))
185 return attr_random_value
186
187
188def GetBIOSAttrOriginalValues(attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500189 """
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 Williams20f38712022-12-08 06:18:26 -0600201 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500202 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 Williams20f38712022-12-08 06:18:26 -0600208 if "name" in attr_name:
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500209 attr_val_data_dict[attr_name] = '""'
Patrick Williams20f38712022-12-08 06:18:26 -0600210 elif "hb_lid_ids" in attr_name:
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500211 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500212
213 return attr_val_data_dict
214
215
216def GetBIOSAttrDefaultValues(attr_val_table_data):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500217 """
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 Williams20f38712022-12-08 06:18:26 -0600229 attr_handle = re.findall(r"\(.*?\)", item["AttributeNameHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500230 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 Williams20f38712022-12-08 06:18:26 -0600235 if "name" in attr_name:
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500236 attr_val_data_dict[attr_name] = '""'
Patrick Williams20f38712022-12-08 06:18:26 -0600237 elif "hb_lid_ids" in attr_name:
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500238 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500239 elif "DefaultValue" in item:
240 attr_val_data_dict[attr_name] = item["DefaultValue"]
241 elif "StringHandle" in item:
Patrick Williams20f38712022-12-08 06:18:26 -0600242 attr_default_value = re.findall(r"\(.*?\)", item["StringHandle"])
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500243 attr_val_data_dict[attr_name] = attr_default_value[0][1:-1]
244
245 return attr_val_data_dict
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500246
247
248def GetNewValuesForAllBIOSAttrs(attr_table_data):
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500249 """
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 Williams20f38712022-12-08 06:18:26 -0600260 string_attr_data = GetBIOSStrAndIntAttributeHandles(
261 "BIOSString", attr_table_data
262 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500263 logger.info(string_attr_data)
Patrick Williams20f38712022-12-08 06:18:26 -0600264 int_attr_data = GetBIOSStrAndIntAttributeHandles(
265 "BIOSInteger", attr_table_data
266 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500267 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 Williams20f38712022-12-08 06:18:26 -0600283 logger.info(
284 "Unable to remove the existing value "
285 + str(data)
286 + " from list "
287 + str(temp_list[attr])
288 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500289 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 Williams20f38712022-12-08 06:18:26 -0600298 random_val = GetRandomBIOSIntAndStrValues(
299 attr, string_attr_data[attr]["MaximumStringLength"]
300 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500301 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 Williams20f38712022-12-08 06:18:26 -0600308 random_val = GetRandomBIOSIntAndStrValues(
309 attr, int_attr_data[attr]["UpperBound"]
310 )
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500311 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