|
9 | 9 |
|
10 | 10 | from sonic_py_common import device_info, logger
|
11 | 11 | from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig
|
12 |
| -from db_migrator_constants import RESTAPI, TELEMETRY, CONSOLE_SWITCH |
| 12 | +from minigraph import parse_xml |
13 | 13 |
|
14 | 14 | INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
|
| 15 | +MINIGRAPH_FILE = '/etc/sonic/minigraph.xml' |
15 | 16 |
|
16 | 17 | # mock the redis for unit test purposes #
|
17 | 18 | try:
|
|
22 | 23 | sys.path.insert(0, modules_path)
|
23 | 24 | sys.path.insert(0, tests_path)
|
24 | 25 | INIT_CFG_FILE = os.path.join(mocked_db_path, "init_cfg.json")
|
| 26 | + MINIGRAPH_FILE = os.path.join(mocked_db_path, "minigraph.xml") |
25 | 27 | except KeyError:
|
26 | 28 | pass
|
27 | 29 |
|
@@ -51,6 +53,16 @@ def __init__(self, namespace, socket=None):
|
51 | 53 | self.TABLE_KEY = 'DATABASE'
|
52 | 54 | self.TABLE_FIELD = 'VERSION'
|
53 | 55 |
|
| 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 | + |
54 | 66 | db_kwargs = {}
|
55 | 67 | if socket:
|
56 | 68 | db_kwargs['unix_socket_path'] = socket
|
@@ -527,38 +539,50 @@ def migrate_vxlan_config(self):
|
527 | 539 |
|
528 | 540 | def migrate_restapi(self):
|
529 | 541 | # 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'] |
530 | 545 | log.log_notice('Migrate RESTAPI configuration')
|
531 | 546 | config = self.configDB.get_entry('RESTAPI', 'config')
|
532 | 547 | if not config:
|
533 |
| - self.configDB.set_entry("RESTAPI", "config", RESTAPI.get("config")) |
| 548 | + self.configDB.set_entry("RESTAPI", "config", restapi_data.get("config")) |
534 | 549 | certs = self.configDB.get_entry('RESTAPI', 'certs')
|
535 | 550 | if not certs:
|
536 |
| - self.configDB.set_entry("RESTAPI", "certs", RESTAPI.get("certs")) |
| 551 | + self.configDB.set_entry("RESTAPI", "certs", restapi_data.get("certs")) |
537 | 552 |
|
538 | 553 | def migrate_telemetry(self):
|
539 | 554 | # 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'] |
540 | 558 | log.log_notice('Migrate TELEMETRY configuration')
|
541 | 559 | gnmi = self.configDB.get_entry('TELEMETRY', 'gnmi')
|
542 | 560 | if not gnmi:
|
543 |
| - self.configDB.set_entry("TELEMETRY", "gnmi", TELEMETRY.get("gnmi")) |
| 561 | + self.configDB.set_entry("TELEMETRY", "gnmi", telemetry_data.get("gnmi")) |
544 | 562 | certs = self.configDB.get_entry('TELEMETRY', 'certs')
|
545 | 563 | if not certs:
|
546 |
| - self.configDB.set_entry("TELEMETRY", "certs", TELEMETRY.get("certs")) |
| 564 | + self.configDB.set_entry("TELEMETRY", "certs", telemetry_data.get("certs")) |
547 | 565 |
|
548 | 566 | def migrate_console_switch(self):
|
549 | 567 | # 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'] |
550 | 571 | log.log_notice('Migrate CONSOLE_SWITCH configuration')
|
551 | 572 | console_mgmt = self.configDB.get_entry('CONSOLE_SWITCH', 'console_mgmt')
|
552 | 573 | if not console_mgmt:
|
553 | 574 | self.configDB.set_entry("CONSOLE_SWITCH", "console_mgmt",
|
554 |
| - CONSOLE_SWITCH.get("console_mgmt")) |
| 575 | + console_switch_data.get("console_mgmt")) |
555 | 576 |
|
556 | 577 | def migrate_device_metadata(self):
|
557 | 578 | # 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') |
559 | 582 | metadata = self.configDB.get_entry('DEVICE_METADATA', 'localhost')
|
| 583 | + device_metadata_data = self.minigraph_data["DEVICE_METADATA"]["localhost"] |
560 | 584 | if 'synchronous_mode' not in metadata:
|
561 |
| - metadata['synchronous_mode'] = 'enable' |
| 585 | + metadata['synchronous_mode'] = device_metadata_data.get("synchronous_mode") |
562 | 586 | self.configDB.set_entry('DEVICE_METADATA', 'localhost', metadata)
|
563 | 587 |
|
564 | 588 | def migrate_port_qos_map_global(self):
|
|
0 commit comments