From: Vasek Sraier Date: Sun, 12 Apr 2020 10:05:48 +0000 (+0200) Subject: sysrepo-lua: dumping schema before module initialization for debugging X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bdcb75b6de78276bddff2c269acabf94e37997ec;p=thirdparty%2Fknot-resolver.git sysrepo-lua: dumping schema before module initialization for debugging --- diff --git a/modules/sysrepo-lua/cbindings/sysrepo_clib.c b/modules/sysrepo-lua/cbindings/sysrepo_clib.c index 9a0cdec03..71cd4b040 100644 --- a/modules/sysrepo-lua/cbindings/sysrepo_clib.c +++ b/modules/sysrepo-lua/cbindings/sysrepo_clib.c @@ -229,24 +229,33 @@ int sysrepo_deinit() } KR_EXPORT struct lyd_node* node_child_first(struct lyd_node* parent) { - assert( - parent->schema->nodetype == LYS_CONTAINER || - parent->schema->nodetype == LYS_LIST || - parent->schema->nodetype == LYS_CHOICE - ); + assert(parent != NULL); + + /* return null when the node does not contain children */ + if( + parent->schema->nodetype != LYS_CONTAINER && + parent->schema->nodetype != LYS_LIST && + parent->schema->nodetype != LYS_CHOICE + ) { + return NULL; + } return parent->child; } KR_EXPORT struct lyd_node* node_child_next(struct lyd_node* prev_child) { + assert(prev_child != NULL); return prev_child->next; } KR_EXPORT const char* node_get_name(struct lyd_node* node) { + assert(node != NULL); return node->schema->name; } KR_EXPORT const char* node_get_value_str(struct lyd_node* node) { + assert(node != NULL); + assert( node->schema->nodetype == LYS_LEAF || node->schema->nodetype == LYS_LEAFLIST @@ -268,11 +277,15 @@ KR_EXPORT const struct lys_module* schema_get_module(const struct lys_node* sche KR_EXPORT const struct lys_node* schema_child_first(const struct lys_node* parent) { assert(parent != NULL); - assert( - parent->nodetype == LYS_CONTAINER || - parent->nodetype == LYS_LIST || - parent->nodetype == LYS_CHOICE - ); + + /* if the node does not contain children, return NULL */ + if ( + parent->nodetype != LYS_CONTAINER && + parent->nodetype != LYS_LIST && + parent->nodetype != LYS_CHOICE + ) { + return NULL; + } return parent->child; } diff --git a/modules/sysrepo-lua/cbindings/sysrepo_clib.h b/modules/sysrepo-lua/cbindings/sysrepo_clib.h index 9305c659d..e771a11de 100644 --- a/modules/sysrepo-lua/cbindings/sysrepo_clib.h +++ b/modules/sysrepo-lua/cbindings/sysrepo_clib.h @@ -50,7 +50,7 @@ typedef struct lyd_node* (*read_conf_f)(); KR_EXPORT int sysrepo_init(apply_conf_f apply_conf_callback, read_conf_f read_conf_callback); KR_EXPORT int sysrepo_deinit(void); -/** Given a libyang node, returns it's first child */ +/** Given a libyang node, returns it's first child (or NULL if there aren't any) */ KR_EXPORT struct lyd_node* node_child_first(struct lyd_node* parent); /** Given a libyang node, return next sibling or NULL if there isn't any */ KR_EXPORT struct lyd_node* node_child_next(struct lyd_node* prev_child); @@ -64,7 +64,7 @@ KR_EXPORT struct lyd_node* node_new_leaf(struct lyd_node* parent, const struct l KR_EXPORT struct lyd_node* node_new_container(struct lyd_node* parent, const struct lys_module* module, const char* name); /** Returns module given a schema node */ KR_EXPORT const struct lys_module* schema_get_module(const struct lys_node* schema); -/** Given a libyang schema node, returns it's first child */ +/** Given a libyang schema node, returns it's first child (or NULL if there aren't any) */ KR_EXPORT const struct lys_node* schema_child_first(const struct lys_node* parent); /** Given a libyang schema node, return next sibling or NULL if there isn't any */ KR_EXPORT const struct lys_node* schema_child_next(const struct lys_node* prev_child); diff --git a/modules/sysrepo-lua/model.lua b/modules/sysrepo-lua/model.lua index 387701cad..ad11500c9 100644 --- a/modules/sysrepo-lua/model.lua +++ b/modules/sysrepo-lua/model.lua @@ -278,7 +278,7 @@ local function ContainerNode(name, container_model, hooks) -- apply to all children for _,v in ipairs(container_model) do -- all children have node property, so we initialize just that - v.node:initialize_schema(child_schema_nodes[v.name]) + v.node:initialize_schema(child_schema_nodes[v.node.name]) end end @@ -537,7 +537,26 @@ return function(clib_binding) local initialized_schema = false local function init_schema() + local depth = 0 + local function print_schema_tree(schema_node) + debug.log("{}{}", string.rep(" ", depth), ffi.string(clib().schema_get_name(schema_node))) + depth = depth + 1 + local children = Helpers.get_schema_children_table(schema_node) + for _,node in pairs(children) do + print_schema_tree(node) + end + depth = depth - 1 + end + + if not initialized_schema then + -- dump schema tree for debugging purpose + debug.log("Loaded schema tree:") + debug.log("") + print_schema_tree(clib().schema_root()) + debug.log("") + debug.log("Schema tree end") + model:initialize_schema(clib().schema_root()) initialized_schema = true end