diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index ae7437389a..ddd1290767 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -44,7 +44,7 @@ def __init__(self, namespace, socket=None): none-zero values. build: sequentially increase within a minor version domain. """ - self.CURRENT_VERSION = 'version_3_0_5' + self.CURRENT_VERSION = 'version_3_0_6' self.TABLE_NAME = 'VERSIONS' self.TABLE_KEY = 'DATABASE' @@ -485,6 +485,23 @@ def migrate_qos_fieldval_reference_format(self): self.migrate_qos_db_fieldval_reference_remove(qos_table_list, self.configDB, self.configDB.CONFIG_DB, '|') return True + def migrate_port_qos_map_global(self): + """ + Generate dscp_to_tc_map for switch. + """ + asics_require_global_dscp_to_tc_map = ["broadcom"] + if self.asic_type not in asics_require_global_dscp_to_tc_map: + return + dscp_to_tc_map_table_names = self.configDB.get_keys('DSCP_TO_TC_MAP') + if len(dscp_to_tc_map_table_names) == 0: + return + + qos_maps = self.configDB.get_table('PORT_QOS_MAP') + if 'global' not in qos_maps.keys(): + # We are unlikely to have more than 1 DSCP_TO_TC_MAP in previous versions + self.configDB.set_entry('PORT_QOS_MAP', 'global', {"dscp_to_tc_map": dscp_to_tc_map_table_names[0]}) + log.log_info("Created entry for global DSCP_TO_TC_MAP {}".format(dscp_to_tc_map_table_names[0])) + def version_unknown(self): """ version_unknown tracks all SONiC versions that doesn't have a version @@ -681,9 +698,17 @@ def version_3_0_4(self): def version_3_0_5(self): """ - Current latest version. Nothing to do here. + Version 3_0_5 """ log.log_info('Handling version_3_0_5') + self.migrate_port_qos_map_global() + return 'version_3_0_6' + + def version_3_0_6(self): + """ + Current latest version. Nothing to do here. + """ + log.log_info('Handling version_3_0_6') return None def get_version(self): diff --git a/tests/db_migrator_input/config_db/qos_map_table_global_expected.json b/tests/db_migrator_input/config_db/qos_map_table_global_expected.json new file mode 100644 index 0000000000..c7fea9c013 --- /dev/null +++ b/tests/db_migrator_input/config_db/qos_map_table_global_expected.json @@ -0,0 +1,12 @@ +{ + "VERSIONS|DATABASE": { + "VERSION": "version_3_0_6" + }, + "DSCP_TO_TC_MAP|AZURE": { + "0": "0", + "1": "1" + }, + "PORT_QOS_MAP|global": { + "dscp_to_tc_map": "AZURE" + } +} diff --git a/tests/db_migrator_input/config_db/qos_map_table_global_input.json b/tests/db_migrator_input/config_db/qos_map_table_global_input.json new file mode 100644 index 0000000000..0ca51a4525 --- /dev/null +++ b/tests/db_migrator_input/config_db/qos_map_table_global_input.json @@ -0,0 +1,10 @@ +{ + "VERSIONS|DATABASE": { + "VERSION": "version_3_0_5" + }, + "DSCP_TO_TC_MAP|AZURE": { + "0": "0", + "1": "1" + } +} + diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index 2fd97aa3f9..8bc7b4101c 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -374,3 +374,39 @@ def test_pfc_enable_migrator(self): diff = DeepDiff(resulting_table, expected_table, ignore_order=True) assert not diff + +class TestGlobalDscpToTcMapMigrator(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 + + def test_global_dscp_to_tc_map_migrator(self): + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'qos_map_table_global_input') + import db_migrator + dbmgtr = db_migrator.DBMigrator(None) + dbmgtr.asic_type = "broadcom" + dbmgtr.hwsku = "vs" + dbmgtr.migrate() + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'qos_map_table_global_expected') + expected_db = Db() + + resulting_table = dbmgtr.configDB.get_table('PORT_QOS_MAP') + expected_table = expected_db.cfgdb.get_table('PORT_QOS_MAP') + + diff = DeepDiff(resulting_table, expected_table, ignore_order=True) + assert not diff + + # Check port_qos_map|global is not generated on mellanox asic + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'qos_map_table_global_input') + dbmgtr_mlnx = db_migrator.DBMigrator(None) + dbmgtr_mlnx.asic_type = "mellanox" + dbmgtr_mlnx.hwsku = "vs" + dbmgtr_mlnx.migrate() + resulting_table = dbmgtr_mlnx.configDB.get_table('PORT_QOS_MAP') + assert resulting_table == {} +