]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
sysrepo-lua: dumping schema before module initialization for debugging
authorVasek Sraier <git@vakabus.cz>
Sun, 12 Apr 2020 10:05:48 +0000 (12:05 +0200)
committerAleš <ales.mrazek@nic.cz>
Mon, 20 Jul 2020 14:54:40 +0000 (16:54 +0200)
modules/sysrepo-lua/cbindings/sysrepo_clib.c
modules/sysrepo-lua/cbindings/sysrepo_clib.h
modules/sysrepo-lua/model.lua

index 9a0cdec0350c12fa916dba76cfa4d100d7b15621..71cd4b040f345be0df293e6da43c2c3bbf1fa1bc 100644 (file)
@@ -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;
 }
index 9305c659def7326336c9b20d94eced14713687ea..e771a11dee0a596c5a26293f12db6866d54225ed 100644 (file)
@@ -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);
index 387701cad5a5cdc7c95317da2aabd18dc0e553d4..ad11500c913a00f5a42df6315d13cfb6ef9f4a4a 100644 (file)
@@ -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