George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | Companion file to utils.robot. |
| 5 | """ |
| 6 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import collections |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | import json |
| 9 | import os |
| 10 | |
| 11 | import bmc_ssh_utils as bsu |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 12 | import gen_print as gp |
| 13 | import gen_robot_keyword as grk |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 14 | import var_funcs as vf |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 15 | from robot.libraries import DateTime |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 16 | from robot.libraries.BuiltIn import BuiltIn |
| 17 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 18 | try: |
| 19 | from robot.utils import DotDict |
| 20 | except ImportError: |
| 21 | pass |
ganesanb | 07958e1 | 2023-04-26 05:38:22 -0700 | [diff] [blame] | 22 | import re |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 23 | |
Sushil Singh | 6624ce5 | 2020-01-22 00:53:41 -0600 | [diff] [blame] | 24 | # The code base directory will be one level up from the directory containing this module. |
| 25 | code_base_dir_path = os.path.dirname(os.path.dirname(__file__)) + os.sep |
| 26 | |
| 27 | |
| 28 | def get_code_base_dir_path(): |
| 29 | r""" |
| 30 | Return the dir path of our code base. |
| 31 | """ |
| 32 | |
| 33 | return code_base_dir_path |
| 34 | |
| 35 | |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 36 | def set_power_policy_method(): |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 37 | r""" |
| 38 | Set the global bmc_power_policy_method to either 'Old' or 'New'. |
| 39 | |
| 40 | The power policy data has moved from an 'org' location to an 'xyz' |
| 41 | location. This keyword will determine whether the new method of getting |
| 42 | the power policy is valid and will set the global bmc_power_policy_method |
| 43 | variable accordingly. If power_policy_setup is already set (by a prior |
| 44 | call to this function), this keyword will simply return. |
| 45 | |
| 46 | If bmc_power_policy_method is "Old", this function will adjust the global |
| 47 | policy variables from data/variables.py: RESTORE_LAST_STATE, |
| 48 | ALWAYS_POWER_ON, ALWAYS_POWER_OFF. |
| 49 | """ |
| 50 | |
| 51 | # Retrieve global variables. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 52 | power_policy_setup = int( |
| 53 | BuiltIn().get_variable_value("${power_policy_setup}", default=0) |
| 54 | ) |
| 55 | bmc_power_policy_method = BuiltIn().get_variable_value( |
| 56 | "${bmc_power_policy_method}", default=0 |
| 57 | ) |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 58 | gp.dpvar(power_policy_setup) |
| 59 | |
| 60 | # If this function has already been run once, we need not continue. |
| 61 | if power_policy_setup: |
| 62 | return |
| 63 | |
| 64 | gp.dpvar(bmc_power_policy_method, 1) |
| 65 | |
| 66 | # The user has not set bmc_power_policy_method via a -v parm so we will |
| 67 | # determine what it should be. |
| 68 | if bmc_power_policy_method == "": |
| 69 | status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 70 | if status == "PASS": |
| 71 | bmc_power_policy_method = "New" |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 72 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 73 | bmc_power_policy_method = "Old" |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 74 | |
| 75 | gp.qpvar(bmc_power_policy_method) |
| 76 | # For old style, we will rewrite these global variable settings to old |
| 77 | # values. |
| 78 | if bmc_power_policy_method == "Old": |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 79 | BuiltIn().set_global_variable( |
| 80 | "${RESTORE_LAST_STATE}", "RESTORE_LAST_STATE" |
| 81 | ) |
| 82 | BuiltIn().set_global_variable("${ALWAYS_POWER_ON}", "ALWAYS_POWER_ON") |
| 83 | BuiltIn().set_global_variable( |
| 84 | "${ALWAYS_POWER_OFF}", "ALWAYS_POWER_OFF" |
| 85 | ) |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 86 | |
| 87 | # Set global variables to control subsequent calls to this function. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 88 | BuiltIn().set_global_variable( |
| 89 | "${bmc_power_policy_method}", bmc_power_policy_method |
| 90 | ) |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 91 | BuiltIn().set_global_variable("${power_policy_setup}", 1) |
| 92 | |
| 93 | |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 94 | def translate_power_policy_value(policy): |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 95 | r""" |
| 96 | Translate the policy value and return the result. |
| 97 | |
| 98 | Using old style functions, callers might call like this with a hard- |
| 99 | code value for policy: |
| 100 | |
George Keishing | efc3ff2 | 2017-12-12 11:49:25 -0600 | [diff] [blame] | 101 | Set BMC Power Policy ALWAYS_POWER_OFF |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 102 | |
| 103 | This function will get the value of the corresponding global variable (if |
| 104 | it exists) and return it. |
| 105 | |
| 106 | This will allow the old style call to still work on systems using the new |
| 107 | method of storing the policy value. |
| 108 | """ |
| 109 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 110 | valid_power_policy_vars = BuiltIn().get_variable_value( |
| 111 | "${valid_power_policy_vars}" |
| 112 | ) |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 113 | |
| 114 | if policy not in valid_power_policy_vars: |
| 115 | return policy |
| 116 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 117 | status, ret_values = grk.run_key_u( |
| 118 | "Get Variable Value ${" + policy + "}", quiet=1 |
| 119 | ) |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 120 | return ret_values |
| 121 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 122 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 123 | def get_bmc_date_time(): |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 124 | r""" |
| 125 | Get date/time info from BMC and return as a dictionary. |
| 126 | |
| 127 | Example of dictionary data returned by this keyword. |
| 128 | time_dict: |
| 129 | [local_time]: Fri 2017-11-03 152756 UTC |
| 130 | [local_time_seconds]: 1509740876 |
| 131 | [universal_time]: Fri 2017-11-03 152756 UTC |
| 132 | [universal_time_seconds]: 1509740876 |
| 133 | [rtc_time]: Fri 2016-05-20 163403 |
| 134 | [rtc_time_seconds]: 1463780043 |
| 135 | [time_zone]: n/a (UTC, +0000) |
| 136 | [network_time_on]: yes |
| 137 | [ntp_synchronized]: no |
| 138 | [rtc_in_local_tz]: no |
| 139 | """ |
| 140 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 141 | out_buf, stderr, rc = bsu.bmc_execute_command("timedatectl") |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 142 | # Example of output returned by call to timedatectl: |
| 143 | # Local time: Fri 2017-11-03 15:27:56 UTC |
| 144 | # Universal time: Fri 2017-11-03 15:27:56 UTC |
| 145 | # RTC time: Fri 2016-05-20 16:34:03 |
| 146 | # Time zone: n/a (UTC, +0000) |
| 147 | # Network time on: yes |
| 148 | # NTP synchronized: no |
| 149 | # RTC in local TZ: no |
| 150 | |
| 151 | # Convert the out_buf to a dictionary. |
| 152 | initial_time_dict = vf.key_value_outbuf_to_dict(out_buf) |
| 153 | |
| 154 | # For each "_time" entry in the dictionary, we will create a corresponding |
| 155 | # "_time_seconds" entry. We create a new dictionary so that the entries |
| 156 | # are kept in a nice order for printing. |
| 157 | try: |
| 158 | result_time_dict = collections.OrderedDict() |
| 159 | except AttributeError: |
| 160 | result_time_dict = DotDict() |
| 161 | |
| 162 | for key, value in initial_time_dict.items(): |
| 163 | result_time_dict[key] = value |
| 164 | if not key.endswith("_time"): |
| 165 | continue |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 166 | result_time_dict[key + "_seconds"] = int( |
| 167 | DateTime.convert_date(value, result_format="epoch") |
| 168 | ) |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 169 | |
| 170 | return result_time_dict |
| 171 | |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 172 | |
| 173 | def get_bmc_df(df_parm_string=""): |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 174 | r""" |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 175 | Get df report from BMC and return as a report "object". |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 176 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 177 | A df report object is a list where each entry is a dictionary whose keys |
| 178 | are the field names from the first entry in report_list. |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 179 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 180 | Example df report object: |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 181 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 182 | df_report: |
| 183 | df_report[0]: |
| 184 | [filesystem]: dev |
| 185 | [1k-blocks]: 247120 |
| 186 | [used]: 0 |
| 187 | [available]: 247120 |
| 188 | [use%]: 0% |
| 189 | [mounted]: /dev |
| 190 | df_report[1]: |
| 191 | [filesystem]: dev |
| 192 | [1k-blocks]: 247120 |
| 193 | [used]: 0 |
| 194 | [available]: 247120 |
| 195 | [use%]: 0% |
| 196 | [mounted]: /dev |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 197 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 198 | . Description of argument(s): |
| 199 | df_parm_string A string containing valid df command parms (e.g. |
| 200 | "-h /var"). |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 201 | """ |
| 202 | |
| 203 | out_buf, stderr, rc = bsu.bmc_execute_command("df " + df_parm_string) |
| 204 | return vf.outbuf_to_report(out_buf) |
George Keishing | 6f407b9 | 2018-01-16 02:21:47 -0600 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def get_sbe(): |
George Keishing | 6f407b9 | 2018-01-16 02:21:47 -0600 | [diff] [blame] | 208 | r""" |
| 209 | Return CFAM value which contains such things as SBE side bit. |
| 210 | """ |
| 211 | |
| 212 | cmd_buf = "pdbg -d p9w -p0 getcfam 0x2808 | sed -re 's/.* = //g'" |
| 213 | out_buf, stderr, rc = bsu.bmc_execute_command(cmd_buf) |
| 214 | |
| 215 | return int(out_buf, 16) |
| 216 | |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 217 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 218 | def compare_mac_address(sys_mac_addr, user_mac_addr): |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 219 | r""" |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 220 | Return 1 if the MAC value matched, otherwise 0. |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 221 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 222 | . Description of argument(s): |
| 223 | sys_mac_addr A valid system MAC string (e.g. "70:e2:84:14:2a:08") |
| 224 | user_mac_addr A user provided MAC string (e.g. "70:e2:84:14:2a:08") |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 225 | """ |
| 226 | |
| 227 | index = 0 |
| 228 | # Example: ['70', 'e2', '84', '14', '2a', '08'] |
| 229 | mac_list = user_mac_addr.split(":") |
| 230 | for item in sys_mac_addr.split(":"): |
| 231 | if int(item, 16) == int(mac_list[index], 16): |
| 232 | index = index + 1 |
| 233 | continue |
| 234 | return 0 |
| 235 | |
| 236 | return 1 |
Michael Walsh | 7847ece | 2018-04-12 11:37:45 -0500 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def get_os_ethtool(interface_name): |
| 240 | r""" |
| 241 | Get OS 'ethtool' output for the given interface_name and return it as a |
| 242 | dictionary. |
| 243 | |
| 244 | Settings for enP52p1s0f0: |
| 245 | Supported ports: [ TP ] |
| 246 | Supported link modes: 10baseT/Half 10baseT/Full |
| 247 | 100baseT/Half 100baseT/Full |
| 248 | 1000baseT/Half 1000baseT/Full |
| 249 | Supported pause frame use: No |
| 250 | Supports auto-negotiation: Yes |
| 251 | Supported FEC modes: Not reported |
| 252 | Advertised link modes: 10baseT/Half 10baseT/Full |
| 253 | 100baseT/Half 100baseT/Full |
| 254 | 1000baseT/Half 1000baseT/Full |
| 255 | Advertised pause frame use: Symmetric |
| 256 | Advertised auto-negotiation: Yes |
| 257 | Advertised FEC modes: Not reported |
| 258 | Speed: Unknown! |
| 259 | Duplex: Unknown! (255) |
| 260 | Port: Twisted Pair |
| 261 | PHYAD: 1 |
| 262 | Transceiver: internal |
| 263 | Auto-negotiation: on |
| 264 | MDI-X: Unknown |
| 265 | Supports Wake-on: g |
| 266 | Wake-on: g |
| 267 | Current message level: 0x000000ff (255) |
| 268 | drv probe link timer ifdown ifup rx_err tx_err |
| 269 | Link detected: no |
| 270 | |
| 271 | Given that data, this function will return the following dictionary. |
| 272 | |
| 273 | ethtool_dict: |
| 274 | [supported_ports]: [ TP ] |
| 275 | [supported_link_modes]: |
| 276 | [supported_link_modes][0]: 10baseT/Half 10baseT/Full |
| 277 | [supported_link_modes][1]: 100baseT/Half 100baseT/Full |
| 278 | [supported_link_modes][2]: 1000baseT/Half 1000baseT/Full |
| 279 | [supported_pause_frame_use]: No |
| 280 | [supports_auto-negotiation]: Yes |
| 281 | [supported_fec_modes]: Not reported |
| 282 | [advertised_link_modes]: |
| 283 | [advertised_link_modes][0]: 10baseT/Half 10baseT/Full |
| 284 | [advertised_link_modes][1]: 100baseT/Half 100baseT/Full |
| 285 | [advertised_link_modes][2]: 1000baseT/Half 1000baseT/Full |
| 286 | [advertised_pause_frame_use]: Symmetric |
| 287 | [advertised_auto-negotiation]: Yes |
| 288 | [advertised_fec_modes]: Not reported |
| 289 | [speed]: Unknown! |
| 290 | [duplex]: Unknown! (255) |
| 291 | [port]: Twisted Pair |
| 292 | [phyad]: 1 |
| 293 | [transceiver]: internal |
| 294 | [auto-negotiation]: on |
| 295 | [mdi-x]: Unknown |
| 296 | [supports_wake-on]: g |
| 297 | [wake-on]: g |
| 298 | [current_message_level]: 0x000000ff (255) |
| 299 | [drv_probe_link_timer_ifdown_ifup_rx_err_tx_err]:<blank> |
| 300 | [link_detected]: no |
| 301 | """ |
| 302 | |
| 303 | # Using sed and tail to massage the data a bit before running |
| 304 | # key_value_outbuf_to_dict. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 305 | cmd_buf = ( |
| 306 | "ethtool " |
| 307 | + interface_name |
| 308 | + " | sed -re 's/(.* link modes:)(.*)/\\1\\n\\2/g' | tail -n +2" |
| 309 | ) |
Michael Walsh | 7847ece | 2018-04-12 11:37:45 -0500 | [diff] [blame] | 310 | stdout, stderr, rc = bsu.os_execute_command(cmd_buf) |
| 311 | result = vf.key_value_outbuf_to_dict(stdout, process_indent=1, strip=" \t") |
| 312 | |
| 313 | return result |
Michael Walsh | 7557510 | 2018-10-04 15:10:44 -0500 | [diff] [blame] | 314 | |
| 315 | |
| 316 | def to_json_ordered(json_str): |
| 317 | r""" |
| 318 | Parse the JSON string data and return an ordered JSON dictionary object. |
| 319 | |
| 320 | Description of argument(s): |
| 321 | json_str The string containing the JSON data. |
| 322 | """ |
| 323 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 324 | try: |
| 325 | return json.loads(json_str, object_pairs_hook=DotDict) |
| 326 | except TypeError: |
| 327 | return json.loads(json_str.decode("utf-8"), object_pairs_hook=DotDict) |
Michael Walsh | 35d78f2 | 2020-01-14 15:01:57 -0600 | [diff] [blame] | 328 | |
| 329 | |
| 330 | def get_bmc_release_info(): |
| 331 | r""" |
| 332 | Get release info from the BMC and return as a dictionary. |
| 333 | |
| 334 | Example: |
| 335 | |
| 336 | ${release_info}= Get BMC Release Info |
| 337 | Rprint Vars release_info |
| 338 | |
| 339 | Output: |
| 340 | |
| 341 | release_info: |
George Keishing | c7cc02b | 2022-08-12 09:09:52 -0500 | [diff] [blame] | 342 | [id]: openbmc-phosphor |
| 343 | [name]: Phosphor OpenBMC (Phosphor OpenBMC Project Reference... |
| 344 | [version]: 2.8.0-dev |
| 345 | [version_id]: 2.8.0-dev-1083-g8954c3505 |
| 346 | [pretty_name]: Phosphor OpenBMC (Phosphor OpenBMC Project Reference... |
| 347 | [build_id]: 2.8.0-dev |
| 348 | [openbmc_target_machine]: witherspoon |
Michael Walsh | 35d78f2 | 2020-01-14 15:01:57 -0600 | [diff] [blame] | 349 | """ |
| 350 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 351 | out_buf, stderr, rc = bsu.bmc_execute_command("cat /etc/os-release") |
Michael Walsh | 35d78f2 | 2020-01-14 15:01:57 -0600 | [diff] [blame] | 352 | return vf.key_value_outbuf_to_dict(out_buf, delim="=", strip='"') |
| 353 | |
| 354 | |
| 355 | def get_os_release_info(): |
| 356 | r""" |
| 357 | Get release info from the OS and return as a dictionary. |
| 358 | |
| 359 | Example: |
| 360 | |
| 361 | ${release_info}= Get OS Release Info |
| 362 | Rprint Vars release_info |
| 363 | |
| 364 | Output: |
| 365 | release_info: |
| 366 | [name]: Red Hat Enterprise Linux Server |
| 367 | [version]: 7.6 (Maipo) |
| 368 | [id]: rhel |
| 369 | [id_like]: fedora |
| 370 | [variant]: Server |
| 371 | [variant_id]: server |
| 372 | [version_id]: 7.6 |
| 373 | [pretty_name]: Red Hat Enterprise Linux Server 7.6 (Maipo) |
| 374 | [ansi_color]: 0;31 |
| 375 | [cpe_name]: cpe:/o:redhat:enterprise_linux:7.6:GA:server |
| 376 | [home_url]: https://www.redhat.com/ |
| 377 | [bug_report_url]: https://bugzilla.redhat.com/ |
| 378 | [redhat_bugzilla_product]: Red Hat Enterprise Linux 7 |
| 379 | [redhat_bugzilla_product_version]: 7.6 |
| 380 | [redhat_support_product]: Red Hat Enterprise Linux |
| 381 | [redhat_support_product_version]: 7.6 |
| 382 | """ |
| 383 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 384 | out_buf, stderr, rc = bsu.os_execute_command("cat /etc/os-release") |
Michael Walsh | 35d78f2 | 2020-01-14 15:01:57 -0600 | [diff] [blame] | 385 | return vf.key_value_outbuf_to_dict(out_buf, delim="=", strip='"') |
Sridevi Ramesh | 9617ebd | 2019-11-25 10:57:21 -0600 | [diff] [blame] | 386 | |
| 387 | |
| 388 | def pdbg(option_string, **bsu_options): |
| 389 | r""" |
| 390 | Run pdbg on the BMC with the caller's option string and return the output. |
| 391 | |
| 392 | Description of argument(s): |
George Keishing | c7cc02b | 2022-08-12 09:09:52 -0500 | [diff] [blame] | 393 | option_string A string of options which are to be processed by the pdbg command. |
| 394 | bsu_options Options to be passed directly to bmc_execute_command. See its prolog for |
| 395 | details. |
Sridevi Ramesh | 9617ebd | 2019-11-25 10:57:21 -0600 | [diff] [blame] | 396 | """ |
| 397 | |
| 398 | # Default print_out to 1. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 399 | if "print_out" not in bsu_options: |
| 400 | bsu_options["print_out"] = 1 |
Sridevi Ramesh | 9617ebd | 2019-11-25 10:57:21 -0600 | [diff] [blame] | 401 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 402 | stdout, stderr, rc = bsu.bmc_execute_command( |
| 403 | "pdbg " + option_string, **bsu_options |
| 404 | ) |
Sridevi Ramesh | 9617ebd | 2019-11-25 10:57:21 -0600 | [diff] [blame] | 405 | return stdout |
Sridevi Ramesh | eb5a162 | 2020-03-19 05:50:46 -0500 | [diff] [blame] | 406 | |
| 407 | |
| 408 | def ecmd(option_string, **bsu_options): |
| 409 | r""" |
| 410 | Run ecmd command on the BMC with the caller's option string and return the output. |
| 411 | |
| 412 | Description of argument(s): |
George Keishing | c7cc02b | 2022-08-12 09:09:52 -0500 | [diff] [blame] | 413 | option_string A string of options which are to be executed on BMC. |
| 414 | (e.g. getscom pu 20010a40 -all, |
| 415 | putscom pu 20010a40 4000000000000000 -p0). |
| 416 | bsu_options Options to be passed directly to bmc_execute_command. See its prolog for |
| 417 | details. |
Sridevi Ramesh | eb5a162 | 2020-03-19 05:50:46 -0500 | [diff] [blame] | 418 | """ |
| 419 | |
| 420 | # Default print_out to 1. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 421 | if "print_out" not in bsu_options: |
| 422 | bsu_options["print_out"] = 1 |
Sridevi Ramesh | eb5a162 | 2020-03-19 05:50:46 -0500 | [diff] [blame] | 423 | |
| 424 | stdout, stderr, rc = bsu.bmc_execute_command(option_string, **bsu_options) |
| 425 | return stdout |
chithrag | 0a8c878 | 2022-03-01 12:35:00 +0000 | [diff] [blame] | 426 | |
| 427 | |
| 428 | def split_string_with_index(stri, n): |
| 429 | r""" |
| 430 | To split every n characters and forms an element for every nth index |
| 431 | |
| 432 | Example : Given ${stri} = "abcdef", then the function call, |
| 433 | ${data}= Split List With Index ${stri} 2 |
| 434 | then, result will be data = ['ab', 'cd', 'ef'] |
| 435 | """ |
| 436 | |
| 437 | n = int(n) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 438 | data = [stri[index : index + n] for index in range(0, len(stri), n)] |
chithrag | 0a8c878 | 2022-03-01 12:35:00 +0000 | [diff] [blame] | 439 | return data |
| 440 | |
| 441 | |
| 442 | def remove_whitespace(instring): |
| 443 | r""" |
| 444 | Removes the white spaces around the string |
| 445 | |
| 446 | Example: instring = " xxx ", then returns instring = "xxx" |
| 447 | """ |
| 448 | |
| 449 | return instring.strip() |
| 450 | |
| 451 | |
| 452 | def zfill_data(data, num): |
| 453 | r""" |
George Keishing | c7cc02b | 2022-08-12 09:09:52 -0500 | [diff] [blame] | 454 | zfill() method adds zeros (0) at the beginning of the string, until it |
| 455 | reaches the specified length. |
chithrag | 0a8c878 | 2022-03-01 12:35:00 +0000 | [diff] [blame] | 456 | |
| 457 | Usage : ${anystr}= Zfill Data ${data} num |
| 458 | |
| 459 | Example : Binary of one Byte has 8 bits - xxxx xxxx |
| 460 | |
| 461 | Consider ${binary} has only 3 bits after converting from Hexadecimal/decimal to Binary |
| 462 | Say ${binary} = 110 then, |
| 463 | ${binary}= Zfill Data ${binary} 8 |
| 464 | Now ${binary} will be 0000 0110 |
| 465 | """ |
| 466 | |
| 467 | return data.zfill(int(num)) |
ganesanb | c288aff | 2022-05-19 19:55:47 +0530 | [diff] [blame] | 468 | |
| 469 | |
| 470 | def get_subsequent_value_from_list(list, value): |
| 471 | r""" |
| 472 | returns first index of the element occurrence. |
| 473 | """ |
| 474 | |
| 475 | index = [list.index(i) for i in list if value in i] |
| 476 | return index |
| 477 | |
| 478 | |
| 479 | def return_decoded_string(input): |
| 480 | r""" |
| 481 | returns decoded string of encoded byte. |
| 482 | """ |
| 483 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 484 | encoded_string = input.encode("ascii", "ignore") |
ganesanb | c288aff | 2022-05-19 19:55:47 +0530 | [diff] [blame] | 485 | decoded_string = encoded_string.decode() |
| 486 | return decoded_string |
ganesanb | 07958e1 | 2023-04-26 05:38:22 -0700 | [diff] [blame] | 487 | |
| 488 | |
| 489 | def remove_unicode_from_uri(uri): |
| 490 | r""" |
| 491 | returns dbus uri without unicode in prefix |
| 492 | """ |
| 493 | |
| 494 | return re.sub("`-|\\|-", "", uri) |
ganesanb | bd187d2 | 2023-05-03 11:00:18 +0000 | [diff] [blame] | 495 | |
| 496 | |
| 497 | def get_bmc_major_minor_version(version): |
| 498 | r""" |
| 499 | returns major version and minor version |
| 500 | from cat /etc/os-release command. |
| 501 | For example, |
| 502 | xyz23.01 --> [23, 01] |
| 503 | xyz.0-112 --> [0, 112] |
| 504 | ZERzzYY-23.04-1-xx3 --> [23, 04, 1, 3] |
| 505 | """ |
| 506 | |
| 507 | return re.findall(r"\d+", re.sub("[A-Z]|[a-z]", "", version)) |
ganesanb | 5502e3e | 2022-12-13 11:07:43 +0000 | [diff] [blame] | 508 | |
| 509 | |
| 510 | def convert_name_into_bytes_with_prefix(name): |
| 511 | r""" |
| 512 | Convert name into bytes with prefix 0x |
| 513 | """ |
| 514 | |
| 515 | bytes_list = [] |
| 516 | |
| 517 | for letter in name: |
| 518 | bytes_list.append(hex(ord(letter))) |
| 519 | |
| 520 | return bytes_list |
| 521 | |
| 522 | |
| 523 | def convert_name_into_bytes_without_prefix(name): |
| 524 | r""" |
| 525 | Convert name into bytes |
| 526 | """ |
| 527 | |
| 528 | tmp_lst = [] |
| 529 | |
| 530 | for letter in name: |
| 531 | value = convert_to_hex_value_without_prefix(letter) |
| 532 | tmp_lst.append(value) |
| 533 | |
| 534 | return tmp_lst |
| 535 | |
| 536 | |
| 537 | def convert_to_hex_value_without_prefix(letter): |
| 538 | r""" |
| 539 | Convert into hex |
| 540 | """ |
| 541 | |
| 542 | value = hex(ord(letter)) |
| 543 | if value[:2] == "0x": |
| 544 | value = value[2:] |
| 545 | |
| 546 | return value |
| 547 | |
| 548 | |
| 549 | def convert_prefix_hex_list_to_non_prefix_hex_list(list): |
| 550 | r""" |
| 551 | Convert into list of hex with prefix to list of hex without prefix. |
| 552 | """ |
| 553 | |
| 554 | tmp_list = [] |
| 555 | |
| 556 | for value in list: |
| 557 | if value[:2] == "0x": |
| 558 | tmp_list.append(value[2:]) |
| 559 | |
| 560 | return tmp_list |