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
);
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 && (dnode
->schema
->nodetype
& (LYS_CONTAINER
| LYS_LIST
)))
384 nb_config_diff_new_subtree(dnode
, changes
);
390 int nb_candidate_edit(struct nb_config
*candidate
,
391 const struct nb_node
*nb_node
,
392 enum nb_operation operation
, const char *xpath
,
393 const struct yang_data
*previous
,
394 const struct yang_data
*data
)
396 struct lyd_node
*dnode
;
397 char xpath_edit
[XPATH_MAXLEN
];
399 if (!nb_operation_is_valid(operation
, nb_node
->snode
)) {
400 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
401 "%s: %s operation not valid for %s", __func__
,
402 nb_operation_name(operation
), xpath
);
406 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
407 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
408 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
411 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
417 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
418 xpath_edit
, (void *)data
->value
, 0,
419 LYD_PATH_OPT_UPDATE
);
420 if (!dnode
&& ly_errno
) {
421 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
427 * If a new node was created, call lyd_validate() only to create
428 * default child nodes.
431 lyd_schema_sort(dnode
, 0);
432 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
436 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
439 * Return a special error code so the caller can choose
440 * whether to ignore it or not.
442 return NB_ERR_NOT_FOUND
;
446 /* TODO: update configuration. */
449 flog_warn(EC_LIB_DEVELOPMENT
,
450 "%s: unknown operation (%u) [xpath %s]", __func__
,
451 operation
, xpath_edit
);
458 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
460 if (candidate
->version
< running_config
->version
)
466 int nb_candidate_update(struct nb_config
*candidate
)
468 struct nb_config
*updated_config
;
470 updated_config
= nb_config_dup(running_config
);
471 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
474 nb_config_replace(candidate
, updated_config
, false);
480 * The northbound configuration callbacks use the 'priv' pointer present in the
481 * libyang lyd_node structure to store pointers to FRR internal variables
482 * associated to YANG lists and presence containers. Before commiting a
483 * candidate configuration, we must restore the 'priv' pointers stored in the
484 * running configuration since they might be lost while editing the candidate.
486 static void nb_candidate_restore_priv_pointers(struct nb_config
*candidate
)
488 struct lyd_node
*root
, *next
, *dnode_iter
;
490 LY_TREE_FOR (running_config
->dnode
, root
) {
491 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
492 struct lyd_node
*dnode_candidate
;
493 char xpath
[XPATH_MAXLEN
];
495 if (!dnode_iter
->priv
)
498 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
500 yang_dnode_get(candidate
->dnode
, xpath
);
502 yang_dnode_set_entry(dnode_candidate
,
506 LY_TREE_DFS_END(root
, next
, dnode_iter
);
512 * Perform YANG syntactic and semantic validation.
514 * WARNING: lyd_validate() can change the configuration as part of the
515 * validation process.
517 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
519 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
522 return NB_ERR_VALIDATION
;
527 /* Perform code-level validation using the northbound callbacks. */
528 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
529 struct nb_config_cbs
*changes
)
531 struct nb_config_cb
*cb
;
533 nb_candidate_restore_priv_pointers(candidate
);
534 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
535 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
538 ret
= nb_configuration_callback(NB_EV_VALIDATE
, change
);
540 return NB_ERR_VALIDATION
;
546 int nb_candidate_validate(struct nb_config
*candidate
)
548 struct nb_config_cbs changes
;
551 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
552 return NB_ERR_VALIDATION
;
554 RB_INIT(nb_config_cbs
, &changes
);
555 nb_config_diff(running_config
, candidate
, &changes
);
556 ret
= nb_candidate_validate_changes(candidate
, &changes
);
557 nb_config_diff_del_changes(&changes
);
562 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
563 enum nb_client client
, const char *comment
,
564 struct nb_transaction
**transaction
)
566 struct nb_config_cbs changes
;
568 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
569 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
570 "%s: failed to validate candidate configuration",
572 return NB_ERR_VALIDATION
;
575 RB_INIT(nb_config_cbs
, &changes
);
576 nb_config_diff(running_config
, candidate
, &changes
);
577 if (RB_EMPTY(nb_config_cbs
, &changes
))
578 return NB_ERR_NO_CHANGES
;
580 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
581 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
582 "%s: failed to validate candidate configuration",
584 nb_config_diff_del_changes(&changes
);
585 return NB_ERR_VALIDATION
;
588 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
589 if (*transaction
== NULL
) {
590 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
591 "%s: failed to create transaction", __func__
);
592 nb_config_diff_del_changes(&changes
);
593 return NB_ERR_LOCKED
;
596 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
599 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
601 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
602 nb_transaction_free(transaction
);
605 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
606 bool save_transaction
, uint32_t *transaction_id
)
608 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
609 nb_transaction_apply_finish(transaction
);
611 /* Replace running by candidate. */
612 transaction
->config
->version
++;
613 nb_config_replace(running_config
, transaction
->config
, true);
615 /* Record transaction. */
617 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
618 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
619 "%s: failed to record transaction", __func__
);
621 nb_transaction_free(transaction
);
624 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
625 bool save_transaction
, const char *comment
,
626 uint32_t *transaction_id
)
628 struct nb_transaction
*transaction
= NULL
;
631 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
634 * Apply the changes if the preparation phase succeeded. Otherwise abort
638 nb_candidate_commit_apply(transaction
, save_transaction
,
640 else if (transaction
!= NULL
)
641 nb_candidate_commit_abort(transaction
);
646 static void nb_log_callback(const enum nb_event event
,
647 enum nb_operation operation
, const char *xpath
,
651 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
652 nb_event_name(event
), nb_operation_name(operation
), xpath
,
657 * Call the northbound configuration callback associated to a given
658 * configuration change.
660 static int nb_configuration_callback(const enum nb_event event
,
661 struct nb_config_change
*change
)
663 enum nb_operation operation
= change
->cb
.operation
;
664 const char *xpath
= change
->cb
.xpath
;
665 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
666 const struct lyd_node
*dnode
= change
->cb
.dnode
;
667 union nb_resource
*resource
;
670 if (debug_northbound
) {
671 const char *value
= "(none)";
673 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
674 value
= yang_dnode_get_string(dnode
, NULL
);
676 nb_log_callback(event
, operation
, xpath
, value
);
679 if (event
== NB_EV_VALIDATE
)
682 resource
= &change
->resource
;
686 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
689 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
692 ret
= (*nb_node
->cbs
.delete)(event
, dnode
);
695 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
704 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
705 __func__
, nb_err_name(ret
), nb_event_name(event
),
706 nb_operation_name(operation
), xpath
);
711 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
712 struct nb_config_cbs
*changes
,
713 enum nb_client client
,
716 struct nb_transaction
*transaction
;
718 if (transaction_in_progress
) {
720 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
721 "%s: error - there's already another transaction in progress",
725 transaction_in_progress
= true;
727 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
728 transaction
->client
= client
;
730 strlcpy(transaction
->comment
, comment
,
731 sizeof(transaction
->comment
));
732 transaction
->config
= config
;
733 transaction
->changes
= *changes
;
738 static void nb_transaction_free(struct nb_transaction
*transaction
)
740 nb_config_diff_del_changes(&transaction
->changes
);
741 XFREE(MTYPE_TMP
, transaction
);
742 transaction_in_progress
= false;
745 /* Process all configuration changes associated to a transaction. */
746 static int nb_transaction_process(enum nb_event event
,
747 struct nb_transaction
*transaction
)
749 struct nb_config_cb
*cb
;
751 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
752 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
756 * Only try to release resources that were allocated
759 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
762 /* Call the appropriate callback. */
763 ret
= nb_configuration_callback(event
, change
);
768 change
->prepare_ok
= true;
773 * At this point it's not possible to reject the
774 * transaction anymore, so any failure here can lead to
775 * inconsistencies and should be treated as a bug.
776 * Operations prone to errors, like validations and
777 * resource allocations, should be performed during the
789 static struct nb_config_cb
*
790 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
791 const struct nb_node
*nb_node
,
792 const struct lyd_node
*dnode
)
794 struct nb_config_cb
*cb
;
796 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
797 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
798 cb
->nb_node
= nb_node
;
800 RB_INSERT(nb_config_cbs
, cbs
, cb
);
805 static struct nb_config_cb
*
806 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
807 const struct nb_node
*nb_node
)
809 struct nb_config_cb s
;
811 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
813 return RB_FIND(nb_config_cbs
, cbs
, &s
);
816 /* Call the 'apply_finish' callbacks. */
817 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
819 struct nb_config_cbs cbs
;
820 struct nb_config_cb
*cb
;
822 /* Initialize tree of 'apply_finish' callbacks. */
823 RB_INIT(nb_config_cbs
, &cbs
);
825 /* Identify the 'apply_finish' callbacks that need to be called. */
826 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
827 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
828 const struct lyd_node
*dnode
= change
->cb
.dnode
;
831 * Iterate up to the root of the data tree. When a node is being
832 * deleted, skip its 'apply_finish' callback if one is defined
833 * (the 'apply_finish' callbacks from the node ancestors should
836 if (change
->cb
.operation
== NB_OP_DELETE
) {
837 char xpath
[XPATH_MAXLEN
];
839 dnode
= dnode
->parent
;
844 * The dnode from 'delete' callbacks point to elements
845 * from the running configuration. Use yang_dnode_get()
846 * to get the corresponding dnode from the candidate
847 * configuration that is being committed.
849 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
850 dnode
= yang_dnode_get(transaction
->config
->dnode
,
854 char xpath
[XPATH_MAXLEN
];
855 struct nb_node
*nb_node
;
857 nb_node
= dnode
->schema
->priv
;
858 if (!nb_node
->cbs
.apply_finish
)
862 * Don't call the callback more than once for the same
865 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
866 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
869 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
872 dnode
= dnode
->parent
;
876 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
877 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
878 if (debug_northbound
)
879 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
882 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
885 /* Release memory. */
886 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
887 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
888 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
889 XFREE(MTYPE_TMP
, cb
);
893 bool nb_operation_is_valid(enum nb_operation operation
,
894 const struct lys_node
*snode
)
896 struct lys_node_container
*scontainer
;
897 struct lys_node_leaf
*sleaf
;
901 if (!(snode
->flags
& LYS_CONFIG_W
))
904 switch (snode
->nodetype
) {
906 sleaf
= (struct lys_node_leaf
*)snode
;
907 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
911 scontainer
= (struct lys_node_container
*)snode
;
912 if (!scontainer
->presence
)
923 if (!(snode
->flags
& LYS_CONFIG_W
))
926 switch (snode
->nodetype
) {
928 sleaf
= (struct lys_node_leaf
*)snode
;
929 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
932 /* List keys can't be modified. */
933 if (lys_is_key(sleaf
, NULL
))
941 if (!(snode
->flags
& LYS_CONFIG_W
))
944 switch (snode
->nodetype
) {
946 sleaf
= (struct lys_node_leaf
*)snode
;
948 /* List keys can't be deleted. */
949 if (lys_is_key(sleaf
, NULL
))
953 * Only optional leafs can be deleted, or leafs whose
954 * parent is a case statement.
956 if (snode
->parent
->nodetype
== LYS_CASE
)
960 if ((sleaf
->flags
& LYS_MAND_TRUE
) || sleaf
->dflt
)
964 scontainer
= (struct lys_node_container
*)snode
;
965 if (!scontainer
->presence
)
976 if (!(snode
->flags
& LYS_CONFIG_W
))
979 switch (snode
->nodetype
) {
982 if (!(snode
->flags
& LYS_USERORDERED
))
989 case NB_OP_APPLY_FINISH
:
990 if (!(snode
->flags
& LYS_CONFIG_W
))
994 if (!(snode
->flags
& LYS_CONFIG_R
))
997 switch (snode
->nodetype
) {
1001 scontainer
= (struct lys_node_container
*)snode
;
1002 if (!scontainer
->presence
)
1009 case NB_OP_GET_NEXT
:
1010 case NB_OP_GET_KEYS
:
1011 case NB_OP_LOOKUP_ENTRY
:
1012 if (!(snode
->flags
& LYS_CONFIG_R
))
1015 switch (snode
->nodetype
) {
1023 if (snode
->flags
& (LYS_CONFIG_W
| LYS_CONFIG_R
))
1026 switch (snode
->nodetype
) {
1039 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1040 (xpath
, arguments
));
1042 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1046 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1048 list_delete(&arguments
);
1053 const char *nb_event_name(enum nb_event event
)
1056 case NB_EV_VALIDATE
:
1069 const char *nb_operation_name(enum nb_operation operation
)
1071 switch (operation
) {
1080 case NB_OP_APPLY_FINISH
:
1081 return "apply_finish";
1082 case NB_OP_GET_ELEM
:
1084 case NB_OP_GET_NEXT
:
1086 case NB_OP_GET_KEYS
:
1088 case NB_OP_LOOKUP_ENTRY
:
1089 return "lookup_entry";
1097 const char *nb_err_name(enum nb_error error
)
1103 return "generic error";
1104 case NB_ERR_NO_CHANGES
:
1105 return "no changes";
1106 case NB_ERR_NOT_FOUND
:
1107 return "element not found";
1109 return "resource is locked";
1110 case NB_ERR_VALIDATION
:
1111 return "validation error";
1112 case NB_ERR_RESOURCE
:
1113 return "failed to allocate resource";
1114 case NB_ERR_INCONSISTENCY
:
1115 return "internal inconsistency";
1121 const char *nb_client_name(enum nb_client client
)
1126 case NB_CLIENT_CONFD
:
1128 case NB_CLIENT_SYSREPO
:
1135 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1137 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1138 struct nb_node
*nb_node
;
1141 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1143 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1144 "%s: unknown data path: %s", __func__
,
1145 module
->nodes
[i
].xpath
);
1149 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1150 priority
= module
->nodes
[i
].priority
;
1152 nb_node
->priority
= priority
;
1156 void nb_init(const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1158 unsigned int errors
= 0;
1160 /* Load YANG modules. */
1161 for (size_t i
= 0; i
< nmodules
; i
++)
1162 yang_module_load(modules
[i
]->name
);
1164 /* Create a nb_node for all YANG schema nodes. */
1165 yang_all_snodes_iterate(nb_node_new_cb
, 0, NULL
, NULL
);
1167 /* Load northbound callbacks. */
1168 for (size_t i
= 0; i
< nmodules
; i
++)
1169 nb_load_callbacks(modules
[i
]);
1171 /* Validate northbound callbacks. */
1172 yang_all_snodes_iterate(nb_node_validate
, 0, &errors
, NULL
);
1175 EC_LIB_NB_CBS_VALIDATION
,
1176 "%s: failed to validate northbound callbacks: %u error(s)",
1181 /* Initialize the northbound database (used for the rollback log). */
1182 if (nb_db_init() != NB_OK
)
1183 flog_warn(EC_LIB_NB_DATABASE
,
1184 "%s: failed to initialize northbound database",
1187 /* Create an empty running configuration. */
1188 running_config
= nb_config_new(NULL
);
1190 /* Initialize the northbound CLI. */
1194 void nb_terminate(void)
1196 /* Terminate the northbound CLI. */
1199 /* Delete all nb_node's from all YANG modules. */
1200 yang_all_snodes_iterate(nb_node_del_cb
, 0, NULL
, NULL
);
1202 /* Delete the running configuration. */
1203 nb_config_free(running_config
);