]> git.proxmox.com Git - mirror_frr.git/blob - lib/northbound_cli.c
lib: always return valid data format for show yang command.
[mirror_frr.git] / lib / northbound_cli.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018 NetDEF, Inc.
4 * Renato Westphal
5 */
6
7 #include <zebra.h>
8
9 #include "libfrr.h"
10 #include "lib/version.h"
11 #include "defaults.h"
12 #include "log.h"
13 #include "lib_errors.h"
14 #include "command.h"
15 #include "termtable.h"
16 #include "db.h"
17 #include "debug.h"
18 #include "yang_translator.h"
19 #include "northbound.h"
20 #include "northbound_cli.h"
21 #include "northbound_db.h"
22 #include "lib/northbound_cli_clippy.c"
23
24 struct debug nb_dbg_cbs_config = {0, "Northbound callbacks: configuration"};
25 struct debug nb_dbg_cbs_state = {0, "Northbound callbacks: state"};
26 struct debug nb_dbg_cbs_rpc = {0, "Northbound callbacks: RPCs"};
27 struct debug nb_dbg_notif = {0, "Northbound notifications"};
28 struct debug nb_dbg_events = {0, "Northbound events"};
29 struct debug nb_dbg_libyang = {0, "libyang debugging"};
30
31 struct nb_config *vty_shared_candidate_config;
32 static struct event_loop *master;
33
34 static void vty_show_nb_errors(struct vty *vty, int error, const char *errmsg)
35 {
36 vty_out(vty, "Error type: %s\n", nb_err_name(error));
37 if (strlen(errmsg) > 0)
38 vty_out(vty, "Error description: %s\n", errmsg);
39 }
40
41 static int nb_cli_classic_commit(struct vty *vty)
42 {
43 struct nb_context context = {};
44 char errmsg[BUFSIZ] = {0};
45 int ret;
46
47 context.client = NB_CLIENT_CLI;
48 context.user = vty;
49 ret = nb_candidate_commit(context, vty->candidate_config, true, NULL,
50 NULL, errmsg, sizeof(errmsg));
51 switch (ret) {
52 case NB_OK:
53 /* Successful commit. Print warnings (if any). */
54 if (strlen(errmsg) > 0)
55 vty_out(vty, "%s\n", errmsg);
56 break;
57 case NB_ERR_NO_CHANGES:
58 break;
59 default:
60 vty_out(vty, "%% Configuration failed.\n\n");
61 vty_show_nb_errors(vty, ret, errmsg);
62 if (vty->pending_commit)
63 vty_out(vty,
64 "The following commands were dynamically grouped into the same transaction and rejected:\n%s",
65 vty->pending_cmds_buf);
66
67 /* Regenerate candidate for consistency. */
68 nb_config_replace(vty->candidate_config, running_config, true);
69 return CMD_WARNING_CONFIG_FAILED;
70 }
71
72 return CMD_SUCCESS;
73 }
74
75 static void nb_cli_pending_commit_clear(struct vty *vty)
76 {
77 vty->pending_commit = 0;
78 XFREE(MTYPE_TMP, vty->pending_cmds_buf);
79 vty->pending_cmds_buflen = 0;
80 vty->pending_cmds_bufpos = 0;
81 }
82
83 int nb_cli_pending_commit_check(struct vty *vty)
84 {
85 int ret = CMD_SUCCESS;
86
87 if (vty->pending_commit) {
88 ret = nb_cli_classic_commit(vty);
89 nb_cli_pending_commit_clear(vty);
90 }
91
92 return ret;
93 }
94
95 static int nb_cli_schedule_command(struct vty *vty)
96 {
97 /* Append command to dynamically sized buffer of scheduled commands. */
98 if (!vty->pending_cmds_buf) {
99 vty->pending_cmds_buflen = 4096;
100 vty->pending_cmds_buf =
101 XCALLOC(MTYPE_TMP, vty->pending_cmds_buflen);
102 }
103 if ((strlen(vty->buf) + 3)
104 > (vty->pending_cmds_buflen - vty->pending_cmds_bufpos)) {
105 vty->pending_cmds_buflen *= 2;
106 vty->pending_cmds_buf =
107 XREALLOC(MTYPE_TMP, vty->pending_cmds_buf,
108 vty->pending_cmds_buflen);
109 }
110 strlcat(vty->pending_cmds_buf, "- ", vty->pending_cmds_buflen);
111 vty->pending_cmds_bufpos = strlcat(vty->pending_cmds_buf, vty->buf,
112 vty->pending_cmds_buflen);
113
114 /* Schedule the commit operation. */
115 vty->pending_commit = 1;
116
117 return CMD_SUCCESS;
118 }
119
120 void nb_cli_enqueue_change(struct vty *vty, const char *xpath,
121 enum nb_operation operation, const char *value)
122 {
123 struct nb_cfg_change *change;
124
125 if (vty->num_cfg_changes == VTY_MAXCFGCHANGES) {
126 /* Not expected to happen. */
127 vty_out(vty,
128 "%% Exceeded the maximum number of changes (%u) for a single command\n\n",
129 VTY_MAXCFGCHANGES);
130 return;
131 }
132
133 change = &vty->cfg_changes[vty->num_cfg_changes++];
134 strlcpy(change->xpath, xpath, sizeof(change->xpath));
135 change->operation = operation;
136 change->value = value;
137 }
138
139 static int nb_cli_apply_changes_internal(struct vty *vty,
140 const char *xpath_base,
141 bool clear_pending)
142 {
143 bool error = false;
144 char buf[BUFSIZ];
145
146 VTY_CHECK_XPATH;
147
148 nb_candidate_edit_config_changes(
149 vty->candidate_config, vty->cfg_changes, vty->num_cfg_changes,
150 xpath_base, VTY_CURR_XPATH, vty->xpath_index, buf, sizeof(buf),
151 &error);
152 if (error) {
153 /*
154 * Failure to edit the candidate configuration should never
155 * happen in practice, unless there's a bug in the code. When
156 * that happens, log the error but otherwise ignore it.
157 */
158 vty_out(vty, "%s", buf);
159 }
160
161 /*
162 * Maybe do an implicit commit when using the classic CLI mode.
163 *
164 * NOTE: the implicit commit might be scheduled to run later when
165 * too many commands are being sent at the same time. This is a
166 * protection mechanism where multiple commands are grouped into the
167 * same configuration transaction, allowing them to be processed much
168 * faster.
169 */
170 if (frr_get_cli_mode() == FRR_CLI_CLASSIC) {
171 if (clear_pending) {
172 if (vty->pending_commit)
173 return nb_cli_pending_commit_check(vty);
174 } else if (vty->pending_allowed)
175 return nb_cli_schedule_command(vty);
176 assert(!vty->pending_commit);
177 return nb_cli_classic_commit(vty);
178 }
179
180 return CMD_SUCCESS;
181 }
182
183 int nb_cli_apply_changes(struct vty *vty, const char *xpath_base_fmt, ...)
184 {
185 char xpath_base[XPATH_MAXLEN] = {};
186 bool implicit_commit;
187 int ret;
188
189 /* Parse the base XPath format string. */
190 if (xpath_base_fmt) {
191 va_list ap;
192
193 va_start(ap, xpath_base_fmt);
194 vsnprintf(xpath_base, sizeof(xpath_base), xpath_base_fmt, ap);
195 va_end(ap);
196 }
197
198 if (vty_mgmt_fe_enabled()) {
199 VTY_CHECK_XPATH;
200
201 implicit_commit = vty_needs_implicit_commit(vty);
202 ret = vty_mgmt_send_config_data(vty);
203 if (ret >= 0 && !implicit_commit)
204 vty->mgmt_num_pending_setcfg++;
205 return ret;
206 }
207
208 return nb_cli_apply_changes_internal(vty, xpath_base, false);
209 }
210
211 int nb_cli_apply_changes_clear_pending(struct vty *vty,
212 const char *xpath_base_fmt, ...)
213 {
214 char xpath_base[XPATH_MAXLEN] = {};
215 bool implicit_commit;
216 int ret;
217
218 /* Parse the base XPath format string. */
219 if (xpath_base_fmt) {
220 va_list ap;
221
222 va_start(ap, xpath_base_fmt);
223 vsnprintf(xpath_base, sizeof(xpath_base), xpath_base_fmt, ap);
224 va_end(ap);
225 }
226
227 if (vty_mgmt_fe_enabled()) {
228 VTY_CHECK_XPATH;
229
230 implicit_commit = vty_needs_implicit_commit(vty);
231 ret = vty_mgmt_send_config_data(vty);
232 if (ret >= 0 && !implicit_commit)
233 vty->mgmt_num_pending_setcfg++;
234 return ret;
235 }
236
237 return nb_cli_apply_changes_internal(vty, xpath_base, true);
238 }
239
240 int nb_cli_rpc(struct vty *vty, const char *xpath, struct list *input,
241 struct list *output)
242 {
243 struct nb_node *nb_node;
244 int ret;
245 char errmsg[BUFSIZ] = {0};
246
247 nb_node = nb_node_find(xpath);
248 if (!nb_node) {
249 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH,
250 "%s: unknown data path: %s", __func__, xpath);
251 return CMD_WARNING;
252 }
253
254 ret = nb_callback_rpc(nb_node, xpath, input, output, errmsg,
255 sizeof(errmsg));
256 switch (ret) {
257 case NB_OK:
258 return CMD_SUCCESS;
259 default:
260 if (strlen(errmsg))
261 vty_show_nb_errors(vty, ret, errmsg);
262 return CMD_WARNING;
263 }
264 }
265
266 void nb_cli_confirmed_commit_clean(struct vty *vty)
267 {
268 event_cancel(&vty->t_confirmed_commit_timeout);
269 nb_config_free(vty->confirmed_commit_rollback);
270 vty->confirmed_commit_rollback = NULL;
271 }
272
273 int nb_cli_confirmed_commit_rollback(struct vty *vty)
274 {
275 struct nb_context context = {};
276 uint32_t transaction_id;
277 char errmsg[BUFSIZ] = {0};
278 int ret;
279
280 /* Perform the rollback. */
281 context.client = NB_CLIENT_CLI;
282 context.user = vty;
283 ret = nb_candidate_commit(
284 context, vty->confirmed_commit_rollback, true,
285 "Rollback to previous configuration - confirmed commit has timed out",
286 &transaction_id, errmsg, sizeof(errmsg));
287 if (ret == NB_OK) {
288 vty_out(vty,
289 "Rollback performed successfully (Transaction ID #%u).\n",
290 transaction_id);
291 /* Print warnings (if any). */
292 if (strlen(errmsg) > 0)
293 vty_out(vty, "%s\n", errmsg);
294 } else {
295 vty_out(vty,
296 "Failed to rollback to previous configuration.\n\n");
297 vty_show_nb_errors(vty, ret, errmsg);
298 }
299
300 return ret;
301 }
302
303 static void nb_cli_confirmed_commit_timeout(struct event *thread)
304 {
305 struct vty *vty = EVENT_ARG(thread);
306
307 /* XXX: broadcast this message to all logged-in users? */
308 vty_out(vty,
309 "\nConfirmed commit has timed out, rolling back to previous configuration.\n\n");
310
311 nb_cli_confirmed_commit_rollback(vty);
312 nb_cli_confirmed_commit_clean(vty);
313 }
314
315 static int nb_cli_commit(struct vty *vty, bool force,
316 unsigned int confirmed_timeout, char *comment)
317 {
318 struct nb_context context = {};
319 uint32_t transaction_id = 0;
320 char errmsg[BUFSIZ] = {0};
321 int ret;
322
323 /* Check if there's a pending confirmed commit. */
324 if (vty->t_confirmed_commit_timeout) {
325 if (confirmed_timeout) {
326 /* Reset timeout if "commit confirmed" is used again. */
327 vty_out(vty,
328 "%% Resetting confirmed-commit timeout to %u minute(s)\n\n",
329 confirmed_timeout);
330
331 event_cancel(&vty->t_confirmed_commit_timeout);
332 event_add_timer(master, nb_cli_confirmed_commit_timeout,
333 vty, confirmed_timeout * 60,
334 &vty->t_confirmed_commit_timeout);
335 } else {
336 /* Accept commit confirmation. */
337 vty_out(vty, "%% Commit complete.\n\n");
338 nb_cli_confirmed_commit_clean(vty);
339 }
340 return CMD_SUCCESS;
341 }
342
343 /* "force" parameter. */
344 if (!force && nb_candidate_needs_update(vty->candidate_config)) {
345 vty_out(vty,
346 "%% Candidate configuration needs to be updated before commit.\n\n");
347 vty_out(vty,
348 "Use the \"update\" command or \"commit force\".\n");
349 return CMD_WARNING;
350 }
351
352 /* "confirm" parameter. */
353 if (confirmed_timeout) {
354 vty->confirmed_commit_rollback = nb_config_dup(running_config);
355
356 vty->t_confirmed_commit_timeout = NULL;
357 event_add_timer(master, nb_cli_confirmed_commit_timeout, vty,
358 confirmed_timeout * 60,
359 &vty->t_confirmed_commit_timeout);
360 }
361
362 context.client = NB_CLIENT_CLI;
363 context.user = vty;
364 ret = nb_candidate_commit(context, vty->candidate_config, true, comment,
365 &transaction_id, errmsg, sizeof(errmsg));
366
367 /* Map northbound return code to CLI return code. */
368 switch (ret) {
369 case NB_OK:
370 nb_config_replace(vty->candidate_config_base, running_config,
371 true);
372 vty_out(vty,
373 "%% Configuration committed successfully (Transaction ID #%u).\n\n",
374 transaction_id);
375 /* Print warnings (if any). */
376 if (strlen(errmsg) > 0)
377 vty_out(vty, "%s\n", errmsg);
378 return CMD_SUCCESS;
379 case NB_ERR_NO_CHANGES:
380 vty_out(vty, "%% No configuration changes to commit.\n\n");
381 return CMD_SUCCESS;
382 default:
383 vty_out(vty,
384 "%% Failed to commit candidate configuration.\n\n");
385 vty_show_nb_errors(vty, ret, errmsg);
386 return CMD_WARNING;
387 }
388 }
389
390 static int nb_cli_candidate_load_file(struct vty *vty,
391 enum nb_cfg_format format,
392 struct yang_translator *translator,
393 const char *path, bool replace)
394 {
395 struct nb_config *loaded_config = NULL;
396 struct lyd_node *dnode;
397 struct ly_ctx *ly_ctx;
398 int ly_format;
399 char buf[BUFSIZ];
400 LY_ERR err;
401
402 switch (format) {
403 case NB_CFG_FMT_CMDS:
404 loaded_config = nb_config_new(NULL);
405 if (!vty_read_config(loaded_config, path, config_default)) {
406 vty_out(vty, "%% Failed to load configuration.\n\n");
407 vty_out(vty,
408 "Please check the logs for more details.\n");
409 nb_config_free(loaded_config);
410 return CMD_WARNING;
411 }
412 break;
413 case NB_CFG_FMT_JSON:
414 case NB_CFG_FMT_XML:
415 ly_format = (format == NB_CFG_FMT_JSON) ? LYD_JSON : LYD_XML;
416
417 ly_ctx = translator ? translator->ly_ctx : ly_native_ctx;
418 err = lyd_parse_data_path(ly_ctx, path, ly_format,
419 LYD_PARSE_ONLY | LYD_PARSE_NO_STATE,
420 0, &dnode);
421 if (err || !dnode) {
422 flog_warn(EC_LIB_LIBYANG, "%s: lyd_parse_path() failed",
423 __func__);
424 vty_out(vty, "%% Failed to load configuration:\n\n");
425 vty_out(vty, "%s",
426 yang_print_errors(ly_native_ctx, buf,
427 sizeof(buf)));
428 return CMD_WARNING;
429 }
430 if (translator
431 && yang_translate_dnode(translator,
432 YANG_TRANSLATE_TO_NATIVE, &dnode)
433 != YANG_TRANSLATE_SUCCESS) {
434 vty_out(vty, "%% Failed to translate configuration\n");
435 yang_dnode_free(dnode);
436 return CMD_WARNING;
437 }
438 loaded_config = nb_config_new(dnode);
439 break;
440 }
441
442 if (replace)
443 nb_config_replace(vty->candidate_config, loaded_config, false);
444 else if (nb_config_merge(vty->candidate_config, loaded_config, false)
445 != NB_OK) {
446 vty_out(vty,
447 "%% Failed to merge the loaded configuration:\n\n");
448 vty_out(vty, "%s",
449 yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
450 return CMD_WARNING;
451 }
452
453 return CMD_SUCCESS;
454 }
455
456 static int nb_cli_candidate_load_transaction(struct vty *vty,
457 uint32_t transaction_id,
458 bool replace)
459 {
460 struct nb_config *loaded_config;
461 char buf[BUFSIZ];
462
463 loaded_config = nb_db_transaction_load(transaction_id);
464 if (!loaded_config) {
465 vty_out(vty, "%% Transaction %u does not exist.\n\n",
466 transaction_id);
467 return CMD_WARNING;
468 }
469
470 if (replace)
471 nb_config_replace(vty->candidate_config, loaded_config, false);
472 else if (nb_config_merge(vty->candidate_config, loaded_config, false)
473 != NB_OK) {
474 vty_out(vty,
475 "%% Failed to merge the loaded configuration:\n\n");
476 vty_out(vty, "%s",
477 yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
478 return CMD_WARNING;
479 }
480
481 return CMD_SUCCESS;
482 }
483
484 /* Prepare the configuration for display. */
485 void nb_cli_show_config_prepare(struct nb_config *config, bool with_defaults)
486 {
487 /* Nothing to do for daemons that don't implement any YANG module. */
488 if (config->dnode == NULL)
489 return;
490
491 /*
492 * Call lyd_validate() only to create default child nodes, ignoring
493 * any possible validation error. This doesn't need to be done when
494 * displaying the running configuration since it's always fully
495 * validated.
496 */
497 if (config != running_config)
498 (void)lyd_validate_all(&config->dnode, ly_native_ctx,
499 LYD_VALIDATE_NO_STATE, NULL);
500 }
501
502 static int lyd_node_cmp(const struct lyd_node **dnode1,
503 const struct lyd_node **dnode2)
504 {
505 struct nb_node *nb_node = (*dnode1)->schema->priv;
506
507 return nb_node->cbs.cli_cmp(*dnode1, *dnode2);
508 }
509
510 static void show_dnode_children_cmds(struct vty *vty,
511 const struct lyd_node *root,
512 bool with_defaults)
513 {
514 struct nb_node *nb_node, *sort_node = NULL;
515 struct listnode *listnode;
516 struct lyd_node *child;
517 struct list *sort_list;
518 void *data;
519
520 LY_LIST_FOR (lyd_child(root), child) {
521 nb_node = child->schema->priv;
522
523 /*
524 * We finished processing current list,
525 * it's time to print the config.
526 */
527 if (sort_node && nb_node != sort_node) {
528 list_sort(sort_list,
529 (int (*)(const void **,
530 const void **))lyd_node_cmp);
531
532 for (ALL_LIST_ELEMENTS_RO(sort_list, listnode, data))
533 nb_cli_show_dnode_cmds(vty, data,
534 with_defaults);
535
536 list_delete(&sort_list);
537 sort_node = NULL;
538 }
539
540 /*
541 * If the config needs to be sorted,
542 * then add the dnode to the sorting
543 * list for later processing.
544 */
545 if (nb_node && nb_node->cbs.cli_cmp) {
546 if (!sort_node) {
547 sort_node = nb_node;
548 sort_list = list_new();
549 }
550
551 listnode_add(sort_list, child);
552 continue;
553 }
554
555 nb_cli_show_dnode_cmds(vty, child, with_defaults);
556 }
557
558 if (sort_node) {
559 list_sort(sort_list,
560 (int (*)(const void **, const void **))lyd_node_cmp);
561
562 for (ALL_LIST_ELEMENTS_RO(sort_list, listnode, data))
563 nb_cli_show_dnode_cmds(vty, data, with_defaults);
564
565 list_delete(&sort_list);
566 sort_node = NULL;
567 }
568 }
569
570 void nb_cli_show_dnode_cmds(struct vty *vty, const struct lyd_node *root,
571 bool with_defaults)
572 {
573 struct nb_node *nb_node;
574
575 if (!with_defaults && yang_dnode_is_default_recursive(root))
576 return;
577
578 nb_node = root->schema->priv;
579
580 if (nb_node && nb_node->cbs.cli_show)
581 (*nb_node->cbs.cli_show)(vty, root, with_defaults);
582
583 if (!(root->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)))
584 show_dnode_children_cmds(vty, root, with_defaults);
585
586 if (nb_node && nb_node->cbs.cli_show_end)
587 (*nb_node->cbs.cli_show_end)(vty, root);
588 }
589
590 static void nb_cli_show_config_cmds(struct vty *vty, struct nb_config *config,
591 bool with_defaults)
592 {
593 struct lyd_node *root;
594
595 vty_out(vty, "Configuration:\n");
596 vty_out(vty, "!\n");
597 vty_out(vty, "frr version %s\n", FRR_VER_SHORT);
598 vty_out(vty, "frr defaults %s\n", frr_defaults_profile());
599
600 LY_LIST_FOR (config->dnode, root) {
601 nb_cli_show_dnode_cmds(vty, root, with_defaults);
602 }
603
604 vty_out(vty, "!\n");
605 vty_out(vty, "end\n");
606 }
607
608 static int nb_cli_show_config_libyang(struct vty *vty, LYD_FORMAT format,
609 struct nb_config *config,
610 struct yang_translator *translator,
611 bool with_defaults)
612 {
613 struct lyd_node *dnode;
614 char *strp;
615 int options = 0;
616
617 dnode = yang_dnode_dup(config->dnode);
618 if (translator
619 && yang_translate_dnode(translator, YANG_TRANSLATE_FROM_NATIVE,
620 &dnode)
621 != YANG_TRANSLATE_SUCCESS) {
622 vty_out(vty, "%% Failed to translate configuration\n");
623 yang_dnode_free(dnode);
624 return CMD_WARNING;
625 }
626
627 SET_FLAG(options, LYD_PRINT_WITHSIBLINGS);
628 if (with_defaults)
629 SET_FLAG(options, LYD_PRINT_WD_ALL);
630 else
631 SET_FLAG(options, LYD_PRINT_WD_TRIM);
632
633 if (lyd_print_mem(&strp, dnode, format, options) == 0 && strp) {
634 vty_out(vty, "%s", strp);
635 free(strp);
636 }
637
638 yang_dnode_free(dnode);
639
640 return CMD_SUCCESS;
641 }
642
643 static int nb_cli_show_config(struct vty *vty, struct nb_config *config,
644 enum nb_cfg_format format,
645 struct yang_translator *translator,
646 bool with_defaults)
647 {
648 nb_cli_show_config_prepare(config, with_defaults);
649
650 switch (format) {
651 case NB_CFG_FMT_CMDS:
652 nb_cli_show_config_cmds(vty, config, with_defaults);
653 break;
654 case NB_CFG_FMT_JSON:
655 return nb_cli_show_config_libyang(vty, LYD_JSON, config,
656 translator, with_defaults);
657 case NB_CFG_FMT_XML:
658 return nb_cli_show_config_libyang(vty, LYD_XML, config,
659 translator, with_defaults);
660 }
661
662 return CMD_SUCCESS;
663 }
664
665 static int nb_write_config(struct nb_config *config, enum nb_cfg_format format,
666 struct yang_translator *translator, char *path,
667 size_t pathlen)
668 {
669 int fd;
670 struct vty *file_vty;
671 int ret = 0;
672
673 snprintf(path, pathlen, "/tmp/frr.tmp.XXXXXXXX");
674 fd = mkstemp(path);
675 if (fd < 0) {
676 flog_warn(EC_LIB_SYSTEM_CALL, "%s: mkstemp() failed: %s",
677 __func__, safe_strerror(errno));
678 return -1;
679 }
680 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
681 flog_warn(EC_LIB_SYSTEM_CALL,
682 "%s: fchmod() failed: %s(%d):", __func__,
683 safe_strerror(errno), errno);
684 return -1;
685 }
686
687 /* Make vty for configuration file. */
688 file_vty = vty_new();
689 file_vty->wfd = fd;
690 file_vty->type = VTY_FILE;
691 if (config)
692 ret = nb_cli_show_config(file_vty, config, format, translator,
693 false);
694 vty_close(file_vty);
695
696 return ret;
697 }
698
699 static int nb_cli_show_config_compare(struct vty *vty,
700 struct nb_config *config1,
701 struct nb_config *config2,
702 enum nb_cfg_format format,
703 struct yang_translator *translator)
704 {
705 char config1_path[256];
706 char config2_path[256];
707 char command[BUFSIZ];
708 FILE *fp;
709 char line[1024];
710 int lineno = 0;
711
712 if (nb_write_config(config1, format, translator, config1_path,
713 sizeof(config1_path))
714 != 0) {
715 vty_out(vty, "%% Failed to process configurations.\n\n");
716 return CMD_WARNING;
717 }
718 if (nb_write_config(config2, format, translator, config2_path,
719 sizeof(config2_path))
720 != 0) {
721 vty_out(vty, "%% Failed to process configurations.\n\n");
722 unlink(config1_path);
723 return CMD_WARNING;
724 }
725
726 snprintf(command, sizeof(command), "diff -u %s %s", config1_path,
727 config2_path);
728 fp = popen(command, "r");
729 if (!fp) {
730 vty_out(vty, "%% Failed to generate configuration diff.\n\n");
731 unlink(config1_path);
732 unlink(config2_path);
733 return CMD_WARNING;
734 }
735 /* Print diff line by line. */
736 while (fgets(line, sizeof(line), fp) != NULL) {
737 if (lineno++ < 2)
738 continue;
739 vty_out(vty, "%s", line);
740 }
741 pclose(fp);
742
743 unlink(config1_path);
744 unlink(config2_path);
745
746 return CMD_SUCCESS;
747 }
748
749 /* Configure exclusively from this terminal. */
750 DEFUN (config_exclusive,
751 config_exclusive_cmd,
752 "configure exclusive",
753 "Configuration from vty interface\n"
754 "Configure exclusively from this terminal\n")
755 {
756 return vty_config_enter(vty, true, true);
757 }
758
759 /* Configure using a private candidate configuration. */
760 DEFUN (config_private,
761 config_private_cmd,
762 "configure private",
763 "Configuration from vty interface\n"
764 "Configure using a private candidate configuration\n")
765 {
766 return vty_config_enter(vty, true, false);
767 }
768
769 DEFPY (config_commit,
770 config_commit_cmd,
771 "commit [{force$force|confirmed (1-60)}]",
772 "Commit changes into the running configuration\n"
773 "Force commit even if the candidate is outdated\n"
774 "Rollback this commit unless there is a confirming commit\n"
775 "Timeout in minutes for the commit to be confirmed\n")
776 {
777 return nb_cli_commit(vty, !!force, confirmed, NULL);
778 }
779
780 DEFPY (config_commit_comment,
781 config_commit_comment_cmd,
782 "commit [{force$force|confirmed (1-60)}] comment LINE...",
783 "Commit changes into the running configuration\n"
784 "Force commit even if the candidate is outdated\n"
785 "Rollback this commit unless there is a confirming commit\n"
786 "Timeout in minutes for the commit to be confirmed\n"
787 "Assign a comment to this commit\n"
788 "Comment for this commit (Max 80 characters)\n")
789 {
790 char *comment;
791 int idx = 0;
792 int ret;
793
794 argv_find(argv, argc, "LINE", &idx);
795 comment = argv_concat(argv, argc, idx);
796 ret = nb_cli_commit(vty, !!force, confirmed, comment);
797 XFREE(MTYPE_TMP, comment);
798
799 return ret;
800 }
801
802 DEFPY (config_commit_check,
803 config_commit_check_cmd,
804 "commit check",
805 "Commit changes into the running configuration\n"
806 "Check if the configuration changes are valid\n")
807 {
808 struct nb_context context = {};
809 char errmsg[BUFSIZ] = {0};
810 int ret;
811
812 context.client = NB_CLIENT_CLI;
813 context.user = vty;
814 ret = nb_candidate_validate(&context, vty->candidate_config, errmsg,
815 sizeof(errmsg));
816 if (ret != NB_OK) {
817 vty_out(vty,
818 "%% Failed to validate candidate configuration.\n\n");
819 vty_show_nb_errors(vty, ret, errmsg);
820 return CMD_WARNING;
821 }
822
823 vty_out(vty, "%% Candidate configuration validated successfully.\n\n");
824
825 return CMD_SUCCESS;
826 }
827
828 DEFPY (config_update,
829 config_update_cmd,
830 "update",
831 "Update candidate configuration\n")
832 {
833 if (!nb_candidate_needs_update(vty->candidate_config)) {
834 vty_out(vty, "%% Update is not necessary.\n\n");
835 return CMD_SUCCESS;
836 }
837
838 if (nb_candidate_update(vty->candidate_config) != NB_OK) {
839 vty_out(vty,
840 "%% Failed to update the candidate configuration.\n\n");
841 vty_out(vty, "Please check the logs for more details.\n");
842 return CMD_WARNING;
843 }
844
845 nb_config_replace(vty->candidate_config_base, running_config, true);
846
847 vty_out(vty, "%% Candidate configuration updated successfully.\n\n");
848
849 return CMD_SUCCESS;
850 }
851
852 DEFPY (config_discard,
853 config_discard_cmd,
854 "discard",
855 "Discard changes in the candidate configuration\n")
856 {
857 nb_config_replace(vty->candidate_config, vty->candidate_config_base,
858 true);
859
860 return CMD_SUCCESS;
861 }
862
863 DEFPY (config_load,
864 config_load_cmd,
865 "configuration load\
866 <\
867 file [<json$json|xml$xml> [translate WORD$translator_family]] FILENAME$filename\
868 |transaction (1-4294967295)$tid\
869 >\
870 [replace$replace]",
871 "Configuration related settings\n"
872 "Load configuration into candidate\n"
873 "Load configuration file into candidate\n"
874 "Load configuration file in JSON format\n"
875 "Load configuration file in XML format\n"
876 "Translate configuration file\n"
877 "YANG module translator\n"
878 "Configuration file name (full path)\n"
879 "Load configuration from transaction into candidate\n"
880 "Transaction ID\n"
881 "Replace instead of merge\n")
882 {
883 if (filename) {
884 enum nb_cfg_format format;
885 struct yang_translator *translator = NULL;
886
887 if (json)
888 format = NB_CFG_FMT_JSON;
889 else if (xml)
890 format = NB_CFG_FMT_XML;
891 else
892 format = NB_CFG_FMT_CMDS;
893
894 if (translator_family) {
895 translator = yang_translator_find(translator_family);
896 if (!translator) {
897 vty_out(vty,
898 "%% Module translator \"%s\" not found\n",
899 translator_family);
900 return CMD_WARNING;
901 }
902 }
903
904 return nb_cli_candidate_load_file(vty, format, translator,
905 filename, !!replace);
906 }
907
908 return nb_cli_candidate_load_transaction(vty, tid, !!replace);
909 }
910
911 DEFPY (show_config_running,
912 show_config_running_cmd,
913 "show configuration running\
914 [<json$json|xml$xml> [translate WORD$translator_family]]\
915 [with-defaults$with_defaults]",
916 SHOW_STR
917 "Configuration information\n"
918 "Running configuration\n"
919 "Change output format to JSON\n"
920 "Change output format to XML\n"
921 "Translate output\n"
922 "YANG module translator\n"
923 "Show default values\n")
924
925 {
926 enum nb_cfg_format format;
927 struct yang_translator *translator = NULL;
928
929 if (json)
930 format = NB_CFG_FMT_JSON;
931 else if (xml)
932 format = NB_CFG_FMT_XML;
933 else
934 format = NB_CFG_FMT_CMDS;
935
936 if (translator_family) {
937 translator = yang_translator_find(translator_family);
938 if (!translator) {
939 vty_out(vty, "%% Module translator \"%s\" not found\n",
940 translator_family);
941 return CMD_WARNING;
942 }
943 }
944
945 nb_cli_show_config(vty, running_config, format, translator,
946 !!with_defaults);
947
948 return CMD_SUCCESS;
949 }
950
951 DEFPY (show_config_candidate,
952 show_config_candidate_cmd,
953 "show configuration candidate\
954 [<json$json|xml$xml> [translate WORD$translator_family]]\
955 [<\
956 with-defaults$with_defaults\
957 |changes$changes\
958 >]",
959 SHOW_STR
960 "Configuration information\n"
961 "Candidate configuration\n"
962 "Change output format to JSON\n"
963 "Change output format to XML\n"
964 "Translate output\n"
965 "YANG module translator\n"
966 "Show default values\n"
967 "Show changes applied in the candidate configuration\n")
968
969 {
970 enum nb_cfg_format format;
971 struct yang_translator *translator = NULL;
972
973 if (json)
974 format = NB_CFG_FMT_JSON;
975 else if (xml)
976 format = NB_CFG_FMT_XML;
977 else
978 format = NB_CFG_FMT_CMDS;
979
980 if (translator_family) {
981 translator = yang_translator_find(translator_family);
982 if (!translator) {
983 vty_out(vty, "%% Module translator \"%s\" not found\n",
984 translator_family);
985 return CMD_WARNING;
986 }
987 }
988
989 if (changes)
990 return nb_cli_show_config_compare(
991 vty, vty->candidate_config_base, vty->candidate_config,
992 format, translator);
993
994 nb_cli_show_config(vty, vty->candidate_config, format, translator,
995 !!with_defaults);
996
997 return CMD_SUCCESS;
998 }
999
1000 DEFPY (show_config_candidate_section,
1001 show_config_candidate_section_cmd,
1002 "show",
1003 SHOW_STR)
1004 {
1005 struct lyd_node *dnode;
1006
1007 /* Top-level configuration node, display everything. */
1008 if (vty->xpath_index == 0)
1009 return nb_cli_show_config(vty, vty->candidate_config,
1010 NB_CFG_FMT_CMDS, NULL, false);
1011
1012 /* Display only the current section of the candidate configuration. */
1013 dnode = yang_dnode_get(vty->candidate_config->dnode, VTY_CURR_XPATH);
1014 if (!dnode)
1015 /* Shouldn't happen. */
1016 return CMD_WARNING;
1017
1018 nb_cli_show_dnode_cmds(vty, dnode, 0);
1019 vty_out(vty, "!\n");
1020
1021 return CMD_SUCCESS;
1022 }
1023
1024 DEFPY (show_config_compare,
1025 show_config_compare_cmd,
1026 "show configuration compare\
1027 <\
1028 candidate$c1_candidate\
1029 |running$c1_running\
1030 |transaction (1-4294967295)$c1_tid\
1031 >\
1032 <\
1033 candidate$c2_candidate\
1034 |running$c2_running\
1035 |transaction (1-4294967295)$c2_tid\
1036 >\
1037 [<json$json|xml$xml> [translate WORD$translator_family]]",
1038 SHOW_STR
1039 "Configuration information\n"
1040 "Compare two different configurations\n"
1041 "Candidate configuration\n"
1042 "Running configuration\n"
1043 "Configuration transaction\n"
1044 "Transaction ID\n"
1045 "Candidate configuration\n"
1046 "Running configuration\n"
1047 "Configuration transaction\n"
1048 "Transaction ID\n"
1049 "Change output format to JSON\n"
1050 "Change output format to XML\n"
1051 "Translate output\n"
1052 "YANG module translator\n")
1053 {
1054 enum nb_cfg_format format;
1055 struct yang_translator *translator = NULL;
1056 struct nb_config *config1, *config_transaction1 = NULL;
1057 struct nb_config *config2, *config_transaction2 = NULL;
1058 int ret = CMD_WARNING;
1059
1060 if (c1_candidate)
1061 config1 = vty->candidate_config;
1062 else if (c1_running)
1063 config1 = running_config;
1064 else {
1065 config_transaction1 = nb_db_transaction_load(c1_tid);
1066 if (!config_transaction1) {
1067 vty_out(vty, "%% Transaction %u does not exist\n\n",
1068 (unsigned int)c1_tid);
1069 goto exit;
1070 }
1071 config1 = config_transaction1;
1072 }
1073
1074 if (c2_candidate)
1075 config2 = vty->candidate_config;
1076 else if (c2_running)
1077 config2 = running_config;
1078 else {
1079 config_transaction2 = nb_db_transaction_load(c2_tid);
1080 if (!config_transaction2) {
1081 vty_out(vty, "%% Transaction %u does not exist\n\n",
1082 (unsigned int)c2_tid);
1083 goto exit;
1084 }
1085 config2 = config_transaction2;
1086 }
1087
1088 if (json)
1089 format = NB_CFG_FMT_JSON;
1090 else if (xml)
1091 format = NB_CFG_FMT_XML;
1092 else
1093 format = NB_CFG_FMT_CMDS;
1094
1095 if (translator_family) {
1096 translator = yang_translator_find(translator_family);
1097 if (!translator) {
1098 vty_out(vty, "%% Module translator \"%s\" not found\n",
1099 translator_family);
1100 goto exit;
1101 }
1102 }
1103
1104 ret = nb_cli_show_config_compare(vty, config1, config2, format,
1105 translator);
1106 exit:
1107 if (config_transaction1)
1108 nb_config_free(config_transaction1);
1109 if (config_transaction2)
1110 nb_config_free(config_transaction2);
1111
1112 return ret;
1113 }
1114
1115 /*
1116 * Stripped down version of the "show configuration compare" command.
1117 * The "candidate" option is not present so the command can be installed in
1118 * the enable node.
1119 */
1120 ALIAS (show_config_compare,
1121 show_config_compare_without_candidate_cmd,
1122 "show configuration compare\
1123 <\
1124 running$c1_running\
1125 |transaction (1-4294967295)$c1_tid\
1126 >\
1127 <\
1128 running$c2_running\
1129 |transaction (1-4294967295)$c2_tid\
1130 >\
1131 [<json$json|xml$xml> [translate WORD$translator_family]]",
1132 SHOW_STR
1133 "Configuration information\n"
1134 "Compare two different configurations\n"
1135 "Running configuration\n"
1136 "Configuration transaction\n"
1137 "Transaction ID\n"
1138 "Running configuration\n"
1139 "Configuration transaction\n"
1140 "Transaction ID\n"
1141 "Change output format to JSON\n"
1142 "Change output format to XML\n"
1143 "Translate output\n"
1144 "YANG module translator\n")
1145
1146 DEFPY (clear_config_transactions,
1147 clear_config_transactions_cmd,
1148 "clear configuration transactions oldest (1-100)$n",
1149 CLEAR_STR
1150 "Configuration activity\n"
1151 "Delete transactions from the transactions log\n"
1152 "Delete oldest <n> transactions\n"
1153 "Number of transactions to delete\n")
1154 {
1155 #ifdef HAVE_CONFIG_ROLLBACKS
1156 if (nb_db_clear_transactions(n) != NB_OK) {
1157 vty_out(vty, "%% Failed to delete transactions.\n\n");
1158 return CMD_WARNING;
1159 }
1160 #else
1161 vty_out(vty,
1162 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1163 #endif /* HAVE_CONFIG_ROLLBACKS */
1164
1165 return CMD_SUCCESS;
1166 }
1167
1168 DEFPY (config_database_max_transactions,
1169 config_database_max_transactions_cmd,
1170 "configuration database max-transactions (1-100)$max",
1171 "Configuration related settings\n"
1172 "Configuration database\n"
1173 "Set the maximum number of transactions to store\n"
1174 "Number of transactions\n")
1175 {
1176 #ifdef HAVE_CONFIG_ROLLBACKS
1177 if (nb_db_set_max_transactions(max) != NB_OK) {
1178 vty_out(vty,
1179 "%% Failed to update the maximum number of transactions.\n\n");
1180 return CMD_WARNING;
1181 }
1182 vty_out(vty,
1183 "%% Maximum number of transactions updated successfully.\n\n");
1184 #else
1185 vty_out(vty,
1186 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1187 #endif /* HAVE_CONFIG_ROLLBACKS */
1188
1189 return CMD_SUCCESS;
1190 }
1191
1192 DEFPY (yang_module_translator_load,
1193 yang_module_translator_load_cmd,
1194 "yang module-translator load FILENAME$filename",
1195 "YANG related settings\n"
1196 "YANG module translator\n"
1197 "Load YANG module translator\n"
1198 "File name (full path)\n")
1199 {
1200 struct yang_translator *translator;
1201
1202 translator = yang_translator_load(filename);
1203 if (!translator) {
1204 vty_out(vty, "%% Failed to load \"%s\"\n\n", filename);
1205 vty_out(vty, "Please check the logs for more details.\n");
1206 return CMD_WARNING;
1207 }
1208
1209 vty_out(vty, "%% Module translator \"%s\" loaded successfully.\n\n",
1210 translator->family);
1211
1212 return CMD_SUCCESS;
1213 }
1214
1215 DEFPY (yang_module_translator_unload_family,
1216 yang_module_translator_unload_cmd,
1217 "yang module-translator unload WORD$translator_family",
1218 "YANG related settings\n"
1219 "YANG module translator\n"
1220 "Unload YANG module translator\n"
1221 "Name of the module translator\n")
1222 {
1223 struct yang_translator *translator;
1224
1225 translator = yang_translator_find(translator_family);
1226 if (!translator) {
1227 vty_out(vty, "%% Module translator \"%s\" not found\n",
1228 translator_family);
1229 return CMD_WARNING;
1230 }
1231
1232 yang_translator_unload(translator);
1233
1234 return CMD_SUCCESS;
1235 }
1236
1237 #ifdef HAVE_CONFIG_ROLLBACKS
1238 static void nb_cli_show_transactions_cb(void *arg, int transaction_id,
1239 const char *client_name,
1240 const char *date, const char *comment)
1241 {
1242 struct ttable *tt = arg;
1243
1244 ttable_add_row(tt, "%d|%s|%s|%s", transaction_id, client_name, date,
1245 comment);
1246 }
1247
1248 static int nb_cli_show_transactions(struct vty *vty)
1249 {
1250 struct ttable *tt;
1251
1252 /* Prepare table. */
1253 tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
1254 ttable_add_row(tt, "Transaction ID|Client|Date|Comment");
1255 tt->style.cell.rpad = 2;
1256 tt->style.corner = '+';
1257 ttable_restyle(tt);
1258 ttable_rowseps(tt, 0, BOTTOM, true, '-');
1259
1260 /* Fetch transactions from the northbound database. */
1261 if (nb_db_transactions_iterate(nb_cli_show_transactions_cb, tt)
1262 != NB_OK) {
1263 vty_out(vty,
1264 "%% Failed to fetch configuration transactions.\n");
1265 return CMD_WARNING;
1266 }
1267
1268 /* Dump the generated table. */
1269 if (tt->nrows > 1) {
1270 char *table;
1271
1272 table = ttable_dump(tt, "\n");
1273 vty_out(vty, "%s\n", table);
1274 XFREE(MTYPE_TMP, table);
1275 } else
1276 vty_out(vty, "No configuration transactions to display.\n\n");
1277
1278 ttable_del(tt);
1279
1280 return CMD_SUCCESS;
1281 }
1282 #endif /* HAVE_CONFIG_ROLLBACKS */
1283
1284 DEFPY (show_config_transaction,
1285 show_config_transaction_cmd,
1286 "show configuration transaction\
1287 [\
1288 (1-4294967295)$transaction_id\
1289 [<json$json|xml$xml> [translate WORD$translator_family]]\
1290 [<\
1291 with-defaults$with_defaults\
1292 |changes$changes\
1293 >]\
1294 ]",
1295 SHOW_STR
1296 "Configuration information\n"
1297 "Configuration transaction\n"
1298 "Transaction ID\n"
1299 "Change output format to JSON\n"
1300 "Change output format to XML\n"
1301 "Translate output\n"
1302 "YANG module translator\n"
1303 "Show default values\n"
1304 "Show changes compared to the previous transaction\n")
1305 {
1306 #ifdef HAVE_CONFIG_ROLLBACKS
1307 if (transaction_id) {
1308 struct nb_config *config;
1309 enum nb_cfg_format format;
1310 struct yang_translator *translator = NULL;
1311
1312 if (json)
1313 format = NB_CFG_FMT_JSON;
1314 else if (xml)
1315 format = NB_CFG_FMT_XML;
1316 else
1317 format = NB_CFG_FMT_CMDS;
1318
1319 if (translator_family) {
1320 translator = yang_translator_find(translator_family);
1321 if (!translator) {
1322 vty_out(vty,
1323 "%% Module translator \"%s\" not found\n",
1324 translator_family);
1325 return CMD_WARNING;
1326 }
1327 }
1328
1329 config = nb_db_transaction_load(transaction_id);
1330 if (!config) {
1331 vty_out(vty, "%% Transaction %u does not exist.\n\n",
1332 (unsigned int)transaction_id);
1333 return CMD_WARNING;
1334 }
1335
1336 if (changes) {
1337 struct nb_config *prev_config;
1338 int ret;
1339
1340 /* NOTE: this can be NULL. */
1341 prev_config =
1342 nb_db_transaction_load(transaction_id - 1);
1343
1344 ret = nb_cli_show_config_compare(
1345 vty, prev_config, config, format, translator);
1346 if (prev_config)
1347 nb_config_free(prev_config);
1348 nb_config_free(config);
1349
1350 return ret;
1351 }
1352
1353 nb_cli_show_config(vty, config, format, translator,
1354 !!with_defaults);
1355 nb_config_free(config);
1356
1357 return CMD_SUCCESS;
1358 }
1359
1360 return nb_cli_show_transactions(vty);
1361 #else
1362 vty_out(vty,
1363 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1364 return CMD_WARNING;
1365 #endif /* HAVE_CONFIG_ROLLBACKS */
1366 }
1367
1368 static int nb_cli_oper_data_cb(const struct lysc_node *snode,
1369 struct yang_translator *translator,
1370 struct yang_data *data, void *arg)
1371 {
1372 struct lyd_node *dnode = arg;
1373 struct ly_ctx *ly_ctx;
1374
1375 if (translator) {
1376 int ret;
1377
1378 ret = yang_translate_xpath(translator,
1379 YANG_TRANSLATE_FROM_NATIVE,
1380 data->xpath, sizeof(data->xpath));
1381 switch (ret) {
1382 case YANG_TRANSLATE_SUCCESS:
1383 break;
1384 case YANG_TRANSLATE_NOTFOUND:
1385 goto exit;
1386 case YANG_TRANSLATE_FAILURE:
1387 goto error;
1388 }
1389
1390 ly_ctx = translator->ly_ctx;
1391 } else
1392 ly_ctx = ly_native_ctx;
1393
1394 LY_ERR err =
1395 lyd_new_path(dnode, ly_ctx, data->xpath, (void *)data->value,
1396 LYD_NEW_PATH_UPDATE, &dnode);
1397 if (err) {
1398 flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path(%s) failed: %s",
1399 __func__, data->xpath, ly_errmsg(ly_native_ctx));
1400 goto error;
1401 }
1402
1403 exit:
1404 yang_data_free(data);
1405 return NB_OK;
1406
1407 error:
1408 yang_data_free(data);
1409 return NB_ERR;
1410 }
1411
1412 DEFPY (show_yang_operational_data,
1413 show_yang_operational_data_cmd,
1414 "show yang operational-data XPATH$xpath\
1415 [{\
1416 format <json$json|xml$xml>\
1417 |translate WORD$translator_family\
1418 |with-config$with_config\
1419 }]",
1420 SHOW_STR
1421 "YANG information\n"
1422 "Show YANG operational data\n"
1423 "XPath expression specifying the YANG data path\n"
1424 "Set the output format\n"
1425 "JavaScript Object Notation\n"
1426 "Extensible Markup Language\n"
1427 "Translate operational data\n"
1428 "YANG module translator\n"
1429 "Merge configuration data\n")
1430 {
1431 LYD_FORMAT format;
1432 struct yang_translator *translator = NULL;
1433 struct ly_ctx *ly_ctx;
1434 struct lyd_node *dnode;
1435 char *strp;
1436 uint32_t print_options = LYD_PRINT_WITHSIBLINGS;
1437 int ret;
1438
1439 if (xml)
1440 format = LYD_XML;
1441 else
1442 format = LYD_JSON;
1443
1444 if (translator_family) {
1445 translator = yang_translator_find(translator_family);
1446 if (!translator) {
1447 vty_out(vty, "%% Module translator \"%s\" not found\n",
1448 translator_family);
1449 return CMD_WARNING;
1450 }
1451
1452 ly_ctx = translator->ly_ctx;
1453 } else
1454 ly_ctx = ly_native_ctx;
1455
1456 /* Obtain data. */
1457 dnode = yang_dnode_new(ly_ctx, false);
1458 ret = nb_oper_data_iterate(xpath, translator, 0, nb_cli_oper_data_cb,
1459 dnode);
1460 if (ret != NB_OK) {
1461 if (format == LYD_JSON)
1462 vty_out(vty, "{}\n");
1463 else {
1464 /* embed ly_last_errmsg() when we get newer libyang */
1465 vty_out(vty, "<!-- Not found -->\n");
1466 }
1467 yang_dnode_free(dnode);
1468 return CMD_WARNING;
1469 }
1470
1471 if (with_config && yang_dnode_exists(running_config->dnode, xpath)) {
1472 struct lyd_node *config_dnode =
1473 yang_dnode_get(running_config->dnode, xpath);
1474 if (config_dnode != NULL) {
1475 lyd_merge_tree(&dnode, yang_dnode_dup(config_dnode),
1476 LYD_MERGE_DESTRUCT);
1477 print_options |= LYD_PRINT_WD_ALL;
1478 }
1479 }
1480
1481 (void)lyd_validate_all(&dnode, ly_ctx, 0, NULL);
1482
1483 /* Display the data. */
1484 if (lyd_print_mem(&strp, dnode, format, print_options) != 0 || !strp) {
1485 vty_out(vty, "%% Failed to display operational data.\n");
1486 yang_dnode_free(dnode);
1487 return CMD_WARNING;
1488 }
1489 vty_out(vty, "%s", strp);
1490 free(strp);
1491 yang_dnode_free(dnode);
1492
1493 return CMD_SUCCESS;
1494 }
1495
1496 DEFPY (show_yang_module,
1497 show_yang_module_cmd,
1498 "show yang module [module-translator WORD$translator_family]",
1499 SHOW_STR
1500 "YANG information\n"
1501 "Show loaded modules\n"
1502 "YANG module translator\n"
1503 "YANG module translator\n")
1504 {
1505 struct ly_ctx *ly_ctx;
1506 struct yang_translator *translator = NULL;
1507 const struct lys_module *module;
1508 struct ttable *tt;
1509 uint32_t idx = 0;
1510
1511 if (translator_family) {
1512 translator = yang_translator_find(translator_family);
1513 if (!translator) {
1514 vty_out(vty, "%% Module translator \"%s\" not found\n",
1515 translator_family);
1516 return CMD_WARNING;
1517 }
1518 ly_ctx = translator->ly_ctx;
1519 } else
1520 ly_ctx = ly_native_ctx;
1521
1522 /* Prepare table. */
1523 tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
1524 ttable_add_row(tt, "Module|Version|Revision|Flags|Namespace");
1525 tt->style.cell.rpad = 2;
1526 tt->style.corner = '+';
1527 ttable_restyle(tt);
1528 ttable_rowseps(tt, 0, BOTTOM, true, '-');
1529
1530 while ((module = ly_ctx_get_module_iter(ly_ctx, &idx))) {
1531 char flags[8];
1532
1533 snprintf(flags, sizeof(flags), "%c%c",
1534 module->implemented ? 'I' : ' ',
1535 LY_ARRAY_COUNT(module->deviated_by) ? 'D' : ' ');
1536
1537 ttable_add_row(tt, "%s|%s|%s|%s|%s", module->name,
1538 (module->parsed->version == 2) ? "1.1" : "1.0",
1539 module->revision ? module->revision : "-", flags,
1540 module->ns);
1541 }
1542
1543 /* Dump the generated table. */
1544 if (tt->nrows > 1) {
1545 char *table;
1546
1547 vty_out(vty, " Flags: I - Implemented, D - Deviated\n\n");
1548
1549 table = ttable_dump(tt, "\n");
1550 vty_out(vty, "%s\n", table);
1551 XFREE(MTYPE_TMP, table);
1552 } else
1553 vty_out(vty, "No YANG modules to display.\n\n");
1554
1555 ttable_del(tt);
1556
1557 return CMD_SUCCESS;
1558 }
1559
1560 DEFPY(show_yang_module_detail, show_yang_module_detail_cmd,
1561 "show yang module\
1562 [module-translator WORD$translator_family]\
1563 WORD$module_name <compiled$compiled|summary|tree$tree|yang$yang|yin$yin>",
1564 SHOW_STR
1565 "YANG information\n"
1566 "Show loaded modules\n"
1567 "YANG module translator\n"
1568 "YANG module translator\n"
1569 "Module name\n"
1570 "Display compiled module in YANG format\n"
1571 "Display summary information about the module\n"
1572 "Display module in the tree (RFC 8340) format\n"
1573 "Display module in the YANG format\n"
1574 "Display module in the YIN format\n")
1575 {
1576 struct ly_ctx *ly_ctx;
1577 struct yang_translator *translator = NULL;
1578 const struct lys_module *module;
1579 LYS_OUTFORMAT format;
1580 char *strp;
1581
1582 if (translator_family) {
1583 translator = yang_translator_find(translator_family);
1584 if (!translator) {
1585 vty_out(vty, "%% Module translator \"%s\" not found\n",
1586 translator_family);
1587 return CMD_WARNING;
1588 }
1589 ly_ctx = translator->ly_ctx;
1590 } else
1591 ly_ctx = ly_native_ctx;
1592
1593 module = ly_ctx_get_module_latest(ly_ctx, module_name);
1594 if (!module) {
1595 vty_out(vty, "%% Module \"%s\" not found\n", module_name);
1596 return CMD_WARNING;
1597 }
1598
1599 if (yang)
1600 format = LYS_OUT_YANG;
1601 else if (yin)
1602 format = LYS_OUT_YIN;
1603 else if (compiled)
1604 format = LYS_OUT_YANG_COMPILED;
1605 else if (tree)
1606 format = LYS_OUT_TREE;
1607 else {
1608 vty_out(vty,
1609 "%% libyang v2 does not currently support summary\n");
1610 return CMD_WARNING;
1611 }
1612
1613 if (lys_print_mem(&strp, module, format, 0) == 0) {
1614 vty_out(vty, "%s\n", strp);
1615 free(strp);
1616 } else {
1617 /* Unexpected. */
1618 vty_out(vty, "%% Error generating module information\n");
1619 return CMD_WARNING;
1620 }
1621
1622 return CMD_SUCCESS;
1623 }
1624
1625 DEFPY (show_yang_module_translator,
1626 show_yang_module_translator_cmd,
1627 "show yang module-translator",
1628 SHOW_STR
1629 "YANG information\n"
1630 "Show loaded YANG module translators\n")
1631 {
1632 struct yang_translator *translator;
1633 struct ttable *tt;
1634
1635 /* Prepare table. */
1636 tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
1637 ttable_add_row(tt, "Family|Module|Deviations|Coverage (%%)");
1638 tt->style.cell.rpad = 2;
1639 tt->style.corner = '+';
1640 ttable_restyle(tt);
1641 ttable_rowseps(tt, 0, BOTTOM, true, '-');
1642
1643 RB_FOREACH (translator, yang_translators, &yang_translators) {
1644 struct yang_tmodule *tmodule;
1645 struct listnode *ln;
1646
1647 for (ALL_LIST_ELEMENTS_RO(translator->modules, ln, tmodule)) {
1648 ttable_add_row(tt, "%s|%s|%s|%.2f", translator->family,
1649 tmodule->module->name,
1650 tmodule->deviations->name,
1651 tmodule->coverage);
1652 }
1653 }
1654
1655 /* Dump the generated table. */
1656 if (tt->nrows > 1) {
1657 char *table;
1658
1659 table = ttable_dump(tt, "\n");
1660 vty_out(vty, "%s\n", table);
1661 XFREE(MTYPE_TMP, table);
1662 } else
1663 vty_out(vty, "No YANG module translators to display.\n\n");
1664
1665 ttable_del(tt);
1666
1667 return CMD_SUCCESS;
1668 }
1669
1670 #ifdef HAVE_CONFIG_ROLLBACKS
1671 static int nb_cli_rollback_configuration(struct vty *vty,
1672 uint32_t transaction_id)
1673 {
1674 struct nb_context context = {};
1675 struct nb_config *candidate;
1676 char comment[80];
1677 char errmsg[BUFSIZ] = {0};
1678 int ret;
1679
1680 candidate = nb_db_transaction_load(transaction_id);
1681 if (!candidate) {
1682 vty_out(vty, "%% Transaction %u does not exist.\n\n",
1683 transaction_id);
1684 return CMD_WARNING;
1685 }
1686
1687 snprintf(comment, sizeof(comment), "Rollback to transaction %u",
1688 transaction_id);
1689
1690 context.client = NB_CLIENT_CLI;
1691 context.user = vty;
1692 ret = nb_candidate_commit(context, candidate, true, comment, NULL,
1693 errmsg, sizeof(errmsg));
1694 nb_config_free(candidate);
1695 switch (ret) {
1696 case NB_OK:
1697 vty_out(vty,
1698 "%% Configuration was successfully rolled back.\n\n");
1699 /* Print warnings (if any). */
1700 if (strlen(errmsg) > 0)
1701 vty_out(vty, "%s\n", errmsg);
1702 return CMD_SUCCESS;
1703 case NB_ERR_NO_CHANGES:
1704 vty_out(vty,
1705 "%% Aborting - no configuration changes detected.\n\n");
1706 return CMD_WARNING;
1707 default:
1708 vty_out(vty, "%% Rollback failed.\n\n");
1709 vty_show_nb_errors(vty, ret, errmsg);
1710 return CMD_WARNING;
1711 }
1712 }
1713 #endif /* HAVE_CONFIG_ROLLBACKS */
1714
1715 DEFPY (rollback_config,
1716 rollback_config_cmd,
1717 "rollback configuration (1-4294967295)$transaction_id",
1718 "Rollback to a previous state\n"
1719 "Running configuration\n"
1720 "Transaction ID\n")
1721 {
1722 #ifdef HAVE_CONFIG_ROLLBACKS
1723 return nb_cli_rollback_configuration(vty, transaction_id);
1724 #else
1725 vty_out(vty,
1726 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1727 return CMD_SUCCESS;
1728 #endif /* HAVE_CONFIG_ROLLBACKS */
1729 }
1730
1731 /* Debug CLI commands. */
1732 static struct debug *nb_debugs[] = {
1733 &nb_dbg_cbs_config, &nb_dbg_cbs_state, &nb_dbg_cbs_rpc,
1734 &nb_dbg_notif, &nb_dbg_events, &nb_dbg_libyang,
1735 };
1736
1737 static const char *const nb_debugs_conflines[] = {
1738 "debug northbound callbacks configuration",
1739 "debug northbound callbacks state",
1740 "debug northbound callbacks rpc",
1741 "debug northbound notifications",
1742 "debug northbound events",
1743 "debug northbound libyang",
1744 };
1745
1746 DEFINE_HOOK(nb_client_debug_set_all, (uint32_t flags, bool set), (flags, set));
1747
1748 static void nb_debug_set_all(uint32_t flags, bool set)
1749 {
1750 for (unsigned int i = 0; i < array_size(nb_debugs); i++) {
1751 DEBUG_FLAGS_SET(nb_debugs[i], flags, set);
1752
1753 /* If all modes have been turned off, don't preserve options. */
1754 if (!DEBUG_MODE_CHECK(nb_debugs[i], DEBUG_MODE_ALL))
1755 DEBUG_CLEAR(nb_debugs[i]);
1756 }
1757
1758 hook_call(nb_client_debug_set_all, flags, set);
1759 }
1760
1761 DEFPY (debug_nb,
1762 debug_nb_cmd,
1763 "[no] debug northbound\
1764 [<\
1765 callbacks$cbs [{configuration$cbs_cfg|state$cbs_state|rpc$cbs_rpc}]\
1766 |notifications$notifications\
1767 |events$events\
1768 |libyang$libyang\
1769 >]",
1770 NO_STR
1771 DEBUG_STR
1772 "Northbound debugging\n"
1773 "Callbacks\n"
1774 "Configuration\n"
1775 "State\n"
1776 "RPC\n"
1777 "Notifications\n"
1778 "Events\n"
1779 "libyang debugging\n")
1780 {
1781 uint32_t mode = DEBUG_NODE2MODE(vty->node);
1782
1783 if (cbs) {
1784 bool none = (!cbs_cfg && !cbs_state && !cbs_rpc);
1785
1786 if (none || cbs_cfg)
1787 DEBUG_MODE_SET(&nb_dbg_cbs_config, mode, !no);
1788 if (none || cbs_state)
1789 DEBUG_MODE_SET(&nb_dbg_cbs_state, mode, !no);
1790 if (none || cbs_rpc)
1791 DEBUG_MODE_SET(&nb_dbg_cbs_rpc, mode, !no);
1792 }
1793 if (notifications)
1794 DEBUG_MODE_SET(&nb_dbg_notif, mode, !no);
1795 if (events)
1796 DEBUG_MODE_SET(&nb_dbg_events, mode, !no);
1797 if (libyang) {
1798 DEBUG_MODE_SET(&nb_dbg_libyang, mode, !no);
1799 yang_debugging_set(!no);
1800 }
1801
1802 /* no specific debug --> act on all of them */
1803 if (strmatch(argv[argc - 1]->text, "northbound")) {
1804 nb_debug_set_all(mode, !no);
1805 yang_debugging_set(!no);
1806 }
1807
1808 return CMD_SUCCESS;
1809 }
1810
1811 DEFINE_HOOK(nb_client_debug_config_write, (struct vty *vty), (vty));
1812
1813 static int nb_debug_config_write(struct vty *vty)
1814 {
1815 for (unsigned int i = 0; i < array_size(nb_debugs); i++)
1816 if (DEBUG_MODE_CHECK(nb_debugs[i], DEBUG_MODE_CONF))
1817 vty_out(vty, "%s\n", nb_debugs_conflines[i]);
1818
1819 hook_call(nb_client_debug_config_write, vty);
1820
1821 return 1;
1822 }
1823
1824 static struct debug_callbacks nb_dbg_cbs = {.debug_set_all = nb_debug_set_all};
1825 static struct cmd_node nb_debug_node = {
1826 .name = "northbound debug",
1827 .node = NORTHBOUND_DEBUG_NODE,
1828 .prompt = "",
1829 .config_write = nb_debug_config_write,
1830 };
1831
1832 void nb_cli_install_default(int node)
1833 {
1834 _install_element(node, &show_config_candidate_section_cmd);
1835
1836 if (frr_get_cli_mode() != FRR_CLI_TRANSACTIONAL)
1837 return;
1838
1839 _install_element(node, &config_commit_cmd);
1840 _install_element(node, &config_commit_comment_cmd);
1841 _install_element(node, &config_commit_check_cmd);
1842 _install_element(node, &config_update_cmd);
1843 _install_element(node, &config_discard_cmd);
1844 _install_element(node, &show_config_running_cmd);
1845 _install_element(node, &show_config_candidate_cmd);
1846 _install_element(node, &show_config_compare_cmd);
1847 _install_element(node, &show_config_transaction_cmd);
1848 }
1849
1850 /* YANG module autocomplete. */
1851 static void yang_module_autocomplete(vector comps, struct cmd_token *token)
1852 {
1853 const struct lys_module *module;
1854 struct yang_translator *module_tr;
1855 uint32_t idx;
1856
1857 idx = 0;
1858 while ((module = ly_ctx_get_module_iter(ly_native_ctx, &idx)))
1859 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, module->name));
1860
1861 RB_FOREACH (module_tr, yang_translators, &yang_translators) {
1862 idx = 0;
1863 while ((module = ly_ctx_get_module_iter(module_tr->ly_ctx,
1864 &idx)))
1865 vector_set(comps,
1866 XSTRDUP(MTYPE_COMPLETION, module->name));
1867 }
1868 }
1869
1870 /* YANG module translator autocomplete. */
1871 static void yang_translator_autocomplete(vector comps, struct cmd_token *token)
1872 {
1873 struct yang_translator *module_tr;
1874
1875 RB_FOREACH (module_tr, yang_translators, &yang_translators)
1876 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, module_tr->family));
1877 }
1878
1879 static const struct cmd_variable_handler yang_var_handlers[] = {
1880 {.varname = "module_name", .completions = yang_module_autocomplete},
1881 {.varname = "translator_family",
1882 .completions = yang_translator_autocomplete},
1883 {.completions = NULL}};
1884
1885 void nb_cli_init(struct event_loop *tm)
1886 {
1887 master = tm;
1888
1889 /* Initialize the shared candidate configuration. */
1890 vty_shared_candidate_config = nb_config_new(NULL);
1891
1892 debug_init(&nb_dbg_cbs);
1893
1894 install_node(&nb_debug_node);
1895 install_element(ENABLE_NODE, &debug_nb_cmd);
1896 install_element(CONFIG_NODE, &debug_nb_cmd);
1897
1898 /* Install commands specific to the transaction-base mode. */
1899 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL) {
1900 install_element(ENABLE_NODE, &config_exclusive_cmd);
1901 install_element(ENABLE_NODE, &config_private_cmd);
1902 install_element(ENABLE_NODE,
1903 &show_config_compare_without_candidate_cmd);
1904 install_element(ENABLE_NODE, &show_config_transaction_cmd);
1905 install_element(ENABLE_NODE, &rollback_config_cmd);
1906 install_element(ENABLE_NODE, &clear_config_transactions_cmd);
1907
1908 install_element(CONFIG_NODE, &config_load_cmd);
1909 install_element(CONFIG_NODE,
1910 &config_database_max_transactions_cmd);
1911 }
1912
1913 /* Other commands. */
1914 install_element(ENABLE_NODE, &show_config_running_cmd);
1915 install_element(CONFIG_NODE, &yang_module_translator_load_cmd);
1916 install_element(CONFIG_NODE, &yang_module_translator_unload_cmd);
1917 install_element(ENABLE_NODE, &show_yang_operational_data_cmd);
1918 install_element(ENABLE_NODE, &show_yang_module_cmd);
1919 install_element(ENABLE_NODE, &show_yang_module_detail_cmd);
1920 install_element(ENABLE_NODE, &show_yang_module_translator_cmd);
1921 cmd_variable_handler_register(yang_var_handlers);
1922 }
1923
1924 void nb_cli_terminate(void)
1925 {
1926 nb_config_free(vty_shared_candidate_config);
1927 }