]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add yang function for counting data nodes
authorIgor Ryzhov <iryzhov@nfware.com>
Fri, 26 Jan 2024 14:54:36 +0000 (16:54 +0200)
committerChristian Hopps <chopps@labn.net>
Fri, 26 Jan 2024 17:34:23 +0000 (12:34 -0500)
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
lib/yang.c
lib/yang.h

index 7d35fb0d3dde04397b02230839f6c096a925278a..ed855c84984605dfb197490682c7d8af00538ad4 100644 (file)
@@ -508,6 +508,30 @@ void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg,
        ly_set_free(set, NULL);
 }
 
+uint32_t yang_dnode_count(const struct lyd_node *dnode, const char *xpath_fmt,
+                         ...)
+{
+       va_list ap;
+       char xpath[XPATH_MAXLEN];
+       struct ly_set *set;
+       uint32_t count;
+
+       va_start(ap, xpath_fmt);
+       vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap);
+       va_end(ap);
+
+       if (lyd_find_xpath(dnode, xpath, &set)) {
+               assert(0);
+               return 0;
+       }
+
+       count = set->count;
+
+       ly_set_free(set, NULL);
+
+       return count;
+}
+
 bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath)
 {
        const struct lysc_node *snode;
index dbb7f7163bd2688d4b20a6a585e593b861165556..1235125f26bd06ff1853e598f58b101ec0367a18 100644 (file)
@@ -420,6 +420,21 @@ void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg,
                        const struct lyd_node *dnode, const char *xpath_fmt,
                        ...) PRINTFRR(4, 5);
 
+/*
+ * Count the number of data nodes that satisfy an XPath query.
+ *
+ * dnode
+ *    Base libyang data node to operate on.
+ *
+ * xpath_fmt
+ *    XPath expression (absolute or relative).
+ *
+ * ...
+ *    any parameters for xpath_fmt.
+ */
+uint32_t yang_dnode_count(const struct lyd_node *dnode, const char *xpath_fmt,
+                         ...) PRINTFRR(2, 3);
+
 /*
  * Check if the libyang data node contains a default value. Non-presence
  * containers are assumed to always contain a default value.