lib: add yang function for counting data nodes

Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
This commit is contained in:
Igor Ryzhov 2024-01-26 16:54:36 +02:00 committed by Christian Hopps
parent 63ca751c11
commit e39b60bac1
2 changed files with 39 additions and 0 deletions

View 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;

View 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.