1e7e9171eSGeorge Keishing#!/usr/bin/env python3 2fdc5ced1SMichael Walsh 3fdc5ced1SMichael Walshr""" 4fdc5ced1SMichael WalshCompanion file to utils.robot. 5fdc5ced1SMichael Walsh""" 6fdc5ced1SMichael Walsh 7e635ddc0SGeorge Keishingimport collections 820f38712SPatrick Williamsimport json 920f38712SPatrick Williamsimport os 1020f38712SPatrick Williams 1120f38712SPatrick Williamsimport bmc_ssh_utils as bsu 12fdc5ced1SMichael Walshimport gen_print as gp 13fdc5ced1SMichael Walshimport gen_robot_keyword as grk 14f880ac66SMichael Walshimport var_funcs as vf 15e635ddc0SGeorge Keishingfrom robot.libraries import DateTime 1620f38712SPatrick Williamsfrom robot.libraries.BuiltIn import BuiltIn 1720f38712SPatrick Williams 18f880ac66SMichael Walshtry: 19f880ac66SMichael Walsh from robot.utils import DotDict 20f880ac66SMichael Walshexcept ImportError: 21f880ac66SMichael Walsh pass 2207958e19Sganesanbimport re 23fdc5ced1SMichael Walsh 246624ce5dSSushil Singh# The code base directory will be one level up from the directory containing this module. 256624ce5dSSushil Singhcode_base_dir_path = os.path.dirname(os.path.dirname(__file__)) + os.sep 266624ce5dSSushil Singh 276624ce5dSSushil Singh 286624ce5dSSushil Singhdef get_code_base_dir_path(): 296624ce5dSSushil Singh r""" 306624ce5dSSushil Singh Return the dir path of our code base. 316624ce5dSSushil Singh """ 326624ce5dSSushil Singh 336624ce5dSSushil Singh return code_base_dir_path 346624ce5dSSushil Singh 356624ce5dSSushil Singh 36fdc5ced1SMichael Walshdef set_power_policy_method(): 37fdc5ced1SMichael Walsh r""" 38fdc5ced1SMichael Walsh Set the global bmc_power_policy_method to either 'Old' or 'New'. 39fdc5ced1SMichael Walsh 40fdc5ced1SMichael Walsh The power policy data has moved from an 'org' location to an 'xyz' 41fdc5ced1SMichael Walsh location. This keyword will determine whether the new method of getting 42fdc5ced1SMichael Walsh the power policy is valid and will set the global bmc_power_policy_method 43fdc5ced1SMichael Walsh variable accordingly. If power_policy_setup is already set (by a prior 44fdc5ced1SMichael Walsh call to this function), this keyword will simply return. 45fdc5ced1SMichael Walsh 46fdc5ced1SMichael Walsh If bmc_power_policy_method is "Old", this function will adjust the global 47fdc5ced1SMichael Walsh policy variables from data/variables.py: RESTORE_LAST_STATE, 48fdc5ced1SMichael Walsh ALWAYS_POWER_ON, ALWAYS_POWER_OFF. 49fdc5ced1SMichael Walsh """ 50fdc5ced1SMichael Walsh 51fdc5ced1SMichael Walsh # Retrieve global variables. 5220f38712SPatrick Williams power_policy_setup = int( 5320f38712SPatrick Williams BuiltIn().get_variable_value("${power_policy_setup}", default=0) 5420f38712SPatrick Williams ) 5520f38712SPatrick Williams bmc_power_policy_method = BuiltIn().get_variable_value( 5620f38712SPatrick Williams "${bmc_power_policy_method}", default=0 5720f38712SPatrick Williams ) 58fdc5ced1SMichael Walsh gp.dpvar(power_policy_setup) 59fdc5ced1SMichael Walsh 60fdc5ced1SMichael Walsh # If this function has already been run once, we need not continue. 61fdc5ced1SMichael Walsh if power_policy_setup: 62fdc5ced1SMichael Walsh return 63fdc5ced1SMichael Walsh 64fdc5ced1SMichael Walsh gp.dpvar(bmc_power_policy_method, 1) 65fdc5ced1SMichael Walsh 66fdc5ced1SMichael Walsh # The user has not set bmc_power_policy_method via a -v parm so we will 67fdc5ced1SMichael Walsh # determine what it should be. 68fdc5ced1SMichael Walsh if bmc_power_policy_method == "": 69fdc5ced1SMichael Walsh status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1) 7020f38712SPatrick Williams if status == "PASS": 7120f38712SPatrick Williams bmc_power_policy_method = "New" 72fdc5ced1SMichael Walsh else: 7320f38712SPatrick Williams bmc_power_policy_method = "Old" 74fdc5ced1SMichael Walsh 75fdc5ced1SMichael Walsh gp.qpvar(bmc_power_policy_method) 76fdc5ced1SMichael Walsh # For old style, we will rewrite these global variable settings to old 77fdc5ced1SMichael Walsh # values. 78fdc5ced1SMichael Walsh if bmc_power_policy_method == "Old": 7920f38712SPatrick Williams BuiltIn().set_global_variable( 8020f38712SPatrick Williams "${RESTORE_LAST_STATE}", "RESTORE_LAST_STATE" 8120f38712SPatrick Williams ) 8220f38712SPatrick Williams BuiltIn().set_global_variable("${ALWAYS_POWER_ON}", "ALWAYS_POWER_ON") 8320f38712SPatrick Williams BuiltIn().set_global_variable( 8420f38712SPatrick Williams "${ALWAYS_POWER_OFF}", "ALWAYS_POWER_OFF" 8520f38712SPatrick Williams ) 86fdc5ced1SMichael Walsh 87fdc5ced1SMichael Walsh # Set global variables to control subsequent calls to this function. 8820f38712SPatrick Williams BuiltIn().set_global_variable( 8920f38712SPatrick Williams "${bmc_power_policy_method}", bmc_power_policy_method 9020f38712SPatrick Williams ) 91fdc5ced1SMichael Walsh BuiltIn().set_global_variable("${power_policy_setup}", 1) 92fdc5ced1SMichael Walsh 93fdc5ced1SMichael Walsh 94fdc5ced1SMichael Walshdef translate_power_policy_value(policy): 95fdc5ced1SMichael Walsh r""" 96fdc5ced1SMichael Walsh Translate the policy value and return the result. 97fdc5ced1SMichael Walsh 98fdc5ced1SMichael Walsh Using old style functions, callers might call like this with a hard- 99fdc5ced1SMichael Walsh code value for policy: 100fdc5ced1SMichael Walsh 101efc3ff2bSGeorge Keishing Set BMC Power Policy ALWAYS_POWER_OFF 102fdc5ced1SMichael Walsh 103fdc5ced1SMichael Walsh This function will get the value of the corresponding global variable (if 104fdc5ced1SMichael Walsh it exists) and return it. 105fdc5ced1SMichael Walsh 106fdc5ced1SMichael Walsh This will allow the old style call to still work on systems using the new 107fdc5ced1SMichael Walsh method of storing the policy value. 108fdc5ced1SMichael Walsh """ 109fdc5ced1SMichael Walsh 11020f38712SPatrick Williams valid_power_policy_vars = BuiltIn().get_variable_value( 11120f38712SPatrick Williams "${valid_power_policy_vars}" 11220f38712SPatrick Williams ) 113fdc5ced1SMichael Walsh 114fdc5ced1SMichael Walsh if policy not in valid_power_policy_vars: 115fdc5ced1SMichael Walsh return policy 116fdc5ced1SMichael Walsh 11720f38712SPatrick Williams status, ret_values = grk.run_key_u( 11820f38712SPatrick Williams "Get Variable Value ${" + policy + "}", quiet=1 11920f38712SPatrick Williams ) 120fdc5ced1SMichael Walsh return ret_values 121fdc5ced1SMichael Walsh 122f880ac66SMichael Walsh 123f880ac66SMichael Walshdef get_bmc_date_time(): 124f880ac66SMichael Walsh r""" 125f880ac66SMichael Walsh Get date/time info from BMC and return as a dictionary. 126f880ac66SMichael Walsh 127f880ac66SMichael Walsh Example of dictionary data returned by this keyword. 128f880ac66SMichael Walsh time_dict: 129f880ac66SMichael Walsh [local_time]: Fri 2017-11-03 152756 UTC 130f880ac66SMichael Walsh [local_time_seconds]: 1509740876 131f880ac66SMichael Walsh [universal_time]: Fri 2017-11-03 152756 UTC 132f880ac66SMichael Walsh [universal_time_seconds]: 1509740876 133f880ac66SMichael Walsh [rtc_time]: Fri 2016-05-20 163403 134f880ac66SMichael Walsh [rtc_time_seconds]: 1463780043 135f880ac66SMichael Walsh [time_zone]: n/a (UTC, +0000) 136f880ac66SMichael Walsh [network_time_on]: yes 137f880ac66SMichael Walsh [ntp_synchronized]: no 138f880ac66SMichael Walsh [rtc_in_local_tz]: no 139f880ac66SMichael Walsh """ 140f880ac66SMichael Walsh 14120f38712SPatrick Williams out_buf, stderr, rc = bsu.bmc_execute_command("timedatectl") 142f880ac66SMichael Walsh # Example of output returned by call to timedatectl: 143f880ac66SMichael Walsh # Local time: Fri 2017-11-03 15:27:56 UTC 144f880ac66SMichael Walsh # Universal time: Fri 2017-11-03 15:27:56 UTC 145f880ac66SMichael Walsh # RTC time: Fri 2016-05-20 16:34:03 146f880ac66SMichael Walsh # Time zone: n/a (UTC, +0000) 147f880ac66SMichael Walsh # Network time on: yes 148f880ac66SMichael Walsh # NTP synchronized: no 149f880ac66SMichael Walsh # RTC in local TZ: no 150f880ac66SMichael Walsh 151f880ac66SMichael Walsh # Convert the out_buf to a dictionary. 152f880ac66SMichael Walsh initial_time_dict = vf.key_value_outbuf_to_dict(out_buf) 153f880ac66SMichael Walsh 154f880ac66SMichael Walsh # For each "_time" entry in the dictionary, we will create a corresponding 155f880ac66SMichael Walsh # "_time_seconds" entry. We create a new dictionary so that the entries 156f880ac66SMichael Walsh # are kept in a nice order for printing. 157f880ac66SMichael Walsh try: 158f880ac66SMichael Walsh result_time_dict = collections.OrderedDict() 159f880ac66SMichael Walsh except AttributeError: 160f880ac66SMichael Walsh result_time_dict = DotDict() 161f880ac66SMichael Walsh 162f880ac66SMichael Walsh for key, value in initial_time_dict.items(): 163f880ac66SMichael Walsh result_time_dict[key] = value 164f880ac66SMichael Walsh if not key.endswith("_time"): 165f880ac66SMichael Walsh continue 16620f38712SPatrick Williams result_time_dict[key + "_seconds"] = int( 16720f38712SPatrick Williams DateTime.convert_date(value, result_format="epoch") 16820f38712SPatrick Williams ) 169f880ac66SMichael Walsh 170f880ac66SMichael Walsh return result_time_dict 171f880ac66SMichael Walsh 172193743ebSMichael Walsh 173193743ebSMichael Walshdef get_bmc_df(df_parm_string=""): 174193743ebSMichael Walsh r""" 175193743ebSMichael Walsh Get df report from BMC and return as a report "object". 176193743ebSMichael Walsh 177193743ebSMichael Walsh A df report object is a list where each entry is a dictionary whose keys 178193743ebSMichael Walsh are the field names from the first entry in report_list. 179193743ebSMichael Walsh 180193743ebSMichael Walsh Example df report object: 181193743ebSMichael Walsh 182193743ebSMichael Walsh df_report: 183193743ebSMichael Walsh df_report[0]: 184193743ebSMichael Walsh [filesystem]: dev 185193743ebSMichael Walsh [1k-blocks]: 247120 186193743ebSMichael Walsh [used]: 0 187193743ebSMichael Walsh [available]: 247120 188193743ebSMichael Walsh [use%]: 0% 189193743ebSMichael Walsh [mounted]: /dev 190193743ebSMichael Walsh df_report[1]: 191193743ebSMichael Walsh [filesystem]: dev 192193743ebSMichael Walsh [1k-blocks]: 247120 193193743ebSMichael Walsh [used]: 0 194193743ebSMichael Walsh [available]: 247120 195193743ebSMichael Walsh [use%]: 0% 196193743ebSMichael Walsh [mounted]: /dev 197193743ebSMichael Walsh 198193743ebSMichael Walsh . Description of argument(s): 199193743ebSMichael Walsh df_parm_string A string containing valid df command parms (e.g. 200193743ebSMichael Walsh "-h /var"). 201193743ebSMichael Walsh """ 202193743ebSMichael Walsh 203193743ebSMichael Walsh out_buf, stderr, rc = bsu.bmc_execute_command("df " + df_parm_string) 204193743ebSMichael Walsh return vf.outbuf_to_report(out_buf) 2056f407b9aSGeorge Keishing 2066f407b9aSGeorge Keishing 2076f407b9aSGeorge Keishingdef get_sbe(): 2086f407b9aSGeorge Keishing r""" 2096f407b9aSGeorge Keishing Return CFAM value which contains such things as SBE side bit. 2106f407b9aSGeorge Keishing """ 2116f407b9aSGeorge Keishing 2126f407b9aSGeorge Keishing cmd_buf = "pdbg -d p9w -p0 getcfam 0x2808 | sed -re 's/.* = //g'" 2136f407b9aSGeorge Keishing out_buf, stderr, rc = bsu.bmc_execute_command(cmd_buf) 2146f407b9aSGeorge Keishing 2156f407b9aSGeorge Keishing return int(out_buf, 16) 2166f407b9aSGeorge Keishing 217bf724770SGeorge Keishing 218096cd565SGunnar Millsdef compare_mac_address(sys_mac_addr, user_mac_addr): 219bf724770SGeorge Keishing r""" 220bf724770SGeorge Keishing Return 1 if the MAC value matched, otherwise 0. 221bf724770SGeorge Keishing 222bf724770SGeorge Keishing . Description of argument(s): 223bf724770SGeorge Keishing sys_mac_addr A valid system MAC string (e.g. "70:e2:84:14:2a:08") 224bf724770SGeorge Keishing user_mac_addr A user provided MAC string (e.g. "70:e2:84:14:2a:08") 225bf724770SGeorge Keishing """ 226bf724770SGeorge Keishing 227bf724770SGeorge Keishing index = 0 228bf724770SGeorge Keishing # Example: ['70', 'e2', '84', '14', '2a', '08'] 229bf724770SGeorge Keishing mac_list = user_mac_addr.split(":") 230bf724770SGeorge Keishing for item in sys_mac_addr.split(":"): 231bf724770SGeorge Keishing if int(item, 16) == int(mac_list[index], 16): 232bf724770SGeorge Keishing index = index + 1 233bf724770SGeorge Keishing continue 234bf724770SGeorge Keishing return 0 235bf724770SGeorge Keishing 236bf724770SGeorge Keishing return 1 2377847ece3SMichael Walsh 2387847ece3SMichael Walsh 2397847ece3SMichael Walshdef get_os_ethtool(interface_name): 2407847ece3SMichael Walsh r""" 2417847ece3SMichael Walsh Get OS 'ethtool' output for the given interface_name and return it as a 2427847ece3SMichael Walsh dictionary. 2437847ece3SMichael Walsh 2447847ece3SMichael Walsh Settings for enP52p1s0f0: 2457847ece3SMichael Walsh Supported ports: [ TP ] 2467847ece3SMichael Walsh Supported link modes: 10baseT/Half 10baseT/Full 2477847ece3SMichael Walsh 100baseT/Half 100baseT/Full 2487847ece3SMichael Walsh 1000baseT/Half 1000baseT/Full 2497847ece3SMichael Walsh Supported pause frame use: No 2507847ece3SMichael Walsh Supports auto-negotiation: Yes 2517847ece3SMichael Walsh Supported FEC modes: Not reported 2527847ece3SMichael Walsh Advertised link modes: 10baseT/Half 10baseT/Full 2537847ece3SMichael Walsh 100baseT/Half 100baseT/Full 2547847ece3SMichael Walsh 1000baseT/Half 1000baseT/Full 2557847ece3SMichael Walsh Advertised pause frame use: Symmetric 2567847ece3SMichael Walsh Advertised auto-negotiation: Yes 2577847ece3SMichael Walsh Advertised FEC modes: Not reported 2587847ece3SMichael Walsh Speed: Unknown! 2597847ece3SMichael Walsh Duplex: Unknown! (255) 2607847ece3SMichael Walsh Port: Twisted Pair 2617847ece3SMichael Walsh PHYAD: 1 2627847ece3SMichael Walsh Transceiver: internal 2637847ece3SMichael Walsh Auto-negotiation: on 2647847ece3SMichael Walsh MDI-X: Unknown 2657847ece3SMichael Walsh Supports Wake-on: g 2667847ece3SMichael Walsh Wake-on: g 2677847ece3SMichael Walsh Current message level: 0x000000ff (255) 2687847ece3SMichael Walsh drv probe link timer ifdown ifup rx_err tx_err 2697847ece3SMichael Walsh Link detected: no 2707847ece3SMichael Walsh 2717847ece3SMichael Walsh Given that data, this function will return the following dictionary. 2727847ece3SMichael Walsh 2737847ece3SMichael Walsh ethtool_dict: 2747847ece3SMichael Walsh [supported_ports]: [ TP ] 2757847ece3SMichael Walsh [supported_link_modes]: 2767847ece3SMichael Walsh [supported_link_modes][0]: 10baseT/Half 10baseT/Full 2777847ece3SMichael Walsh [supported_link_modes][1]: 100baseT/Half 100baseT/Full 2787847ece3SMichael Walsh [supported_link_modes][2]: 1000baseT/Half 1000baseT/Full 2797847ece3SMichael Walsh [supported_pause_frame_use]: No 2807847ece3SMichael Walsh [supports_auto-negotiation]: Yes 2817847ece3SMichael Walsh [supported_fec_modes]: Not reported 2827847ece3SMichael Walsh [advertised_link_modes]: 2837847ece3SMichael Walsh [advertised_link_modes][0]: 10baseT/Half 10baseT/Full 2847847ece3SMichael Walsh [advertised_link_modes][1]: 100baseT/Half 100baseT/Full 2857847ece3SMichael Walsh [advertised_link_modes][2]: 1000baseT/Half 1000baseT/Full 2867847ece3SMichael Walsh [advertised_pause_frame_use]: Symmetric 2877847ece3SMichael Walsh [advertised_auto-negotiation]: Yes 2887847ece3SMichael Walsh [advertised_fec_modes]: Not reported 2897847ece3SMichael Walsh [speed]: Unknown! 2907847ece3SMichael Walsh [duplex]: Unknown! (255) 2917847ece3SMichael Walsh [port]: Twisted Pair 2927847ece3SMichael Walsh [phyad]: 1 2937847ece3SMichael Walsh [transceiver]: internal 2947847ece3SMichael Walsh [auto-negotiation]: on 2957847ece3SMichael Walsh [mdi-x]: Unknown 2967847ece3SMichael Walsh [supports_wake-on]: g 2977847ece3SMichael Walsh [wake-on]: g 2987847ece3SMichael Walsh [current_message_level]: 0x000000ff (255) 2997847ece3SMichael Walsh [drv_probe_link_timer_ifdown_ifup_rx_err_tx_err]:<blank> 3007847ece3SMichael Walsh [link_detected]: no 3017847ece3SMichael Walsh """ 3027847ece3SMichael Walsh 3037847ece3SMichael Walsh # Using sed and tail to massage the data a bit before running 3047847ece3SMichael Walsh # key_value_outbuf_to_dict. 30520f38712SPatrick Williams cmd_buf = ( 30620f38712SPatrick Williams "ethtool " 30720f38712SPatrick Williams + interface_name 30820f38712SPatrick Williams + " | sed -re 's/(.* link modes:)(.*)/\\1\\n\\2/g' | tail -n +2" 30920f38712SPatrick Williams ) 3107847ece3SMichael Walsh stdout, stderr, rc = bsu.os_execute_command(cmd_buf) 3117847ece3SMichael Walsh result = vf.key_value_outbuf_to_dict(stdout, process_indent=1, strip=" \t") 3127847ece3SMichael Walsh 3137847ece3SMichael Walsh return result 3147557510aSMichael Walsh 3157557510aSMichael Walsh 3167557510aSMichael Walshdef to_json_ordered(json_str): 3177557510aSMichael Walsh r""" 3187557510aSMichael Walsh Parse the JSON string data and return an ordered JSON dictionary object. 3197557510aSMichael Walsh 3207557510aSMichael Walsh Description of argument(s): 3217557510aSMichael Walsh json_str The string containing the JSON data. 3227557510aSMichael Walsh """ 3237557510aSMichael Walsh 32436efbc04SGeorge Keishing try: 3257557510aSMichael Walsh return json.loads(json_str, object_pairs_hook=DotDict) 32636efbc04SGeorge Keishing except TypeError: 32736efbc04SGeorge Keishing return json.loads(json_str.decode("utf-8"), object_pairs_hook=DotDict) 32835d78f25SMichael Walsh 32935d78f25SMichael Walsh 33035d78f25SMichael Walshdef get_bmc_release_info(): 33135d78f25SMichael Walsh r""" 33235d78f25SMichael Walsh Get release info from the BMC and return as a dictionary. 33335d78f25SMichael Walsh 33435d78f25SMichael Walsh Example: 33535d78f25SMichael Walsh 33635d78f25SMichael Walsh ${release_info}= Get BMC Release Info 33735d78f25SMichael Walsh Rprint Vars release_info 33835d78f25SMichael Walsh 33935d78f25SMichael Walsh Output: 34035d78f25SMichael Walsh 34135d78f25SMichael Walsh release_info: 34235d78f25SMichael Walsh [id]: openbmc-phosphor 34335d78f25SMichael Walsh [name]: Phosphor OpenBMC (Phosphor OpenBMC Project Reference... 34435d78f25SMichael Walsh [version]: 2.8.0-dev 34535d78f25SMichael Walsh [version_id]: 2.8.0-dev-1083-g8954c3505 34635d78f25SMichael Walsh [pretty_name]: Phosphor OpenBMC (Phosphor OpenBMC Project Reference... 34735d78f25SMichael Walsh [build_id]: 2.8.0-dev 34835d78f25SMichael Walsh [openbmc_target_machine]: witherspoon 34935d78f25SMichael Walsh """ 35035d78f25SMichael Walsh 35120f38712SPatrick Williams out_buf, stderr, rc = bsu.bmc_execute_command("cat /etc/os-release") 35235d78f25SMichael Walsh return vf.key_value_outbuf_to_dict(out_buf, delim="=", strip='"') 35335d78f25SMichael Walsh 35435d78f25SMichael Walsh 35535d78f25SMichael Walshdef get_os_release_info(): 35635d78f25SMichael Walsh r""" 35735d78f25SMichael Walsh Get release info from the OS and return as a dictionary. 35835d78f25SMichael Walsh 35935d78f25SMichael Walsh Example: 36035d78f25SMichael Walsh 36135d78f25SMichael Walsh ${release_info}= Get OS Release Info 36235d78f25SMichael Walsh Rprint Vars release_info 36335d78f25SMichael Walsh 36435d78f25SMichael Walsh Output: 36535d78f25SMichael Walsh release_info: 36635d78f25SMichael Walsh [name]: Red Hat Enterprise Linux Server 36735d78f25SMichael Walsh [version]: 7.6 (Maipo) 36835d78f25SMichael Walsh [id]: rhel 36935d78f25SMichael Walsh [id_like]: fedora 37035d78f25SMichael Walsh [variant]: Server 37135d78f25SMichael Walsh [variant_id]: server 37235d78f25SMichael Walsh [version_id]: 7.6 37335d78f25SMichael Walsh [pretty_name]: Red Hat Enterprise Linux Server 7.6 (Maipo) 37435d78f25SMichael Walsh [ansi_color]: 0;31 37535d78f25SMichael Walsh [cpe_name]: cpe:/o:redhat:enterprise_linux:7.6:GA:server 37635d78f25SMichael Walsh [home_url]: https://www.redhat.com/ 37735d78f25SMichael Walsh [bug_report_url]: https://bugzilla.redhat.com/ 37835d78f25SMichael Walsh [redhat_bugzilla_product]: Red Hat Enterprise Linux 7 37935d78f25SMichael Walsh [redhat_bugzilla_product_version]: 7.6 38035d78f25SMichael Walsh [redhat_support_product]: Red Hat Enterprise Linux 38135d78f25SMichael Walsh [redhat_support_product_version]: 7.6 38235d78f25SMichael Walsh """ 38335d78f25SMichael Walsh 38420f38712SPatrick Williams out_buf, stderr, rc = bsu.os_execute_command("cat /etc/os-release") 38535d78f25SMichael Walsh return vf.key_value_outbuf_to_dict(out_buf, delim="=", strip='"') 3869617ebddSSridevi Ramesh 3879617ebddSSridevi Ramesh 3889617ebddSSridevi Rameshdef pdbg(option_string, **bsu_options): 3899617ebddSSridevi Ramesh r""" 3909617ebddSSridevi Ramesh Run pdbg on the BMC with the caller's option string and return the output. 3919617ebddSSridevi Ramesh 3929617ebddSSridevi Ramesh Description of argument(s): 3939617ebddSSridevi Ramesh option_string A string of options which are to be processed by the pdbg command. 3949617ebddSSridevi Ramesh bsu_options Options to be passed directly to bmc_execute_command. See its prolog for 3959617ebddSSridevi Ramesh details. 3969617ebddSSridevi Ramesh """ 3979617ebddSSridevi Ramesh 3989617ebddSSridevi Ramesh # Default print_out to 1. 39920f38712SPatrick Williams if "print_out" not in bsu_options: 40020f38712SPatrick Williams bsu_options["print_out"] = 1 4019617ebddSSridevi Ramesh 40220f38712SPatrick Williams stdout, stderr, rc = bsu.bmc_execute_command( 40320f38712SPatrick Williams "pdbg " + option_string, **bsu_options 40420f38712SPatrick Williams ) 4059617ebddSSridevi Ramesh return stdout 406eb5a162eSSridevi Ramesh 407eb5a162eSSridevi Ramesh 408eb5a162eSSridevi Rameshdef ecmd(option_string, **bsu_options): 409eb5a162eSSridevi Ramesh r""" 410eb5a162eSSridevi Ramesh Run ecmd command on the BMC with the caller's option string and return the output. 411eb5a162eSSridevi Ramesh 412eb5a162eSSridevi Ramesh Description of argument(s): 413eb5a162eSSridevi Ramesh option_string A string of options which are to be executed on BMC. 414eb5a162eSSridevi Ramesh (e.g. getscom pu 20010a40 -all, 415eb5a162eSSridevi Ramesh putscom pu 20010a40 4000000000000000 -p0). 416eb5a162eSSridevi Ramesh bsu_options Options to be passed directly to bmc_execute_command. See its prolog for 417eb5a162eSSridevi Ramesh details. 418eb5a162eSSridevi Ramesh """ 419eb5a162eSSridevi Ramesh 420eb5a162eSSridevi Ramesh # Default print_out to 1. 42120f38712SPatrick Williams if "print_out" not in bsu_options: 42220f38712SPatrick Williams bsu_options["print_out"] = 1 423eb5a162eSSridevi Ramesh 424eb5a162eSSridevi Ramesh stdout, stderr, rc = bsu.bmc_execute_command(option_string, **bsu_options) 425eb5a162eSSridevi Ramesh return stdout 4260a8c8787Schithrag 4270a8c8787Schithrag 4280a8c8787Schithragdef split_string_with_index(stri, n): 4290a8c8787Schithrag r""" 4300a8c8787Schithrag To split every n characters and forms an element for every nth index 4310a8c8787Schithrag 4320a8c8787Schithrag Example : Given ${stri} = "abcdef", then the function call, 4330a8c8787Schithrag ${data}= Split List With Index ${stri} 2 4340a8c8787Schithrag then, result will be data = ['ab', 'cd', 'ef'] 4350a8c8787Schithrag """ 4360a8c8787Schithrag 4370a8c8787Schithrag n = int(n) 4380a8c8787Schithrag data = [stri[index : index + n] for index in range(0, len(stri), n)] 4390a8c8787Schithrag return data 4400a8c8787Schithrag 4410a8c8787Schithrag 4420a8c8787Schithragdef remove_whitespace(instring): 4430a8c8787Schithrag r""" 4440a8c8787Schithrag Removes the white spaces around the string 4450a8c8787Schithrag 4460a8c8787Schithrag Example: instring = " xxx ", then returns instring = "xxx" 4470a8c8787Schithrag """ 4480a8c8787Schithrag 4490a8c8787Schithrag return instring.strip() 4500a8c8787Schithrag 4510a8c8787Schithrag 4520a8c8787Schithragdef zfill_data(data, num): 4530a8c8787Schithrag r""" 454c7cc02b0SGeorge Keishing zfill() method adds zeros (0) at the beginning of the string, until it 455c7cc02b0SGeorge Keishing reaches the specified length. 4560a8c8787Schithrag 4570a8c8787Schithrag Usage : ${anystr}= Zfill Data ${data} num 4580a8c8787Schithrag 4590a8c8787Schithrag Example : Binary of one Byte has 8 bits - xxxx xxxx 4600a8c8787Schithrag 4610a8c8787Schithrag Consider ${binary} has only 3 bits after converting from Hexadecimal/decimal to Binary 4620a8c8787Schithrag Say ${binary} = 110 then, 4630a8c8787Schithrag ${binary}= Zfill Data ${binary} 8 4640a8c8787Schithrag Now ${binary} will be 0000 0110 4650a8c8787Schithrag """ 4660a8c8787Schithrag 4670a8c8787Schithrag return data.zfill(int(num)) 468c288affeSganesanb 469c288affeSganesanb 470c288affeSganesanbdef get_subsequent_value_from_list(list, value): 471c288affeSganesanb r""" 472c288affeSganesanb returns first index of the element occurrence. 473c288affeSganesanb """ 474c288affeSganesanb 475c288affeSganesanb index = [list.index(i) for i in list if value in i] 476c288affeSganesanb return index 477c288affeSganesanb 478c288affeSganesanb 479c288affeSganesanbdef return_decoded_string(input): 480c288affeSganesanb r""" 481c288affeSganesanb returns decoded string of encoded byte. 482c288affeSganesanb """ 483c288affeSganesanb 48420f38712SPatrick Williams encoded_string = input.encode("ascii", "ignore") 485c288affeSganesanb decoded_string = encoded_string.decode() 486c288affeSganesanb return decoded_string 48707958e19Sganesanb 48807958e19Sganesanb 48907958e19Sganesanbdef remove_unicode_from_uri(uri): 49007958e19Sganesanb r""" 49107958e19Sganesanb returns dbus uri without unicode in prefix 49207958e19Sganesanb """ 49307958e19Sganesanb 49407958e19Sganesanb return re.sub("`-|\\|-", "", uri) 495bd187d2bSganesanb 496bd187d2bSganesanb 497bd187d2bSganesanbdef get_bmc_major_minor_version(version): 498bd187d2bSganesanb r""" 499bd187d2bSganesanb returns major version and minor version 500bd187d2bSganesanb from cat /etc/os-release command. 501bd187d2bSganesanb For example, 502bd187d2bSganesanb xyz23.01 --> [23, 01] 503bd187d2bSganesanb xyz.0-112 --> [0, 112] 504bd187d2bSganesanb ZERzzYY-23.04-1-xx3 --> [23, 04, 1, 3] 505bd187d2bSganesanb """ 506bd187d2bSganesanb 507bd187d2bSganesanb return re.findall(r"\d+", re.sub("[A-Z]|[a-z]", "", version)) 508*5502e3edSganesanb 509*5502e3edSganesanb 510*5502e3edSganesanbdef convert_name_into_bytes_with_prefix(name): 511*5502e3edSganesanb r""" 512*5502e3edSganesanb Convert name into bytes with prefix 0x 513*5502e3edSganesanb """ 514*5502e3edSganesanb 515*5502e3edSganesanb bytes_list = [] 516*5502e3edSganesanb 517*5502e3edSganesanb for letter in name: 518*5502e3edSganesanb bytes_list.append(hex(ord(letter))) 519*5502e3edSganesanb 520*5502e3edSganesanb return bytes_list 521*5502e3edSganesanb 522*5502e3edSganesanb 523*5502e3edSganesanbdef convert_name_into_bytes_without_prefix(name): 524*5502e3edSganesanb r""" 525*5502e3edSganesanb Convert name into bytes 526*5502e3edSganesanb """ 527*5502e3edSganesanb 528*5502e3edSganesanb tmp_lst = [] 529*5502e3edSganesanb 530*5502e3edSganesanb for letter in name: 531*5502e3edSganesanb value = convert_to_hex_value_without_prefix(letter) 532*5502e3edSganesanb tmp_lst.append(value) 533*5502e3edSganesanb 534*5502e3edSganesanb return tmp_lst 535*5502e3edSganesanb 536*5502e3edSganesanb 537*5502e3edSganesanbdef convert_to_hex_value_without_prefix(letter): 538*5502e3edSganesanb r""" 539*5502e3edSganesanb Convert into hex 540*5502e3edSganesanb """ 541*5502e3edSganesanb 542*5502e3edSganesanb value = hex(ord(letter)) 543*5502e3edSganesanb if value[:2] == "0x": 544*5502e3edSganesanb value = value[2:] 545*5502e3edSganesanb 546*5502e3edSganesanb return value 547*5502e3edSganesanb 548*5502e3edSganesanb 549*5502e3edSganesanbdef convert_prefix_hex_list_to_non_prefix_hex_list(list): 550*5502e3edSganesanb r""" 551*5502e3edSganesanb Convert into list of hex with prefix to list of hex without prefix. 552*5502e3edSganesanb """ 553*5502e3edSganesanb 554*5502e3edSganesanb tmp_list = [] 555*5502e3edSganesanb 556*5502e3edSganesanb for value in list: 557*5502e3edSganesanb if value[:2] == "0x": 558*5502e3edSganesanb tmp_list.append(value[2:]) 559*5502e3edSganesanb 560*5502e3edSganesanb return tmp_list 561