]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: northbound: add new get() callback to add lyd_node direclty
authorChristian Hopps <chopps@labn.net>
Sun, 24 Nov 2024 07:56:22 +0000 (02:56 -0500)
committerChristian Hopps <chopps@labn.net>
Tue, 7 Jan 2025 10:33:28 +0000 (05:33 -0500)
This allows eliminating the superfluous yang_data object (which
is getting created used to call lyd_new_term then deleted). Instead
just call lyd_new_term() in the callback directly.

Signed-off-by: Christian Hopps <chopps@labn.net>
lib/northbound.c
lib/northbound.h
lib/northbound_oper.c

index a385cc9ece9453094b7c52668bd3100bb3045ba7..c67ed924a95a5c976ba9f6d73d9d76f1f09c7ac4 100644 (file)
@@ -273,9 +273,11 @@ static unsigned int nb_node_validate_cbs(const struct nb_node *nb_node)
        error += nb_node_validate_cb(nb_node, NB_CB_APPLY_FINISH,
                                     !!nb_node->cbs.apply_finish, true);
        error += nb_node_validate_cb(nb_node, NB_CB_GET_ELEM,
-                                    !!nb_node->cbs.get_elem, false);
+                                    (nb_node->cbs.get_elem || nb_node->cbs.get), false);
        error += nb_node_validate_cb(nb_node, NB_CB_GET_NEXT,
-                                    !!nb_node->cbs.get_next, false);
+                                    (nb_node->cbs.get_next ||
+                                     (nb_node->snode->nodetype == LYS_LEAFLIST && nb_node->cbs.get)),
+                                    false);
        error += nb_node_validate_cb(nb_node, NB_CB_GET_KEYS,
                                     !!nb_node->cbs.get_keys, false);
        error += nb_node_validate_cb(nb_node, NB_CB_LOOKUP_ENTRY,
index 97a1d31e5792ba656335a5ca91fc59383581df69..42f763c3efabb8d49e1fe8c8c92a9895fc6e0919 100644 (file)
@@ -21,6 +21,7 @@ extern "C" {
 /* Forward declaration(s). */
 struct vty;
 struct debug;
+struct nb_node;
 
 struct nb_yang_xpath_tag {
        uint32_t ns;
@@ -102,6 +103,20 @@ enum nb_cb_operation {
        NB_CB_NOTIFY,
 };
 
+/* Northbound error codes. */
+enum nb_error {
+       NB_OK = 0,
+       NB_ERR,
+       NB_ERR_NO_CHANGES,
+       NB_ERR_NOT_FOUND,
+       NB_ERR_EXISTS,
+       NB_ERR_LOCKED,
+       NB_ERR_VALIDATION,
+       NB_ERR_RESOURCE,
+       NB_ERR_INCONSISTENCY,
+       NB_YIELD,
+};
+
 union nb_resource {
        int fd;
        void *ptr;
@@ -425,6 +440,25 @@ struct nb_callbacks {
         */
        void (*apply_finish)(struct nb_cb_apply_finish_args *args);
 
+       /*
+        * Operational data callback (new direct tree add method).
+        *
+        * The callback function should create a new lyd_node (leaf) or
+        * lyd_node's (leaf list) for the value and attach to parent.
+        *
+        * nb_node
+        *    The node representing the leaf or leaf list
+        * list_entry
+        *    List entry from get_next (or NULL).
+        * parent
+        *    The parent lyd_node to attach the leaf data to.
+        *
+        * Returns:
+        *    Returns an nb_error if the data could not be added to the tree.
+        */
+       enum nb_error (*get)(const struct nb_node *nb_node, const void *list_entry,
+                            struct lyd_node *parent);
+
        /*
         * Operational data callback.
         *
@@ -672,20 +706,6 @@ struct frr_yang_module_info {
 #endif
 };
 
-/* Northbound error codes. */
-enum nb_error {
-       NB_OK = 0,
-       NB_ERR,
-       NB_ERR_NO_CHANGES,
-       NB_ERR_NOT_FOUND,
-       NB_ERR_EXISTS,
-       NB_ERR_LOCKED,
-       NB_ERR_VALIDATION,
-       NB_ERR_RESOURCE,
-       NB_ERR_INCONSISTENCY,
-       NB_YIELD,
-};
-
 /* Default priority. */
 #define NB_DFLT_PRIORITY (UINT32_MAX / 2)
 
@@ -814,8 +834,9 @@ extern struct debug nb_dbg_libyang;
 extern struct nb_config *running_config;
 
 /* Wrappers for the northbound callbacks. */
-extern struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node,
-                                             const char *xpath,
+extern struct yang_data *nb_callback_has_new_get_elem(const struct nb_node *nb_node);
+
+extern struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node, const char *xpath,
                                              const void *list_entry);
 extern const void *nb_callback_get_next(const struct nb_node *nb_node,
                                        const void *parent_list_entry,
index a3ff3607800e31edacda44dd7c8c140a1b87bfce..ce529cd26b77cf5813b372a5ff1519ccbffc2982 100644 (file)
@@ -584,6 +584,10 @@ static enum nb_error nb_op_iter_leaf(struct nb_op_yield_state *ys,
        if (lysc_is_key(snode))
                return NB_OK;
 
+       /* Check for new simple get */
+       if (nb_node->cbs.get)
+               return nb_node->cbs.get(nb_node, ni->list_entry, ni->inner);
+
        data = nb_callback_get_elem(nb_node, xpath, ni->list_entry);
        if (data == NULL)
                return NB_OK;
@@ -617,6 +621,10 @@ static enum nb_error nb_op_iter_leaflist(struct nb_op_yield_state *ys,
        if (CHECK_FLAG(snode->flags, LYS_CONFIG_W))
                return NB_OK;
 
+       /* Check for new simple get */
+       if (nb_node->cbs.get)
+               return nb_node->cbs.get(nb_node, ni->list_entry, ni->inner);
+
        do {
                struct yang_data *data;