Skip to content

Commit

Permalink
[sflow][db_migrator] Egress Sflow support (#3020)
Browse files Browse the repository at this point in the history
* [sflow][db_migrator] Egress Sflow support
  • Loading branch information
rajkumar38 authored Nov 28, 2023
1 parent 02a588b commit 6dfeee6
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 2 deletions.
43 changes: 41 additions & 2 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
21 changes: 21 additions & 0 deletions tests/db_migrator_input/appl_db/sflow_table_expected.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
17 changes: 17 additions & 0 deletions tests/db_migrator_input/appl_db/sflow_table_input.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
57 changes: 57 additions & 0 deletions tests/db_migrator_input/config_db/sflow_table_expected.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
53 changes: 53 additions & 0 deletions tests/db_migrator_input/config_db/sflow_table_input.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
58 changes: 58 additions & 0 deletions tests/db_migrator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6dfeee6

Please sign in to comment.