Skip to content

Commit

Permalink
tools: add config-write callbacks to nb templates
Browse files Browse the repository at this point in the history
Add the cli_show (config write) callback when emitting the
create or modify callback in the northbound template.

Signed-off-by: Mark Stapp <[email protected]>
  • Loading branch information
Mark Stapp committed Jan 5, 2024
1 parent 43331c0 commit 38a4272
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion tools/gen_northbound_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ static void __attribute__((noreturn)) usage(int status)
static struct nb_callback_info {
int operation;
bool optional;
bool need_config_write;
char return_type[32];
char return_value[32];
char arguments[128];
} nb_callbacks[] = {
{
.operation = NB_OP_CREATE,
.need_config_write = true,
.return_type = "int ",
.return_value = "NB_OK",
.arguments = "struct nb_cb_create_args *args",
},
{
.operation = NB_OP_MODIFY,
.need_config_write = true,
.return_type = "int ",
.return_value = "NB_OK",
.arguments = "struct nb_cb_modify_args *args",
Expand Down Expand Up @@ -96,6 +99,16 @@ static struct nb_callback_info {
},
};

/*
* Special-purpose info block for the cli-config-write callback. This
* is different enough from the config-oriented callbacks that it doesn't
* really fit in the array above.
*/
static struct nb_callback_info nb_config_write = {
.return_type = "void ",
.arguments = "struct vty *vty, const struct lyd_node *dnode, bool show_defaults",
};

static void replace_hyphens_by_underscores(char *str)
{
char *p;
Expand Down Expand Up @@ -134,14 +147,51 @@ static void generate_callback_name(const struct lysc_node *snode,
replace_hyphens_by_underscores(buffer);
}

static void generate_config_write_cb_name(const struct lysc_node *snode,
char *buffer, size_t size)
{
struct list *snodes;
struct listnode *ln;

buffer[0] = '\0';
snprintf(buffer, size, "cli_write");

snodes = list_new();
for (; snode; snode = snode->parent) {
/* Skip schema-only snodes. */
if (CHECK_FLAG(snode->nodetype, LYS_USES | LYS_CHOICE | LYS_CASE
| LYS_INPUT
| LYS_OUTPUT))
continue;

listnode_add_head(snodes, (void *)snode);
}

for (ALL_LIST_ELEMENTS_RO(snodes, ln, snode)) {
strlcat(buffer, "_", size);
strlcat(buffer, snode->name, size);
}
list_delete(&snodes);

replace_hyphens_by_underscores(buffer);
}

static void generate_prototype(const struct nb_callback_info *ncinfo,
const char *cb_name)
{
printf("%s%s(%s);\n", ncinfo->return_type, cb_name, ncinfo->arguments);
}

static void generate_config_write_prototype(const struct nb_callback_info *ncinfo,
const char *cb_name)
{
printf("%s%s(%s);\n", ncinfo->return_type, cb_name, ncinfo->arguments);
}

static int generate_prototypes(const struct lysc_node *snode, void *arg)
{
bool need_config_write = true;

switch (snode->nodetype) {
case LYS_CONTAINER:
case LYS_LEAF:
Expand All @@ -165,6 +215,15 @@ static int generate_prototypes(const struct lysc_node *snode, void *arg)
generate_callback_name(snode, cb->operation, cb_name,
sizeof(cb_name));
generate_prototype(cb, cb_name);

if (cb->need_config_write && need_config_write) {
generate_config_write_cb_name(snode, cb_name,
sizeof(cb_name));
generate_config_write_prototype(&nb_config_write,
cb_name);

need_config_write = false;
}
}

return YANG_ITER_CONTINUE;
Expand Down Expand Up @@ -200,9 +259,22 @@ static void generate_callback(const struct nb_callback_info *ncinfo,
printf("\treturn %s;\n}\n\n", ncinfo->return_value);
}

static void generate_config_write_callback(const struct nb_callback_info *ncinfo,
const char *cb_name)
{
printf("%s%s%s(%s)\n{\n", static_cbs ? "static " : "",
ncinfo->return_type, cb_name, ncinfo->arguments);

/* Add a comment, since these callbacks may not all be needed. */
printf("\t/* TODO: this cli callback is optional; the cli output may not need to be done at each node. */\n");

printf("}\n\n");
}

static int generate_callbacks(const struct lysc_node *snode, void *arg)
{
bool first = true;
bool need_config_write = true;

switch (snode->nodetype) {
case LYS_CONTAINER:
Expand Down Expand Up @@ -240,6 +312,15 @@ static int generate_callbacks(const struct lysc_node *snode, void *arg)
generate_callback_name(snode, cb->operation, cb_name,
sizeof(cb_name));
generate_callback(cb, cb_name);

if (cb->need_config_write && need_config_write) {
generate_config_write_cb_name(snode, cb_name,
sizeof(cb_name));
generate_config_write_callback(&nb_config_write,
cb_name);

need_config_write = false;
}
}

return YANG_ITER_CONTINUE;
Expand All @@ -248,6 +329,7 @@ static int generate_callbacks(const struct lysc_node *snode, void *arg)
static int generate_nb_nodes(const struct lysc_node *snode, void *arg)
{
bool first = true;
bool need_config_write = true;

switch (snode->nodetype) {
case LYS_CONTAINER:
Expand Down Expand Up @@ -286,6 +368,14 @@ static int generate_nb_nodes(const struct lysc_node *snode, void *arg)
sizeof(cb_name));
printf("\t\t\t\t.%s = %s,\n", nb_operation_name(cb->operation),
cb_name);

if (cb->need_config_write && need_config_write) {
generate_config_write_cb_name(snode, cb_name,
sizeof(cb_name));
printf("\t\t\t\t.cli_show = %s,\n", cb_name);

need_config_write = false;
}
}

if (!first) {
Expand Down Expand Up @@ -355,6 +445,9 @@ int main(int argc, char *argv[])
/* Create a nb_node for all YANG schema nodes. */
nb_nodes_create();

/* Emit bare-bones license line. */
printf("// SPDX-License-Identifier: GPL-2.0-or-later\n\n");

/* Generate callback prototypes. */
if (!static_cbs) {
printf("/* prototypes */\n");
Expand All @@ -371,7 +464,7 @@ int main(int argc, char *argv[])

/* Generate frr_yang_module_info array. */
printf("/* clang-format off */\n"
"const struct frr_yang_module_info %s_info = {\n"
"const struct frr_yang_module_info %s_nb_info = {\n"
"\t.name = \"%s\",\n"
"\t.nodes = {\n",
module_name_underscores, module->name);
Expand Down

0 comments on commit 38a4272

Please sign in to comment.