blob: 2b7660f48fa9bd1e1614f3ce25f468c669c8e3a8 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3 -u
Chris Austenb29d2e82016-06-07 12:25:35 -05002import sys
3from robot.libraries.BuiltIn import BuiltIn
4import imp
5import string
Rahul Maheshwari757d80c2016-10-17 01:09:55 -05006import random
Michael Walsh8b270ec2017-01-31 12:07:08 -06007import subprocess
8from robot.utils import DotDict
9
Chris Austenb29d2e82016-06-07 12:25:35 -050010
Rahul Maheshwari757d80c2016-10-17 01:09:55 -050011def random_mac():
12 r"""
13 Return random mac address in the following format.
14 Example: 00:01:6C:80:02:78
15 """
16 return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff)
Gunnar Mills096cd562018-03-26 10:19:12 -050017 for _ in range(6))))
18
Rahul Maheshwari757d80c2016-10-17 01:09:55 -050019
20def random_ip():
21 r"""
22 Return random ip address in the following format.
23 Example: 9.3.128.100
24 """
25 return ".".join(map(str, (random.randint(0, 255)
Gunnar Mills096cd562018-03-26 10:19:12 -050026 for _ in range(4))))
27
Chris Austenb29d2e82016-06-07 12:25:35 -050028
29def get_sensor(module_name, value):
Gunnar Millsbb398ac2016-11-14 11:50:22 -060030 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050031
Gunnar Millsbb398ac2016-11-14 11:50:22 -060032 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050033
Gunnar Millsbb398ac2016-11-14 11:50:22 -060034 if m.ID_LOOKUP['SENSOR'][i] == value:
35 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050036
Gunnar Millsbb398ac2016-11-14 11:50:22 -060037 return 0xFF
38
Chris Austenb29d2e82016-06-07 12:25:35 -050039
Gunnar Mills096cd562018-03-26 10:19:12 -050040def get_inventory_sensor(module_name, value):
Gunnar Millsbb398ac2016-11-14 11:50:22 -060041 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050042
Gunnar Millsbb398ac2016-11-14 11:50:22 -060043 value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>')
Chris Austenb29d2e82016-06-07 12:25:35 -050044
Gunnar Millsbb398ac2016-11-14 11:50:22 -060045 for i in m.ID_LOOKUP['SENSOR']:
Chris Austenb29d2e82016-06-07 12:25:35 -050046
Gunnar Millsbb398ac2016-11-14 11:50:22 -060047 if m.ID_LOOKUP['SENSOR'][i] == value:
48 return i
Chris Austenb29d2e82016-06-07 12:25:35 -050049
Gunnar Millsbb398ac2016-11-14 11:50:22 -060050 return 0xFF
Chris Austenb29d2e82016-06-07 12:25:35 -050051
52
53################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060054# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050055#
56# i.e. get_inventory_list('../data/Palmetto.py')
57#
58# [/org/openbmc/inventory//system/chassis/motherboard/cpu0/core0,
59# /org/openbmc/inventory/system/chassis/motherboard/dimm0]
60################################################################
61def get_inventory_list(module_name):
62
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050063 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -060064 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050065
Gunnar Millsbb398ac2016-11-14 11:50:22 -060066 for i in m.ID_LOOKUP['FRU']:
67 s = m.ID_LOOKUP['FRU'][i]
Gunnar Mills096cd562018-03-26 10:19:12 -050068 s = s.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050069 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -060070
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050071 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -050072
73
74################################################################
Gunnar Millsbb398ac2016-11-14 11:50:22 -060075# This will return the URI's of the FRU type
Chris Austenb29d2e82016-06-07 12:25:35 -050076#
George Keishingbe3cdfd2018-08-28 00:00:06 -050077# i.e. get_inventory_fru_type_list('../data/Witherspoon.py', 'CPU')
Chris Austenb29d2e82016-06-07 12:25:35 -050078#
79# [/org/openbmc/inventory//system/chassis/motherboard/cpu0,
80# /org/openbmc/inventory//system/chassis/motherboard/cpu1]
81################################################################
Gunnar Mills096cd562018-03-26 10:19:12 -050082def get_inventory_fru_type_list(module_name, fru):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050083 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -060084 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -050085
Gunnar Millsbb398ac2016-11-14 11:50:22 -060086 for i in m.FRU_INSTANCES.keys():
87 if m.FRU_INSTANCES[i]['fru_type'] == fru:
Gunnar Mills096cd562018-03-26 10:19:12 -050088 s = i.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050089 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -060090
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050091 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -050092
93
94################################################################
95# This will return the URI's of the FRU type that contain VPD
96#
97# i.e. get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
98#
99# [/org/openbmc/inventory/system/chassis/motherboard/dimm0,
100# /org/openbmc/inventory/system/chassis/motherboard/dimm1]
101################################################################
Gunnar Mills096cd562018-03-26 10:19:12 -0500102def get_vpd_inventory_list(module_name, fru):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500103 inventory_list = []
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600104 m = imp.load_source('module.name', module_name)
Chris Austenb29d2e82016-06-07 12:25:35 -0500105
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600106 for i in m.ID_LOOKUP['FRU_STR']:
107 x = m.ID_LOOKUP['FRU_STR'][i]
Chris Austenb29d2e82016-06-07 12:25:35 -0500108
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600109 if m.FRU_INSTANCES[x]['fru_type'] == fru:
Gunnar Mills096cd562018-03-26 10:19:12 -0500110 s = x.replace('<inventory_root>', m.INVENTORY_ROOT)
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500111 inventory_list.append(s)
Gunnar Millsbb398ac2016-11-14 11:50:22 -0600112
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500113 return inventory_list
Chris Austenb29d2e82016-06-07 12:25:35 -0500114
115
116def call_keyword(keyword):
117 return BuiltIn().run_keyword(keyword)
118
119
120def main():
George Keishing36efbc02018-12-12 10:18:23 -0600121 print(get_vpd_inventory_list('../data/Palmetto.py', 'DIMM'))
Chris Austenb29d2e82016-06-07 12:25:35 -0500122
123
124if __name__ == "__main__":
Gunnar Mills096cd562018-03-26 10:19:12 -0500125 main()
Michael Walsh8b270ec2017-01-31 12:07:08 -0600126
127
Michael Walsh8b270ec2017-01-31 12:07:08 -0600128def get_mtr_report(host=""):
Michael Walsh8b270ec2017-01-31 12:07:08 -0600129 r"""
130 Get an mtr report and return it as a dictionary of dictionaries.
131
132 The key for the top level dictionary will be the host DNS name. The key
133 for the next level dictionary will be the field of a given row of the
134 report.
135
136 Example result:
137
138 report:
George Keishingfb4b1252017-06-15 00:05:45 -0500139 report[host_dummy-dnsname.com]:
140 report[host_dummy-dnsname.com][row_num]: 1
141 report[host_dummy-dnsname.com][host]: host_dummy-dnsname.com
142 report[host_dummy-dnsname.com][loss]: 0.0
143 report[host_dummy-dnsname.com][snt]: 10
144 report[host_dummy-dnsname.com][last]: 0.2
145 report[host_dummy-dnsname.com][avg]: 3.5
146 report[host_dummy-dnsname.com][best]: 0.2
147 report[host_dummy-dnsname.com][wrst]: 32.5
148 report[host_dummy-dnsname.com][stdev]: 10.2
149 report[bmc-dummy-dnsname.com]:
150 report[bmc-dummy-dnsname.com][row_num]: 2
151 report[bmc-dummy-dnsname.com][host]: bmc-dummy-dnsname.com
152 report[bmc-dummy-dnsname.com][loss]: 0.0
153 report[bmc-dummy-dnsname.com][snt]: 10
154 report[bmc-dummy-dnsname.com][last]: 0.5
155 report[bmc-dummy-dnsname.com][avg]: 0.5
156 report[bmc-dummy-dnsname.com][best]: 0.5
157 report[bmc-dummy-dnsname.com][wrst]: 0.5
158 report[bmc-dummy-dnsname.com][stdev]: 0.0
Michael Walsh8b270ec2017-01-31 12:07:08 -0600159
160 Description of arguments:
161 host The DNS name or IP address to be passed to the mtr command.
162 """
163
Gunnar Millsacc7c562019-08-20 13:12:46 -0500164 # Run the mtr command. Exclude the header line. Trim leading space from
Michael Walsh8b270ec2017-01-31 12:07:08 -0600165 # each line. Change all multiple spaces delims to single space delims.
166 cmd_buf = "mtr --report " + host +\
167 " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'"
168 sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE,
169 stderr=subprocess.STDOUT)
170 out_buf, err_buf = sub_proc.communicate()
171 shell_rc = sub_proc.returncode
172
173 # Split the output by line.
174 rows = out_buf.rstrip('\n').split("\n")
175
176 # Initialize report dictionary.
177 report = DotDict()
178 for row in rows:
179 # Process each row of mtr output.
180 # Create a list of fields by splitting on space delimiter.
181 row_list = row.split(" ")
182 # Create dictionary for the row.
183 row = DotDict()
184 row['row_num'] = row_list[0].rstrip('.')
185 row['host'] = row_list[1]
186 row['loss'] = row_list[2].rstrip('%')
187 row['snt'] = row_list[3]
188 row['last'] = row_list[4]
189 row['avg'] = row_list[5]
190 row['best'] = row_list[6]
191 row['wrst'] = row_list[7]
192 row['stdev'] = row_list[8]
193 report[row['host']] = row
194
195 # Return the full report as dictionary of dictionaries.
196 return report
197
Michael Walsh8b270ec2017-01-31 12:07:08 -0600198
Michael Walsh8b270ec2017-01-31 12:07:08 -0600199def get_mtr_row(host=""):
Michael Walsh8b270ec2017-01-31 12:07:08 -0600200 r"""
201 Run an mtr report and get a specified row and return it as a dictionary.
202
203 Example result:
204
205 row:
206 row[row_num]: 2
George Keishingfb4b1252017-06-15 00:05:45 -0500207 row[host]: bmc-dummy-dnsname.com
Michael Walsh8b270ec2017-01-31 12:07:08 -0600208 row[loss]: 0.0
209 row[snt]: 10
210 row[last]: 0.5
211 row[avg]: 0.5
212 row[best]: 0.4
213 row[wrst]: 0.7
214 row[stdev]: 0.1
215
216 Description of arguments:
217 host The DNS name or IP address to be passed to the mtr command as
218 well as the indicating which row of the report to return.
219 """
220
221 report = get_mtr_report(host)
222
223 # The max length of host in output is 28 chars.
224 row = [value for key, value in report.items() if host[0:28] in key][0]
225
226 return row
227
George Keishingefa97352017-03-13 07:13:03 -0500228
George Keishingefa97352017-03-13 07:13:03 -0500229def list_to_set(fru_list=""):
230 r"""
231 Pack the list into a set tuple and return.
232
233 It may seem that this function is rather trivial. However, it simplifies
234 the code and improves robot program readability and achieve the result
235 required.
236
237 Example result:
238
239 set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable',
240 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model'])
241
242 # Description of arguments.
243 fru_list List of FRU's elements.
244 """
245 return set(fru_list)
246
George Keishing81ae45b2017-09-28 14:05:04 -0500247
248def min_list_value(value_list):
249 r"""
250 Returns the element from the list with minimum value.
251 """
252 return min(value_list)
nagarjunb2287138e62022-04-19 16:49:16 +0530253
254
255def convert_lsb_to_msb(string):
256 r"""
257 Reverse given string (From LSB first to MSB first) and converts to HEX.
258
George Keishing86d85f42022-08-18 23:02:22 -0500259 Input string 0a 00
nagarjunb2287138e62022-04-19 16:49:16 +0530260 Return string 0a
261 """
262 datalist = string.split(" ")
263 new_list = datalist[::-1]
264 new_string = "".join([str(element) for element in new_list])
265 return int(new_string, 16)
266
267
268def add_prefix_to_string(string, prefix):
269 r"""
270 Add given prefix to the string and return string.
271
272 Input string 0a 01
273 Return string 0x0a 0x01
274 """
275 prefix_string = ''
276 data_list = string.strip().split(" ")
277 for item in data_list:
278 prefix_string += prefix + item + ' '
279 return prefix_string.strip()