| George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 -u | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 2 |  | 
|  | 3 | r""" | 
|  | 4 | Generic utility functions. | 
|  | 5 | """ | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 6 | import importlib.util | 
| George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import random | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | import string | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 9 | import subprocess | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 10 |  | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 11 | from robot.libraries.BuiltIn import BuiltIn | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 12 | from robot.utils import DotDict | 
|  | 13 |  | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 14 |  | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 15 | def load_source(name, module_path): | 
|  | 16 | r""" | 
|  | 17 | import util to replace deprecated imp.load_source | 
|  | 18 | """ | 
|  | 19 | spec = importlib.util.spec_from_file_location(name, module_path) | 
|  | 20 | module = importlib.util.module_from_spec(spec) | 
|  | 21 | spec.loader.exec_module(module) | 
|  | 22 | return module | 
|  | 23 |  | 
|  | 24 |  | 
| Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 25 | def random_mac(): | 
|  | 26 | r""" | 
|  | 27 | Return random mac address in the following format. | 
|  | 28 | Example: 00:01:6C:80:02:78 | 
|  | 29 | """ | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 30 | return ":".join( | 
|  | 31 | map( | 
|  | 32 | lambda x: "%02x" % x, | 
|  | 33 | (random.randint(0x00, 0xFF) for _ in range(6)), | 
|  | 34 | ) | 
|  | 35 | ) | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 36 |  | 
| Rahul Maheshwari | 757d80c | 2016-10-17 01:09:55 -0500 | [diff] [blame] | 37 |  | 
|  | 38 | def random_ip(): | 
|  | 39 | r""" | 
|  | 40 | Return random ip address in the following format. | 
|  | 41 | Example: 9.3.128.100 | 
|  | 42 | """ | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 43 | return ".".join(map(str, (random.randint(0, 255) for _ in range(4)))) | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 44 |  | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 45 |  | 
|  | 46 | def get_sensor(module_name, value): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 47 | r""" | 
|  | 48 | Return sensor matched ID name. | 
|  | 49 | """ | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 50 | m = load_source("module.name", module_name) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 51 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 52 | for i in m.ID_LOOKUP["SENSOR"]: | 
|  | 53 | if m.ID_LOOKUP["SENSOR"][i] == value: | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 54 | return i | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 55 |  | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 56 | return 0xFF | 
|  | 57 |  | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 58 |  | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 59 | def get_inventory_sensor(module_name, value): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 60 | r""" | 
|  | 61 | Return sensor matched ID name from inventory. | 
|  | 62 | """ | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 63 | m = load_source("module.name", module_name) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 64 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 65 | value = string.replace(value, m.INVENTORY_ROOT, "<inventory_root>") | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 66 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 67 | for i in m.ID_LOOKUP["SENSOR"]: | 
|  | 68 | if m.ID_LOOKUP["SENSOR"][i] == value: | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 69 | return i | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 70 |  | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 71 | return 0xFF | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 72 |  | 
|  | 73 |  | 
|  | 74 | ################################################################ | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 75 | #  This will return the URI's of the FRU type | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 76 | # | 
|  | 77 | #  i.e.  get_inventory_list('../data/Palmetto.py') | 
|  | 78 | # | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 79 | #  [/org/openbmc/inventory/system/chassis/motherboard/cpu0/core0, | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 80 | #   /org/openbmc/inventory/system/chassis/motherboard/dimm0] | 
|  | 81 | ################################################################ | 
|  | 82 | def get_inventory_list(module_name): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 83 | r""" | 
|  | 84 | Return all FRU URI(s) list available from inventory. | 
|  | 85 | """ | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 86 |  | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 87 | inventory_list = [] | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 88 | m = load_source("module.name", module_name) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 89 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 90 | for i in m.ID_LOOKUP["FRU"]: | 
|  | 91 | s = m.ID_LOOKUP["FRU"][i] | 
|  | 92 | s = s.replace("<inventory_root>", m.INVENTORY_ROOT) | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 93 | inventory_list.append(s) | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 94 |  | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 95 | return inventory_list | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 96 |  | 
|  | 97 |  | 
|  | 98 | ################################################################ | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 99 | #  This will return the URI's of the FRU type | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 100 | # | 
| George Keishing | be3cdfd | 2018-08-28 00:00:06 -0500 | [diff] [blame] | 101 | #  i.e.  get_inventory_fru_type_list('../data/Witherspoon.py', 'CPU') | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 102 | # | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 103 | #  [/org/openbmc/inventory/system/chassis/motherboard/cpu0, | 
|  | 104 | #   /org/openbmc/inventory/system/chassis/motherboard/cpu1] | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 105 | ################################################################ | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 106 | def get_inventory_fru_type_list(module_name, fru): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 107 | r""" | 
|  | 108 | Return FRU URI(s) list of a given type from inventory. | 
|  | 109 | """ | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 110 | inventory_list = [] | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 111 | m = load_source("module.name", module_name) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 112 |  | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 113 | for i in m.FRU_INSTANCES.keys(): | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 114 | if m.FRU_INSTANCES[i]["fru_type"] == fru: | 
|  | 115 | s = i.replace("<inventory_root>", m.INVENTORY_ROOT) | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 116 | inventory_list.append(s) | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 117 |  | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 118 | return inventory_list | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 119 |  | 
|  | 120 |  | 
|  | 121 | ################################################################ | 
|  | 122 | #  This will return the URI's of the FRU type that contain VPD | 
|  | 123 | # | 
|  | 124 | #  i.e.  get_vpd_inventory_list('../data/Palmetto.py', 'DIMM') | 
|  | 125 | # | 
|  | 126 | #  [/org/openbmc/inventory/system/chassis/motherboard/dimm0, | 
|  | 127 | #   /org/openbmc/inventory/system/chassis/motherboard/dimm1] | 
|  | 128 | ################################################################ | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 129 | def get_vpd_inventory_list(module_name, fru): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 130 | r""" | 
|  | 131 | Return VPD URI(s) list of a FRU type from inventory. | 
|  | 132 | """ | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 133 | inventory_list = [] | 
| Brian Ma | 139f1da | 2024-10-18 13:34:14 +0800 | [diff] [blame] | 134 | m = load_source("module.name", module_name) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 135 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 136 | for i in m.ID_LOOKUP["FRU_STR"]: | 
|  | 137 | x = m.ID_LOOKUP["FRU_STR"][i] | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 138 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 139 | if m.FRU_INSTANCES[x]["fru_type"] == fru: | 
|  | 140 | s = x.replace("<inventory_root>", m.INVENTORY_ROOT) | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 141 | inventory_list.append(s) | 
| Gunnar Mills | bb398ac | 2016-11-14 11:50:22 -0600 | [diff] [blame] | 142 |  | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 143 | return inventory_list | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 144 |  | 
|  | 145 |  | 
|  | 146 | def call_keyword(keyword): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 147 | r""" | 
|  | 148 | Return result of the execute robot keyword. | 
|  | 149 | """ | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 150 | return BuiltIn().run_keyword(keyword) | 
|  | 151 |  | 
|  | 152 |  | 
|  | 153 | def main(): | 
| George Keishing | d9b3e16 | 2022-08-18 23:28:35 -0500 | [diff] [blame] | 154 | r""" | 
|  | 155 | Python main func call. | 
|  | 156 | """ | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 157 | print(get_vpd_inventory_list("../data/Palmetto.py", "DIMM")) | 
| Chris Austen | b29d2e8 | 2016-06-07 12:25:35 -0500 | [diff] [blame] | 158 |  | 
|  | 159 |  | 
|  | 160 | if __name__ == "__main__": | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 161 | main() | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 162 |  | 
|  | 163 |  | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 164 | def get_mtr_report(host=""): | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 165 | r""" | 
|  | 166 | Get an mtr report and return it as a dictionary of dictionaries. | 
|  | 167 |  | 
|  | 168 | The key for the top level dictionary will be the host DNS name.  The key | 
|  | 169 | for the next level dictionary will be the field of a given row of the | 
|  | 170 | report. | 
|  | 171 |  | 
|  | 172 | Example result: | 
|  | 173 |  | 
|  | 174 | report: | 
| George Keishing | fb4b125 | 2017-06-15 00:05:45 -0500 | [diff] [blame] | 175 | report[host_dummy-dnsname.com]: | 
|  | 176 | report[host_dummy-dnsname.com][row_num]:  1 | 
|  | 177 | report[host_dummy-dnsname.com][host]:     host_dummy-dnsname.com | 
|  | 178 | report[host_dummy-dnsname.com][loss]:     0.0 | 
|  | 179 | report[host_dummy-dnsname.com][snt]:      10 | 
|  | 180 | report[host_dummy-dnsname.com][last]:     0.2 | 
|  | 181 | report[host_dummy-dnsname.com][avg]:      3.5 | 
|  | 182 | report[host_dummy-dnsname.com][best]:     0.2 | 
|  | 183 | report[host_dummy-dnsname.com][wrst]:     32.5 | 
|  | 184 | report[host_dummy-dnsname.com][stdev]:    10.2 | 
|  | 185 | report[bmc-dummy-dnsname.com]: | 
|  | 186 | report[bmc-dummy-dnsname.com][row_num]:     2 | 
|  | 187 | report[bmc-dummy-dnsname.com][host]:        bmc-dummy-dnsname.com | 
|  | 188 | report[bmc-dummy-dnsname.com][loss]:        0.0 | 
|  | 189 | report[bmc-dummy-dnsname.com][snt]:         10 | 
|  | 190 | report[bmc-dummy-dnsname.com][last]:        0.5 | 
|  | 191 | report[bmc-dummy-dnsname.com][avg]:         0.5 | 
|  | 192 | report[bmc-dummy-dnsname.com][best]:        0.5 | 
|  | 193 | report[bmc-dummy-dnsname.com][wrst]:        0.5 | 
|  | 194 | report[bmc-dummy-dnsname.com][stdev]:       0.0 | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 195 |  | 
|  | 196 | Description of arguments: | 
|  | 197 | host   The DNS name or IP address to be passed to the mtr command. | 
|  | 198 | """ | 
|  | 199 |  | 
| Gunnar Mills | acc7c56 | 2019-08-20 13:12:46 -0500 | [diff] [blame] | 200 | # Run the mtr command.  Exclude the header line.  Trim leading space from | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 201 | # each line.  Change all multiple spaces delims to single space delims. | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 202 | cmd_buf = ( | 
|  | 203 | "mtr --report " | 
|  | 204 | + host | 
|  | 205 | + " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'" | 
|  | 206 | ) | 
|  | 207 | sub_proc = subprocess.Popen( | 
|  | 208 | cmd_buf, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT | 
|  | 209 | ) | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 210 | out_buf, err_buf = sub_proc.communicate() | 
|  | 211 | shell_rc = sub_proc.returncode | 
| manimozhik | 6abea65 | 2023-11-09 07:05:16 +0000 | [diff] [blame] | 212 | out_buf = out_buf.decode("utf-8") | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 213 |  | 
|  | 214 | # Split the output by line. | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 215 | rows = out_buf.rstrip("\n").split("\n") | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 216 |  | 
|  | 217 | # Initialize report dictionary. | 
|  | 218 | report = DotDict() | 
|  | 219 | for row in rows: | 
|  | 220 | # Process each row of mtr output. | 
|  | 221 | # Create a list of fields by splitting on space delimiter. | 
|  | 222 | row_list = row.split(" ") | 
|  | 223 | # Create dictionary for the row. | 
|  | 224 | row = DotDict() | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 225 | row["row_num"] = row_list[0].rstrip(".") | 
|  | 226 | row["host"] = row_list[1] | 
|  | 227 | row["loss"] = row_list[2].rstrip("%") | 
|  | 228 | row["snt"] = row_list[3] | 
|  | 229 | row["last"] = row_list[4] | 
|  | 230 | row["avg"] = row_list[5] | 
|  | 231 | row["best"] = row_list[6] | 
|  | 232 | row["wrst"] = row_list[7] | 
|  | 233 | row["stdev"] = row_list[8] | 
|  | 234 | report[row["host"]] = row | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 235 |  | 
|  | 236 | # Return the full report as dictionary of dictionaries. | 
|  | 237 | return report | 
|  | 238 |  | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 239 |  | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 240 | def get_mtr_row(host=""): | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 241 | r""" | 
|  | 242 | Run an mtr report and get a specified row and return it as a dictionary. | 
|  | 243 |  | 
|  | 244 | Example result: | 
|  | 245 |  | 
|  | 246 | row: | 
|  | 247 | row[row_num]:              2 | 
| George Keishing | fb4b125 | 2017-06-15 00:05:45 -0500 | [diff] [blame] | 248 | row[host]:                 bmc-dummy-dnsname.com | 
| Michael Walsh | 8b270ec | 2017-01-31 12:07:08 -0600 | [diff] [blame] | 249 | row[loss]:                 0.0 | 
|  | 250 | row[snt]:                  10 | 
|  | 251 | row[last]:                 0.5 | 
|  | 252 | row[avg]:                  0.5 | 
|  | 253 | row[best]:                 0.4 | 
|  | 254 | row[wrst]:                 0.7 | 
|  | 255 | row[stdev]:                0.1 | 
|  | 256 |  | 
|  | 257 | Description of arguments: | 
|  | 258 | host   The DNS name or IP address to be passed to the mtr command as | 
|  | 259 | well as the indicating which row of the report to return. | 
|  | 260 | """ | 
|  | 261 |  | 
|  | 262 | report = get_mtr_report(host) | 
|  | 263 |  | 
|  | 264 | # The max length of host in output is 28 chars. | 
|  | 265 | row = [value for key, value in report.items() if host[0:28] in key][0] | 
|  | 266 |  | 
|  | 267 | return row | 
|  | 268 |  | 
| George Keishing | efa9735 | 2017-03-13 07:13:03 -0500 | [diff] [blame] | 269 |  | 
| George Keishing | efa9735 | 2017-03-13 07:13:03 -0500 | [diff] [blame] | 270 | def list_to_set(fru_list=""): | 
|  | 271 | r""" | 
|  | 272 | Pack the list into a set tuple and return. | 
|  | 273 |  | 
|  | 274 | It may seem that this function is rather trivial. However, it simplifies | 
|  | 275 | the code and improves robot program readability and achieve the result | 
|  | 276 | required. | 
|  | 277 |  | 
|  | 278 | Example result: | 
|  | 279 |  | 
|  | 280 | set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable', | 
|  | 281 | 'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model']) | 
|  | 282 |  | 
|  | 283 | # Description of arguments. | 
|  | 284 | fru_list   List of FRU's elements. | 
|  | 285 | """ | 
|  | 286 | return set(fru_list) | 
|  | 287 |  | 
| George Keishing | 81ae45b | 2017-09-28 14:05:04 -0500 | [diff] [blame] | 288 |  | 
|  | 289 | def min_list_value(value_list): | 
|  | 290 | r""" | 
|  | 291 | Returns the element from the list with minimum value. | 
|  | 292 | """ | 
|  | 293 | return min(value_list) | 
| nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 294 |  | 
|  | 295 |  | 
|  | 296 | def convert_lsb_to_msb(string): | 
|  | 297 | r""" | 
|  | 298 | Reverse given string (From LSB first to MSB first) and converts to HEX. | 
|  | 299 |  | 
| George Keishing | 86d85f4 | 2022-08-18 23:02:22 -0500 | [diff] [blame] | 300 | Input string     0a 00 | 
| nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 301 | Return string    0a | 
|  | 302 | """ | 
|  | 303 | datalist = string.split(" ") | 
|  | 304 | new_list = datalist[::-1] | 
|  | 305 | new_string = "".join([str(element) for element in new_list]) | 
|  | 306 | return int(new_string, 16) | 
|  | 307 |  | 
|  | 308 |  | 
|  | 309 | def add_prefix_to_string(string, prefix): | 
|  | 310 | r""" | 
|  | 311 | Add given prefix to the string and return string. | 
|  | 312 |  | 
|  | 313 | Input string      0a 01 | 
|  | 314 | Return string     0x0a 0x01 | 
|  | 315 | """ | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 316 | prefix_string = "" | 
| nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 317 | data_list = string.strip().split(" ") | 
|  | 318 | for item in data_list: | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 319 | prefix_string += prefix + item + " " | 
| nagarjunb22 | 87138e6 | 2022-04-19 16:49:16 +0530 | [diff] [blame] | 320 | return prefix_string.strip() | 
| George Keishing | 8ef9e72 | 2023-01-18 10:21:31 +0530 | [diff] [blame] | 321 |  | 
|  | 322 |  | 
|  | 323 | def get_value_from_nested_dict(key, nested_dict): | 
|  | 324 | r""" | 
|  | 325 | Returns the key value from the nested dictionary. | 
|  | 326 |  | 
|  | 327 | key               Key value of the dictionary to look up. | 
|  | 328 | nested_dict       Dictionary data. | 
|  | 329 | """ | 
|  | 330 | result = [] | 
|  | 331 | for k, v in nested_dict.items(): | 
|  | 332 | if k == key: | 
|  | 333 | result.append(v) | 
|  | 334 | elif isinstance(v, dict) and k != key: | 
|  | 335 | result += get_value_from_nested_dict(key, v) | 
|  | 336 |  | 
|  | 337 | return result |