Skip to content

Commit 7bc08c2

Browse files
authored
[db_migrator] Remove hardcoded config and migrate config from minigraph (sonic-net#2887)
Microsoft ADO: 18217044 This PR is follow up to an old PR: sonic-net#2515 This PR addresses two issues: Not migrating RESTAPI, TELEMETRY, DEVICE_METADATA entries if they are not supported in target image's minigraph.py. Not migrating these entries if minigraph.xml file is missing. Not maintaining the config for these tables in two different places. Currently db migrator has its own constants, and minigraph.py maintains its own. How I did it This change removes hardcoding config in migrator code, and migrating the config for RESTAPI, TELEMETRY, DEVICE_METADATA from minigraph generator. How to verify it Tested on a physical device.
1 parent b1aa942 commit 7bc08c2

File tree

6 files changed

+82
-45
lines changed

6 files changed

+82
-45
lines changed

scripts/db_migrator.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
from sonic_py_common import device_info, logger
1111
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig
12-
from db_migrator_constants import RESTAPI, TELEMETRY, CONSOLE_SWITCH
12+
from minigraph import parse_xml
1313

1414
INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
15+
MINIGRAPH_FILE = '/etc/sonic/minigraph.xml'
1516

1617
# mock the redis for unit test purposes #
1718
try:
@@ -22,6 +23,7 @@
2223
sys.path.insert(0, modules_path)
2324
sys.path.insert(0, tests_path)
2425
INIT_CFG_FILE = os.path.join(mocked_db_path, "init_cfg.json")
26+
MINIGRAPH_FILE = os.path.join(mocked_db_path, "minigraph.xml")
2527
except KeyError:
2628
pass
2729

@@ -51,6 +53,16 @@ def __init__(self, namespace, socket=None):
5153
self.TABLE_KEY = 'DATABASE'
5254
self.TABLE_FIELD = 'VERSION'
5355

56+
# load config data from minigraph to get the default/hardcoded values from minigraph.py
57+
# this is to avoid duplicating the hardcoded these values in db_migrator
58+
self.minigraph_data = None
59+
try:
60+
if os.path.isfile(MINIGRAPH_FILE):
61+
self.minigraph_data = parse_xml(MINIGRAPH_FILE)
62+
except Exception as e:
63+
log.log_error('Caught exception while trying to parse minigraph: ' + str(e))
64+
pass
65+
5466
db_kwargs = {}
5567
if socket:
5668
db_kwargs['unix_socket_path'] = socket
@@ -527,38 +539,50 @@ def migrate_vxlan_config(self):
527539

528540
def migrate_restapi(self):
529541
# RESTAPI - add missing key
542+
if not self.minigraph_data or 'RESTAPI' not in self.minigraph_data:
543+
return
544+
restapi_data = self.minigraph_data['RESTAPI']
530545
log.log_notice('Migrate RESTAPI configuration')
531546
config = self.configDB.get_entry('RESTAPI', 'config')
532547
if not config:
533-
self.configDB.set_entry("RESTAPI", "config", RESTAPI.get("config"))
548+
self.configDB.set_entry("RESTAPI", "config", restapi_data.get("config"))
534549
certs = self.configDB.get_entry('RESTAPI', 'certs')
535550
if not certs:
536-
self.configDB.set_entry("RESTAPI", "certs", RESTAPI.get("certs"))
551+
self.configDB.set_entry("RESTAPI", "certs", restapi_data.get("certs"))
537552

538553
def migrate_telemetry(self):
539554
# TELEMETRY - add missing key
555+
if not self.minigraph_data or 'TELEMETRY' not in self.minigraph_data:
556+
return
557+
telemetry_data = self.minigraph_data['TELEMETRY']
540558
log.log_notice('Migrate TELEMETRY configuration')
541559
gnmi = self.configDB.get_entry('TELEMETRY', 'gnmi')
542560
if not gnmi:
543-
self.configDB.set_entry("TELEMETRY", "gnmi", TELEMETRY.get("gnmi"))
561+
self.configDB.set_entry("TELEMETRY", "gnmi", telemetry_data.get("gnmi"))
544562
certs = self.configDB.get_entry('TELEMETRY', 'certs')
545563
if not certs:
546-
self.configDB.set_entry("TELEMETRY", "certs", TELEMETRY.get("certs"))
564+
self.configDB.set_entry("TELEMETRY", "certs", telemetry_data.get("certs"))
547565

548566
def migrate_console_switch(self):
549567
# CONSOLE_SWITCH - add missing key
568+
if not self.minigraph_data or 'CONSOLE_SWITCH' not in self.minigraph_data:
569+
return
570+
console_switch_data = self.minigraph_data['CONSOLE_SWITCH']
550571
log.log_notice('Migrate CONSOLE_SWITCH configuration')
551572
console_mgmt = self.configDB.get_entry('CONSOLE_SWITCH', 'console_mgmt')
552573
if not console_mgmt:
553574
self.configDB.set_entry("CONSOLE_SWITCH", "console_mgmt",
554-
CONSOLE_SWITCH.get("console_mgmt"))
575+
console_switch_data.get("console_mgmt"))
555576

