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 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
463 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
464 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
467 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
473 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
474 xpath_edit
, (void *)data
->value
, 0,
475 LYD_PATH_OPT_UPDATE
);
476 if (!dnode
&& ly_errno
) {
477 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
483 * If a new node was created, call lyd_validate() only to create
484 * default child nodes.
487 lyd_schema_sort(dnode
, 0);
488 lyd_validate(&dnode
, LYD_OPT_CONFIG
, ly_native_ctx
);
492 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
495 * Return a special error code so the caller can choose
496 * whether to ignore it or not.
498 return NB_ERR_NOT_FOUND
;
502 /* TODO: update configuration. */
505 flog_warn(EC_LIB_DEVELOPMENT
,
506 "%s: unknown operation (%u) [xpath %s]", __func__
,
507 operation
, xpath_edit
);
514 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
516 if (candidate
->version
< running_config
->version
)
522 int nb_candidate_update(struct nb_config
*candidate
)
524 struct nb_config
*updated_config
;
526 updated_config
= nb_config_dup(running_config
);
527 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
530 nb_config_replace(candidate
, updated_config
, false);
536 * Perform YANG syntactic and semantic validation.
538 * WARNING: lyd_validate() can change the configuration as part of the
539 * validation process.
541 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
543 if (lyd_validate(&candidate
->dnode
, LYD_OPT_STRICT
| LYD_OPT_CONFIG
,
546 return NB_ERR_VALIDATION
;
551 /* Perform code-level validation using the northbound callbacks. */
552 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
553 struct nb_config_cbs
*changes
)
555 struct nb_config_cb
*cb
;
557 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
558 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
561 ret
= nb_callback_configuration(NB_EV_VALIDATE
, change
);
563 return NB_ERR_VALIDATION
;
569 int nb_candidate_validate(struct nb_config
*candidate
)
571 struct nb_config_cbs changes
;
574 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
575 return NB_ERR_VALIDATION
;
577 RB_INIT(nb_config_cbs
, &changes
);
578 nb_config_diff(running_config
, candidate
, &changes
);
579 ret
= nb_candidate_validate_changes(candidate
, &changes
);
580 nb_config_diff_del_changes(&changes
);
585 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
586 enum nb_client client
, const char *comment
,
587 struct nb_transaction
**transaction
)
589 struct nb_config_cbs changes
;
591 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
592 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
593 "%s: failed to validate candidate configuration",
595 return NB_ERR_VALIDATION
;
598 RB_INIT(nb_config_cbs
, &changes
);
599 nb_config_diff(running_config
, candidate
, &changes
);
600 if (RB_EMPTY(nb_config_cbs
, &changes
))
601 return NB_ERR_NO_CHANGES
;
603 if (nb_candidate_validate_changes(candidate
, &changes
) != NB_OK
) {
604 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
605 "%s: failed to validate candidate configuration",
607 nb_config_diff_del_changes(&changes
);
608 return NB_ERR_VALIDATION
;
611 *transaction
= nb_transaction_new(candidate
, &changes
, client
, comment
);
612 if (*transaction
== NULL
) {
613 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
614 "%s: failed to create transaction", __func__
);
615 nb_config_diff_del_changes(&changes
);
616 return NB_ERR_LOCKED
;
619 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
622 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
624 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
625 nb_transaction_free(transaction
);
628 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
629 bool save_transaction
, uint32_t *transaction_id
)
631 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
632 nb_transaction_apply_finish(transaction
);
634 /* Replace running by candidate. */
635 transaction
->config
->version
++;
636 nb_config_replace(running_config
, transaction
->config
, true);
638 /* Record transaction. */
640 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
641 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
642 "%s: failed to record transaction", __func__
);
644 nb_transaction_free(transaction
);
647 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
648 bool save_transaction
, const char *comment
,
649 uint32_t *transaction_id
)
651 struct nb_transaction
*transaction
= NULL
;
654 ret
= nb_candidate_commit_prepare(candidate
, client
, comment
,
657 * Apply the changes if the preparation phase succeeded. Otherwise abort
661 nb_candidate_commit_apply(transaction
, save_transaction
,
663 else if (transaction
!= NULL
)
664 nb_candidate_commit_abort(transaction
);
669 static void nb_log_callback(const enum nb_event event
,
670 enum nb_operation operation
, const char *xpath
,
674 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
675 nb_event_name(event
), nb_operation_name(operation
), xpath
,
680 * Call the northbound configuration callback associated to a given
681 * configuration change.
683 static int nb_callback_configuration(const enum nb_event event
,
684 struct nb_config_change
*change
)
686 enum nb_operation operation
= change
->cb
.operation
;
687 const char *xpath
= change
->cb
.xpath
;
688 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
689 const struct lyd_node
*dnode
= change
->cb
.dnode
;
690 union nb_resource
*resource
;
693 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
694 const char *value
= "(none)";
696 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
697 value
= yang_dnode_get_string(dnode
, NULL
);
699 nb_log_callback(event
, operation
, xpath
, value
);
702 if (event
== NB_EV_VALIDATE
)
705 resource
= &change
->resource
;
709 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
712 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
715 ret
= (*nb_node
->cbs
.destroy
)(event
, dnode
);
718 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
721 flog_err(EC_LIB_DEVELOPMENT
,
722 "%s: unknown operation (%u) [xpath %s]", __func__
,
729 enum lib_log_refs ref
;
733 priority
= LOG_WARNING
;
734 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
737 priority
= LOG_WARNING
;
738 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
741 priority
= LOG_WARNING
;
742 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
746 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
749 flog_err(EC_LIB_DEVELOPMENT
,
750 "%s: unknown event (%u) [xpath %s]",
751 __func__
, event
, xpath
);
756 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
757 __func__
, nb_err_name(ret
), nb_event_name(event
),
758 nb_operation_name(operation
), xpath
);
764 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
766 const void *list_entry
)
768 DEBUGD(&nb_dbg_cbs_state
,
769 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
772 return nb_node
->cbs
.get_elem(xpath
, list_entry
);
775 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
776 const void *parent_list_entry
,
777 const void *list_entry
)
779 DEBUGD(&nb_dbg_cbs_state
,
780 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
781 nb_node
->xpath
, parent_list_entry
, list_entry
);
783 return nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
786 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
787 struct yang_list_keys
*keys
)
789 DEBUGD(&nb_dbg_cbs_state
,
790 "northbound callback (get_keys): node [%s] list_entry [%p]",
791 nb_node
->xpath
, list_entry
);
793 return nb_node
->cbs
.get_keys(list_entry
, keys
);
796 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
797 const void *parent_list_entry
,
798 const struct yang_list_keys
*keys
)
800 DEBUGD(&nb_dbg_cbs_state
,
801 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
802 nb_node
->xpath
, parent_list_entry
);
804 return nb_node
->cbs
.lookup_entry(parent_list_entry
, keys
);
807 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
808 const struct list
*input
, struct list
*output
)
810 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
812 return nb_node
->cbs
.rpc(xpath
, input
, output
);
815 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
816 struct nb_config_cbs
*changes
,
817 enum nb_client client
,
820 struct nb_transaction
*transaction
;
822 if (transaction_in_progress
) {
824 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
825 "%s: error - there's already another transaction in progress",
829 transaction_in_progress
= true;
831 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
832 transaction
->client
= client
;
834 strlcpy(transaction
->comment
, comment
,
835 sizeof(transaction
->comment
));
836 transaction
->config
= config
;
837 transaction
->changes
= *changes
;
842 static void nb_transaction_free(struct nb_transaction
*transaction
)
844 nb_config_diff_del_changes(&transaction
->changes
);
845 XFREE(MTYPE_TMP
, transaction
);
846 transaction_in_progress
= false;
849 /* Process all configuration changes associated to a transaction. */
850 static int nb_transaction_process(enum nb_event event
,
851 struct nb_transaction
*transaction
)
853 struct nb_config_cb
*cb
;
855 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
856 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
860 * Only try to release resources that were allocated
863 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
866 /* Call the appropriate callback. */
867 ret
= nb_callback_configuration(event
, change
);
872 change
->prepare_ok
= true;
877 * At this point it's not possible to reject the
878 * transaction anymore, so any failure here can lead to
879 * inconsistencies and should be treated as a bug.
880 * Operations prone to errors, like validations and
881 * resource allocations, should be performed during the
893 static struct nb_config_cb
*
894 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
895 const struct nb_node
*nb_node
,
896 const struct lyd_node
*dnode
)
898 struct nb_config_cb
*cb
;
900 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
901 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
902 cb
->nb_node
= nb_node
;
904 RB_INSERT(nb_config_cbs
, cbs
, cb
);
909 static struct nb_config_cb
*
910 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
911 const struct nb_node
*nb_node
)
913 struct nb_config_cb s
;
915 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
917 return RB_FIND(nb_config_cbs
, cbs
, &s
);
920 /* Call the 'apply_finish' callbacks. */
921 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
923 struct nb_config_cbs cbs
;
924 struct nb_config_cb
*cb
;
926 /* Initialize tree of 'apply_finish' callbacks. */
927 RB_INIT(nb_config_cbs
, &cbs
);
929 /* Identify the 'apply_finish' callbacks that need to be called. */
930 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
931 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
932 const struct lyd_node
*dnode
= change
->cb
.dnode
;
935 * Iterate up to the root of the data tree. When a node is being
936 * deleted, skip its 'apply_finish' callback if one is defined
937 * (the 'apply_finish' callbacks from the node ancestors should
940 if (change
->cb
.operation
== NB_OP_DESTROY
) {
941 char xpath
[XPATH_MAXLEN
];
943 dnode
= dnode
->parent
;
948 * The dnode from 'delete' callbacks point to elements
949 * from the running configuration. Use yang_dnode_get()
950 * to get the corresponding dnode from the candidate
951 * configuration that is being committed.
953 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
954 dnode
= yang_dnode_get(transaction
->config
->dnode
,
958 char xpath
[XPATH_MAXLEN
];
959 struct nb_node
*nb_node
;
961 nb_node
= dnode
->schema
->priv
;
962 if (!nb_node
->cbs
.apply_finish
)
966 * Don't call the callback more than once for the same
969 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
970 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
973 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
976 dnode
= dnode
->parent
;
980 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
981 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
982 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
))
983 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
986 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
989 /* Release memory. */
990 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
991 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
992 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
993 XFREE(MTYPE_TMP
, cb
);
997 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
998 const char *xpath
, const void *list_entry
,
999 const struct yang_list_keys
*list_keys
,
1000 struct yang_translator
*translator
,
1001 bool first
, uint32_t flags
,
1002 nb_oper_data_cb cb
, void *arg
)
1004 struct lys_node
*child
;
1006 LY_TREE_FOR (snode
->child
, child
) {
1009 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1010 list_keys
, translator
, false,
1019 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1020 const char *xpath
, const void *list_entry
,
1021 const struct yang_list_keys
*list_keys
,
1022 struct yang_translator
*translator
,
1023 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1025 struct yang_data
*data
;
1027 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1030 /* Ignore list keys. */
1031 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1034 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1036 /* Leaf of type "empty" is not present. */
1039 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1042 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1044 const void *list_entry
,
1045 const struct yang_list_keys
*list_keys
,
1046 struct yang_translator
*translator
,
1047 uint32_t flags
, nb_oper_data_cb cb
,
1050 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1053 /* Presence containers. */
1054 if (nb_node
->cbs
.get_elem
) {
1055 struct yang_data
*data
;
1058 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1060 /* Presence container is not present. */
1063 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1068 /* Iterate over the child nodes. */
1069 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1070 list_keys
, translator
, false, flags
,
1075 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1076 const void *parent_list_entry
,
1077 const struct yang_list_keys
*parent_list_keys
,
1078 struct yang_translator
*translator
, uint32_t flags
,
1079 nb_oper_data_cb cb
, void *arg
)
1081 const void *list_entry
= NULL
;
1083 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1087 struct yang_data
*data
;
1090 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1093 /* End of the list. */
1096 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1100 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1103 } while (list_entry
);
1108 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1109 const char *xpath_list
,
1110 const void *parent_list_entry
,
1111 const struct yang_list_keys
*parent_list_keys
,
1112 struct yang_translator
*translator
,
1113 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1115 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1116 const void *list_entry
= NULL
;
1117 uint32_t position
= 1;
1119 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1122 /* Iterate over all list entries. */
1124 struct yang_list_keys list_keys
;
1125 char xpath
[XPATH_MAXLEN
* 2];
1128 /* Obtain list entry. */
1129 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1132 /* End of the list. */
1135 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1136 /* Obtain the list entry keys. */
1137 if (nb_callback_get_keys(nb_node
, list_entry
,
1140 flog_warn(EC_LIB_NB_CB_STATE
,
1141 "%s: failed to get list keys",
1146 /* Build XPath of the list entry. */
1147 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1148 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1149 snprintf(xpath
+ strlen(xpath
),
1150 sizeof(xpath
) - strlen(xpath
),
1151 "[%s='%s']", slist
->keys
[i
]->name
,
1156 * Keyless list - build XPath using a positional index.
1158 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1163 /* Iterate over the child nodes. */
1164 ret
= nb_oper_data_iter_children(
1165 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1166 translator
, false, flags
, cb
, arg
);
1169 } while (list_entry
);
1174 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1175 const char *xpath_parent
,
1176 const void *list_entry
,
1177 const struct yang_list_keys
*list_keys
,
1178 struct yang_translator
*translator
,
1179 bool first
, uint32_t flags
,
1180 nb_oper_data_cb cb
, void *arg
)
1182 struct nb_node
*nb_node
;
1183 char xpath
[XPATH_MAXLEN
];
1186 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1187 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1191 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1192 if (!first
&& snode
->nodetype
!= LYS_USES
)
1193 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1194 "/%s", snode
->name
);
1196 nb_node
= snode
->priv
;
1197 switch (snode
->nodetype
) {
1199 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1200 list_keys
, translator
, flags
,
1204 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1205 list_keys
, translator
, flags
, cb
,
1209 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1210 list_keys
, translator
, flags
,
1214 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1215 list_keys
, translator
, flags
, cb
,
1219 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1220 list_keys
, translator
, false,
1230 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1231 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1233 struct nb_node
*nb_node
;
1234 const void *list_entry
= NULL
;
1235 struct yang_list_keys list_keys
;
1236 struct list
*list_dnodes
;
1237 struct lyd_node
*dnode
, *dn
;
1238 struct listnode
*ln
;
1241 nb_node
= nb_node_find(xpath
);
1243 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1244 "%s: unknown data path: %s", __func__
, xpath
);
1248 /* For now this function works only with containers and lists. */
1249 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1251 EC_LIB_NB_OPERATIONAL_DATA
,
1252 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1258 * Create a data tree from the XPath so that we can parse the keys of
1259 * all YANG lists (if any).
1262 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1263 LYD_PATH_OPT_UPDATE
);
1264 if (!dnode
&& ly_errno
) {
1265 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1270 * We can remove the following two lines once we depend on
1271 * libyang-v0.16-r2, which has the LYD_PATH_OPT_NOPARENTRET flag for
1274 dnode
= yang_dnode_get(dnode
, xpath
);
1278 * Create a linked list to sort the data nodes starting from the root.
1280 list_dnodes
= list_new();
1281 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1282 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1284 listnode_add_head(list_dnodes
, dn
);
1287 * Use the northbound callbacks to find list entry pointer corresponding
1288 * to the given XPath.
1290 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1291 struct lyd_node
*child
;
1295 /* Obtain the list entry keys. */
1296 memset(&list_keys
, 0, sizeof(list_keys
));
1297 LY_TREE_FOR (dn
->child
, child
) {
1298 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1301 strlcpy(list_keys
.key
[n
],
1302 yang_dnode_get_string(child
, NULL
),
1303 sizeof(list_keys
.key
[n
]));
1308 != ((struct lys_node_list
*)dn
->schema
)->keys_size
) {
1309 list_delete(&list_dnodes
);
1310 yang_dnode_free(dnode
);
1311 return NB_ERR_NOT_FOUND
;
1314 /* Find the list entry pointer. */
1315 nn
= dn
->schema
->priv
;
1317 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
1318 if (list_entry
== NULL
) {
1319 list_delete(&list_dnodes
);
1320 yang_dnode_free(dnode
);
1321 return NB_ERR_NOT_FOUND
;
1325 /* If a list entry was given, iterate over that list entry only. */
1326 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1327 ret
= nb_oper_data_iter_children(
1328 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1329 translator
, true, flags
, cb
, arg
);
1331 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1332 &list_keys
, translator
, true,
1335 list_delete(&list_dnodes
);
1336 yang_dnode_free(dnode
);
1341 bool nb_operation_is_valid(enum nb_operation operation
,
1342 const struct lys_node
*snode
)
1344 struct nb_node
*nb_node
= snode
->priv
;
1345 struct lys_node_container
*scontainer
;
1346 struct lys_node_leaf
*sleaf
;
1348 switch (operation
) {
1350 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1353 switch (snode
->nodetype
) {
1355 sleaf
= (struct lys_node_leaf
*)snode
;
1356 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1360 scontainer
= (struct lys_node_container
*)snode
;
1361 if (!scontainer
->presence
)
1372 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1375 switch (snode
->nodetype
) {
1377 sleaf
= (struct lys_node_leaf
*)snode
;
1378 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1381 /* List keys can't be modified. */
1382 if (lys_is_key(sleaf
, NULL
))
1390 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1393 switch (snode
->nodetype
) {
1395 sleaf
= (struct lys_node_leaf
*)snode
;
1397 /* List keys can't be deleted. */
1398 if (lys_is_key(sleaf
, NULL
))
1402 * Only optional leafs can be deleted, or leafs whose
1403 * parent is a case statement.
1405 if (snode
->parent
->nodetype
== LYS_CASE
)
1409 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1414 scontainer
= (struct lys_node_container
*)snode
;
1415 if (!scontainer
->presence
)
1426 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1429 switch (snode
->nodetype
) {
1432 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1439 case NB_OP_APPLY_FINISH
:
1440 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1443 case NB_OP_GET_ELEM
:
1444 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1447 switch (snode
->nodetype
) {
1452 scontainer
= (struct lys_node_container
*)snode
;
1453 if (!scontainer
->presence
)
1460 case NB_OP_GET_NEXT
:
1461 switch (snode
->nodetype
) {
1463 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1467 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1474 case NB_OP_GET_KEYS
:
1475 case NB_OP_LOOKUP_ENTRY
:
1476 switch (snode
->nodetype
) {
1478 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1480 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1488 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1491 switch (snode
->nodetype
) {
1504 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1505 (xpath
, arguments
));
1507 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1511 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
1513 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1515 list_delete(&arguments
);
1520 /* Running configuration user pointers management. */
1521 struct nb_config_entry
{
1522 char xpath
[XPATH_MAXLEN
];
1526 static bool running_config_entry_cmp(const void *value1
, const void *value2
)
1528 const struct nb_config_entry
*c1
= value1
;
1529 const struct nb_config_entry
*c2
= value2
;
1531 return strmatch(c1
->xpath
, c2
->xpath
);
1534 static unsigned int running_config_entry_key_make(void *value
)
1536 return string_hash_make(value
);
1539 static void *running_config_entry_alloc(void *p
)
1541 struct nb_config_entry
*new, *key
= p
;
1543 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY
, sizeof(*new));
1544 strlcpy(new->xpath
, key
->xpath
, sizeof(new->xpath
));
1549 static void running_config_entry_free(void *arg
)
1551 XFREE(MTYPE_NB_CONFIG_ENTRY
, arg
);
1554 void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
)
1556 struct nb_config_entry
*config
, s
;
1558 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1559 config
= hash_get(running_config_entries
, &s
,
1560 running_config_entry_alloc
);
1561 config
->entry
= entry
;
1564 static void *nb_running_unset_entry_helper(const struct lyd_node
*dnode
)
1566 struct nb_config_entry
*config
, s
;
1567 struct lyd_node
*child
;
1570 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1571 config
= hash_release(running_config_entries
, &s
);
1573 entry
= config
->entry
;
1574 running_config_entry_free(config
);
1577 /* Unset user pointers from the child nodes. */
1578 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
)) {
1579 LY_TREE_FOR (dnode
->child
, child
) {
1580 (void)nb_running_unset_entry_helper(child
);
1587 void *nb_running_unset_entry(const struct lyd_node
*dnode
)
1591 entry
= nb_running_unset_entry_helper(dnode
);
1597 void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
1598 bool abort_if_not_found
)
1600 const struct lyd_node
*orig_dnode
= dnode
;
1601 char xpath_buf
[XPATH_MAXLEN
];
1603 assert(dnode
|| xpath
);
1606 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
1609 struct nb_config_entry
*config
, s
;
1611 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1612 config
= hash_lookup(running_config_entries
, &s
);
1614 return config
->entry
;
1616 dnode
= dnode
->parent
;
1619 if (!abort_if_not_found
)
1622 yang_dnode_get_path(orig_dnode
, xpath_buf
, sizeof(xpath_buf
));
1623 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
1624 "%s: failed to find entry [xpath %s]", __func__
, xpath_buf
);
1625 zlog_backtrace(LOG_ERR
);
1629 /* Logging functions. */
1630 const char *nb_event_name(enum nb_event event
)
1633 case NB_EV_VALIDATE
:
1646 const char *nb_operation_name(enum nb_operation operation
)
1648 switch (operation
) {
1657 case NB_OP_APPLY_FINISH
:
1658 return "apply_finish";
1659 case NB_OP_GET_ELEM
:
1661 case NB_OP_GET_NEXT
:
1663 case NB_OP_GET_KEYS
:
1665 case NB_OP_LOOKUP_ENTRY
:
1666 return "lookup_entry";
1674 const char *nb_err_name(enum nb_error error
)
1680 return "generic error";
1681 case NB_ERR_NO_CHANGES
:
1682 return "no changes";
1683 case NB_ERR_NOT_FOUND
:
1684 return "element not found";
1686 return "resource is locked";
1687 case NB_ERR_VALIDATION
:
1688 return "validation error";
1689 case NB_ERR_RESOURCE
:
1690 return "failed to allocate resource";
1691 case NB_ERR_INCONSISTENCY
:
1692 return "internal inconsistency";
1698 const char *nb_client_name(enum nb_client client
)
1703 case NB_CLIENT_CONFD
:
1705 case NB_CLIENT_SYSREPO
:
1712 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1714 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1715 struct nb_node
*nb_node
;
1718 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1720 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1721 "%s: unknown data path: %s", __func__
,
1722 module
->nodes
[i
].xpath
);
1726 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1727 priority
= module
->nodes
[i
].priority
;
1729 nb_node
->priority
= priority
;
1733 void nb_init(struct thread_master
*tm
,
1734 const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1736 unsigned int errors
= 0;
1738 /* Load YANG modules. */
1739 for (size_t i
= 0; i
< nmodules
; i
++)
1740 yang_module_load(modules
[i
]->name
);
1742 /* Create a nb_node for all YANG schema nodes. */
1745 /* Load northbound callbacks. */
1746 for (size_t i
= 0; i
< nmodules
; i
++)
1747 nb_load_callbacks(modules
[i
]);
1749 /* Validate northbound callbacks. */
1750 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1753 EC_LIB_NB_CBS_VALIDATION
,
1754 "%s: failed to validate northbound callbacks: %u error(s)",
1759 /* Create an empty running configuration. */
1760 running_config
= nb_config_new(NULL
);
1761 running_config_entries
= hash_create(running_config_entry_key_make
,
1762 running_config_entry_cmp
,
1763 "Running Configuration Entries");
1765 /* Initialize the northbound CLI. */
1769 void nb_terminate(void)
1771 /* Terminate the northbound CLI. */
1774 /* Delete all nb_node's from all YANG modules. */
1777 /* Delete the running configuration. */
1778 hash_clean(running_config_entries
, running_config_entry_free
);
1779 hash_free(running_config_entries
);
1780 nb_config_free(running_config
);