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"
29 #include "northbound.h"
30 #include "northbound_cli.h"
31 #include "northbound_db.h"
33 DEFINE_MTYPE_STATIC(LIB
, NB_NODE
, "Northbound Node")
34 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG
, "Northbound Configuration")
35 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG_ENTRY
, "Northbound Configuration Entry")
37 /* Running configuration - shouldn't be modified directly. */
38 struct nb_config
*running_config
;
40 /* Hash table of user pointers associated with configuration entries. */
41 static struct hash
*running_config_entries
;
44 * Global lock used to prevent multiple configuration transactions from
45 * happening concurrently.
47 static bool transaction_in_progress
;
49 static int nb_callback_configuration(const enum nb_event event
,
50 struct nb_config_change
*change
);
51 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
52 struct nb_config_cbs
*changes
,
53 enum nb_client client
,
55 static void nb_transaction_free(struct nb_transaction
*transaction
);
56 static int nb_transaction_process(enum nb_event event
,
57 struct nb_transaction
*transaction
);
58 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
);
59 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
60 const char *xpath
, const void *list_entry
,
61 const struct yang_list_keys
*list_keys
,
62 struct yang_translator
*translator
,
63 bool first
, uint32_t flags
,
64 nb_oper_data_cb cb
, void *arg
);
66 static int nb_node_check_config_only(const struct lys_node
*snode
, void *arg
)
68 bool *config_only
= arg
;
70 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
72 return YANG_ITER_STOP
;
75 return YANG_ITER_CONTINUE
;
78 static int nb_node_new_cb(const struct lys_node
*snode
, void *arg
)
80 struct nb_node
*nb_node
;
81 struct lys_node
*sparent
, *sparent_list
;
83 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
84 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
85 sizeof(nb_node
->xpath
));
86 nb_node
->priority
= NB_DFLT_PRIORITY
;
87 sparent
= yang_snode_real_parent(snode
);
89 nb_node
->parent
= sparent
->priv
;
90 sparent_list
= yang_snode_parent_list(snode
);
92 nb_node
->parent_list
= sparent_list
->priv
;
95 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
96 bool config_only
= true;
98 yang_snodes_iterate_subtree(snode
, nb_node_check_config_only
,
99 YANG_ITER_ALLOW_AUGMENTATIONS
,
102 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
104 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
105 struct lys_node_list
*slist
;
107 slist
= (struct lys_node_list
*)snode
;
108 if (slist
->keys_size
== 0)
109 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
113 * Link the northbound node and the libyang schema node with one
116 nb_node
->snode
= snode
;
117 lys_set_private(snode
, nb_node
);
119 return YANG_ITER_CONTINUE
;
122 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
124 struct nb_node
*nb_node
;
126 nb_node
= snode
->priv
;
127 lys_set_private(snode
, NULL
);
128 XFREE(MTYPE_NB_NODE
, nb_node
);
130 return YANG_ITER_CONTINUE
;
133 void nb_nodes_create(void)
135 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
138 void nb_nodes_delete(void)
140 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
143 struct nb_node
*nb_node_find(const char *xpath
)
145 const struct lys_node
*snode
;
148 * Use libyang to find the schema node associated to the xpath and get
149 * the northbound node from there (snode private pointer).
151 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
158 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
159 enum nb_operation operation
,
160 int callback_implemented
, bool optional
)
164 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
166 if (!valid
&& callback_implemented
)
167 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
168 "unneeded '%s' callback for '%s'",
169 nb_operation_name(operation
), nb_node
->xpath
);
171 if (!optional
&& valid
&& !callback_implemented
) {
172 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
173 nb_operation_name(operation
), nb_node
->xpath
);
181 * Check if the required callbacks were implemented for the given northbound
184 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
187 unsigned int error
= 0;
189 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
190 !!nb_node
->cbs
.create
, false);
191 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
192 !!nb_node
->cbs
.modify
, false);
193 error
+= nb_node_validate_cb(nb_node
, NB_OP_DESTROY
,
194 !!nb_node
->cbs
.destroy
, false);
195 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
197 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
198 !!nb_node
->cbs
.apply_finish
, true);
199 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
200 !!nb_node
->cbs
.get_elem
, false);
201 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
202 !!nb_node
->cbs
.get_next
, false);
203 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
204 !!nb_node
->cbs
.get_keys
, false);
205 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
206 !!nb_node
->cbs
.lookup_entry
, false);
207 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
213 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
215 /* Top-level nodes can have any priority. */
216 if (!nb_node
->parent
)
219 if (nb_node
->priority
< nb_node
->parent
->priority
) {
220 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
221 "node has higher priority than its parent [xpath %s]",
229 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
231 struct nb_node
*nb_node
= snode
->priv
;
232 unsigned int *errors
= arg
;
234 /* Validate callbacks and priority. */
235 *errors
+= nb_node_validate_cbs(nb_node
);
236 *errors
+= nb_node_validate_priority(nb_node
);
238 return YANG_ITER_CONTINUE
;
241 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
243 struct nb_config
*config
;
245 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
247 config
->dnode
= dnode
;
249 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
255 void nb_config_free(struct nb_config
*config
)
258 yang_dnode_free(config
->dnode
);
259 XFREE(MTYPE_NB_CONFIG
, config
);
262 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
264 struct nb_config
*dup
;
266 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
267 dup
->dnode
= yang_dnode_dup(config
->dnode
);
268 dup
->version
= config
->version
;
273 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
274 bool preserve_source
)
278 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
280 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
282 if (!preserve_source
)
283 nb_config_free(config_src
);
285 return (ret
== 0) ? NB_OK
: NB_ERR
;
288 void nb_config_replace(struct nb_config
*config_dst
,
289 struct nb_config
*config_src
, bool preserve_source
)
291 /* Update version. */
292 if (config_src
->version
!= 0)
293 config_dst
->version
= config_src
->version
;
296 if (config_dst
->dnode
)
297 yang_dnode_free(config_dst
->dnode
);
298 if (preserve_source
) {
299 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
301 config_dst
->dnode
= config_src
->dnode
;
302 config_src
->dnode
= NULL
;
303 nb_config_free(config_src
);
307 /* Generate the nb_config_cbs tree. */
308 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
309 const struct nb_config_cb
*b
)
311 /* Sort by priority first. */
312 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
314 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
318 * Use XPath as a tie-breaker. This will naturally sort parent nodes
319 * before their children.
321 return strcmp(a
->xpath
, b
->xpath
);
323 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
325 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
326 enum nb_operation operation
,
327 const struct lyd_node
*dnode
)
329 struct nb_config_change
*change
;
331 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
332 change
->cb
.operation
= operation
;
333 change
->cb
.nb_node
= dnode
->schema
->priv
;
334 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
335 change
->cb
.dnode
= dnode
;
337 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
340 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
342 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
343 struct nb_config_change
*change
;
345 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
347 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
348 XFREE(MTYPE_TMP
, change
);
353 * Helper function used when calculating the delta between two different
354 * configurations. Given a new subtree, calculate all new YANG data nodes,
355 * excluding default leafs and leaf-lists. This is a recursive function.
357 static void nb_config_diff_created(const struct lyd_node
*dnode
,
358 struct nb_config_cbs
*changes
)
360 enum nb_operation operation
;
361 struct lyd_node
*child
;
363 switch (dnode
->schema
->nodetype
) {
366 if (lyd_wd_default((struct lyd_node_leaf_list
*)dnode
))
369 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
370 operation
= NB_OP_CREATE
;
371 else if (nb_operation_is_valid(NB_OP_MODIFY
, dnode
->schema
))
372 operation
= NB_OP_MODIFY
;
376 nb_config_diff_add_change(changes
, operation
, dnode
);
380 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
381 nb_config_diff_add_change(changes
, NB_OP_CREATE
, dnode
);
383 /* Process child nodes recursively. */
384 LY_TREE_FOR (dnode
->child
, child
) {
385 nb_config_diff_created(child
, changes
);
393 static void nb_config_diff_deleted(const struct lyd_node
*dnode
,
394 struct nb_config_cbs
*changes
)
396 if (nb_operation_is_valid(NB_OP_DESTROY
, dnode
->schema
))
397 nb_config_diff_add_change(changes
, NB_OP_DESTROY
, dnode
);
398 else if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_CONTAINER
)) {
399 struct lyd_node
*child
;
402 * Non-presence containers need special handling since they
403 * don't have "destroy" callbacks. In this case, what we need to
404 * do is to call the "destroy" callbacks of their child nodes
405 * when applicable (i.e. optional nodes).
407 LY_TREE_FOR (dnode
->child
, child
) {
408 nb_config_diff_deleted(child
, changes
);
413 /* Calculate the delta between two different configurations. */
414 static void nb_config_diff(const struct nb_config
*config1
,
415 const struct nb_config
*config2
,
416 struct nb_config_cbs
*changes
)
418 struct lyd_difflist
*diff
;
420 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
421 LYD_DIFFOPT_WITHDEFAULTS
);
424 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
426 struct lyd_node
*dnode
;
428 type
= diff
->type
[i
];
431 case LYD_DIFF_CREATED
:
432 dnode
= diff
->second
[i
];
433 nb_config_diff_created(dnode
, changes
);
435 case LYD_DIFF_DELETED
:
436 dnode
= diff
->first
[i
];
437 nb_config_diff_deleted(dnode
, changes
);
439 case LYD_DIFF_CHANGED
:
440 dnode
= diff
->second
[i
];
441 nb_config_diff_add_change(changes
, NB_OP_MODIFY
, dnode
);
443 case LYD_DIFF_MOVEDAFTER1
:
444 case LYD_DIFF_MOVEDAFTER2
:
453 int nb_candidate_edit(struct nb_config
*candidate
,
454 const struct nb_node
*nb_node
,
455 enum nb_operation operation
, const char *xpath
,
456 const struct yang_data
*previous
,
457 const struct yang_data
*data
)
459 struct lyd_node
*dnode
;
460 char xpath_edit
[XPATH_MAXLEN
];
462 if (!nb_operation_is_valid(operation
, nb_node
->snode
)) {
463 flog_warn(EC_LIB_NB_CANDIDATE_EDIT_ERROR
,
464 "%s: %s operation not valid for %s", __func__
,
465 nb_operation_name(operation
), xpath
);
469 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
470 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
471 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
474 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
480 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
481 xpath_edit
, (void *)data
->value
, 0,
482 LYD_PATH_OPT_UPDATE
);
483 if (!dnode
&& ly_errno
) {
484 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
490 * If a new node was created, call lyd_validate() only to create
491 * default child nodes.
494 lyd_schema_sort(dnode
, 0);
495 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
499 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
502 * Return a special error code so the caller can choose
503 * whether to ignore it or not.
505 return NB_ERR_NOT_FOUND
;
509 /* TODO: update configuration. */
512 flog_warn(EC_LIB_DEVELOPMENT
,
513 "%s: unknown operation (%u) [xpath %s]", __func__
,
514 operation
, xpath_edit
);
521 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
523 if (candidate
->version
< running_config
->version
)
529 int nb_candidate_update(struct nb_config
*candidate
)
531 struct nb_config
*updated_config
;
533 updated_config
= nb_config_dup(running_config
);
534 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
537 nb_config_replace(candidate
, updated_config
, false);
543 * Perform YANG syntactic and semantic validation.
545 * WARNING: lyd_validate() can change the configuration as part of the
546 * validation process.
548 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
550 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
553 return NB_ERR_VALIDATION
;
558 /* Perform code-level validation using the northbound callbacks. */
559 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
560 struct nb_config_cbs
*changes
)
562 struct nb_config_cb
*cb
;
564 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
565 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
568 ret
= nb_callback_configuration(NB_EV_VALIDATE
, change
);
570 return NB_ERR_VALIDATION
;
576 int nb_candidate_validate(struct nb_config
*candidate
)
578 struct nb_config_cbs changes
;
581 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
582 return NB_ERR_VALIDATION
;
584 RB_INIT(nb_config_cbs
, &changes
);
585 nb_config_diff(running_config
, candidate
, &changes
);
586 ret
= nb_candidate_validate_changes(candidate
, &changes
);
587 nb_config_diff_del_changes(&changes
);
592 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
593 enum nb_client client
, const char *comment
,
594 struct nb_transaction
**transaction
)
596 struct nb_config_cbs changes
;
598 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
599 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
600 "%s: failed to validate candidate configuration",
602 return NB_ERR_VALIDATION
;
605 RB_INIT(nb_config_cbs
, &changes
);
606 nb_config_diff(running_config
, candidate
, &changes
);
607 if (RB_EMPTY(nb_config_cbs
, &changes
))
608 return NB_ERR_NO_CHANGES
;
610 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
611 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
612 "%s: failed to validate candidate configuration",
614 nb_config_diff_del_changes(&changes
);
615 return NB_ERR_VALIDATION
;
618 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
619 if (*transaction
== NULL
) {
620 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
621 "%s: failed to create transaction", __func__
);
622 nb_config_diff_del_changes(&changes
);
623 return NB_ERR_LOCKED
;
626 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
629 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
631 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
632 nb_transaction_free(transaction
);
635 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
636 bool save_transaction
, uint32_t *transaction_id
)
638 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
639 nb_transaction_apply_finish(transaction
);
641 /* Replace running by candidate. */
642 transaction
->config
->version
++;
643 nb_config_replace(running_config
, transaction
->config
, true);
645 /* Record transaction. */
647 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
648 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
649 "%s: failed to record transaction", __func__
);
651 nb_transaction_free(transaction
);
654 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
655 bool save_transaction
, const char *comment
,
656 uint32_t *transaction_id
)
658 struct nb_transaction
*transaction
= NULL
;
661 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
664 * Apply the changes if the preparation phase succeeded. Otherwise abort
668 nb_candidate_commit_apply(transaction
, save_transaction
,
670 else if (transaction
!= NULL
)
671 nb_candidate_commit_abort(transaction
);
676 static void nb_log_callback(const enum nb_event event
,
677 enum nb_operation operation
, const char *xpath
,
681 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
682 nb_event_name(event
), nb_operation_name(operation
), xpath
,
687 * Call the northbound configuration callback associated to a given
688 * configuration change.
690 static int nb_callback_configuration(const enum nb_event event
,
691 struct nb_config_change
*change
)
693 enum nb_operation operation
= change
->cb
.operation
;
694 const char *xpath
= change
->cb
.xpath
;
695 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
696 const struct lyd_node
*dnode
= change
->cb
.dnode
;
697 union nb_resource
*resource
;
700 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
701 const char *value
= "(none)";
703 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
704 value
= yang_dnode_get_string(dnode
, NULL
);
706 nb_log_callback(event
, operation
, xpath
, value
);
709 if (event
== NB_EV_VALIDATE
)
712 resource
= &change
->resource
;
716 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
719 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
722 ret
= (*nb_node
->cbs
.destroy
)(event
, dnode
);
725 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
732 enum lib_log_refs ref
= 0;
736 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
739 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
742 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
745 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
748 if (event
== NB_EV_VALIDATE
|| event
== NB_EV_PREPARE
)
751 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
752 __func__
, nb_err_name(ret
),
753 nb_event_name(event
),
754 nb_operation_name(operation
), xpath
);
758 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
759 __func__
, nb_err_name(ret
),
760 nb_event_name(event
),
761 nb_operation_name(operation
), xpath
);
767 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
769 const void *list_entry
)
771 DEBUGD(&nb_dbg_cbs_state
,
772 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
775 return nb_node
->cbs
.get_elem(xpath
, list_entry
);
778 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
779 const void *parent_list_entry
,
780 const void *list_entry
)
782 DEBUGD(&nb_dbg_cbs_state
,
783 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
784 nb_node
->xpath
, parent_list_entry
, list_entry
);
786 return nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
789 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
790 struct yang_list_keys
*keys
)
792 DEBUGD(&nb_dbg_cbs_state
,
793 "northbound callback (get_keys): node [%s] list_entry [%p]",
794 nb_node
->xpath
, list_entry
);
796 return nb_node
->cbs
.get_keys(list_entry
, keys
);
799 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
800 const void *parent_list_entry
,
801 const struct yang_list_keys
*keys
)
803 DEBUGD(&nb_dbg_cbs_state
,
804 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
805 nb_node
->xpath
, parent_list_entry
);
807 return nb_node
->cbs
.lookup_entry(parent_list_entry
, keys
);
810 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
811 const struct list
*input
, struct list
*output
)
813 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
815 return nb_node
->cbs
.rpc(xpath
, input
, output
);
818 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
819 struct nb_config_cbs
*changes
,
820 enum nb_client client
,
823 struct nb_transaction
*transaction
;
825 if (transaction_in_progress
) {
827 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
828 "%s: error - there's already another transaction in progress",
832 transaction_in_progress
= true;
834 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
835 transaction
->client
= client
;
837 strlcpy(transaction
->comment
, comment
,
838 sizeof(transaction
->comment
));
839 transaction
->config
= config
;
840 transaction
->changes
= *changes
;
845 static void nb_transaction_free(struct nb_transaction
*transaction
)
847 nb_config_diff_del_changes(&transaction
->changes
);
848 XFREE(MTYPE_TMP
, transaction
);
849 transaction_in_progress
= false;
852 /* Process all configuration changes associated to a transaction. */
853 static int nb_transaction_process(enum nb_event event
,
854 struct nb_transaction
*transaction
)
856 struct nb_config_cb
*cb
;
858 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
859 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
863 * Only try to release resources that were allocated
866 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
869 /* Call the appropriate callback. */
870 ret
= nb_callback_configuration(event
, change
);
875 change
->prepare_ok
= true;
880 * At this point it's not possible to reject the
881 * transaction anymore, so any failure here can lead to
882 * inconsistencies and should be treated as a bug.
883 * Operations prone to errors, like validations and
884 * resource allocations, should be performed during the
896 static struct nb_config_cb
*
897 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
898 const struct nb_node
*nb_node
,
899 const struct lyd_node
*dnode
)
901 struct nb_config_cb
*cb
;
903 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
904 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
905 cb
->nb_node
= nb_node
;
907 RB_INSERT(nb_config_cbs
, cbs
, cb
);
912 static struct nb_config_cb
*
913 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
914 const struct nb_node
*nb_node
)
916 struct nb_config_cb s
;
918 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
920 return RB_FIND(nb_config_cbs
, cbs
, &s
);
923 /* Call the 'apply_finish' callbacks. */
924 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
926 struct nb_config_cbs cbs
;
927 struct nb_config_cb
*cb
;
929 /* Initialize tree of 'apply_finish' callbacks. */
930 RB_INIT(nb_config_cbs
, &cbs
);
932 /* Identify the 'apply_finish' callbacks that need to be called. */
933 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
934 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
935 const struct lyd_node
*dnode
= change
->cb
.dnode
;
938 * Iterate up to the root of the data tree. When a node is being
939 * deleted, skip its 'apply_finish' callback if one is defined
940 * (the 'apply_finish' callbacks from the node ancestors should
943 if (change
->cb
.operation
== NB_OP_DESTROY
) {
944 char xpath
[XPATH_MAXLEN
];
946 dnode
= dnode
->parent
;
951 * The dnode from 'delete' callbacks point to elements
952 * from the running configuration. Use yang_dnode_get()
953 * to get the corresponding dnode from the candidate
954 * configuration that is being committed.
956 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
957 dnode
= yang_dnode_get(transaction
->config
->dnode
,
961 char xpath
[XPATH_MAXLEN
];
962 struct nb_node
*nb_node
;
964 nb_node
= dnode
->schema
->priv
;
965 if (!nb_node
->cbs
.apply_finish
)
969 * Don't call the callback more than once for the same
972 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
973 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
976 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
979 dnode
= dnode
->parent
;
983 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
984 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
985 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
))
986 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
989 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
992 /* Release memory. */
993 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
994 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
995 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
996 XFREE(MTYPE_TMP
, cb
);
1000 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
1001 const char *xpath
, const void *list_entry
,
1002 const struct yang_list_keys
*list_keys
,
1003 struct yang_translator
*translator
,
1004 bool first
, uint32_t flags
,
1005 nb_oper_data_cb cb
, void *arg
)
1007 struct lys_node
*child
;
1009 LY_TREE_FOR (snode
->child
, child
) {
1012 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1013 list_keys
, translator
, false,
1022 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1023 const char *xpath
, const void *list_entry
,
1024 const struct yang_list_keys
*list_keys
,
1025 struct yang_translator
*translator
,
1026 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1028 struct yang_data
*data
;
1030 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1033 /* Ignore list keys. */
1034 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1037 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1039 /* Leaf of type "empty" is not present. */
1042 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1045 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1047 const void *list_entry
,
1048 const struct yang_list_keys
*list_keys
,
1049 struct yang_translator
*translator
,
1050 uint32_t flags
, nb_oper_data_cb cb
,
1053 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1056 /* Presence containers. */
1057 if (nb_node
->cbs
.get_elem
) {
1058 struct yang_data
*data
;
1061 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1063 /* Presence container is not present. */
1066 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1071 /* Iterate over the child nodes. */
1072 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1073 list_keys
, translator
, false, flags
,
1078 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1079 const void *parent_list_entry
,
1080 const struct yang_list_keys
*parent_list_keys
,
1081 struct yang_translator
*translator
, uint32_t flags
,
1082 nb_oper_data_cb cb
, void *arg
)
1084 const void *list_entry
= NULL
;
1086 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1090 struct yang_data
*data
;
1093 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1096 /* End of the list. */
1099 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1103 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1106 } while (list_entry
);
1111 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1112 const char *xpath_list
,
1113 const void *parent_list_entry
,
1114 const struct yang_list_keys
*parent_list_keys
,
1115 struct yang_translator
*translator
,
1116 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1118 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1119 const void *list_entry
= NULL
;
1120 uint32_t position
= 1;
1122 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1125 /* Iterate over all list entries. */
1127 struct yang_list_keys list_keys
;
1128 char xpath
[XPATH_MAXLEN
* 2];
1131 /* Obtain list entry. */
1132 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1135 /* End of the list. */
1138 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1139 /* Obtain the list entry keys. */
1140 if (nb_callback_get_keys(nb_node
, list_entry
,
1143 flog_warn(EC_LIB_NB_CB_STATE
,
1144 "%s: failed to get list keys",
1149 /* Build XPath of the list entry. */
1150 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1151 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1152 snprintf(xpath
+ strlen(xpath
),
1153 sizeof(xpath
) - strlen(xpath
),
1154 "[%s='%s']", slist
->keys
[i
]->name
,
1159 * Keyless list - build XPath using a positional index.
1161 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1166 /* Iterate over the child nodes. */
1167 ret
= nb_oper_data_iter_children(
1168 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1169 translator
, false, flags
, cb
, arg
);
1172 } while (list_entry
);
1177 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1178 const char *xpath_parent
,
1179 const void *list_entry
,
1180 const struct yang_list_keys
*list_keys
,
1181 struct yang_translator
*translator
,
1182 bool first
, uint32_t flags
,
1183 nb_oper_data_cb cb
, void *arg
)
1185 struct nb_node
*nb_node
;
1186 char xpath
[XPATH_MAXLEN
];
1189 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1190 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1194 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1195 if (!first
&& snode
->nodetype
!= LYS_USES
)
1196 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1197 "/%s", snode
->name
);
1199 nb_node
= snode
->priv
;
1200 switch (snode
->nodetype
) {
1202 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1203 list_keys
, translator
, flags
,
1207 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1208 list_keys
, translator
, flags
, cb
,
1212 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1213 list_keys
, translator
, flags
,
1217 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1218 list_keys
, translator
, flags
, cb
,
1222 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1223 list_keys
, translator
, false,
1233 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1234 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1236 struct nb_node
*nb_node
;
1237 const void *list_entry
= NULL
;
1238 struct yang_list_keys list_keys
;
1239 struct list
*list_dnodes
;
1240 struct lyd_node
*dnode
, *dn
;
1241 struct listnode
*ln
;
1244 nb_node
= nb_node_find(xpath
);
1246 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1247 "%s: unknown data path: %s", __func__
, xpath
);
1251 /* For now this function works only with containers and lists. */
1252 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1254 EC_LIB_NB_OPERATIONAL_DATA
,
1255 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1261 * Create a data tree from the XPath so that we can parse the keys of
1262 * all YANG lists (if any).
1265 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1266 LYD_PATH_OPT_UPDATE
);
1267 if (!dnode
&& ly_errno
) {
1268 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1273 * We can remove the following two lines once we depend on
1274 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1277 dnode
= yang_dnode_get(dnode
, xpath
);
1281 * Create a linked list to sort the data nodes starting from the root.
1283 list_dnodes
= list_new();
1284 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1285 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1287 listnode_add_head(list_dnodes
, dn
);
1290 * Use the northbound callbacks to find list entry pointer corresponding
1291 * to the given XPath.
1293 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1294 struct lyd_node
*child
;
1298 /* Obtain the list entry keys. */
1299 memset(&list_keys
, 0, sizeof(list_keys
));
1300 LY_TREE_FOR (dn
->child
, child
) {
1301 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1304 strlcpy(list_keys
.key
[n
],
1305 yang_dnode_get_string(child
, NULL
),
1306 sizeof(list_keys
.key
[n
]));
1311 != ((struct lys_node_list
*)dn
->schema
)->keys_size
) {
1312 list_delete(&list_dnodes
);
1313 yang_dnode_free(dnode
);
1314 return NB_ERR_NOT_FOUND
;
1317 /* Find the list entry pointer. */
1318 nn
= dn
->schema
->priv
;
1320 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
1321 if (list_entry
== NULL
) {
1322 list_delete(&list_dnodes
);
1323 yang_dnode_free(dnode
);
1324 return NB_ERR_NOT_FOUND
;
1328 /* If a list entry was given, iterate over that list entry only. */
1329 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1330 ret
= nb_oper_data_iter_children(
1331 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1332 translator
, true, flags
, cb
, arg
);
1334 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1335 &list_keys
, translator
, true,
1338 list_delete(&list_dnodes
);
1339 yang_dnode_free(dnode
);
1344 bool nb_operation_is_valid(enum nb_operation operation
,
1345 const struct lys_node
*snode
)
1347 struct nb_node
*nb_node
= snode
->priv
;
1348 struct lys_node_container
*scontainer
;
1349 struct lys_node_leaf
*sleaf
;
1351 switch (operation
) {
1353 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1356 switch (snode
->nodetype
) {
1358 sleaf
= (struct lys_node_leaf
*)snode
;
1359 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1363 scontainer
= (struct lys_node_container
*)snode
;
1364 if (!scontainer
->presence
)
1375 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1378 switch (snode
->nodetype
) {
1380 sleaf
= (struct lys_node_leaf
*)snode
;
1381 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1384 /* List keys can't be modified. */
1385 if (lys_is_key(sleaf
, NULL
))
1393 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1396 switch (snode
->nodetype
) {
1398 sleaf
= (struct lys_node_leaf
*)snode
;
1400 /* List keys can't be deleted. */
1401 if (lys_is_key(sleaf
, NULL
))
1405 * Only optional leafs can be deleted, or leafs whose
1406 * parent is a case statement.
1408 if (snode
->parent
->nodetype
== LYS_CASE
)
1412 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1417 scontainer
= (struct lys_node_container
*)snode
;
1418 if (!scontainer
->presence
)
1429 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1432 switch (snode
->nodetype
) {
1435 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1442 case NB_OP_APPLY_FINISH
:
1443 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1446 case NB_OP_GET_ELEM
:
1447 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1450 switch (snode
->nodetype
) {
1455 scontainer
= (struct lys_node_container
*)snode
;
1456 if (!scontainer
->presence
)
1463 case NB_OP_GET_NEXT
:
1464 switch (snode
->nodetype
) {
1466 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1470 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1477 case NB_OP_GET_KEYS
:
1478 case NB_OP_LOOKUP_ENTRY
:
1479 switch (snode
->nodetype
) {
1481 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1483 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1491 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1494 switch (snode
->nodetype
) {
1507 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1508 (xpath
, arguments
));
1510 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1514 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
1516 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1518 list_delete(&arguments
);
1523 /* Running configuration user pointers management. */
1524 struct nb_config_entry
{
1525 char xpath
[XPATH_MAXLEN
];
1529 static bool running_config_entry_cmp(const void *value1
, const void *value2
)
1531 const struct nb_config_entry
*c1
= value1
;
1532 const struct nb_config_entry
*c2
= value2
;
1534 return strmatch(c1
->xpath
, c2
->xpath
);
1537 static unsigned int running_config_entry_key_make(void *value
)
1539 return string_hash_make(value
);
1542 static void *running_config_entry_alloc(void *p
)
1544 struct nb_config_entry
*new, *key
= p
;
1546 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY
, sizeof(*new));
1547 strlcpy(new->xpath
, key
->xpath
, sizeof(new->xpath
));
1552 static void running_config_entry_free(void *arg
)
1554 XFREE(MTYPE_NB_CONFIG_ENTRY
, arg
);
1557 void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
)
1559 struct nb_config_entry
*config
, s
;
1561 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1562 config
= hash_get(running_config_entries
, &s
,
1563 running_config_entry_alloc
);
1564 config
->entry
= entry
;
1567 static void *nb_running_unset_entry_helper(const struct lyd_node
*dnode
)
1569 struct nb_config_entry
*config
, s
;
1570 struct lyd_node
*child
;
1573 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1574 config
= hash_release(running_config_entries
, &s
);
1576 entry
= config
->entry
;
1577 running_config_entry_free(config
);
1580 /* Unset user pointers from the child nodes. */
1581 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
)) {
1582 LY_TREE_FOR (dnode
->child
, child
) {
1583 (void)nb_running_unset_entry_helper(child
);
1590 void *nb_running_unset_entry(const struct lyd_node
*dnode
)
1594 entry
= nb_running_unset_entry_helper(dnode
);
1600 void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
1601 bool abort_if_not_found
)
1603 const struct lyd_node
*orig_dnode
= dnode
;
1604 char xpath_buf
[XPATH_MAXLEN
];
1606 assert(dnode
|| xpath
);
1609 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
1612 struct nb_config_entry
*config
, s
;
1614 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1615 config
= hash_lookup(running_config_entries
, &s
);
1617 return config
->entry
;
1619 dnode
= dnode
->parent
;
1622 if (!abort_if_not_found
)
1625 yang_dnode_get_path(orig_dnode
, xpath_buf
, sizeof(xpath_buf
));
1626 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
1627 "%s: failed to find entry [xpath %s]", __func__
, xpath_buf
);
1628 zlog_backtrace(LOG_ERR
);
1632 /* Logging functions. */
1633 const char *nb_event_name(enum nb_event event
)
1636 case NB_EV_VALIDATE
:
1649 const char *nb_operation_name(enum nb_operation operation
)
1651 switch (operation
) {
1660 case NB_OP_APPLY_FINISH
:
1661 return "apply_finish";
1662 case NB_OP_GET_ELEM
:
1664 case NB_OP_GET_NEXT
:
1666 case NB_OP_GET_KEYS
:
1668 case NB_OP_LOOKUP_ENTRY
:
1669 return "lookup_entry";
1677 const char *nb_err_name(enum nb_error error
)
1683 return "generic error";
1684 case NB_ERR_NO_CHANGES
:
1685 return "no changes";
1686 case NB_ERR_NOT_FOUND
:
1687 return "element not found";
1689 return "resource is locked";
1690 case NB_ERR_VALIDATION
:
1691 return "validation error";
1692 case NB_ERR_RESOURCE
:
1693 return "failed to allocate resource";
1694 case NB_ERR_INCONSISTENCY
:
1695 return "internal inconsistency";
1701 const char *nb_client_name(enum nb_client client
)
1706 case NB_CLIENT_CONFD
:
1708 case NB_CLIENT_SYSREPO
:
1715 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1717 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1718 struct nb_node
*nb_node
;
1721 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1723 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1724 "%s: unknown data path: %s", __func__
,
1725 module
->nodes
[i
].xpath
);
1729 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1730 priority
= module
->nodes
[i
].priority
;
1732 nb_node
->priority
= priority
;
1736 void nb_init(struct thread_master
*tm
,
1737 const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1739 unsigned int errors
= 0;
1741 /* Load YANG modules. */
1742 for (size_t i
= 0; i
< nmodules
; i
++)
1743 yang_module_load(modules
[i
]->name
);
1745 /* Create a nb_node for all YANG schema nodes. */
1748 /* Load northbound callbacks. */
1749 for (size_t i
= 0; i
< nmodules
; i
++)
1750 nb_load_callbacks(modules
[i
]);
1752 /* Validate northbound callbacks. */
1753 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1756 EC_LIB_NB_CBS_VALIDATION
,
1757 "%s: failed to validate northbound callbacks: %u error(s)",
1762 /* Initialize the northbound database (used for the rollback log). */
1763 if (nb_db_init() != NB_OK
)
1764 flog_warn(EC_LIB_NB_DATABASE
,
1765 "%s: failed to initialize northbound database",
1768 /* Create an empty running configuration. */
1769 running_config
= nb_config_new(NULL
);
1770 running_config_entries
= hash_create(running_config_entry_key_make
,
1771 running_config_entry_cmp
,
1772 "Running Configuration Entries");
1774 /* Initialize the northbound CLI. */
1778 void nb_terminate(void)
1780 /* Terminate the northbound CLI. */
1783 /* Delete all nb_node's from all YANG modules. */
1786 /* Delete the running configuration. */
1787 hash_clean(running_config_entries
, running_config_entry_free
);
1788 hash_free(running_config_entries
);
1789 nb_config_free(running_config
);