Skip to content

Commit f1522b9

Browse files
author
Praveen Chaudhary
authored
[config_mgmt.py]: Set leaf-list to empty list while port breakout. (#1268)
-- Set leaf-list to empty list while port breakout, because deletions are not done to fields once set. -- Minor changes in test file. Signed-off-by: Praveen Chaudhary <[email protected]>
1 parent 99c05d5 commit f1522b9

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

config/config_mgmt.py

+35-8
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ def validateConfigData(self):
126126
try:
127127
self.sy.validate_data_tree()
128128
except Exception as e:
129-
self.sysLog(msg='Data Validation Failed')
129+
self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR,
130+
msg='Data Validation Failed')
130131
return False
131132

132133
self.sysLog(msg='Data Validation successful', doPrint=True)
@@ -146,7 +147,8 @@ def sysLog(self, logLevel=syslog.LOG_INFO, msg=None, doPrint=False):
146147
# log debug only if enabled
147148
if self.DEBUG == False and logLevel == syslog.LOG_DEBUG:
148149
return
149-
if flags.interactive !=0 and doPrint == True:
150+
# always print < Info level msg with doPrint flag
151+
if doPrint == True and (logLevel < syslog.LOG_INFO or flags.interactive != 0):
150152
print("{}".format(msg))
151153
syslog.openlog(self.SYSLOG_IDENTIFIER)
152154
syslog.syslog(logLevel, msg)
@@ -712,18 +714,31 @@ def _deleteHandler(diff, inp, outp, config):
712714
'''
713715
if isinstance(inp, dict):
714716
# Example Case: diff = PORT': {delete: {u'Ethernet1': {...}}}}
717+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
718+
msg="Delete Dict diff:{}".format(diff))
715719
for key in diff:
716720
# make sure keys from diff are present in inp but not in outp
717721
if key in inp and key not in outp:
718-
# assign key to None(null), redis will delete entire key
719-
config[key] = None
722+
if type(inp[key]) == list:
723+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
724+
msg="Delete List key:{}".format(key))
725+
# assign current lists as empty.
726+
config[key] = []
727+
else:
728+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
729+
msg="Delete Dict key:{}".format(key))
730+
# assign key to None(null), redis will delete entire key
731+
config[key] = None
720732
else:
721733
# should not happen
722734
raise Exception('Invalid deletion of {} in diff'.format(key))
723735

724736
elif isinstance(inp, list):
725-
# Example case: {u'VLAN': {u'Vlan100': {'members': {delete: [(95, 'Ethernet1')]}}
726-
# just take list from outputs
737+
# Example case: diff: [(3, 'Ethernet10'), (2, 'Ethernet8')]
738+
# inp:['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10']
739+
# outp:['Ethernet0', 'Ethernet4']
740+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
741+
msg="Delete List diff: {} inp:{} outp:{}".format(diff, inp, outp))
727742
config.extend(outp)
728743
return
729744

@@ -733,19 +748,31 @@ def _insertHandler(diff, inp, outp, config):
733748
'''
734749
if isinstance(outp, dict):
735750
# Example Case: diff = PORT': {insert: {u'Ethernet1': {...}}}}
751+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
752+
msg="Insert Dict diff:{}".format(diff))
736753
for key in diff:
737754
# make sure keys are only in outp
738755
if key not in inp and key in outp:
756+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
757+
msg="Insert Dict key:{}".format(key))
739758
# assign key in config same as outp
740759
config[key] = outp[key]
741760
else:
742761
# should not happen
743762
raise Exception('Invalid insertion of {} in diff'.format(key))
744763

