1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
10 #include "lib/version.h"
13 #include "lib_errors.h"
15 #include "termtable.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"
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"};
31 struct nb_config
*vty_shared_candidate_config
;
32 static struct thread_master
*master
;
34 static void vty_show_nb_errors(struct vty
*vty
, int error
, const char *errmsg
)
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
);
41 static int nb_cli_classic_commit(struct vty
*vty
)
43 struct nb_context context
= {};
44 char errmsg
[BUFSIZ
] = {0};
47 context
.client
= NB_CLIENT_CLI
;
49 ret
= nb_candidate_commit(context
, vty
->candidate_config
, true, NULL
,
50 NULL
, errmsg
, sizeof(errmsg
));
53 /* Successful commit. Print warnings (if any). */
54 if (strlen(errmsg
) > 0)
55 vty_out(vty
, "%s\n", errmsg
);
57 case NB_ERR_NO_CHANGES
:
60 vty_out(vty
, "%% Configuration failed.\n\n");
61 vty_show_nb_errors(vty
, ret
, errmsg
);
62 if (vty
->pending_commit
)
64 "The following commands were dynamically grouped into the same transaction and rejected:\n%s",
65 vty
->pending_cmds_buf
);
67 /* Regenerate candidate for consistency. */
68 nb_config_replace(vty
->candidate_config
, running_config
, true);
69 return CMD_WARNING_CONFIG_FAILED
;
75 static void nb_cli_pending_commit_clear(struct vty
*vty
)
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;
83 int nb_cli_pending_commit_check(struct vty
*vty
)
85 int ret
= CMD_SUCCESS
;
87 if (vty
->pending_commit
) {
88 ret
= nb_cli_classic_commit(vty
);
89 nb_cli_pending_commit_clear(vty
);
95 static int nb_cli_schedule_command(struct vty
*vty
)
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
);
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
);
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
);
114 /* Schedule the commit operation. */
115 vty
->pending_commit
= 1;
120 void nb_cli_enqueue_change(struct vty
*vty
, const char *xpath
,
121 enum nb_operation operation
, const char *value
)
123 struct nb_cfg_change
*change
;
125 if (vty
->num_cfg_changes
== VTY_MAXCFGCHANGES
) {
126 /* Not expected to happen. */
128 "%% Exceeded the maximum number of changes (%u) for a single command\n\n",
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
;
139 static int nb_cli_apply_changes_internal(struct vty
*vty
,
140 const char *xpath_base
,
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
),
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.
158 vty_out(vty
, "%s", buf
);
162 * Maybe do an implicit commit when using the classic CLI mode.
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
170 if (frr_get_cli_mode() == FRR_CLI_CLASSIC
) {
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
);
183 int nb_cli_apply_changes(struct vty
*vty
, const char *xpath_base_fmt
, ...)
185 char xpath_base
[XPATH_MAXLEN
] = {};
186 bool implicit_commit
;
189 /* Parse the base XPath format string. */
190 if (xpath_base_fmt
) {
193 va_start(ap
, xpath_base_fmt
);
194 vsnprintf(xpath_base
, sizeof(xpath_base
), xpath_base_fmt
, ap
);
198 if (vty_mgmt_fe_enabled()) {
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
++;
208 return nb_cli_apply_changes_internal(vty
, xpath_base
, false);
211 int nb_cli_apply_changes_clear_pending(struct vty
*vty
,
212 const char *xpath_base_fmt
, ...)
214 char xpath_base
[XPATH_MAXLEN
] = {};
215 bool implicit_commit
;
218 /* Parse the base XPath format string. */
219 if (xpath_base_fmt
) {
222 va_start(ap
, xpath_base_fmt
);
223 vsnprintf(xpath_base
, sizeof(xpath_base
), xpath_base_fmt
, ap
);
227 if (vty_mgmt_fe_enabled()) {
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
++;
237 return nb_cli_apply_changes_internal(vty
, xpath_base
, true);
240 int nb_cli_rpc(struct vty
*vty
, const char *xpath
, struct list
*input
,
243 struct nb_node
*nb_node
;
245 char errmsg
[BUFSIZ
] = {0};
247 nb_node
= nb_node_find(xpath
);
249 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
250 "%s: unknown data path: %s", __func__
, xpath
);
254 ret
= nb_callback_rpc(nb_node
, xpath
, input
, output
, errmsg
,
261 vty_show_nb_errors(vty
, ret
, errmsg
);
266 void nb_cli_confirmed_commit_clean(struct vty
*vty
)
268 thread_cancel(&vty
->t_confirmed_commit_timeout
);
269 nb_config_free(vty
->confirmed_commit_rollback
);
270 vty
->confirmed_commit_rollback
= NULL
;
273 int nb_cli_confirmed_commit_rollback(struct vty
*vty
)
275 struct nb_context context
= {};
276 uint32_t transaction_id
;
277 char errmsg
[BUFSIZ
] = {0};
280 /* Perform the rollback. */
281 context
.client
= NB_CLIENT_CLI
;
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
));
289 "Rollback performed successfully (Transaction ID #%u).\n",
291 /* Print warnings (if any). */
292 if (strlen(errmsg
) > 0)
293 vty_out(vty
, "%s\n", errmsg
);
296 "Failed to rollback to previous configuration.\n\n");
297 vty_show_nb_errors(vty
, ret
, errmsg
);
303 static void nb_cli_confirmed_commit_timeout(struct thread
*thread
)
305 struct vty
*vty
= THREAD_ARG(thread
);
307 /* XXX: broadcast this message to all logged-in users? */
309 "\nConfirmed commit has timed out, rolling back to previous configuration.\n\n");
311 nb_cli_confirmed_commit_rollback(vty
);
312 nb_cli_confirmed_commit_clean(vty
);
315 static int nb_cli_commit(struct vty
*vty
, bool force
,
316 unsigned int confirmed_timeout
, char *comment
)
318 struct nb_context context
= {};
319 uint32_t transaction_id
= 0;
320 char errmsg
[BUFSIZ
] = {0};
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. */
328 "%% Resetting confirmed-commit timeout to %u minute(s)\n\n",
331 thread_cancel(&vty
->t_confirmed_commit_timeout
);
332 thread_add_timer(master
,
333 nb_cli_confirmed_commit_timeout
, vty
,
334 confirmed_timeout
* 60,
335 &vty
->t_confirmed_commit_timeout
);
337 /* Accept commit confirmation. */
338 vty_out(vty
, "%% Commit complete.\n\n");
339 nb_cli_confirmed_commit_clean(vty
);
344 /* "force" parameter. */
345 if (!force
&& nb_candidate_needs_update(vty
->candidate_config
)) {
347 "%% Candidate configuration needs to be updated before commit.\n\n");
349 "Use the \"update\" command or \"commit force\".\n");
353 /* "confirm" parameter. */
354 if (confirmed_timeout
) {
355 vty
->confirmed_commit_rollback
= nb_config_dup(running_config
);
357 vty
->t_confirmed_commit_timeout
= NULL
;
358 thread_add_timer(master
, nb_cli_confirmed_commit_timeout
, vty
,
359 confirmed_timeout
* 60,
360 &vty
->t_confirmed_commit_timeout
);
363 context
.client
= NB_CLIENT_CLI
;
365 ret
= nb_candidate_commit(context
, vty
->candidate_config
, true, comment
,
366 &transaction_id
, errmsg
, sizeof(errmsg
));
368 /* Map northbound return code to CLI return code. */
371 nb_config_replace(vty
->candidate_config_base
, running_config
,
374 "%% Configuration committed successfully (Transaction ID #%u).\n\n",
376 /* Print warnings (if any). */
377 if (strlen(errmsg
) > 0)
378 vty_out(vty
, "%s\n", errmsg
);
380 case NB_ERR_NO_CHANGES
:
381 vty_out(vty
, "%% No configuration changes to commit.\n\n");
385 "%% Failed to commit candidate configuration.\n\n");
386 vty_show_nb_errors(vty
, ret
, errmsg
);
391 static int nb_cli_candidate_load_file(struct vty
*vty
,
392 enum nb_cfg_format format
,
393 struct yang_translator
*translator
,
394 const char *path
, bool replace
)
396 struct nb_config
*loaded_config
= NULL
;
397 struct lyd_node
*dnode
;
398 struct ly_ctx
*ly_ctx
;
404 case NB_CFG_FMT_CMDS
:
405 loaded_config
= nb_config_new(NULL
);
406 if (!vty_read_config(loaded_config
, path
, config_default
)) {
407 vty_out(vty
, "%% Failed to load configuration.\n\n");
409 "Please check the logs for more details.\n");
410 nb_config_free(loaded_config
);
414 case NB_CFG_FMT_JSON
:
416 ly_format
= (format
== NB_CFG_FMT_JSON
) ? LYD_JSON
: LYD_XML
;
418 ly_ctx
= translator
? translator
->ly_ctx
: ly_native_ctx
;
419 err
= lyd_parse_data_path(ly_ctx
, path
, ly_format
,
420 LYD_PARSE_ONLY
| LYD_PARSE_NO_STATE
,
423 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_parse_path() failed",
425 vty_out(vty
, "%% Failed to load configuration:\n\n");
427 yang_print_errors(ly_native_ctx
, buf
,
432 && yang_translate_dnode(translator
,
433 YANG_TRANSLATE_TO_NATIVE
, &dnode
)
434 != YANG_TRANSLATE_SUCCESS
) {
435 vty_out(vty
, "%% Failed to translate configuration\n");
436 yang_dnode_free(dnode
);
439 loaded_config
= nb_config_new(dnode
);
444 nb_config_replace(vty
->candidate_config
, loaded_config
, false);
445 else if (nb_config_merge(vty
->candidate_config
, loaded_config
, false)
448 "%% Failed to merge the loaded configuration:\n\n");
450 yang_print_errors(ly_native_ctx
, buf
, sizeof(buf
)));
457 static int nb_cli_candidate_load_transaction(struct vty
*vty
,
458 uint32_t transaction_id
,
461 struct nb_config
*loaded_config
;
464 loaded_config
= nb_db_transaction_load(transaction_id
);
465 if (!loaded_config
) {
466 vty_out(vty
, "%% Transaction %u does not exist.\n\n",
472 nb_config_replace(vty
->candidate_config
, loaded_config
, false);
473 else if (nb_config_merge(vty
->candidate_config
, loaded_config
, false)
476 "%% Failed to merge the loaded configuration:\n\n");
478 yang_print_errors(ly_native_ctx
, buf
, sizeof(buf
)));
485 /* Prepare the configuration for display. */
486 void nb_cli_show_config_prepare(struct nb_config
*config
, bool with_defaults
)
488 /* Nothing to do for daemons that don't implement any YANG module. */
489 if (config
->dnode
== NULL
)
493 * Call lyd_validate() only to create default child nodes, ignoring
494 * any possible validation error. This doesn't need to be done when
495 * displaying the running configuration since it's always fully
498 if (config
!= running_config
)
499 (void)lyd_validate_all(&config
->dnode
, ly_native_ctx
,
500 LYD_VALIDATE_NO_STATE
, NULL
);
503 static int lyd_node_cmp(const struct lyd_node
**dnode1
,
504 const struct lyd_node
**dnode2
)
506 struct nb_node
*nb_node
= (*dnode1
)->schema
->priv
;
508 return nb_node
->cbs
.cli_cmp(*dnode1
, *dnode2
);
511 static void show_dnode_children_cmds(struct vty
*vty
,
512 const struct lyd_node
*root
,
515 struct nb_node
*nb_node
, *sort_node
= NULL
;
516 struct listnode
*listnode
;
517 struct lyd_node
*child
;
518 struct list
*sort_list
;
521 LY_LIST_FOR (lyd_child(root
), child
) {
522 nb_node
= child
->schema
->priv
;
525 * We finished processing current list,
526 * it's time to print the config.
528 if (sort_node
&& nb_node
!= sort_node
) {
530 (int (*)(const void **,
531 const void **))lyd_node_cmp
);
533 for (ALL_LIST_ELEMENTS_RO(sort_list
, listnode
, data
))
534 nb_cli_show_dnode_cmds(vty
, data
,
537 list_delete(&sort_list
);
542 * If the config needs to be sorted,
543 * then add the dnode to the sorting
544 * list for later processing.
546 if (nb_node
&& nb_node
->cbs
.cli_cmp
) {
549 sort_list
= list_new();
552 listnode_add(sort_list
, child
);
556 nb_cli_show_dnode_cmds(vty
, child
, with_defaults
);
561 (int (*)(const void **, const void **))lyd_node_cmp
);
563 for (ALL_LIST_ELEMENTS_RO(sort_list
, listnode
, data
))
564 nb_cli_show_dnode_cmds(vty
, data
, with_defaults
);
566 list_delete(&sort_list
);
571 void nb_cli_show_dnode_cmds(struct vty
*vty
, const struct lyd_node
*root
,
574 struct nb_node
*nb_node
;
576 if (!with_defaults
&& yang_dnode_is_default_recursive(root
))
579 nb_node
= root
->schema
->priv
;
581 if (nb_node
&& nb_node
->cbs
.cli_show
)
582 (*nb_node
->cbs
.cli_show
)(vty
, root
, with_defaults
);
584 if (!(root
->schema
->nodetype
& (LYS_LEAF
| LYS_LEAFLIST
| LYS_ANYDATA
)))
585 show_dnode_children_cmds(vty
, root
, with_defaults
);
587 if (nb_node
&& nb_node
->cbs
.cli_show_end
)
588 (*nb_node
->cbs
.cli_show_end
)(vty
, root
);
591 static void nb_cli_show_config_cmds(struct vty
*vty
, struct nb_config
*config
,
594 struct lyd_node
*root
;
596 vty_out(vty
, "Configuration:\n");
598 vty_out(vty
, "frr version %s\n", FRR_VER_SHORT
);
599 vty_out(vty
, "frr defaults %s\n", frr_defaults_profile());
601 LY_LIST_FOR (config
->dnode
, root
) {
602 nb_cli_show_dnode_cmds(vty
, root
, with_defaults
);
606 vty_out(vty
, "end\n");
609 static int nb_cli_show_config_libyang(struct vty
*vty
, LYD_FORMAT format
,
610 struct nb_config
*config
,
611 struct yang_translator
*translator
,
614 struct lyd_node
*dnode
;
618 dnode
= yang_dnode_dup(config
->dnode
);
620 && yang_translate_dnode(translator
, YANG_TRANSLATE_FROM_NATIVE
,
622 != YANG_TRANSLATE_SUCCESS
) {
623 vty_out(vty
, "%% Failed to translate configuration\n");
624 yang_dnode_free(dnode
);
628 SET_FLAG(options
, LYD_PRINT_WITHSIBLINGS
);
630 SET_FLAG(options
, LYD_PRINT_WD_ALL
);
632 SET_FLAG(options
, LYD_PRINT_WD_TRIM
);
634 if (lyd_print_mem(&strp
, dnode
, format
, options
) == 0 && strp
) {
635 vty_out(vty
, "%s", strp
);
639 yang_dnode_free(dnode
);
644 static int nb_cli_show_config(struct vty
*vty
, struct nb_config
*config
,
645 enum nb_cfg_format format
,
646 struct yang_translator
*translator
,
649 nb_cli_show_config_prepare(config
, with_defaults
);
652 case NB_CFG_FMT_CMDS
:
653 nb_cli_show_config_cmds(vty
, config
, with_defaults
);
655 case NB_CFG_FMT_JSON
:
656 return nb_cli_show_config_libyang(vty
, LYD_JSON
, config
,
657 translator
, with_defaults
);
659 return nb_cli_show_config_libyang(vty
, LYD_XML
, config
,
660 translator
, with_defaults
);
666 static int nb_write_config(struct nb_config
*config
, enum nb_cfg_format format
,
667 struct yang_translator
*translator
, char *path
,
671 struct vty
*file_vty
;
674 snprintf(path
, pathlen
, "/tmp/frr.tmp.XXXXXXXX");
677 flog_warn(EC_LIB_SYSTEM_CALL
, "%s: mkstemp() failed: %s",
678 __func__
, safe_strerror(errno
));
681 if (fchmod(fd
, CONFIGFILE_MASK
) != 0) {
682 flog_warn(EC_LIB_SYSTEM_CALL
,
683 "%s: fchmod() failed: %s(%d):", __func__
,
684 safe_strerror(errno
), errno
);
688 /* Make vty for configuration file. */
689 file_vty
= vty_new();
691 file_vty
->type
= VTY_FILE
;
693 ret
= nb_cli_show_config(file_vty
, config
, format
, translator
,
700 static int nb_cli_show_config_compare(struct vty
*vty
,
701 struct nb_config
*config1
,
702 struct nb_config
*config2
,
703 enum nb_cfg_format format
,
704 struct yang_translator
*translator
)
706 char config1_path
[256];
707 char config2_path
[256];
708 char command
[BUFSIZ
];
713 if (nb_write_config(config1
, format
, translator
, config1_path
,
714 sizeof(config1_path
))
716 vty_out(vty
, "%% Failed to process configurations.\n\n");
719 if (nb_write_config(config2
, format
, translator
, config2_path
,
720 sizeof(config2_path
))
722 vty_out(vty
, "%% Failed to process configurations.\n\n");
723 unlink(config1_path
);
727 snprintf(command
, sizeof(command
), "diff -u %s %s", config1_path
,
729 fp
= popen(command
, "r");
731 vty_out(vty
, "%% Failed to generate configuration diff.\n\n");
732 unlink(config1_path
);
733 unlink(config2_path
);
736 /* Print diff line by line. */
737 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
740 vty_out(vty
, "%s", line
);
744 unlink(config1_path
);
745 unlink(config2_path
);
750 /* Configure exclusively from this terminal. */
751 DEFUN (config_exclusive
,
752 config_exclusive_cmd
,
753 "configure exclusive",
754 "Configuration from vty interface\n"
755 "Configure exclusively from this terminal\n")
757 return vty_config_enter(vty
, true, true);
760 /* Configure using a private candidate configuration. */
761 DEFUN (config_private
,
764 "Configuration from vty interface\n"
765 "Configure using a private candidate configuration\n")
767 return vty_config_enter(vty
, true, false);
770 DEFPY (config_commit
,
772 "commit [{force$force|confirmed (1-60)}]",
773 "Commit changes into the running configuration\n"
774 "Force commit even if the candidate is outdated\n"
775 "Rollback this commit unless there is a confirming commit\n"
776 "Timeout in minutes for the commit to be confirmed\n")
778 return nb_cli_commit(vty
, !!force
, confirmed
, NULL
);
781 DEFPY (config_commit_comment
,
782 config_commit_comment_cmd
,
783 "commit [{force$force|confirmed (1-60)}] comment LINE...",
784 "Commit changes into the running configuration\n"
785 "Force commit even if the candidate is outdated\n"
786 "Rollback this commit unless there is a confirming commit\n"
787 "Timeout in minutes for the commit to be confirmed\n"
788 "Assign a comment to this commit\n"
789 "Comment for this commit (Max 80 characters)\n")
795 argv_find(argv
, argc
, "LINE", &idx
);
796 comment
= argv_concat(argv
, argc
, idx
);
797 ret
= nb_cli_commit(vty
, !!force
, confirmed
, comment
);
798 XFREE(MTYPE_TMP
, comment
);
803 DEFPY (config_commit_check
,
804 config_commit_check_cmd
,
806 "Commit changes into the running configuration\n"
807 "Check if the configuration changes are valid\n")
809 struct nb_context context
= {};
810 char errmsg
[BUFSIZ
] = {0};
813 context
.client
= NB_CLIENT_CLI
;
815 ret
= nb_candidate_validate(&context
, vty
->candidate_config
, errmsg
,
819 "%% Failed to validate candidate configuration.\n\n");
820 vty_show_nb_errors(vty
, ret
, errmsg
);
824 vty_out(vty
, "%% Candidate configuration validated successfully.\n\n");
829 DEFPY (config_update
,
832 "Update candidate configuration\n")
834 if (!nb_candidate_needs_update(vty
->candidate_config
)) {
835 vty_out(vty
, "%% Update is not necessary.\n\n");
839 if (nb_candidate_update(vty
->candidate_config
) != NB_OK
) {
841 "%% Failed to update the candidate configuration.\n\n");
842 vty_out(vty
, "Please check the logs for more details.\n");
846 nb_config_replace(vty
->candidate_config_base
, running_config
, true);
848 vty_out(vty
, "%% Candidate configuration updated successfully.\n\n");
853 DEFPY (config_discard
,
856 "Discard changes in the candidate configuration\n")
858 nb_config_replace(vty
->candidate_config
, vty
->candidate_config_base
,
868 file [<json$json|xml$xml> [translate WORD$translator_family]] FILENAME$filename\
869 |transaction (1-4294967295)$tid\
872 "Configuration related settings\n"
873 "Load configuration into candidate\n"
874 "Load configuration file into candidate\n"
875 "Load configuration file in JSON format\n"
876 "Load configuration file in XML format\n"
877 "Translate configuration file\n"
878 "YANG module translator\n"
879 "Configuration file name (full path)\n"
880 "Load configuration from transaction into candidate\n"
882 "Replace instead of merge\n")
885 enum nb_cfg_format format
;
886 struct yang_translator
*translator
= NULL
;
889 format
= NB_CFG_FMT_JSON
;
891 format
= NB_CFG_FMT_XML
;
893 format
= NB_CFG_FMT_CMDS
;
895 if (translator_family
) {
896 translator
= yang_translator_find(translator_family
);
899 "%% Module translator \"%s\" not found\n",
905 return nb_cli_candidate_load_file(vty
, format
, translator
,
906 filename
, !!replace
);
909 return nb_cli_candidate_load_transaction(vty
, tid
, !!replace
);
912 DEFPY (show_config_running
,
913 show_config_running_cmd
,
914 "show configuration running\
915 [<json$json|xml$xml> [translate WORD$translator_family]]\
916 [with-defaults$with_defaults]",
918 "Configuration information\n"
919 "Running configuration\n"
920 "Change output format to JSON\n"
921 "Change output format to XML\n"
923 "YANG module translator\n"
924 "Show default values\n")
927 enum nb_cfg_format format
;
928 struct yang_translator
*translator
= NULL
;
931 format
= NB_CFG_FMT_JSON
;
933 format
= NB_CFG_FMT_XML
;
935 format
= NB_CFG_FMT_CMDS
;
937 if (translator_family
) {
938 translator
= yang_translator_find(translator_family
);
940 vty_out(vty
, "%% Module translator \"%s\" not found\n",
946 nb_cli_show_config(vty
, running_config
, format
, translator
,
952 DEFPY (show_config_candidate
,
953 show_config_candidate_cmd
,
954 "show configuration candidate\
955 [<json$json|xml$xml> [translate WORD$translator_family]]\
957 with-defaults$with_defaults\
961 "Configuration information\n"
962 "Candidate configuration\n"
963 "Change output format to JSON\n"
964 "Change output format to XML\n"
966 "YANG module translator\n"
967 "Show default values\n"
968 "Show changes applied in the candidate configuration\n")
971 enum nb_cfg_format format
;
972 struct yang_translator
*translator
= NULL
;
975 format
= NB_CFG_FMT_JSON
;
977 format
= NB_CFG_FMT_XML
;
979 format
= NB_CFG_FMT_CMDS
;
981 if (translator_family
) {
982 translator
= yang_translator_find(translator_family
);
984 vty_out(vty
, "%% Module translator \"%s\" not found\n",
991 return nb_cli_show_config_compare(
992 vty
, vty
->candidate_config_base
, vty
->candidate_config
,
995 nb_cli_show_config(vty
, vty
->candidate_config
, format
, translator
,
1001 DEFPY (show_config_candidate_section
,
1002 show_config_candidate_section_cmd
,
1006 struct lyd_node
*dnode
;
1008 /* Top-level configuration node, display everything. */
1009 if (vty
->xpath_index
== 0)
1010 return nb_cli_show_config(vty
, vty
->candidate_config
,
1011 NB_CFG_FMT_CMDS
, NULL
, false);
1013 /* Display only the current section of the candidate configuration. */
1014 dnode
= yang_dnode_get(vty
->candidate_config
->dnode
, VTY_CURR_XPATH
);
1016 /* Shouldn't happen. */
1019 nb_cli_show_dnode_cmds(vty
, dnode
, 0);
1020 vty_out(vty
, "!\n");
1025 DEFPY (show_config_compare
,
1026 show_config_compare_cmd
,
1027 "show configuration compare\
1029 candidate$c1_candidate\
1030 |running$c1_running\
1031 |transaction (1-4294967295)$c1_tid\
1034 candidate$c2_candidate\
1035 |running$c2_running\
1036 |transaction (1-4294967295)$c2_tid\
1038 [<json$json|xml$xml> [translate WORD$translator_family]]",
1040 "Configuration information\n"
1041 "Compare two different configurations\n"
1042 "Candidate configuration\n"
1043 "Running configuration\n"
1044 "Configuration transaction\n"
1046 "Candidate configuration\n"
1047 "Running configuration\n"
1048 "Configuration transaction\n"
1050 "Change output format to JSON\n"
1051 "Change output format to XML\n"
1052 "Translate output\n"
1053 "YANG module translator\n")
1055 enum nb_cfg_format format
;
1056 struct yang_translator
*translator
= NULL
;
1057 struct nb_config
*config1
, *config_transaction1
= NULL
;
1058 struct nb_config
*config2
, *config_transaction2
= NULL
;
1059 int ret
= CMD_WARNING
;
1062 config1
= vty
->candidate_config
;
1063 else if (c1_running
)
1064 config1
= running_config
;
1066 config_transaction1
= nb_db_transaction_load(c1_tid
);
1067 if (!config_transaction1
) {
1068 vty_out(vty
, "%% Transaction %u does not exist\n\n",
1069 (unsigned int)c1_tid
);
1072 config1
= config_transaction1
;
1076 config2
= vty
->candidate_config
;
1077 else if (c2_running
)
1078 config2
= running_config
;
1080 config_transaction2
= nb_db_transaction_load(c2_tid
);
1081 if (!config_transaction2
) {
1082 vty_out(vty
, "%% Transaction %u does not exist\n\n",
1083 (unsigned int)c2_tid
);
1086 config2
= config_transaction2
;
1090 format
= NB_CFG_FMT_JSON
;
1092 format
= NB_CFG_FMT_XML
;
1094 format
= NB_CFG_FMT_CMDS
;
1096 if (translator_family
) {
1097 translator
= yang_translator_find(translator_family
);
1099 vty_out(vty
, "%% Module translator \"%s\" not found\n",
1105 ret
= nb_cli_show_config_compare(vty
, config1
, config2
, format
,
1108 if (config_transaction1
)
1109 nb_config_free(config_transaction1
);
1110 if (config_transaction2
)
1111 nb_config_free(config_transaction2
);
1117 * Stripped down version of the "show configuration compare" command.
1118 * The "candidate" option is not present so the command can be installed in
1121 ALIAS (show_config_compare
,
1122 show_config_compare_without_candidate_cmd
,
1123 "show configuration compare\
1126 |transaction (1-4294967295)$c1_tid\
1130 |transaction (1-4294967295)$c2_tid\
1132 [<json$json|xml$xml> [translate WORD$translator_family]]",
1134 "Configuration information\n"
1135 "Compare two different configurations\n"
1136 "Running configuration\n"
1137 "Configuration transaction\n"
1139 "Running configuration\n"
1140 "Configuration transaction\n"
1142 "Change output format to JSON\n"
1143 "Change output format to XML\n"
1144 "Translate output\n"
1145 "YANG module translator\n")
1147 DEFPY (clear_config_transactions
,
1148 clear_config_transactions_cmd
,
1149 "clear configuration transactions oldest (1-100)$n",
1151 "Configuration activity\n"
1152 "Delete transactions from the transactions log\n"
1153 "Delete oldest <n> transactions\n"
1154 "Number of transactions to delete\n")
1156 #ifdef HAVE_CONFIG_ROLLBACKS
1157 if (nb_db_clear_transactions(n
) != NB_OK
) {
1158 vty_out(vty
, "%% Failed to delete transactions.\n\n");
1163 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1164 #endif /* HAVE_CONFIG_ROLLBACKS */
1169 DEFPY (config_database_max_transactions
,
1170 config_database_max_transactions_cmd
,
1171 "configuration database max-transactions (1-100)$max",
1172 "Configuration related settings\n"
1173 "Configuration database\n"
1174 "Set the maximum number of transactions to store\n"
1175 "Number of transactions\n")
1177 #ifdef HAVE_CONFIG_ROLLBACKS
1178 if (nb_db_set_max_transactions(max
) != NB_OK
) {
1180 "%% Failed to update the maximum number of transactions.\n\n");
1184 "%% Maximum number of transactions updated successfully.\n\n");
1187 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1188 #endif /* HAVE_CONFIG_ROLLBACKS */
1193 DEFPY (yang_module_translator_load
,
1194 yang_module_translator_load_cmd
,
1195 "yang module-translator load FILENAME$filename",
1196 "YANG related settings\n"
1197 "YANG module translator\n"
1198 "Load YANG module translator\n"
1199 "File name (full path)\n")
1201 struct yang_translator
*translator
;
1203 translator
= yang_translator_load(filename
);
1205 vty_out(vty
, "%% Failed to load \"%s\"\n\n", filename
);
1206 vty_out(vty
, "Please check the logs for more details.\n");
1210 vty_out(vty
, "%% Module translator \"%s\" loaded successfully.\n\n",
1211 translator
->family
);
1216 DEFPY (yang_module_translator_unload_family
,
1217 yang_module_translator_unload_cmd
,
1218 "yang module-translator unload WORD$translator_family",
1219 "YANG related settings\n"
1220 "YANG module translator\n"
1221 "Unload YANG module translator\n"
1222 "Name of the module translator\n")
1224 struct yang_translator
*translator
;
1226 translator
= yang_translator_find(translator_family
);
1228 vty_out(vty
, "%% Module translator \"%s\" not found\n",
1233 yang_translator_unload(translator
);
1238 #ifdef HAVE_CONFIG_ROLLBACKS
1239 static void nb_cli_show_transactions_cb(void *arg
, int transaction_id
,
1240 const char *client_name
,
1241 const char *date
, const char *comment
)
1243 struct ttable
*tt
= arg
;
1245 ttable_add_row(tt
, "%d|%s|%s|%s", transaction_id
, client_name
, date
,
1249 static int nb_cli_show_transactions(struct vty
*vty
)
1253 /* Prepare table. */
1254 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
1255 ttable_add_row(tt
, "Transaction ID|Client|Date|Comment");
1256 tt
->style
.cell
.rpad
= 2;
1257 tt
->style
.corner
= '+';
1259 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
1261 /* Fetch transactions from the northbound database. */
1262 if (nb_db_transactions_iterate(nb_cli_show_transactions_cb
, tt
)
1265 "%% Failed to fetch configuration transactions.\n");
1269 /* Dump the generated table. */
1270 if (tt
->nrows
> 1) {
1273 table
= ttable_dump(tt
, "\n");
1274 vty_out(vty
, "%s\n", table
);
1275 XFREE(MTYPE_TMP
, table
);
1277 vty_out(vty
, "No configuration transactions to display.\n\n");
1283 #endif /* HAVE_CONFIG_ROLLBACKS */
1285 DEFPY (show_config_transaction
,
1286 show_config_transaction_cmd
,
1287 "show configuration transaction\
1289 (1-4294967295)$transaction_id\
1290 [<json$json|xml$xml> [translate WORD$translator_family]]\
1292 with-defaults$with_defaults\
1297 "Configuration information\n"
1298 "Configuration transaction\n"
1300 "Change output format to JSON\n"
1301 "Change output format to XML\n"
1302 "Translate output\n"
1303 "YANG module translator\n"
1304 "Show default values\n"
1305 "Show changes compared to the previous transaction\n")
1307 #ifdef HAVE_CONFIG_ROLLBACKS
1308 if (transaction_id
) {
1309 struct nb_config
*config
;
1310 enum nb_cfg_format format
;
1311 struct yang_translator
*translator
= NULL
;
1314 format
= NB_CFG_FMT_JSON
;
1316 format
= NB_CFG_FMT_XML
;
1318 format
= NB_CFG_FMT_CMDS
;
1320 if (translator_family
) {
1321 translator
= yang_translator_find(translator_family
);
1324 "%% Module translator \"%s\" not found\n",
1330 config
= nb_db_transaction_load(transaction_id
);
1332 vty_out(vty
, "%% Transaction %u does not exist.\n\n",
1333 (unsigned int)transaction_id
);
1338 struct nb_config
*prev_config
;
1341 /* NOTE: this can be NULL. */
1343 nb_db_transaction_load(transaction_id
- 1);
1345 ret
= nb_cli_show_config_compare(
1346 vty
, prev_config
, config
, format
, translator
);
1348 nb_config_free(prev_config
);
1349 nb_config_free(config
);
1354 nb_cli_show_config(vty
, config
, format
, translator
,
1356 nb_config_free(config
);
1361 return nb_cli_show_transactions(vty
);
1364 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1366 #endif /* HAVE_CONFIG_ROLLBACKS */
1369 static int nb_cli_oper_data_cb(const struct lysc_node
*snode
,
1370 struct yang_translator
*translator
,
1371 struct yang_data
*data
, void *arg
)
1373 struct lyd_node
*dnode
= arg
;
1374 struct ly_ctx
*ly_ctx
;
1379 ret
= yang_translate_xpath(translator
,
1380 YANG_TRANSLATE_FROM_NATIVE
,
1381 data
->xpath
, sizeof(data
->xpath
));
1383 case YANG_TRANSLATE_SUCCESS
:
1385 case YANG_TRANSLATE_NOTFOUND
:
1387 case YANG_TRANSLATE_FAILURE
:
1391 ly_ctx
= translator
->ly_ctx
;
1393 ly_ctx
= ly_native_ctx
;
1396 lyd_new_path(dnode
, ly_ctx
, data
->xpath
, (void *)data
->value
,
1397 LYD_NEW_PATH_UPDATE
, &dnode
);
1399 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path(%s) failed: %s",
1400 __func__
, data
->xpath
, ly_errmsg(ly_native_ctx
));
1405 yang_data_free(data
);
1409 yang_data_free(data
);
1413 DEFPY (show_yang_operational_data
,
1414 show_yang_operational_data_cmd
,
1415 "show yang operational-data XPATH$xpath\
1417 format <json$json|xml$xml>\
1418 |translate WORD$translator_family\
1419 |with-config$with_config\
1422 "YANG information\n"
1423 "Show YANG operational data\n"
1424 "XPath expression specifying the YANG data path\n"
1425 "Set the output format\n"
1426 "JavaScript Object Notation\n"
1427 "Extensible Markup Language\n"
1428 "Translate operational data\n"
1429 "YANG module translator\n"
1430 "Merge configuration data\n")
1433 struct yang_translator
*translator
= NULL
;
1434 struct ly_ctx
*ly_ctx
;
1435 struct lyd_node
*dnode
;
1437 uint32_t print_options
= LYD_PRINT_WITHSIBLINGS
;
1444 if (translator_family
) {
1445 translator
= yang_translator_find(translator_family
);
1447 vty_out(vty
, "%% Module translator \"%s\" not found\n",
1452 ly_ctx
= translator
->ly_ctx
;
1454 ly_ctx
= ly_native_ctx
;
1457 dnode
= yang_dnode_new(ly_ctx
, false);
1458 if (nb_oper_data_iterate(xpath
, translator
, 0, nb_cli_oper_data_cb
,
1461 vty_out(vty
, "%% Failed to fetch operational data.\n");
1462 yang_dnode_free(dnode
);
1466 if (with_config
&& yang_dnode_exists(running_config
->dnode
, xpath
)) {
1467 struct lyd_node
*config_dnode
=
1468 yang_dnode_get(running_config
->dnode
, xpath
);
1469 if (config_dnode
!= NULL
) {
1470 lyd_merge_tree(&dnode
, yang_dnode_dup(config_dnode
),
1471 LYD_MERGE_DESTRUCT
);
1472 print_options
|= LYD_PRINT_WD_ALL
;
1476 (void)lyd_validate_all(&dnode
, ly_ctx
, 0, NULL
);
1478 /* Display the data. */
1479 if (lyd_print_mem(&strp
, dnode
, format
, print_options
) != 0 || !strp
) {
1480 vty_out(vty
, "%% Failed to display operational data.\n");
1481 yang_dnode_free(dnode
);
1484 vty_out(vty
, "%s", strp
);
1486 yang_dnode_free(dnode
);
1491 DEFPY (show_yang_module
,
1492 show_yang_module_cmd
,
1493 "show yang module [module-translator WORD$translator_family]",
1495 "YANG information\n"
1496 "Show loaded modules\n"
1497 "YANG module translator\n"
1498 "YANG module translator\n")
1500 struct ly_ctx
*ly_ctx
;
1501 struct yang_translator
*translator
= NULL
;
1502 const struct lys_module
*module
;
1506 if (translator_family
) {
1507 translator
= yang_translator_find(translator_family
);
1509 vty_out(vty
, "%% Module translator \"%s\" not found\n",
1513 ly_ctx
= translator
->ly_ctx
;
1515 ly_ctx
= ly_native_ctx
;
1517 /* Prepare table. */
1518 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
1519 ttable_add_row(tt
, "Module|Version|Revision|Flags|Namespace");
1520 tt
->style
.cell
.rpad
= 2;
1521 tt
->style
.corner
= '+';
1523 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
1525 while ((module
= ly_ctx_get_module_iter(ly_ctx
, &idx
))) {
1528 snprintf(flags
, sizeof(flags
), "%c%c",
1529 module
->implemented
? 'I' : ' ',
1530 LY_ARRAY_COUNT(module
->deviated_by
) ? 'D' : ' ');
1532 ttable_add_row(tt
, "%s|%s|%s|%s|%s", module
->name
,
1533 (module
->parsed
->version
== 2) ? "1.1" : "1.0",
1534 module
->revision
? module
->revision
: "-", flags
,
1538 /* Dump the generated table. */
1539 if (tt
->nrows
> 1) {
1542 vty_out(vty
, " Flags: I - Implemented, D - Deviated\n\n");
1544 table
= ttable_dump(tt
, "\n");
1545 vty_out(vty
, "%s\n", table
);
1546 XFREE(MTYPE_TMP
, table
);
1548 vty_out(vty
, "No YANG modules to display.\n\n");
1555 DEFPY(show_yang_module_detail
, show_yang_module_detail_cmd
,
1557 [module-translator WORD$translator_family]\
1558 WORD$module_name <compiled$compiled|summary|tree$tree|yang$yang|yin$yin>",
1560 "YANG information\n"
1561 "Show loaded modules\n"
1562 "YANG module translator\n"
1563 "YANG module translator\n"
1565 "Display compiled module in YANG format\n"
1566 "Display summary information about the module\n"
1567 "Display module in the tree (RFC 8340) format\n"
1568 "Display module in the YANG format\n"
1569 "Display module in the YIN format\n")
1571 struct ly_ctx
*ly_ctx
;
1572 struct yang_translator
*translator
= NULL
;
1573 const struct lys_module
*module
;
1574 LYS_OUTFORMAT format
;
1577 if (translator_family
) {
1578 translator
= yang_translator_find(translator_family
);
1580 vty_out(vty
, "%% Module translator \"%s\" not found\n",
1584 ly_ctx
= translator
->ly_ctx
;
1586 ly_ctx
= ly_native_ctx
;
1588 module
= ly_ctx_get_module_latest(ly_ctx
, module_name
);
1590 vty_out(vty
, "%% Module \"%s\" not found\n", module_name
);
1595 format
= LYS_OUT_YANG
;
1597 format
= LYS_OUT_YIN
;
1599 format
= LYS_OUT_YANG_COMPILED
;
1601 format
= LYS_OUT_TREE
;
1604 "%% libyang v2 does not currently support summary\n");
1608 if (lys_print_mem(&strp
, module
, format
, 0) == 0) {
1609 vty_out(vty
, "%s\n", strp
);
1613 vty_out(vty
, "%% Error generating module information\n");
1620 DEFPY (show_yang_module_translator
,
1621 show_yang_module_translator_cmd
,
1622 "show yang module-translator",
1624 "YANG information\n"
1625 "Show loaded YANG module translators\n")
1627 struct yang_translator
*translator
;
1630 /* Prepare table. */
1631 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
1632 ttable_add_row(tt
, "Family|Module|Deviations|Coverage (%%)");
1633 tt
->style
.cell
.rpad
= 2;
1634 tt
->style
.corner
= '+';
1636 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
1638 RB_FOREACH (translator
, yang_translators
, &yang_translators
) {
1639 struct yang_tmodule
*tmodule
;
1640 struct listnode
*ln
;
1642 for (ALL_LIST_ELEMENTS_RO(translator
->modules
, ln
, tmodule
)) {
1643 ttable_add_row(tt
, "%s|%s|%s|%.2f", translator
->family
,
1644 tmodule
->module
->name
,
1645 tmodule
->deviations
->name
,
1650 /* Dump the generated table. */
1651 if (tt
->nrows
> 1) {
1654 table
= ttable_dump(tt
, "\n");
1655 vty_out(vty
, "%s\n", table
);
1656 XFREE(MTYPE_TMP
, table
);
1658 vty_out(vty
, "No YANG module translators to display.\n\n");
1665 #ifdef HAVE_CONFIG_ROLLBACKS
1666 static int nb_cli_rollback_configuration(struct vty
*vty
,
1667 uint32_t transaction_id
)
1669 struct nb_context context
= {};
1670 struct nb_config
*candidate
;
1672 char errmsg
[BUFSIZ
] = {0};
1675 candidate
= nb_db_transaction_load(transaction_id
);
1677 vty_out(vty
, "%% Transaction %u does not exist.\n\n",
1682 snprintf(comment
, sizeof(comment
), "Rollback to transaction %u",
1685 context
.client
= NB_CLIENT_CLI
;
1687 ret
= nb_candidate_commit(context
, candidate
, true, comment
, NULL
,
1688 errmsg
, sizeof(errmsg
));
1689 nb_config_free(candidate
);
1693 "%% Configuration was successfully rolled back.\n\n");
1694 /* Print warnings (if any). */
1695 if (strlen(errmsg
) > 0)
1696 vty_out(vty
, "%s\n", errmsg
);
1698 case NB_ERR_NO_CHANGES
:
1700 "%% Aborting - no configuration changes detected.\n\n");
1703 vty_out(vty
, "%% Rollback failed.\n\n");
1704 vty_show_nb_errors(vty
, ret
, errmsg
);
1708 #endif /* HAVE_CONFIG_ROLLBACKS */
1710 DEFPY (rollback_config
,
1711 rollback_config_cmd
,
1712 "rollback configuration (1-4294967295)$transaction_id",
1713 "Rollback to a previous state\n"
1714 "Running configuration\n"
1717 #ifdef HAVE_CONFIG_ROLLBACKS
1718 return nb_cli_rollback_configuration(vty
, transaction_id
);
1721 "%% FRR was compiled without --enable-config-rollbacks.\n\n");
1723 #endif /* HAVE_CONFIG_ROLLBACKS */
1726 /* Debug CLI commands. */
1727 static struct debug
*nb_debugs
[] = {
1728 &nb_dbg_cbs_config
, &nb_dbg_cbs_state
, &nb_dbg_cbs_rpc
,
1729 &nb_dbg_notif
, &nb_dbg_events
, &nb_dbg_libyang
,
1732 static const char *const nb_debugs_conflines
[] = {
1733 "debug northbound callbacks configuration",
1734 "debug northbound callbacks state",
1735 "debug northbound callbacks rpc",
1736 "debug northbound notifications",
1737 "debug northbound events",
1738 "debug northbound libyang",
1741 DEFINE_HOOK(nb_client_debug_set_all
, (uint32_t flags
, bool set
), (flags
, set
));
1743 static void nb_debug_set_all(uint32_t flags
, bool set
)
1745 for (unsigned int i
= 0; i
< array_size(nb_debugs
); i
++) {
1746 DEBUG_FLAGS_SET(nb_debugs
[i
], flags
, set
);
1748 /* If all modes have been turned off, don't preserve options. */
1749 if (!DEBUG_MODE_CHECK(nb_debugs
[i
], DEBUG_MODE_ALL
))
1750 DEBUG_CLEAR(nb_debugs
[i
]);
1753 hook_call(nb_client_debug_set_all
, flags
, set
);
1758 "[no] debug northbound\
1760 callbacks$cbs [{configuration$cbs_cfg|state$cbs_state|rpc$cbs_rpc}]\
1761 |notifications$notifications\
1767 "Northbound debugging\n"
1774 "libyang debugging\n")
1776 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
1779 bool none
= (!cbs_cfg
&& !cbs_state
&& !cbs_rpc
);
1781 if (none
|| cbs_cfg
)
1782 DEBUG_MODE_SET(&nb_dbg_cbs_config
, mode
, !no
);
1783 if (none
|| cbs_state
)
1784 DEBUG_MODE_SET(&nb_dbg_cbs_state
, mode
, !no
);
1785 if (none
|| cbs_rpc
)
1786 DEBUG_MODE_SET(&nb_dbg_cbs_rpc
, mode
, !no
);
1789 DEBUG_MODE_SET(&nb_dbg_notif
, mode
, !no
);
1791 DEBUG_MODE_SET(&nb_dbg_events
, mode
, !no
);
1793 DEBUG_MODE_SET(&nb_dbg_libyang
, mode
, !no
);
1794 yang_debugging_set(!no
);
1797 /* no specific debug --> act on all of them */
1798 if (strmatch(argv
[argc
- 1]->text
, "northbound")) {
1799 nb_debug_set_all(mode
, !no
);
1800 yang_debugging_set(!no
);
1806 DEFINE_HOOK(nb_client_debug_config_write
, (struct vty
*vty
), (vty
));
1808 static int nb_debug_config_write(struct vty
*vty
)
1810 for (unsigned int i
= 0; i
< array_size(nb_debugs
); i
++)
1811 if (DEBUG_MODE_CHECK(nb_debugs
[i
], DEBUG_MODE_CONF
))
1812 vty_out(vty
, "%s\n", nb_debugs_conflines
[i
]);
1814 hook_call(nb_client_debug_config_write
, vty
);
1819 static struct debug_callbacks nb_dbg_cbs
= {.debug_set_all
= nb_debug_set_all
};
1820 static struct cmd_node nb_debug_node
= {
1821 .name
= "northbound debug",
1822 .node
= NORTHBOUND_DEBUG_NODE
,
1824 .config_write
= nb_debug_config_write
,
1827 void nb_cli_install_default(int node
)
1829 _install_element(node
, &show_config_candidate_section_cmd
);
1831 if (frr_get_cli_mode() != FRR_CLI_TRANSACTIONAL
)
1834 _install_element(node
, &config_commit_cmd
);
1835 _install_element(node
, &config_commit_comment_cmd
);
1836 _install_element(node
, &config_commit_check_cmd
);
1837 _install_element(node
, &config_update_cmd
);
1838 _install_element(node
, &config_discard_cmd
);
1839 _install_element(node
, &show_config_running_cmd
);
1840 _install_element(node
, &show_config_candidate_cmd
);
1841 _install_element(node
, &show_config_compare_cmd
);
1842 _install_element(node
, &show_config_transaction_cmd
);
1845 /* YANG module autocomplete. */
1846 static void yang_module_autocomplete(vector comps
, struct cmd_token
*token
)
1848 const struct lys_module
*module
;
1849 struct yang_translator
*module_tr
;
1853 while ((module
= ly_ctx_get_module_iter(ly_native_ctx
, &idx
)))
1854 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, module
->name
));
1856 RB_FOREACH (module_tr
, yang_translators
, &yang_translators
) {
1858 while ((module
= ly_ctx_get_module_iter(module_tr
->ly_ctx
,
1861 XSTRDUP(MTYPE_COMPLETION
, module
->name
));
1865 /* YANG module translator autocomplete. */
1866 static void yang_translator_autocomplete(vector comps
, struct cmd_token
*token
)
1868 struct yang_translator
*module_tr
;
1870 RB_FOREACH (module_tr
, yang_translators
, &yang_translators
)
1871 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, module_tr
->family
));
1874 static const struct cmd_variable_handler yang_var_handlers
[] = {
1875 {.varname
= "module_name", .completions
= yang_module_autocomplete
},
1876 {.varname
= "translator_family",
1877 .completions
= yang_translator_autocomplete
},
1878 {.completions
= NULL
}};
1880 void nb_cli_init(struct thread_master
*tm
)
1884 /* Initialize the shared candidate configuration. */
1885 vty_shared_candidate_config
= nb_config_new(NULL
);
1887 debug_init(&nb_dbg_cbs
);
1889 install_node(&nb_debug_node
);
1890 install_element(ENABLE_NODE
, &debug_nb_cmd
);
1891 install_element(CONFIG_NODE
, &debug_nb_cmd
);
1893 /* Install commands specific to the transaction-base mode. */
1894 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL
) {
1895 install_element(ENABLE_NODE
, &config_exclusive_cmd
);
1896 install_element(ENABLE_NODE
, &config_private_cmd
);
1897 install_element(ENABLE_NODE
,
1898 &show_config_compare_without_candidate_cmd
);
1899 install_element(ENABLE_NODE
, &show_config_transaction_cmd
);
1900 install_element(ENABLE_NODE
, &rollback_config_cmd
);
1901 install_element(ENABLE_NODE
, &clear_config_transactions_cmd
);
1903 install_element(CONFIG_NODE
, &config_load_cmd
);
1904 install_element(CONFIG_NODE
,
1905 &config_database_max_transactions_cmd
);
1908 /* Other commands. */
1909 install_element(ENABLE_NODE
, &show_config_running_cmd
);
1910 install_element(CONFIG_NODE
, &yang_module_translator_load_cmd
);
1911 install_element(CONFIG_NODE
, &yang_module_translator_unload_cmd
);
1912 install_element(ENABLE_NODE
, &show_yang_operational_data_cmd
);
1913 install_element(ENABLE_NODE
, &show_yang_module_cmd
);
1914 install_element(ENABLE_NODE
, &show_yang_module_detail_cmd
);
1915 install_element(ENABLE_NODE
, &show_yang_module_translator_cmd
);
1916 cmd_variable_handler_register(yang_var_handlers
);
1919 void nb_cli_terminate(void)
1921 nb_config_free(vty_shared_candidate_config
);