blob: 36707cdc7f486c08664e5db74514c899488390c8 [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 Ramesh2ab3d382021-03-29 04:16:01 -0500169 else:
170 attr_random_value = random.randint(0, int(count))
171 return attr_random_value
172
173
174def GetBIOSAttrOriginalValues(attr_val_table_data):
175
176 """
177 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
178 attribute handle and its values.
179
180 Description of argument(s):
181 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
182
183 @return Dict of BIOS attribute and its value.
184
185 """
186 attr_val_data_dict = {}
187 for item in attr_val_table_data:
188 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
189 attr_name = attr_handle[0][1:-1]
190
191 command = "bios GetBIOSAttributeCurrentValueByHandle -a " + attr_name
192 value = pldmtool(command)
193 attr_val_data_dict[attr_name] = value["CurrentValue"]
194 if not value["CurrentValue"]:
195 if 'name' in attr_name:
196 attr_val_data_dict[attr_name] = '""'
197
198 return attr_val_data_dict
199
200
201def GetBIOSAttrDefaultValues(attr_val_table_data):
202
203 """
204 From pldmtool GetBIOSTable of type AttributeValueTable get the dict of
205 attribute handle and its default attribute values.
206
207 Description of argument(s):
208 attr_val_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
209
210 @return Dict of BIOS attribute and its default attribute value.
211
212 """
213 attr_val_data_dict = {}
214 for item in attr_val_table_data:
215 attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
216 attr_name = attr_handle[0][1:-1]
217
218 if "DefaultString" in item:
219 attr_val_data_dict[attr_name] = item["DefaultString"]
220 if not item["DefaultString"]:
221 if 'name' in attr_name:
222 attr_val_data_dict[attr_name] = '""'
223 elif "DefaultValue" in item:
224 attr_val_data_dict[attr_name] = item["DefaultValue"]
225 elif "StringHandle" in item:
226 attr_default_value = re.findall(r'\(.*?\)', item["StringHandle"])
227 attr_val_data_dict[attr_name] = attr_default_value[0][1:-1]
228
229 return attr_val_data_dict
Anusha Dathatriba3cb8c2021-05-25 21:22:02 -0500230
231
232def GetNewValuesForAllBIOSAttrs(attr_table_data):
233
234 """
235 Get a new set of values for all attributes in Attribute Table.
236
237 Description of argument(s):
238 attr_table_data pldmtool output from GetBIOSTable table type AttributeValueTable.
239
240 @return Dict of BIOS attribute and new attribute value.
241
242 """
243 existing_data = GetBIOSAttrOriginalValues(attr_table_data)
244 logger.info(existing_data)
245 string_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSString", attr_table_data)
246 logger.info(string_attr_data)
247 int_attr_data = GetBIOSStrAndIntAttributeHandles("BIOSInteger", attr_table_data)
248 logger.info(int_attr_data)
249 enum_attr_data = GetBIOSEnumAttributeOptionalValues(attr_table_data)
250 logger.info(enum_attr_data)
251
252 attr_random_data = {}
253 temp_list = enum_attr_data.copy()
254 for attr in enum_attr_data:
255 try:
256 temp_list[attr].remove(existing_data[attr])
257 except ValueError:
258 try:
259 # The data values have a double quote in them.
260 # Eg: '"IBM I"' instead of just 'IBM I'
261 data = '"' + str(existing_data[attr]) + '"'
262 temp_list[attr].remove(data)
263 except ValueError:
264 logger.info("Unable to remove the existing value "
265 + str(data) + " from list " + str(temp_list[attr]))
266 valid_values = temp_list[attr][:]
267 value = random.choice(valid_values)
268 attr_random_data[attr] = value.strip('"')
269 logger.info("Values generated for enumeration type attributes")
270
271 for attr in string_attr_data:
272 # Iterating to make sure we have a different value
273 # other than the existing value.
274 for iter in range(5):
275 random_val = GetRandomBIOSIntAndStrValues(attr, string_attr_data[attr]["MaximumStringLength"])
276 if random_val != existing_data[attr]:
277 break
278 attr_random_data[attr] = random_val.strip('"')
279 logger.info("Values generated for string type attributes")
280
281 for attr in int_attr_data:
282 for iter in range(5):
283 random_val = GetRandomBIOSIntAndStrValues(attr, int_attr_data[attr]["UpperBound"])
284 if random_val != existing_data[attr]:
285 break
286 attr_random_data[attr] = random_val
287 logger.info("Values generated for integer type attributes")
288
289 return attr_random_data