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

[sonic-yang-mgmt] Adding flag to disable/enable log printing #9659

Merged
merged 7 commits into from
Jan 5, 2022
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
56 changes: 30 additions & 26 deletions src/sonic-yang-mgmt/sonic_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""
class SonicYang(SonicYangExtMixin):

def __init__(self, yang_dir, debug=False):
def __init__(self, yang_dir, debug=False, print_log_enabled=True):
self.yang_dir = yang_dir
self.ctx = None
self.module = None
Expand All @@ -21,6 +21,11 @@ def __init__(self, yang_dir, debug=False):
# logging vars
self.SYSLOG_IDENTIFIER = "sonic_yang"
self.DEBUG = debug
self.print_log_enabled = print_log_enabled
if not print_log_enabled:
# The default libyang log options are ly.LY_LOLOG|ly.LY_LOSTORE_LAST.
# Removing ly.LY_LOLOG will stop libyang from printing the logs.
ly.set_log_options(ly.LY_LOSTORE_LAST)

# yang model files, need this map it to module
self.yangFiles = list()
Expand Down Expand Up @@ -51,11 +56,10 @@ def __del__(self):
pass

def sysLog(self, debug=syslog.LOG_INFO, msg=None, doPrint=False):

# log debug only if enabled
if self.DEBUG == False and debug == syslog.LOG_DEBUG:
return
if doPrint:
if doPrint and self.print_log_enabled:
print("{}({}):{}".format(self.SYSLOG_IDENTIFIER, debug, msg))
syslog.openlog(self.SYSLOG_IDENTIFIER)
syslog.syslog(debug, msg)
Expand All @@ -64,7 +68,7 @@ def sysLog(self, debug=syslog.LOG_INFO, msg=None, doPrint=False):
return

def fail(self, e):
print(e)
self.sysLog(msg=e, debug=syslog.LOG_ERR, doPrint=True)
raise e

"""
Expand All @@ -76,7 +80,7 @@ def _load_schema_module(self, yang_file):
try:
return self.ctx.parse_module_path(yang_file, ly.LYS_IN_YANG)
except Exception as e:
print("Failed to load yang module file: " + yang_file)
self.sysLog(msg="Failed to load yang module file: " + yang_file, debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)

"""
Expand Down Expand Up @@ -120,7 +124,7 @@ def _load_schema_modules_ctx(self, yang_dir=None):
try:
ctx.parse_module_path(str(file), ly.LYS_IN_YANG)
except Exception as e:
print("Failed to parse yang module file: " + file)
self.sysLog(msg="Failed to parse yang module file: " + file, debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)