556577
def migrate_device_metadata(self):
557578
# DEVICE_METADATA - synchronous_mode entry
558-
log.log_notice('Migrate DEVICE_METADATA missing configuration (synchronous_mode=enable)')
579+
if not self.minigraph_data or 'DEVICE_METADATA' not in self.minigraph_data:
580+
return
581+
log.log_notice('Migrate DEVICE_METADATA missing configuration')
559582
metadata = self.configDB.get_entry('DEVICE_METADATA', 'localhost')
583+
device_metadata_data = self.minigraph_data["DEVICE_METADATA"]["localhost"]
560584
if 'synchronous_mode' not in metadata:
561-
metadata['synchronous_mode'] = 'enable'
585+
metadata['synchronous_mode'] = device_metadata_data.get("synchronous_mode")
562586
self.configDB.set_entry('DEVICE_METADATA', 'localhost', metadata)
563587

564588
def migrate_port_qos_map_global(self):

scripts/db_migrator_constants.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
'scripts/coredump-compress',
118118
'scripts/configlet',
119119
'scripts/db_migrator.py',
120-
'scripts/db_migrator_constants.py',
121120
'scripts/decode-syseeprom',
122121
'scripts/dropcheck',
123122
'scripts/disk_check.py',

tests/db_migrator_input/config_db/cross_branch_upgrade_to_version_2_0_2_expected.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
},
77
"RESTAPI|certs": {
88
"server_key": "/etc/sonic/credentials/restapiserver.key",
9-
"ca_crt": "/etc/sonic/credentials/AME_ROOT_CERTIFICATE.pem",
10-
"server_crt": "/etc/sonic/credentials/restapiserver.crt",
11-
"client_crt_cname": "client.restapi.sonic.gbl"
9+
"ca_crt": "/etc/sonic/credentials/restapica.crt",
10+
"server_crt": "/etc/sonic/credentials/restapiserver.crt",
11+
"client_crt_cname": "client.restapi.sonic"
1212
},
1313
"TELEMETRY|gnmi": {
1414
"client_auth": "true",

tests/db_migrator_input/minigraph.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<DeviceMiniGraph xmlns="Microsoft.Search.Autopilot.Evolution" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
2+
<CpgDec/>
3+
<DpgDec>
4+
<DeviceDataPlaneInfo>
5+
<LoopbackIPInterfaces/>
6+
<ManagementIPInterfaces/>
7+
<Hostname>SONiC-Dummy</Hostname>
8+
<PortChannelInterfaces/>
9+
<VlanInterfaces/>
10+
<IPInterfaces/>
11+
<AclInterfaces/>
12+
</DeviceDataPlaneInfo>
13+
</DpgDec>
14+
<PngDec>
15+
<Devices>
16+
<Device i:type="TorRouter">
17+
<Hostname>SONiC-Dummy</Hostname>
18+
</Device>
19+
</Devices>
20+
</PngDec>
21+
<MetadataDeclaration>
22+
<Devices xmlns:a="http://schemas">
23+
<a:DeviceMetadata>
24+
<a:Properties/>
25+
</a:DeviceMetadata>
26+
</Devices>
27+
</MetadataDeclaration>
28+
<Hostname>SONiC-Dummy</Hostname>
29+
</DeviceMiniGraph>

tests/db_migrator_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,31 @@ def test_warm_upgrade_to_2_0_2(self):
501501
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'cross_branch_upgrade_to_version_2_0_2_expected')
502502
expected_db = Db()
503503

504-
expected_db
505504
new_tables = ["RESTAPI", "TELEMETRY", "CONSOLE_SWITCH"]
506505
for table in new_tables:
507506
resulting_table = dbmgtr.configDB.get_table(table)
508507
expected_table = expected_db.cfgdb.get_table(table)
509508
diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
510509
assert not diff
511510

511+
def test_warm_upgrade__without_mg_to_2_0_2(self):
512+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'cross_branch_upgrade_to_version_2_0_2_input')
513+
import db_migrator
514+
dbmgtr = db_migrator.DBMigrator(None)
515+
# set minigraph_data to None to mimic the missing minigraph.xml scenario
516+
dbmgtr.minigraph_data = None
517+
dbmgtr.migrate()
518+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'cross_branch_upgrade_without_mg_2_0_2_expected.json')
519+
expected_db = Db()
520+
521+
new_tables = ["RESTAPI", "TELEMETRY", "CONSOLE_SWITCH"]
522+
for table in new_tables:
523+
resulting_table = dbmgtr.configDB.get_table(table)
524+
expected_table = expected_db.cfgdb.get_table(table)
525+
print(resulting_table)
526+
diff = DeepDiff(resulting_table, expected_table, ignore_order=True)
527+
assert not diff
528+
512529
class Test_Migrate_Loopback(object):
513530
@classmethod
514531
def setup_class(cls):

0 commit comments

Comments
 (0)