|
| 1 | +""" This module provides interface to interact with the BMC of the DUT via platform API remotely """ |
| 2 | +import ast |
| 3 | +import json |
| 4 | +import logging |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +def bmc_pmon_api(conn, name, args=None): |
| 10 | + if args is None: |
| 11 | + args = [] |
| 12 | + conn.request('POST', '/platform/chassis/bmc/{}'.format(name), json.dumps({'args': args})) |
| 13 | + resp = conn.getresponse() |
| 14 | + res = json.loads(resp.read())['res'] |
| 15 | + logger.info('Executing chassis API: "{}", arguments: "{}", result: "{}"'.format(name, args, res)) |
| 16 | + return res |
| 17 | + |
| 18 | + |
| 19 | +def bmc_host_api(duthost, api_name, *args): |
| 20 | + bmc_instance = 'sudo python -c "import sonic_platform; \ |
| 21 | + bmc = sonic_platform.platform.Platform().get_chassis().get_bmc(); \ |
| 22 | + print(bmc.{})"' |
| 23 | + res = duthost.shell(bmc_instance.format(api_name + str(args)))['stdout'] |
| 24 | + try: |
| 25 | + return ast.literal_eval(res) |
| 26 | + except (ValueError, SyntaxError): |
| 27 | + return res.strip() |
| 28 | + |
| 29 | + |
| 30 | +def get_name(conn): |
| 31 | + return bmc_pmon_api(conn, 'get_name') |
| 32 | + |
| 33 | + |
| 34 | +def get_presence(conn): |
| 35 | + return bmc_pmon_api(conn, 'get_presence') |
| 36 | + |
| 37 | + |
| 38 | +def get_model(duthost): |
| 39 | + return bmc_host_api(duthost, 'get_model') |
| 40 | + |
| 41 | + |
| 42 | +def get_serial(duthost): |
| 43 | + return bmc_host_api(duthost, 'get_serial') |
| 44 | + |
| 45 | + |
| 46 | +def get_revision(conn): |
| 47 | + return bmc_pmon_api(conn, 'get_revision') |
| 48 | + |
| 49 | + |
| 50 | +def get_status(conn): |
| 51 | + return bmc_pmon_api(conn, 'get_status') |
| 52 | + |
| 53 | + |
| 54 | +def is_replaceable(conn): |
| 55 | + return bmc_pmon_api(conn, 'is_replaceable') |
| 56 | + |
| 57 | + |
| 58 | +def get_eeprom(duthost): |
| 59 | + return bmc_host_api(duthost, 'get_eeprom') |
| 60 | + |
| 61 | + |
| 62 | +def get_version(duthost): |
| 63 | + return bmc_host_api(duthost, 'get_version') |
| 64 | + |
| 65 | + |
| 66 | +def reset_root_password(duthost): |
| 67 | + return bmc_host_api(duthost, 'reset_root_password') |
| 68 | + |
| 69 | + |
| 70 | +def trigger_bmc_debug_log_dump(duthost): |
| 71 | + return bmc_host_api(duthost, 'trigger_bmc_debug_log_dump') |
| 72 | + |
| 73 | + |
| 74 | +def get_bmc_debug_log_dump(duthost, task_id, filename, path): |
| 75 | + return bmc_host_api(duthost, 'get_bmc_debug_log_dump', task_id, filename, path) |
| 76 | + |
| 77 | + |
| 78 | +def update_firmware(duthost, fw_image): |
| 79 | + return bmc_host_api(duthost, 'update_firmware', fw_image) |
| 80 | + |
| 81 | + |
| 82 | +def request_bmc_reset(duthost): |
| 83 | + return bmc_host_api(duthost, '_request_bmc_reset') |
0 commit comments