Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YANG] Fix issue: Non compliant leaf list in config_db schema #135

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/sonic-yang-mgmt/sonic_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def __init__(self, yang_dir, debug=False, print_log_enabled=True):
# all yang modules, such as grouping.
self.preProcessedYang = dict()

self.processingPath = []

self.leaf_list_with_string_value_set = set()

try:
self.ctx = ly.Context(yang_dir)
except Exception as e:
Expand Down
39 changes: 34 additions & 5 deletions src/sonic-yang-mgmt/sonic_yang_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ def _yangConvert(val):
# if it is a leaf-list do it for each element
if leafDict[key]['__isleafList']:
vValue = list()
if isinstance(value, str):
self.leaf_list_with_string_value_set.add(tuple(self.processingPath))
value = (x.strip() for x in value.split(','))
for v in value:
vValue.append(_yangConvert(v))
else:
Expand Down Expand Up @@ -545,16 +548,21 @@ def _xlateList(self, model, yang, config, table, exceptionList):
primaryKeys = list(config.keys())
for pkey in primaryKeys:
try:
self.processingPath.append(pkey)
vKey = None
self.sysLog(syslog.LOG_DEBUG, "xlateList Extract pkey:{}".\
format(pkey))
# Find and extracts key from each dict in config
keyDict = self._extractKey(pkey, listKeys)
# fill rest of the values in keyDict
for vKey in config[pkey]:
self.processingPath.append(vKey)
Junchao-Mellanox marked this conversation as resolved.
Show resolved Hide resolved
self.sysLog(syslog.LOG_DEBUG, "xlateList vkey {}".format(vKey))
keyDict[vKey] = self._findYangTypedValue(vKey, \
config[pkey][vKey], leafDict)
try:
keyDict[vKey] = self._findYangTypedValue(vKey, \
config[pkey][vKey], leafDict)
finally:
self.processingPath.pop()
yang.append(keyDict)
# delete pkey from config, done to match one key with one list
del config[pkey]
Expand All @@ -566,6 +574,8 @@ def _xlateList(self, model, yang, config, table, exceptionList):
exceptionList.append(str(e))
# with multilist, we continue matching other keys.
continue
finally:
self.processingPath.pop()

return

Expand Down Expand Up @@ -596,8 +606,10 @@ def _xlateContainerInContainer(self, model, yang, configC, table):
if not configC.get(ccontainer['@name']):
return
self.sysLog(msg="xlateProcessListOfContainer: {}".format(ccontainer['@name']))
self.processingPath.append(ccontainer['@name'])
self._xlateContainer(ccontainer, yang[ccontainer['@name']], \
configC[ccontainer['@name']], table)
self.processingPath.pop()
# clean empty container
if len(yang[ccontainer['@name']]) == 0:
del yang[ccontainer['@name']]
Expand Down Expand Up @@ -645,8 +657,10 @@ def _xlateContainer(self, model, yang, config, table):
for vKey in vKeys:
#vkey must be a leaf\leaf-list\choice in container
if leafDict.get(vKey):
self.processingPath.append(vKey)
self.sysLog(syslog.LOG_DEBUG, "xlateContainer vkey {}".format(vKey))
yang[vKey] = self._findYangTypedValue(vKey, configC[vKey], leafDict)
self.processingPath.pop()
# delete entry from copy of config
del configC[vKey]

Expand Down Expand Up @@ -676,8 +690,10 @@ def _xlateConfigDBtoYang(self, jIn, yangJ):
yangJ[key] = dict() if yangJ.get(key) is None else yangJ[key]
yangJ[key][subkey] = dict()
self.sysLog(msg="xlateConfigDBtoYang {}:{}".format(key, subkey))
self.processingPath.append(table)
self._xlateContainer(cmap['container'], yangJ[key][subkey], \
jIn[table], table)
self.processingPath = []

return

Expand Down Expand Up @@ -734,9 +750,12 @@ def _revYangConvert(val):

# if it is a leaf-list do it for each element
if leafDict[key]['__isleafList']:
vValue = list()
for v in value:
vValue.append(_revYangConvert(v))
if tuple(self.processingPath) in self.leaf_list_with_string_value_set:
vValue = ','.join((_revYangConvert(x) for x in value))
else:
vValue = list()
for v in value:
vValue.append(_revYangConvert(v))
elif leafDict[key]['type']['@name'] == 'boolean':
vValue = 'true' if value else 'false'
else:
Expand Down Expand Up @@ -845,12 +864,16 @@ def _revXlateList(self, model, yang, config, table):
# create key of config DB table
pkey, pkeydict = self._createKey(entry, listKeys)
self.sysLog(syslog.LOG_DEBUG, "revXlateList pkey:{}".format(pkey))
self.processingPath.append(pkey)
config[pkey]= dict()
# fill rest of the entries
for key in entry:
if key not in pkeydict:
self.processingPath.append(key)
config[pkey][key] = self._revFindYangTypedValue(key, \
entry[key], leafDict)
self.processingPath.pop()
self.processingPath.pop()

return

Expand All @@ -874,8 +897,10 @@ def _revXlateContainerInContainer(self, model, yang, config, table):
if yang.get(modelContainer['@name']):
config[modelContainer['@name']] = dict()
self.sysLog(msg="revXlateContainerInContainer {}".format(modelContainer['@name']))
self.processingPath.append(modelContainer['@name'])
self._revXlateContainer(modelContainer, yang[modelContainer['@name']], \
config[modelContainer['@name']], table)
self.processingPath.pop()
return

"""
Expand Down Expand Up @@ -907,7 +932,9 @@ def _revXlateContainer(self, model, yang, config, table):
#vkey must be a leaf\leaf-list\choice in container
if leafDict.get(vKey):
self.sysLog(syslog.LOG_DEBUG, "revXlateContainer vkey {}".format(vKey))
self.processingPath.append(vKey)
config[vKey] = self._revFindYangTypedValue(vKey, yang[vKey], leafDict)
self.processingPath.pop()

return

Expand Down Expand Up @@ -935,8 +962,10 @@ def _revXlateYangtoConfigDB(self, yangJ, cDbJson):
cDbJson[table] = dict()
#print(key + "--" + subkey)
self.sysLog(msg="revXlateYangtoConfigDB {}".format(table))
self.processingPath.append(table)
self._revXlateContainer(cmap['container'], yangJ[module_top][container], \
cDbJson[table], table)
self.processingPath = []

return

Expand Down
10 changes: 8 additions & 2 deletions src/sonic-yang-models/tests/files/sample_config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,21 @@
"description": "",
"speed": "11100",
"tpid": "0x8100",
"admin_status": "up"
"admin_status": "up",
"autoneg": "on",
"adv_speeds": "100000,50000",
"adv_interface_types": "CR,CR4"
},
"Ethernet2": {
"alias": "Eth1/3",
"lanes": "67",
"description": "",
"speed": "11100",
"tpid": "0x8100",
"admin_status": "up"
"admin_status": "up",
"autoneg": "on",
"adv_speeds": "all",
"adv_interface_types": "all"
},
"Ethernet3": {
"alias": "Eth1/4",
Expand Down