2 * Copyright (C) 2018 NetDEF, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "lib_errors.h"
28 #include "northbound.h"
29 #include "northbound_cli.h"
30 #include "northbound_db.h"
32 DEFINE_MTYPE_STATIC(LIB
, NB_NODE
, "Northbound Node")
33 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG
, "Northbound Configuration")
35 /* Running configuration - shouldn't be modified directly. */
36 struct nb_config
*running_config
;
39 * Global lock used to prevent multiple configuration transactions from
40 * happening concurrently.
42 static bool transaction_in_progress
;
44 static int nb_callback_configuration(const enum nb_event event
,
45 struct nb_config_change
*change
);
46 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
47 struct nb_config_cbs
*changes
,
48 enum nb_client client
,
50 static void nb_transaction_free(struct nb_transaction
*transaction
);
51 static int nb_transaction_process(enum nb_event event
,
52 struct nb_transaction
*transaction
);
53 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
);
54 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
55 const char *xpath
, const void *list_entry
,
56 const struct yang_list_keys
*list_keys
,
57 struct yang_translator
*translator
,
58 bool first
, uint32_t flags
,
59 nb_oper_data_cb cb
, void *arg
);
61 static int nb_node_check_config_only(const struct lys_node
*snode
, void *arg
)
63 bool *config_only
= arg
;
65 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
67 return YANG_ITER_STOP
;
70 return YANG_ITER_CONTINUE
;
73 static int nb_node_new_cb(const struct lys_node
*snode
, void *arg
)
75 struct nb_node
*nb_node
;
76 struct lys_node
*sparent
, *sparent_list
;
78 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
79 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
80 sizeof(nb_node
->xpath
));
81 nb_node
->priority
= NB_DFLT_PRIORITY
;
82 sparent
= yang_snode_real_parent(snode
);
84 nb_node
->parent
= sparent
->priv
;
85 sparent_list
= yang_snode_parent_list(snode
);
87 nb_node
->parent_list
= sparent_list
->priv
;
90 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
91 bool config_only
= true;
93 yang_snodes_iterate_subtree(snode
, nb_node_check_config_only
,
94 YANG_ITER_ALLOW_AUGMENTATIONS
,
97 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
99 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
100 struct lys_node_list
*slist
;
102 slist
= (struct lys_node_list
*)snode
;
103 if (slist
->keys_size
== 0)
104 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
108 * Link the northbound node and the libyang schema node with one
111 nb_node
->snode
= snode
;
112 lys_set_private(snode
, nb_node
);
114 return YANG_ITER_CONTINUE
;
117 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
119 struct nb_node
*nb_node
;
121 nb_node
= snode
->priv
;
122 lys_set_private(snode
, NULL
);
123 XFREE(MTYPE_NB_NODE
, nb_node
);
125 return YANG_ITER_CONTINUE
;
128 void nb_nodes_create(void)
130 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
133 void nb_nodes_delete(void)
135 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
138 struct nb_node
*nb_node_find(const char *xpath
)
140 const struct lys_node
*snode
;
143 * Use libyang to find the schema node associated to the xpath and get
144 * the northbound node from there (snode private pointer).
146 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
153 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
154 enum nb_operation operation
,
155 int callback_implemented
, bool optional
)
159 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
161 if (!valid
&& callback_implemented
)
162 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
163 "unneeded '%s' callback for '%s'",
164 nb_operation_name(operation
), nb_node
->xpath
);
166 if (!optional
&& valid
&& !callback_implemented
) {
167 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
168 nb_operation_name(operation
), nb_node
->xpath
);
176 * Check if the required callbacks were implemented for the given northbound
179 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
182 unsigned int error
= 0;
184 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
185 !!nb_node
->cbs
.create
, false);
186 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
187 !!nb_node
->cbs
.modify
, false);
188 error
+= nb_node_validate_cb(nb_node
, NB_OP_DESTROY
,
189 !!nb_node
->cbs
.destroy
, false);
190 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
192 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
193 !!nb_node
->cbs
.apply_finish
, true);
194 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
195 !!nb_node
->cbs
.get_elem
, false);
196 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
197 !!nb_node
->cbs
.get_next
, false);
198 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
199 !!nb_node
->cbs
.get_keys
, false);
200 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
201 !!nb_node
->cbs
.lookup_entry
, false);
202 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
208 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
210 /* Top-level nodes can have any priority. */
211 if (!nb_node
->parent
)
214 if (nb_node
->priority
< nb_node
->parent
->priority
) {
215 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
216 "node has higher priority than its parent [xpath %s]",
224 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
226 struct nb_node
*nb_node
= snode
->priv
;
227 unsigned int *errors
= arg
;
229 /* Validate callbacks and priority. */
230 *errors
+= nb_node_validate_cbs(nb_node
);
231 *errors
+= nb_node_validate_priority(nb_node
);
233 return YANG_ITER_CONTINUE
;
236 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
238 struct nb_config
*config
;
240 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
242 config
->dnode
= dnode
;
244 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
250 void nb_config_free(struct nb_config
*config
)
253 yang_dnode_free(config
->dnode
);
254 XFREE(MTYPE_NB_CONFIG
, config
);
257 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
259 struct nb_config
*dup
;
261 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
262 dup
->dnode
= yang_dnode_dup(config
->dnode
);
263 dup
->version
= config
->version
;
268 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
269 bool preserve_source
)
273 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
275 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
277 if (!preserve_source
)
278 nb_config_free(config_src
);
280 return (ret
== 0) ? NB_OK
: NB_ERR
;
283 void nb_config_replace(struct nb_config
*config_dst
,
284 struct nb_config
*config_src
, bool preserve_source
)
286 /* Update version. */
287 if (config_src
->version
!= 0)
288 config_dst
->version
= config_src
->version
;
291 if (config_dst
->dnode
)
292 yang_dnode_free(config_dst
->dnode
);
293 if (preserve_source
) {
294 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
296 config_dst
->dnode
= config_src
->dnode
;
297 config_src
->dnode
= NULL
;
298 nb_config_free(config_src
);
302 /* Generate the nb_config_cbs tree. */
303 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
304 const struct nb_config_cb
*b
)
306 /* Sort by priority first. */
307 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
309 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
313 * Use XPath as a tie-breaker. This will naturally sort parent nodes
314 * before their children.
316 return strcmp(a
->xpath
, b
->xpath
);
318 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
320 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
321 enum nb_operation operation
,
322 const struct lyd_node
*dnode
)
324 struct nb_config_change
*change
;
326 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
327 change
->cb
.operation
= operation
;
328 change
->cb
.nb_node
= dnode
->schema
->priv
;
329 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
330 change
->cb
.dnode
= dnode
;
332 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
335 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
337 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
338 struct nb_config_change
*change
;
340 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
342 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
343 XFREE(MTYPE_TMP
, change
);
348 * Helper function used when calculating the delta between two different
349 * configurations. Given a new subtree, calculate all new YANG data nodes,
350 * excluding default leafs and leaf-lists. This is a recursive function.
352 static void nb_config_diff_created(const struct lyd_node
*dnode
,
353 struct nb_config_cbs
*changes
)
355 enum nb_operation operation
;
356 struct lyd_node
*child
;
358 switch (dnode
->schema
->nodetype
) {
361 if (lyd_wd_default((struct lyd_node_leaf_list
*)dnode
))
364 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
365 operation
= NB_OP_CREATE
;
366 else if (nb_operation_is_valid(NB_OP_MODIFY
, dnode
->schema
))
367 operation
= NB_OP_MODIFY
;
371 nb_config_diff_add_change(changes
, operation
, dnode
);
375 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
376 nb_config_diff_add_change(changes
, NB_OP_CREATE
, dnode
);
378 /* Process child nodes recursively. */
379 LY_TREE_FOR (dnode
->child
, child
) {
380 nb_config_diff_created(child
, changes
);
388 static void nb_config_diff_deleted(const struct lyd_node
*dnode
,
389 struct nb_config_cbs
*changes
)
391 if (nb_operation_is_valid(NB_OP_DESTROY
, dnode
->schema
))
392 nb_config_diff_add_change(changes
, NB_OP_DESTROY
, dnode
);
393 else if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_CONTAINER
)) {
394 struct lyd_node
*child
;
397 * Non-presence containers need special handling since they
398 * don't have "destroy" callbacks. In this case, what we need to
399 * do is to call the "destroy" callbacks of their child nodes
400 * when applicable (i.e. optional nodes).
402 LY_TREE_FOR (dnode
->child
, child
) {
403 nb_config_diff_deleted(child
, changes
);
408 /* Calculate the delta between two different configurations. */
409 static void nb_config_diff(const struct nb_config
*config1
,
410 const struct nb_config
*config2
,
411 struct nb_config_cbs
*changes
)
413 struct lyd_difflist
*diff
;
415 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
416 LYD_DIFFOPT_WITHDEFAULTS
);
419 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
421 struct lyd_node
*dnode
;
423 type
= diff
->type
[i
];
426 case LYD_DIFF_CREATED
:
427 dnode
= diff
->second
[i
];
428 nb_config_diff_created(dnode
, changes
);
430 case LYD_DIFF_DELETED
:
431 dnode
= diff
->first
[i
];
432 nb_config_diff_deleted(dnode
, changes
);
434 case LYD_DIFF_CHANGED
:
435 dnode
= diff
->second
[i
];
436 nb_config_diff_add_change(changes
, NB_OP_MODIFY
, dnode
);
438 case LYD_DIFF_MOVEDAFTER1
:
439 case LYD_DIFF_MOVEDAFTER2
:
448 int nb_candidate_edit(struct nb_config
*candidate
,
449 const struct nb_node
*nb_node
,
450 enum nb_operation operation
, const char *xpath
,
451 const struct yang_data
*previous
,
452 const struct yang_data
*data
)
454 struct lyd_node
*dnode
;
455 char xpath_edit
[XPATH_MAXLEN
];
457 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
458 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
459 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
462 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
468 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
469 xpath_edit
, (void *)data
->value
, 0,
470 LYD_PATH_OPT_UPDATE
);
471 if (!dnode
&& ly_errno
) {
472 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
478 * If a new node was created, call lyd_validate() only to create
479 * default child nodes.
482 lyd_schema_sort(dnode
, 0);
483 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
487 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
490 * Return a special error code so the caller can choose
491 * whether to ignore it or not.
493 return NB_ERR_NOT_FOUND
;
497 /* TODO: update configuration. */
500 flog_warn(EC_LIB_DEVELOPMENT
,
501 "%s: unknown operation (%u) [xpath %s]", __func__
,
502 operation
, xpath_edit
);
509 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
511 if (candidate
->version
< running_config
->version
)
517 int nb_candidate_update(struct nb_config
*candidate
)
519 struct nb_config
*updated_config
;
521 updated_config
= nb_config_dup(running_config
);
522 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
525 nb_config_replace(candidate
, updated_config
, false);
531 * The northbound configuration callbacks use the 'priv' pointer present in the
532 * libyang lyd_node structure to store pointers to FRR internal variables
533 * associated to YANG lists and presence containers. Before commiting a
534 * candidate configuration, we must restore the 'priv' pointers stored in the
535 * running configuration since they might be lost while editing the candidate.
537 static void nb_candidate_restore_priv_pointers(struct nb_config
*candidate
)
539 struct lyd_node
*root
, *next
, *dnode_iter
;
541 LY_TREE_FOR (running_config
->dnode
, root
) {
542 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
543 struct lyd_node
*dnode_candidate
;
544 char xpath
[XPATH_MAXLEN
];
546 if (!dnode_iter
->priv
)
549 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
551 yang_dnode_get(candidate
->dnode
, xpath
);
553 yang_dnode_set_entry(dnode_candidate
,
557 LY_TREE_DFS_END(root
, next
, dnode_iter
);
563 * Perform YANG syntactic and semantic validation.
565 * WARNING: lyd_validate() can change the configuration as part of the
566 * validation process.
568 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
570 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
573 return NB_ERR_VALIDATION
;
578 /* Perform code-level validation using the northbound callbacks. */
579 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
580 struct nb_config_cbs
*changes
)
582 struct nb_config_cb
*cb
;
584 nb_candidate_restore_priv_pointers(candidate
);
585 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
586 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
589 ret
= nb_callback_configuration(NB_EV_VALIDATE
, change
);
591 return NB_ERR_VALIDATION
;
597 int nb_candidate_validate(struct nb_config
*candidate
)
599 struct nb_config_cbs changes
;
602 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
603 return NB_ERR_VALIDATION
;
605 RB_INIT(nb_config_cbs
, &changes
);
606 nb_config_diff(running_config
, candidate
, &changes
);
607 ret
= nb_candidate_validate_changes(candidate
, &changes
);
608 nb_config_diff_del_changes(&changes
);
613 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
614 enum nb_client client
, const char *comment
,
615 struct nb_transaction
**transaction
)
617 struct nb_config_cbs changes
;
619 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
620 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
621 "%s: failed to validate candidate configuration",
623 return NB_ERR_VALIDATION
;
626 RB_INIT(nb_config_cbs
, &changes
);
627 nb_config_diff(running_config
, candidate
, &changes
);
628 if (RB_EMPTY(nb_config_cbs
, &changes
))
629 return NB_ERR_NO_CHANGES
;
631 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
632 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
633 "%s: failed to validate candidate configuration",
635 nb_config_diff_del_changes(&changes
);
636 return NB_ERR_VALIDATION
;
639 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
640 if (*transaction
== NULL
) {
641 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
642 "%s: failed to create transaction", __func__
);
643 nb_config_diff_del_changes(&changes
);
644 return NB_ERR_LOCKED
;
647 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
650 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
652 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
653 nb_transaction_free(transaction
);
656 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
657 bool save_transaction
, uint32_t *transaction_id
)
659 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
660 nb_transaction_apply_finish(transaction
);
662 /* Replace running by candidate. */
663 transaction
->config
->version
++;
664 nb_config_replace(running_config
, transaction
->config
, true);
666 /* Record transaction. */
668 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
669 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
670 "%s: failed to record transaction", __func__
);
672 nb_transaction_free(transaction
);
675 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
676 bool save_transaction
, const char *comment
,
677 uint32_t *transaction_id
)
679 struct nb_transaction
*transaction
= NULL
;
682 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
685 * Apply the changes if the preparation phase succeeded. Otherwise abort
689 nb_candidate_commit_apply(transaction
, save_transaction
,
691 else if (transaction
!= NULL
)
692 nb_candidate_commit_abort(transaction
);
697 static void nb_log_callback(const enum nb_event event
,
698 enum nb_operation operation
, const char *xpath
,
702 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
703 nb_event_name(event
), nb_operation_name(operation
), xpath
,
708 * Call the northbound configuration callback associated to a given
709 * configuration change.
711 static int nb_callback_configuration(const enum nb_event event
,
712 struct nb_config_change
*change
)
714 enum nb_operation operation
= change
->cb
.operation
;
715 const char *xpath
= change
->cb
.xpath
;
716 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
717 const struct lyd_node
*dnode
= change
->cb
.dnode
;
718 union nb_resource
*resource
;
721 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
722 const char *value
= "(none)";
724 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
725 value
= yang_dnode_get_string(dnode
, NULL
);
727 nb_log_callback(event
, operation
, xpath
, value
);
730 if (event
== NB_EV_VALIDATE
)
733 resource
= &change
->resource
;
737 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
740 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
743 ret
= (*nb_node
->cbs
.destroy
)(event
, dnode
);
746 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
749 flog_err(EC_LIB_DEVELOPMENT
,
750 "%s: unknown operation (%u) [xpath %s]", __func__
,
757 enum lib_log_refs ref
;
761 priority
= LOG_WARNING
;
762 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
765 priority
= LOG_WARNING
;
766 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
769 priority
= LOG_WARNING
;
770 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
774 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
777 flog_err(EC_LIB_DEVELOPMENT
,
778 "%s: unknown event (%u) [xpath %s]",
779 __func__
, event
, xpath
);
784 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
785 __func__
, nb_err_name(ret
), nb_event_name(event
),
786 nb_operation_name(operation
), xpath
);
792 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
794 const void *list_entry
)
796 DEBUGD(&nb_dbg_cbs_state
,
797 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
800 return nb_node
->cbs
.get_elem(xpath
, list_entry
);
803 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
804 const void *parent_list_entry
,
805 const void *list_entry
)
807 DEBUGD(&nb_dbg_cbs_state
,
808 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
809 nb_node
->xpath
, parent_list_entry
, list_entry
);
811 return nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
814 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
815 struct yang_list_keys
*keys
)
817 DEBUGD(&nb_dbg_cbs_state
,
818 "northbound callback (get_keys): node [%s] list_entry [%p]",
819 nb_node
->xpath
, list_entry
);
821 return nb_node
->cbs
.get_keys(list_entry
, keys
);
824 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
825 const void *parent_list_entry
,
826 const struct yang_list_keys
*keys
)
828 DEBUGD(&nb_dbg_cbs_state
,
829 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
830 nb_node
->xpath
, parent_list_entry
);
832 return nb_node
->cbs
.lookup_entry(parent_list_entry
, keys
);
835 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
836 const struct list
*input
, struct list
*output
)
838 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
840 return nb_node
->cbs
.rpc(xpath
, input
, output
);
843 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
844 struct nb_config_cbs
*changes
,
845 enum nb_client client
,
848 struct nb_transaction
*transaction
;
850 if (transaction_in_progress
) {
852 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
853 "%s: error - there's already another transaction in progress",
857 transaction_in_progress
= true;
859 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
860 transaction
->client
= client
;
862 strlcpy(transaction
->comment
, comment
,
863 sizeof(transaction
->comment
));
864 transaction
->config
= config
;
865 transaction
->changes
= *changes
;
870 static void nb_transaction_free(struct nb_transaction
*transaction
)
872 nb_config_diff_del_changes(&transaction
->changes
);
873 XFREE(MTYPE_TMP
, transaction
);
874 transaction_in_progress
= false;
877 /* Process all configuration changes associated to a transaction. */
878 static int nb_transaction_process(enum nb_event event
,
879 struct nb_transaction
*transaction
)
881 struct nb_config_cb
*cb
;
883 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
884 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
888 * Only try to release resources that were allocated
891 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
894 /* Call the appropriate callback. */
895 ret
= nb_callback_configuration(event
, change
);
900 change
->prepare_ok
= true;
905 * At this point it's not possible to reject the
906 * transaction anymore, so any failure here can lead to
907 * inconsistencies and should be treated as a bug.
908 * Operations prone to errors, like validations and
909 * resource allocations, should be performed during the
921 static struct nb_config_cb
*
922 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
923 const struct nb_node
*nb_node
,
924 const struct lyd_node
*dnode
)
926 struct nb_config_cb
*cb
;
928 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
929 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
930 cb
->nb_node
= nb_node
;
932 RB_INSERT(nb_config_cbs
, cbs
, cb
);
937 static struct nb_config_cb
*
938 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
939 const struct nb_node
*nb_node
)
941 struct nb_config_cb s
;
943 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
945 return RB_FIND(nb_config_cbs
, cbs
, &s
);
948 /* Call the 'apply_finish' callbacks. */
949 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
951 struct nb_config_cbs cbs
;
952 struct nb_config_cb
*cb
;
954 /* Initialize tree of 'apply_finish' callbacks. */
955 RB_INIT(nb_config_cbs
, &cbs
);
957 /* Identify the 'apply_finish' callbacks that need to be called. */
958 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
959 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
960 const struct lyd_node
*dnode
= change
->cb
.dnode
;
963 * Iterate up to the root of the data tree. When a node is being
964 * deleted, skip its 'apply_finish' callback if one is defined
965 * (the 'apply_finish' callbacks from the node ancestors should
968 if (change
->cb
.operation
== NB_OP_DESTROY
) {
969 char xpath
[XPATH_MAXLEN
];
971 dnode
= dnode
->parent
;
976 * The dnode from 'delete' callbacks point to elements
977 * from the running configuration. Use yang_dnode_get()
978 * to get the corresponding dnode from the candidate
979 * configuration that is being committed.
981 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
982 dnode
= yang_dnode_get(transaction
->config
->dnode
,
986 char xpath
[XPATH_MAXLEN
];
987 struct nb_node
*nb_node
;
989 nb_node
= dnode
->schema
->priv
;
990 if (!nb_node
->cbs
.apply_finish
)
994 * Don't call the callback more than once for the same
997 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
998 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
1001 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
1004 dnode
= dnode
->parent
;
1008 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1009 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
1010 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
))
1011 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
1014 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
1017 /* Release memory. */
1018 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
1019 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
1020 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
1021 XFREE(MTYPE_TMP
, cb
);
1025 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
1026 const char *xpath
, const void *list_entry
,
1027 const struct yang_list_keys
*list_keys
,
1028 struct yang_translator
*translator
,
1029 bool first
, uint32_t flags
,
1030 nb_oper_data_cb cb
, void *arg
)
1032 struct lys_node
*child
;
1034 LY_TREE_FOR (snode
->child
, child
) {
1037 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1038 list_keys
, translator
, false,
1047 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1048 const char *xpath
, const void *list_entry
,
1049 const struct yang_list_keys
*list_keys
,
1050 struct yang_translator
*translator
,
1051 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1053 struct yang_data
*data
;
1055 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1058 /* Ignore list keys. */
1059 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1062 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1064 /* Leaf of type "empty" is not present. */
1067 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1070 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1072 const void *list_entry
,
1073 const struct yang_list_keys
*list_keys
,
1074 struct yang_translator
*translator
,
1075 uint32_t flags
, nb_oper_data_cb cb
,
1078 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1081 /* Presence containers. */
1082 if (nb_node
->cbs
.get_elem
) {
1083 struct yang_data
*data
;
1086 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1088 /* Presence container is not present. */
1091 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1096 /* Iterate over the child nodes. */
1097 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1098 list_keys
, translator
, false, flags
,
1103 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1104 const void *parent_list_entry
,
1105 const struct yang_list_keys
*parent_list_keys
,
1106 struct yang_translator
*translator
, uint32_t flags
,
1107 nb_oper_data_cb cb
, void *arg
)
1109 const void *list_entry
= NULL
;
1111 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1115 struct yang_data
*data
;
1118 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1121 /* End of the list. */
1124 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1128 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1131 } while (list_entry
);
1136 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1137 const char *xpath_list
,
1138 const void *parent_list_entry
,
1139 const struct yang_list_keys
*parent_list_keys
,
1140 struct yang_translator
*translator
,
1141 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1143 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1144 const void *list_entry
= NULL
;
1145 uint32_t position
= 1;
1147 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1150 /* Iterate over all list entries. */
1152 struct yang_list_keys list_keys
;
1153 char xpath
[XPATH_MAXLEN
* 2];
1156 /* Obtain list entry. */
1157 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1160 /* End of the list. */
1163 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1164 /* Obtain the list entry keys. */
1165 if (nb_callback_get_keys(nb_node
, list_entry
,
1168 flog_warn(EC_LIB_NB_CB_STATE
,
1169 "%s: failed to get list keys",
1174 /* Build XPath of the list entry. */
1175 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1176 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1177 snprintf(xpath
+ strlen(xpath
),
1178 sizeof(xpath
) - strlen(xpath
),
1179 "[%s='%s']", slist
->keys
[i
]->name
,
1184 * Keyless list - build XPath using a positional index.
1186 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1191 /* Iterate over the child nodes. */
1192 ret
= nb_oper_data_iter_children(
1193 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1194 translator
, false, flags
, cb
, arg
);
1197 } while (list_entry
);
1202 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1203 const char *xpath_parent
,
1204 const void *list_entry
,
1205 const struct yang_list_keys
*list_keys
,
1206 struct yang_translator
*translator
,
1207 bool first
, uint32_t flags
,
1208 nb_oper_data_cb cb
, void *arg
)
1210 struct nb_node
*nb_node
;
1211 char xpath
[XPATH_MAXLEN
];
1214 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1215 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1219 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1220 if (!first
&& snode
->nodetype
!= LYS_USES
)
1221 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1222 "/%s", snode
->name
);
1224 nb_node
= snode
->priv
;
1225 switch (snode
->nodetype
) {
1227 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1228 list_keys
, translator
, flags
,
1232 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1233 list_keys
, translator
, flags
, cb
,
1237 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1238 list_keys
, translator
, flags
,
1242 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1243 list_keys
, translator
, flags
, cb
,
1247 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1248 list_keys
, translator
, false,
1258 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1259 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1261 struct nb_node
*nb_node
;
1262 const void *list_entry
= NULL
;
1263 struct yang_list_keys list_keys
;
1264 struct list
*list_dnodes
;
1265 struct lyd_node
*dnode
, *dn
;
1266 struct listnode
*ln
;
1269 nb_node
= nb_node_find(xpath
);
1271 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1272 "%s: unknown data path: %s", __func__
, xpath
);
1276 /* For now this function works only with containers and lists. */
1277 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1279 EC_LIB_NB_OPERATIONAL_DATA
,
1280 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1286 * Create a data tree from the XPath so that we can parse the keys of
1287 * all YANG lists (if any).
1290 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1291 LYD_PATH_OPT_UPDATE
);
1292 if (!dnode
&& ly_errno
) {
1293 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1298 * We can remove the following two lines once we depend on
1299 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1302 dnode
= yang_dnode_get(dnode
, xpath
);
1306 * Create a linked list to sort the data nodes starting from the root.
1308 list_dnodes
= list_new();
1309 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1310 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1312 listnode_add_head(list_dnodes
, dn
);
1315 * Use the northbound callbacks to find list entry pointer corresponding
1316 * to the given XPath.
1318 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1319 struct lyd_node
*child
;
1323 /* Obtain the list entry keys. */
1324 memset(&list_keys
, 0, sizeof(list_keys
));
1325 LY_TREE_FOR (dn
->child
, child
) {
1326 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1329 strlcpy(list_keys
.key
[n
],
1330 yang_dnode_get_string(child
, NULL
),
1331 sizeof(list_keys
.key
[n
]));
1336 != ((struct lys_node_list
*)dn
->schema
)->keys_size
) {
1337 list_delete(&list_dnodes
);
1338 yang_dnode_free(dnode
);
1339 return NB_ERR_NOT_FOUND
;
1342 /* Find the list entry pointer. */
1343 nn
= dn
->schema
->priv
;
1345 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
1346 if (list_entry
== NULL
) {
1347 list_delete(&list_dnodes
);
1348 yang_dnode_free(dnode
);
1349 return NB_ERR_NOT_FOUND
;
1353 /* If a list entry was given, iterate over that list entry only. */
1354 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1355 ret
= nb_oper_data_iter_children(
1356 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1357 translator
, true, flags
, cb
, arg
);
1359 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1360 &list_keys
, translator
, true,
1363 list_delete(&list_dnodes
);
1364 yang_dnode_free(dnode
);
1369 bool nb_operation_is_valid(enum nb_operation operation
,
1370 const struct lys_node
*snode
)
1372 struct nb_node
*nb_node
= snode
->priv
;
1373 struct lys_node_container
*scontainer
;
1374 struct lys_node_leaf
*sleaf
;
1376 switch (operation
) {
1378 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1381 switch (snode
->nodetype
) {
1383 sleaf
= (struct lys_node_leaf
*)snode
;
1384 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1388 scontainer
= (struct lys_node_container
*)snode
;
1389 if (!scontainer
->presence
)
1400 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1403 switch (snode
->nodetype
) {
1405 sleaf
= (struct lys_node_leaf
*)snode
;
1406 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1409 /* List keys can't be modified. */
1410 if (lys_is_key(sleaf
, NULL
))
1418 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1421 switch (snode
->nodetype
) {
1423 sleaf
= (struct lys_node_leaf
*)snode
;
1425 /* List keys can't be deleted. */
1426 if (lys_is_key(sleaf
, NULL
))
1430 * Only optional leafs can be deleted, or leafs whose
1431 * parent is a case statement.
1433 if (snode
->parent
->nodetype
== LYS_CASE
)
1437 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1442 scontainer
= (struct lys_node_container
*)snode
;
1443 if (!scontainer
->presence
)
1454 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1457 switch (snode
->nodetype
) {
1460 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1467 case NB_OP_APPLY_FINISH
:
1468 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1471 case NB_OP_GET_ELEM
:
1472 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1475 switch (snode
->nodetype
) {
1480 scontainer
= (struct lys_node_container
*)snode
;
1481 if (!scontainer
->presence
)
1488 case NB_OP_GET_NEXT
:
1489 switch (snode
->nodetype
) {
1491 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1495 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1502 case NB_OP_GET_KEYS
:
1503 case NB_OP_LOOKUP_ENTRY
:
1504 switch (snode
->nodetype
) {
1506 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1508 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1516 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1519 switch (snode
->nodetype
) {
1532 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1533 (xpath
, arguments
));
1535 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1539 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
1541 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1543 list_delete(&arguments
);
1548 const char *nb_event_name(enum nb_event event
)
1551 case NB_EV_VALIDATE
:
1564 const char *nb_operation_name(enum nb_operation operation
)
1566 switch (operation
) {
1575 case NB_OP_APPLY_FINISH
:
1576 return "apply_finish";
1577 case NB_OP_GET_ELEM
:
1579 case NB_OP_GET_NEXT
:
1581 case NB_OP_GET_KEYS
:
1583 case NB_OP_LOOKUP_ENTRY
:
1584 return "lookup_entry";
1592 const char *nb_err_name(enum nb_error error
)
1598 return "generic error";
1599 case NB_ERR_NO_CHANGES
:
1600 return "no changes";
1601 case NB_ERR_NOT_FOUND
:
1602 return "element not found";
1604 return "resource is locked";
1605 case NB_ERR_VALIDATION
:
1606 return "validation error";
1607 case NB_ERR_RESOURCE
:
1608 return "failed to allocate resource";
1609 case NB_ERR_INCONSISTENCY
:
1610 return "internal inconsistency";
1616 const char *nb_client_name(enum nb_client client
)
1621 case NB_CLIENT_CONFD
:
1623 case NB_CLIENT_SYSREPO
:
1630 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1632 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1633 struct nb_node
*nb_node
;
1636 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1638 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1639 "%s: unknown data path: %s", __func__
,
1640 module
->nodes
[i
].xpath
);
1644 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1645 priority
= module
->nodes
[i
].priority
;
1647 nb_node
->priority
= priority
;
1651 void nb_init(struct thread_master
*tm
,
1652 const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1654 unsigned int errors
= 0;
1656 /* Load YANG modules. */
1657 for (size_t i
= 0; i
< nmodules
; i
++)
1658 yang_module_load(modules
[i
]->name
);
1660 /* Create a nb_node for all YANG schema nodes. */
1663 /* Load northbound callbacks. */
1664 for (size_t i
= 0; i
< nmodules
; i
++)
1665 nb_load_callbacks(modules
[i
]);
1667 /* Validate northbound callbacks. */
1668 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1671 EC_LIB_NB_CBS_VALIDATION
,
1672 "%s: failed to validate northbound callbacks: %u error(s)",
1677 /* Create an empty running configuration. */
1678 running_config
= nb_config_new(NULL
);
1680 /* Initialize the northbound CLI. */
1684 void nb_terminate(void)
1686 /* Terminate the northbound CLI. */
1689 /* Delete all nb_node's from all YANG modules. */
1692 /* Delete the running configuration. */
1693 nb_config_free(running_config
);