]> git.proxmox.com Git - mirror_frr.git/blob - tools/gen_northbound_callbacks.c
lib: add support for YANG lists with mixed config and state data
[mirror_frr.git] / tools / gen_northbound_callbacks.c
1 /*
2 * Copyright (C) 2018 NetDEF, Inc.
3 * Renato Westphal
4 *
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)
8 * any later version.
9 *
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
13 * more details.
14 *
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
18 */
19
20 #define REALLY_NEED_PLAIN_GETOPT 1
21
22 #include <zebra.h>
23
24 #include <unistd.h>
25
26 #include "yang.h"
27 #include "northbound.h"
28
29 static void __attribute__((noreturn)) usage(int status)
30 {
31 fprintf(stderr, "usage: gen_northbound_callbacks [-h] MODULE\n");
32 exit(status);
33 }
34
35 static struct nb_callback_info {
36 int operation;
37 bool optional;
38 char return_type[32];
39 char return_value[32];
40 char arguments[128];
41 } nb_callbacks[] = {
42 {
43 .operation = NB_OP_CREATE,
44 .return_type = "int ",
45 .return_value = "NB_OK",
46 .arguments =
47 "enum nb_event event, const struct lyd_node *dnode, union nb_resource *resource",
48 },
49 {
50 .operation = NB_OP_MODIFY,
51 .return_type = "int ",
52 .return_value = "NB_OK",
53 .arguments =
54 "enum nb_event event, const struct lyd_node *dnode, union nb_resource *resource",
55 },
56 {
57 .operation = NB_OP_DELETE,
58 .return_type = "int ",
59 .return_value = "NB_OK",
60 .arguments =
61 "enum nb_event event, const struct lyd_node *dnode",
62 },
63 {
64 .operation = NB_OP_MOVE,
65 .return_type = "int ",
66 .return_value = "NB_OK",
67 .arguments =
68 "enum nb_event event, const struct lyd_node *dnode",
69 },
70 {
71 .operation = NB_OP_APPLY_FINISH,
72 .optional = true,
73 .return_type = "void ",
74 .return_value = "",
75 .arguments = "const struct lyd_node *dnode",
76 },
77 {
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",
82 },
83 {
84 .operation = NB_OP_GET_NEXT,
85 .return_type = "const void *",
86 .return_value = "NULL",
87 .arguments = "const char *xpath, const void *list_entry",
88 },
89 {
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",
94 },
95 {
96 .operation = NB_OP_LOOKUP_ENTRY,
97 .return_type = "const void *",
98 .return_value = "NULL",
99 .arguments = "const struct yang_list_keys *keys",
100 },
101 {
102 .operation = NB_OP_RPC,
103 .return_type = "int ",
104 .return_value = "NB_OK",
105 .arguments =
106 "const char *xpath, const struct list *input, struct list *output",
107 },
108 {
109 /* sentinel */
110 .operation = -1,
111 },
112 };
113
114 static void replace_hyphens_by_underscores(char *str)
115 {
116 char *p;
117
118 p = str;
119 while ((p = strchr(p, '-')) != NULL)
120 *p++ = '_';
121 }
122
123 static void generate_callback_name(struct lys_node *snode,
124 enum nb_operation operation, char *buffer,
125 size_t size)
126 {
127 struct list *snodes;
128 struct listnode *ln;
129
130 snodes = list_new();
131 for (; snode; snode = lys_parent(snode)) {
132 /* Skip schema-only snodes. */
133 if (CHECK_FLAG(snode->nodetype, LYS_USES | LYS_CHOICE | LYS_CASE
134 | LYS_INPUT
135 | LYS_OUTPUT))
136 continue;
137
138 listnode_add_head(snodes, snode);
139 }
140
141 memset(buffer, 0, size);
142 for (ALL_LIST_ELEMENTS_RO(snodes, ln, snode)) {
143 strlcat(buffer, snode->name, size);
144 strlcat(buffer, "_", size);
145 }
146 strlcat(buffer, nb_operation_name(operation), size);
147 list_delete(&snodes);
148
149 replace_hyphens_by_underscores(buffer);
150 }
151
152 static int generate_callbacks(const struct lys_node *snode, void *arg)
153 {
154 bool first = true;
155
156 switch (snode->nodetype) {
157 case LYS_CONTAINER:
158 case LYS_LEAF:
159 case LYS_LEAFLIST:
160 case LYS_LIST:
161 case LYS_NOTIF:
162 case LYS_RPC:
163 break;
164 default:
165 return YANG_ITER_CONTINUE;
166 }
167
168 for (struct nb_callback_info *cb = &nb_callbacks[0];
169 cb->operation != -1; cb++) {
170 char cb_name[BUFSIZ];
171
172 if (cb->optional
173 || !nb_operation_is_valid(cb->operation, snode))
174 continue;
175
176 if (first) {
177 char xpath[XPATH_MAXLEN];
178
179 yang_snode_get_path(snode, YANG_PATH_DATA, xpath,
180 sizeof(xpath));
181
182 printf("/*\n"
183 " * XPath: %s\n"
184 " */\n",
185 xpath);
186 first = false;
187 }
188
189 generate_callback_name((struct lys_node *)snode, cb->operation,
190 cb_name, sizeof(cb_name));
191 printf("static %s%s(%s)\n"
192 "{\n"
193 "\t/* TODO: implement me. */\n"
194 "\treturn %s;\n"
195 "}\n\n",
196 nb_callbacks[cb->operation].return_type, cb_name,
197 nb_callbacks[cb->operation].arguments,
198 nb_callbacks[cb->operation].return_value);
199 }
200
201 return YANG_ITER_CONTINUE;
202 }
203
204 static int generate_nb_nodes(const struct lys_node *snode, void *arg)
205 {
206 bool first = true;
207
208 switch (snode->nodetype) {
209 case LYS_CONTAINER:
210 case LYS_LEAF:
211 case LYS_LEAFLIST:
212 case LYS_LIST:
213 case LYS_NOTIF:
214 case LYS_RPC:
215 break;
216 default:
217 return YANG_ITER_CONTINUE;
218 }
219
220 for (struct nb_callback_info *cb = &nb_callbacks[0];
221 cb->operation != -1; cb++) {
222 char cb_name[BUFSIZ];
223
224 if (cb->optional
225 || !nb_operation_is_valid(cb->operation, snode))
226 continue;
227
228 if (first) {
229 char xpath[XPATH_MAXLEN];
230
231 yang_snode_get_path(snode, YANG_PATH_DATA, xpath,
232 sizeof(xpath));
233
234 printf("\t\t{\n"
235 "\t\t\t.xpath = \"%s\",\n",
236 xpath);
237 first = false;
238 }
239
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);
244 }
245
246 if (!first)
247 printf("\t\t},\n");
248
249 return YANG_ITER_CONTINUE;
250 }
251
252 int main(int argc, char *argv[])
253 {
254 struct yang_module *module;
255 char module_name_underscores[64];
256 int opt;
257
258 while ((opt = getopt(argc, argv, "h")) != -1) {
259 switch (opt) {
260 case 'h':
261 usage(EXIT_SUCCESS);
262 /* NOTREACHED */
263 default:
264 usage(EXIT_FAILURE);
265 /* NOTREACHED */
266 }
267 }
268 argc -= optind;
269 argv += optind;
270 if (argc != 1)
271 usage(EXIT_FAILURE);
272
273 yang_init();
274
275 /* Load all FRR native models to ensure all augmentations are loaded. */
276 yang_module_load_all();
277 module = yang_module_find(argv[0]);
278 if (!module)
279 /* Non-native FRR module (e.g. modules from unit tests). */
280 module = yang_module_load(argv[0]);
281
282 /* Create a nb_node for all YANG schema nodes. */
283 nb_nodes_create();
284
285 /* Generate callback functions. */
286 yang_snodes_iterate_module(module->info, generate_callbacks, 0, NULL);
287
288 strlcpy(module_name_underscores, module->name,
289 sizeof(module_name_underscores));
290 replace_hyphens_by_underscores(module_name_underscores);
291
292 /* Generate frr_yang_module_info array. */
293 printf("/* clang-format off */\n"
294 "const struct frr_yang_module_info %s_info = {\n"
295 "\t.name = \"%s\",\n"
296 "\t.nodes = {\n",
297 module_name_underscores, module->name);
298 yang_snodes_iterate_module(module->info, generate_nb_nodes, 0, NULL);
299 printf("\t\t{\n"
300 "\t\t\t.xpath = NULL,\n"
301 "\t\t},\n");
302 printf("\t}\n"
303 "};\n");
304
305 /* Cleanup and exit. */
306 nb_nodes_delete();
307 yang_terminate();
308
309 return 0;
310 }