From 6dfeee699bf85d74d0dfbbd02fc7697c7f1e8b11 Mon Sep 17 00:00:00 2001 From: Rajkumar-Marvell <54936542+rajkumar38@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:38:40 +0530 Subject: [PATCH] [sflow][db_migrator] Egress Sflow support (#3020) * [sflow][db_migrator] Egress Sflow support --- scripts/db_migrator.py | 43 +++++++++++++- .../appl_db/sflow_table_expected.json | 21 +++++++ .../appl_db/sflow_table_input.json | 17 ++++++ .../config_db/sflow_table_expected.json | 57 ++++++++++++++++++ .../config_db/sflow_table_input.json | 53 +++++++++++++++++ tests/db_migrator_test.py | 58 +++++++++++++++++++ 6 files changed, 247 insertions(+), 2 deletions(-) create mode 100644 tests/db_migrator_input/appl_db/sflow_table_expected.json create mode 100644 tests/db_migrator_input/appl_db/sflow_table_input.json create mode 100644 tests/db_migrator_input/config_db/sflow_table_expected.json create mode 100644 tests/db_migrator_input/config_db/sflow_table_input.json diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index f15f27bae8..a82143957e 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -50,7 +50,7 @@ def __init__(self, namespace, socket=None): none-zero values. build: sequentially increase within a minor version domain. """ - self.CURRENT_VERSION = 'version_4_0_4' + self.CURRENT_VERSION = 'version_4_0_5' self.TABLE_NAME = 'VERSIONS' self.TABLE_KEY = 'DATABASE' @@ -726,6 +726,35 @@ def migrate_config_db_flex_counter_delay_status(self): flex_counter['FLEX_COUNTER_DELAY_STATUS'] = 'true' self.configDB.mod_entry('FLEX_COUNTER_TABLE', obj, flex_counter) + def migrate_sflow_table(self): + """ + Migrate "SFLOW_TABLE" and "SFLOW_SESSION_TABLE" to update default sample_direction + """ + + sflow_tbl = self.configDB.get_table('SFLOW') + for k, v in sflow_tbl.items(): + if 'sample_direction' not in v: + v['sample_direction'] = 'rx' + self.configDB.set_entry('SFLOW', k, v) + + sflow_sess_tbl = self.configDB.get_table('SFLOW_SESSION') + for k, v in sflow_sess_tbl.items(): + if 'sample_direction' not in v: + v['sample_direction'] = 'rx' + self.configDB.set_entry('SFLOW_SESSION', k, v) + + sflow_table = self.appDB.get_table("SFLOW_TABLE") + for key, value in sflow_table.items(): + if 'sample_direction' not in value: + sflow_key = "SFLOW_TABLE:{}".format(key) + self.appDB.set(self.appDB.APPL_DB, sflow_key, 'sample_direction','rx') + + sflow_sess_table = self.appDB.get_table("SFLOW_SESSION_TABLE") + for key, value in sflow_sess_table.items(): + if 'sample_direction' not in value: + sflow_key = "SFLOW_SESSION_TABLE:{}".format(key) + self.appDB.set(self.appDB.APPL_DB, sflow_key, 'sample_direction','rx') + def version_unknown(self): """ version_unknown tracks all SONiC versions that doesn't have a version @@ -1059,9 +1088,19 @@ def version_4_0_3(self): def version_4_0_4(self): """ Version 4_0_4. - This is the latest version for master branch """ log.log_info('Handling version_4_0_4') + + self.migrate_sflow_table() + self.set_version('version_4_0_5') + return 'version_4_0_5' + + def version_4_0_5(self): + """ + Version 4_0_5. + This is the latest version for master branch + """ + log.log_info('Handling version_4_0_5') return None def get_version(self): diff --git a/tests/db_migrator_input/appl_db/sflow_table_expected.json b/tests/db_migrator_input/appl_db/sflow_table_expected.json new file mode 100644 index 0000000000..7a786ae087 --- /dev/null +++ b/tests/db_migrator_input/appl_db/sflow_table_expected.json @@ -0,0 +1,21 @@ +{ + "SFLOW_TABLE:global": { + "admin_state": "up", + "sample_direction": "rx" + }, + "SFLOW_SESSION_TABLE:Ethernet6": { + "admin_state": "up", + "sample_direction": "rx", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet7": { + "admin_state": "up", + "sample_direction": "rx", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet8": { + "admin_state": "up", + "sample_direction": "rx", + "sample_rate": "1000" + } +} diff --git a/tests/db_migrator_input/appl_db/sflow_table_input.json b/tests/db_migrator_input/appl_db/sflow_table_input.json new file mode 100644 index 0000000000..4fcf7be5e5 --- /dev/null +++ b/tests/db_migrator_input/appl_db/sflow_table_input.json @@ -0,0 +1,17 @@ +{ + "SFLOW_TABLE:global": { + "admin_state": "up" + }, + "SFLOW_SESSION_TABLE:Ethernet6": { + "admin_state": "up", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet7": { + "admin_state": "up", + "sample_rate": "1000" + }, + "SFLOW_SESSION_TABLE:Ethernet8": { + "admin_state": "up", + "sample_rate": "1000" + } +} diff --git a/tests/db_migrator_input/config_db/sflow_table_expected.json b/tests/db_migrator_input/config_db/sflow_table_expected.json new file mode 100644 index 0000000000..66b45b4bb3 --- /dev/null +++ b/tests/db_migrator_input/config_db/sflow_table_expected.json @@ -0,0 +1,57 @@ +{ + "VERSIONS|DATABASE": { + "VERSION": "version_4_0_5" + }, + "SFLOW|global": { + "admin_state": "up", + "sample_direction": "rx" + }, + "SFLOW_SESSION|Ethernet0": { + "admin_state": "up", + "sample_direction": "rx", + "sample_rate": "1000" + }, + "SFLOW_SESSION|Ethernet2": { + "admin_state": "down", + "sample_direction": "rx", + "sample_rate": "2000" + }, + "SFLOW_SESSION|Ethernet4": { + "admin_state": "up", + "sample_direction": "rx", + "sample_rate": "4000" + }, + "PORT|Ethernet0": { + "admin_status": "up", + "alias": "Ethernet1/1", + "description": "", + "index": "1", + "lanes": "77,78", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + }, + "PORT|Ethernet2": { + "admin_status": "up", + "alias": "Ethernet2/1", + "description": "", + "index": "2", + "lanes": "79,80", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + }, + "PORT|Ethernet4": { + "admin_status": "up", + "alias": "Ethernet4/1", + "description": "", + "index": "3", + "lanes": "81,82", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + } +} diff --git a/tests/db_migrator_input/config_db/sflow_table_input.json b/tests/db_migrator_input/config_db/sflow_table_input.json new file mode 100644 index 0000000000..4c14c26225 --- /dev/null +++ b/tests/db_migrator_input/config_db/sflow_table_input.json @@ -0,0 +1,53 @@ +{ + "VERSIONS|DATABASE": { + "VERSION": "version_4_0_2" + }, + "SFLOW|global": { + "admin_state": "up" + }, + "SFLOW_SESSION|Ethernet0": { + "admin_state": "up", + "sample_rate": "1000" + }, + "SFLOW_SESSION|Ethernet2": { + "admin_state": "down", + "sample_rate": "2000" + }, + "SFLOW_SESSION|Ethernet4": { + "admin_state": "up", + "sample_rate": "4000" + }, + "PORT|Ethernet0": { + "admin_status": "up", + "alias": "Ethernet1/1", + "description": "", + "index": "1", + "lanes": "77,78", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + }, + "PORT|Ethernet2": { + "admin_status": "up", + "alias": "Ethernet2/1", + "description": "", + "index": "2", + "lanes": "79,80", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + }, + "PORT|Ethernet4": { + "admin_status": "up", + "alias": "Ethernet4/1", + "description": "", + "index": "3", + "lanes": "81,82", + "mtu": "9100", + "pfc_asym": "off", + "speed": "100000", + "tpid": "0x8100" + } +} diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index e75f758947..014b645532 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -740,3 +740,61 @@ def test_fast_reboot_upgrade_to_4_0_3(self): advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_4_0_3') assert not self.check_config_db(dbmgtr.configDB, expected_db.cfgdb) assert dbmgtr.CURRENT_VERSION == expected_db.cfgdb.get_entry('VERSIONS', 'DATABASE')['VERSION'] + +class TestSflowSampleDirectionMigrator(object): + @classmethod + def setup_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "2" + + @classmethod + def teardown_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "0" + dbconnector.dedicated_dbs['CONFIG_DB'] = None + dbconnector.dedicated_dbs['APPL_DB'] = None + + def test_sflow_migrator(self): + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'sflow_table_input') + dbconnector.dedicated_dbs['APPL_DB'] = os.path.join(mock_db_path, 'appl_db', 'sflow_table_input') + import db_migrator + dbmgtr = db_migrator.DBMigrator(None) + dbmgtr.migrate() + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'sflow_table_expected') + dbconnector.dedicated_dbs['APPL_DB'] = os.path.join(mock_db_path, 'appl_db', 'sflow_table_expected') + expected_db = Db() + + # verify migrated config DB + resulting_table = dbmgtr.configDB.get_table('SFLOW') + expected_table = expected_db.cfgdb.get_table('SFLOW') + diff = DeepDiff(resulting_table, expected_table, ignore_order=True) + assert not diff + + resulting_table = dbmgtr.configDB.get_table('SFLOW_SESSION') + expected_table = expected_db.cfgdb.get_table('SFLOW_SESSION') + diff = DeepDiff(resulting_table, expected_table, ignore_order=True) + assert not diff + + # verify migrated appDB + expected_appl_db = SonicV2Connector(host='127.0.0.1') + expected_appl_db.connect(expected_appl_db.APPL_DB) + + + expected_keys = expected_appl_db.keys(expected_appl_db.APPL_DB, "SFLOW_TABLE:global") + expected_keys.sort() + resulting_keys = dbmgtr.appDB.keys(dbmgtr.appDB.APPL_DB, "SFLOW_TABLE:global") + resulting_keys.sort() + for key in expected_keys: + resulting_keys = dbmgtr.appDB.get_all(dbmgtr.appDB.APPL_DB, key) + expected_keys = expected_appl_db.get_all(expected_appl_db.APPL_DB, key) + diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True) + assert not diff + + expected_keys = expected_appl_db.keys(expected_appl_db.APPL_DB, "SFLOW_SESSION_TABLE:*") + expected_keys.sort() + resulting_keys = dbmgtr.appDB.keys(dbmgtr.appDB.APPL_DB, "SFLOW_SESSION_TABLE:*") + resulting_keys.sort() + assert expected_keys == resulting_keys + for key in expected_keys: + resulting_keys = dbmgtr.appDB.get_all(dbmgtr.appDB.APPL_DB, key) + expected_keys = expected_appl_db.get_all(expected_appl_db.APPL_DB, key) + diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True) + assert not diff