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
);
98 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
99 struct lys_node_list
*slist
;
101 slist
= (struct lys_node_list
*)snode
;
102 if (slist
->keys_size
== 0)
103 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
107 * Link the northbound node and the libyang schema node with one
110 nb_node
->snode
= snode
;
111 lys_set_private(snode
, nb_node
);
113 return YANG_ITER_CONTINUE
;
116 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
118 struct nb_node
*nb_node
;
120 nb_node
= snode
->priv
;
121 lys_set_private(snode
, NULL
);
122 XFREE(MTYPE_NB_NODE
, nb_node
);
124 return YANG_ITER_CONTINUE
;
127 void nb_nodes_create(void)
129 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
132 void nb_nodes_delete(void)
134 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
137 struct nb_node
*nb_node_find(const char *xpath
)
139 const struct lys_node
*snode
;
142 * Use libyang to find the schema node associated to the xpath and get
143 * the northbound node from there (snode private pointer).
145 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
152 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
153 enum nb_operation operation
,
154 int callback_implemented
, bool optional
)
158 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
160 if (!valid
&& callback_implemented
)
161 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
162 "unneeded '%s' callback for '%s'",
163 nb_operation_name(operation
), nb_node
->xpath
);
165 if (!optional
&& valid
&& !callback_implemented
) {
166 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
167 nb_operation_name(operation
), nb_node
->xpath
);
175 * Check if the required callbacks were implemented for the given northbound
178 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
181 unsigned int error
= 0;
183 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
184 !!nb_node
->cbs
.create
, false);
185 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
186 !!nb_node
->cbs
.modify
, false);
187 error
+= nb_node_validate_cb(nb_node
, NB_OP_DELETE
,
188 !!nb_node
->cbs
.delete, false);
189 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
191 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
192 !!nb_node
->cbs
.apply_finish
, true);
193 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
194 !!nb_node
->cbs
.get_elem
, false);
195 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
196 !!nb_node
->cbs
.get_next
, false);
197 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
198 !!nb_node
->cbs
.get_keys
, false);
199 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
200 !!nb_node
->cbs
.lookup_entry
, false);
201 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
207 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
209 /* Top-level nodes can have any priority. */
210 if (!nb_node
->parent
)
213 if (nb_node
->priority
< nb_node
->parent
->priority
) {
214 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
215 "node has higher priority than its parent [xpath %s]",
223 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
225 struct nb_node
*nb_node
= snode
->priv
;
226 unsigned int *errors
= arg
;
228 /* Validate callbacks and priority. */
229 *errors
+= nb_node_validate_cbs(nb_node
);
230 *errors
+= nb_node_validate_priority(nb_node
);
232 return YANG_ITER_CONTINUE
;
235 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
237 struct nb_config
*config
;
239 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
241 config
->dnode
= dnode
;
243 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
249 void nb_config_free(struct nb_config
*config
)
252 yang_dnode_free(config
->dnode
);
253 XFREE(MTYPE_NB_CONFIG
, config
);
256 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
258 struct nb_config
*dup
;
260 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
261 dup
->dnode
= yang_dnode_dup(config
->dnode
);
262 dup
->version
= config
->version
;
267 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
268 bool preserve_source
)
272 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
274 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
276 if (!preserve_source
)
277 nb_config_free(config_src
);
279 return (ret
== 0) ? NB_OK
: NB_ERR
;
282 void nb_config_replace(struct nb_config
*config_dst
,
283 struct nb_config
*config_src
, bool preserve_source
)
285 /* Update version. */
286 if (config_src
->version
!= 0)
287 config_dst
->version
= config_src
->version
;
290 if (config_dst
->dnode
)
291 yang_dnode_free(config_dst
->dnode
);
292 if (preserve_source
) {
293 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
295 config_dst
->dnode
= config_src
->dnode
;
296 config_src
->dnode
= NULL
;
297 nb_config_free(config_src
);
301 /* Generate the nb_config_cbs tree. */
302 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
303 const struct nb_config_cb
*b
)
305 /* Sort by priority first. */
306 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
308 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
312 * Use XPath as a tie-breaker. This will naturally sort parent nodes
313 * before their children.
315 return strcmp(a
->xpath
, b
->xpath
);
317 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
319 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
320 enum nb_operation operation
,
321 const struct lyd_node
*dnode
)
323 struct nb_config_change
*change
;
325 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
326 change
->cb
.operation
= operation
;
327 change
->cb
.nb_node
= dnode
->schema
->priv
;
328 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
329 change
->cb
.dnode
= dnode
;
331 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
334 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
336 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
337 struct nb_config_change
*change
;
339 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
341 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
342 XFREE(MTYPE_TMP
, change
);
347 * Helper function used when calculating the delta between two different
348 * configurations. Given a new subtree, calculate all new YANG data nodes,
349 * excluding default leafs and leaf-lists. This is a recursive function.
351 static void nb_config_diff_new_subtree(const struct lyd_node
*dnode
,
352 struct nb_config_cbs
*changes
)
354 struct lyd_node
*child
;
356 LY_TREE_FOR (dnode
->child
, child
) {
357 enum nb_operation operation
;
359 switch (child
->schema
->nodetype
) {
362 if (lyd_wd_default((struct lyd_node_leaf_list
*)child
))
365 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
366 operation
= NB_OP_CREATE
;
367 else if (nb_operation_is_valid(NB_OP_MODIFY
,
369 operation
= NB_OP_MODIFY
;
373 nb_config_diff_add_change(changes
, operation
, child
);
377 if (nb_operation_is_valid(NB_OP_CREATE
, child
->schema
))
378 nb_config_diff_add_change(changes
, NB_OP_CREATE
,
380 nb_config_diff_new_subtree(child
, changes
);
388 /* Calculate the delta between two different configurations. */
389 static void nb_config_diff(const struct nb_config
*config1
,
390 const struct nb_config
*config2
,
391 struct nb_config_cbs
*changes
)
393 struct lyd_difflist
*diff
;
395 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
396 LYD_DIFFOPT_WITHDEFAULTS
);
399 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
401 struct lyd_node
*dnode
;
402 enum nb_operation operation
;
404 type
= diff
->type
[i
];
407 case LYD_DIFF_CREATED
:
408 dnode
= diff
->second
[i
];
410 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
411 operation
= NB_OP_CREATE
;
412 else if (nb_operation_is_valid(NB_OP_MODIFY
,
414 operation
= NB_OP_MODIFY
;
418 case LYD_DIFF_DELETED
:
419 dnode
= diff
->first
[i
];
420 operation
= NB_OP_DELETE
;
422 case LYD_DIFF_CHANGED
:
423 dnode
= diff
->second
[i
];
424 operation
= NB_OP_MODIFY
;
426 case LYD_DIFF_MOVEDAFTER1
:
427 case LYD_DIFF_MOVEDAFTER2
:
432 nb_config_diff_add_change(changes
, operation
, dnode
);
434 if (type
== LYD_DIFF_CREATED
435 && CHECK_FLAG(dnode
->schema
->nodetype
,
436 LYS_CONTAINER
| LYS_LIST
))
437 nb_config_diff_new_subtree(dnode
, changes
);
443 int nb_candidate_edit(struct nb_config
*candidate
,
444 const struct nb_node
*nb_node
,
445 enum nb_operation operation
, const char *xpath
,
446 const struct yang_data
*previous
,
447 const struct yang_data
*data
)
449 struct lyd_node
*dnode
;
450 char xpath_edit
[XPATH_MAXLEN
];
452 if (!nb_operation_is_valid(operation
, nb_node
->snode
)) {
453 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
454 "%s: %s operation not valid for %s", __func__
,
455 nb_operation_name(operation
), xpath
);
459 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
460 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
461 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
464 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
470 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
471 xpath_edit
, (void *)data
->value
, 0,
472 LYD_PATH_OPT_UPDATE
);
473 if (!dnode
&& ly_errno
) {
474 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
480 * If a new node was created, call lyd_validate() only to create
481 * default child nodes.
484 lyd_schema_sort(dnode
, 0);
485 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
489 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
492 * Return a special error code so the caller can choose
493 * whether to ignore it or not.
495 return NB_ERR_NOT_FOUND
;
499 /* TODO: update configuration. */
502 flog_warn(EC_LIB_DEVELOPMENT
,
503 "%s: unknown operation (%u) [xpath %s]", __func__
,
504 operation
, xpath_edit
);
511 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
513 if (candidate
->version
< running_config
->version
)
519 int nb_candidate_update(struct nb_config
*candidate
)
521 struct nb_config
*updated_config
;
523 updated_config
= nb_config_dup(running_config
);
524 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
527 nb_config_replace(candidate
, updated_config
, false);
533 * The northbound configuration callbacks use the 'priv' pointer present in the
534 * libyang lyd_node structure to store pointers to FRR internal variables
535 * associated to YANG lists and presence containers. Before commiting a
536 * candidate configuration, we must restore the 'priv' pointers stored in the
537 * running configuration since they might be lost while editing the candidate.
539 static void nb_candidate_restore_priv_pointers(struct nb_config
*candidate
)
541 struct lyd_node
*root
, *next
, *dnode_iter
;
543 LY_TREE_FOR (running_config
->dnode
, root
) {
544 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
545 struct lyd_node
*dnode_candidate
;
546 char xpath
[XPATH_MAXLEN
];
548 if (!dnode_iter
->priv
)
551 yang_dnode_get_path(dnode_iter
, xpath
, sizeof(xpath
));
553 yang_dnode_get(candidate
->dnode
, xpath
);
555 yang_dnode_set_entry(dnode_candidate
,
559 LY_TREE_DFS_END(root
, next
, dnode_iter
);
565 * Perform YANG syntactic and semantic validation.
567 * WARNING: lyd_validate() can change the configuration as part of the
568 * validation process.
570 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
572 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
575 return NB_ERR_VALIDATION
;
580 /* Perform code-level validation using the northbound callbacks. */
581 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
582 struct nb_config_cbs
*changes
)
584 struct nb_config_cb
*cb
;
586 nb_candidate_restore_priv_pointers(candidate
);
587 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
588 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
591 ret
= nb_configuration_callback(NB_EV_VALIDATE
, change
);
593 return NB_ERR_VALIDATION
;
599 int nb_candidate_validate(struct nb_config
*candidate
)
601 struct nb_config_cbs changes
;
604 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
605 return NB_ERR_VALIDATION
;
607 RB_INIT(nb_config_cbs
, &changes
);
608 nb_config_diff(running_config
, candidate
, &changes
);
609 ret
= nb_candidate_validate_changes(candidate
, &changes
);
610 nb_config_diff_del_changes(&changes
);
615 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
616 enum nb_client client
, const char *comment
,
617 struct nb_transaction
**transaction
)
619 struct nb_config_cbs changes
;
621 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
622 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
623 "%s: failed to validate candidate configuration",
625 return NB_ERR_VALIDATION
;
628 RB_INIT(nb_config_cbs
, &changes
);
629 nb_config_diff(running_config
, candidate
, &changes
);
630 if (RB_EMPTY(nb_config_cbs
, &changes
))
631 return NB_ERR_NO_CHANGES
;
633 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
634 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
635 "%s: failed to validate candidate configuration",
637 nb_config_diff_del_changes(&changes
);
638 return NB_ERR_VALIDATION
;
641 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
642 if (*transaction
== NULL
) {
643 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
644 "%s: failed to create transaction", __func__
);
645 nb_config_diff_del_changes(&changes
);
646 return NB_ERR_LOCKED
;
649 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
652 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
654 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
655 nb_transaction_free(transaction
);
658 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
659 bool save_transaction
, uint32_t *transaction_id
)
661 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
662 nb_transaction_apply_finish(transaction
);
664 /* Replace running by candidate. */
665 transaction
->config
->version
++;
666 nb_config_replace(running_config
, transaction
->config
, true);
668 /* Record transaction. */
670 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
671 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
672 "%s: failed to record transaction", __func__
);
674 nb_transaction_free(transaction
);
677 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
678 bool save_transaction
, const char *comment
,
679 uint32_t *transaction_id
)
681 struct nb_transaction
*transaction
= NULL
;
684 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
687 * Apply the changes if the preparation phase succeeded. Otherwise abort
691 nb_candidate_commit_apply(transaction
, save_transaction
,
693 else if (transaction
!= NULL
)
694 nb_candidate_commit_abort(transaction
);
699 static void nb_log_callback(const enum nb_event event
,
700 enum nb_operation operation
, const char *xpath
,
704 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
705 nb_event_name(event
), nb_operation_name(operation
), xpath
,
710 * Call the northbound configuration callback associated to a given
711 * configuration change.
713 static int nb_configuration_callback(const enum nb_event event
,
714 struct nb_config_change
*change
)
716 enum nb_operation operation
= change
->cb
.operation
;
717 const char *xpath
= change
->cb
.xpath
;
718 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
719 const struct lyd_node
*dnode
= change
->cb
.dnode
;
720 union nb_resource
*resource
;
722 enum lib_log_refs ref
;
724 if (debug_northbound
) {
725 const char *value
= "(none)";
727 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
728 value
= yang_dnode_get_string(dnode
, NULL
);
730 nb_log_callback(event
, operation
, xpath
, value
);
733 if (event
== NB_EV_VALIDATE
)
736 resource
= &change
->resource
;
740 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
743 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
746 ret
= (*nb_node
->cbs
.delete)(event
, dnode
);
749 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
758 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
761 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
764 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
767 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
770 if (event
== NB_EV_VALIDATE
|| event
== NB_EV_PREPARE
)
773 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
774 __func__
, nb_err_name(ret
),
775 nb_event_name(event
),
776 nb_operation_name(operation
), xpath
);
780 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
781 __func__
, nb_err_name(ret
),
782 nb_event_name(event
),
783 nb_operation_name(operation
), xpath
);
789 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
790 struct nb_config_cbs
*changes
,
791 enum nb_client client
,
794 struct nb_transaction
*transaction
;
796 if (transaction_in_progress
) {
798 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
799 "%s: error - there's already another transaction in progress",
803 transaction_in_progress
= true;
805 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
806 transaction
->client
= client
;
808 strlcpy(transaction
->comment
, comment
,
809 sizeof(transaction
->comment
));
810 transaction
->config
= config
;
811 transaction
->changes
= *changes
;
816 static void nb_transaction_free(struct nb_transaction
*transaction
)
818 nb_config_diff_del_changes(&transaction
->changes
);
819 XFREE(MTYPE_TMP
, transaction
);
820 transaction_in_progress
= false;
823 /* Process all configuration changes associated to a transaction. */
824 static int nb_transaction_process(enum nb_event event
,
825 struct nb_transaction
*transaction
)
827 struct nb_config_cb
*cb
;
829 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
830 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
834 * Only try to release resources that were allocated
837 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
840 /* Call the appropriate callback. */
841 ret
= nb_configuration_callback(event
, change
);
846 change
->prepare_ok
= true;
851 * At this point it's not possible to reject the
852 * transaction anymore, so any failure here can lead to
853 * inconsistencies and should be treated as a bug.
854 * Operations prone to errors, like validations and
855 * resource allocations, should be performed during the
867 static struct nb_config_cb
*
868 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
869 const struct nb_node
*nb_node
,
870 const struct lyd_node
*dnode
)
872 struct nb_config_cb
*cb
;
874 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
875 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
876 cb
->nb_node
= nb_node
;
878 RB_INSERT(nb_config_cbs
, cbs
, cb
);
883 static struct nb_config_cb
*
884 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
885 const struct nb_node
*nb_node
)
887 struct nb_config_cb s
;
889 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
891 return RB_FIND(nb_config_cbs
, cbs
, &s
);
894 /* Call the 'apply_finish' callbacks. */
895 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
897 struct nb_config_cbs cbs
;
898 struct nb_config_cb
*cb
;
900 /* Initialize tree of 'apply_finish' callbacks. */
901 RB_INIT(nb_config_cbs
, &cbs
);
903 /* Identify the 'apply_finish' callbacks that need to be called. */
904 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
905 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
906 const struct lyd_node
*dnode
= change
->cb
.dnode
;
909 * Iterate up to the root of the data tree. When a node is being
910 * deleted, skip its 'apply_finish' callback if one is defined
911 * (the 'apply_finish' callbacks from the node ancestors should
914 if (change
->cb
.operation
== NB_OP_DELETE
) {
915 char xpath
[XPATH_MAXLEN
];
917 dnode
= dnode
->parent
;
922 * The dnode from 'delete' callbacks point to elements
923 * from the running configuration. Use yang_dnode_get()
924 * to get the corresponding dnode from the candidate
925 * configuration that is being committed.
927 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
928 dnode
= yang_dnode_get(transaction
->config
->dnode
,
932 char xpath
[XPATH_MAXLEN
];
933 struct nb_node
*nb_node
;
935 nb_node
= dnode
->schema
->priv
;
936 if (!nb_node
->cbs
.apply_finish
)
940 * Don't call the callback more than once for the same
943 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
944 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
947 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
950 dnode
= dnode
->parent
;
954 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
955 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
956 if (debug_northbound
)
957 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
960 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
963 /* Release memory. */
964 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
965 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
966 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
967 XFREE(MTYPE_TMP
, cb
);
971 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
972 const char *xpath
, const void *list_entry
,
973 const struct yang_list_keys
*list_keys
,
974 struct yang_translator
*translator
,
975 bool first
, uint32_t flags
,
976 nb_oper_data_cb cb
, void *arg
)
978 struct lys_node
*child
;
980 LY_TREE_FOR (snode
->child
, child
) {
983 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
984 list_keys
, translator
, false,
993 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
994 const char *xpath
, const void *list_entry
,
995 const struct yang_list_keys
*list_keys
,
996 struct yang_translator
*translator
,
997 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
999 struct yang_data
*data
;
1001 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1004 /* Ignore list keys. */
1005 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1008 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
1010 /* Leaf of type "empty" is not present. */
1013 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1016 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1018 const void *list_entry
,
1019 const struct yang_list_keys
*list_keys
,
1020 struct yang_translator
*translator
,
1021 uint32_t flags
, nb_oper_data_cb cb
,
1024 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1027 /* Presence containers. */
1028 if (nb_node
->cbs
.get_elem
) {
1029 struct yang_data
*data
;
1032 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
1034 /* Presence container is not present. */
1037 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1042 /* Iterate over the child nodes. */
1043 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1044 list_keys
, translator
, false, flags
,
1049 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1050 const void *parent_list_entry
,
1051 const struct yang_list_keys
*parent_list_keys
,
1052 struct yang_translator
*translator
, uint32_t flags
,
1053 nb_oper_data_cb cb
, void *arg
)
1055 const void *list_entry
= NULL
;
1057 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1061 struct yang_data
*data
;
1065 nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
1067 /* End of the list. */
1070 data
= nb_node
->cbs
.get_elem(xpath
, list_entry
);
1074 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1077 } while (list_entry
);
1082 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1083 const char *xpath_list
,
1084 const void *parent_list_entry
,
1085 const struct yang_list_keys
*parent_list_keys
,
1086 struct yang_translator
*translator
,
1087 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1089 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1090 const void *list_entry
= NULL
;
1091 uint32_t position
= 1;
1093 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1096 /* Iterate over all list entries. */
1098 struct yang_list_keys list_keys
;
1099 char xpath
[XPATH_MAXLEN
* 2];
1102 /* Obtain list entry. */
1104 nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
1106 /* End of the list. */
1109 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1110 /* Obtain the list entry keys. */
1111 if (nb_node
->cbs
.get_keys(list_entry
, &list_keys
)
1113 flog_warn(EC_LIB_NB_CB_STATE
,
1114 "%s: failed to get list keys",
1119 /* Build XPath of the list entry. */
1120 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1121 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1122 snprintf(xpath
+ strlen(xpath
),
1123 sizeof(xpath
) - strlen(xpath
),
1124 "[%s='%s']", slist
->keys
[i
]->name
,
1129 * Keyless list - build XPath using a positional index.
1131 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1136 /* Iterate over the child nodes. */
1137 ret
= nb_oper_data_iter_children(
1138 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1139 translator
, false, flags
, cb
, arg
);
1142 } while (list_entry
);
1147 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1148 const char *xpath_parent
,
1149 const void *list_entry
,
1150 const struct yang_list_keys
*list_keys
,
1151 struct yang_translator
*translator
,
1152 bool first
, uint32_t flags
,
1153 nb_oper_data_cb cb
, void *arg
)
1155 struct nb_node
*nb_node
;
1156 char xpath
[XPATH_MAXLEN
];
1159 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1160 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1164 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1165 if (!first
&& snode
->nodetype
!= LYS_USES
)
1166 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1167 "/%s", snode
->name
);
1169 nb_node
= snode
->priv
;
1170 switch (snode
->nodetype
) {
1172 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1173 list_keys
, translator
, flags
,
1177 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1178 list_keys
, translator
, flags
, cb
,
1182 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1183 list_keys
, translator
, flags
,
1187 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1188 list_keys
, translator
, flags
, cb
,
1192 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1193 list_keys
, translator
, false,
1203 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1204 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1206 struct nb_node
*nb_node
;
1207 const void *list_entry
= NULL
;
1208 struct yang_list_keys list_keys
;
1209 struct list
*list_dnodes
;
1210 struct lyd_node
*dnode
, *dn
;
1211 struct listnode
*ln
;
1214 nb_node
= nb_node_find(xpath
);
1216 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1217 "%s: unknown data path: %s", __func__
, xpath
);
1221 /* For now this function works only with containers and lists. */
1222 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1224 EC_LIB_NB_OPERATIONAL_DATA
,
1225 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1231 * Create a data tree from the XPath so that we can parse the keys of
1232 * all YANG lists (if any).
1235 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1236 LYD_PATH_OPT_UPDATE
);
1237 if (!dnode
&& ly_errno
) {
1238 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1243 * We can remove the following two lines once we depend on
1244 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1247 dnode
= yang_dnode_get(dnode
, xpath
);
1251 * Create a linked list to sort the data nodes starting from the root.
1253 list_dnodes
= list_new();
1254 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1255 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1257 listnode_add_head(list_dnodes
, dn
);
1260 * Use the northbound callbacks to find list entry pointer corresponding
1261 * to the given XPath.
1263 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1264 struct lyd_node
*child
;
1268 /* Obtain the list entry keys. */
1269 memset(&list_keys
, 0, sizeof(list_keys
));
1270 LY_TREE_FOR (dn
->child
, child
) {
1271 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1274 strlcpy(list_keys
.key
[n
],
1275 yang_dnode_get_string(child
, NULL
),
1276 sizeof(list_keys
.key
[n
]));
1280 assert(list_keys
.num
1281 == ((struct lys_node_list
*)dn
->schema
)->keys_size
);
1283 /* Find the list entry pointer. */
1284 nn
= dn
->schema
->priv
;
1285 list_entry
= nn
->cbs
.lookup_entry(list_entry
, &list_keys
);
1286 if (list_entry
== NULL
) {
1287 list_delete(&list_dnodes
);
1288 yang_dnode_free(dnode
);
1289 return NB_ERR_NOT_FOUND
;
1293 /* If a list entry was given, iterate over that list entry only. */
1294 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1295 ret
= nb_oper_data_iter_children(
1296 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1297 translator
, true, flags
, cb
, arg
);
1299 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1300 &list_keys
, translator
, true,
1303 list_delete(&list_dnodes
);
1304 yang_dnode_free(dnode
);
1309 bool nb_operation_is_valid(enum nb_operation operation
,
1310 const struct lys_node
*snode
)
1312 struct nb_node
*nb_node
= snode
->priv
;
1313 struct lys_node_container
*scontainer
;
1314 struct lys_node_leaf
*sleaf
;
1316 switch (operation
) {
1318 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1321 switch (snode
->nodetype
) {
1323 sleaf
= (struct lys_node_leaf
*)snode
;
1324 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1328 scontainer
= (struct lys_node_container
*)snode
;
1329 if (!scontainer
->presence
)
1340 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1343 switch (snode
->nodetype
) {
1345 sleaf
= (struct lys_node_leaf
*)snode
;
1346 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1349 /* List keys can't be modified. */
1350 if (lys_is_key(sleaf
, NULL
))
1358 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1361 switch (snode
->nodetype
) {
1363 sleaf
= (struct lys_node_leaf
*)snode
;
1365 /* List keys can't be deleted. */
1366 if (lys_is_key(sleaf
, NULL
))
1370 * Only optional leafs can be deleted, or leafs whose
1371 * parent is a case statement.
1373 if (snode
->parent
->nodetype
== LYS_CASE
)
1377 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1382 scontainer
= (struct lys_node_container
*)snode
;
1383 if (!scontainer
->presence
)
1394 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1397 switch (snode
->nodetype
) {
1400 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1407 case NB_OP_APPLY_FINISH
:
1408 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1411 case NB_OP_GET_ELEM
:
1412 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1415 switch (snode
->nodetype
) {
1420 scontainer
= (struct lys_node_container
*)snode
;
1421 if (!scontainer
->presence
)
1428 case NB_OP_GET_NEXT
:
1429 switch (snode
->nodetype
) {
1431 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1435 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1442 case NB_OP_GET_KEYS
:
1443 case NB_OP_LOOKUP_ENTRY
:
1444 switch (snode
->nodetype
) {
1446 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1448 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1456 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1459 switch (snode
->nodetype
) {
1472 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1473 (xpath
, arguments
));
1475 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1479 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1481 list_delete(&arguments
);
1486 const char *nb_event_name(enum nb_event event
)
1489 case NB_EV_VALIDATE
:
1502 const char *nb_operation_name(enum nb_operation operation
)
1504 switch (operation
) {
1513 case NB_OP_APPLY_FINISH
:
1514 return "apply_finish";
1515 case NB_OP_GET_ELEM
:
1517 case NB_OP_GET_NEXT
:
1519 case NB_OP_GET_KEYS
:
1521 case NB_OP_LOOKUP_ENTRY
:
1522 return "lookup_entry";
1530 const char *nb_err_name(enum nb_error error
)
1536 return "generic error";
1537 case NB_ERR_NO_CHANGES
:
1538 return "no changes";
1539 case NB_ERR_NOT_FOUND
:
1540 return "element not found";
1542 return "resource is locked";
1543 case NB_ERR_VALIDATION
:
1544 return "validation error";
1545 case NB_ERR_RESOURCE
:
1546 return "failed to allocate resource";
1547 case NB_ERR_INCONSISTENCY
:
1548 return "internal inconsistency";
1554 const char *nb_client_name(enum nb_client client
)
1559 case NB_CLIENT_CONFD
:
1561 case NB_CLIENT_SYSREPO
:
1568 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1570 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1571 struct nb_node
*nb_node
;
1574 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1576 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1577 "%s: unknown data path: %s", __func__
,
1578 module
->nodes
[i
].xpath
);
1582 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1583 priority
= module
->nodes
[i
].priority
;
1585 nb_node
->priority
= priority
;
1589 void nb_init(struct thread_master
*tm
,
1590 const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1592 unsigned int errors
= 0;
1594 /* Load YANG modules. */
1595 for (size_t i
= 0; i
< nmodules
; i
++)
1596 yang_module_load(modules
[i
]->name
);
1598 /* Create a nb_node for all YANG schema nodes. */
1601 /* Load northbound callbacks. */
1602 for (size_t i
= 0; i
< nmodules
; i
++)
1603 nb_load_callbacks(modules
[i
]);
1605 /* Validate northbound callbacks. */
1606 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1609 EC_LIB_NB_CBS_VALIDATION
,
1610 "%s: failed to validate northbound callbacks: %u error(s)",
1615 /* Initialize the northbound database (used for the rollback log). */
1616 if (nb_db_init() != NB_OK
)
1617 flog_warn(EC_LIB_NB_DATABASE
,
1618 "%s: failed to initialize northbound database",
1621 /* Create an empty running configuration. */
1622 running_config
= nb_config_new(NULL
);
1624 /* Initialize the northbound CLI. */
1628 void nb_terminate(void)
1630 /* Terminate the northbound CLI. */
1633 /* Delete all nb_node's from all YANG modules. */
1636 /* Delete the running configuration. */
1637 nb_config_free(running_config
);