summaryrefslogtreecommitdiffstats
path: root/tools/gen_northbound_callbacks.c
blob: a8798113635768af170c9e954b8bbdf627fc8faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2018  NetDEF, Inc.
 *                     Renato Westphal
 */

#define REALLY_NEED_PLAIN_GETOPT 1

#include <zebra.h>
#include <sys/stat.h>

#include <unistd.h>

#include "yang.h"
#include "northbound.h"

static bool static_cbs;

static void __attribute__((noreturn)) usage(int status)
{
	extern const char *__progname;
	fprintf(stderr, "usage: %s [-h] [-s] [-p path] MODULE\n", __progname);
	exit(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_CB_CREATE,
		.need_config_write = true,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_create_args *args",
	},
	{
		.operation = NB_CB_MODIFY,
		.need_config_write = true,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_modify_args *args",
	},
	{
		.operation = NB_CB_DESTROY,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_destroy_args *args",
	},
	{
		.operation = NB_CB_MOVE,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_move_args *args",
	},
	{
		.operation = NB_CB_APPLY_FINISH,
		.optional = true,
		.return_type = "void ",
		.return_value = "",
		.arguments = "struct nb_cb_apply_finish_args *args",
	},
	{
		.operation = NB_CB_GET_ELEM,
		.return_type = "struct yang_data *",
		.return_value = "NULL",
		.arguments = "struct nb_cb_get_elem_args *args",
	},
	{
		.operation = NB_CB_GET_NEXT,
		.return_type = "const void *",
		.return_value = "NULL",
		.arguments = "struct nb_cb_get_next_args *args",
	},
	{
		.operation = NB_CB_GET_KEYS,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_get_keys_args *args",
	},
	{
		.operation = NB_CB_LOOKUP_ENTRY,
		.return_type = "const void *",
		.return_value = "NULL",
		.arguments = "struct nb_cb_lookup_entry_args *args",
	},
	{
		.operation = NB_CB_RPC,
		.return_type = "int ",
		.return_value = "NB_OK",
		.arguments = "struct nb_cb_rpc_args *args",
	},
	{
		/* sentinel */
		.operation = -1,
	},
};

/*
 * 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;

	p = str;
	while ((p = strchr(p, '-')) != NULL)
		*p++ = '_';
}

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

	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);
	}

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

	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';

	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, snode->name, size);
		strlcat(buffer, "_", size);
	}

	strlcat(buffer, "cli_write", 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:
	case LYS_LEAFLIST:
	case LYS_LIST:
	case LYS_NOTIF:
	case LYS_RPC:
		break;
	default:
		return YANG_ITER_CONTINUE;
	}

	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;

		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;
}

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);

	switch (ncinfo->operation) {
	case NB_CB_CREATE:
	case NB_CB_MODIFY:
	case NB_CB_DESTROY:
	case NB_CB_MOVE:
		printf("\tswitch (args->event) {\n"
		       "\tcase NB_EV_VALIDATE:\n"
		       "\tcase NB_EV_PREPARE:\n"
		       "\tcase NB_EV_ABORT:\n"
		       "\tcase NB_EV_APPLY:\n"
		       "\t\t/* TODO: implement me. */\n"
		       "\t\tbreak;\n"
		       "\t}\n\n"
		       );
		break;

	default:
		printf("\t/* TODO: implement me. */\n");
		break;
	}

	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:
	case LYS_LEAF:
	case LYS_LEAFLIST:
	case LYS_LIST:
	case LYS_NOTIF:
	case LYS_RPC:
		break;
	default:
		return YANG_ITER_CONTINUE;
	}

	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 (first) {
			char xpath[XPATH_MAXLEN];

			yang_snode_get_path(snode, YANG_PATH_DATA, xpath,
					    sizeof(xpath));

			printf("/*\n"
			       " * XPath: %s\n"
			       " */\n",
			       xpath);
			first = false;
		}

		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;
}

