10
10
from sonic_py_common import device_info , logger
11
11
from swsscommon .swsscommon import SonicV2Connector , ConfigDBConnector , SonicDBConfig
12
12
from minigraph import parse_xml
13
+ from utilities_common .helper import update_config
13
14
14
15
INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
15
16
MINIGRAPH_FILE = '/etc/sonic/minigraph.xml'
17
+ GOLDEN_CFG_FILE = '/etc/sonic/golden_config_db.json'
16
18
17
19
# mock the redis for unit test purposes #
18
20
try :
24
26
sys .path .insert (0 , tests_path )
25
27
INIT_CFG_FILE = os .path .join (mocked_db_path , "init_cfg.json" )
26
28
MINIGRAPH_FILE = os .path .join (mocked_db_path , "minigraph.xml" )
29
+ GOLDEN_CFG_FILE = os .path .join (mocked_db_path , "golden_config_db.json" )
27
30
except KeyError :
28
31
pass
29
32
30
33
SYSLOG_IDENTIFIER = 'db_migrator'
34
+ DEFAULT_NAMESPACE = ''
31
35
32
36
33
37
# Global logger instance
@@ -60,15 +64,8 @@ def __init__(self, namespace, socket=None):
60
64
self .TABLE_KEY = 'DATABASE'
61
65
self .TABLE_FIELD = 'VERSION'
62
66
63
- # load config data from minigraph to get the default/hardcoded values from minigraph.py
64
- # this is to avoid duplicating the hardcoded these values in db_migrator
65
- self .minigraph_data = None
66
- try :
67
- if os .path .isfile (MINIGRAPH_FILE ):
68
- self .minigraph_data = parse_xml (MINIGRAPH_FILE )
69
- except Exception as e :
70
- log .log_error ('Caught exception while trying to parse minigraph: ' + str (e ))
71
- pass
67
+ # Generate config_src_data from minigraph and golden config
68
+ self .generate_config_src (namespace )
72
69
73
70
db_kwargs = {}
74
71
if socket :
@@ -107,6 +104,55 @@ def __init__(self, namespace, socket=None):
107
104
from mellanox_buffer_migrator import MellanoxBufferMigrator
108
105
self .mellanox_buffer_migrator = MellanoxBufferMigrator (self .configDB , self .appDB , self .stateDB )
109
106
107
+ def generate_config_src (self , ns ):
108
+ '''
109
+ Generate config_src_data from minigraph and golden config
110
+ This method uses golden_config_data and minigraph_data as local variables,
111
+ which means they are not accessible or modifiable from outside this method.
112
+ This way, this method ensures that these variables are not changed unintentionally.
113
+ Args:
114
+ ns: namespace
115
+ Returns:
116
+ '''
117
+ # load config data from golden_config_db.json
118
+ golden_config_data = None
119
+ try :
120
+ if os .path .isfile (GOLDEN_CFG_FILE ):
121
+ with open (GOLDEN_CFG_FILE ) as f :
122
+ golden_data = json .load (f )
123
+ if ns is None :
124
+ golden_config_data = golden_data
125
+ else :
126
+ if ns == DEFAULT_NAMESPACE :
127
+ config_namespace = "localhost"
128
+ else :
129
+ config_namespace = ns
130
+ golden_config_data = golden_data [config_namespace ]
131
+ except Exception as e :
132
+ log .log_error ('Caught exception while trying to load golden config: ' + str (e ))
133
+ pass
134
+ # load config data from minigraph to get the default/hardcoded values from minigraph.py
135
+ minigraph_data = None
136
+ try :
137
+ if os .path .isfile (MINIGRAPH_FILE ):
138
+ minigraph_data = parse_xml (MINIGRAPH_FILE )
139
+ except Exception as e :
140
+ log .log_error ('Caught exception while trying to parse minigraph: ' + str (e ))
141
+ pass
142
+ # When both golden config and minigraph exists, override minigraph config with golden config
143
+ # config_src_data is the source of truth for config data
144
+ # this is to avoid duplicating the hardcoded these values in db_migrator
145
+ self .config_src_data = None
146
+ if minigraph_data :
147
+ # Shallow copy for better performance
148
+ self .config_src_data = minigraph_data
149
+ if golden_config_data :
150
+ # Shallow copy for better performance
151
+ self .config_src_data = update_config (minigraph_data , golden_config_data , False )
152
+ elif golden_config_data :
153
+ # Shallow copy for better performance
154
+ self .config_src_data = golden_config_data
155
+
110
156
def migrate_pfc_wd_table (self ):
111
157
'''
112
158
Migrate all data entries from table PFC_WD_TABLE to PFC_WD
@@ -547,9 +593,9 @@ def migrate_vxlan_config(self):
547
593
548
594
def migrate_restapi (self ):
549
595
# RESTAPI - add missing key
550
- if not self .minigraph_data or 'RESTAPI' not in self .minigraph_data :
596
+ if not self .config_src_data or 'RESTAPI' not in self .config_src_data :
551
597
return
552
- restapi_data = self .minigraph_data ['RESTAPI' ]
598
+ restapi_data = self .config_src_data ['RESTAPI' ]
553
599
log .log_notice ('Migrate RESTAPI configuration' )
554
600
config = self .configDB .get_entry ('RESTAPI' , 'config' )
555
601
if not config :
@@ -560,9 +606,9 @@ def migrate_restapi(self):
560
606
561
607
def migrate_telemetry (self ):
562
608
# TELEMETRY - add missing key
563
- if not self .minigraph_data or 'TELEMETRY' not in self .minigraph_data :
609
+ if not self .config_src_data or 'TELEMETRY' not in self .config_src_data :
564
610
return
565
- telemetry_data = self .minigraph_data ['TELEMETRY' ]
611
+ telemetry_data = self .config_src_data ['TELEMETRY' ]
566
612
log .log_notice ('Migrate TELEMETRY configuration' )
567
613
gnmi = self .configDB .get_entry ('TELEMETRY' , 'gnmi' )
568
614
if not gnmi :
@@ -573,9 +619,9 @@ def migrate_telemetry(self):
573
619
574
620
def migrate_console_switch (self ):
575
621
# CONSOLE_SWITCH - add missing key
576
- if not self .minigraph_data or 'CONSOLE_SWITCH' not in self .minigraph_data :
622
+ if not self .config_src_data or 'CONSOLE_SWITCH' not in self .config_src_data :
577
623
return
578
- console_switch_data = self .minigraph_data ['CONSOLE_SWITCH' ]
624
+ console_switch_data = self .config_src_data ['CONSOLE_SWITCH' ]
579
625
log .log_notice ('Migrate CONSOLE_SWITCH configuration' )
580
626
console_mgmt = self .configDB .get_entry ('CONSOLE_SWITCH' , 'console_mgmt' )
581
627
if not console_mgmt :
@@ -584,11 +630,11 @@ def migrate_console_switch(self):
584
630
585
631
def migrate_device_metadata (self ):
586
632
# DEVICE_METADATA - synchronous_mode entry
587
- if not self .minigraph_data or 'DEVICE_METADATA' not in self .minigraph_data :
633
+ if not self .config_src_data or 'DEVICE_METADATA' not in self .config_src_data :
588
634
return
589
635
log .log_notice ('Migrate DEVICE_METADATA missing configuration' )
590
636
metadata = self .configDB .get_entry ('DEVICE_METADATA' , 'localhost' )
591
- device_metadata_data = self .minigraph_data ["DEVICE_METADATA" ]["localhost" ]
637
+ device_metadata_data = self .config_src_data ["DEVICE_METADATA" ]["localhost" ]
592
638
if 'synchronous_mode' not in metadata :
593
639
metadata ['synchronous_mode' ] = device_metadata_data .get ("synchronous_mode" )
594
640
self .configDB .set_entry ('DEVICE_METADATA' , 'localhost' , metadata )
@@ -628,19 +674,19 @@ def migrate_dns_nameserver(self):
628
674
Handle DNS_NAMESERVER table migration. Migrations handled:
629
675
If there's no DNS_NAMESERVER in config_DB, load DNS_NAMESERVER from minigraph
630
676
"""
631
- if not self .minigraph_data or 'DNS_NAMESERVER' not in self .minigraph_data :
677
+ if not self .config_src_data or 'DNS_NAMESERVER' not in self .config_src_data :
632
678
return
633
679
dns_table = self .configDB .get_table ('DNS_NAMESERVER' )
634
680
if not dns_table :
635
- for addr , config in self .minigraph_data ['DNS_NAMESERVER' ].items ():
681
+ for addr , config in self .config_src_data ['DNS_NAMESERVER' ].items ():
636
682
self .configDB .set_entry ('DNS_NAMESERVER' , addr , config )
637
683
638
684
def migrate_routing_config_mode (self ):
639
685
# DEVICE_METADATA - synchronous_mode entry
640
- if not self .minigraph_data or 'DEVICE_METADATA' not in self .minigraph_data :
686
+ if not self .config_src_data or 'DEVICE_METADATA' not in self .config_src_data :
641
687
return
642
688
device_metadata_old = self .configDB .get_entry ('DEVICE_METADATA' , 'localhost' )
643
- device_metadata_new = self .minigraph_data ['DEVICE_METADATA' ]['localhost' ]
689
+ device_metadata_new = self .config_src_data ['DEVICE_METADATA' ]['localhost' ]
644
690
# overwrite the routing-config-mode as per minigraph parser
645
691
# Criteria for update:
646
692
# if config mode is missing in base OS or if base and target modes are not same
0 commit comments