From 9395ebdedf92891293820183f3d6fc514614d871 Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Thu, 19 Aug 2021 10:12:35 -0700 Subject: [PATCH] [show][platform] Revise chassis info fallback to only fall back on pmon crash (#1751) What I did Changed the behavior of the get_chassis_info() function which is used by show version and show platform summary to report the serial / model / revision of the switch to only attempt to call the platform API if pmon completely failed to provision STATE_DB with CHASSIS_INFO How I did it Altered get_chassis_info() according to the above behavior. See changelog for details. How to verify it Verify show platform summary and show version correctly report chassis info. Delete the CHASSIS_INFO table from STATE_DB Verify show platform summary and show version still correctly report the info. --- show/platform.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/show/platform.py b/show/platform.py index 94d1d8bd63ef..e70b4e92e108 100644 --- a/show/platform.py +++ b/show/platform.py @@ -12,23 +12,27 @@ def get_chassis_info(): """ - Attempts to get the chassis info via STATE_DB and falls back to direct Platform API calls. + Attempts to retrieve chassis information from CHASSIS_INFO table in STATE_DB if this table does + not exist then we assume pmon has crashed and will attempt to call the platform API directly. If this + call fails we simply return N/A. """ - chassis_info = device_info.get_chassis_info() - required_keys = ['serial', 'model', 'revision'] - failed_vals = ['', 'N/A'] - platform_chassis = None + keys = ["serial", "model", "revision"] - for k in required_keys: - if chassis_info.get(k, '') in failed_vals: - if platform_chassis is None: + def try_get(platform, attr, fallback): + try: + if platform["chassis"] is None: import sonic_platform - platform_chassis = sonic_platform.platform.Platform().get_chassis() - try: - chassis_info[k] = getattr(platform_chassis, "get_".format(k))() - except AttributeError: - chassis_info[k] = 'N/A' + platform["chassis"] = sonic_platform.platform.Platform().get_chassis() + return getattr(platform["chassis"], "get_{}".format(attr))() + except Exception: + return 'N/A' + + chassis_info = device_info.get_chassis_info() + + if all(v is None for k, v in chassis_info.items()): + platform_cache = {"chassis": None} + chassis_info = {k:try_get(platform_cache, k, "N/A") for k in keys} return chassis_info