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
);
54 static void nb_node_new_cb(const struct lys_node
*snode
, void *arg1
, void *arg2
)
56 struct nb_node
*nb_node
;
57 struct lys_node
*sparent
, *sparent_list
;
59 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
60 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
61 sizeof(nb_node
->xpath
));
62 nb_node
->priority
= NB_DFLT_PRIORITY
;
63 sparent
= yang_snode_real_parent(snode
);
65 nb_node
->parent
= sparent
->priv
;
66 sparent_list
= yang_snode_parent_list(snode
);
68 nb_node
->parent_list
= sparent_list
->priv
;
71 * Link the northbound node and the libyang schema node with one
74 nb_node
->snode
= snode
;
75 lys_set_private(snode
, nb_node
);
78 static void nb_node_del_cb(const struct lys_node
*snode
, void *arg1
, void *arg2
)
80 struct nb_node
*nb_node
;
82 nb_node
= snode
->priv
;
83 lys_set_private(snode
, NULL
);
84 XFREE(MTYPE_NB_NODE
, nb_node
);
87 struct nb_node
*nb_node_find(const char *xpath
)
89 const struct lys_node
*snode
;
92 * Use libyang to find the schema node associated to the xpath and get
93 * the northbound node from there (snode private pointer).
95 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
102 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
103 enum nb_operation operation
,
104 int callback_implemented
, bool optional
)
108 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
110 if (!valid
&& callback_implemented
)
111 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
112 "unneeded '%s' callback for '%s'",
113 nb_operation_name(operation
), nb_node
->xpath
);
115 if (!optional
&& valid
&& !callback_implemented
) {
116 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
117 nb_operation_name(operation
), nb_node
->xpath
);
125 * Check if the required callbacks were implemented for the given northbound
128 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
131 unsigned int error
= 0;
133 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
134 !!nb_node
->cbs
.create
, false);
135 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
136 !!nb_node
->cbs
.modify
, false);
137 error
+= nb_node_validate_cb(nb_node
, NB_OP_DELETE
,
138 !!nb_node
->cbs
.delete, false);
139 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
141 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
142 !!nb_node
->cbs
.apply_finish
, true);
143 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
144 !!nb_node
->cbs
.get_elem
, false);
145 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
146 !!nb_node
->cbs
.get_next
, false);
147 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
148 !!nb_node
->cbs
.get_keys
, false);
149 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
150 !!nb_node
->cbs
.lookup_entry
, false);
151 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
157 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
159 /* Top-level nodes can have any priority. */
160 if (!nb_node
->parent
)
163 if (nb_node
->priority
< nb_node
->parent
->priority
) {
164 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
165 "node has higher priority than its parent [xpath %s]",
173 static void nb_node_validate(const struct lys_node
*snode
, void *arg1
,
176 struct nb_node
*nb_node
= snode
->priv
;
177 unsigned int *errors
= arg1
;
179 /* Validate callbacks and priority. */
180 *errors
+= nb_node_validate_cbs(nb_node
);
181 *errors
+= nb_node_validate_priority(nb_node
);
184 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
186 struct nb_config
*config
;
188 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
190 config
->dnode
= dnode
;
192 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
198 void nb_config_free(struct nb_config
*config
)
201 yang_dnode_free(config
->dnode
);
202 XFREE(MTYPE_NB_CONFIG
, config
);
205 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
207 struct nb_config
*dup
;
209 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
210 dup
->dnode
= yang_dnode_dup(config
->dnode
);
211 dup
->version
= config
->version
;
216 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
217 bool preserve_source
)
221 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
223 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
225 if (!preserve_source
)
226 nb_config_free(config_src
);
228 return (ret
== 0) ? NB_OK
: NB_ERR
;
231 void nb_config_replace(struct nb_config
*config_dst
,
232 struct nb_config
*config_src
, bool preserve_source
)
234 /* Update version. */
235 if (config_src
->version
!= 0)
236 config_dst
->version
= config_src
->version
;
239 yang_dnode_free(config_dst
->dnode
);
240 if (preserve_source
) {
241 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
243 config_dst
->dnode
= config_src
->dnode
;
244 config_src
->dnode
= NULL
;
245 nb_config_free(config_src
);
249 /* Generate the nb_config_cbs tree. */
250 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
251 const struct nb_config_cb
*b
)
253 /* Sort by priority first. */
254 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
256 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
260 * Use XPath as a tie-breaker. This will naturally sort parent nodes
261 * before their children.
263 return strcmp(a
->xpath
, b
->xpath
);
265 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
267 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
268 enum nb_operation operation
,
269 const struct lyd_node
*dnode
)
271 struct nb_config_change
*change
;
273 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
274 change
->cb
.operation
= operation
;
275 change
->cb
.nb_node
= dnode
->schema
->priv
;
276 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
277 change
->cb
.dnode
= dnode
;
279 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
282 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
284 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
285 struct nb_config_change
*change
;
287 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
289 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
290 XFREE(MTYPE_TMP
, change
);
295 * Helper function used when calculating the delta between two different
296 * configurations. Given a new subtree, calculate all new YANG data nodes,
297 * excluding default leafs and leaf-lists. This is a recursive function.
299 static void nb_config_diff_new_subtree(const struct lyd_node
*dnode
,
300 struct nb_config_cbs
*changes
)
302 struct lyd_node
*child
;
304 LY_TREE_FOR (dnode
->child
, child
) {
305 enum nb_operation operation
;
307 switch (child
->schema
->nodetype
) {
310 if (lyd_wd_default((struct lyd_node_leaf_list
*)child
))
313 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
314 operation
= NB_OP_CREATE
;
315 else if (nb_operation_is_valid(NB_OP_MODIFY
,
317 operation
= NB_OP_MODIFY
;
321 nb_config_diff_add_change(changes
, operation
, child
);
325 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
326 nb_config_diff_add_change(changes
, NB_OP_CREATE
,
328 nb_config_diff_new_subtree(child
, changes
);
336 /* Calculate the delta between two different configurations. */
337 static void nb_config_diff(const struct nb_config
*config1
,
338 const struct nb_config
*config2
,
339 struct nb_config_cbs
*changes
)
341 struct lyd_difflist
*diff
;
343 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
344 LYD_DIFFOPT_WITHDEFAULTS
);
347 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
349 struct lyd_node
*dnode
;
350 enum nb_operation operation
;
352 type
= diff
->type
[i
];
355 case LYD_DIFF_CREATED
:
356 dnode
= diff
->second
[i
];
358 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
359 operation
= NB_OP_CREATE
;
360 else if (nb_operation_is_valid(NB_OP_MODIFY
,
362 operation
= NB_OP_MODIFY
;
366 case LYD_DIFF_DELETED
:
367 dnode
= diff
->first
[i
];
368 operation
= NB_OP_DELETE
;
370 case LYD_DIFF_CHANGED
:
371 dnode
= diff
->second
[i
];
372 operation
= NB_OP_MODIFY
;
374 case LYD_DIFF_MOVEDAFTER1
:
375 case LYD_DIFF_MOVEDAFTER2
:
380 nb_config_diff_add_change(changes
, operation
, dnode
);
382 if (type
== LYD_DIFF_CREATED
383 && CHECK_FLAG(dnode
->schema
->nodetype
,
384 LYS_CONTAINER
| LYS_LIST
))
385 nb_config_diff_new_subtree(dnode
, changes
);
391 int nb_candidate_edit(struct nb_config
*candidate
,
392 const struct nb_node
*nb_node
,
393 enum nb_operation operation
, const char *xpath
,
394 const struct yang_data
*previous
,
395 const struct yang_data
*data
)
397 struct lyd_node
*dnode
;
398 char xpath_edit
[XPATH_MAXLEN
];
400 if (!nb_operation_is_valid(operation
, nb_node
->snode
)) {
401 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
402 "%s: %s operation not valid for %s", __func__
,
403 nb_operation_name(operation
), xpath
);
407 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
408 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
409 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
412 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
418 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
419 xpath_edit
, (void *)data
->value
, 0,
420 LYD_PATH_OPT_UPDATE
);
421 if (!dnode
&& ly_errno
) {
422 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
428 * If a new node was created, call lyd_validate() only to create
429 * default child nodes.
432 lyd_schema_sort(dnode
, 0);
433 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
437 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
440 * Return a special error code so the caller can choose
441 * whether to ignore it or not.
443 return NB_ERR_NOT_FOUND
;
447 /* TODO: update configuration. */
450 flog_warn(EC_LIB_DEVELOPMENT
,
451 "%s: unknown operation (%u) [xpath %s]", __func__
,
452 operation
, xpath_edit
);
459 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
461 if (candidate
->version
< running_config
->version
)
467 int nb_candidate_update(struct nb_config
*candidate
)
469 struct nb_config
*updated_config
;
471 updated_config
= nb_config_dup(running_config
);
472 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
475 nb_config_replace(candidate
, updated_config
, false);
481 * The northbound configuration callbacks use the 'priv' pointer present in the
482 * libyang lyd_node structure to store pointers to FRR internal variables
483 * associated to YANG lists and presence containers. Before commiting a
484 * candidate configuration, we must restore the 'priv' pointers stored in the
485 * running configuration since they might be lost while editing the candidate.
487 static void nb_candidate_restore_priv_pointers(struct nb_config
*candidate
)
489 struct lyd_node
*root
, *next
, *dnode_iter
;
491 LY_TREE_FOR (running_config
->dnode
, root
) {
492 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
493 struct lyd_node
*dnode_candidate
;
494 char xpath
[XPATH_MAXLEN
];
496 if (!dnode_iter
->priv
)
499 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
501 yang_dnode_get(candidate
->dnode
, xpath
);
503 yang_dnode_set_entry(dnode_candidate
,
507 LY_TREE_DFS_END(root
, next
, dnode_iter
);
513 * Perform YANG syntactic and semantic validation.
515 * WARNING: lyd_validate() can change the configuration as part of the
516 * validation process.
518 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
520 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
523 return NB_ERR_VALIDATION
;
528 /* Perform code-level validation using the northbound callbacks. */
529 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
530 struct nb_config_cbs
*changes
)
532 struct nb_config_cb
*cb
;
534 nb_candidate_restore_priv_pointers(candidate
);
535 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
536 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
539 ret
= nb_configuration_callback(NB_EV_VALIDATE
, change
);
541 return NB_ERR_VALIDATION
;
547 int nb_candidate_validate(struct nb_config
*candidate
)
549 struct nb_config_cbs changes
;
552 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
553 return NB_ERR_VALIDATION
;
555 RB_INIT(nb_config_cbs
, &changes
);
556 nb_config_diff(running_config
, candidate
, &changes
);
557 ret
= nb_candidate_validate_changes(candidate
, &changes
);
558 nb_config_diff_del_changes(&changes
);
563 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
564 enum nb_client client
, const char *comment
,
565 struct nb_transaction
**transaction
)
567 struct nb_config_cbs changes
;
569 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
570 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
571 "%s: failed to validate candidate configuration",
573 return NB_ERR_VALIDATION
;
576 RB_INIT(nb_config_cbs
, &changes
);
577 nb_config_diff(running_config
, candidate
, &changes
);
578 if (RB_EMPTY(nb_config_cbs
, &changes
))
579 return NB_ERR_NO_CHANGES
;
581 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
582 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
583 "%s: failed to validate candidate configuration",
585 nb_config_diff_del_changes(&changes
);
586 return NB_ERR_VALIDATION
;
589 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
590 if (*transaction
== NULL
) {
591 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
592 "%s: failed to create transaction", __func__
);
593 nb_config_diff_del_changes(&changes
);
594 return NB_ERR_LOCKED
;
597 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
600 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
602 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
603 nb_transaction_free(transaction
);
606 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
607 bool save_transaction
, uint32_t *transaction_id
)
609 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
610 nb_transaction_apply_finish(transaction
);
612 /* Replace running by candidate. */
613 transaction
->config
->version
++;
614 nb_config_replace(running_config
, transaction
->config
, true);
616 /* Record transaction. */
618 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
619 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
620 "%s: failed to record transaction", __func__
);
622 nb_transaction_free(transaction
);
625 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
626 bool save_transaction
, const char *comment
,
627 uint32_t *transaction_id
)
629 struct nb_transaction
*transaction
= NULL
;
632 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
635 * Apply the changes if the preparation phase succeeded. Otherwise abort
639 nb_candidate_commit_apply(transaction
, save_transaction
,
641 else if (transaction
!= NULL
)
642 nb_candidate_commit_abort(transaction
);
647 static void nb_log_callback(const enum nb_event event
,
648 enum nb_operation operation
, const char *xpath
,
652 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
653 nb_event_name(event
), nb_operation_name(operation
), xpath
,
658 * Call the northbound configuration callback associated to a given
659 * configuration change.
661 static int nb_configuration_callback(const enum nb_event event
,
662 struct nb_config_change
*change
)
664 enum nb_operation operation
= change
->cb
.operation
;
665 const char *xpath
= change
->cb
.xpath
;
666 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
667 const struct lyd_node
*dnode
= change
->cb
.dnode
;
668 union nb_resource
*resource
;
671 if (debug_northbound
) {
672 const char *value
= "(none)";
674 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
675 value
= yang_dnode_get_string(dnode
, NULL
);
677 nb_log_callback(event
, operation
, xpath
, value
);
680 if (event
== NB_EV_VALIDATE
)
683 resource
= &change
->resource
;
687 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
690 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
693 ret
= (*nb_node
->cbs
.delete)(event
, dnode
);
696 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
705 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
706 __func__
, nb_err_name(ret
), nb_event_name(event
),
707 nb_operation_name(operation
), xpath
);
712 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
713 struct nb_config_cbs
*changes
,
714 enum nb_client client
,
717 struct nb_transaction
*transaction
;
719 if (transaction_in_progress
) {
721 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
722 "%s: error - there's already another transaction in progress",
726 transaction_in_progress
= true;
728 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
729 transaction
->client
= client
;
731 strlcpy(transaction
->comment
, comment
,
732 sizeof(transaction
->comment
));
733 transaction
->config
= config
;
734 transaction
->changes
= *changes
;
739 static void nb_transaction_free(struct nb_transaction
*transaction
)
741 nb_config_diff_del_changes(&transaction
->changes
);
742 XFREE(MTYPE_TMP
, transaction
);
743 transaction_in_progress
= false;
746 /* Process all configuration changes associated to a transaction. */
747 static int nb_transaction_process(enum nb_event event
,
748 struct nb_transaction
*transaction
)
750 struct nb_config_cb
*cb
;
752 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
753 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
757 * Only try to release resources that were allocated
760 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
763 /* Call the appropriate callback. */
764 ret
= nb_configuration_callback(event
, change
);
769 change
->prepare_ok
= true;
774 * At this point it's not possible to reject the
775 * transaction anymore, so any failure here can lead to
776 * inconsistencies and should be treated as a bug.
777 * Operations prone to errors, like validations and
778 * resource allocations, should be performed during the
790 static struct nb_config_cb
*
791 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
792 const struct nb_node
*nb_node
,
793 const struct lyd_node
*dnode
)
795 struct nb_config_cb
*cb
;
797 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
798 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
799 cb
->nb_node
= nb_node
;
801 RB_INSERT(nb_config_cbs
, cbs
, cb
);
806 static struct nb_config_cb
*
807 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
808 const struct nb_node
*nb_node
)
810 struct nb_config_cb s
;
812 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
814 return RB_FIND(nb_config_cbs
, cbs
, &s
);
817 /* Call the 'apply_finish' callbacks. */
818 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
820 struct nb_config_cbs cbs
;
821 struct nb_config_cb
*cb
;
823 /* Initialize tree of 'apply_finish' callbacks. */
824 RB_INIT(nb_config_cbs
, &cbs
);
826 /* Identify the 'apply_finish' callbacks that need to be called. */
827 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
828 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
829 const struct lyd_node
*dnode
= change
->cb
.dnode
;
832 * Iterate up to the root of the data tree. When a node is being
833 * deleted, skip its 'apply_finish' callback if one is defined
834 * (the 'apply_finish' callbacks from the node ancestors should
837 if (change
->cb
.operation
== NB_OP_DELETE
) {
838 char xpath
[XPATH_MAXLEN
];
840 dnode
= dnode
->parent
;
845 * The dnode from 'delete' callbacks point to elements
846 * from the running configuration. Use yang_dnode_get()
847 * to get the corresponding dnode from the candidate
848 * configuration that is being committed.
850 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
851 dnode
= yang_dnode_get(transaction
->config
->dnode
,
855 char xpath
[XPATH_MAXLEN
];
856 struct nb_node
*nb_node
;
858 nb_node
= dnode
->schema
->priv
;
859 if (!nb_node
->cbs
.apply_finish
)
863 * Don't call the callback more than once for the same
866 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
867 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
870 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
873 dnode
= dnode
->parent
;
877 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
878 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
879 if (debug_northbound
)
880 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
883 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
886 /* Release memory. */
887 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
888 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
889 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
890 XFREE(MTYPE_TMP
, cb
);
894 bool nb_operation_is_valid(enum nb_operation operation
,
895 const struct lys_node
*snode
)
897 struct lys_node_container
*scontainer
;
898 struct lys_node_leaf
*sleaf
;
902 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
905 switch (snode
->nodetype
) {
907 sleaf
= (struct lys_node_leaf
*)snode
;
908 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
912 scontainer
= (struct lys_node_container
*)snode
;
913 if (!scontainer
->presence
)
924 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
927 switch (snode
->nodetype
) {
929 sleaf
= (struct lys_node_leaf
*)snode
;
930 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
933 /* List keys can't be modified. */
934 if (lys_is_key(sleaf
, NULL
))
942 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
945 switch (snode
->nodetype
) {
947 sleaf
= (struct lys_node_leaf
*)snode
;
949 /* List keys can't be deleted. */
950 if (lys_is_key(sleaf
, NULL
))
954 * Only optional leafs can be deleted, or leafs whose
955 * parent is a case statement.
957 if (snode
->parent
->nodetype
== LYS_CASE
)
961 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
966 scontainer
= (struct lys_node_container
*)snode
;
967 if (!scontainer
->presence
)
978 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
981 switch (snode
->nodetype
) {
984 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
991 case NB_OP_APPLY_FINISH
:
992 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
996 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
999 switch (snode
->nodetype
) {
1003 scontainer
= (struct lys_node_container
*)snode
;
1004 if (!scontainer
->presence
)
1011 case NB_OP_GET_NEXT
:
1012 case NB_OP_GET_KEYS
:
1013 case NB_OP_LOOKUP_ENTRY
:
1014 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1017 switch (snode
->nodetype
) {
1025 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1028 switch (snode
->nodetype
) {
1041 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1042 (xpath
, arguments
));
1044 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1048 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1050 list_delete(&arguments
);
1055 const char *nb_event_name(enum nb_event event
)
1058 case NB_EV_VALIDATE
:
1071 const char *nb_operation_name(enum nb_operation operation
)
1073 switch (operation
) {
1082 case NB_OP_APPLY_FINISH
:
1083 return "apply_finish";
1084 case NB_OP_GET_ELEM
:
1086 case NB_OP_GET_NEXT
:
1088 case NB_OP_GET_KEYS
:
1090 case NB_OP_LOOKUP_ENTRY
:
1091 return "lookup_entry";
1099 const char *nb_err_name(enum nb_error error
)
1105 return "generic error";
1106 case NB_ERR_NO_CHANGES
:
1107 return "no changes";
1108 case NB_ERR_NOT_FOUND
:
1109 return "element not found";
1111 return "resource is locked";
1112 case NB_ERR_VALIDATION
:
1113 return "validation error";
1114 case NB_ERR_RESOURCE
:
1115 return "failed to allocate resource";
1116 case NB_ERR_INCONSISTENCY
:
1117 return "internal inconsistency";
1123 const char *nb_client_name(enum nb_client client
)
1128 case NB_CLIENT_CONFD
:
1130 case NB_CLIENT_SYSREPO
:
1137 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1139 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1140 struct nb_node
*nb_node
;
1143 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1145 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1146 "%s: unknown data path: %s", __func__
,
1147 module
->nodes
[i
].xpath
);
1151 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1152 priority
= module
->nodes
[i
].priority
;
1154 nb_node
->priority
= priority
;
1158 void nb_init(const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1160 unsigned int errors
= 0;
1162 /* Load YANG modules. */
1163 for (size_t i
= 0; i
< nmodules
; i
++)
1164 yang_module_load(modules
[i
]->name
);
1166 /* Create a nb_node for all YANG schema nodes. */
1167 yang_all_snodes_iterate(nb_node_new_cb
, 0, NULL
, NULL
);
1169 /* Load northbound callbacks. */
1170 for (size_t i
= 0; i
< nmodules
; i
++)
1171 nb_load_callbacks(modules
[i
]);
1173 /* Validate northbound callbacks. */
1174 yang_all_snodes_iterate(nb_node_validate
, 0, &errors
, NULL
);
1177 EC_LIB_NB_CBS_VALIDATION
,
1178 "%s: failed to validate northbound callbacks: %u error(s)",
1183 /* Initialize the northbound database (used for the rollback log). */
1184 if (nb_db_init() != NB_OK
)
1185 flog_warn(EC_LIB_NB_DATABASE
,
1186 "%s: failed to initialize northbound database",
1189 /* Create an empty running configuration. */
1190 running_config
= nb_config_new(NULL
);
1192 /* Initialize the northbound CLI. */
1196 void nb_terminate(void)
1198 /* Terminate the northbound CLI. */
1201 /* Delete all nb_node's from all YANG modules. */
1202 yang_all_snodes_iterate(nb_node_del_cb
, 0, NULL
, NULL
);
1204 /* Delete the running configuration. */
1205 nb_config_free(running_config
);