]>
git.proxmox.com Git - mirror_frr.git/blob - tools/gen_northbound_callbacks.c
2 * Copyright (C) 2018 NetDEF, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #define REALLY_NEED_PLAIN_GETOPT 1
27 #include "northbound.h"
29 static void __attribute__((noreturn
)) usage(int status
)
31 fprintf(stderr
, "usage: gen_northbound_callbacks [-h] MODULE\n");
35 static struct nb_callback_info
{
39 char return_value
[32];
43 .operation
= NB_OP_CREATE
,
44 .return_type
= "int ",
45 .return_value
= "NB_OK",
47 "enum nb_event event, const struct lyd_node *dnode, union nb_resource *resource",
50 .operation
= NB_OP_MODIFY
,
51 .return_type
= "int ",
52 .return_value
= "NB_OK",
54 "enum nb_event event, const struct lyd_node *dnode, union nb_resource *resource",
57 .operation
= NB_OP_DELETE
,
58 .return_type
= "int ",
59 .return_value
= "NB_OK",
61 "enum nb_event event, const struct lyd_node *dnode",
64 .operation
= NB_OP_MOVE
,
65 .return_type
= "int ",
66 .return_value
= "NB_OK",
68 "enum nb_event event, const struct lyd_node *dnode",
71 .operation
= NB_OP_APPLY_FINISH
,
73 .return_type
= "void ",
75 .arguments
= "const struct lyd_node *dnode",
78 .operation
= NB_OP_GET_ELEM
,
79 .return_type
= "struct yang_data *",
80 .return_value
= "NULL",
81 .arguments
= "const char *xpath, const void *list_entry",
84 .operation
= NB_OP_GET_NEXT
,
85 .return_type
= "const void *",
86 .return_value
= "NULL",
87 .arguments
= "const char *xpath, const void *list_entry",
90 .operation
= NB_OP_GET_KEYS
,
91 .return_type
= "int ",
92 .return_value
= "NB_OK",
93 .arguments
= "const void *list_entry, struct yang_list_keys *keys",
96 .operation
= NB_OP_LOOKUP_ENTRY
,
97 .return_type
= "const void *",
98 .return_value
= "NULL",
99 .arguments
= "const struct yang_list_keys *keys",
102 .operation
= NB_OP_RPC
,
103 .return_type
= "int ",
104 .return_value
= "NB_OK",
106 "const char *xpath, const struct list *input, struct list *output",
114 static void replace_hyphens_by_underscores(char *str
)
119 while ((p
= strchr(p
, '-')) != NULL
)
123 static void generate_callback_name(struct lys_node
*snode
,
124 enum nb_operation operation
, char *buffer
,
131 for (; snode
; snode
= lys_parent(snode
)) {
132 /* Skip schema-only snodes. */
133 if (CHECK_FLAG(snode
->nodetype
, LYS_USES
| LYS_CHOICE
| LYS_CASE
138 listnode_add_head(snodes
, snode
);
141 memset(buffer
, 0, size
);
142 for (ALL_LIST_ELEMENTS_RO(snodes
, ln
, snode
)) {
143 strlcat(buffer
, snode
->name
, size
);
144 strlcat(buffer
, "_", size
);
146 strlcat(buffer
, nb_operation_name(operation
), size
);
147 list_delete(&snodes
);
149 replace_hyphens_by_underscores(buffer
);
152 static int generate_callbacks(const struct lys_node
*snode
, void *arg
)
156 switch (snode
->nodetype
) {
165 return YANG_ITER_CONTINUE
;
168 for (struct nb_callback_info
*cb
= &nb_callbacks
[0];
169 cb
->operation
!= -1; cb
++) {
170 char cb_name
[BUFSIZ
];
173 || !nb_operation_is_valid(cb
->operation
, snode
))
177 char xpath
[XPATH_MAXLEN
];
179 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath
,
189 generate_callback_name((struct lys_node
*)snode
, cb
->operation
,
190 cb_name
, sizeof(cb_name
));
191 printf("static %s%s(%s)\n"
193 "\t/* TODO: implement me. */\n"
196 nb_callbacks
[cb
->operation
].return_type
, cb_name
,
197 nb_callbacks
[cb
->operation
].arguments
,
198 nb_callbacks
[cb
->operation
].return_value
);
201 return YANG_ITER_CONTINUE
;
204 static int generate_nb_nodes(const struct lys_node
*snode
, void *arg
)
208 switch (snode
->nodetype
) {
217 return YANG_ITER_CONTINUE
;
220 for (struct nb_callback_info
*cb
= &nb_callbacks
[0];
221 cb
->operation
!= -1; cb
++) {
222 char cb_name
[BUFSIZ
];
225 || !nb_operation_is_valid(cb
->operation
, snode
))
229 char xpath
[XPATH_MAXLEN
];
231 yang_snode_get_path(snode
, YANG_PATH_DATA
, xpath
,
235 "\t\t\t.xpath = \"%s\",\n",
240 generate_callback_name((struct lys_node
*)snode
, cb
->operation
,
241 cb_name
, sizeof(cb_name
));
242 printf("\t\t\t.cbs.%s = %s,\n",
243 nb_operation_name(cb
->operation
), cb_name
);
249 return YANG_ITER_CONTINUE
;
252 int main(int argc
, char *argv
[])
254 struct yang_module
*module
;
255 char module_name_underscores
[64];
258 while ((opt
= getopt(argc
, argv
, "h")) != -1) {
275 /* Load YANG module. */
276 module
= yang_module_load(argv
[0]);
278 /* Generate callback functions. */
279 yang_snodes_iterate_module(module
->info
, generate_callbacks
, 0, NULL
);
281 strlcpy(module_name_underscores
, module
->name
,
282 sizeof(module_name_underscores
));
283 replace_hyphens_by_underscores(module_name_underscores
);
285 /* Generate frr_yang_module_info array. */
286 printf("/* clang-format off */\n"
287 "const struct frr_yang_module_info %s_info = {\n"
288 "\t.name = \"%s\",\n"
290 module_name_underscores
, module
->name
);
291 yang_snodes_iterate_module(module
->info
, generate_nb_nodes
, 0, NULL
);
293 "\t\t\t.xpath = NULL,\n"
298 /* Cleanup and exit. */