|
3 | 3 | from tabulate import tabulate |
4 | 4 | from swsscommon.swsscommon import SonicV2Connector |
5 | 5 | from utilities_common.chassis import is_smartswitch |
| 6 | +from sonic_platform_base.module_base import ModuleBase |
6 | 7 |
|
7 | 8 | import utilities_common.cli as clicommon |
8 | 9 | from sonic_py_common import multi_asic |
@@ -58,18 +59,21 @@ def status(db, chassis_module_name): |
58 | 59 | continue |
59 | 60 |
|
60 | 61 | data_dict = state_db.get_all(state_db.STATE_DB, key) |
61 | | - desc = data_dict[CHASSIS_MODULE_INFO_DESC_FIELD] |
62 | | - slot = data_dict[CHASSIS_MODULE_INFO_SLOT_FIELD] |
63 | | - oper_status = data_dict[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD] |
64 | | - serial = data_dict[CHASSIS_MODULE_INFO_SERIAL_FIELD] |
65 | 62 |
|
| 63 | + # Use default values if any field is missing |
| 64 | + desc = data_dict.get(CHASSIS_MODULE_INFO_DESC_FIELD, 'N/A') |
| 65 | + slot = data_dict.get(CHASSIS_MODULE_INFO_SLOT_FIELD, 'N/A') |
| 66 | + oper_status = data_dict.get(CHASSIS_MODULE_INFO_OPERSTATUS_FIELD, ModuleBase.MODULE_STATUS_EMPTY) |
| 67 | + serial = data_dict.get(CHASSIS_MODULE_INFO_SERIAL_FIELD, 'N/A') |
| 68 | + |
| 69 | + # Determine admin_status |
66 | 70 | if is_smartswitch(): |
67 | 71 | admin_status = 'down' |
68 | 72 | else: |
69 | 73 | admin_status = 'up' |
70 | 74 | config_data = chassis_cfg_table.get(key_list[1]) |
71 | 75 | if config_data is not None: |
72 | | - admin_status = config_data.get(CHASSIS_MODULE_INFO_ADMINSTATUS_FIELD) |
| 76 | + admin_status = config_data.get(CHASSIS_MODULE_INFO_ADMINSTATUS_FIELD, admin_status) |
73 | 77 |
|
74 | 78 | table.append((key_list[1], desc, slot, oper_status, admin_status, serial)) |
75 | 79 |
|
@@ -100,13 +104,15 @@ def midplane_status(chassis_module_name): |
100 | 104 | table = [] |
101 | 105 | for key in natsorted(keys): |
102 | 106 | key_list = key.split('|') |
103 | | - if len(key_list) != 2: # error data in DB, log it and ignore |
| 107 | + if len(key_list) != 2: |
104 | 108 | print('Warn: Invalid Key {} in {} table'.format(key, CHASSIS_MIDPLANE_INFO_TABLE)) |
105 | 109 | continue |
106 | 110 |
|
107 | 111 | data_dict = state_db.get_all(state_db.STATE_DB, key) |
108 | | - ip = data_dict[CHASSIS_MIDPLANE_INFO_IP_FIELD] |
109 | | - access = data_dict[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD] |
| 112 | + |
| 113 | + # Defensive access with fallback defaults |
| 114 | + ip = data_dict.get(CHASSIS_MIDPLANE_INFO_IP_FIELD, 'N/A') |
| 115 | + access = data_dict.get(CHASSIS_MIDPLANE_INFO_ACCESS_FIELD, 'Unknown') |
110 | 116 |
|
111 | 117 | table.append((key_list[1], ip, access)) |
112 | 118 |
|
|
0 commit comments