summaryrefslogtreecommitdiff
path: root/lib/northbound_cli.c
diff options
context:
space:
mode:
authorSantosh P K <50885001+Spantik@users.noreply.github.com>2020-06-10 22:47:07 +0530
committerGitHub <noreply@github.com>2020-06-10 22:47:07 +0530
commit57dd2f009739664cbf857a82e7a6970c916b8e10 (patch)
tree7187878d1394bbc452ecc1e2bffd5edafb9a920d /lib/northbound_cli.c
parent5e0494b38a53e5a3501088efb4b48b2cbb7a080f (diff)
parent1abe6c535e89f8b445c7d5808bc7972a6b60236d (diff)
Merge pull request #6414 from opensourcerouting/nb-error-handling
NB context + enhanced error handling
Diffstat (limited to 'lib/northbound_cli.c')
-rw-r--r--lib/northbound_cli.c101
1 files changed, 61 insertions, 40 deletions
diff --git a/lib/northbound_cli.c b/lib/northbound_cli.c
index d4467facaf..105fc83cef 100644
--- a/lib/northbound_cli.c
+++ b/lib/northbound_cli.c
@@ -46,23 +46,11 @@ struct debug nb_dbg_libyang = {0, "libyang debugging"};
struct nb_config *vty_shared_candidate_config;
static struct thread_master *master;
-static void vty_show_libyang_errors(struct vty *vty, struct ly_ctx *ly_ctx)
+static void vty_show_nb_errors(struct vty *vty, int error, const char *errmsg)
{
- struct ly_err_item *ei;
- const char *path;
-
- ei = ly_err_first(ly_ctx);
- if (!ei)
- return;
-
- for (; ei; ei = ei->next)
- vty_out(vty, "%s\n", ei->msg);
-
- path = ly_errpath(ly_ctx);
- if (path)
- vty_out(vty, "YANG path: %s\n", path);
-
- ly_err_clean(ly_ctx, NULL);
+ vty_out(vty, "Error type: %s\n", nb_err_name(error));
+ if (strlen(errmsg) > 0)
+ vty_out(vty, "Error description: %s\n", errmsg);
}
void nb_cli_enqueue_change(struct vty *vty, const char *xpath,
@@ -158,24 +146,31 @@ int nb_cli_apply_changes(struct vty *vty, const char *xpath_base_fmt, ...)
}
if (error) {
+ char buf[BUFSIZ];
+
/*
* Failure to edit the candidate configuration should never
* happen in practice, unless there's a bug in the code. When
* that happens, log the error but otherwise ignore it.
*/
vty_out(vty, "%% Failed to edit configuration.\n\n");
- vty_show_libyang_errors(vty, ly_native_ctx);
+ vty_out(vty, "%s",
+ yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
}
/* Do an implicit "commit" when using the classic CLI mode. */
if (frr_get_cli_mode() == FRR_CLI_CLASSIC) {
- ret = nb_candidate_commit(vty->candidate_config, NB_CLIENT_CLI,
- vty, false, NULL, NULL);
+ struct nb_context context = {};
+ char errmsg[BUFSIZ] = {0};
+
+ context.client = NB_CLIENT_CLI;
+ context.user = vty;
+ ret = nb_candidate_commit(&context, vty->candidate_config,
+ false, NULL, NULL, errmsg,
+ sizeof(errmsg));
if (ret != NB_OK && ret != NB_ERR_NO_CHANGES) {
- vty_out(vty, "%% Configuration failed: %s.\n\n",
- nb_err_name(ret));
- vty_out(vty,
- "Please check the logs for more details.\n");
+ vty_out(vty, "%% Configuration failed.\n\n");
+ vty_show_nb_errors(vty, ret, errmsg);
/* Regenerate candidate for consistency. */
nb_config_replace(vty->candidate_config, running_config,
@@ -217,20 +212,27 @@ void nb_cli_confirmed_commit_clean(struct vty *vty)
int nb_cli_confirmed_commit_rollback(struct vty *vty)
{
+ struct nb_context context = {};
uint32_t transaction_id;
+ char errmsg[BUFSIZ] = {0};
int ret;
/* Perform the rollback. */
+ context.client = NB_CLIENT_CLI;
+ context.user = vty;
ret = nb_candidate_commit(
- vty->confirmed_commit_rollback, NB_CLIENT_CLI, vty, true,
+ &context, vty->confirmed_commit_rollback, true,
"Rollback to previous configuration - confirmed commit has timed out",
- &transaction_id);
+ &transaction_id, errmsg, sizeof(errmsg));
if (ret == NB_OK)
vty_out(vty,
"Rollback performed successfully (Transaction ID #%u).\n",
transaction_id);
- else
- vty_out(vty, "Failed to rollback to previous configuration.\n");
+ else {
+ vty_out(vty,
+ "Failed to rollback to previous configuration.\n\n");
+ vty_show_nb_errors(vty, ret, errmsg);
+ }
return ret;
}
@@ -252,7 +254,9 @@ static int nb_cli_confirmed_commit_timeout(struct thread *thread)
static int nb_cli_commit(struct vty *vty, bool force,
unsigned int confirmed_timeout, char *comment)
{
+ struct nb_context context = {};
uint32_t transaction_id = 0;
+ char errmsg[BUFSIZ] = {0};
int ret;
/* Check if there's a pending confirmed commit. */
@@ -295,8 +299,11 @@ static int nb_cli_commit(struct vty *vty, bool force,
&vty->t_confirmed_commit_timeout);
}
- ret = nb_candidate_commit(vty->candidate_config, NB_CLIENT_CLI, vty,
- true, comment, &transaction_id);
+ context.client = NB_CLIENT_CLI;
+ context.user = vty;
+ ret = nb_candidate_commit(&context, vty->candidate_config, true,
+ comment, &transaction_id, errmsg,
+ sizeof(errmsg));
/* Map northbound return code to CLI return code. */
switch (ret) {
@@ -312,9 +319,8 @@ static int nb_cli_commit(struct vty *vty, bool force,
return CMD_SUCCESS;
default:
vty_out(vty,
- "%% Failed to commit candidate configuration: %s.\n\n",
- nb_err_name(ret));
- vty_out(vty, "Please check the logs for more details.\n");
+ "%% Failed to commit candidate configuration.\n\n");
+ vty_show_nb_errors(vty, ret, errmsg);
return CMD_WARNING;
}
}
@@ -328,6 +334,7 @@ static int nb_cli_candidate_load_file(struct vty *vty,
struct lyd_node *dnode;
struct ly_ctx *ly_ctx;
int ly_format;
+ char buf[BUFSIZ];
switch (format) {
case NB_CFG_FMT_CMDS:
@@ -350,7 +357,9 @@ static int nb_cli_candidate_load_file(struct vty *vty,
flog_warn(EC_LIB_LIBYANG, "%s: lyd_parse_path() failed",
__func__);
vty_out(vty, "%% Failed to load configuration:\n\n");
- vty_show_libyang_errors(vty, ly_ctx);
+ vty_out(vty, "%s",
+ yang_print_errors(ly_native_ctx, buf,
+ sizeof(buf)));
return CMD_WARNING;
}
if (translator
@@ -371,7 +380,8 @@ static int nb_cli_candidate_load_file(struct vty *vty,
!= NB_OK) {
vty_out(vty,
"%% Failed to merge the loaded configuration:\n\n");
- vty_show_libyang_errors(vty, ly_native_ctx);
+ vty_out(vty, "%s",
+ yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
return CMD_WARNING;
}
@@ -383,6 +393,7 @@ static int nb_cli_candidate_load_transaction(struct vty *vty,
bool replace)
{
struct nb_config *loaded_config;
+ char buf[BUFSIZ];
loaded_config = nb_db_transaction_load(transaction_id);
if (!loaded_config) {
@@ -397,7 +408,8 @@ static int nb_cli_candidate_load_transaction(struct vty *vty,
!= NB_OK) {
vty_out(vty,
"%% Failed to merge the loaded configuration:\n\n");
- vty_show_libyang_errors(vty, ly_native_ctx);
+ vty_out(vty, "%s",
+ yang_print_errors(ly_native_ctx, buf, sizeof(buf)));
return CMD_WARNING;
}
@@ -690,13 +702,18 @@ DEFPY (config_commit_check,
"Commit changes into the running configuration\n"
"Check if the configuration changes are valid\n")
{
+ struct nb_context context = {};
+ char errmsg[BUFSIZ] = {0};
int ret;
- ret = nb_candidate_validate(vty->candidate_config);
+ context.client = NB_CLIENT_CLI;
+ context.user = vty;
+ ret = nb_candidate_validate(&context, vty->candidate_config, errmsg,
+ sizeof(errmsg));
if (ret != NB_OK) {
vty_out(vty,
"%% Failed to validate candidate configuration.\n\n");
- vty_show_libyang_errors(vty, ly_native_ctx);
+ vty_show_nb_errors(vty, ret, errmsg);
return CMD_WARNING;
}
@@ -1530,8 +1547,10 @@ DEFPY (show_yang_module_translator,
static int nb_cli_rollback_configuration(struct vty *vty,
uint32_t transaction_id)
{
+ struct nb_context context = {};
struct nb_config *candidate;
char comment[80];
+ char errmsg[BUFSIZ] = {0};
int ret;
candidate = nb_db_transaction_load(transaction_id);
@@ -1544,8 +1563,10 @@ static int nb_cli_rollback_configuration(struct vty *vty,
snprintf(comment, sizeof(comment), "Rollback to transaction %u",
transaction_id);
- ret = nb_candidate_commit(candidate, NB_CLIENT_CLI, vty, true, comment,
- NULL);
+ context.client = NB_CLIENT_CLI;
+ context.user = vty;
+ ret = nb_candidate_commit(&context, candidate, true, comment, NULL,
+ errmsg, sizeof(errmsg));
nb_config_free(candidate);
switch (ret) {
case NB_OK:
@@ -1558,7 +1579,7 @@ static int nb_cli_rollback_configuration(struct vty *vty,
return CMD_WARNING;
default:
vty_out(vty, "%% Rollback failed.\n\n");
- vty_out(vty, "Please check the logs for more details.\n");
+ vty_show_nb_errors(vty, ret, errmsg);
return CMD_WARNING;
}
}