return ctx
Expand All @@ -134,7 +138,7 @@ def _load_data_file(self, data_file):
try:
data_node = self.ctx.parse_data_path(data_file, ly.LYD_JSON, ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
except Exception as e:
print("Failed to load data file: " + str(data_file))
self.sysLog(msg="Failed to load data file: " + str(data_file), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
self.root = data_node
Expand Down Expand Up @@ -176,7 +180,7 @@ def _load_data_model(self, yang_dir, yang_files, data_files, output=None):
for i in range(1, len(data_files)):
self._merge_data(data_files[i])
except Exception as e:
print("Failed to load data files")
self.sysLog(msg="Failed to load data files", debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
return

Expand Down Expand Up @@ -217,7 +221,7 @@ def _get_module_tree(self, module_name, format):
try:
module = self.ctx.get_module(str(module_name))
except Exception as e:
print("Cound not get module: " + str(module_name))
self.sysLog(msg="Cound not get module: " + str(module_name), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
if (module is not None):
Expand Down Expand Up @@ -256,7 +260,7 @@ def validate_data_tree(self):
try:
self._validate_data(self.root, self.ctx)
except Exception as e:
print("Failed to validate data tree")
self.sysLog(msg="Failed to validate data tree\n{", debug=syslog.LOG_ERR, doPrint=True)
raise SonicYangException("Failed to validate data tree\n{}".\
format(str(e)))

Expand All @@ -267,12 +271,12 @@ def validate_data_tree(self):
"""
def _find_parent_data_node(self, data_xpath):
if (self.root is None):
print("data not loaded")
self.sysLog(msg="data not loaded", debug=syslog.LOG_ERR, doPrint=True)
return None
try:
data_node = self._find_data_node(data_xpath)
except Exception as e:
print("Failed to find data node from xpath: " + str(data_xpath))
self.sysLog(msg="Failed to find data node from xpath: " + str(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
if data_node is not None:
Expand All @@ -291,7 +295,7 @@ def _get_parent_data_xpath(self, data_xpath):
try:
data_node = self._find_parent_data_node(data_xpath)
except Exception as e:
print("Failed to find parent node from xpath: " + str(data_xpath))
self.sysLog(msg="Failed to find parent node from xpath: " + str(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
if data_node is not None:
Expand All @@ -310,7 +314,7 @@ def _new_data_node(self, xpath, value):
try:
data_node = self.root.new_path(self.ctx, xpath, val, 0, 0)
except Exception as e:
print("Failed to add data node for path: " + str(xpath))
self.sysLog(msg="Failed to add data node for path: " + str(xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
return data_node
Expand All @@ -326,7 +330,7 @@ def _find_data_node(self, data_xpath):
try:
set = self.root.find_path(data_xpath)
except Exception as e:
print("Failed to find data node from xpath: " + str(data_xpath))
self.sysLog(msg="Failed to find data node from xpath: " + str(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
if set is not None:
Expand Down Expand Up @@ -386,7 +390,7 @@ def _add_data_node(self, data_xpath, value):
#check if the node added to the data tree
self._find_data_node(data_xpath)
except Exception as e:
print("add_node(): Failed to add data node for xpath: " + str(data_xpath))
self.sysLog(msg="add_node(): Failed to add data node for xpath: " + str(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)

"""
Expand Down Expand Up @@ -426,10 +430,10 @@ def _deleteNode(self, xpath=None, node=None):
#deleted node not found
return True
else:
print('Could not delete Node')
self.sysLog(msg='Could not delete Node', debug=syslog.LOG_ERR, doPrint=True)
return False
else:
print("failed to find node, xpath: " + xpath)
self.sysLog(msg="failed to find node, xpath: " + xpath, debug=syslog.LOG_ERR, doPrint=True)

return False

Expand All @@ -443,7 +447,7 @@ def _find_data_node_value(self, data_xpath):
try:
data_node = self._find_data_node(data_xpath)
except Exception as e:
print("find_data_node_value(): Failed to find data node from xpath: {}".format(data_xpath))
self.sysLog(msg="find_data_node_value(): Failed to find data node from xpath: {}".format(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
else:
if (data_node is not None):
Expand All @@ -462,7 +466,7 @@ def _set_data_node_value(self, data_xpath, value):
try:
self.root.new_path(self.ctx, data_xpath, str(value), ly.LYD_ANYDATA_STRING, ly.LYD_PATH_OPT_UPDATE)
except Exception as e:
print("set data node value failed for xpath: " + str(data_xpath))
self.sysLog(msg="set data node value failed for xpath: " + str(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)

"""
Expand Down Expand Up @@ -497,15 +501,15 @@ def _find_schema_dependencies(self, schema_xpath):
try:
schema_node = self._find_schema_node(schema_xpath)
except Exception as e:
print("Cound not find the schema node from xpath: " + str(schema_xpath))
self.sysLog(msg="Cound not find the schema node from xpath: " + str(schema_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
return ref_list

schema_node = ly.Schema_Node_Leaf(schema_node)
backlinks = schema_node.backlinks()
if backlinks.number() > 0:
for link in backlinks.schema():
print("backlink schema: {}".format(link.path()))
self.sysLog(msg="backlink schema: {}".format(link.path()), doPrint=True)
ref_list.append(link.path())
return ref_list

Expand All @@ -521,7 +525,7 @@ def find_data_dependencies(self, data_xpath):
try:
data_node = self._find_data_node(data_xpath)
except Exception as e:
print("find_data_dependencies(): Failed to find data node from xpath: {}".format(data_xapth))
self.sysLog(msg="find_data_dependencies(): Failed to find data node from xpath: {}".format(data_xapth), debug=syslog.LOG_ERR, doPrint=True)
return ref_list

try:
Expand All @@ -538,7 +542,7 @@ def find_data_dependencies(self, data_xpath):
if value == casted.value_str():
ref_list.append(data_set.path())
except Exception as e:
print('Failed to find node or dependencies for {}'.format(data_xpath))
self.sysLog(msg='Failed to find node or dependencies for {}'.format(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
raise SonicYangException("Failed to find node or dependencies for \
{}\n{}".format(data_xpath, str(e)))

Expand Down Expand Up @@ -598,7 +602,7 @@ def _get_data_type(self, schema_xpath):
try:
schema_node = self._find_schema_node(schema_xpath)
except Exception as e:
print("get_data_type(): Failed to find schema node from xpath: {}".format(schema_xpath))
self.sysLog(msg="get_data_type(): Failed to find schema node from xpath: {}".format(schema_xpath), debug=syslog.LOG_ERR, doPrint=True)
self.fail(e)
return None

Expand All @@ -618,7 +622,7 @@ def _get_leafref_type(self, data_xpath):
subtype = data_node.subtype()
if (subtype is not None):
if data_node.schema().subtype().type().base() != ly.LY_TYPE_LEAFREF:
print("get_leafref_type() node type for data xpath: {} is not LEAFREF".format(data_xpath))
self.sysLog(msg="get_leafref_type() node type for data xpath: {} is not LEAFREF".format(data_xpath), debug=syslog.LOG_ERR, doPrint=True)
return ly.LY_TYPE_UNKNOWN
else:
return subtype.value_type()
Expand Down
5 changes: 1 addition & 4 deletions src/sonic-yang-mgmt/sonic_yang_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ def _cropConfigDB(self, croppedFile=None):
del self.jIn[table]

if len(self.tablesWithOutYang):
print("Note: Below table(s) have no YANG models:")
for table in self.tablesWithOutYang.keys():
print(str(table), end=", ")
print()
self.sysLog(msg=f"Note: Below table(s) have no YANG models: {', '.join(self.tablesWithOutYang)}", doPrint=True)

if croppedFile:
with open(croppedFile, 'w') as f:
Expand Down