forked from Mirror/frr
lib: introduce the yang_dnode_iterate helper
Implement helper function that iterates over data nodes that satisfy XPath query. Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
This commit is contained in:
parent
ff94358e54
commit
7b611145c8
26
lib/yang.c
26
lib/yang.c
|
@ -448,6 +448,32 @@ bool yang_dnode_exists(const struct lyd_node *dnode, const char *xpath_fmt, ...)
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg,
|
||||||
|
const struct lyd_node *dnode, const char *xpath_fmt,
|
||||||
|
...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
char xpath[XPATH_MAXLEN];
|
||||||
|
struct ly_set *set;
|
||||||
|
|
||||||
|
va_start(ap, xpath_fmt);
|
||||||
|
vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
set = lyd_find_path(dnode, xpath);
|
||||||
|
assert(set);
|
||||||
|
for (unsigned int i = 0; i < set->number; i++) {
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
dnode = set->set.d[i];
|
||||||
|
ret = (*cb)(dnode, arg);
|
||||||
|
if (ret == YANG_ITER_STOP)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ly_set_free(set);
|
||||||
|
}
|
||||||
|
|
||||||
bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath_fmt,
|
bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath_fmt,
|
||||||
...)
|
...)
|
||||||
{
|
{
|
||||||
|
|
22
lib/yang.h
22
lib/yang.h
|
@ -107,6 +107,9 @@ enum yang_iter_flags {
|
||||||
/* Callback used by the yang_snodes_iterate_*() family of functions. */
|
/* Callback used by the yang_snodes_iterate_*() family of functions. */
|
||||||
typedef int (*yang_iterate_cb)(const struct lys_node *snode, void *arg);
|
typedef int (*yang_iterate_cb)(const struct lys_node *snode, void *arg);
|
||||||
|
|
||||||
|
/* Callback used by the yang_dnode_iterate() function. */
|
||||||
|
typedef int (*yang_dnode_iter_cb)(const struct lyd_node *dnode, void *arg);
|
||||||
|
|
||||||
/* Return values of the 'yang_iterate_cb' callback. */
|
/* Return values of the 'yang_iterate_cb' callback. */
|
||||||
#define YANG_ITER_CONTINUE 0
|
#define YANG_ITER_CONTINUE 0
|
||||||
#define YANG_ITER_STOP -1
|
#define YANG_ITER_STOP -1
|
||||||
|
@ -357,6 +360,25 @@ extern struct lyd_node *yang_dnode_get(const struct lyd_node *dnode,
|
||||||
extern bool yang_dnode_exists(const struct lyd_node *dnode,
|
extern bool yang_dnode_exists(const struct lyd_node *dnode,
|
||||||
const char *xpath_fmt, ...);
|
const char *xpath_fmt, ...);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Iterate over all libyang data nodes that satisfy an XPath query.
|
||||||
|
*
|
||||||
|
* cb
|
||||||
|
* Function to call with each data node.
|
||||||
|
*
|
||||||
|
* arg
|
||||||
|
* Arbitrary argument passed as the second parameter in each call to 'cb'.
|
||||||
|
*
|
||||||
|
* dnode
|
||||||
|
* Base libyang data node to operate on.
|
||||||
|
*
|
||||||
|
* xpath_fmt
|
||||||
|
* XPath expression (absolute or relative).
|
||||||
|
*/
|
||||||
|
void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg,
|
||||||
|
const struct lyd_node *dnode, const char *xpath_fmt,
|
||||||
|
...);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if the libyang data node contains a default value. Non-presence
|
* Check if the libyang data node contains a default value. Non-presence
|
||||||
* containers are assumed to always contain a default value.
|
* containers are assumed to always contain a default value.
|
||||||
|
|
Loading…
Reference in a new issue