Skip to content

Commit

Permalink
[202311] Migrate GNMI table (sonic-net#3138)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu authored Feb 1, 2024
1 parent 9c1d489 commit be6224a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
39 changes: 37 additions & 2 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, namespace, socket=None):
none-zero values.
build: sequentially increase within a minor version domain.
"""
self.CURRENT_VERSION = 'version_202311_02'
self.CURRENT_VERSION = 'version_202311_03'

self.TABLE_NAME = 'VERSIONS'
self.TABLE_KEY = 'DATABASE'
Expand Down Expand Up @@ -618,6 +618,30 @@ def migrate_telemetry(self):
if not certs:
self.configDB.set_entry("TELEMETRY", "certs", telemetry_data.get("certs"))

def migrate_gnmi(self):
# If there's GNMI table in CONFIG_DB, no need to migrate
gnmi = self.configDB.get_entry('GNMI', 'gnmi')
certs = self.configDB.get_entry('GNMI', 'certs')
if gnmi and certs:
return
if self.config_src_data:
if 'GNMI' in self.config_src_data:
# If there's GNMI in minigraph or golden config, copy configuration from config_src_data
gnmi_data = self.config_src_data['GNMI']
log.log_notice('Migrate GNMI configuration')
if 'gnmi' in gnmi_data:
self.configDB.set_entry("GNMI", "gnmi", gnmi_data.get('gnmi'))
if 'certs' in gnmi_data:
self.configDB.set_entry("GNMI", "certs", gnmi_data.get('certs'))
else:
# If there's no minigraph or golden config, copy configuration from CONFIG_DB TELEMETRY table
gnmi = self.configDB.get_entry('TELEMETRY', 'gnmi')
if gnmi:
self.configDB.set_entry("GNMI", "gnmi", gnmi)
certs = self.configDB.get_entry('TELEMETRY', 'certs')
if certs:
self.configDB.set_entry("GNMI", "certs", certs)

def migrate_console_switch(self):
# CONSOLE_SWITCH - add missing key
if not self.config_src_data or 'CONSOLE_SWITCH' not in self.config_src_data:
Expand Down Expand Up @@ -1159,9 +1183,20 @@ def version_202311_01(self):
def version_202311_02(self):
"""
Version 202311_02.
This is current last erversion for 202311 branch
"""
log.log_info('Handling version_202311_02')
# Update GNMI table
self.migrate_gnmi()

self.set_version('version_202311_03')
return 'version_202311_03'

def version_202311_03(self):
"""
Version 202311_03.
This is current last erversion for 202311 branch
"""
log.log_info('Handling version_202311_03')
return None

def get_version(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/db_migrator_input/config_db/gnmi-configdb-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"GNMI|gnmi": {
"client_auth": "true",
"log_level": "2",
"port": "50051"
},
"GNMI|certs": {
"server_key": "/etc/sonic/telemetry/streamingtelemetryserver.key",
"ca_crt": "/etc/sonic/telemetry/dsmsroot.cer",
"server_crt": "/etc/sonic/telemetry/streamingtelemetryserver.cer"
},
"VERSIONS|DATABASE": {
"VERSION": "version_202311_03"
}
}
15 changes: 15 additions & 0 deletions tests/db_migrator_input/config_db/gnmi-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"TELEMETRY|gnmi": {
"client_auth": "true",
"log_level": "2",
"port": "50051"
},
"TELEMETRY|certs": {
"server_key": "/etc/sonic/telemetry/streamingtelemetryserver.key",
"ca_crt": "/etc/sonic/telemetry/dsmsroot.cer",
"server_crt": "/etc/sonic/telemetry/streamingtelemetryserver.cer"
},
"VERSIONS|DATABASE": {
"VERSION": "version_202311_01"
}
}
15 changes: 15 additions & 0 deletions tests/db_migrator_input/config_db/gnmi-minigraph-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"GNMI|gnmi": {
"client_auth": "true",
"log_level": "2",
"port": "50052"
},
"GNMI|certs": {
"server_key": "/etc/sonic/telemetry/streamingtelemetryserver.key",
"ca_crt": "/etc/sonic/telemetry/dsmsroot.cer",
"server_crt": "/etc/sonic/telemetry/streamingtelemetryserver.cer"
},
"VERSIONS|DATABASE": {
"VERSION": "version_202311_03"
}
}
56 changes: 56 additions & 0 deletions tests/db_migrator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,59 @@ def test_init(self, mock_args):
mock_args.return_value=argparse.Namespace(namespace=None, operation='get_version', socket=None)
import db_migrator
db_migrator.main()


class TestGNMIMigrator(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_gnmi_migrator_minigraph(self):
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'gnmi-input')
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
# Set config_src_data
dbmgtr.config_src_data = {
'GNMI': {
'gnmi': {
"client_auth": "true",
"log_level": "2",
"port": "50052"
},
'certs': {
"server_key": "/etc/sonic/telemetry/streamingtelemetryserver.key",
"ca_crt": "/etc/sonic/telemetry/dsmsroot.cer",
"server_crt": "/etc/sonic/telemetry/streamingtelemetryserver.cer"
}
}
}
dbmgtr.migrate()
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'gnmi-minigraph-expected')
expected_db = Db()
advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_202311_03')
resulting_table = dbmgtr.configDB.get_table("GNMI")
expected_table = expected_db.cfgdb.get_table("GNMI")

diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
assert not diff

def test_gnmi_migrator_configdb(self):
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'gnmi-input')
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
# Set config_src_data
dbmgtr.config_src_data = {}
dbmgtr.migrate()
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'gnmi-configdb-expected')
expected_db = Db()
advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_202311_03')
resulting_table = dbmgtr.configDB.get_table("GNMI")
expected_table = expected_db.cfgdb.get_table("GNMI")

diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
assert not diff

0 comments on commit be6224a

Please sign in to comment.