745764
elif isinstance(outp, list):
746-
# just take list from output
747-
# Example case: {u'VLAN': {u'Vlan100': {'members': {insert: [(95, 'Ethernet1')]}}
765+
# Example diff:[(2, 'Ethernet8'), (3, 'Ethernet10')]
766+
# in:['Ethernet0', 'Ethernet4']
767+
# out:['Ethernet0', 'Ethernet4', 'Ethernet8', 'Ethernet10']
768+
self.sysLog(logLevel=syslog.LOG_DEBUG, \
769+
msg="Insert list diff:{} inp:{} outp:{}".format(diff, inp, outp))
748770
config.extend(outp)
771+
# configDb stores []->[""], i.e. empty list as list of empty
772+
# string. While adding default config for newly created ports,
773+
# inp can be [""], in that case remove it from delta config.
774+
if inp == ['']:
775+
config.remove('');
749776
return
750777

751778
def _recurCreateConfig(diff, inp, outp, config):

tests/config_mgmt_test.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from unittest import TestCase
1111
from mock import MagicMock, call
1212
from json import dump
13+
from copy import deepcopy
1314

1415
class TestConfigMgmt(TestCase):
1516
'''
@@ -22,14 +23,14 @@ def setUp(self):
2223
return
2324

2425
def test_config_validation(self):
25-
curConfig = dict(configDbJson)
26+
curConfig = deepcopy(configDbJson)
2627
self.writeJson(curConfig, config_mgmt.CONFIG_DB_JSON_FILE)
2728
cm = config_mgmt.ConfigMgmt(source=config_mgmt.CONFIG_DB_JSON_FILE)
2829
assert cm.validateConfigData() == True
2930
return
3031

3132
def test_table_without_yang(self):
32-
curConfig = dict(configDbJson)
33+
curConfig = deepcopy(configDbJson)
3334
unknown = {"unknown_table": {"ukey": "uvalue"}}
3435
self.updateConfig(curConfig, unknown)
3536
self.writeJson(curConfig, config_mgmt.CONFIG_DB_JSON_FILE)
@@ -38,7 +39,7 @@ def test_table_without_yang(self):
3839
return
3940

4041
def test_search_keys(self):
41-
curConfig = dict(configDbJson)
42+
curConfig = deepcopy(configDbJson)
4243
self.writeJson(curConfig, config_mgmt.CONFIG_DB_JSON_FILE)
4344
cmdpb = config_mgmt.ConfigMgmtDPB(source=config_mgmt.CONFIG_DB_JSON_FILE)
4445
out = cmdpb.configWithKeys(portBreakOutConfigDbJson, \
@@ -61,7 +62,7 @@ def test_break_out(self):
6162
self.writeJson(portBreakOutConfigDbJson, \
6263
config_mgmt.DEFAULT_CONFIG_DB_JSON_FILE)
6364
# prepare config dj json to start with
64-
curConfig = dict(configDbJson)
65+
curConfig = deepcopy(configDbJson)
6566
#Ethernet8: start from 4x25G-->2x50G with -f -l
6667
self.dpb_port8_4x25G_2x50G_f_l(curConfig)
6768
#Ethernet8: move from 2x50G-->1x100G without force, list deps
@@ -182,6 +183,13 @@ def updateConfig(self, conf, uconf):
182183
else:
183184
if isinstance(conf[it], list) and isinstance(uconf[it], list):
184185
conf[it] = list(uconf[it])
186+
'''
187+
configDb stores []->[""], i.e. empty list as
188+
list of empty string. So we need to replicate
189+
same behaviour here.
190+
'''
191+
if len(conf[it]) == 0:
192+
conf[it] = [""]
185193
elif isinstance(conf[it], dict) and isinstance(uconf[it], dict):
186194
self.updateConfig(conf[it], uconf[it])
187195
else:
@@ -442,7 +450,7 @@ def dpb_port8_4x25G_2x50G_f_l(self, curConfig):
442450
'ports': ['Ethernet0', 'Ethernet4']
443451
},
444452
'NO-NSW-PACL-TEST': {
445-
'ports': None
453+
'ports': []
446454
}
447455
},
448456
'INTERFACE': None,

0 commit comments

Comments
 (0)