blob: 1599ffb1e3a350bf0bbc19ff83d918d4dd06195e [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3 -u
George Keishingd9b3e162022-08-18 23:28:35 -05002
3r"""
4Generic utility functions.
5"""
Chris Austenb29d2e82016-06-07 12:25:35 -05006import imp
7import string
Rahul Maheshwari757d80c2016-10-17 01:09:55 -05008import random
Michael Walsh8b270ec2017-01-31 12:07:08 -06009import subprocess
George Keishingd9b3e162022-08-18 23:28:35 -050010from robot.libraries.BuiltIn import BuiltIn
Michael Walsh8b270ec2017-01-31 12:07:08 -060011from robot.utils import DotDict
12
Chris Austenb29d2e82016-06-07 12:25:35 -050013
Rahul Maheshwari757d80c2016-10-17 01:09:55 -050014def random_mac():
15 r"""
16 Return random mac address in the following format.
17 Example: 00:01:6C:80:02:78
18 """
19 return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff)
Gunnar Mills096cd562018-03-26 10:19:12 -050020 for _ in range(6))))
21
Rahul Maheshwari757d80c2016-10-17 01:09:55 -050022
23def random_ip():
24 r"""
25 Return random ip address in the following format.
26 Example: 9.3.128.100
27 """
28 return ".".join(map(str, (random.randint(0, 255)
Gunnar Mills096cd562018-03-26 10:19:12 -050029 for _ in range(4))))
30
Chris Austenb29d2e82016-06-07 12:25:35 -050031
32def get_sensor(module_name, value):
George Keishingd9b3e162022-08-18 23:28:35 -050033 r"""
34 Return sensor matched ID name.
35 """
Gunnar Millsbb398ac2016-11-14 11:50:22 -060036 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050037
Gunnar Millsbb398ac2016-11-14 11:50:22 -060038 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050039
Gunnar Millsbb398ac2016-11-14 11:50:22 -060040 if m.ID_LOOKUP['SENSOR'][i] == value:
41 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050042
Gunnar Millsbb398ac2016-11-14 11:50:22 -060043 return 0xFF
44
Chris Austenb29d2e82016-06-07 12:25:35 -050045
Gunnar Mills096cd562018-03-26 10:19:12 -050046def get_inventory_sensor(module_name, value):
George Keishingd9b3e162022-08-18 23:28:35 -050047 r"""
48 Return sensor matched ID name from inventory.
49 """
Gunnar Millsbb398ac2016-11-14 11:50:22 -060050 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050051
Gunnar Millsbb398ac2016-11-14 11:50:22 -060052 value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>')
Chris Austenb29d2e82016-06-07 12:25:35 -050053
Gunnar Millsbb398ac2016-11-14 11:50:22 -060054 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050055
Gunnar Millsbb398ac2016-11-14 11:50:22 -060056 if m.ID_LOOKUP['SENSOR'][i] == value:
57 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050058
Gunnar Millsbb398ac2016-11-14 11:50:22 -060059 return 0xFF
Chris Austenb29d2e82016-06-07 12:25:35 -050060
61
62################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060063# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050064#
65# i.e. get_inventory_list('../data/Palmetto.py')
66#
George Keishingd9b3e162022-08-18 23:28:35 -050067# [/org/openbmc/inventory/system/chassis/motherboard/cpu0/core0,
Chris Austenb29d2e82016-06-07 12:25:35 -050068# /org/openbmc/inventory/system/chassis/motherboard/dimm0]
69################################################################
70def get_inventory_list(module_name):
George Keishingd9b3e162022-08-18 23:28:35 -050071 r"""
72 Return all FRU URI(s) list available from inventory.
73 """
Chris Austenb29d2e82016-06-07 12:25:35 -050074
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050075 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -060076 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050077
Gunnar Millsbb398ac2016-11-14 11:50:22 -060078 for i in m.ID_LOOKUP['FRU']:
79 s = m.ID_LOOKUP['FRU'][i]
Gunnar Mills096cd562018-03-26 10:19:12 -050080 s = s.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050081 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -060082
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050083 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -050084
85
86################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060087# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050088#
George Keishingbe3cdfd2018-08-28 00:00:06 -050089# i.e. get_inventory_fru_type_list('../data/Witherspoon.py', 'CPU')
Chris Austenb29d2e82016-06-07 12:25:35 -050090#
George Keishingd9b3e162022-08-18 23:28:35 -050091# [/org/openbmc/inventory/system/chassis/motherboard/cpu0,
92# /org/openbmc/inventory/system/chassis/motherboard/cpu1]
Chris Austenb29d2e82016-06-07 12:25:35 -050093################################################################
Gunnar Mills096cd562018-03-26 10:19:12 -050094def get_inventory_fru_type_list(module_name, fru):
George Keishingd9b3e162022-08-18 23:28:35 -050095 r"""
96 Return FRU URI(s) list of a given type from inventory.
97 """
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050098 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -060099 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -0500100
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600101 for i in m.FRU_INSTANCES.keys():
102 if m.FRU_INSTANCES[i]['fru_type'] == fru:
Gunnar Mills096cd562018-03-26 10:19:12 -0500103 s = i.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500104 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600105
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500106 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -0500107
108
109################################################################
110# This will return the URI's of the FRU type that contain VPD
111#
112# i.e. get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
113#
114# [/org/openbmc/inventory/system/chassis/motherboard/dimm0,
115# /org/openbmc/inventory/system/chassis/motherboard/dimm1]
116################################################################
Gunnar Mills096cd562018-03-26 10:19:12 -0500117def get_vpd_inventory_list(module_name, fru):
George Keishingd9b3e162022-08-18 23:28:35 -0500118 r"""
119 Return VPD URI(s) list of a FRU type from inventory.
120 """
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500121 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600122 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -0500123
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600124 for i in m.ID_LOOKUP['FRU_STR']:
125 x = m.ID_LOOKUP['FRU_STR'][i]
Chris Austenb29d2e82016-06-07 12:25:35 -0500126
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600127 if m.FRU_INSTANCES[x]['fru_type'] == fru:
Gunnar Mills096cd562018-03-26 10:19:12 -0500128 s = x.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500129 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600130
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500131 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -0500132
133
134def call_keyword(keyword):
George Keishingd9b3e162022-08-18 23:28:35 -0500135 r"""
136 Return result of the execute robot keyword.
137 """
Chris Austenb29d2e82016-06-07 12:25:35 -0500138 return BuiltIn().run_keyword(keyword)
139
140
141def main():
George Keishingd9b3e162022-08-18 23:28:35 -0500142 r"""
143 Python main func call.
144 """
George Keishing36efbc02018-12-12 10:18:23 -0600145 print(get_vpd_inventory_list('../data/Palmetto.py', 'DIMM'))
Chris Austenb29d2e82016-06-07 12:25:35 -0500146
147
148if __name__ == "__main__":
Gunnar Mills096cd562018-03-26 10:19:12 -0500149 main()
Michael Walsh8b270ec2017-01-31 12:07:08 -0600150
151
Michael Walsh8b270ec2017-01-31 12:07:08 -0600152def get_mtr_report(host=""):
Michael Walsh8b270ec2017-01-31 12:07:08 -0600153 r"""
154 Get an mtr report and return it as a dictionary of dictionaries.
155
156 The key for the top level dictionary will be the host DNS name. The key
157 for the next level dictionary will be the field of a given row of the
158 report.
159
160 Example result:
161
162 report:
George Keishingfb4b1252017-06-15 00:05:45 -0500163 report[host_dummy-dnsname.com]:
164 report[host_dummy-dnsname.com][row_num]: 1
165 report[host_dummy-dnsname.com][host]: host_dummy-dnsname.com
166 report[host_dummy-dnsname.com][loss]: 0.0
167 report[host_dummy-dnsname.com][snt]: 10
168 report[host_dummy-dnsname.com][last]: 0.2
169 report[host_dummy-dnsname.com][avg]: 3.5
170 report[host_dummy-dnsname.com][best]: 0.2
171 report[host_dummy-dnsname.com][wrst]: 32.5
172 report[host_dummy-dnsname.com][stdev]: 10.2
173 report[bmc-dummy-dnsname.com]:
174 report[bmc-dummy-dnsname.com][row_num]: 2
175 report[bmc-dummy-dnsname.com][host]: bmc-dummy-dnsname.com
176 report[bmc-dummy-dnsname.com][loss]: 0.0
177 report[bmc-dummy-dnsname.com][snt]: 10
178 report[bmc-dummy-dnsname.com][last]: 0.5
179 report[bmc-dummy-dnsname.com][avg]: 0.5
180 report[bmc-dummy-dnsname.com][best]: 0.5
181 report[bmc-dummy-dnsname.com][wrst]: 0.5
182 report[bmc-dummy-dnsname.com][stdev]: 0.0
Michael Walsh8b270ec2017-01-31 12:07:08 -0600183
184 Description of arguments:
185 host The DNS name or IP address to be passed to the mtr command.
186 """
187
Gunnar Millsacc7c562019-08-20 13:12:46 -0500188 # Run the mtr command. Exclude the header line. Trim leading space from
Michael Walsh8b270ec2017-01-31 12:07:08 -0600189 # each line. Change all multiple spaces delims to single space delims.
190 cmd_buf = "mtr --report " + host +\
191 " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'"
192 sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE,
193 stderr=subprocess.STDOUT)
194 out_buf, err_buf = sub_proc.communicate()
195 shell_rc = sub_proc.returncode
196
197 # Split the output by line.
198 rows = out_buf.rstrip('\n').split("\n")
199
200 # Initialize report dictionary.
201 report = DotDict()
202 for row in rows:
203 # Process each row of mtr output.
204 # Create a list of fields by splitting on space delimiter.
205 row_list = row.split(" ")
206 # Create dictionary for the row.
207 row = DotDict()
208 row['row_num'] = row_list[0].rstrip('.')
209 row['host'] = row_list[1]
210 row['loss'] = row_list[2].rstrip('%')
211 row['snt'] = row_list[3]
212 row['last'] = row_list[4]
213 row['avg'] = row_list[5]
214 row['best'] = row_list[6]
215 row['wrst'] = row_list[7]
216 row['stdev'] = row_list[8]
217 report[row['host']] = row
218
219 # Return the full report as dictionary of dictionaries.
220 return report
221
Michael Walsh8b270ec2017-01-31 12:07:08 -0600222
Michael Walsh8b270ec2017-01-31 12:07:08 -0600223def get_mtr_row(host=""):
Michael Walsh8b270ec2017-01-31 12:07:08 -0600224 r"""
225 Run an mtr report and get a specified row and return it as a dictionary.
226
227 Example result:
228
229 row:
230 row[row_num]: 2
George Keishingfb4b1252017-06-15 00:05:45 -0500231 row[host]: bmc-dummy-dnsname.com
Michael Walsh8b270ec2017-01-31 12:07:08 -0600232 row[loss]: 0.0
233 row[snt]: 10
234 row[last]: 0.5
235 row[avg]: 0.5
236 row[best]: 0.4
237 row[wrst]: 0.7
238 row[stdev]: 0.1
239
240 Description of arguments:
241 host The DNS name or IP address to be passed to the mtr command as
242 well as the indicating which row of the report to return.
243 """
244
245 report = get_mtr_report(host)
246
247 # The max length of host in output is 28 chars.
248 row = [value for key, value in report.items() if host[0:28] in key][0]
249
250 return row
251
George Keishingefa97352017-03-13 07:13:03 -0500252
George Keishingefa97352017-03-13 07:13:03 -0500253def list_to_set(fru_list=""):
254 r"""
255 Pack the list into a set tuple and return.
256
257 It may seem that this function is rather trivial. However, it simplifies
258 the code and improves robot program readability and achieve the result
259 required.
260
261 Example result:
262
263 set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable',
264 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model'])
265
266 # Description of arguments.
267 fru_list List of FRU's elements.
268 """
269 return set(fru_list)
270
George Keishing81ae45b2017-09-28 14:05:04 -0500271
272def min_list_value(value_list):
273 r"""
274 Returns the element from the list with minimum value.
275 """
276 return min(value_list)
nagarjunb2287138e62022-04-19 16:49:16 +0530277
278
279def convert_lsb_to_msb(string):
280 r"""
281 Reverse given string (From LSB first to MSB first) and converts to HEX.
282
George Keishing86d85f42022-08-18 23:02:22 -0500283 Input string 0a 00
nagarjunb2287138e62022-04-19 16:49:16 +0530284 Return string 0a
285 """
286 datalist = string.split(" ")
287 new_list = datalist[::-1]
288 new_string = "".join([str(element) for element in new_list])
289 return int(new_string, 16)
290
291
292def add_prefix_to_string(string, prefix):
293 r"""
294 Add given prefix to the string and return string.
295
296 Input string 0a 01
297 Return string 0x0a 0x01
298 """
299 prefix_string = ''
300 data_list = string.strip().split(" ")
301 for item in data_list:
302 prefix_string += prefix + item + ' '
303 return prefix_string.strip()