blob: 8e0f2cc6c9d620bf8915703b2146213884d7cb37 [file] [log] [blame]
Rahul Maheshwari4d488572019-12-10 23:53:05 -06001#!/usr/bin/env python
2
3r"""
4PLDM functions.
5"""
6
7import re
8import var_funcs as vf
9import func_args as fa
10import bmc_ssh_utils as bsu
Sridevi Ramesh961050b2020-11-12 11:04:30 -060011import json
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050012import random
13import string
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -050014from robot.api import logger
Rahul Maheshwari4d488572019-12-10 23:53:05 -060015
16
Sridevi Ramesh961050b2020-11-12 11:04:30 -060017def pldmtool(option_string, **bsu_options):
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050018
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
44 stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050045 if stderr:
46 return stderr
47 try:
48 return json.loads(stdout)
49 except ValueError:
50 return stdout
Sridevi Ramesh57537452021-01-18 03:25:05 -060051
52
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050053def GetBIOSEnumAttributeOptionalValues(attr_val_table_data):
54
Sridevi Ramesh57537452021-01-18 03:25:05 -060055 """
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050056 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
57 attribute handle and its optional values for BIOS Enumeration type.
Sridevi Ramesh57537452021-01-18 03:25:05 -060058
59 Description of argument(s):
60 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050061 e.g.
Sridevi Ramesh57537452021-01-18 03:25:05 -060062 [{
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 Ramesh57537452021-01-18 03:25:05 -060072 }]
Sridevi Ramesh57537452021-01-18 03:25:05 -060073 @return Dictionary of BIOS attribute and its value.
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050074 e.g. {'pvm_pcie_error_inject': ['Disabled', 'Enabled']}
Sridevi Ramesh57537452021-01-18 03:25:05 -060075 """
76
77 attr_val_data_dict = {}
78 for item in attr_val_table_data:
79 for attr in item:
80 if (attr == "NumberOfPossibleValues"):
81 value_list = []
82 for i in range(0, int(item[attr])):
83 attr_values = item["PossibleValueStringHandle[" + str(i) + "]"]
84 value = re.search(r'\((.*?)\)', attr_values).group(1)
85 if value:
86 # Example:
87 # value = '"Power Off"'
88 if ' ' in value:
89 value = '"' + value + '"'
90 value_list.append(value)
91 else:
92 value_list.append('')
93
94 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
95 attr_val_data_dict[attr_handle[0][1:-1]] = value_list
96 return attr_val_data_dict
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -050097
98
99def GetBIOSStrAndIntAttributeHandles(attr_type, attr_val_table_data):
100
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 = {}
116 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
117 # Example:
118 # {'vmi_if0_ipv4_prefix_length': {'UpperBound': 32, 'LowerBound': 0}
119 if (item["AttributeType"] == "BIOSInteger"):
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}}
125 elif (item["AttributeType"] == "BIOSString"):
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
130 if (attr_type == "BIOSInteger"):
131 return attr_val_int_dict
132 elif (attr_type == "BIOSString"):
133 return attr_val_str_dict
134
135
136def GetRandomBIOSIntAndStrValues(attr_name, count):
137
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 """
149 attr_random_value = ''
150
151 # Example
152 # 12.13.14.15
153 if 'gateway' in attr_name:
154 attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
155 # Example
156 # 11.11.11.11
157 elif 'ipaddr' in attr_name:
158 attr_random_value = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
159 # Example
160 # E5YWEDWJJ
161 elif 'name' in attr_name:
162 data = string.ascii_uppercase + string.digits
163 attr_random_value = ''.join(random.choice(data) for _ in range(int(count)))
164
Sridevi Ramesh74291d42021-04-28 07:52:35 -0500165 elif 'mfg_flags' in attr_name:
166 data = string.ascii_uppercase + string.digits
167 attr_random_value = ''.join(random.choice(data) for _ in range(int(count)))
168
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500169 elif 'hb_lid_ids' in attr_name:
170 attr_random_value = str(random.randint(0, int(count)))
171
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500172 else:
173 attr_random_value = random.randint(0, int(count))
174 return attr_random_value
175
176
177def GetBIOSAttrOriginalValues(attr_val_table_data):
178
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:
191 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
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"]:
198 if 'name' in attr_name:
199 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500200 elif 'hb_lid_ids' in attr_name:
201 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500202
203 return attr_val_data_dict
204
205
206def GetBIOSAttrDefaultValues(attr_val_table_data):
207
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:
220 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
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"]:
226 if 'name' in attr_name:
227 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh48615cd2021-06-15 07:21:22 -0500228 elif 'hb_lid_ids' in attr_name:
229 attr_val_data_dict[attr_name] = '""'
Sridevi Ramesh2ab3d382021-03-29 04:16:01 -0500230 elif "DefaultValue" in item:
231 attr_val_data_dict[attr_name] = item["DefaultValue"]
232 elif "StringHandle" in item:
233 attr_default_value = re.findall(r'\(.*?\)', item["StringHandle"])
234 attr_val_data_dict[attr_name] = attr_default_value[0][1:-1]
235
236 return attr_val_data_dict
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500237
238
239def GetNewValuesForAllBIOSAttrs(attr_table_data):
240
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)
252 string_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSString", attr_table_data)
253 logger.info(string_attr_data)
254 int_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSInteger", attr_table_data)
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:
271 logger.info("Unable to remove the existing value "
272 + str(data) + " from list " + str(temp_list[attr]))
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):
282 random_val = GetRandomBIOSIntAndStrValues(attr, string_attr_data[attr]["MaximumStringLength"])
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):
290 random_val = GetRandomBIOSIntAndStrValues(attr, int_attr_data[attr]["UpperBound"])
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