Skip to content

Commit

Permalink
[sonic_yang.py]: Adding debug support in sonic_yang. (sonic-net#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen Chaudhary authored and zhenggen-xu committed Jan 13, 2020
1 parent 17e0bb1 commit 9df7056
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/sonic-yang-mgmt/_sonic_yang_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,22 @@ def xlateList(self, model, yang, config, table):

leafDict = self.createLeafDict(model)

self.logInFile("Xlate {}".format(table))
# Find and extracts key from each dict in config
for pkey in config:
try:
vKey = None
self.logInFile("xlate Extract pkey {} {}".format(pkey,keyExt[table]))
keyDict = self.extractKey(pkey, keyExt[table])
# fill rest of the values in keyDict
for vKey in config[pkey]:
self.logInFile("xlate vkey {}".format(vKey), keyExt[table])
keyDict[vKey] = self.findYangTypedValue(vKey, \
config[pkey][vKey], leafDict)
yang.append(keyDict)
except Exception as e:
print("Exception while Config DB --> YANG: pkey:{}, "\
"vKey:{}, value: {}".format(pkey, vKey, config[pkey][vKey]))
"vKey:{}, value: {}".format(pkey, vKey, config[pkey].get(vKey)))
raise e

return
Expand Down
27 changes: 26 additions & 1 deletion src/sonic-yang-mgmt/sonic_yang.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import yang as ly
from json import dump
from glob import glob
from datetime import datetime

"""
Yang schema and data tree python APIs based on libyang python
"""
class sonic_yang:
def __init__(self, yang_dir):

def __init__(self, yang_dir, debug=True):
self.yang_dir = yang_dir
self.ctx = None
self.module = None
self.root = None

self.DEBUG_FILE = None
if debug:
self.DEBUG_FILE = '_debug_sonic_yang'
with open(self.DEBUG_FILE, 'w') as df:
df.write('--- Start sonic_yang logging ---\n\n')

# yang model files, need this map it to module
self.yangFiles = list()
# map from TABLE in config DB to container and module
Expand Down Expand Up @@ -39,6 +47,23 @@ def fail(self, e):
"""
from _sonic_yang_ext import *

"""
Loggign in debug file, this function prints header then object
"""
def logInFile(self, header="", obj=None, json=False):

if self.DEBUG_FILE:
with open(self.DEBUG_FILE, 'a') as df:
time = datetime.now()
df.write('\n\n{}: {}\n'.format(time, header))
if json:
dump(obj, df, indent=4)
elif obj:
df.write('{}: {}'.format(time, obj))
df.write('\n----')

return

"""
load_schema_module(): load a Yang model file
input: yang_file - full path of a Yang model file
Expand Down

0 comments on commit 9df7056

Please sign in to comment.