1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
11 #include "lib_errors.h"
16 #include "frr_pthread.h"
17 #include "northbound.h"
18 #include "northbound_cli.h"
19 #include "northbound_db.h"
22 DEFINE_MTYPE_STATIC(LIB
, NB_NODE
, "Northbound Node");
23 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG
, "Northbound Configuration");
24 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG_ENTRY
, "Northbound Configuration Entry");
26 /* Running configuration - shouldn't be modified directly. */
27 struct nb_config
*running_config
;
29 /* Hash table of user pointers associated with configuration entries. */
30 static struct hash
*running_config_entries
;
32 /* Management lock for the running configuration. */
34 /* Mutex protecting this structure. */
40 /* Northbound client who owns this lock. */
41 enum nb_client owner_client
;
43 /* Northbound user who owns this lock. */
44 const void *owner_user
;
45 } running_config_mgmt_lock
;
47 /* Knob to record config transaction */
48 static bool nb_db_enabled
;
50 * Global lock used to prevent multiple configuration transactions from
51 * happening concurrently.
53 static bool transaction_in_progress
;
55 static int nb_callback_pre_validate(struct nb_context
*context
,
56 const struct nb_node
*nb_node
,
57 const struct lyd_node
*dnode
, char *errmsg
,
59 static int nb_callback_configuration(struct nb_context
*context
,
60 const enum nb_event event
,
61 struct nb_config_change
*change
,
62 char *errmsg
, size_t errmsg_len
);
63 static struct nb_transaction
*
64 nb_transaction_new(struct nb_context context
, struct nb_config
*config
,
65 struct nb_config_cbs
*changes
, const char *comment
,
66 char *errmsg
, size_t errmsg_len
);
67 static void nb_transaction_free(struct nb_transaction
*transaction
);
68 static int nb_transaction_process(enum nb_event event
,
69 struct nb_transaction
*transaction
,
70 char *errmsg
, size_t errmsg_len
);
71 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
,
72 char *errmsg
, size_t errmsg_len
);
73 static int nb_oper_data_iter_node(const struct lysc_node
*snode
,
74 const char *xpath
, const void *list_entry
,
75 const struct yang_list_keys
*list_keys
,
76 struct yang_translator
*translator
,
77 bool first
, uint32_t flags
,
78 nb_oper_data_cb cb
, void *arg
);
80 static int nb_node_check_config_only(const struct lysc_node
*snode
, void *arg
)
82 bool *config_only
= arg
;
84 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
86 return YANG_ITER_STOP
;
89 return YANG_ITER_CONTINUE
;
92 static int nb_node_new_cb(const struct lysc_node
*snode
, void *arg
)
94 struct nb_node
*nb_node
;
95 struct lysc_node
*sparent
, *sparent_list
;
96 struct frr_yang_module_info
*module
;
98 module
= (struct frr_yang_module_info
*)arg
;
99 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
100 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
101 sizeof(nb_node
->xpath
));
102 nb_node
->priority
= NB_DFLT_PRIORITY
;
103 sparent
= yang_snode_real_parent(snode
);
105 nb_node
->parent
= sparent
->priv
;
106 sparent_list
= yang_snode_parent_list(snode
);
108 nb_node
->parent_list
= sparent_list
->priv
;
111 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
112 bool config_only
= true;
114 (void)yang_snodes_iterate_subtree(snode
, NULL
,
115 nb_node_check_config_only
, 0,
118 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
120 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
121 if (yang_snode_num_keys(snode
) == 0)
122 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
126 * Link the northbound node and the libyang schema node with one
129 nb_node
->snode
= snode
;
130 assert(snode
->priv
== NULL
);
131 ((struct lysc_node
*)snode
)->priv
= nb_node
;
133 if (module
&& module
->ignore_cbs
)
134 SET_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
);
136 return YANG_ITER_CONTINUE
;
139 static int nb_node_del_cb(const struct lysc_node
*snode
, void *arg
)
141 struct nb_node
*nb_node
;
143 nb_node
= snode
->priv
;
145 ((struct lysc_node
*)snode
)->priv
= NULL
;
146 XFREE(MTYPE_NB_NODE
, nb_node
);
149 return YANG_ITER_CONTINUE
;
152 void nb_nodes_create(void)
154 yang_snodes_iterate(NULL
, nb_node_new_cb
, 0, NULL
);
157 void nb_nodes_delete(void)
159 yang_snodes_iterate(NULL
, nb_node_del_cb
, 0, NULL
);
162 struct nb_node
*nb_node_find(const char *path
)
164 const struct lysc_node
*snode
;
167 * Use libyang to find the schema node associated to the path and get
168 * the northbound node from there (snode private pointer).
170 snode
= lys_find_path(ly_native_ctx
, NULL
, path
, 0);
177 void nb_node_set_dependency_cbs(const char *dependency_xpath
,
178 const char *dependant_xpath
,
179 struct nb_dependency_callbacks
*cbs
)
181 struct nb_node
*dependency
= nb_node_find(dependency_xpath
);
182 struct nb_node
*dependant
= nb_node_find(dependant_xpath
);
184 if (!dependency
|| !dependant
)
187 dependency
->dep_cbs
.get_dependant_xpath
= cbs
->get_dependant_xpath
;
188 dependant
->dep_cbs
.get_dependency_xpath
= cbs
->get_dependency_xpath
;
191 bool nb_node_has_dependency(struct nb_node
*node
)
193 return node
->dep_cbs
.get_dependency_xpath
!= NULL
;
196 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
197 enum nb_operation operation
,
198 int callback_implemented
, bool optional
)
202 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
205 * Add an exception for operational data callbacks. A rw list usually
206 * doesn't need any associated operational data callbacks. But if this
207 * rw list is augmented by another module which adds state nodes under
208 * it, then this list will need to have the 'get_next()', 'get_keys()'
209 * and 'lookup_entry()' callbacks. As such, never log a warning when
210 * these callbacks are implemented when they are not needed, since this
211 * depends on context (e.g. some daemons might augment "frr-interface"
212 * while others don't).
214 if (!valid
&& callback_implemented
&& operation
!= NB_OP_GET_NEXT
215 && operation
!= NB_OP_GET_KEYS
&& operation
!= NB_OP_LOOKUP_ENTRY
)
216 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
217 "unneeded '%s' callback for '%s'",
218 nb_operation_name(operation
), nb_node
->xpath
);
220 if (!optional
&& valid
&& !callback_implemented
) {
221 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
222 nb_operation_name(operation
), nb_node
->xpath
);
230 * Check if the required callbacks were implemented for the given northbound
233 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
236 unsigned int error
= 0;
238 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
241 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
242 !!nb_node
->cbs
.create
, false);
243 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
244 !!nb_node
->cbs
.modify
, false);
245 error
+= nb_node_validate_cb(nb_node
, NB_OP_DESTROY
,
246 !!nb_node
->cbs
.destroy
, false);
247 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
249 error
+= nb_node_validate_cb(nb_node
, NB_OP_PRE_VALIDATE
,
250 !!nb_node
->cbs
.pre_validate
, true);
251 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
252 !!nb_node
->cbs
.apply_finish
, true);
253 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
254 !!nb_node
->cbs
.get_elem
, false);
255 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
256 !!nb_node
->cbs
.get_next
, false);
257 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
258 !!nb_node
->cbs
.get_keys
, false);
259 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
260 !!nb_node
->cbs
.lookup_entry
, false);
261 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
267 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
269 /* Top-level nodes can have any priority. */
270 if (!nb_node
->parent
)
273 if (nb_node
->priority
< nb_node
->parent
->priority
) {
274 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
275 "node has higher priority than its parent [xpath %s]",
283 static int nb_node_validate(const struct lysc_node
*snode
, void *arg
)
285 struct nb_node
*nb_node
= snode
->priv
;
286 unsigned int *errors
= arg
;
288 /* Validate callbacks and priority. */
290 *errors
+= nb_node_validate_cbs(nb_node
);
291 *errors
+= nb_node_validate_priority(nb_node
);
294 return YANG_ITER_CONTINUE
;
297 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
299 struct nb_config
*config
;
301 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
303 config
->dnode
= dnode
;
305 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
308 RB_INIT(nb_config_cbs
, &config
->cfg_chgs
);
313 void nb_config_free(struct nb_config
*config
)
316 yang_dnode_free(config
->dnode
);
317 nb_config_diff_del_changes(&config
->cfg_chgs
);
318 XFREE(MTYPE_NB_CONFIG
, config
);
321 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
323 struct nb_config
*dup
;
325 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
326 dup
->dnode
= yang_dnode_dup(config
->dnode
);
327 dup
->version
= config
->version
;
329 RB_INIT(nb_config_cbs
, &dup
->cfg_chgs
);
334 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
335 bool preserve_source
)
339 ret
= lyd_merge_siblings(&config_dst
->dnode
, config_src
->dnode
, 0);
341 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
343 if (!preserve_source
)
344 nb_config_free(config_src
);
346 return (ret
== 0) ? NB_OK
: NB_ERR
;
349 void nb_config_replace(struct nb_config
*config_dst
,
350 struct nb_config
*config_src
, bool preserve_source
)
352 /* Update version. */
353 if (config_src
->version
!= 0)
354 config_dst
->version
= config_src
->version
;
357 if (config_dst
->dnode
)
358 yang_dnode_free(config_dst
->dnode
);
359 if (preserve_source
) {
360 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
362 config_dst
->dnode
= config_src
->dnode
;
363 config_src
->dnode
= NULL
;
364 nb_config_free(config_src
);
368 /* Generate the nb_config_cbs tree. */
369 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
370 const struct nb_config_cb
*b
)
372 /* Sort by priority first. */
373 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
375 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
379 * Preserve the order of the configuration changes as told by libyang.
387 * All 'apply_finish' callbacks have their sequence number set to zero.
388 * In this case, compare them using their dnode pointers (the order
389 * doesn't matter for callbacks that have the same priority).
391 if (a
->dnode
< b
->dnode
)
393 if (a
->dnode
> b
->dnode
)
398 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
400 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
401 enum nb_operation operation
,
403 const struct lyd_node
*dnode
)
405 struct nb_config_change
*change
;
407 /* Ignore unimplemented nodes. */
408 if (!dnode
->schema
->priv
)
411 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
412 change
->cb
.operation
= operation
;
413 change
->cb
.seq
= *seq
;
415 change
->cb
.nb_node
= dnode
->schema
->priv
;
416 change
->cb
.dnode
= dnode
;
418 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
421 void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
423 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
424 struct nb_config_change
*change
;
426 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
428 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
429 XFREE(MTYPE_TMP
, change
);
434 * Helper function used when calculating the delta between two different
435 * configurations. Given a new subtree, calculate all new YANG data nodes,
436 * excluding default leafs and leaf-lists. This is a recursive function.
438 void nb_config_diff_created(const struct lyd_node
*dnode
, uint32_t *seq
,
439 struct nb_config_cbs
*changes
)
441 enum nb_operation operation
;
442 struct lyd_node
*child
;
444 /* Ignore unimplemented nodes. */
445 if (!dnode
->schema
->priv
)
448 switch (dnode
->schema
->nodetype
) {
451 if (lyd_is_default(dnode
))
454 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
455 operation
= NB_OP_CREATE
;
456 else if (nb_operation_is_valid(NB_OP_MODIFY
, dnode
->schema
))
457 operation
= NB_OP_MODIFY
;
461 nb_config_diff_add_change(changes
, operation
, seq
, dnode
);
465 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
466 nb_config_diff_add_change(changes
, NB_OP_CREATE
, seq
,
469 /* Process child nodes recursively. */
470 LY_LIST_FOR (lyd_child(dnode
), child
) {
471 nb_config_diff_created(child
, seq
, changes
);
479 static void nb_config_diff_deleted(const struct lyd_node
*dnode
, uint32_t *seq
,
480 struct nb_config_cbs
*changes
)
482 /* Ignore unimplemented nodes. */
483 if (!dnode
->schema
->priv
)
486 if (nb_operation_is_valid(NB_OP_DESTROY
, dnode
->schema
))
487 nb_config_diff_add_change(changes
, NB_OP_DESTROY
, seq
, dnode
);
488 else if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_CONTAINER
)) {
489 struct lyd_node
*child
;
492 * Non-presence containers need special handling since they
493 * don't have "destroy" callbacks. In this case, what we need to
494 * do is to call the "destroy" callbacks of their child nodes
495 * when applicable (i.e. optional nodes).
497 LY_LIST_FOR (lyd_child(dnode
), child
) {
498 nb_config_diff_deleted(child
, seq
, changes
);
503 static int nb_lyd_diff_get_op(const struct lyd_node
*dnode
)
505 const struct lyd_meta
*meta
;
506 LY_LIST_FOR (dnode
->meta
, meta
) {
507 if (strcmp(meta
->name
, "operation")
508 || strcmp(meta
->annotation
->module
->name
, "yang"))
510 return lyd_get_meta_value(meta
)[0];
515 #if 0 /* Used below in nb_config_diff inside normally disabled code */
516 static inline void nb_config_diff_dnode_log_path(const char *context
,
518 const struct lyd_node
*dnode
)
520 if (dnode
->schema
->nodetype
& LYD_NODE_TERM
)
521 zlog_debug("nb_config_diff: %s: %s: %s", context
, path
,
522 lyd_get_value(dnode
));
524 zlog_debug("nb_config_diff: %s: %s", context
, path
);
527 static inline void nb_config_diff_dnode_log(const char *context
,
528 const struct lyd_node
*dnode
)
531 zlog_debug("nb_config_diff: %s: NULL", context
);
535 char *path
= lyd_path(dnode
, LYD_PATH_STD
, NULL
, 0);
536 nb_config_diff_dnode_log_path(context
, path
, dnode
);
542 * Calculate the delta between two different configurations.
544 * NOTE: 'config1' is the reference DB, while 'config2' is
545 * the DB being compared against 'config1'. Typically 'config1'
546 * should be the Running DB and 'config2' is the Candidate DB.
548 void nb_config_diff(const struct nb_config
*config1
,
549 const struct nb_config
*config2
,
550 struct nb_config_cbs
*changes
)
552 struct lyd_node
*diff
= NULL
;
553 const struct lyd_node
*root
, *dnode
;
554 struct lyd_node
*target
;
559 #if 0 /* Useful (noisy) when debugging diff code, and for improving later */
560 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
561 LY_LIST_FOR(config1
->dnode
, root
) {
562 LYD_TREE_DFS_BEGIN(root
, dnode
) {
563 nb_config_diff_dnode_log("from", dnode
);
564 LYD_TREE_DFS_END(root
, dnode
);
567 LY_LIST_FOR(config2
->dnode
, root
) {
568 LYD_TREE_DFS_BEGIN(root
, dnode
) {
569 nb_config_diff_dnode_log("to", dnode
);
570 LYD_TREE_DFS_END(root
, dnode
);
576 err
= lyd_diff_siblings(config1
->dnode
, config2
->dnode
,
577 LYD_DIFF_DEFAULTS
, &diff
);
580 if (diff
&& DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
583 if (!lyd_print_mem(&s
, diff
, LYD_JSON
,
584 LYD_PRINT_WITHSIBLINGS
| LYD_PRINT_WD_ALL
)) {
585 zlog_debug("%s: %s", __func__
, s
);
592 LY_LIST_FOR (diff
, root
) {
593 LYD_TREE_DFS_BEGIN (root
, dnode
) {
594 op
= nb_lyd_diff_get_op(dnode
);
596 path
= lyd_path(dnode
, LYD_PATH_STD
, NULL
, 0);
598 #if 0 /* Useful (noisy) when debugging diff code, and for improving later */
599 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
601 snprintf(context
, sizeof(context
),
602 "iterating diff: oper: %c seq: %u", op
, seq
);
603 nb_config_diff_dnode_log_path(context
, path
, dnode
);
607 case 'c': /* create */
609 * This is rather inefficient, but when we use
610 * dnode from the diff instead of the
611 * candidate config node we get failures when
612 * looking up default values, etc, based on
615 target
= yang_dnode_get(config2
->dnode
, path
);
617 nb_config_diff_created(target
, &seq
, changes
);
619 /* Skip rest of sub-tree, move to next sibling
621 LYD_TREE_DFS_continue
= 1;
623 case 'd': /* delete */
624 target
= yang_dnode_get(config1
->dnode
, path
);
626 nb_config_diff_deleted(target
, &seq
, changes
);
628 /* Skip rest of sub-tree, move to next sibling
630 LYD_TREE_DFS_continue
= 1;
632 case 'r': /* replace */
633 /* either moving an entry or changing a value */
634 target
= yang_dnode_get(config2
->dnode
, path
);
636 nb_config_diff_add_change(changes
, NB_OP_MODIFY
,
644 LYD_TREE_DFS_END(root
, dnode
);
651 int nb_candidate_edit(struct nb_config
*candidate
,
652 const struct nb_node
*nb_node
,
653 enum nb_operation operation
, const char *xpath
,
654 const struct yang_data
*previous
,
655 const struct yang_data
*data
)
657 struct lyd_node
*dnode
, *dep_dnode
;
658 char xpath_edit
[XPATH_MAXLEN
];
659 char dep_xpath
[XPATH_MAXLEN
];
662 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
663 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
664 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
667 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
672 err
= lyd_new_path(candidate
->dnode
, ly_native_ctx
, xpath_edit
,
673 (void *)data
->value
, LYD_NEW_PATH_UPDATE
,
676 flog_warn(EC_LIB_LIBYANG
,
677 "%s: lyd_new_path(%s) failed: %d", __func__
,
681 /* Create default nodes */
682 LY_ERR err
= lyd_new_implicit_tree(
683 dnode
, LYD_IMPLICIT_NO_STATE
, NULL
);
685 flog_warn(EC_LIB_LIBYANG
,
686 "%s: lyd_new_implicit_all failed: %d",
692 * dnode returned by the lyd_new_path may be from a
693 * different schema, so we need to update the nb_node
695 nb_node
= dnode
->schema
->priv
;
696 if (nb_node
->dep_cbs
.get_dependency_xpath
) {
697 nb_node
->dep_cbs
.get_dependency_xpath(
700 err
= lyd_new_path(candidate
->dnode
,
701 ly_native_ctx
, dep_xpath
,
702 NULL
, LYD_NEW_PATH_UPDATE
,
704 /* Create default nodes */
705 if (!err
&& dep_dnode
)
706 err
= lyd_new_implicit_tree(
708 LYD_IMPLICIT_NO_STATE
, NULL
);
712 "%s: dependency: lyd_new_path(%s) failed: %d",
713 __func__
, dep_xpath
, err
);
720 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
723 * Return a special error code so the caller can choose
724 * whether to ignore it or not.
726 return NB_ERR_NOT_FOUND
;
727 /* destroy dependant */
728 if (nb_node
->dep_cbs
.get_dependant_xpath
) {
729 nb_node
->dep_cbs
.get_dependant_xpath(dnode
, dep_xpath
);
731 dep_dnode
= yang_dnode_get(candidate
->dnode
, dep_xpath
);
733 lyd_free_tree(dep_dnode
);
735 lyd_free_tree(dnode
);
738 /* TODO: update configuration. */
740 case NB_OP_PRE_VALIDATE
:
741 case NB_OP_APPLY_FINISH
:
745 case NB_OP_LOOKUP_ENTRY
:
747 flog_warn(EC_LIB_DEVELOPMENT
,
748 "%s: unknown operation (%u) [xpath %s]", __func__
,
749 operation
, xpath_edit
);
756 static void nb_update_candidate_changes(struct nb_config
*candidate
,
757 struct nb_cfg_change
*change
,
760 enum nb_operation oper
= change
->operation
;
761 char *xpath
= change
->xpath
;
762 struct lyd_node
*root
= NULL
;
763 struct lyd_node
*dnode
;
764 struct nb_config_cbs
*cfg_chgs
= &candidate
->cfg_chgs
;
770 root
= yang_dnode_get(candidate
->dnode
, xpath
);
773 root
= yang_dnode_get(running_config
->dnode
, xpath
);
777 case NB_OP_PRE_VALIDATE
:
778 case NB_OP_APPLY_FINISH
:
782 case NB_OP_LOOKUP_ENTRY
:
786 assert(!"non-enum value, invalid");
792 LYD_TREE_DFS_BEGIN (root
, dnode
) {
793 op
= nb_lyd_diff_get_op(dnode
);
796 nb_config_diff_created(dnode
, seq
, cfg_chgs
);
797 LYD_TREE_DFS_continue
= 1;
800 nb_config_diff_deleted(dnode
, seq
, cfg_chgs
);
801 LYD_TREE_DFS_continue
= 1;
804 nb_config_diff_add_change(cfg_chgs
, NB_OP_MODIFY
, seq
,
810 LYD_TREE_DFS_END(root
, dnode
);
814 static bool nb_is_operation_allowed(struct nb_node
*nb_node
,
815 struct nb_cfg_change
*change
)
817 enum nb_operation oper
= change
->operation
;
819 if (lysc_is_key(nb_node
->snode
)) {
820 if (oper
== NB_OP_MODIFY
|| oper
== NB_OP_DESTROY
)
826 void nb_candidate_edit_config_changes(
827 struct nb_config
*candidate_config
, struct nb_cfg_change cfg_changes
[],
828 size_t num_cfg_changes
, const char *xpath_base
, const char *curr_xpath
,
829 int xpath_index
, char *err_buf
, int err_bufsize
, bool *error
)
836 if (xpath_base
== NULL
)
839 /* Edit candidate configuration. */
840 for (size_t i
= 0; i
< num_cfg_changes
; i
++) {
841 struct nb_cfg_change
*change
= &cfg_changes
[i
];
842 struct nb_node
*nb_node
;
843 char xpath
[XPATH_MAXLEN
];
844 struct yang_data
*data
;
847 /* Handle relative XPaths. */
848 memset(xpath
, 0, sizeof(xpath
));
850 && (xpath_base
[0] == '.' || change
->xpath
[0] == '.'))
851 strlcpy(xpath
, curr_xpath
, sizeof(xpath
));
853 if (xpath_base
[0] == '.')
854 strlcat(xpath
, xpath_base
+ 1, sizeof(xpath
));
856 strlcat(xpath
, xpath_base
, sizeof(xpath
));
858 if (change
->xpath
[0] == '.')
859 strlcat(xpath
, change
->xpath
+ 1, sizeof(xpath
));
861 strlcpy(xpath
, change
->xpath
, sizeof(xpath
));
863 /* Find the northbound node associated to the data path. */
864 nb_node
= nb_node_find(xpath
);
866 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
867 "%s: unknown data path: %s", __func__
, xpath
);
872 /* Find if the node to be edited is not a key node */
873 if (!nb_is_operation_allowed(nb_node
, change
)) {
874 zlog_err(" Xpath %s points to key node", xpath
);
880 /* If the value is not set, get the default if it exists. */
881 if (change
->value
== NULL
)
882 change
->value
= yang_snode_get_default(nb_node
->snode
);
883 data
= yang_data_new(xpath
, change
->value
);
886 * Ignore "not found" errors when editing the candidate
889 ret
= nb_candidate_edit(candidate_config
, nb_node
,
890 change
->operation
, xpath
, NULL
, data
);
891 yang_data_free(data
);
892 if (ret
!= NB_OK
&& ret
!= NB_ERR_NOT_FOUND
) {
894 EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
895 "%s: failed to edit candidate configuration: operation [%s] xpath [%s]",
896 __func__
, nb_operation_name(change
->operation
),
902 nb_update_candidate_changes(candidate_config
, change
, &seq
);
905 if (error
&& *error
) {
909 * Failure to edit the candidate configuration should never
910 * happen in practice, unless there's a bug in the code. When
911 * that happens, log the error but otherwise ignore it.
913 snprintf(err_buf
, err_bufsize
,
914 "%% Failed to edit configuration.\n\n%s",
915 yang_print_errors(ly_native_ctx
, buf
, sizeof(buf
)));
919 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
921 if (candidate
->version
< running_config
->version
)
927 int nb_candidate_update(struct nb_config
*candidate
)
929 struct nb_config
*updated_config
;
931 updated_config
= nb_config_dup(running_config
);
932 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
935 nb_config_replace(candidate
, updated_config
, false);
941 * Perform YANG syntactic and semantic validation.
943 * WARNING: lyd_validate() can change the configuration as part of the
944 * validation process.
946 int nb_candidate_validate_yang(struct nb_config
*candidate
, bool no_state
,
947 char *errmsg
, size_t errmsg_len
)
949 if (lyd_validate_all(&candidate
->dnode
, ly_native_ctx
,
950 no_state
? LYD_VALIDATE_NO_STATE
:
951 LYD_VALIDATE_PRESENT
, NULL
)
953 yang_print_errors(ly_native_ctx
, errmsg
, errmsg_len
);
954 return NB_ERR_VALIDATION
;
960 /* Perform code-level validation using the northbound callbacks. */
961 int nb_candidate_validate_code(struct nb_context
*context
,
962 struct nb_config
*candidate
,
963 struct nb_config_cbs
*changes
, char *errmsg
,
966 struct nb_config_cb
*cb
;
967 struct lyd_node
*root
, *child
;
970 /* First validate the candidate as a whole. */
971 LY_LIST_FOR (candidate
->dnode
, root
) {
972 LYD_TREE_DFS_BEGIN (root
, child
) {
973 struct nb_node
*nb_node
;
975 nb_node
= child
->schema
->priv
;
976 if (!nb_node
|| !nb_node
->cbs
.pre_validate
)
979 ret
= nb_callback_pre_validate(context
, nb_node
, child
,
982 return NB_ERR_VALIDATION
;
985 LYD_TREE_DFS_END(root
, child
);
989 /* Now validate the configuration changes. */
990 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
991 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
993 ret
= nb_callback_configuration(context
, NB_EV_VALIDATE
, change
,
996 return NB_ERR_VALIDATION
;
1002 int nb_candidate_diff_and_validate_yang(struct nb_context
*context
,
1003 struct nb_config
*candidate
,
1004 struct nb_config_cbs
*changes
,
1005 char *errmsg
, size_t errmsg_len
)
1007 if (nb_candidate_validate_yang(candidate
, true, errmsg
,
1010 return NB_ERR_VALIDATION
;
1012 RB_INIT(nb_config_cbs
, changes
);
1013 nb_config_diff(running_config
, candidate
, changes
);
1018 int nb_candidate_validate(struct nb_context
*context
,
1019 struct nb_config
*candidate
, char *errmsg
,
1022 struct nb_config_cbs changes
;
1025 ret
= nb_candidate_diff_and_validate_yang(context
, candidate
, &changes
,
1026 errmsg
, errmsg_len
);
1030 ret
= nb_candidate_validate_code(context
, candidate
, &changes
, errmsg
,
1032 nb_config_diff_del_changes(&changes
);
1037 int nb_candidate_commit_prepare(struct nb_context context
,
1038 struct nb_config
*candidate
,
1039 const char *comment
,
1040 struct nb_transaction
**transaction
,
1041 bool skip_validate
, bool ignore_zero_change
,
1042 char *errmsg
, size_t errmsg_len
)
1044 struct nb_config_cbs changes
;
1047 && nb_candidate_validate_yang(candidate
, true, errmsg
, errmsg_len
)
1049 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
1050 "%s: failed to validate candidate configuration",
1052 return NB_ERR_VALIDATION
;
1055 RB_INIT(nb_config_cbs
, &changes
);
1056 nb_config_diff(running_config
, candidate
, &changes
);
1057 if (!ignore_zero_change
&& RB_EMPTY(nb_config_cbs
, &changes
)) {
1060 "No changes to apply were found during preparation phase");
1061 return NB_ERR_NO_CHANGES
;
1065 && nb_candidate_validate_code(&context
, candidate
, &changes
, errmsg
,
1068 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
1069 "%s: failed to validate candidate configuration",
1071 nb_config_diff_del_changes(&changes
);
1072 return NB_ERR_VALIDATION
;
1076 * Re-use an existing transaction if provided. Else allocate a new one.
1079 *transaction
= nb_transaction_new(context
, candidate
, &changes
,
1080 comment
, errmsg
, errmsg_len
);
1081 if (*transaction
== NULL
) {
1082 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
1083 "%s: failed to create transaction: %s", __func__
,
1085 nb_config_diff_del_changes(&changes
);
1086 return NB_ERR_LOCKED
;
1089 return nb_transaction_process(NB_EV_PREPARE
, *transaction
, errmsg
,
1093 void nb_candidate_commit_abort(struct nb_transaction
*transaction
, char *errmsg
,
1096 (void)nb_transaction_process(NB_EV_ABORT
, transaction
, errmsg
,
1098 nb_transaction_free(transaction
);
1101 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
1102 bool save_transaction
, uint32_t *transaction_id
,
1103 char *errmsg
, size_t errmsg_len
)
1105 (void)nb_transaction_process(NB_EV_APPLY
, transaction
, errmsg
,
1107 nb_transaction_apply_finish(transaction
, errmsg
, errmsg_len
);
1109 /* Replace running by candidate. */
1110 transaction
->config
->version
++;
1111 nb_config_replace(running_config
, transaction
->config
, true);
1113 /* Record transaction. */
1114 if (save_transaction
&& nb_db_enabled
1115 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
1116 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
1117 "%s: failed to record transaction", __func__
);
1119 nb_transaction_free(transaction
);
1122 int nb_candidate_commit(struct nb_context context
, struct nb_config
*candidate
,
1123 bool save_transaction
, const char *comment
,
1124 uint32_t *transaction_id
, char *errmsg
,
1127 struct nb_transaction
*transaction
= NULL
;
1130 ret
= nb_candidate_commit_prepare(context
, candidate
, comment
,
1131 &transaction
, false, false, errmsg
,
1134 * Apply the changes if the preparation phase succeeded. Otherwise abort
1138 nb_candidate_commit_apply(transaction
, save_transaction
,
1139 transaction_id
, errmsg
, errmsg_len
);
1140 else if (transaction
!= NULL
)
1141 nb_candidate_commit_abort(transaction
, errmsg
, errmsg_len
);
1146 int nb_running_lock(enum nb_client client
, const void *user
)
1150 frr_with_mutex (&running_config_mgmt_lock
.mtx
) {
1151 if (!running_config_mgmt_lock
.locked
) {
1152 running_config_mgmt_lock
.locked
= true;
1153 running_config_mgmt_lock
.owner_client
= client
;
1154 running_config_mgmt_lock
.owner_user
= user
;
1162 int nb_running_unlock(enum nb_client client
, const void *user
)
1166 frr_with_mutex (&running_config_mgmt_lock
.mtx
) {
1167 if (running_config_mgmt_lock
.locked
1168 && running_config_mgmt_lock
.owner_client
== client
1169 && running_config_mgmt_lock
.owner_user
== user
) {
1170 running_config_mgmt_lock
.locked
= false;
1171 running_config_mgmt_lock
.owner_client
= NB_CLIENT_NONE
;
1172 running_config_mgmt_lock
.owner_user
= NULL
;
1180 int nb_running_lock_check(enum nb_client client
, const void *user
)
1184 frr_with_mutex (&running_config_mgmt_lock
.mtx
) {
1185 if (!running_config_mgmt_lock
.locked
1186 || (running_config_mgmt_lock
.owner_client
== client
1187 && running_config_mgmt_lock
.owner_user
== user
))
1194 static void nb_log_config_callback(const enum nb_event event
,
1195 enum nb_operation operation
,
1196 const struct lyd_node
*dnode
)
1199 char xpath
[XPATH_MAXLEN
];
1201 if (!DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
))
1204 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1205 if (yang_snode_is_typeless_data(dnode
->schema
))
1208 value
= yang_dnode_get_string(dnode
, NULL
);
1211 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
1212 nb_event_name(event
), nb_operation_name(operation
), xpath
,
1216 static int nb_callback_create(struct nb_context
*context
,
1217 const struct nb_node
*nb_node
,
1218 enum nb_event event
, const struct lyd_node
*dnode
,
1219 union nb_resource
*resource
, char *errmsg
,
1222 struct nb_cb_create_args args
= {};
1223 bool unexpected_error
= false;
1226 assert(!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
));
1228 nb_log_config_callback(event
, NB_OP_CREATE
, dnode
);
1230 args
.context
= context
;
1233 args
.resource
= resource
;
1234 args
.errmsg
= errmsg
;
1235 args
.errmsg_len
= errmsg_len
;
1236 ret
= nb_node
->cbs
.create(&args
);
1238 /* Detect and log unexpected errors. */
1243 case NB_ERR_VALIDATION
:
1244 if (event
!= NB_EV_VALIDATE
)
1245 unexpected_error
= true;
1247 case NB_ERR_RESOURCE
:
1248 if (event
!= NB_EV_PREPARE
)
1249 unexpected_error
= true;
1251 case NB_ERR_INCONSISTENCY
:
1252 if (event
== NB_EV_VALIDATE
)
1253 unexpected_error
= true;
1256 unexpected_error
= true;
1259 if (unexpected_error
)
1260 DEBUGD(&nb_dbg_cbs_config
,
1261 "northbound callback: unexpected return value: %s",
1267 static int nb_callback_modify(struct nb_context
*context
,
1268 const struct nb_node
*nb_node
,
1269 enum nb_event event
, const struct lyd_node
*dnode
,
1270 union nb_resource
*resource
, char *errmsg
,
1273 struct nb_cb_modify_args args
= {};
1274 bool unexpected_error
= false;
1277 assert(!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
));
1279 nb_log_config_callback(event
, NB_OP_MODIFY
, dnode
);
1281 args
.context
= context
;
1284 args
.resource
= resource
;
1285 args
.errmsg
= errmsg
;
1286 args
.errmsg_len
= errmsg_len
;
1287 ret
= nb_node
->cbs
.modify(&args
);
1289 /* Detect and log unexpected errors. */
1294 case NB_ERR_VALIDATION
:
1295 if (event
!= NB_EV_VALIDATE
)
1296 unexpected_error
= true;
1298 case NB_ERR_RESOURCE
:
1299 if (event
!= NB_EV_PREPARE
)
1300 unexpected_error
= true;
1302 case NB_ERR_INCONSISTENCY
:
1303 if (event
== NB_EV_VALIDATE
)
1304 unexpected_error
= true;
1307 unexpected_error
= true;
1310 if (unexpected_error
)
1311 DEBUGD(&nb_dbg_cbs_config
,
1312 "northbound callback: unexpected return value: %s",
1318 static int nb_callback_destroy(struct nb_context
*context
,
1319 const struct nb_node
*nb_node
,
1320 enum nb_event event
,
1321 const struct lyd_node
*dnode
, char *errmsg
,
1324 struct nb_cb_destroy_args args
= {};
1325 bool unexpected_error
= false;
1328 assert(!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
));
1330 nb_log_config_callback(event
, NB_OP_DESTROY
, dnode
);
1332 args
.context
= context
;
1335 args
.errmsg
= errmsg
;
1336 args
.errmsg_len
= errmsg_len
;
1337 ret
= nb_node
->cbs
.destroy(&args
);
1339 /* Detect and log unexpected errors. */
1344 case NB_ERR_VALIDATION
:
1345 if (event
!= NB_EV_VALIDATE
)
1346 unexpected_error
= true;
1348 case NB_ERR_INCONSISTENCY
:
1349 if (event
== NB_EV_VALIDATE
)
1350 unexpected_error
= true;
1353 unexpected_error
= true;
1356 if (unexpected_error
)
1357 DEBUGD(&nb_dbg_cbs_config
,
1358 "northbound callback: unexpected return value: %s",
1364 static int nb_callback_move(struct nb_context
*context
,
1365 const struct nb_node
*nb_node
, enum nb_event event
,
1366 const struct lyd_node
*dnode
, char *errmsg
,
1369 struct nb_cb_move_args args
= {};
1370 bool unexpected_error
= false;
1373 assert(!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
));
1375 nb_log_config_callback(event
, NB_OP_MOVE
, dnode
);
1377 args
.context
= context
;
1380 args
.errmsg
= errmsg
;
1381 args
.errmsg_len
= errmsg_len
;
1382 ret
= nb_node
->cbs
.move(&args
);
1384 /* Detect and log unexpected errors. */
1389 case NB_ERR_VALIDATION
:
1390 if (event
!= NB_EV_VALIDATE
)
1391 unexpected_error
= true;
1393 case NB_ERR_INCONSISTENCY
:
1394 if (event
== NB_EV_VALIDATE
)
1395 unexpected_error
= true;
1398 unexpected_error
= true;
1401 if (unexpected_error
)
1402 DEBUGD(&nb_dbg_cbs_config
,
1403 "northbound callback: unexpected return value: %s",
1409 static int nb_callback_pre_validate(struct nb_context
*context
,
1410 const struct nb_node
*nb_node
,
1411 const struct lyd_node
*dnode
, char *errmsg
,
1414 struct nb_cb_pre_validate_args args
= {};
1415 bool unexpected_error
= false;
1418 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1421 nb_log_config_callback(NB_EV_VALIDATE
, NB_OP_PRE_VALIDATE
, dnode
);
1424 args
.errmsg
= errmsg
;
1425 args
.errmsg_len
= errmsg_len
;
1426 ret
= nb_node
->cbs
.pre_validate(&args
);
1428 /* Detect and log unexpected errors. */
1431 case NB_ERR_VALIDATION
:
1434 unexpected_error
= true;
1437 if (unexpected_error
)
1438 DEBUGD(&nb_dbg_cbs_config
,
1439 "northbound callback: unexpected return value: %s",
1445 static void nb_callback_apply_finish(struct nb_context
*context
,
1446 const struct nb_node
*nb_node
,
1447 const struct lyd_node
*dnode
, char *errmsg
,
1450 struct nb_cb_apply_finish_args args
= {};
1452 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1455 nb_log_config_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
, dnode
);
1457 args
.context
= context
;
1459 args
.errmsg
= errmsg
;
1460 args
.errmsg_len
= errmsg_len
;
1461 nb_node
->cbs
.apply_finish(&args
);
1464 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
1466 const void *list_entry
)
1468 struct nb_cb_get_elem_args args
= {};
1470 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1473 DEBUGD(&nb_dbg_cbs_state
,
1474 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
1478 args
.list_entry
= list_entry
;
1479 return nb_node
->cbs
.get_elem(&args
);
1482 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
1483 const void *parent_list_entry
,
1484 const void *list_entry
)
1486 struct nb_cb_get_next_args args
= {};
1488 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1491 DEBUGD(&nb_dbg_cbs_state
,
1492 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
1493 nb_node
->xpath
, parent_list_entry
, list_entry
);
1495 args
.parent_list_entry
= parent_list_entry
;
1496 args
.list_entry
= list_entry
;
1497 return nb_node
->cbs
.get_next(&args
);
1500 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
1501 struct yang_list_keys
*keys
)
1503 struct nb_cb_get_keys_args args
= {};
1505 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1508 DEBUGD(&nb_dbg_cbs_state
,
1509 "northbound callback (get_keys): node [%s] list_entry [%p]",
1510 nb_node
->xpath
, list_entry
);
1512 args
.list_entry
= list_entry
;
1514 return nb_node
->cbs
.get_keys(&args
);
1517 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
1518 const void *parent_list_entry
,
1519 const struct yang_list_keys
*keys
)
1521 struct nb_cb_lookup_entry_args args
= {};
1523 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1526 DEBUGD(&nb_dbg_cbs_state
,
1527 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
1528 nb_node
->xpath
, parent_list_entry
);
1530 args
.parent_list_entry
= parent_list_entry
;
1532 return nb_node
->cbs
.lookup_entry(&args
);
1535 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
1536 const struct list
*input
, struct list
*output
, char *errmsg
,
1539 struct nb_cb_rpc_args args
= {};
1541 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1544 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
1548 args
.output
= output
;
1549 args
.errmsg
= errmsg
;
1550 args
.errmsg_len
= errmsg_len
;
1551 return nb_node
->cbs
.rpc(&args
);
1555 * Call the northbound configuration callback associated to a given
1556 * configuration change.
1558 static int nb_callback_configuration(struct nb_context
*context
,
1559 const enum nb_event event
,
1560 struct nb_config_change
*change
,
1561 char *errmsg
, size_t errmsg_len
)
1563 enum nb_operation operation
= change
->cb
.operation
;
1564 char xpath
[XPATH_MAXLEN
];
1565 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
1566 const struct lyd_node
*dnode
= change
->cb
.dnode
;
1567 union nb_resource
*resource
;
1570 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_IGNORE_CBS
))
1573 if (event
== NB_EV_VALIDATE
)
1576 resource
= &change
->resource
;
1578 switch (operation
) {
1580 ret
= nb_callback_create(context
, nb_node
, event
, dnode
,
1581 resource
, errmsg
, errmsg_len
);
1584 ret
= nb_callback_modify(context
, nb_node
, event
, dnode
,
1585 resource
, errmsg
, errmsg_len
);
1588 ret
= nb_callback_destroy(context
, nb_node
, event
, dnode
,
1589 errmsg
, errmsg_len
);
1592 ret
= nb_callback_move(context
, nb_node
, event
, dnode
, errmsg
,
1595 case NB_OP_PRE_VALIDATE
:
1596 case NB_OP_APPLY_FINISH
:
1597 case NB_OP_GET_ELEM
:
1598 case NB_OP_GET_NEXT
:
1599 case NB_OP_GET_KEYS
:
1600 case NB_OP_LOOKUP_ENTRY
:
1602 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1603 flog_err(EC_LIB_DEVELOPMENT
,
1604 "%s: unknown operation (%u) [xpath %s]", __func__
,
1610 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1613 case NB_EV_VALIDATE
:
1614 flog_warn(EC_LIB_NB_CB_CONFIG_VALIDATE
,
1615 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1616 nb_err_name(ret
), nb_event_name(event
),
1617 nb_operation_name(operation
), xpath
,
1618 errmsg
[0] ? " message: " : "", errmsg
);
1621 flog_warn(EC_LIB_NB_CB_CONFIG_PREPARE
,
1622 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1623 nb_err_name(ret
), nb_event_name(event
),
1624 nb_operation_name(operation
), xpath
,
1625 errmsg
[0] ? " message: " : "", errmsg
);
1628 flog_warn(EC_LIB_NB_CB_CONFIG_ABORT
,
1629 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1630 nb_err_name(ret
), nb_event_name(event
),
1631 nb_operation_name(operation
), xpath
,
1632 errmsg
[0] ? " message: " : "", errmsg
);
1635 flog_err(EC_LIB_NB_CB_CONFIG_APPLY
,
1636 "error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]%s%s",
1637 nb_err_name(ret
), nb_event_name(event
),
1638 nb_operation_name(operation
), xpath
,
1639 errmsg
[0] ? " message: " : "", errmsg
);
1642 flog_err(EC_LIB_DEVELOPMENT
,
1643 "%s: unknown event (%u) [xpath %s]", __func__
,
1652 static struct nb_transaction
*
1653 nb_transaction_new(struct nb_context context
, struct nb_config
*config
,
1654 struct nb_config_cbs
*changes
, const char *comment
,
1655 char *errmsg
, size_t errmsg_len
)
1657 struct nb_transaction
*transaction
;
1659 if (nb_running_lock_check(context
.client
, context
.user
)) {
1661 "running configuration is locked by another client",
1666 if (transaction_in_progress
) {
1668 "there's already another transaction in progress",
1672 transaction_in_progress
= true;
1674 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
1675 transaction
->context
= context
;
1677 strlcpy(transaction
->comment
, comment
,
1678 sizeof(transaction
->comment
));
1679 transaction
->config
= config
;
1680 transaction
->changes
= *changes
;
1685 static void nb_transaction_free(struct nb_transaction
*transaction
)
1687 nb_config_diff_del_changes(&transaction
->changes
);
1688 XFREE(MTYPE_TMP
, transaction
);
1689 transaction_in_progress
= false;
1692 /* Process all configuration changes associated to a transaction. */
1693 static int nb_transaction_process(enum nb_event event
,
1694 struct nb_transaction
*transaction
,
1695 char *errmsg
, size_t errmsg_len
)
1697 struct nb_config_cb
*cb
;
1699 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
1700 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
1704 * Only try to release resources that were allocated
1707 if (event
== NB_EV_ABORT
&& !change
->prepare_ok
)
1710 /* Call the appropriate callback. */
1711 ret
= nb_callback_configuration(&transaction
->context
, event
,
1712 change
, errmsg
, errmsg_len
);
1717 change
->prepare_ok
= true;
1722 * At this point it's not possible to reject the
1723 * transaction anymore, so any failure here can lead to
1724 * inconsistencies and should be treated as a bug.
1725 * Operations prone to errors, like validations and
1726 * resource allocations, should be performed during the
1730 case NB_EV_VALIDATE
:
1738 static struct nb_config_cb
*
1739 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const struct nb_node
*nb_node
,
1740 const struct lyd_node
*dnode
)
1742 struct nb_config_cb
*cb
;
1744 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
1745 cb
->nb_node
= nb_node
;
1747 RB_INSERT(nb_config_cbs
, cbs
, cb
);
1752 static struct nb_config_cb
*
1753 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
,
1754 const struct nb_node
*nb_node
,
1755 const struct lyd_node
*dnode
)
1757 struct nb_config_cb s
;
1760 s
.nb_node
= nb_node
;
1762 return RB_FIND(nb_config_cbs
, cbs
, &s
);
1765 /* Call the 'apply_finish' callbacks. */
1766 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
,
1767 char *errmsg
, size_t errmsg_len
)
1769 struct nb_config_cbs cbs
;
1770 struct nb_config_cb
*cb
;
1772 /* Initialize tree of 'apply_finish' callbacks. */
1773 RB_INIT(nb_config_cbs
, &cbs
);
1775 /* Identify the 'apply_finish' callbacks that need to be called. */
1776 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
1777 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
1778 const struct lyd_node
*dnode
= change
->cb
.dnode
;
1781 * Iterate up to the root of the data tree. When a node is being
1782 * deleted, skip its 'apply_finish' callback if one is defined
1783 * (the 'apply_finish' callbacks from the node ancestors should
1784 * be called though).
1786 if (change
->cb
.operation
== NB_OP_DESTROY
) {
1787 char xpath
[XPATH_MAXLEN
];
1789 dnode
= lyd_parent(dnode
);
1794 * The dnode from 'delete' callbacks point to elements
1795 * from the running configuration. Use yang_dnode_get()
1796 * to get the corresponding dnode from the candidate
1797 * configuration that is being committed.
1799 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1800 dnode
= yang_dnode_get(transaction
->config
->dnode
,
1804 struct nb_node
*nb_node
;
1806 nb_node
= dnode
->schema
->priv
;
1807 if (!nb_node
|| !nb_node
->cbs
.apply_finish
)
1811 * Don't call the callback more than once for the same
1814 if (nb_apply_finish_cb_find(&cbs
, nb_node
, dnode
))
1817 nb_apply_finish_cb_new(&cbs
, nb_node
, dnode
);
1820 dnode
= lyd_parent(dnode
);
1824 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1825 RB_FOREACH (cb
, nb_config_cbs
, &cbs
)
1826 nb_callback_apply_finish(&transaction
->context
, cb
->nb_node
,
1827 cb
->dnode
, errmsg
, errmsg_len
);
1829 /* Release memory. */
1830 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
1831 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
1832 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
1833 XFREE(MTYPE_TMP
, cb
);
1837 static int nb_oper_data_iter_children(const struct lysc_node
*snode
,
1838 const char *xpath
, const void *list_entry
,
1839 const struct yang_list_keys
*list_keys
,
1840 struct yang_translator
*translator
,
1841 bool first
, uint32_t flags
,
1842 nb_oper_data_cb cb
, void *arg
)
1844 const struct lysc_node
*child
;
1846 LY_LIST_FOR (lysc_node_child(snode
), child
) {
1849 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1850 list_keys
, translator
, false,
1859 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1860 const char *xpath
, const void *list_entry
,
1861 const struct yang_list_keys
*list_keys
,
1862 struct yang_translator
*translator
,
1863 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1865 struct yang_data
*data
;
1867 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1870 /* Ignore list keys. */
1871 if (lysc_is_key(nb_node
->snode
))
1874 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1876 /* Leaf of type "empty" is not present. */
1879 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1882 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1884 const void *list_entry
,
1885 const struct yang_list_keys
*list_keys
,
1886 struct yang_translator
*translator
,
1887 uint32_t flags
, nb_oper_data_cb cb
,
1890 const struct lysc_node
*snode
= nb_node
->snode
;
1892 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1895 /* Read-only presence containers. */
1896 if (nb_node
->cbs
.get_elem
) {
1897 struct yang_data
*data
;
1900 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1902 /* Presence container is not present. */
1905 ret
= (*cb
)(snode
, translator
, data
, arg
);
1910 /* Read-write presence containers. */
1911 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
)) {
1912 struct lysc_node_container
*scontainer
;
1914 scontainer
= (struct lysc_node_container
*)snode
;
1915 if (CHECK_FLAG(scontainer
->flags
, LYS_PRESENCE
)
1916 && !yang_dnode_get(running_config
->dnode
, xpath
))
1920 /* Iterate over the child nodes. */
1921 return nb_oper_data_iter_children(snode
, xpath
, list_entry
, list_keys
,
1922 translator
, false, flags
, cb
, arg
);
1926 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1927 const void *parent_list_entry
,
1928 const struct yang_list_keys
*parent_list_keys
,
1929 struct yang_translator
*translator
, uint32_t flags
,
1930 nb_oper_data_cb cb
, void *arg
)
1932 const void *list_entry
= NULL
;
1934 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1938 struct yang_data
*data
;
1941 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1944 /* End of the list. */
1947 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1951 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1954 } while (list_entry
);
1959 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1960 const char *xpath_list
,
1961 const void *parent_list_entry
,
1962 const struct yang_list_keys
*parent_list_keys
,
1963 struct yang_translator
*translator
,
1964 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1966 const struct lysc_node
*snode
= nb_node
->snode
;
1967 const void *list_entry
= NULL
;
1968 uint32_t position
= 1;
1970 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1973 /* Iterate over all list entries. */
1975 const struct lysc_node_leaf
*skey
;
1976 struct yang_list_keys list_keys
= {};
1977 char xpath
[XPATH_MAXLEN
* 2];
1980 /* Obtain list entry. */
1981 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1984 /* End of the list. */
1987 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1988 /* Obtain the list entry keys. */
1989 if (nb_callback_get_keys(nb_node
, list_entry
,
1992 flog_warn(EC_LIB_NB_CB_STATE
,
1993 "%s: failed to get list keys",
1998 /* Build XPath of the list entry. */
1999 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
2001 LY_FOR_KEYS (snode
, skey
) {
2002 assert(i
< list_keys
.num
);
2003 snprintf(xpath
+ strlen(xpath
),
2004 sizeof(xpath
) - strlen(xpath
),
2005 "[%s='%s']", skey
->name
,
2009 assert(i
== list_keys
.num
);
2012 * Keyless list - build XPath using a positional index.
2014 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
2019 /* Iterate over the child nodes. */
2020 ret
= nb_oper_data_iter_children(
2021 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
2022 translator
, false, flags
, cb
, arg
);
2025 } while (list_entry
);
2030 static int nb_oper_data_iter_node(const struct lysc_node
*snode
,
2031 const char *xpath_parent
,
2032 const void *list_entry
,
2033 const struct yang_list_keys
*list_keys
,
2034 struct yang_translator
*translator
,
2035 bool first
, uint32_t flags
,
2036 nb_oper_data_cb cb
, void *arg
)
2038 struct nb_node
*nb_node
;
2039 char xpath
[XPATH_MAXLEN
];
2042 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
2043 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
2047 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
2048 if (!first
&& snode
->nodetype
!= LYS_USES
) {
2049 struct lysc_node
*parent
;
2051 /* Get the real parent. */
2052 parent
= snode
->parent
;
2055 * When necessary, include the namespace of the augmenting
2058 if (parent
&& parent
->module
!= snode
->module
)
2059 snprintf(xpath
+ strlen(xpath
),
2060 sizeof(xpath
) - strlen(xpath
), "/%s:%s",
2061 snode
->module
->name
, snode
->name
);
2063 snprintf(xpath
+ strlen(xpath
),
2064 sizeof(xpath
) - strlen(xpath
), "/%s",
2068 nb_node
= snode
->priv
;
2069 switch (snode
->nodetype
) {
2071 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
2072 list_keys
, translator
, flags
,
2076 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
2077 list_keys
, translator
, flags
, cb
,
2081 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
2082 list_keys
, translator
, flags
,
2086 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
2087 list_keys
, translator
, flags
, cb
,
2091 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
2092 list_keys
, translator
, false,
2102 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
2103 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
2105 struct nb_node
*nb_node
;
2106 const void *list_entry
= NULL
;
2107 struct yang_list_keys list_keys
;
2108 struct list
*list_dnodes
;
2109 struct lyd_node
*dnode
, *dn
;
2110 struct listnode
*ln
;
2113 nb_node
= nb_node_find(xpath
);
2115 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
2116 "%s: unknown data path: %s", __func__
, xpath
);
2120 /* For now this function works only with containers and lists. */
2121 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
2123 EC_LIB_NB_OPERATIONAL_DATA
,
2124 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
2130 * Create a data tree from the XPath so that we can parse the keys of
2131 * all YANG lists (if any).
2134 LY_ERR err
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
,
2135 LYD_NEW_PATH_UPDATE
, &dnode
);
2136 if (err
|| !dnode
) {
2137 const char *errmsg
=
2138 err
? ly_errmsg(ly_native_ctx
) : "node not found";
2139 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed %s",
2145 * Create a linked list to sort the data nodes starting from the root.
2147 list_dnodes
= list_new();
2148 for (dn
= dnode
; dn
; dn
= lyd_parent(dn
)) {
2149 if (dn
->schema
->nodetype
!= LYS_LIST
|| !lyd_child(dn
))
2151 listnode_add_head(list_dnodes
, dn
);
2154 * Use the northbound callbacks to find list entry pointer corresponding
2155 * to the given XPath.
2157 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
2158 struct lyd_node
*child
;
2162 /* Obtain the list entry keys. */
2163 memset(&list_keys
, 0, sizeof(list_keys
));
2164 LY_LIST_FOR (lyd_child(dn
), child
) {
2165 if (!lysc_is_key(child
->schema
))
2167 strlcpy(list_keys
.key
[n
],
2168 yang_dnode_get_string(child
, NULL
),
2169 sizeof(list_keys
.key
[n
]));
2173 if (list_keys
.num
!= yang_snode_num_keys(dn
->schema
)) {
2174 list_delete(&list_dnodes
);
2175 yang_dnode_free(dnode
);
2176 return NB_ERR_NOT_FOUND
;
2179 /* Find the list entry pointer. */
2180 nn
= dn
->schema
->priv
;
2181 if (!nn
->cbs
.lookup_entry
) {
2183 EC_LIB_NB_OPERATIONAL_DATA
,
2184 "%s: data path doesn't support iteration over operational data: %s",
2186 list_delete(&list_dnodes
);
2187 yang_dnode_free(dnode
);
2192 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
2193 if (list_entry
== NULL
) {
2194 list_delete(&list_dnodes
);
2195 yang_dnode_free(dnode
);
2196 return NB_ERR_NOT_FOUND
;
2200 /* If a list entry was given, iterate over that list entry only. */
2201 if (dnode
->schema
->nodetype
== LYS_LIST
&& lyd_child(dnode
))
2202 ret
= nb_oper_data_iter_children(
2203 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
2204 translator
, true, flags
, cb
, arg
);
2206 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
2207 &list_keys
, translator
, true,
2210 list_delete(&list_dnodes
);
2211 yang_dnode_free(dnode
);
2216 bool nb_operation_is_valid(enum nb_operation operation
,
2217 const struct lysc_node
*snode
)
2219 struct nb_node
*nb_node
= snode
->priv
;
2220 struct lysc_node_container
*scontainer
;
2221 struct lysc_node_leaf
*sleaf
;
2223 switch (operation
) {
2225 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2228 switch (snode
->nodetype
) {
2230 sleaf
= (struct lysc_node_leaf
*)snode
;
2231 if (sleaf
->type
->basetype
!= LY_TYPE_EMPTY
)
2235 scontainer
= (struct lysc_node_container
*)snode
;
2236 if (!CHECK_FLAG(scontainer
->flags
, LYS_PRESENCE
))
2247 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2250 switch (snode
->nodetype
) {
2252 sleaf
= (struct lysc_node_leaf
*)snode
;
2253 if (sleaf
->type
->basetype
== LY_TYPE_EMPTY
)
2256 /* List keys can't be modified. */
2257 if (lysc_is_key(sleaf
))
2265 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2268 switch (snode
->nodetype
) {
2270 sleaf
= (struct lysc_node_leaf
*)snode
;
2272 /* List keys can't be deleted. */
2273 if (lysc_is_key(sleaf
))
2277 * Only optional leafs can be deleted, or leafs whose
2278 * parent is a case statement.
2280 if (snode
->parent
->nodetype
== LYS_CASE
)
2284 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
2289 scontainer
= (struct lysc_node_container
*)snode
;
2290 if (!CHECK_FLAG(scontainer
->flags
, LYS_PRESENCE
))
2301 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2304 switch (snode
->nodetype
) {
2307 if (!CHECK_FLAG(snode
->flags
, LYS_ORDBY_USER
))
2314 case NB_OP_PRE_VALIDATE
:
2315 case NB_OP_APPLY_FINISH
:
2316 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2319 case NB_OP_GET_ELEM
:
2320 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
2323 switch (snode
->nodetype
) {
2328 scontainer
= (struct lysc_node_container
*)snode
;
2329 if (!CHECK_FLAG(scontainer
->flags
, LYS_PRESENCE
))
2336 case NB_OP_GET_NEXT
:
2337 switch (snode
->nodetype
) {
2339 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
2343 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
2350 case NB_OP_GET_KEYS
:
2351 case NB_OP_LOOKUP_ENTRY
:
2352 switch (snode
->nodetype
) {
2354 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
2356 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
2364 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
2367 switch (snode
->nodetype
) {
2380 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
2381 (xpath
, arguments
));
2383 int nb_notification_send(const char *xpath
, struct list
*arguments
)
2387 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
2389 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
2391 list_delete(&arguments
);
2396 /* Running configuration user pointers management. */
2397 struct nb_config_entry
{
2398 char xpath
[XPATH_MAXLEN
];
2402 static bool running_config_entry_cmp(const void *value1
, const void *value2
)
2404 const struct nb_config_entry
*c1
= value1
;
2405 const struct nb_config_entry
*c2
= value2
;
2407 return strmatch(c1
->xpath
, c2
->xpath
);
2410 static unsigned int running_config_entry_key_make(const void *value
)
2412 return string_hash_make(value
);
2415 static void *running_config_entry_alloc(void *p
)
2417 struct nb_config_entry
*new, *key
= p
;
2419 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY
, sizeof(*new));
2420 strlcpy(new->xpath
, key
->xpath
, sizeof(new->xpath
));
2425 static void running_config_entry_free(void *arg
)
2427 XFREE(MTYPE_NB_CONFIG_ENTRY
, arg
);
2430 void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
)
2432 struct nb_config_entry
*config
, s
;
2434 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
2435 config
= hash_get(running_config_entries
, &s
,
2436 running_config_entry_alloc
);
2437 config
->entry
= entry
;
2440 void nb_running_move_tree(const char *xpath_from
, const char *xpath_to
)
2442 struct nb_config_entry
*entry
;
2443 struct list
*entries
= hash_to_list(running_config_entries
);
2444 struct listnode
*ln
;
2446 for (ALL_LIST_ELEMENTS_RO(entries
, ln
, entry
)) {
2447 if (!frrstr_startswith(entry
->xpath
, xpath_from
))
2450 hash_release(running_config_entries
, entry
);
2453 frrstr_replace(entry
->xpath
, xpath_from
, xpath_to
);
2454 strlcpy(entry
->xpath
, newpath
, sizeof(entry
->xpath
));
2455 XFREE(MTYPE_TMP
, newpath
);
2457 (void)hash_get(running_config_entries
, entry
,
2461 list_delete(&entries
);
2464 static void *nb_running_unset_entry_helper(const struct lyd_node
*dnode
)
2466 struct nb_config_entry
*config
, s
;
2467 struct lyd_node
*child
;
2470 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
2471 config
= hash_release(running_config_entries
, &s
);
2473 entry
= config
->entry
;
2474 running_config_entry_free(config
);
2477 /* Unset user pointers from the child nodes. */
2478 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
)) {
2479 LY_LIST_FOR (lyd_child(dnode
), child
) {
2480 (void)nb_running_unset_entry_helper(child
);
2487 void *nb_running_unset_entry(const struct lyd_node
*dnode
)
2491 entry
= nb_running_unset_entry_helper(dnode
);
2497 static void *nb_running_get_entry_worker(const struct lyd_node
*dnode
,
2499 bool abort_if_not_found
,
2502 const struct lyd_node
*orig_dnode
= dnode
;
2503 char xpath_buf
[XPATH_MAXLEN
];
2504 bool rec_flag
= true;
2506 assert(dnode
|| xpath
);
2509 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
2511 while (rec_flag
&& dnode
) {
2512 struct nb_config_entry
*config
, s
;
2514 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
2515 config
= hash_lookup(running_config_entries
, &s
);
2517 return config
->entry
;
2519 rec_flag
= rec_search
;
2521 dnode
= lyd_parent(dnode
);
2524 if (!abort_if_not_found
)
2527 yang_dnode_get_path(orig_dnode
, xpath_buf
, sizeof(xpath_buf
));
2528 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
2529 "%s: failed to find entry [xpath %s]", __func__
, xpath_buf
);
2530 zlog_backtrace(LOG_ERR
);
2534 void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
2535 bool abort_if_not_found
)
2537 return nb_running_get_entry_worker(dnode
, xpath
, abort_if_not_found
,
2541 void *nb_running_get_entry_non_rec(const struct lyd_node
*dnode
,
2542 const char *xpath
, bool abort_if_not_found
)
2544 return nb_running_get_entry_worker(dnode
, xpath
, abort_if_not_found
,
2548 /* Logging functions. */
2549 const char *nb_event_name(enum nb_event event
)
2552 case NB_EV_VALIDATE
:
2562 assert(!"Reached end of function we should never hit");
2565 const char *nb_operation_name(enum nb_operation operation
)
2567 switch (operation
) {
2576 case NB_OP_PRE_VALIDATE
:
2577 return "pre_validate";
2578 case NB_OP_APPLY_FINISH
:
2579 return "apply_finish";
2580 case NB_OP_GET_ELEM
:
2582 case NB_OP_GET_NEXT
:
2584 case NB_OP_GET_KEYS
:
2586 case NB_OP_LOOKUP_ENTRY
:
2587 return "lookup_entry";
2592 assert(!"Reached end of function we should never hit");
2595 const char *nb_err_name(enum nb_error error
)
2601 return "generic error";
2602 case NB_ERR_NO_CHANGES
:
2603 return "no changes";
2604 case NB_ERR_NOT_FOUND
:
2605 return "element not found";
2607 return "resource is locked";
2608 case NB_ERR_VALIDATION
:
2609 return "validation";
2610 case NB_ERR_RESOURCE
:
2611 return "failed to allocate resource";
2612 case NB_ERR_INCONSISTENCY
:
2613 return "internal inconsistency";
2616 assert(!"Reached end of function we should never hit");
2619 const char *nb_client_name(enum nb_client client
)
2624 case NB_CLIENT_CONFD
:
2626 case NB_CLIENT_SYSREPO
:
2628 case NB_CLIENT_GRPC
:
2630 case NB_CLIENT_PCEP
:
2632 case NB_CLIENT_MGMTD_SERVER
:
2633 return "MGMTD Server";
2634 case NB_CLIENT_MGMTD_BE
:
2635 return "MGMT Backend";
2636 case NB_CLIENT_NONE
:
2640 assert(!"Reached end of function we should never hit");
2643 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
2646 if (module
->ignore_cbs
)
2649 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
2650 struct nb_node
*nb_node
;
2653 if (i
> YANG_MODULE_MAX_NODES
) {
2655 "%s: %s.yang has more than %u nodes. Please increase YANG_MODULE_MAX_NODES to fix this problem.",
2656 __func__
, module
->name
, YANG_MODULE_MAX_NODES
);
2660 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
2662 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
2663 "%s: unknown data path: %s", __func__
,
2664 module
->nodes
[i
].xpath
);
2668 nb_node
->cbs
= module
->nodes
[i
].cbs
;
2669 priority
= module
->nodes
[i
].priority
;
2671 nb_node
->priority
= priority
;
2675 void nb_validate_callbacks(void)
2677 unsigned int errors
= 0;
2679 yang_snodes_iterate(NULL
, nb_node_validate
, 0, &errors
);
2682 EC_LIB_NB_CBS_VALIDATION
,
2683 "%s: failed to validate northbound callbacks: %u error(s)",
2690 void nb_init(struct thread_master
*tm
,
2691 const struct frr_yang_module_info
*const modules
[],
2692 size_t nmodules
, bool db_enabled
)
2694 struct yang_module
*loaded
[nmodules
], **loadedp
= loaded
;
2695 bool explicit_compile
;
2698 * Currently using this explicit compile feature in libyang2 leads to
2699 * incorrect behavior in FRR. The functionality suppresses the compiling
2700 * of modules until they have all been loaded into the context. This
2701 * avoids multiple recompiles of the same modules as they are
2702 * imported/augmented etc.
2704 explicit_compile
= false;
2706 nb_db_enabled
= db_enabled
;
2708 yang_init(true, explicit_compile
);
2710 /* Load YANG modules and their corresponding northbound callbacks. */
2711 for (size_t i
= 0; i
< nmodules
; i
++) {
2712 DEBUGD(&nb_dbg_events
, "northbound: loading %s.yang",
2714 *loadedp
++ = yang_module_load(modules
[i
]->name
);
2717 if (explicit_compile
)
2718 yang_init_loading_complete();
2720 /* Initialize the compiled nodes with northbound data */
2721 for (size_t i
= 0; i
< nmodules
; i
++) {
2722 yang_snodes_iterate(loaded
[i
]->info
, nb_node_new_cb
, 0,
2723 (void *)modules
[i
]);
2724 nb_load_callbacks(modules
[i
]);
2727 /* Validate northbound callbacks. */
2728 nb_validate_callbacks();
2730 /* Create an empty running configuration. */
2731 running_config
= nb_config_new(NULL
);
2732 running_config_entries
= hash_create(running_config_entry_key_make
,
2733 running_config_entry_cmp
,
2734 "Running Configuration Entries");
2735 pthread_mutex_init(&running_config_mgmt_lock
.mtx
, NULL
);
2737 /* Initialize the northbound CLI. */
2741 void nb_terminate(void)
2743 /* Terminate the northbound CLI. */
2746 /* Delete all nb_node's from all YANG modules. */
2749 /* Delete the running configuration. */
2750 hash_clean(running_config_entries
, running_config_entry_free
);
2751 hash_free(running_config_entries
);
2752 nb_config_free(running_config
);
2753 pthread_mutex_destroy(&running_config_mgmt_lock
.mtx
);