static int generate_nb_nodes(const struct lysc_node *snode, void *arg)
{
	bool first = true;
	char cb_name[BUFSIZ];
	char xpath[XPATH_MAXLEN];
	bool config_pass = *(bool *)arg;
	bool need_config_write = true;

	switch (snode->nodetype) {
	case LYS_CONTAINER:
	case LYS_LEAF:
	case LYS_LEAFLIST:
	case LYS_LIST:
	case LYS_NOTIF:
	case LYS_RPC:
		break;
	default:
		return YANG_ITER_CONTINUE;
	}

	/* We generate two types of structs currently; behavior is a little
	 * different between the types.
	 */
	for (struct nb_callback_info *cb = &nb_callbacks[0];
	     cb->operation != -1; cb++) {

		if (cb->optional
		    || !nb_cb_operation_is_valid(cb->operation, snode))
			continue;

		if (config_pass) {
			if (first) {
				yang_snode_get_path(snode, YANG_PATH_DATA, xpath,
						    sizeof(xpath));

				printf("\t\t{\n"
				       "\t\t\t.xpath = \"%s\",\n",
				       xpath);
				printf("\t\t\t.cbs = {\n");
				first = false;
			}

			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);
		} else if (cb->need_config_write && need_config_write) {
			if (first) {
				yang_snode_get_path(snode,
						    YANG_PATH_DATA,
						    xpath,
						    sizeof(xpath));

				printf("\t\t{\n"
				       "\t\t\t.xpath = \"%s\",\n",
				       xpath);
				printf("\t\t\t.cbs = {\n");
				first = false;
			}

			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) {
		printf("\t\t\t}\n");
		printf("\t\t},\n");
	}

	return YANG_ITER_CONTINUE;
}

int main(int argc, char *argv[])
{
	const char *search_path = NULL;
	struct yang_module *module;
	char module_name_underscores[64];
	struct stat st;
	int opt;
	bool config_pass;

	while ((opt = getopt(argc, argv, "hp:s")) != -1) {
		switch (opt) {
		case 'h':
			usage(EXIT_SUCCESS);
			/* NOTREACHED */
		case 'p':
			if (stat(optarg, &st) == -1) {
				fprintf(stderr,
				    "error: invalid search path '%s': %s\n",
				    optarg, strerror(errno));
				exit(EXIT_FAILURE);
			}
			if (S_ISDIR(st.st_mode) == 0) {
				fprintf(stderr,
				    "error: search path is not directory");
				exit(EXIT_FAILURE);
			}

			search_path = optarg;
			break;
		case 's':
			static_cbs = true;
			break;
		default:
			usage(EXIT_FAILURE);
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;
	if (argc != 1)
		usage(EXIT_FAILURE);

	yang_init(false, true);

	if (search_path)
		ly_ctx_set_searchdir(ly_native_ctx, search_path);

	/* Load all FRR native models to ensure all augmentations are loaded. */
	yang_module_load_all();

	module = yang_module_find(argv[0]);
	if (!module)
		/* Non-native FRR module (e.g. modules from unit tests). */
		module = yang_module_load(argv[0], NULL);

	yang_init_loading_complete();

	/* Create a nb_node for all YANG schema nodes. */
	nb_nodes_create();

	/* Emit bare-bones license line (and fool the checkpatch regex
	 * that triggers a warning).
	 */
	printf("// SPDX-" "License-Identifier: GPL-2.0-or-later\n\n");

	/* Generate callback prototypes. */
	if (!static_cbs) {
		printf("/* prototypes */\n");
		yang_snodes_iterate(module->info, generate_prototypes, 0, NULL);
		printf("\n");
	}

	/* Generate callback functions. */
	yang_snodes_iterate(module->info, generate_callbacks, 0, NULL);

	strlcpy(module_name_underscores, module->name,
		sizeof(module_name_underscores));
	replace_hyphens_by_underscores(module_name_underscores);

	/*
	 * We're going to generate two structs here, two arrays of callbacks:
	 * first one with config-handling callbacks, then a second struct with
	 * config-output-oriented callbacks.
	 */

	/* Generate frr_yang_module_info array, with config-handling callbacks */
	config_pass = true;
	printf("/* clang-format off */\n"
	       "const struct frr_yang_module_info %s_nb_info = {\n"
	       "\t.name = \"%s\",\n"
	       "\t.nodes = {\n",
	       module_name_underscores, module->name);
	yang_snodes_iterate(module->info, generate_nb_nodes, 0, &config_pass);

	/* Emit terminator element */
	printf("\t\t{\n"
	       "\t\t\t.xpath = NULL,\n"
	       "\t\t},\n");
	printf("\t}\n"
	       "};\n");

	/* Generate second array, with output-oriented callbacks. */
	config_pass = false;
	printf("\n/* clang-format off */\n"
	       "const struct frr_yang_module_info %s_cli_info = {\n"
	       "\t.name = \"%s\",\n"
	       "\t.nodes = {\n",
	       module_name_underscores, module->name);
	yang_snodes_iterate(module->info, generate_nb_nodes, 0, &config_pass);

	/* Emit terminator element */
	printf("\t\t{\n"
	       "\t\t\t.xpath = NULL,\n"
	       "\t\t},\n");
	printf("\t}\n"
	       "};\n");

	/* Cleanup and exit. */
	nb_nodes_delete();
	yang_terminate();

	return 0;
}