tools: add new-style get operation callback support

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2024-12-29 22:31:21 -05:00
parent 36f25da612
commit 153c06043b

View File

@ -15,12 +15,13 @@
#include "yang.h"
#include "northbound.h"
static bool static_cbs;
static bool f_static_cbs;
static bool f_new_cbs;
static void __attribute__((noreturn)) usage(int status)
{
extern const char *__progname;
fprintf(stderr, "usage: %s [-h] [-s] [-p path]* MODULE\n", __progname);
fprintf(stderr, "usage: %s [-h] [-n] [-s] [-p path]* MODULE\n", __progname);
exit(status);
}
@ -111,6 +112,14 @@ static struct nb_callback_info nb_config_write = {
.arguments = "struct vty *vty, const struct lyd_node *dnode, bool show_defaults",
};
static struct nb_callback_info nb_oper_get = {
.operation = NB_CB_GET_ELEM,
.return_type = "enum nb_error ",
.return_value = "NB_OK",
.arguments =
"const struct nb_node *nb_node, const void *parent_list_entry, struct lyd_node *parent",
};
static void replace_hyphens_by_underscores(char *str)
{
char *p;
@ -120,6 +129,14 @@ static void replace_hyphens_by_underscores(char *str)
*p++ = '_';
}
static const char *__operation_name(enum nb_cb_operation operation)
{
if (f_new_cbs && operation == NB_CB_GET_ELEM)
return "get";
else
return nb_cb_operation_name(operation);
}
static void generate_callback_name(const struct lysc_node *snode,
enum nb_cb_operation operation, char *buffer,
size_t size)
@ -143,7 +160,7 @@ static void generate_callback_name(const struct lysc_node *snode,
strlcat(buffer, snode->name, size);
strlcat(buffer, "_", size);
}
strlcat(buffer, nb_cb_operation_name(operation), size);
strlcat(buffer, __operation_name(operation), size);
list_delete(&snodes);
replace_hyphens_by_underscores(buffer);
@ -208,17 +225,25 @@ static int generate_prototypes(const struct lysc_node *snode, void *arg)
return YANG_ITER_CONTINUE;
}
for (struct nb_callback_info *cb = &nb_callbacks[0];
cb->operation != -1; cb++) {
for (struct nb_callback_info *cb = &nb_callbacks[0]; cb->operation != -1; cb++) {
char cb_name[BUFSIZ];
if (cb->optional
|| !nb_cb_operation_is_valid(cb->operation, snode))
continue;
if (f_new_cbs && cb->operation == NB_CB_GET_NEXT && snode->nodetype == LYS_LEAFLIST)
continue;
generate_callback_name(snode, cb->operation, cb_name,
sizeof(cb_name));
generate_prototype(cb, cb_name);
if (cb->operation == NB_CB_GET_ELEM) {
if (f_new_cbs)
generate_prototype(&nb_oper_get, cb_name);
else
generate_prototype(cb, cb_name);
}
if (cb->need_config_write && need_config_write) {
generate_config_write_cb_name(snode, cb_name,
@ -236,8 +261,8 @@ static int generate_prototypes(const struct lysc_node *snode, void *arg)
static void generate_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);
printf("%s%s%s(%s)\n{\n", f_static_cbs ? "static " : "", ncinfo->return_type, cb_name,
ncinfo->arguments);
switch (ncinfo->operation) {
case NB_CB_CREATE:
@ -266,8 +291,8 @@ static void generate_callback(const struct nb_callback_info *ncinfo,
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);
printf("%s%s%s(%s)\n{\n", f_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");
@ -313,9 +338,18 @@ static int generate_callbacks(const struct lysc_node *snode, void *arg)
first = false;
}
if (f_new_cbs && cb->operation == NB_CB_GET_NEXT && snode->nodetype == LYS_LEAFLIST)
continue;
generate_callback_name(snode, cb->operation, cb_name,
sizeof(cb_name));
generate_callback(cb, cb_name);
if (cb->operation == NB_CB_GET_ELEM) {
if (f_new_cbs)
generate_callback(&nb_oper_get, cb_name);
else
generate_callback(cb, cb_name);
}
if (cb->need_config_write && need_config_write) {
generate_config_write_cb_name(snode, cb_name,
@ -371,12 +405,13 @@ static int generate_nb_nodes(const struct lysc_node *snode, void *arg)
printf("\t\t\t.cbs = {\n");
first = false;
}
if (f_new_cbs && cb->operation == NB_CB_GET_NEXT &&
snode->nodetype == LYS_LEAFLIST)
continue;
generate_callback_name(snode, cb->operation, cb_name,
sizeof(cb_name));
printf("\t\t\t\t.%s = %s,\n",
nb_cb_operation_name(cb->operation),
cb_name);
printf("\t\t\t\t.%s = %s,\n", __operation_name(cb->operation), cb_name);
} else if (cb->need_config_write && need_config_write) {
if (first) {
yang_snode_get_path(snode,
@ -417,11 +452,14 @@ int main(int argc, char *argv[])
int opt;
bool config_pass;
while ((opt = getopt(argc, argv, "hp:s")) != -1) {
while ((opt = getopt(argc, argv, "hnp:s")) != -1) {
switch (opt) {
case 'h':
usage(EXIT_SUCCESS);
/* NOTREACHED */
case 'n':
f_new_cbs = true;
break;
case 'p':
if (stat(optarg, &st) == -1) {
fprintf(stderr,
@ -438,7 +476,7 @@ int main(int argc, char *argv[])
*darr_append(search_paths) = darr_strdup(optarg);
break;
case 's':
static_cbs = true;
f_static_cbs = true;
break;
default:
usage(EXIT_FAILURE);
@ -477,7 +515,7 @@ int main(int argc, char *argv[])
printf("// SPDX-" "License-Identifier: GPL-2.0-or-later\n\n");
/* Generate callback prototypes. */
if (!static_cbs) {
if (!f_static_cbs) {
printf("/* prototypes */\n");
yang_snodes_iterate(module->info, generate_prototypes, 0, NULL);
printf("\n");