Skip to content

[sonic_yang.py]: Adding debug support in sonic_yang. #28

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

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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