From b11a7522aaf806dabadbc0d8fdd32816c26a6682 Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Wed, 12 May 2021 02:22:26 +0000 Subject: [PATCH 1/6] [sonic-py-common] Add platform and chassis info methods to device_info --- .../sonic_py_common/device_info.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index f486113196fb..47c37a110537 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -7,6 +7,8 @@ import yaml from natsort import natsorted +from . import multi_asic + # TODO: Replace with swsscommon from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector @@ -32,6 +34,52 @@ FRONTEND_ASIC_SUB_ROLE = "FrontEnd" BACKEND_ASIC_SUB_ROLE = "BackEnd" +# Chassis STATE_DB keys +CHASSIS_INFO_TABLE = 'CHASSIS_INFO|chassis {}' +CHASSIS_INFO_CARD_NUM_FIELD = 'module_num' +CHASSIS_INFO_SERIAL_FIELD = 'serial' +CHASSIS_INFO_MODEL_FIELD = 'model' +CHASSIS_INFO_REV_FIELD = 'revision' + +# Get hardware information +def get_platform_info(): + """ + This function is used to get the HW info helper function + """ + + hw_info_dict = {} + + version_info = device_info.get_sonic_version_info() + + hw_info_dict['platform'] = device_info.get_platform() + hw_info_dict['hwsku'] = device_info.get_hwsku() + hw_info_dict['asic_type'] = version_info['asic_type'] + hw_info_dict['asic_count'] = multi_asic.get_num_asics() + + return hw_info_dict + + +def get_chassis_info(): + """ + This function is used to get the Chassis serial / model / rev number + """ + + chassis_info_dict = {} + + try: + # Init statedb connection + db = swsscommon.SonicV2Connector() + db.connect(db.STATE_DB) + table = CHASSIS_INFO_TABLE.format(1) + + chassis_info_dict['serial'] = db.get(db.STATE_DB, table, CHASSIS_INFO_SERIAL_FIELD) + chassis_info_dict['model'] = db.get(db.STATE_DB, table, CHASSIS_INFO_MODEL_FIELD) + chassis_info_dict['revision'] = db.get(db.STATE_DB, table, CHASSIS_INFO_REV_FIELD) + except: + pass + + return chassis_info_dict + def get_localhost_info(field): try: From 9aff9c7f261663ba522e6012ea8dce2bf2087ec6 Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Wed, 12 May 2021 03:20:21 +0000 Subject: [PATCH 2/6] Fix imports --- src/sonic-py-common/sonic_py_common/device_info.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 47c37a110537..0347bdddeb5f 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -7,8 +7,6 @@ import yaml from natsort import natsorted -from . import multi_asic - # TODO: Replace with swsscommon from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector @@ -46,15 +44,16 @@ def get_platform_info(): """ This function is used to get the HW info helper function """ + from .multi_asic import get_num_asics hw_info_dict = {} - version_info = device_info.get_sonic_version_info() + version_info = get_sonic_version_info() - hw_info_dict['platform'] = device_info.get_platform() - hw_info_dict['hwsku'] = device_info.get_hwsku() + hw_info_dict['platform'] = get_platform() + hw_info_dict['hwsku'] = get_hwsku() hw_info_dict['asic_type'] = version_info['asic_type'] - hw_info_dict['asic_count'] = multi_asic.get_num_asics() + hw_info_dict['asic_count'] = get_num_asics() return hw_info_dict From 19b03142cc933cf10fdffbe470b60257ac6a56db Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Wed, 12 May 2021 23:30:47 +0000 Subject: [PATCH 3/6] Add unit test for get_chassis_info --- src/sonic-py-common/tests/device_info_test.py | 10 +++++++++- src/sonic-py-common/tests/mock_swsssdk.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/sonic-py-common/tests/mock_swsssdk.py diff --git a/src/sonic-py-common/tests/device_info_test.py b/src/sonic-py-common/tests/device_info_test.py index f3b14b0a5f13..eee563c255ec 100644 --- a/src/sonic-py-common/tests/device_info_test.py +++ b/src/sonic-py-common/tests/device_info_test.py @@ -11,6 +11,7 @@ from sonic_py_common import device_info +from . import mock_swsssdk # TODO: Remove this if/else block once we no longer support Python 2 if sys.version_info.major == 3: @@ -49,7 +50,6 @@ 'onie_kernel_version': '4.10.11' } - class TestDeviceInfo(object): @classmethod def setup_class(cls): @@ -70,6 +70,14 @@ def test_get_platform(self): result = device_info.get_platform() assert result == "x86_64-mlnx_msn2700-r0" + def test_get_chassis_info(self): + with mock.patch("device_info.swsssdk", new=mock_swsssdk): + result = device_info.get_chassis_info() + truth = {"serial": self.TEST_SERIAL, + "model": self.TEST_MODEL, + "revision": self.TEST_REV} + assert result == truth + @classmethod def teardown_class(cls): print("TEARDOWN") diff --git a/src/sonic-py-common/tests/mock_swsssdk.py b/src/sonic-py-common/tests/mock_swsssdk.py new file mode 100644 index 000000000000..c88e945e09c5 --- /dev/null +++ b/src/sonic-py-common/tests/mock_swsssdk.py @@ -0,0 +1,13 @@ +class SonicV2Connector: + + def __init__(self): + self.STATE_DB = 'STATE_DB' + self.data = {"serial": self.TEST_SERIAL, + "model": self.TEST_MODEL, + "revision": self.TEST_REV} + + def connect(self, db): + pass + + def get(self, db, table, field): + self.data.get(field, "N/A") From a3053363082bf878ccde46271ca0e4ff85fa22e6 Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Thu, 13 May 2021 23:37:57 +0000 Subject: [PATCH 4/6] Fix unit test for device_info --- src/sonic-py-common/sonic_py_common/device_info.py | 2 +- src/sonic-py-common/tests/device_info_test.py | 10 +++++----- src/sonic-py-common/tests/mock_swsssdk.py | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 0347bdddeb5f..1a6855b85d89 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -67,7 +67,7 @@ def get_chassis_info(): try: # Init statedb connection - db = swsscommon.SonicV2Connector() + db = SonicV2Connector() db.connect(db.STATE_DB) table = CHASSIS_INFO_TABLE.format(1) diff --git a/src/sonic-py-common/tests/device_info_test.py b/src/sonic-py-common/tests/device_info_test.py index eee563c255ec..73ac278d46e6 100644 --- a/src/sonic-py-common/tests/device_info_test.py +++ b/src/sonic-py-common/tests/device_info_test.py @@ -11,7 +11,7 @@ from sonic_py_common import device_info -from . import mock_swsssdk +from .mock_swsssdk import SonicV2Connector # TODO: Remove this if/else block once we no longer support Python 2 if sys.version_info.major == 3: @@ -71,11 +71,11 @@ def test_get_platform(self): assert result == "x86_64-mlnx_msn2700-r0" def test_get_chassis_info(self): - with mock.patch("device_info.swsssdk", new=mock_swsssdk): + with mock.patch("sonic_py_common.device_info.SonicV2Connector", new=SonicV2Connector): result = device_info.get_chassis_info() - truth = {"serial": self.TEST_SERIAL, - "model": self.TEST_MODEL, - "revision": self.TEST_REV} + truth = {"serial": SonicV2Connector.TEST_SERIAL, + "model": SonicV2Connector.TEST_MODEL, + "revision": SonicV2Connector.TEST_REV} assert result == truth @classmethod diff --git a/src/sonic-py-common/tests/mock_swsssdk.py b/src/sonic-py-common/tests/mock_swsssdk.py index c88e945e09c5..df6affaf1853 100644 --- a/src/sonic-py-common/tests/mock_swsssdk.py +++ b/src/sonic-py-common/tests/mock_swsssdk.py @@ -1,4 +1,7 @@ class SonicV2Connector: + TEST_SERIAL = "MT1822K07815" + TEST_MODEL = "MSN2700-CS2FO" + TEST_REV = "A1" def __init__(self): self.STATE_DB = 'STATE_DB' @@ -10,4 +13,4 @@ def connect(self, db): pass def get(self, db, table, field): - self.data.get(field, "N/A") + return self.data.get(field, "N/A") From 9f92c746b66eb3f6ffbf1022b811b58fe1c4e9f0 Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Tue, 25 May 2021 14:05:41 +0000 Subject: [PATCH 5/6] Reorder functions and add spacing --- .../sonic_py_common/device_info.py | 81 ++++++++++--------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 1a6855b85d89..026421d0b22e 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -39,46 +39,6 @@ CHASSIS_INFO_MODEL_FIELD = 'model' CHASSIS_INFO_REV_FIELD = 'revision' -# Get hardware information -def get_platform_info(): - """ - This function is used to get the HW info helper function - """ - from .multi_asic import get_num_asics - - hw_info_dict = {} - - version_info = get_sonic_version_info() - - hw_info_dict['platform'] = get_platform() - hw_info_dict['hwsku'] = get_hwsku() - hw_info_dict['asic_type'] = version_info['asic_type'] - hw_info_dict['asic_count'] = get_num_asics() - - return hw_info_dict - - -def get_chassis_info(): - """ - This function is used to get the Chassis serial / model / rev number - """ - - chassis_info_dict = {} - - try: - # Init statedb connection - db = SonicV2Connector() - db.connect(db.STATE_DB) - table = CHASSIS_INFO_TABLE.format(1) - - chassis_info_dict['serial'] = db.get(db.STATE_DB, table, CHASSIS_INFO_SERIAL_FIELD) - chassis_info_dict['model'] = db.get(db.STATE_DB, table, CHASSIS_INFO_MODEL_FIELD) - chassis_info_dict['revision'] = db.get(db.STATE_DB, table, CHASSIS_INFO_REV_FIELD) - except: - pass - - return chassis_info_dict - def get_localhost_info(field): try: @@ -350,6 +310,47 @@ def get_sonic_version_file(): return SONIC_VERSION_YAML_PATH + +# Get hardware information +def get_platform_info(): + """ + This function is used to get the HW info helper function + """ + from .multi_asic import get_num_asics + + hw_info_dict = {} + + version_info = get_sonic_version_info() + + hw_info_dict['platform'] = get_platform() + hw_info_dict['hwsku'] = get_hwsku() + hw_info_dict['asic_type'] = version_info['asic_type'] + hw_info_dict['asic_count'] = get_num_asics() + + return hw_info_dict + + +def get_chassis_info(): + """ + This function is used to get the Chassis serial / model / rev number + """ + + chassis_info_dict = {} + + try: + # Init statedb connection + db = SonicV2Connector() + db.connect(db.STATE_DB) + table = CHASSIS_INFO_TABLE.format(1) + + chassis_info_dict['serial'] = db.get(db.STATE_DB, table, CHASSIS_INFO_SERIAL_FIELD) + chassis_info_dict['model'] = db.get(db.STATE_DB, table, CHASSIS_INFO_MODEL_FIELD) + chassis_info_dict['revision'] = db.get(db.STATE_DB, table, CHASSIS_INFO_REV_FIELD) + except: + pass + + return chassis_info_dict + # # Multi-NPU functionality # From 921038f78c8c957eb58ff07b3360011dd88e6c8f Mon Sep 17 00:00:00 2001 From: Alexander Allen Date: Tue, 25 May 2021 17:03:22 +0000 Subject: [PATCH 6/6] Fix try / except call in get_chassis_info() --- src/sonic-py-common/sonic_py_common/device_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sonic-py-common/sonic_py_common/device_info.py b/src/sonic-py-common/sonic_py_common/device_info.py index 026421d0b22e..2ab20518948e 100644 --- a/src/sonic-py-common/sonic_py_common/device_info.py +++ b/src/sonic-py-common/sonic_py_common/device_info.py @@ -346,7 +346,7 @@ def get_chassis_info(): chassis_info_dict['serial'] = db.get(db.STATE_DB, table, CHASSIS_INFO_SERIAL_FIELD) chassis_info_dict['model'] = db.get(db.STATE_DB, table, CHASSIS_INFO_MODEL_FIELD) chassis_info_dict['revision'] = db.get(db.STATE_DB, table, CHASSIS_INFO_REV_FIELD) - except: + except Exception: pass return chassis_info_dict