summaryrefslogtreecommitdiff
path: root/lib/northbound_cli.c
diff options
context:
space:
mode:
authorRenato Westphal <renato@opensourcerouting.org>2019-10-06 01:16:47 -0300
committerRenato Westphal <renato@opensourcerouting.org>2019-10-11 21:18:36 -0300
commit5e6a9350c16f54113eeedb497c98fe45b8ce6222 (patch)
treea4931a1274531b4b1d975ff9ca23bc70670b9676 /lib/northbound_cli.c
parentc7c9103b01899d316372cc0272c37df9a7e59426 (diff)
lib: avoid expensive operations when editing a candidate config
nb_candidate_edit() was calling both the lyd_schema_sort() and lyd_validate() functions whenever a new node was added to the candidate configuration. This was done to ensure the candidate is always ready to be displayed correctly (libyang only creates default child nodes during the validation process, and data nodes aren't guaranteed to be ordered by default). The problem is that the two aforementioned functions are too expensive to be called in the northbound hot path. Instead, it makes more sense to call them only before displaying the configuration (in which case a recursive sort needs to be done). Introduce the nb_cli_show_config_prepare() to achieve that purpose. Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
Diffstat (limited to 'lib/northbound_cli.c')
-rw-r--r--lib/northbound_cli.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/northbound_cli.c b/lib/northbound_cli.c
index a215d8b759..07e8c9dff9 100644
--- a/lib/northbound_cli.c
+++ b/lib/northbound_cli.c
@@ -421,6 +421,23 @@ static struct lyd_node *ly_iter_next_up(const struct lyd_node *elem)
return elem->parent;
}
+/* Prepare the configuration for display. */
+void nb_cli_show_config_prepare(struct nb_config *config, bool with_defaults)
+{
+ lyd_schema_sort(config->dnode, 1);
+
+ /*
+ * When "with-defaults" is used, call lyd_validate() only to create
+ * default child nodes, ignoring any possible validation error. This
+ * doesn't need to be done when displaying the running configuration
+ * since it's always fully validated.
+ */
+ if (with_defaults && config != running_config)
+ (void)lyd_validate(&config->dnode,
+ LYD_OPT_CONFIG | LYD_OPT_WHENAUTODEL,
+ ly_native_ctx);
+}
+
void nb_cli_show_dnode_cmds(struct vty *vty, struct lyd_node *root,
bool with_defaults)
{
@@ -513,6 +530,8 @@ static int nb_cli_show_config(struct vty *vty, struct nb_config *config,
struct yang_translator *translator,
bool with_defaults)
{
+ nb_cli_show_config_prepare(config, with_defaults);
+
switch (format) {
case NB_CFG_FMT_CMDS:
nb_cli_show_config_cmds(vty, config, with_defaults);