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"
27 #include "northbound.h"
28 #include "northbound_cli.h"
29 #include "northbound_db.h"
31 DEFINE_MTYPE_STATIC(LIB
, NB_NODE
, "Northbound Node")
32 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG
, "Northbound Configuration")
34 /* Running configuration - shouldn't be modified directly. */
35 struct nb_config
*running_config
;
38 * Global lock used to prevent multiple configuration transactions from
39 * happening concurrently.
41 static bool transaction_in_progress
;
43 static int nb_configuration_callback(const enum nb_event event
,
44 struct nb_config_change
*change
);
45 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
46 struct nb_config_cbs
*changes
,
47 enum nb_client client
,
49 static void nb_transaction_free(struct nb_transaction
*transaction
);
50 static int nb_transaction_process(enum nb_event event
,
51 struct nb_transaction
*transaction
);
52 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
);
53 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
54 const char *xpath
, const void *list_entry
,
55 const struct yang_list_keys
*list_keys
,
56 struct yang_translator
*translator
,
57 bool first
, uint32_t flags
,
58 nb_oper_data_cb cb
, void *arg
);
60 static int nb_node_check_config_only(const struct lys_node
*snode
, void *arg
)
62 bool *config_only
= arg
;
64 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
66 return YANG_ITER_STOP
;
69 return YANG_ITER_CONTINUE
;
72 static int nb_node_new_cb(const struct lys_node
*snode
, void *arg
)
74 struct nb_node
*nb_node
;
75 struct lys_node
*sparent
, *sparent_list
;
77 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
78 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
79 sizeof(nb_node
->xpath
));
80 nb_node
->priority
= NB_DFLT_PRIORITY
;
81 sparent
= yang_snode_real_parent(snode
);
83 nb_node
->parent
= sparent
->priv
;
84 sparent_list
= yang_snode_parent_list(snode
);
86 nb_node
->parent_list
= sparent_list
->priv
;
89 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
90 bool config_only
= true;
92 yang_snodes_iterate_subtree(snode
, nb_node_check_config_only
,
93 YANG_ITER_ALLOW_AUGMENTATIONS
,
96 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
100 * Link the northbound node and the libyang schema node with one
103 nb_node
->snode
= snode
;
104 lys_set_private(snode
, nb_node
);
106 return YANG_ITER_CONTINUE
;
109 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
111 struct nb_node
*nb_node
;
113 nb_node
= snode
->priv
;
114 lys_set_private(snode
, NULL
);
115 XFREE(MTYPE_NB_NODE
, nb_node
);
117 return YANG_ITER_CONTINUE
;
120 void nb_nodes_create(void)
122 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
125 void nb_nodes_delete(void)
127 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
130 struct nb_node
*nb_node_find(const char *xpath
)
132 const struct lys_node
*snode
;
135 * Use libyang to find the schema node associated to the xpath and get
136 * the northbound node from there (snode private pointer).
138 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
145 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
146 enum nb_operation operation
,
147 int callback_implemented
, bool optional
)
151 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
153 if (!valid
&& callback_implemented
)
154 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
155 "unneeded '%s' callback for '%s'",
156 nb_operation_name(operation
), nb_node
->xpath
);
158 if (!optional
&& valid
&& !callback_implemented
) {
159 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
160 nb_operation_name(operation
), nb_node
->xpath
);
168 * Check if the required callbacks were implemented for the given northbound
171 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
174 unsigned int error
= 0;
176 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
177 !!nb_node
->cbs
.create
, false);
178 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
179 !!nb_node
->cbs
.modify
, false);
180 error
+= nb_node_validate_cb(nb_node
, NB_OP_DELETE
,
181 !!nb_node
->cbs
.delete, false);
182 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
184 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
185 !!nb_node
->cbs
.apply_finish
, true);
186 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
187 !!nb_node
->cbs
.get_elem
, false);
188 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
189 !!nb_node
->cbs
.get_next
, false);
190 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
191 !!nb_node
->cbs
.get_keys
, false);
192 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
193 !!nb_node
->cbs
.lookup_entry
, false);
194 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
200 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
202 /* Top-level nodes can have any priority. */
203 if (!nb_node
->parent
)
206 if (nb_node
->priority
< nb_node
->parent
->priority
) {
207 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
208 "node has higher priority than its parent [xpath %s]",
216 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
218 struct nb_node
*nb_node
= snode
->priv
;
219 unsigned int *errors
= arg
;
221 /* Validate callbacks and priority. */
222 *errors
+= nb_node_validate_cbs(nb_node
);
223 *errors
+= nb_node_validate_priority(nb_node
);
225 return YANG_ITER_CONTINUE
;
228 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
230 struct nb_config
*config
;
232 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
234 config
->dnode
= dnode
;
236 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
242 void nb_config_free(struct nb_config
*config
)
245 yang_dnode_free(config
->dnode
);
246 XFREE(MTYPE_NB_CONFIG
, config
);
249 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
251 struct nb_config
*dup
;
253 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
254 dup
->dnode
= yang_dnode_dup(config
->dnode
);
255 dup
->version
= config
->version
;
260 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
261 bool preserve_source
)
265 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
267 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
269 if (!preserve_source
)
270 nb_config_free(config_src
);
272 return (ret
== 0) ? NB_OK
: NB_ERR
;
275 void nb_config_replace(struct nb_config
*config_dst
,
276 struct nb_config
*config_src
, bool preserve_source
)
278 /* Update version. */
279 if (config_src
->version
!= 0)
280 config_dst
->version
= config_src
->version
;
283 if (config_dst
->dnode
)
284 yang_dnode_free(config_dst
->dnode
);
285 if (preserve_source
) {
286 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
288 config_dst
->dnode
= config_src
->dnode
;
289 config_src
->dnode
= NULL
;
290 nb_config_free(config_src
);
294 /* Generate the nb_config_cbs tree. */
295 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
296 const struct nb_config_cb
*b
)
298 /* Sort by priority first. */
299 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
301 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
305 * Use XPath as a tie-breaker. This will naturally sort parent nodes
306 * before their children.
308 return strcmp(a
->xpath
, b
->xpath
);
310 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
312 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
313 enum nb_operation operation
,
314 const struct lyd_node
*dnode
)
316 struct nb_config_change
*change
;
318 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
319 change
->cb
.operation
= operation
;
320 change
->cb
.nb_node
= dnode
->schema
->priv
;
321 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
322 change
->cb
.dnode
= dnode
;
324 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
327 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
329 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
330 struct nb_config_change
*change
;
332 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
334 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
335 XFREE(MTYPE_TMP
, change
);
340 * Helper function used when calculating the delta between two different
341 * configurations. Given a new subtree, calculate all new YANG data nodes,
342 * excluding default leafs and leaf-lists. This is a recursive function.
344 static void nb_config_diff_new_subtree(const struct lyd_node
*dnode
,
345 struct nb_config_cbs
*changes
)
347 struct lyd_node
*child
;
349 LY_TREE_FOR (dnode
->child
, child
) {
350 enum nb_operation operation
;
352 switch (child
->schema
->nodetype
) {
355 if (lyd_wd_default((struct lyd_node_leaf_list
*)child
))
358 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
359 operation
= NB_OP_CREATE
;
360 else if (nb_operation_is_valid(NB_OP_MODIFY
,
362 operation
= NB_OP_MODIFY
;
366 nb_config_diff_add_change(changes
, operation
, child
);
370 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
371 nb_config_diff_add_change(changes
, NB_OP_CREATE
,
373 nb_config_diff_new_subtree(child
, changes
);
381 /* Calculate the delta between two different configurations. */
382 static void nb_config_diff(const struct nb_config
*config1
,
383 const struct nb_config
*config2
,
384 struct nb_config_cbs
*changes
)
386 struct lyd_difflist
*diff
;
388 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
389 LYD_DIFFOPT_WITHDEFAULTS
);
392 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
394 struct lyd_node
*dnode
;
395 enum nb_operation operation
;
397 type
= diff
->type
[i
];
400 case LYD_DIFF_CREATED
:
401 dnode
= diff
->second
[i
];
403 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
404 operation
= NB_OP_CREATE
;
405 else if (nb_operation_is_valid(NB_OP_MODIFY
,
407 operation
= NB_OP_MODIFY
;
411 case LYD_DIFF_DELETED
:
412 dnode
= diff
->first
[i
];
413 operation
= NB_OP_DELETE
;
415 case LYD_DIFF_CHANGED
:
416 dnode
= diff
->second
[i
];
417 operation
= NB_OP_MODIFY
;
419 case LYD_DIFF_MOVEDAFTER1
:
420 case LYD_DIFF_MOVEDAFTER2
:
425 nb_config_diff_add_change(changes
, operation
, dnode
);
427 if (type
== LYD_DIFF_CREATED
428 && CHECK_FLAG(dnode
->schema
->nodetype
,
429 LYS_CONTAINER
| LYS_LIST
))
430 nb_config_diff_new_subtree(dnode
, changes
);
436 int nb_candidate_edit(struct nb_config
*candidate
,
437 const struct nb_node
*nb_node
,
438 enum nb_operation operation
, const char *xpath
,
439 const struct yang_data
*previous
,
440 const struct yang_data
*data
)
442 struct lyd_node
*dnode
;
443 char xpath_edit
[XPATH_MAXLEN
];
445 if (!nb_operation_is_valid(operation
, nb_node
->snode
)) {
446 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
447 "%s: %s operation not valid for %s", __func__
,
448 nb_operation_name(operation
), xpath
);
452 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
453 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
454 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
457 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
463 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
464 xpath_edit
, (void *)data
->value
, 0,
465 LYD_PATH_OPT_UPDATE
);
466 if (!dnode
&& ly_errno
) {
467 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
473 * If a new node was created, call lyd_validate() only to create
474 * default child nodes.
477 lyd_schema_sort(dnode
, 0);
478 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
482 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
485 * Return a special error code so the caller can choose
486 * whether to ignore it or not.
488 return NB_ERR_NOT_FOUND
;
492 /* TODO: update configuration. */
495 flog_warn(EC_LIB_DEVELOPMENT
,
496 "%s: unknown operation (%u) [xpath %s]", __func__
,
497 operation
, xpath_edit
);
504 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
506 if (candidate
->version
< running_config
->version
)
512 int nb_candidate_update(struct nb_config
*candidate
)
514 struct nb_config
*updated_config
;
516 updated_config
= nb_config_dup(running_config
);
517 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
520 nb_config_replace(candidate
, updated_config
, false);
526 * The northbound configuration callbacks use the 'priv' pointer present in the
527 * libyang lyd_node structure to store pointers to FRR internal variables
528 * associated to YANG lists and presence containers. Before commiting a
529 * candidate configuration, we must restore the 'priv' pointers stored in the
530 * running configuration since they might be lost while editing the candidate.
532 static void nb_candidate_restore_priv_pointers(struct nb_config
*candidate
)
534 struct lyd_node
*root
, *next
, *dnode_iter
;
536 LY_TREE_FOR (running_config
->dnode
, root
) {
537 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
538 struct lyd_node
*dnode_candidate
;
539 char xpath
[XPATH_MAXLEN
];
541 if (!dnode_iter
->priv
)
544 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
546 yang_dnode_get(candidate
->dnode
, xpath
);
548 yang_dnode_set_entry(dnode_candidate
,
552 LY_TREE_DFS_END(root
, next
, dnode_iter
);
558 * Perform YANG syntactic and semantic validation.
560 * WARNING: lyd_validate() can change the configuration as part of the
561 * validation process.
563 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
565 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
568 return NB_ERR_VALIDATION
;
573 /* Perform code-level validation using the northbound callbacks. */
574 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
575 struct nb_config_cbs
*changes
)
577 struct nb_config_cb
*cb
;
579 nb_candidate_restore_priv_pointers(candidate
);
580 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
581 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
584 ret
= nb_configuration_callback(NB_EV_VALIDATE
, change
);
586 return NB_ERR_VALIDATION
;
592 int nb_candidate_validate(struct nb_config
*candidate
)
594 struct nb_config_cbs changes
;
597 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
598 return NB_ERR_VALIDATION
;
600 RB_INIT(nb_config_cbs
, &changes
);
601 nb_config_diff(running_config
, candidate
, &changes
);
602 ret
= nb_candidate_validate_changes(candidate
, &changes
);
603 nb_config_diff_del_changes(&changes
);
608 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
609 enum nb_client client
, const char *comment
,
610 struct nb_transaction
**transaction
)
612 struct nb_config_cbs changes
;
614 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
615 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
616 "%s: failed to validate candidate configuration",
618 return NB_ERR_VALIDATION
;
621 RB_INIT(nb_config_cbs
, &changes
);
622 nb_config_diff(running_config
, candidate
, &changes
);
623 if (RB_EMPTY(nb_config_cbs
, &changes
))
624 return NB_ERR_NO_CHANGES
;
626 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
627 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
628 "%s: failed to validate candidate configuration",
630 nb_config_diff_del_changes(&changes
);
631 return NB_ERR_VALIDATION
;
634 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
635 if (*transaction
== NULL
) {
636 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
637 "%s: failed to create transaction", __func__
);
638 nb_config_diff_del_changes(&changes
);
639 return NB_ERR_LOCKED
;
642 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
645 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
647 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
648 nb_transaction_free(transaction
);
651 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
652 bool save_transaction
, uint32_t *transaction_id
)
654 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
655 nb_transaction_apply_finish(transaction
);
657 /* Replace running by candidate. */
658 transaction
->config
->version
++;
659 nb_config_replace(running_config
, transaction
->config
, true);
661 /* Record transaction. */
663 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
664 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
665 "%s: failed to record transaction", __func__
);
667 nb_transaction_free(transaction
);
670 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
671 bool save_transaction
, const char *comment
,
672 uint32_t *transaction_id
)
674 struct nb_transaction
*transaction
= NULL
;
677 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
680 * Apply the changes if the preparation phase succeeded. Otherwise abort
684 nb_candidate_commit_apply(transaction
, save_transaction
,
686 else if (transaction
!= NULL
)
687 nb_candidate_commit_abort(transaction
);
692 static void nb_log_callback(const enum nb_event event
,
693 enum nb_operation operation
, const char *xpath
,
697 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
698 nb_event_name(event
), nb_operation_name(operation
), xpath
,
703 * Call the northbound configuration callback associated to a given
704 * configuration change.
706 static int nb_configuration_callback(const enum nb_event event
,
707 struct nb_config_change
*change
)
709 enum nb_operation operation
= change
->cb
.operation
;
710 const char *xpath
= change
->cb
.xpath
;
711 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
712 const struct lyd_node
*dnode
= change
->cb
.dnode
;
713 union nb_resource
*resource
;
716 if (debug_northbound
) {
717 const char *value
= "(none)";
719 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
720 value
= yang_dnode_get_string(dnode
, NULL
);
722 nb_log_callback(event
, operation
, xpath
, value
);
725 if (event
== NB_EV_VALIDATE
)
728 resource
= &change
->resource
;
732 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
735 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
738 ret
= (*nb_node
->cbs
.delete)(event
, dnode
);
741 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
750 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
751 __func__
, nb_err_name(ret
), nb_event_name(event
),
752 nb_operation_name(operation
), xpath
);
757 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
758 struct nb_config_cbs
*changes
,
759 enum nb_client client
,
762 struct nb_transaction
*transaction
;
764 if (transaction_in_progress
) {
766 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
767 "%s: error - there's already another transaction in progress",
771 transaction_in_progress
= true;
773 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
774 transaction
->client
= client
;
776 strlcpy(transaction
->comment
, comment
,
777 sizeof(transaction
->comment
));
778 transaction
->config
= config
;
779 transaction
->changes
= *changes
;
784 static void nb_transaction_free(struct nb_transaction
*transaction
)
786 nb_config_diff_del_changes(&transaction
->changes
);
787 XFREE(MTYPE_TMP
, transaction
);
788 transaction_in_progress
= false;
791 /* Process all configuration changes associated to a transaction. */
792 static int nb_transaction_process(enum nb_event event
,
793 struct nb_transaction
*transaction
)
795 struct nb_config_cb
*cb
;
797 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
798 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
802 * Only try to release resources that were allocated
805 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
808 /* Call the appropriate callback. */
809 ret
= nb_configuration_callback(event
, change
);
814 change
->prepare_ok
= true;
819 * At this point it's not possible to reject the
820 * transaction anymore, so any failure here can lead to
821 * inconsistencies and should be treated as a bug.
822 * Operations prone to errors, like validations and
823 * resource allocations, should be performed during the
835 static struct nb_config_cb
*
836 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
837 const struct nb_node
*nb_node
,
838 const struct lyd_node
*dnode
)
840 struct nb_config_cb
*cb
;
842 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
843 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
844 cb
->nb_node
= nb_node
;
846 RB_INSERT(nb_config_cbs
, cbs
, cb
);
851 static struct nb_config_cb
*
852 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
853 const struct nb_node
*nb_node
)
855 struct nb_config_cb s
;
857 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
859 return RB_FIND(nb_config_cbs
, cbs
, &s
);
862 /* Call the 'apply_finish' callbacks. */
863 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
865 struct nb_config_cbs cbs
;
866 struct nb_config_cb
*cb
;
868 /* Initialize tree of 'apply_finish' callbacks. */
869 RB_INIT(nb_config_cbs
, &cbs
);
871 /* Identify the 'apply_finish' callbacks that need to be called. */
872 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
873 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
874 const struct lyd_node
*dnode
= change
->cb
.dnode
;
877 * Iterate up to the root of the data tree. When a node is being
878 * deleted, skip its 'apply_finish' callback if one is defined
879 * (the 'apply_finish' callbacks from the node ancestors should
882 if (change
->cb
.operation
== NB_OP_DELETE
) {
883 char xpath
[XPATH_MAXLEN
];
885 dnode
= dnode
->parent
;
890 * The dnode from 'delete' callbacks point to elements
891 * from the running configuration. Use yang_dnode_get()
892 * to get the corresponding dnode from the candidate
893 * configuration that is being committed.
895 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
896 dnode
= yang_dnode_get(transaction
->config
->dnode
,
900 char xpath
[XPATH_MAXLEN
];
901 struct nb_node
*nb_node
;
903 nb_node
= dnode
->schema
->priv
;
904 if (!nb_node
->cbs
.apply_finish
)
908 * Don't call the callback more than once for the same
911 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
912 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
915 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
918 dnode
= dnode
->parent
;
922 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
923 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
924 if (debug_northbound
)
925 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
928 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
931 /* Release memory. */
932 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
933 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
934 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
935 XFREE(MTYPE_TMP
, cb
);
939 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
940 const char *xpath
, const void *list_entry
,
941 const struct yang_list_keys
*list_keys
,
942 struct yang_translator
*translator
,
943 bool first
, uint32_t flags
,
944 nb_oper_data_cb cb
, void *arg
)
946 struct lys_node
*child
;
948 LY_TREE_FOR (snode
->child
, child
) {
951 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
952 list_keys
, translator
, false,
961 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
962 const char *xpath
, const void *list_entry
,
963 const struct yang_list_keys
*list_keys
,
964 struct yang_translator
*translator
,
965 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
967 struct yang_data
*data
;
969 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
972 /* Ignore list keys. */
973 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
976 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
978 /* Leaf of type "empty" is not present. */
981 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
984 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
986 const void *list_entry
,
987 const struct yang_list_keys
*list_keys
,
988 struct yang_translator
*translator
,
989 uint32_t flags
, nb_oper_data_cb cb
,
992 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
995 /* Presence containers. */
996 if (nb_node
->cbs
.get_elem
) {
997 struct yang_data
*data
;
1000 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
1002 /* Presence container is not present. */
1005 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1010 /* Iterate over the child nodes. */
1011 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1012 list_keys
, translator
, false, flags
,
1017 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1018 const void *parent_list_entry
,
1019 const struct yang_list_keys
*parent_list_keys
,
1020 struct yang_translator
*translator
, uint32_t flags
,
1021 nb_oper_data_cb cb
, void *arg
)
1023 const void *list_entry
= NULL
;
1025 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1029 struct yang_data
*data
;
1033 nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
1035 /* End of the list. */
1038 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
1042 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1045 } while (list_entry
);
1050 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1051 const char *xpath_list
,
1052 const void *parent_list_entry
,
1053 const struct yang_list_keys
*parent_list_keys
,
1054 struct yang_translator
*translator
,
1055 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1057 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1058 const void *list_entry
= NULL
;
1060 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1063 /* Iterate over all list entries. */
1065 struct yang_list_keys list_keys
;
1066 char xpath
[XPATH_MAXLEN
];
1069 /* Obtain list entry. */
1071 nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
1073 /* End of the list. */
1076 /* Obtain the list entry keys. */
1077 if (nb_node
->cbs
.get_keys(list_entry
, &list_keys
) != NB_OK
) {
1078 flog_warn(EC_LIB_NB_CB_STATE
,
1079 "%s: failed to get list keys", __func__
);
1083 /* Build XPath of the list entry. */
1084 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1085 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1086 snprintf(xpath
+ strlen(xpath
),
1087 sizeof(xpath
) - strlen(xpath
), "[%s='%s']",
1088 slist
->keys
[i
]->name
, list_keys
.key
[i
]);
1091 /* Iterate over the child nodes. */
1092 ret
= nb_oper_data_iter_children(
1093 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1094 translator
, false, flags
, cb
, arg
);
1097 } while (list_entry
);
1102 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1103 const char *xpath_parent
,
1104 const void *list_entry
,
1105 const struct yang_list_keys
*list_keys
,
1106 struct yang_translator
*translator
,
1107 bool first
, uint32_t flags
,
1108 nb_oper_data_cb cb
, void *arg
)
1110 struct nb_node
*nb_node
;
1111 char xpath
[XPATH_MAXLEN
];
1114 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1115 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1119 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1120 if (!first
&& snode
->nodetype
!= LYS_USES
)
1121 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1122 "/%s", snode
->name
);
1124 nb_node
= snode
->priv
;
1125 switch (snode
->nodetype
) {
1127 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1128 list_keys
, translator
, flags
,
1132 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1133 list_keys
, translator
, flags
, cb
,
1137 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1138 list_keys
, translator
, flags
,
1142 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1143 list_keys
, translator
, flags
, cb
,
1147 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1148 list_keys
, translator
, false,
1158 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1159 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1161 struct nb_node
*nb_node
;
1162 const void *list_entry
= NULL
;
1163 struct yang_list_keys list_keys
;
1164 struct list
*list_dnodes
;
1165 struct lyd_node
*dnode
, *dn
;
1166 struct listnode
*ln
;
1169 nb_node
= nb_node_find(xpath
);
1171 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1172 "%s: unknown data path: %s", __func__
, xpath
);
1176 /* For now this function works only with containers and lists. */
1177 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1179 EC_LIB_NB_OPERATIONAL_DATA
,
1180 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1186 * Create a data tree from the XPath so that we can parse the keys of
1187 * all YANG lists (if any).
1190 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1191 LYD_PATH_OPT_UPDATE
);
1192 if (!dnode
&& ly_errno
) {
1193 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1198 * We can remove the following two lines once we depend on
1199 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1202 dnode
= yang_dnode_get(dnode
, xpath
);
1206 * Create a linked list to sort the data nodes starting from the root.
1208 list_dnodes
= list_new();
1209 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1210 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1212 listnode_add_head(list_dnodes
, dn
);
1215 * Use the northbound callbacks to find list entry pointer corresponding
1216 * to the given XPath.
1218 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1219 struct lyd_node
*child
;
1223 /* Obtain the list entry keys. */
1224 memset(&list_keys
, 0, sizeof(list_keys
));
1225 LY_TREE_FOR (dn
->child
, child
) {
1226 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1229 strlcpy(list_keys
.key
[n
],
1230 yang_dnode_get_string(child
, NULL
),
1231 sizeof(list_keys
.key
[n
]));
1235 assert(list_keys
.num
1236 == ((struct lys_node_list
*)dn
->schema
)->keys_size
);
1238 /* Find the list entry pointer. */
1239 nn
= dn
->schema
->priv
;
1240 list_entry
= nn
->cbs
.lookup_entry(list_entry
, &list_keys
);
1241 if (list_entry
== NULL
) {
1242 list_delete(&list_dnodes
);
1243 yang_dnode_free(dnode
);
1244 return NB_ERR_NOT_FOUND
;
1248 /* If a list entry was given, iterate over that list entry only. */
1249 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1250 ret
= nb_oper_data_iter_children(
1251 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1252 translator
, true, flags
, cb
, arg
);
1254 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1255 &list_keys
, translator
, true,
1258 list_delete(&list_dnodes
);
1259 yang_dnode_free(dnode
);
1264 bool nb_operation_is_valid(enum nb_operation operation
,
1265 const struct lys_node
*snode
)
1267 struct nb_node
*nb_node
= snode
->priv
;
1268 struct lys_node_container
*scontainer
;
1269 struct lys_node_leaf
*sleaf
;
1271 switch (operation
) {
1273 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1276 switch (snode
->nodetype
) {
1278 sleaf
= (struct lys_node_leaf
*)snode
;
1279 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1283 scontainer
= (struct lys_node_container
*)snode
;
1284 if (!scontainer
->presence
)
1295 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1298 switch (snode
->nodetype
) {
1300 sleaf
= (struct lys_node_leaf
*)snode
;
1301 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1304 /* List keys can't be modified. */
1305 if (lys_is_key(sleaf
, NULL
))
1313 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1316 switch (snode
->nodetype
) {
1318 sleaf
= (struct lys_node_leaf
*)snode
;
1320 /* List keys can't be deleted. */
1321 if (lys_is_key(sleaf
, NULL
))
1325 * Only optional leafs can be deleted, or leafs whose
1326 * parent is a case statement.
1328 if (snode
->parent
->nodetype
== LYS_CASE
)
1332 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1337 scontainer
= (struct lys_node_container
*)snode
;
1338 if (!scontainer
->presence
)
1349 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1352 switch (snode
->nodetype
) {
1355 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1362 case NB_OP_APPLY_FINISH
:
1363 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1366 case NB_OP_GET_ELEM
:
1367 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1370 switch (snode
->nodetype
) {
1375 scontainer
= (struct lys_node_container
*)snode
;
1376 if (!scontainer
->presence
)
1383 case NB_OP_GET_NEXT
:
1384 switch (snode
->nodetype
) {
1386 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1390 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1397 case NB_OP_GET_KEYS
:
1398 case NB_OP_LOOKUP_ENTRY
:
1399 switch (snode
->nodetype
) {
1401 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1409 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1412 switch (snode
->nodetype
) {
1425 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1426 (xpath
, arguments
));
1428 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1432 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1434 list_delete(&arguments
);
1439 const char *nb_event_name(enum nb_event event
)
1442 case NB_EV_VALIDATE
:
1455 const char *nb_operation_name(enum nb_operation operation
)
1457 switch (operation
) {
1466 case NB_OP_APPLY_FINISH
:
1467 return "apply_finish";
1468 case NB_OP_GET_ELEM
:
1470 case NB_OP_GET_NEXT
:
1472 case NB_OP_GET_KEYS
:
1474 case NB_OP_LOOKUP_ENTRY
:
1475 return "lookup_entry";
1483 const char *nb_err_name(enum nb_error error
)
1489 return "generic error";
1490 case NB_ERR_NO_CHANGES
:
1491 return "no changes";
1492 case NB_ERR_NOT_FOUND
:
1493 return "element not found";
1495 return "resource is locked";
1496 case NB_ERR_VALIDATION
:
1497 return "validation error";
1498 case NB_ERR_RESOURCE
:
1499 return "failed to allocate resource";
1500 case NB_ERR_INCONSISTENCY
:
1501 return "internal inconsistency";
1507 const char *nb_client_name(enum nb_client client
)
1512 case NB_CLIENT_CONFD
:
1514 case NB_CLIENT_SYSREPO
:
1521 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1523 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1524 struct nb_node
*nb_node
;
1527 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1529 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1530 "%s: unknown data path: %s", __func__
,
1531 module
->nodes
[i
].xpath
);
1535 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1536 priority
= module
->nodes
[i
].priority
;
1538 nb_node
->priority
= priority
;
1542 void nb_init(const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1544 unsigned int errors
= 0;
1546 /* Load YANG modules. */
1547 for (size_t i
= 0; i
< nmodules
; i
++)
1548 yang_module_load(modules
[i
]->name
);
1550 /* Create a nb_node for all YANG schema nodes. */
1553 /* Load northbound callbacks. */
1554 for (size_t i
= 0; i
< nmodules
; i
++)
1555 nb_load_callbacks(modules
[i
]);
1557 /* Validate northbound callbacks. */
1558 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1561 EC_LIB_NB_CBS_VALIDATION
,
1562 "%s: failed to validate northbound callbacks: %u error(s)",
1567 /* Initialize the northbound database (used for the rollback log). */
1568 if (nb_db_init() != NB_OK
)
1569 flog_warn(EC_LIB_NB_DATABASE
,
1570 "%s: failed to initialize northbound database",
1573 /* Create an empty running configuration. */
1574 running_config
= nb_config_new(NULL
);
1576 /* Initialize the northbound CLI. */
1580 void nb_terminate(void)
1582 /* Terminate the northbound CLI. */
1585 /* Delete all nb_node's from all YANG modules. */
1588 /* Delete the running configuration. */
1589 nb_config_free(running_config
);