lib: rework the yang schema node iteration functions

* Rename yang_snodes_iterate() to yang_snodes_iterate_subtree() and
  expose it in the public API.
* Rename yang_module_snodes_iterate() to yang_snodes_iterate_module().
* Rename yang_all_snodes_iterate() to yang_snodes_iterate_all().
* Make it possible to stop the iteration at any time by returning
  YANG_ITER_STOP in the iteration callbacks.
* Make the iteration callbacks accept only one user argument and not
  two.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
This commit is contained in:
Renato Westphal 2018-11-02 21:56:26 -02:00
parent a1b5f469e7
commit e0ccfad220
8 changed files with 188 additions and 124 deletions

View File

@ -51,7 +51,7 @@ static int nb_transaction_process(enum nb_event event,
struct nb_transaction *transaction);
static void nb_transaction_apply_finish(struct nb_transaction *transaction);
static void nb_node_new_cb(const struct lys_node *snode, void *arg1, void *arg2)
static int nb_node_new_cb(const struct lys_node *snode, void *arg)
{
struct nb_node *nb_node;
struct lys_node *sparent, *sparent_list;
@ -73,15 +73,19 @@ static void nb_node_new_cb(const struct lys_node *snode, void *arg1, void *arg2)
*/
nb_node->snode = snode;
lys_set_private(snode, nb_node);
return YANG_ITER_CONTINUE;
}
static void nb_node_del_cb(const struct lys_node *snode, void *arg1, void *arg2)
static int nb_node_del_cb(const struct lys_node *snode, void *arg)
{
struct nb_node *nb_node;
nb_node = snode->priv;
lys_set_private(snode, NULL);
XFREE(MTYPE_NB_NODE, nb_node);
return YANG_ITER_CONTINUE;
}
struct nb_node *nb_node_find(const char *xpath)
@ -170,15 +174,16 @@ static unsigned int nb_node_validate_priority(const struct nb_node *nb_node)
return 0;
}
static void nb_node_validate(const struct lys_node *snode, void *arg1,
void *arg2)
static int nb_node_validate(const struct lys_node *snode, void *arg)
{
struct nb_node *nb_node = snode->priv;
unsigned int *errors = arg1;
unsigned int *errors = arg;
/* Validate callbacks and priority. */
*errors += nb_node_validate_cbs(nb_node);
*errors += nb_node_validate_priority(nb_node);
return YANG_ITER_CONTINUE;
}
struct nb_config *nb_config_new(struct lyd_node *dnode)
@ -1165,14 +1170,14 @@ void nb_init(const struct frr_yang_module_info *modules[], size_t nmodules)
yang_module_load(modules[i]->name);
/* Create a nb_node for all YANG schema nodes. */
yang_all_snodes_iterate(nb_node_new_cb, 0, NULL, NULL);
yang_snodes_iterate_all(nb_node_new_cb, 0, NULL);
/* Load northbound callbacks. */
for (size_t i = 0; i < nmodules; i++)
nb_load_callbacks(modules[i]);
/* Validate northbound callbacks. */
yang_all_snodes_iterate(nb_node_validate, 0, &errors, NULL);
yang_snodes_iterate_all(nb_node_validate, 0, &errors);
if (errors > 0) {
flog_err(
EC_LIB_NB_CBS_VALIDATION,
@ -1200,7 +1205,7 @@ void nb_terminate(void)
nb_cli_terminate();
/* Delete all nb_node's from all YANG modules. */
yang_all_snodes_iterate(nb_node_del_cb, 0, NULL, NULL);
yang_snodes_iterate_all(nb_node_del_cb, 0, NULL);
/* Delete the running configuration. */
nb_config_free(running_config);

View File

@ -977,17 +977,16 @@ static int frr_confd_dp_read(struct thread *thread)
return 0;
}
static void frr_confd_subscribe_state(const struct lys_node *snode, void *arg1,
void *arg2)
static int frr_confd_subscribe_state(const struct lys_node *snode, void *arg)
{
struct nb_node *nb_node = snode->priv;
struct confd_data_cbs *data_cbs = arg1;
struct confd_data_cbs *data_cbs = arg;
if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
return;
return YANG_ITER_CONTINUE;
/* We only need to subscribe to the root of the state subtrees. */
if (snode->parent && CHECK_FLAG(snode->parent->flags, LYS_CONFIG_R))
return;
return YANG_ITER_CONTINUE;
if (debug_northbound)
zlog_debug("%s: providing data to '%s' (callpoint %s)",
@ -996,6 +995,8 @@ static void frr_confd_subscribe_state(const struct lys_node *snode, void *arg1,
strlcpy(data_cbs->callpoint, snode->name, sizeof(data_cbs->callpoint));
if (confd_register_data_cb(dctx, data_cbs) != CONFD_OK)
flog_err_confd("confd_register_data_cb");
return YANG_ITER_CONTINUE;
}
static int frr_confd_init_dp(const char *program_name)
@ -1068,7 +1069,7 @@ static int frr_confd_init_dp(const char *program_name)
* Iterate over all loaded YANG modules and subscribe to the paths
* referent to state data.
*/
yang_all_snodes_iterate(frr_confd_subscribe_state, 0, &data_cbs, NULL);
yang_snodes_iterate_all(frr_confd_subscribe_state, 0, &data_cbs);
/* Register notification stream. */
memset(&ncbs, 0, sizeof(ncbs));
@ -1132,12 +1133,14 @@ static void frr_confd_finish_dp(void)
/* ------------ Main ------------ */
static void frr_confd_calculate_snode_hash(const struct lys_node *snode,
void *arg1, void *arg2)
static int frr_confd_calculate_snode_hash(const struct lys_node *snode,
void *arg)
{
struct nb_node *nb_node = snode->priv;
nb_node->confd_hash = confd_str2hash(snode->name);
return YANG_ITER_CONTINUE;
}
static int frr_confd_init(const char *program_name)
@ -1168,7 +1171,7 @@ static int frr_confd_init(const char *program_name)
goto error;
}
yang_all_snodes_iterate(frr_confd_calculate_snode_hash, 0, NULL, NULL);
yang_snodes_iterate_all(frr_confd_calculate_snode_hash, 0, NULL);
hook_register(nb_notification_send, frr_confd_notification_send);

View File

@ -732,18 +732,17 @@ static void frr_sr_subscribe_config(struct yang_module *module)
sr_strerror(ret));
}
static void frr_sr_subscribe_state(const struct lys_node *snode, void *arg1,
void *arg2)
static int frr_sr_subscribe_state(const struct lys_node *snode, void *arg)
{
struct yang_module *module = arg1;
struct yang_module *module = arg;
struct nb_node *nb_node;
int ret;
if (!CHECK_FLAG(snode->flags, LYS_CONFIG_R))
return;
return YANG_ITER_CONTINUE;
/* We only need to subscribe to the root of the state subtrees. */
if (snode->parent && CHECK_FLAG(snode->parent->flags, LYS_CONFIG_R))
return;
return YANG_ITER_CONTINUE;
nb_node = snode->priv;
if (debug_northbound)
@ -756,17 +755,18 @@ static void frr_sr_subscribe_state(const struct lys_node *snode, void *arg1,
if (ret != SR_ERR_OK)
flog_err(EC_LIB_LIBSYSREPO, "sr_dp_get_items_subscribe(): %s",
sr_strerror(ret));
return YANG_ITER_CONTINUE;
}
static void frr_sr_subscribe_rpc(const struct lys_node *snode, void *arg1,
void *arg2)
static int frr_sr_subscribe_rpc(const struct lys_node *snode, void *arg)
{
struct yang_module *module = arg1;
struct yang_module *module = arg;
struct nb_node *nb_node;
int ret;
if (snode->nodetype != LYS_RPC)
return;
return YANG_ITER_CONTINUE;
nb_node = snode->priv;
if (debug_northbound)
@ -779,17 +779,18 @@ static void frr_sr_subscribe_rpc(const struct lys_node *snode, void *arg1,
if (ret != SR_ERR_OK)
flog_err(EC_LIB_LIBSYSREPO, "sr_rpc_subscribe(): %s",
sr_strerror(ret));
return YANG_ITER_CONTINUE;
}
static void frr_sr_subscribe_action(const struct lys_node *snode, void *arg1,
void *arg2)
static int frr_sr_subscribe_action(const struct lys_node *snode, void *arg)
{
struct yang_module *module = arg1;
struct yang_module *module = arg;
struct nb_node *nb_node;
int ret;
if (snode->nodetype != LYS_ACTION)
return;
return YANG_ITER_CONTINUE;
nb_node = snode->priv;
if (debug_northbound)
@ -802,6 +803,8 @@ static void frr_sr_subscribe_action(const struct lys_node *snode, void *arg1,
if (ret != SR_ERR_OK)
flog_err(EC_LIB_LIBSYSREPO, "sr_action_subscribe(): %s",
sr_strerror(ret));
return YANG_ITER_CONTINUE;
}
/* FRR's Sysrepo initialization. */
@ -839,12 +842,12 @@ static int frr_sr_init(const char *program_name)
/* Perform subscriptions. */
RB_FOREACH (module, yang_modules, &yang_modules) {
frr_sr_subscribe_config(module);
yang_module_snodes_iterate(module->info, frr_sr_subscribe_state,
0, module, NULL);
yang_module_snodes_iterate(module->info, frr_sr_subscribe_rpc,
0, module, NULL);
yang_module_snodes_iterate(
module->info, frr_sr_subscribe_action, 0, module, NULL);
yang_snodes_iterate_module(module->info, frr_sr_subscribe_state,
0, module);
yang_snodes_iterate_module(module->info, frr_sr_subscribe_rpc,
0, module);
yang_snodes_iterate_module(module->info,
frr_sr_subscribe_action, 0, module);
}
hook_register(nb_notification_send, frr_sr_notification_send);

View File

@ -127,16 +127,11 @@ struct yang_module *yang_module_find(const char *module_name)
return RB_FIND(yang_modules, &yang_modules, &s);
}
/*
* Helper function for yang_module_snodes_iterate() and
* yang_all_snodes_iterate(). This is a recursive function.
*/
static void yang_snodes_iterate(const struct lys_node *snode,
void (*func)(const struct lys_node *, void *,
void *),
uint16_t flags, void *arg1, void *arg2)
int yang_snodes_iterate_subtree(const struct lys_node *snode,
yang_iterate_cb cb, uint16_t flags, void *arg)
{
struct lys_node *child;
int ret = YANG_ITER_CONTINUE;
if (CHECK_FLAG(flags, YANG_ITER_FILTER_IMPLICIT)) {
switch (snode->nodetype) {
@ -173,7 +168,7 @@ static void yang_snodes_iterate(const struct lys_node *snode,
break;
case LYS_GROUPING:
/* Return since we're not interested in the grouping subtree. */
return;
return YANG_ITER_CONTINUE;
case LYS_USES:
case LYS_AUGMENT:
/* Always ignore nodes of these types. */
@ -187,7 +182,9 @@ static void yang_snodes_iterate(const struct lys_node *snode,
break;
}
(*func)(snode, arg1, arg2);
ret = (*cb)(snode, arg);
if (ret == YANG_ITER_STOP)
return ret;
next:
/*
@ -195,42 +192,55 @@ next:
* access snode->child is undefined behavior.
*/
if (CHECK_FLAG(snode->nodetype, LYS_LEAF | LYS_LEAFLIST))
return;
return YANG_ITER_CONTINUE;
LY_TREE_FOR (snode->child, child) {
if (child->parent != snode)
continue;
yang_snodes_iterate(child, func, flags, arg1, arg2);
ret = yang_snodes_iterate_subtree(child, cb, flags, arg);
if (ret == YANG_ITER_STOP)
return ret;
}
return ret;
}
void yang_module_snodes_iterate(const struct lys_module *module,
void (*func)(const struct lys_node *, void *,
void *),
uint16_t flags, void *arg1, void *arg2)
int yang_snodes_iterate_module(const struct lys_module *module,
yang_iterate_cb cb, uint16_t flags, void *arg)
{
struct lys_node *snode;
int ret = YANG_ITER_CONTINUE;
LY_TREE_FOR (module->data, snode) {
yang_snodes_iterate(snode, func, flags, arg1, arg2);
ret = yang_snodes_iterate_subtree(snode, cb, flags, arg);
if (ret == YANG_ITER_STOP)
return ret;
}
for (uint8_t i = 0; i < module->augment_size; i++) {
yang_snodes_iterate(
(const struct lys_node *)&module->augment[i], func,
flags, arg1, arg2);
ret = yang_snodes_iterate_subtree(
(const struct lys_node *)&module->augment[i], cb, flags,
arg);
if (ret == YANG_ITER_STOP)
return ret;
}
return ret;
}
void yang_all_snodes_iterate(void (*func)(const struct lys_node *, void *,
void *),
uint16_t flags, void *arg1, void *arg2)
int yang_snodes_iterate_all(yang_iterate_cb cb, uint16_t flags, void *arg)
{
struct yang_module *module;
int ret = YANG_ITER_CONTINUE;
RB_FOREACH (module, yang_modules, &yang_modules)
yang_module_snodes_iterate(module->info, func, flags, arg1,
arg2);
RB_FOREACH (module, yang_modules, &yang_modules) {
ret = yang_snodes_iterate_module(module->info, cb, flags, arg);
if (ret == YANG_ITER_STOP)
return ret;
}
return ret;
}
void yang_snode_get_path(const struct lys_node *snode, enum yang_path_type type,

View File

@ -86,14 +86,26 @@ enum yang_path_type {
YANG_PATH_DATA,
};
/* Filter non-presence containers. */
#define YANG_ITER_FILTER_NPCONTAINERS 0x0001
/* Filter list keys (leafs). */
#define YANG_ITER_FILTER_LIST_KEYS 0x0002
/* Filter RPC input/output nodes. */
#define YANG_ITER_FILTER_INPUT_OUTPUT 0x0004
/* Filter implicitely created nodes. */
#define YANG_ITER_FILTER_IMPLICIT 0x0008
enum yang_iter_flags {
/* Filter non-presence containers. */
YANG_ITER_FILTER_NPCONTAINERS = (1<<0),
/* Filter list keys (leafs). */
YANG_ITER_FILTER_LIST_KEYS = (1<<1),
/* Filter RPC input/output nodes. */
YANG_ITER_FILTER_INPUT_OUTPUT = (1<<2),
/* Filter implicitely created nodes. */
YANG_ITER_FILTER_IMPLICIT = (1<<3),
};
/* Callback used by the yang_snodes_iterate_*() family of functions. */
typedef int (*yang_iterate_cb)(const struct lys_node *snode, void *arg);
/* Return values of the 'yang_iterate_cb' callback. */
#define YANG_ITER_CONTINUE 0
#define YANG_ITER_STOP -1
/* Global libyang context for native FRR models. */
extern struct ly_ctx *ly_native_ctx;
@ -140,47 +152,67 @@ extern struct yang_module *yang_module_find(const char *module_name);
*/
extern void yang_module_embed(struct yang_module_embed *embed);
/*
* Iterate recursively over all children of a schema node.
*
* snode
* YANG schema node to operate on.
*
* cb
* Function to call with each schema node.
*
* flags
* YANG_ITER_* flags to control how the iteration is performed.
*
* arg
* Arbitrary argument passed as the second parameter in each call to 'cb'.
*
* Returns:
* The return value of the last called callback.
*/
extern int yang_snodes_iterate_subtree(const struct lys_node *snode,
yang_iterate_cb cb, uint16_t flags,
void *arg);
/*
* Iterate over all libyang schema nodes from the given YANG module.
*
* module
* YANG module to operate on.
*
* func
* cb
* Function to call with each schema node.
*
* flags
* YANG_ITER_FILTER_* flags to specify node types that should be filtered.
* YANG_ITER_* flags to control how the iteration is performed.
*
* arg1
* Arbitrary argument passed as the second parameter in each call to 'func'.
* arg
* Arbitrary argument passed as the second parameter in each call to 'cb'.
*
* arg2
* Arbitrary argument passed as the third parameter in each call to 'func'.
* Returns:
* The return value of the last called callback.
*/
extern void yang_module_snodes_iterate(const struct lys_module *module,
void (*func)(const struct lys_node *,
void *, void *),
uint16_t flags, void *arg1, void *arg2);
extern int yang_snodes_iterate_module(const struct lys_module *module,
yang_iterate_cb cb, uint16_t flags,
void *arg);
/*
* Iterate over all libyang schema nodes from all loaded YANG modules.
*
* func
* cb
* Function to call with each schema node.
*
* flags
* YANG_ITER_FILTER_* flags to specify node types that should be filtered.
* YANG_ITER_* flags to control how the iteration is performed.
*
* arg1
* Arbitrary argument passed as the second parameter in each call to 'func'.
* arg
* Arbitrary argument passed as the second parameter in each call to 'cb'.
*
* arg2
* Arbitrary argument passed as the third parameter in each call to 'func'.
* Returns:
* The return value of the last called callback.
*/
extern void yang_all_snodes_iterate(void (*func)(const struct lys_node *,
void *, void *),
uint16_t flags, void *arg1, void *arg2);
extern int yang_snodes_iterate_all(yang_iterate_cb cb, uint16_t flags,
void *arg);
/*
* Build schema path or data path of the schema node.

View File

@ -400,24 +400,28 @@ error:
return YANG_TRANSLATE_FAILURE;
}
static void yang_translator_validate_cb(const struct lys_node *snode_custom,
void *arg1, void *arg2)
struct translator_validate_args {
struct yang_translator *translator;
unsigned int errors;
};
static int yang_translator_validate_cb(const struct lys_node *snode_custom,
void *arg)
{
struct yang_translator *translator = arg1;
unsigned int *errors = arg2;
struct translator_validate_args *args = arg;
struct yang_mapping_node *mapping;
const struct lys_node *snode_native;
const struct lys_type *stype_custom, *stype_native;
char xpath[XPATH_MAXLEN];
yang_snode_get_path(snode_custom, YANG_PATH_DATA, xpath, sizeof(xpath));
mapping = yang_mapping_lookup(translator, YANG_TRANSLATE_TO_NATIVE,
xpath);
mapping = yang_mapping_lookup(args->translator,
YANG_TRANSLATE_TO_NATIVE, xpath);
if (!mapping) {
flog_warn(EC_LIB_YANG_TRANSLATOR_LOAD,
"%s: missing mapping for \"%s\"", __func__, xpath);
*errors += 1;
return;
args->errors += 1;
return YANG_ITER_CONTINUE;
}
snode_native =
@ -433,12 +437,14 @@ static void yang_translator_validate_cb(const struct lys_node *snode_custom,
EC_LIB_YANG_TRANSLATOR_LOAD,
"%s: YANG types are incompatible (xpath: \"%s\")",
__func__, xpath);
*errors += 1;
return;
args->errors += 1;
return YANG_ITER_CONTINUE;
}
/* TODO: check if the value spaces are identical. */
}
return YANG_ITER_CONTINUE;
}
/*
@ -449,32 +455,36 @@ static unsigned int yang_translator_validate(struct yang_translator *translator)
{
struct yang_tmodule *tmodule;
struct listnode *ln;
unsigned int errors = 0;
struct translator_validate_args args;
args.translator = translator;
args.errors = 0;
for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
yang_module_snodes_iterate(
yang_snodes_iterate_module(
tmodule->module, yang_translator_validate_cb,
YANG_ITER_FILTER_NPCONTAINERS
| YANG_ITER_FILTER_LIST_KEYS
| YANG_ITER_FILTER_INPUT_OUTPUT,
translator, &errors);
&args);
}
if (errors)
if (args.errors)
flog_warn(
EC_LIB_YANG_TRANSLATOR_LOAD,
"%s: failed to validate \"%s\" module translator: %u error(s)",
__func__, translator->family, errors);
__func__, translator->family, args.errors);
return errors;
return args.errors;
}
static void yang_module_nodes_count_cb(const struct lys_node *snode, void *arg1,
void *arg2)
static int yang_module_nodes_count_cb(const struct lys_node *snode, void *arg)
{
unsigned int *total = arg1;
unsigned int *total = arg;
*total += 1;
return YANG_ITER_CONTINUE;
}
/* Calculate the number of nodes for the given module. */
@ -482,11 +492,11 @@ static unsigned int yang_module_nodes_count(const struct lys_module *module)
{
unsigned int total = 0;
yang_module_snodes_iterate(module, yang_module_nodes_count_cb,
yang_snodes_iterate_module(module, yang_module_nodes_count_cb,
YANG_ITER_FILTER_NPCONTAINERS
| YANG_ITER_FILTER_LIST_KEYS
| YANG_ITER_FILTER_INPUT_OUTPUT,
&total, NULL);
&total);
return total;
}

View File

@ -149,8 +149,7 @@ static void generate_callback_name(struct lys_node *snode,
replace_hyphens_by_underscores(buffer);
}
static void generate_callbacks(const struct lys_node *snode, void *arg1,
void *arg2)
static int generate_callbacks(const struct lys_node *snode, void *arg)
{
bool first = true;
@ -163,7 +162,7 @@ static void generate_callbacks(const struct lys_node *snode, void *arg1,
case LYS_RPC:
break;
default:
return;
return YANG_ITER_CONTINUE;
}
for (struct nb_callback_info *cb = &nb_callbacks[0];
@ -198,10 +197,11 @@ static void generate_callbacks(const struct lys_node *snode, void *arg1,
nb_callbacks[cb->operation].arguments,
nb_callbacks[cb->operation].return_value);
}
return YANG_ITER_CONTINUE;
}
static void generate_nb_nodes(const struct lys_node *snode, void *arg1,
void *arg2)
static int generate_nb_nodes(const struct lys_node *snode, void *arg)
{
bool first = true;
@ -214,7 +214,7 @@ static void generate_nb_nodes(const struct lys_node *snode, void *arg1,
case LYS_RPC:
break;
default:
return;
return YANG_ITER_CONTINUE;
}
for (struct nb_callback_info *cb = &nb_callbacks[0];
@ -245,6 +245,8 @@ static void generate_nb_nodes(const struct lys_node *snode, void *arg1,
if (!first)
printf("\t\t},\n");
return YANG_ITER_CONTINUE;
}
int main(int argc, char *argv[])
@ -274,8 +276,7 @@ int main(int argc, char *argv[])
module = yang_module_load(argv[0]);
/* Generate callback functions. */
yang_module_snodes_iterate(module->info, generate_callbacks, 0, NULL,
NULL);
yang_snodes_iterate_module(module->info, generate_callbacks, 0, NULL);
strlcpy(module_name_underscores, module->name,
sizeof(module_name_underscores));
@ -287,8 +288,7 @@ int main(int argc, char *argv[])
"\t.name = \"%s\",\n"
"\t.nodes = {\n",
module_name_underscores, module->name);
yang_module_snodes_iterate(module->info, generate_nb_nodes, 0, NULL,
NULL);
yang_snodes_iterate_module(module->info, generate_nb_nodes, 0, NULL);
printf("\t\t{\n"
"\t\t\t.xpath = NULL,\n"
"\t\t},\n");

View File

@ -32,8 +32,7 @@ static void __attribute__((noreturn)) usage(int status)
exit(status);
}
static void generate_yang_deviation(const struct lys_node *snode, void *arg1,
void *arg2)
static int generate_yang_deviation(const struct lys_node *snode, void *arg)
{
char xpath[XPATH_MAXLEN];
@ -42,6 +41,8 @@ static void generate_yang_deviation(const struct lys_node *snode, void *arg1,
printf(" deviation \"%s\" {\n", xpath);
printf(" deviate not-supported;\n");
printf(" }\n\n");
return YANG_ITER_CONTINUE;
}
int main(int argc, char *argv[])
@ -70,8 +71,8 @@ int main(int argc, char *argv[])
module = yang_module_load(argv[0]);
/* Generate deviations. */
yang_module_snodes_iterate(module->info, generate_yang_deviation,
YANG_ITER_FILTER_IMPLICIT, NULL, NULL);
yang_snodes_iterate_module(module->info, generate_yang_deviation,
YANG_ITER_FILTER_IMPLICIT, NULL);
/* Cleanup and exit. */
yang_terminate();