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 "frr_pthread.h"
30 #include "northbound.h"
31 #include "northbound_cli.h"
32 #include "northbound_db.h"
34 DEFINE_MTYPE_STATIC(LIB
, NB_NODE
, "Northbound Node")
35 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG
, "Northbound Configuration")
36 DEFINE_MTYPE_STATIC(LIB
, NB_CONFIG_ENTRY
, "Northbound Configuration Entry")
38 /* Running configuration - shouldn't be modified directly. */
39 struct nb_config
*running_config
;
41 /* Hash table of user pointers associated with configuration entries. */
42 static struct hash
*running_config_entries
;
44 /* Management lock for the running configuration. */
46 /* Mutex protecting this structure. */
52 /* Northbound client who owns this lock. */
53 enum nb_client owner_client
;
55 /* Northbound user who owns this lock. */
56 const void *owner_user
;
57 } running_config_mgmt_lock
;
60 * Global lock used to prevent multiple configuration transactions from
61 * happening concurrently.
63 static bool transaction_in_progress
;
65 static int nb_callback_configuration(const enum nb_event event
,
66 struct nb_config_change
*change
);
67 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
68 struct nb_config_cbs
*changes
,
69 enum nb_client client
,
72 static void nb_transaction_free(struct nb_transaction
*transaction
);
73 static int nb_transaction_process(enum nb_event event
,
74 struct nb_transaction
*transaction
);
75 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
);
76 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
77 const char *xpath
, const void *list_entry
,
78 const struct yang_list_keys
*list_keys
,
79 struct yang_translator
*translator
,
80 bool first
, uint32_t flags
,
81 nb_oper_data_cb cb
, void *arg
);
83 static int nb_node_check_config_only(const struct lys_node
*snode
, void *arg
)
85 bool *config_only
= arg
;
87 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
89 return YANG_ITER_STOP
;
92 return YANG_ITER_CONTINUE
;
95 static int nb_node_new_cb(const struct lys_node
*snode
, void *arg
)
97 struct nb_node
*nb_node
;
98 struct lys_node
*sparent
, *sparent_list
;
100 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
101 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
102 sizeof(nb_node
->xpath
));
103 nb_node
->priority
= NB_DFLT_PRIORITY
;
104 sparent
= yang_snode_real_parent(snode
);
106 nb_node
->parent
= sparent
->priv
;
107 sparent_list
= yang_snode_parent_list(snode
);
109 nb_node
->parent_list
= sparent_list
->priv
;
112 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
113 bool config_only
= true;
115 yang_snodes_iterate_subtree(snode
, nb_node_check_config_only
,
116 YANG_ITER_ALLOW_AUGMENTATIONS
,
119 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
121 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
122 struct lys_node_list
*slist
;
124 slist
= (struct lys_node_list
*)snode
;
125 if (slist
->keys_size
== 0)
126 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
130 * Link the northbound node and the libyang schema node with one
133 nb_node
->snode
= snode
;
134 lys_set_private(snode
, nb_node
);
136 return YANG_ITER_CONTINUE
;
139 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
141 struct nb_node
*nb_node
;
143 nb_node
= snode
->priv
;
144 lys_set_private(snode
, NULL
);
145 XFREE(MTYPE_NB_NODE
, nb_node
);
147 return YANG_ITER_CONTINUE
;
150 void nb_nodes_create(void)
152 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
155 void nb_nodes_delete(void)
157 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
160 struct nb_node
*nb_node_find(const char *xpath
)
162 const struct lys_node
*snode
;
165 * Use libyang to find the schema node associated to the xpath and get
166 * the northbound node from there (snode private pointer).
168 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
175 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
176 enum nb_operation operation
,
177 int callback_implemented
, bool optional
)
181 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
183 if (!valid
&& callback_implemented
)
184 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
185 "unneeded '%s' callback for '%s'",
186 nb_operation_name(operation
), nb_node
->xpath
);
188 if (!optional
&& valid
&& !callback_implemented
) {
189 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
190 nb_operation_name(operation
), nb_node
->xpath
);
198 * Check if the required callbacks were implemented for the given northbound
201 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
204 unsigned int error
= 0;
206 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
207 !!nb_node
->cbs
.create
, false);
208 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
209 !!nb_node
->cbs
.modify
, false);
210 error
+= nb_node_validate_cb(nb_node
, NB_OP_DESTROY
,
211 !!nb_node
->cbs
.destroy
, false);
212 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
214 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
215 !!nb_node
->cbs
.apply_finish
, true);
216 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
217 !!nb_node
->cbs
.get_elem
, false);
218 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
219 !!nb_node
->cbs
.get_next
, false);
220 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
221 !!nb_node
->cbs
.get_keys
, false);
222 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
223 !!nb_node
->cbs
.lookup_entry
, false);
224 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
230 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
232 /* Top-level nodes can have any priority. */
233 if (!nb_node
->parent
)
236 if (nb_node
->priority
< nb_node
->parent
->priority
) {
237 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
238 "node has higher priority than its parent [xpath %s]",
246 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
248 struct nb_node
*nb_node
= snode
->priv
;
249 unsigned int *errors
= arg
;
251 /* Validate callbacks and priority. */
252 *errors
+= nb_node_validate_cbs(nb_node
);
253 *errors
+= nb_node_validate_priority(nb_node
);
255 return YANG_ITER_CONTINUE
;
258 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
260 struct nb_config
*config
;
262 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
264 config
->dnode
= dnode
;
266 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
268 pthread_rwlock_init(&config
->lock
, NULL
);
273 void nb_config_free(struct nb_config
*config
)
276 yang_dnode_free(config
->dnode
);
277 pthread_rwlock_destroy(&config
->lock
);
278 XFREE(MTYPE_NB_CONFIG
, config
);
281 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
283 struct nb_config
*dup
;
285 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
286 dup
->dnode
= yang_dnode_dup(config
->dnode
);
287 dup
->version
= config
->version
;
288 pthread_rwlock_init(&dup
->lock
, NULL
);
293 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
294 bool preserve_source
)
298 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
300 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
302 if (!preserve_source
)
303 nb_config_free(config_src
);
305 return (ret
== 0) ? NB_OK
: NB_ERR
;
308 void nb_config_replace(struct nb_config
*config_dst
,
309 struct nb_config
*config_src
, bool preserve_source
)
311 /* Update version. */
312 if (config_src
->version
!= 0)
313 config_dst
->version
= config_src
->version
;
316 if (config_dst
->dnode
)
317 yang_dnode_free(config_dst
->dnode
);
318 if (preserve_source
) {
319 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
321 config_dst
->dnode
= config_src
->dnode
;
322 config_src
->dnode
= NULL
;
323 nb_config_free(config_src
);
327 /* Generate the nb_config_cbs tree. */
328 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
329 const struct nb_config_cb
*b
)
331 /* Sort by priority first. */
332 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
334 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
338 * Use XPath as a tie-breaker. This will naturally sort parent nodes
339 * before their children.
341 return strcmp(a
->xpath
, b
->xpath
);
343 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
345 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
346 enum nb_operation operation
,
347 const struct lyd_node
*dnode
)
349 struct nb_config_change
*change
;
351 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
352 change
->cb
.operation
= operation
;
353 change
->cb
.nb_node
= dnode
->schema
->priv
;
354 yang_dnode_get_path(dnode
, change
->cb
.xpath
, sizeof(change
->cb
.xpath
));
355 change
->cb
.dnode
= dnode
;
357 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
360 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
362 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
363 struct nb_config_change
*change
;
365 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
367 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
368 XFREE(MTYPE_TMP
, change
);
373 * Helper function used when calculating the delta between two different
374 * configurations. Given a new subtree, calculate all new YANG data nodes,
375 * excluding default leafs and leaf-lists. This is a recursive function.
377 static void nb_config_diff_created(const struct lyd_node
*dnode
,
378 struct nb_config_cbs
*changes
)
380 enum nb_operation operation
;
381 struct lyd_node
*child
;
383 switch (dnode
->schema
->nodetype
) {
386 if (lyd_wd_default((struct lyd_node_leaf_list
*)dnode
))
389 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
390 operation
= NB_OP_CREATE
;
391 else if (nb_operation_is_valid(NB_OP_MODIFY
, dnode
->schema
))
392 operation
= NB_OP_MODIFY
;
396 nb_config_diff_add_change(changes
, operation
, dnode
);
400 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
401 nb_config_diff_add_change(changes
, NB_OP_CREATE
, dnode
);
403 /* Process child nodes recursively. */
404 LY_TREE_FOR (dnode
->child
, child
) {
405 nb_config_diff_created(child
, changes
);
413 static void nb_config_diff_deleted(const struct lyd_node
*dnode
,
414 struct nb_config_cbs
*changes
)
416 if (nb_operation_is_valid(NB_OP_DESTROY
, dnode
->schema
))
417 nb_config_diff_add_change(changes
, NB_OP_DESTROY
, dnode
);
418 else if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_CONTAINER
)) {
419 struct lyd_node
*child
;
422 * Non-presence containers need special handling since they
423 * don't have "destroy" callbacks. In this case, what we need to
424 * do is to call the "destroy" callbacks of their child nodes
425 * when applicable (i.e. optional nodes).
427 LY_TREE_FOR (dnode
->child
, child
) {
428 nb_config_diff_deleted(child
, changes
);
433 /* Calculate the delta between two different configurations. */
434 static void nb_config_diff(const struct nb_config
*config1
,
435 const struct nb_config
*config2
,
436 struct nb_config_cbs
*changes
)
438 struct lyd_difflist
*diff
;
440 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
441 LYD_DIFFOPT_WITHDEFAULTS
);
444 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
446 struct lyd_node
*dnode
;
448 type
= diff
->type
[i
];
451 case LYD_DIFF_CREATED
:
452 dnode
= diff
->second
[i
];
453 nb_config_diff_created(dnode
, changes
);
455 case LYD_DIFF_DELETED
:
456 dnode
= diff
->first
[i
];
457 nb_config_diff_deleted(dnode
, changes
);
459 case LYD_DIFF_CHANGED
:
460 dnode
= diff
->second
[i
];
461 nb_config_diff_add_change(changes
, NB_OP_MODIFY
, dnode
);
463 case LYD_DIFF_MOVEDAFTER1
:
464 case LYD_DIFF_MOVEDAFTER2
:
473 int nb_candidate_edit(struct nb_config
*candidate
,
474 const struct nb_node
*nb_node
,
475 enum nb_operation operation
, const char *xpath
,
476 const struct yang_data
*previous
,
477 const struct yang_data
*data
)
479 struct lyd_node
*dnode
;
480 char xpath_edit
[XPATH_MAXLEN
];
482 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
483 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
484 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
487 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
493 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
494 xpath_edit
, (void *)data
->value
, 0,
495 LYD_PATH_OPT_UPDATE
);
496 if (!dnode
&& ly_errno
) {
497 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
503 * If a new node was created, call lyd_validate() only to create
504 * default child nodes.
507 lyd_schema_sort(dnode
, 0);
509 LYD_OPT_CONFIG
| LYD_OPT_WHENAUTODEL
,
514 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
517 * Return a special error code so the caller can choose
518 * whether to ignore it or not.
520 return NB_ERR_NOT_FOUND
;
524 /* TODO: update configuration. */
527 flog_warn(EC_LIB_DEVELOPMENT
,
528 "%s: unknown operation (%u) [xpath %s]", __func__
,
529 operation
, xpath_edit
);
536 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
540 pthread_rwlock_rdlock(&running_config
->lock
);
542 if (candidate
->version
< running_config
->version
)
545 pthread_rwlock_unlock(&running_config
->lock
);
550 int nb_candidate_update(struct nb_config
*candidate
)
552 struct nb_config
*updated_config
;
554 pthread_rwlock_rdlock(&running_config
->lock
);
556 updated_config
= nb_config_dup(running_config
);
558 pthread_rwlock_unlock(&running_config
->lock
);
560 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
563 nb_config_replace(candidate
, updated_config
, false);
569 * Perform YANG syntactic and semantic validation.
571 * WARNING: lyd_validate() can change the configuration as part of the
572 * validation process.
574 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
576 if (lyd_validate(&candidate
->dnode
,
577 LYD_OPT_STRICT
| LYD_OPT_CONFIG
| LYD_OPT_WHENAUTODEL
,
580 return NB_ERR_VALIDATION
;
585 /* Perform code-level validation using the northbound callbacks. */
586 static int nb_candidate_validate_changes(struct nb_config
*candidate
,
587 struct nb_config_cbs
*changes
)
589 struct nb_config_cb
*cb
;
591 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
592 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
595 ret
= nb_callback_configuration(NB_EV_VALIDATE
, change
);
597 return NB_ERR_VALIDATION
;
603 int nb_candidate_validate(struct nb_config
*candidate
)
605 struct nb_config_cbs changes
;
608 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
609 return NB_ERR_VALIDATION
;
611 RB_INIT(nb_config_cbs
, &changes
);
612 pthread_rwlock_rdlock(&running_config
->lock
);
614 nb_config_diff(running_config
, candidate
, &changes
);
615 ret
= nb_candidate_validate_changes(candidate
, &changes
);
616 nb_config_diff_del_changes(&changes
);
618 pthread_rwlock_unlock(&running_config
->lock
);
623 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
624 enum nb_client client
, const void *user
,
626 struct nb_transaction
**transaction
)
628 struct nb_config_cbs changes
;
630 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
631 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
632 "%s: failed to validate candidate configuration",
634 return NB_ERR_VALIDATION
;
637 RB_INIT(nb_config_cbs
, &changes
);
638 pthread_rwlock_rdlock(&running_config
->lock
);
640 nb_config_diff(running_config
, candidate
, &changes
);
641 if (RB_EMPTY(nb_config_cbs
, &changes
)) {
642 pthread_rwlock_unlock(&running_config
->lock
);
643 return NB_ERR_NO_CHANGES
;
646 if (nb_candidate_validate_changes(candidate
, &changes
)
649 EC_LIB_NB_CANDIDATE_INVALID
,
650 "%s: failed to validate candidate configuration",
652 nb_config_diff_del_changes(&changes
);
653 pthread_rwlock_unlock(&running_config
->lock
);
654 return NB_ERR_VALIDATION
;
657 *transaction
= nb_transaction_new(candidate
, &changes
, client
,
659 if (*transaction
== NULL
) {
660 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
661 "%s: failed to create transaction", __func__
);
662 nb_config_diff_del_changes(&changes
);
663 pthread_rwlock_unlock(&running_config
->lock
);
664 return NB_ERR_LOCKED
;
667 pthread_rwlock_unlock(&running_config
->lock
);
669 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
672 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
674 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
675 nb_transaction_free(transaction
);
678 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
679 bool save_transaction
, uint32_t *transaction_id
)
681 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
682 nb_transaction_apply_finish(transaction
);
684 /* Replace running by candidate. */
685 transaction
->config
->version
++;
686 pthread_rwlock_wrlock(&running_config
->lock
);
688 nb_config_replace(running_config
, transaction
->config
, true);
690 pthread_rwlock_unlock(&running_config
->lock
);
692 /* Record transaction. */
694 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
695 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
696 "%s: failed to record transaction", __func__
);
698 nb_transaction_free(transaction
);
701 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
702 const void *user
, bool save_transaction
,
703 const char *comment
, uint32_t *transaction_id
)
705 struct nb_transaction
*transaction
= NULL
;
708 ret
= nb_candidate_commit_prepare(candidate
, client
, user
, comment
,
711 * Apply the changes if the preparation phase succeeded. Otherwise abort
715 nb_candidate_commit_apply(transaction
, save_transaction
,
717 else if (transaction
!= NULL
)
718 nb_candidate_commit_abort(transaction
);
723 int nb_running_lock(enum nb_client client
, const void *user
)
727 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
728 if (!running_config_mgmt_lock
.locked
) {
729 running_config_mgmt_lock
.locked
= true;
730 running_config_mgmt_lock
.owner_client
= client
;
731 running_config_mgmt_lock
.owner_user
= user
;
739 int nb_running_unlock(enum nb_client client
, const void *user
)
743 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
744 if (running_config_mgmt_lock
.locked
745 && running_config_mgmt_lock
.owner_client
== client
746 && running_config_mgmt_lock
.owner_user
== user
) {
747 running_config_mgmt_lock
.locked
= false;
748 running_config_mgmt_lock
.owner_client
= NB_CLIENT_NONE
;
749 running_config_mgmt_lock
.owner_user
= NULL
;
757 int nb_running_lock_check(enum nb_client client
, const void *user
)
761 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
762 if (!running_config_mgmt_lock
.locked
763 || (running_config_mgmt_lock
.owner_client
== client
764 && running_config_mgmt_lock
.owner_user
== user
))
771 static void nb_log_callback(const enum nb_event event
,
772 enum nb_operation operation
, const char *xpath
,
776 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
777 nb_event_name(event
), nb_operation_name(operation
), xpath
,
778 value
? value
: "(NULL)");
782 * Call the northbound configuration callback associated to a given
783 * configuration change.
785 static int nb_callback_configuration(const enum nb_event event
,
786 struct nb_config_change
*change
)
788 enum nb_operation operation
= change
->cb
.operation
;
789 const char *xpath
= change
->cb
.xpath
;
790 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
791 const struct lyd_node
*dnode
= change
->cb
.dnode
;
792 union nb_resource
*resource
;
795 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
796 const char *value
= "(none)";
798 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
799 value
= yang_dnode_get_string(dnode
, NULL
);
801 nb_log_callback(event
, operation
, xpath
, value
);
804 if (event
== NB_EV_VALIDATE
)
807 resource
= &change
->resource
;
811 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
814 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
817 ret
= (*nb_node
->cbs
.destroy
)(event
, dnode
);
820 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
823 flog_err(EC_LIB_DEVELOPMENT
,
824 "%s: unknown operation (%u) [xpath %s]", __func__
,
831 enum lib_log_refs ref
;
835 priority
= LOG_WARNING
;
836 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
839 priority
= LOG_WARNING
;
840 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
843 priority
= LOG_WARNING
;
844 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
848 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
851 flog_err(EC_LIB_DEVELOPMENT
,
852 "%s: unknown event (%u) [xpath %s]",
853 __func__
, event
, xpath
);
858 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
859 __func__
, nb_err_name(ret
), nb_event_name(event
),
860 nb_operation_name(operation
), xpath
);
866 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
868 const void *list_entry
)
870 DEBUGD(&nb_dbg_cbs_state
,
871 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
874 return nb_node
->cbs
.get_elem(xpath
, list_entry
);
877 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
878 const void *parent_list_entry
,
879 const void *list_entry
)
881 DEBUGD(&nb_dbg_cbs_state
,
882 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
883 nb_node
->xpath
, parent_list_entry
, list_entry
);
885 return nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
888 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
889 struct yang_list_keys
*keys
)
891 DEBUGD(&nb_dbg_cbs_state
,
892 "northbound callback (get_keys): node [%s] list_entry [%p]",
893 nb_node
->xpath
, list_entry
);
895 return nb_node
->cbs
.get_keys(list_entry
, keys
);
898 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
899 const void *parent_list_entry
,
900 const struct yang_list_keys
*keys
)
902 DEBUGD(&nb_dbg_cbs_state
,
903 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
904 nb_node
->xpath
, parent_list_entry
);
906 return nb_node
->cbs
.lookup_entry(parent_list_entry
, keys
);
909 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
910 const struct list
*input
, struct list
*output
)
912 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
914 return nb_node
->cbs
.rpc(xpath
, input
, output
);
917 static struct nb_transaction
*
918 nb_transaction_new(struct nb_config
*config
, struct nb_config_cbs
*changes
,
919 enum nb_client client
, const void *user
, const char *comment
)
921 struct nb_transaction
*transaction
;
923 if (nb_running_lock_check(client
, user
)) {
925 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
926 "%s: running configuration is locked by another client",
931 if (transaction_in_progress
) {
933 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
934 "%s: error - there's already another transaction in progress",
938 transaction_in_progress
= true;
940 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
941 transaction
->client
= client
;
943 strlcpy(transaction
->comment
, comment
,
944 sizeof(transaction
->comment
));
945 transaction
->config
= config
;
946 transaction
->changes
= *changes
;
951 static void nb_transaction_free(struct nb_transaction
*transaction
)
953 nb_config_diff_del_changes(&transaction
->changes
);
954 XFREE(MTYPE_TMP
, transaction
);
955 transaction_in_progress
= false;
958 /* Process all configuration changes associated to a transaction. */
959 static int nb_transaction_process(enum nb_event event
,
960 struct nb_transaction
*transaction
)
962 struct nb_config_cb
*cb
;
965 * Need to lock the running configuration since transaction->changes
966 * can contain pointers to data nodes from the running configuration.
968 pthread_rwlock_rdlock(&running_config
->lock
);
970 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
971 struct nb_config_change
*change
=
972 (struct nb_config_change
*)cb
;
976 * Only try to release resources that were allocated
979 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
982 /* Call the appropriate callback. */
983 ret
= nb_callback_configuration(event
, change
);
987 pthread_rwlock_unlock(
988 &running_config
->lock
);
991 change
->prepare_ok
= true;
996 * At this point it's not possible to reject the
997 * transaction anymore, so any failure here can
998 * lead to inconsistencies and should be treated
999 * as a bug. Operations prone to errors, like
1000 * validations and resource allocations, should
1001 * be performed during the 'prepare' phase.
1009 pthread_rwlock_unlock(&running_config
->lock
);
1014 static struct nb_config_cb
*
1015 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const char *xpath
,
1016 const struct nb_node
*nb_node
,
1017 const struct lyd_node
*dnode
)
1019 struct nb_config_cb
*cb
;
1021 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
1022 strlcpy(cb
->xpath
, xpath
, sizeof(cb
->xpath
));
1023 cb
->nb_node
= nb_node
;
1025 RB_INSERT(nb_config_cbs
, cbs
, cb
);
1030 static struct nb_config_cb
*
1031 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
, const char *xpath
,
1032 const struct nb_node
*nb_node
)
1034 struct nb_config_cb s
;
1036 strlcpy(s
.xpath
, xpath
, sizeof(s
.xpath
));
1037 s
.nb_node
= nb_node
;
1038 return RB_FIND(nb_config_cbs
, cbs
, &s
);
1041 /* Call the 'apply_finish' callbacks. */
1042 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
1044 struct nb_config_cbs cbs
;
1045 struct nb_config_cb
*cb
;
1047 /* Initialize tree of 'apply_finish' callbacks. */
1048 RB_INIT(nb_config_cbs
, &cbs
);
1050 /* Identify the 'apply_finish' callbacks that need to be called. */
1051 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
1052 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
1053 const struct lyd_node
*dnode
= change
->cb
.dnode
;
1056 * Iterate up to the root of the data tree. When a node is being
1057 * deleted, skip its 'apply_finish' callback if one is defined
1058 * (the 'apply_finish' callbacks from the node ancestors should
1059 * be called though).
1061 if (change
->cb
.operation
== NB_OP_DESTROY
) {
1062 char xpath
[XPATH_MAXLEN
];
1064 dnode
= dnode
->parent
;
1069 * The dnode from 'delete' callbacks point to elements
1070 * from the running configuration. Use yang_dnode_get()
1071 * to get the corresponding dnode from the candidate
1072 * configuration that is being committed.
1074 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1075 dnode
= yang_dnode_get(transaction
->config
->dnode
,
1079 char xpath
[XPATH_MAXLEN
];
1080 struct nb_node
*nb_node
;
1082 nb_node
= dnode
->schema
->priv
;
1083 if (!nb_node
->cbs
.apply_finish
)
1087 * Don't call the callback more than once for the same
1090 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1091 if (nb_apply_finish_cb_find(&cbs
, xpath
, nb_node
))
1094 nb_apply_finish_cb_new(&cbs
, xpath
, nb_node
, dnode
);
1097 dnode
= dnode
->parent
;
1101 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1102 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
1103 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
))
1104 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
,
1107 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
1110 /* Release memory. */
1111 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
1112 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
1113 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
1114 XFREE(MTYPE_TMP
, cb
);
1118 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
1119 const char *xpath
, const void *list_entry
,
1120 const struct yang_list_keys
*list_keys
,
1121 struct yang_translator
*translator
,
1122 bool first
, uint32_t flags
,
1123 nb_oper_data_cb cb
, void *arg
)
1125 struct lys_node
*child
;
1127 LY_TREE_FOR (snode
->child
, child
) {
1130 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1131 list_keys
, translator
, false,
1140 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1141 const char *xpath
, const void *list_entry
,
1142 const struct yang_list_keys
*list_keys
,
1143 struct yang_translator
*translator
,
1144 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1146 struct yang_data
*data
;
1148 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1151 /* Ignore list keys. */
1152 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1155 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1157 /* Leaf of type "empty" is not present. */
1160 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1163 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1165 const void *list_entry
,
1166 const struct yang_list_keys
*list_keys
,
1167 struct yang_translator
*translator
,
1168 uint32_t flags
, nb_oper_data_cb cb
,
1171 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1174 /* Presence containers. */
1175 if (nb_node
->cbs
.get_elem
) {
1176 struct yang_data
*data
;
1179 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1181 /* Presence container is not present. */
1184 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1189 /* Iterate over the child nodes. */
1190 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1191 list_keys
, translator
, false, flags
,
1196 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1197 const void *parent_list_entry
,
1198 const struct yang_list_keys
*parent_list_keys
,
1199 struct yang_translator
*translator
, uint32_t flags
,
1200 nb_oper_data_cb cb
, void *arg
)
1202 const void *list_entry
= NULL
;
1204 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1208 struct yang_data
*data
;
1211 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1214 /* End of the list. */
1217 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1221 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1224 } while (list_entry
);
1229 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1230 const char *xpath_list
,
1231 const void *parent_list_entry
,
1232 const struct yang_list_keys
*parent_list_keys
,
1233 struct yang_translator
*translator
,
1234 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1236 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1237 const void *list_entry
= NULL
;
1238 uint32_t position
= 1;
1240 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1243 /* Iterate over all list entries. */
1245 struct yang_list_keys list_keys
;
1246 char xpath
[XPATH_MAXLEN
* 2];
1249 /* Obtain list entry. */
1250 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1253 /* End of the list. */
1256 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1257 /* Obtain the list entry keys. */
1258 if (nb_callback_get_keys(nb_node
, list_entry
,
1261 flog_warn(EC_LIB_NB_CB_STATE
,
1262 "%s: failed to get list keys",
1267 /* Build XPath of the list entry. */
1268 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1269 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1270 snprintf(xpath
+ strlen(xpath
),
1271 sizeof(xpath
) - strlen(xpath
),
1272 "[%s='%s']", slist
->keys
[i
]->name
,
1277 * Keyless list - build XPath using a positional index.
1279 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1284 /* Iterate over the child nodes. */
1285 ret
= nb_oper_data_iter_children(
1286 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1287 translator
, false, flags
, cb
, arg
);
1290 } while (list_entry
);
1295 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1296 const char *xpath_parent
,
1297 const void *list_entry
,
1298 const struct yang_list_keys
*list_keys
,
1299 struct yang_translator
*translator
,
1300 bool first
, uint32_t flags
,
1301 nb_oper_data_cb cb
, void *arg
)
1303 struct nb_node
*nb_node
;
1304 char xpath
[XPATH_MAXLEN
];
1307 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1308 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1312 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1313 if (!first
&& snode
->nodetype
!= LYS_USES
)
1314 snprintf(xpath
+ strlen(xpath
), sizeof(xpath
) - strlen(xpath
),
1315 "/%s", snode
->name
);
1317 nb_node
= snode
->priv
;
1318 switch (snode
->nodetype
) {
1320 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1321 list_keys
, translator
, flags
,
1325 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1326 list_keys
, translator
, flags
, cb
,
1330 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1331 list_keys
, translator
, flags
,
1335 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1336 list_keys
, translator
, flags
, cb
,
1340 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1341 list_keys
, translator
, false,
1351 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1352 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1354 struct nb_node
*nb_node
;
1355 const void *list_entry
= NULL
;
1356 struct yang_list_keys list_keys
;
1357 struct list
*list_dnodes
;
1358 struct lyd_node
*dnode
, *dn
;
1359 struct listnode
*ln
;
1362 nb_node
= nb_node_find(xpath
);
1364 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1365 "%s: unknown data path: %s", __func__
, xpath
);
1369 /* For now this function works only with containers and lists. */
1370 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1372 EC_LIB_NB_OPERATIONAL_DATA
,
1373 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1379 * Create a data tree from the XPath so that we can parse the keys of
1380 * all YANG lists (if any).
1383 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1384 LYD_PATH_OPT_UPDATE
| LYD_PATH_OPT_NOPARENTRET
);
1386 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1392 * Create a linked list to sort the data nodes starting from the root.
1394 list_dnodes
= list_new();
1395 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1396 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1398 listnode_add_head(list_dnodes
, dn
);
1401 * Use the northbound callbacks to find list entry pointer corresponding
1402 * to the given XPath.
1404 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1405 struct lyd_node
*child
;
1409 /* Obtain the list entry keys. */
1410 memset(&list_keys
, 0, sizeof(list_keys
));
1411 LY_TREE_FOR (dn
->child
, child
) {
1412 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1415 strlcpy(list_keys
.key
[n
],
1416 yang_dnode_get_string(child
, NULL
),
1417 sizeof(list_keys
.key
[n
]));
1422 != ((struct lys_node_list
*)dn
->schema
)->keys_size
) {
1423 list_delete(&list_dnodes
);
1424 yang_dnode_free(dnode
);
1425 return NB_ERR_NOT_FOUND
;
1428 /* Find the list entry pointer. */
1429 nn
= dn
->schema
->priv
;
1431 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
1432 if (list_entry
== NULL
) {
1433 list_delete(&list_dnodes
);
1434 yang_dnode_free(dnode
);
1435 return NB_ERR_NOT_FOUND
;
1439 /* If a list entry was given, iterate over that list entry only. */
1440 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1441 ret
= nb_oper_data_iter_children(
1442 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1443 translator
, true, flags
, cb
, arg
);
1445 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1446 &list_keys
, translator
, true,
1449 list_delete(&list_dnodes
);
1450 yang_dnode_free(dnode
);
1455 bool nb_operation_is_valid(enum nb_operation operation
,
1456 const struct lys_node
*snode
)
1458 struct nb_node
*nb_node
= snode
->priv
;
1459 struct lys_node_container
*scontainer
;
1460 struct lys_node_leaf
*sleaf
;
1462 switch (operation
) {
1464 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1467 switch (snode
->nodetype
) {
1469 sleaf
= (struct lys_node_leaf
*)snode
;
1470 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1474 scontainer
= (struct lys_node_container
*)snode
;
1475 if (!scontainer
->presence
)
1486 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1489 switch (snode
->nodetype
) {
1491 sleaf
= (struct lys_node_leaf
*)snode
;
1492 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1495 /* List keys can't be modified. */
1496 if (lys_is_key(sleaf
, NULL
))
1504 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1507 switch (snode
->nodetype
) {
1509 sleaf
= (struct lys_node_leaf
*)snode
;
1511 /* List keys can't be deleted. */
1512 if (lys_is_key(sleaf
, NULL
))
1516 * Only optional leafs can be deleted, or leafs whose
1517 * parent is a case statement.
1519 if (snode
->parent
->nodetype
== LYS_CASE
)
1523 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1528 scontainer
= (struct lys_node_container
*)snode
;
1529 if (!scontainer
->presence
)
1540 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1543 switch (snode
->nodetype
) {
1546 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1553 case NB_OP_APPLY_FINISH
:
1554 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1557 case NB_OP_GET_ELEM
:
1558 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1561 switch (snode
->nodetype
) {
1566 scontainer
= (struct lys_node_container
*)snode
;
1567 if (!scontainer
->presence
)
1574 case NB_OP_GET_NEXT
:
1575 switch (snode
->nodetype
) {
1577 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1581 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1588 case NB_OP_GET_KEYS
:
1589 case NB_OP_LOOKUP_ENTRY
:
1590 switch (snode
->nodetype
) {
1592 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1594 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1602 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1605 switch (snode
->nodetype
) {
1618 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1619 (xpath
, arguments
));
1621 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1625 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
1627 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1629 list_delete(&arguments
);
1634 /* Running configuration user pointers management. */
1635 struct nb_config_entry
{
1636 char xpath
[XPATH_MAXLEN
];
1640 static bool running_config_entry_cmp(const void *value1
, const void *value2
)
1642 const struct nb_config_entry
*c1
= value1
;
1643 const struct nb_config_entry
*c2
= value2
;
1645 return strmatch(c1
->xpath
, c2
->xpath
);
1648 static unsigned int running_config_entry_key_make(const void *value
)
1650 return string_hash_make(value
);
1653 static void *running_config_entry_alloc(void *p
)
1655 struct nb_config_entry
*new, *key
= p
;
1657 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY
, sizeof(*new));
1658 strlcpy(new->xpath
, key
->xpath
, sizeof(new->xpath
));
1663 static void running_config_entry_free(void *arg
)
1665 XFREE(MTYPE_NB_CONFIG_ENTRY
, arg
);
1668 void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
)
1670 struct nb_config_entry
*config
, s
;
1672 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1673 config
= hash_get(running_config_entries
, &s
,
1674 running_config_entry_alloc
);
1675 config
->entry
= entry
;
1678 static void *nb_running_unset_entry_helper(const struct lyd_node
*dnode
)
1680 struct nb_config_entry
*config
, s
;
1681 struct lyd_node
*child
;
1684 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1685 config
= hash_release(running_config_entries
, &s
);
1687 entry
= config
->entry
;
1688 running_config_entry_free(config
);
1691 /* Unset user pointers from the child nodes. */
1692 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
)) {
1693 LY_TREE_FOR (dnode
->child
, child
) {
1694 (void)nb_running_unset_entry_helper(child
);
1701 void *nb_running_unset_entry(const struct lyd_node
*dnode
)
1705 entry
= nb_running_unset_entry_helper(dnode
);
1711 void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
1712 bool abort_if_not_found
)
1714 const struct lyd_node
*orig_dnode
= dnode
;
1715 char xpath_buf
[XPATH_MAXLEN
];
1717 assert(dnode
|| xpath
);
1720 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
1723 struct nb_config_entry
*config
, s
;
1725 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1726 config
= hash_lookup(running_config_entries
, &s
);
1728 return config
->entry
;
1730 dnode
= dnode
->parent
;
1733 if (!abort_if_not_found
)
1736 yang_dnode_get_path(orig_dnode
, xpath_buf
, sizeof(xpath_buf
));
1737 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
1738 "%s: failed to find entry [xpath %s]", __func__
, xpath_buf
);
1739 zlog_backtrace(LOG_ERR
);
1743 /* Logging functions. */
1744 const char *nb_event_name(enum nb_event event
)
1747 case NB_EV_VALIDATE
:
1760 const char *nb_operation_name(enum nb_operation operation
)
1762 switch (operation
) {
1771 case NB_OP_APPLY_FINISH
:
1772 return "apply_finish";
1773 case NB_OP_GET_ELEM
:
1775 case NB_OP_GET_NEXT
:
1777 case NB_OP_GET_KEYS
:
1779 case NB_OP_LOOKUP_ENTRY
:
1780 return "lookup_entry";
1788 const char *nb_err_name(enum nb_error error
)
1794 return "generic error";
1795 case NB_ERR_NO_CHANGES
:
1796 return "no changes";
1797 case NB_ERR_NOT_FOUND
:
1798 return "element not found";
1800 return "resource is locked";
1801 case NB_ERR_VALIDATION
:
1802 return "validation error";
1803 case NB_ERR_RESOURCE
:
1804 return "failed to allocate resource";
1805 case NB_ERR_INCONSISTENCY
:
1806 return "internal inconsistency";
1812 const char *nb_client_name(enum nb_client client
)
1817 case NB_CLIENT_CONFD
:
1819 case NB_CLIENT_SYSREPO
:
1821 case NB_CLIENT_GRPC
:
1828 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1830 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1831 struct nb_node
*nb_node
;
1834 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1836 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1837 "%s: unknown data path: %s", __func__
,
1838 module
->nodes
[i
].xpath
);
1842 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1843 priority
= module
->nodes
[i
].priority
;
1845 nb_node
->priority
= priority
;
1849 void nb_init(struct thread_master
*tm
,
1850 const struct frr_yang_module_info
*modules
[], size_t nmodules
)
1852 unsigned int errors
= 0;
1854 /* Load YANG modules. */
1855 for (size_t i
= 0; i
< nmodules
; i
++)
1856 yang_module_load(modules
[i
]->name
);
1858 /* Create a nb_node for all YANG schema nodes. */
1861 /* Load northbound callbacks. */
1862 for (size_t i
= 0; i
< nmodules
; i
++)
1863 nb_load_callbacks(modules
[i
]);
1865 /* Validate northbound callbacks. */
1866 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1869 EC_LIB_NB_CBS_VALIDATION
,
1870 "%s: failed to validate northbound callbacks: %u error(s)",
1875 /* Create an empty running configuration. */
1876 running_config
= nb_config_new(NULL
);
1877 running_config_entries
= hash_create(running_config_entry_key_make
,
1878 running_config_entry_cmp
,
1879 "Running Configuration Entries");
1880 pthread_mutex_init(&running_config_mgmt_lock
.mtx
, NULL
);
1882 /* Initialize the northbound CLI. */
1886 void nb_terminate(void)
1888 /* Terminate the northbound CLI. */
1891 /* Delete all nb_node's from all YANG modules. */
1894 /* Delete the running configuration. */
1895 hash_clean(running_config_entries
, running_config_entry_free
);
1896 hash_free(running_config_entries
);
1897 nb_config_free(running_config
);
1898 pthread_mutex_destroy(&running_config_mgmt_lock
.mtx
);