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 void nb_log_callback(const enum nb_event event
,
68 enum nb_operation operation
, const char *xpath
,
70 static struct nb_transaction
*nb_transaction_new(struct nb_config
*config
,
71 struct nb_config_cbs
*changes
,
72 enum nb_client client
,
75 static void nb_transaction_free(struct nb_transaction
*transaction
);
76 static int nb_transaction_process(enum nb_event event
,
77 struct nb_transaction
*transaction
);
78 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
);
79 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
80 const char *xpath
, const void *list_entry
,
81 const struct yang_list_keys
*list_keys
,
82 struct yang_translator
*translator
,
83 bool first
, uint32_t flags
,
84 nb_oper_data_cb cb
, void *arg
);
86 static int nb_node_check_config_only(const struct lys_node
*snode
, void *arg
)
88 bool *config_only
= arg
;
90 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
)) {
92 return YANG_ITER_STOP
;
95 return YANG_ITER_CONTINUE
;
98 static int nb_node_new_cb(const struct lys_node
*snode
, void *arg
)
100 struct nb_node
*nb_node
;
101 struct lys_node
*sparent
, *sparent_list
;
103 nb_node
= XCALLOC(MTYPE_NB_NODE
, sizeof(*nb_node
));
104 yang_snode_get_path(snode
, YANG_PATH_DATA
, nb_node
->xpath
,
105 sizeof(nb_node
->xpath
));
106 nb_node
->priority
= NB_DFLT_PRIORITY
;
107 sparent
= yang_snode_real_parent(snode
);
109 nb_node
->parent
= sparent
->priv
;
110 sparent_list
= yang_snode_parent_list(snode
);
112 nb_node
->parent_list
= sparent_list
->priv
;
115 if (CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
116 bool config_only
= true;
118 yang_snodes_iterate_subtree(snode
, nb_node_check_config_only
,
119 YANG_ITER_ALLOW_AUGMENTATIONS
,
122 SET_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
);
124 if (CHECK_FLAG(snode
->nodetype
, LYS_LIST
)) {
125 struct lys_node_list
*slist
;
127 slist
= (struct lys_node_list
*)snode
;
128 if (slist
->keys_size
== 0)
129 SET_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
);
133 * Link the northbound node and the libyang schema node with one
136 nb_node
->snode
= snode
;
137 lys_set_private(snode
, nb_node
);
139 return YANG_ITER_CONTINUE
;
142 static int nb_node_del_cb(const struct lys_node
*snode
, void *arg
)
144 struct nb_node
*nb_node
;
146 nb_node
= snode
->priv
;
147 lys_set_private(snode
, NULL
);
148 XFREE(MTYPE_NB_NODE
, nb_node
);
150 return YANG_ITER_CONTINUE
;
153 void nb_nodes_create(void)
155 yang_snodes_iterate_all(nb_node_new_cb
, 0, NULL
);
158 void nb_nodes_delete(void)
160 yang_snodes_iterate_all(nb_node_del_cb
, 0, NULL
);
163 struct nb_node
*nb_node_find(const char *xpath
)
165 const struct lys_node
*snode
;
168 * Use libyang to find the schema node associated to the xpath and get
169 * the northbound node from there (snode private pointer).
171 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
178 static int nb_node_validate_cb(const struct nb_node
*nb_node
,
179 enum nb_operation operation
,
180 int callback_implemented
, bool optional
)
184 valid
= nb_operation_is_valid(operation
, nb_node
->snode
);
187 * Add an exception for operational data callbacks. A rw list usually
188 * doesn't need any associated operational data callbacks. But if this
189 * rw list is augmented by another module which adds state nodes under
190 * it, then this list will need to have the 'get_next()', 'get_keys()'
191 * and 'lookup_entry()' callbacks. As such, never log a warning when
192 * these callbacks are implemented when they are not needed, since this
193 * depends on context (e.g. some daemons might augment "frr-interface"
194 * while others don't).
196 if (!valid
&& callback_implemented
&& operation
!= NB_OP_GET_NEXT
197 && operation
!= NB_OP_GET_KEYS
&& operation
!= NB_OP_LOOKUP_ENTRY
)
198 flog_warn(EC_LIB_NB_CB_UNNEEDED
,
199 "unneeded '%s' callback for '%s'",
200 nb_operation_name(operation
), nb_node
->xpath
);
202 if (!optional
&& valid
&& !callback_implemented
) {
203 flog_err(EC_LIB_NB_CB_MISSING
, "missing '%s' callback for '%s'",
204 nb_operation_name(operation
), nb_node
->xpath
);
212 * Check if the required callbacks were implemented for the given northbound
215 static unsigned int nb_node_validate_cbs(const struct nb_node
*nb_node
)
218 unsigned int error
= 0;
220 error
+= nb_node_validate_cb(nb_node
, NB_OP_CREATE
,
221 !!nb_node
->cbs
.create
, false);
222 error
+= nb_node_validate_cb(nb_node
, NB_OP_MODIFY
,
223 !!nb_node
->cbs
.modify
, false);
224 error
+= nb_node_validate_cb(nb_node
, NB_OP_DESTROY
,
225 !!nb_node
->cbs
.destroy
, false);
226 error
+= nb_node_validate_cb(nb_node
, NB_OP_MOVE
, !!nb_node
->cbs
.move
,
228 error
+= nb_node_validate_cb(nb_node
, NB_OP_PRE_VALIDATE
,
229 !!nb_node
->cbs
.pre_validate
, true);
230 error
+= nb_node_validate_cb(nb_node
, NB_OP_APPLY_FINISH
,
231 !!nb_node
->cbs
.apply_finish
, true);
232 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_ELEM
,
233 !!nb_node
->cbs
.get_elem
, false);
234 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_NEXT
,
235 !!nb_node
->cbs
.get_next
, false);
236 error
+= nb_node_validate_cb(nb_node
, NB_OP_GET_KEYS
,
237 !!nb_node
->cbs
.get_keys
, false);
238 error
+= nb_node_validate_cb(nb_node
, NB_OP_LOOKUP_ENTRY
,
239 !!nb_node
->cbs
.lookup_entry
, false);
240 error
+= nb_node_validate_cb(nb_node
, NB_OP_RPC
, !!nb_node
->cbs
.rpc
,
246 static unsigned int nb_node_validate_priority(const struct nb_node
*nb_node
)
248 /* Top-level nodes can have any priority. */
249 if (!nb_node
->parent
)
252 if (nb_node
->priority
< nb_node
->parent
->priority
) {
253 flog_err(EC_LIB_NB_CB_INVALID_PRIO
,
254 "node has higher priority than its parent [xpath %s]",
262 static int nb_node_validate(const struct lys_node
*snode
, void *arg
)
264 struct nb_node
*nb_node
= snode
->priv
;
265 unsigned int *errors
= arg
;
267 /* Validate callbacks and priority. */
268 *errors
+= nb_node_validate_cbs(nb_node
);
269 *errors
+= nb_node_validate_priority(nb_node
);
271 return YANG_ITER_CONTINUE
;
274 struct nb_config
*nb_config_new(struct lyd_node
*dnode
)
276 struct nb_config
*config
;
278 config
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*config
));
280 config
->dnode
= dnode
;
282 config
->dnode
= yang_dnode_new(ly_native_ctx
, true);
288 void nb_config_free(struct nb_config
*config
)
291 yang_dnode_free(config
->dnode
);
292 XFREE(MTYPE_NB_CONFIG
, config
);
295 struct nb_config
*nb_config_dup(const struct nb_config
*config
)
297 struct nb_config
*dup
;
299 dup
= XCALLOC(MTYPE_NB_CONFIG
, sizeof(*dup
));
300 dup
->dnode
= yang_dnode_dup(config
->dnode
);
301 dup
->version
= config
->version
;
306 int nb_config_merge(struct nb_config
*config_dst
, struct nb_config
*config_src
,
307 bool preserve_source
)
311 ret
= lyd_merge(config_dst
->dnode
, config_src
->dnode
, LYD_OPT_EXPLICIT
);
313 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_merge() failed", __func__
);
315 if (!preserve_source
)
316 nb_config_free(config_src
);
318 return (ret
== 0) ? NB_OK
: NB_ERR
;
321 void nb_config_replace(struct nb_config
*config_dst
,
322 struct nb_config
*config_src
, bool preserve_source
)
324 /* Update version. */
325 if (config_src
->version
!= 0)
326 config_dst
->version
= config_src
->version
;
329 if (config_dst
->dnode
)
330 yang_dnode_free(config_dst
->dnode
);
331 if (preserve_source
) {
332 config_dst
->dnode
= yang_dnode_dup(config_src
->dnode
);
334 config_dst
->dnode
= config_src
->dnode
;
335 config_src
->dnode
= NULL
;
336 nb_config_free(config_src
);
340 /* Generate the nb_config_cbs tree. */
341 static inline int nb_config_cb_compare(const struct nb_config_cb
*a
,
342 const struct nb_config_cb
*b
)
344 /* Sort by priority first. */
345 if (a
->nb_node
->priority
< b
->nb_node
->priority
)
347 if (a
->nb_node
->priority
> b
->nb_node
->priority
)
351 * Preserve the order of the configuration changes as told by libyang.
359 * All 'apply_finish' callbacks have their sequence number set to zero.
360 * In this case, compare them using their dnode pointers (the order
361 * doesn't matter for callbacks that have the same priority).
363 if (a
->dnode
< b
->dnode
)
365 if (a
->dnode
> b
->dnode
)
370 RB_GENERATE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
372 static void nb_config_diff_add_change(struct nb_config_cbs
*changes
,
373 enum nb_operation operation
,
375 const struct lyd_node
*dnode
)
377 struct nb_config_change
*change
;
379 change
= XCALLOC(MTYPE_TMP
, sizeof(*change
));
380 change
->cb
.operation
= operation
;
381 change
->cb
.seq
= *seq
;
383 change
->cb
.nb_node
= dnode
->schema
->priv
;
384 change
->cb
.dnode
= dnode
;
386 RB_INSERT(nb_config_cbs
, changes
, &change
->cb
);
389 static void nb_config_diff_del_changes(struct nb_config_cbs
*changes
)
391 while (!RB_EMPTY(nb_config_cbs
, changes
)) {
392 struct nb_config_change
*change
;
394 change
= (struct nb_config_change
*)RB_ROOT(nb_config_cbs
,
396 RB_REMOVE(nb_config_cbs
, changes
, &change
->cb
);
397 XFREE(MTYPE_TMP
, change
);
402 * Helper function used when calculating the delta between two different
403 * configurations. Given a new subtree, calculate all new YANG data nodes,
404 * excluding default leafs and leaf-lists. This is a recursive function.
406 static void nb_config_diff_created(const struct lyd_node
*dnode
, uint32_t *seq
,
407 struct nb_config_cbs
*changes
)
409 enum nb_operation operation
;
410 struct lyd_node
*child
;
412 switch (dnode
->schema
->nodetype
) {
415 if (lyd_wd_default((struct lyd_node_leaf_list
*)dnode
))
418 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
419 operation
= NB_OP_CREATE
;
420 else if (nb_operation_is_valid(NB_OP_MODIFY
, dnode
->schema
))
421 operation
= NB_OP_MODIFY
;
425 nb_config_diff_add_change(changes
, operation
, seq
, dnode
);
429 if (nb_operation_is_valid(NB_OP_CREATE
, dnode
->schema
))
430 nb_config_diff_add_change(changes
, NB_OP_CREATE
, seq
,
433 /* Process child nodes recursively. */
434 LY_TREE_FOR (dnode
->child
, child
) {
435 nb_config_diff_created(child
, seq
, changes
);
443 static void nb_config_diff_deleted(const struct lyd_node
*dnode
, uint32_t *seq
,
444 struct nb_config_cbs
*changes
)
446 if (nb_operation_is_valid(NB_OP_DESTROY
, dnode
->schema
))
447 nb_config_diff_add_change(changes
, NB_OP_DESTROY
, seq
, dnode
);
448 else if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_CONTAINER
)) {
449 struct lyd_node
*child
;
452 * Non-presence containers need special handling since they
453 * don't have "destroy" callbacks. In this case, what we need to
454 * do is to call the "destroy" callbacks of their child nodes
455 * when applicable (i.e. optional nodes).
457 LY_TREE_FOR (dnode
->child
, child
) {
458 nb_config_diff_deleted(child
, seq
, changes
);
463 /* Calculate the delta between two different configurations. */
464 static void nb_config_diff(const struct nb_config
*config1
,
465 const struct nb_config
*config2
,
466 struct nb_config_cbs
*changes
)
468 struct lyd_difflist
*diff
;
471 diff
= lyd_diff(config1
->dnode
, config2
->dnode
,
472 LYD_DIFFOPT_WITHDEFAULTS
);
475 for (int i
= 0; diff
->type
[i
] != LYD_DIFF_END
; i
++) {
477 struct lyd_node
*dnode
;
479 type
= diff
->type
[i
];
482 case LYD_DIFF_CREATED
:
483 dnode
= diff
->second
[i
];
484 nb_config_diff_created(dnode
, &seq
, changes
);
486 case LYD_DIFF_DELETED
:
487 dnode
= diff
->first
[i
];
488 nb_config_diff_deleted(dnode
, &seq
, changes
);
490 case LYD_DIFF_CHANGED
:
491 dnode
= diff
->second
[i
];
492 nb_config_diff_add_change(changes
, NB_OP_MODIFY
, &seq
,
495 case LYD_DIFF_MOVEDAFTER1
:
496 case LYD_DIFF_MOVEDAFTER2
:
505 int nb_candidate_edit(struct nb_config
*candidate
,
506 const struct nb_node
*nb_node
,
507 enum nb_operation operation
, const char *xpath
,
508 const struct yang_data
*previous
,
509 const struct yang_data
*data
)
511 struct lyd_node
*dnode
;
512 char xpath_edit
[XPATH_MAXLEN
];
514 /* Use special notation for leaf-lists (RFC 6020, section 9.13.5). */
515 if (nb_node
->snode
->nodetype
== LYS_LEAFLIST
)
516 snprintf(xpath_edit
, sizeof(xpath_edit
), "%s[.='%s']", xpath
,
519 strlcpy(xpath_edit
, xpath
, sizeof(xpath_edit
));
525 dnode
= lyd_new_path(candidate
->dnode
, ly_native_ctx
,
526 xpath_edit
, (void *)data
->value
, 0,
527 LYD_PATH_OPT_UPDATE
);
528 if (!dnode
&& ly_errno
) {
529 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
535 dnode
= yang_dnode_get(candidate
->dnode
, xpath_edit
);
538 * Return a special error code so the caller can choose
539 * whether to ignore it or not.
541 return NB_ERR_NOT_FOUND
;
545 /* TODO: update configuration. */
548 flog_warn(EC_LIB_DEVELOPMENT
,
549 "%s: unknown operation (%u) [xpath %s]", __func__
,
550 operation
, xpath_edit
);
557 bool nb_candidate_needs_update(const struct nb_config
*candidate
)
559 if (candidate
->version
< running_config
->version
)
565 int nb_candidate_update(struct nb_config
*candidate
)
567 struct nb_config
*updated_config
;
569 updated_config
= nb_config_dup(running_config
);
570 if (nb_config_merge(updated_config
, candidate
, true) != NB_OK
)
573 nb_config_replace(candidate
, updated_config
, false);
579 * Perform YANG syntactic and semantic validation.
581 * WARNING: lyd_validate() can change the configuration as part of the
582 * validation process.
584 static int nb_candidate_validate_yang(struct nb_config
*candidate
)
586 if (lyd_validate(&candidate
->dnode
,
587 LYD_OPT_STRICT
| LYD_OPT_CONFIG
| LYD_OPT_WHENAUTODEL
,
590 return NB_ERR_VALIDATION
;
595 /* Perform code-level validation using the northbound callbacks. */
596 static int nb_candidate_validate_code(struct nb_config
*candidate
,
597 struct nb_config_cbs
*changes
)
599 struct nb_config_cb
*cb
;
600 struct lyd_node
*root
, *next
, *child
;
603 /* First validate the candidate as a whole. */
604 LY_TREE_FOR (candidate
->dnode
, root
) {
605 LY_TREE_DFS_BEGIN (root
, next
, child
) {
606 struct nb_node
*nb_node
;
608 nb_node
= child
->schema
->priv
;
609 if (!nb_node
->cbs
.pre_validate
)
612 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
,
614 char xpath
[XPATH_MAXLEN
];
616 yang_dnode_get_path(child
, xpath
,
618 nb_log_callback(NB_EV_VALIDATE
,
619 NB_OP_PRE_VALIDATE
, xpath
,
623 ret
= (*nb_node
->cbs
.pre_validate
)(child
);
625 return NB_ERR_VALIDATION
;
628 LY_TREE_DFS_END(root
, next
, child
);
632 /* Now validate the configuration changes. */
633 RB_FOREACH (cb
, nb_config_cbs
, changes
) {
634 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
636 ret
= nb_callback_configuration(NB_EV_VALIDATE
, change
);
638 return NB_ERR_VALIDATION
;
644 int nb_candidate_validate(struct nb_config
*candidate
)
646 struct nb_config_cbs changes
;
649 if (nb_candidate_validate_yang(candidate
) != NB_OK
)
650 return NB_ERR_VALIDATION
;
652 RB_INIT(nb_config_cbs
, &changes
);
653 nb_config_diff(running_config
, candidate
, &changes
);
654 ret
= nb_candidate_validate_code(candidate
, &changes
);
655 nb_config_diff_del_changes(&changes
);
660 int nb_candidate_commit_prepare(struct nb_config
*candidate
,
661 enum nb_client client
, const void *user
,
663 struct nb_transaction
**transaction
)
665 struct nb_config_cbs changes
;
667 if (nb_candidate_validate_yang(candidate
) != NB_OK
) {
668 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
669 "%s: failed to validate candidate configuration",
671 return NB_ERR_VALIDATION
;
674 RB_INIT(nb_config_cbs
, &changes
);
675 nb_config_diff(running_config
, candidate
, &changes
);
676 if (RB_EMPTY(nb_config_cbs
, &changes
))
677 return NB_ERR_NO_CHANGES
;
679 if (nb_candidate_validate_code(candidate
, &changes
) != NB_OK
) {
680 flog_warn(EC_LIB_NB_CANDIDATE_INVALID
,
681 "%s: failed to validate candidate configuration",
683 nb_config_diff_del_changes(&changes
);
684 return NB_ERR_VALIDATION
;
688 nb_transaction_new(candidate
, &changes
, client
, user
, comment
);
689 if (*transaction
== NULL
) {
690 flog_warn(EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
691 "%s: failed to create transaction", __func__
);
692 nb_config_diff_del_changes(&changes
);
693 return NB_ERR_LOCKED
;
696 return nb_transaction_process(NB_EV_PREPARE
, *transaction
);
699 void nb_candidate_commit_abort(struct nb_transaction
*transaction
)
701 (void)nb_transaction_process(NB_EV_ABORT
, transaction
);
702 nb_transaction_free(transaction
);
705 void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
706 bool save_transaction
, uint32_t *transaction_id
)
708 (void)nb_transaction_process(NB_EV_APPLY
, transaction
);
709 nb_transaction_apply_finish(transaction
);
711 /* Replace running by candidate. */
712 transaction
->config
->version
++;
713 nb_config_replace(running_config
, transaction
->config
, true);
715 /* Record transaction. */
717 && nb_db_transaction_save(transaction
, transaction_id
) != NB_OK
)
718 flog_warn(EC_LIB_NB_TRANSACTION_RECORD_FAILED
,
719 "%s: failed to record transaction", __func__
);
721 nb_transaction_free(transaction
);
724 int nb_candidate_commit(struct nb_config
*candidate
, enum nb_client client
,
725 const void *user
, bool save_transaction
,
726 const char *comment
, uint32_t *transaction_id
)
728 struct nb_transaction
*transaction
= NULL
;
731 ret
= nb_candidate_commit_prepare(candidate
, client
, user
, comment
,
734 * Apply the changes if the preparation phase succeeded. Otherwise abort
738 nb_candidate_commit_apply(transaction
, save_transaction
,
740 else if (transaction
!= NULL
)
741 nb_candidate_commit_abort(transaction
);
746 int nb_running_lock(enum nb_client client
, const void *user
)
750 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
751 if (!running_config_mgmt_lock
.locked
) {
752 running_config_mgmt_lock
.locked
= true;
753 running_config_mgmt_lock
.owner_client
= client
;
754 running_config_mgmt_lock
.owner_user
= user
;
762 int nb_running_unlock(enum nb_client client
, const void *user
)
766 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
767 if (running_config_mgmt_lock
.locked
768 && running_config_mgmt_lock
.owner_client
== client
769 && running_config_mgmt_lock
.owner_user
== user
) {
770 running_config_mgmt_lock
.locked
= false;
771 running_config_mgmt_lock
.owner_client
= NB_CLIENT_NONE
;
772 running_config_mgmt_lock
.owner_user
= NULL
;
780 int nb_running_lock_check(enum nb_client client
, const void *user
)
784 frr_with_mutex(&running_config_mgmt_lock
.mtx
) {
785 if (!running_config_mgmt_lock
.locked
786 || (running_config_mgmt_lock
.owner_client
== client
787 && running_config_mgmt_lock
.owner_user
== user
))
794 static void nb_log_callback(const enum nb_event event
,
795 enum nb_operation operation
, const char *xpath
,
799 "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
800 nb_event_name(event
), nb_operation_name(operation
), xpath
,
801 value
? value
: "(NULL)");
805 * Call the northbound configuration callback associated to a given
806 * configuration change.
808 static int nb_callback_configuration(const enum nb_event event
,
809 struct nb_config_change
*change
)
811 enum nb_operation operation
= change
->cb
.operation
;
812 char xpath
[XPATH_MAXLEN
];
813 const struct nb_node
*nb_node
= change
->cb
.nb_node
;
814 const struct lyd_node
*dnode
= change
->cb
.dnode
;
815 union nb_resource
*resource
;
818 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
819 const char *value
= "(none)";
821 if (dnode
&& !yang_snode_is_typeless_data(dnode
->schema
))
822 value
= yang_dnode_get_string(dnode
, NULL
);
824 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
825 nb_log_callback(event
, operation
, xpath
, value
);
828 if (event
== NB_EV_VALIDATE
)
831 resource
= &change
->resource
;
835 ret
= (*nb_node
->cbs
.create
)(event
, dnode
, resource
);
838 ret
= (*nb_node
->cbs
.modify
)(event
, dnode
, resource
);
841 ret
= (*nb_node
->cbs
.destroy
)(event
, dnode
);
844 ret
= (*nb_node
->cbs
.move
)(event
, dnode
);
847 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
848 flog_err(EC_LIB_DEVELOPMENT
,
849 "%s: unknown operation (%u) [xpath %s]", __func__
,
856 enum lib_log_refs ref
;
858 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
862 priority
= LOG_WARNING
;
863 ref
= EC_LIB_NB_CB_CONFIG_VALIDATE
;
866 priority
= LOG_WARNING
;
867 ref
= EC_LIB_NB_CB_CONFIG_PREPARE
;
870 priority
= LOG_WARNING
;
871 ref
= EC_LIB_NB_CB_CONFIG_ABORT
;
875 ref
= EC_LIB_NB_CB_CONFIG_APPLY
;
878 flog_err(EC_LIB_DEVELOPMENT
,
879 "%s: unknown event (%u) [xpath %s]",
880 __func__
, event
, xpath
);
885 "%s: error processing configuration change: error [%s] event [%s] operation [%s] xpath [%s]",
886 __func__
, nb_err_name(ret
), nb_event_name(event
),
887 nb_operation_name(operation
), xpath
);
893 struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
895 const void *list_entry
)
897 DEBUGD(&nb_dbg_cbs_state
,
898 "northbound callback (get_elem): xpath [%s] list_entry [%p]",
901 return nb_node
->cbs
.get_elem(xpath
, list_entry
);
904 const void *nb_callback_get_next(const struct nb_node
*nb_node
,
905 const void *parent_list_entry
,
906 const void *list_entry
)
908 DEBUGD(&nb_dbg_cbs_state
,
909 "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
910 nb_node
->xpath
, parent_list_entry
, list_entry
);
912 return nb_node
->cbs
.get_next(parent_list_entry
, list_entry
);
915 int nb_callback_get_keys(const struct nb_node
*nb_node
, const void *list_entry
,
916 struct yang_list_keys
*keys
)
918 DEBUGD(&nb_dbg_cbs_state
,
919 "northbound callback (get_keys): node [%s] list_entry [%p]",
920 nb_node
->xpath
, list_entry
);
922 return nb_node
->cbs
.get_keys(list_entry
, keys
);
925 const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
926 const void *parent_list_entry
,
927 const struct yang_list_keys
*keys
)
929 DEBUGD(&nb_dbg_cbs_state
,
930 "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
931 nb_node
->xpath
, parent_list_entry
);
933 return nb_node
->cbs
.lookup_entry(parent_list_entry
, keys
);
936 int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
937 const struct list
*input
, struct list
*output
)
939 DEBUGD(&nb_dbg_cbs_rpc
, "northbound RPC: %s", xpath
);
941 return nb_node
->cbs
.rpc(xpath
, input
, output
);
944 static struct nb_transaction
*
945 nb_transaction_new(struct nb_config
*config
, struct nb_config_cbs
*changes
,
946 enum nb_client client
, const void *user
, const char *comment
)
948 struct nb_transaction
*transaction
;
950 if (nb_running_lock_check(client
, user
)) {
952 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
953 "%s: running configuration is locked by another client",
958 if (transaction_in_progress
) {
960 EC_LIB_NB_TRANSACTION_CREATION_FAILED
,
961 "%s: error - there's already another transaction in progress",
965 transaction_in_progress
= true;
967 transaction
= XCALLOC(MTYPE_TMP
, sizeof(*transaction
));
968 transaction
->client
= client
;
970 strlcpy(transaction
->comment
, comment
,
971 sizeof(transaction
->comment
));
972 transaction
->config
= config
;
973 transaction
->changes
= *changes
;
978 static void nb_transaction_free(struct nb_transaction
*transaction
)
980 nb_config_diff_del_changes(&transaction
->changes
);
981 XFREE(MTYPE_TMP
, transaction
);
982 transaction_in_progress
= false;
985 /* Process all configuration changes associated to a transaction. */
986 static int nb_transaction_process(enum nb_event event
,
987 struct nb_transaction
*transaction
)
989 struct nb_config_cb
*cb
;
991 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
992 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
996 * Only try to release resources that were allocated
999 if (event
== NB_EV_ABORT
&& change
->prepare_ok
== false)
1002 /* Call the appropriate callback. */
1003 ret
= nb_callback_configuration(event
, change
);
1008 change
->prepare_ok
= true;
1013 * At this point it's not possible to reject the
1014 * transaction anymore, so any failure here can lead to
1015 * inconsistencies and should be treated as a bug.
1016 * Operations prone to errors, like validations and
1017 * resource allocations, should be performed during the
1029 static struct nb_config_cb
*
1030 nb_apply_finish_cb_new(struct nb_config_cbs
*cbs
, const struct nb_node
*nb_node
,
1031 const struct lyd_node
*dnode
)
1033 struct nb_config_cb
*cb
;
1035 cb
= XCALLOC(MTYPE_TMP
, sizeof(*cb
));
1036 cb
->nb_node
= nb_node
;
1038 RB_INSERT(nb_config_cbs
, cbs
, cb
);
1043 static struct nb_config_cb
*
1044 nb_apply_finish_cb_find(struct nb_config_cbs
*cbs
,
1045 const struct nb_node
*nb_node
,
1046 const struct lyd_node
*dnode
)
1048 struct nb_config_cb s
;
1051 s
.nb_node
= nb_node
;
1053 return RB_FIND(nb_config_cbs
, cbs
, &s
);
1056 /* Call the 'apply_finish' callbacks. */
1057 static void nb_transaction_apply_finish(struct nb_transaction
*transaction
)
1059 struct nb_config_cbs cbs
;
1060 struct nb_config_cb
*cb
;
1061 char xpath
[XPATH_MAXLEN
];
1063 /* Initialize tree of 'apply_finish' callbacks. */
1064 RB_INIT(nb_config_cbs
, &cbs
);
1066 /* Identify the 'apply_finish' callbacks that need to be called. */
1067 RB_FOREACH (cb
, nb_config_cbs
, &transaction
->changes
) {
1068 struct nb_config_change
*change
= (struct nb_config_change
*)cb
;
1069 const struct lyd_node
*dnode
= change
->cb
.dnode
;
1072 * Iterate up to the root of the data tree. When a node is being
1073 * deleted, skip its 'apply_finish' callback if one is defined
1074 * (the 'apply_finish' callbacks from the node ancestors should
1075 * be called though).
1077 if (change
->cb
.operation
== NB_OP_DESTROY
) {
1078 dnode
= dnode
->parent
;
1083 * The dnode from 'delete' callbacks point to elements
1084 * from the running configuration. Use yang_dnode_get()
1085 * to get the corresponding dnode from the candidate
1086 * configuration that is being committed.
1088 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
1089 dnode
= yang_dnode_get(transaction
->config
->dnode
,
1093 struct nb_node
*nb_node
;
1095 nb_node
= dnode
->schema
->priv
;
1096 if (!nb_node
->cbs
.apply_finish
)
1100 * Don't call the callback more than once for the same
1103 if (nb_apply_finish_cb_find(&cbs
, nb_node
, dnode
))
1106 nb_apply_finish_cb_new(&cbs
, nb_node
, dnode
);
1109 dnode
= dnode
->parent
;
1113 /* Call the 'apply_finish' callbacks, sorted by their priorities. */
1114 RB_FOREACH (cb
, nb_config_cbs
, &cbs
) {
1115 if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config
, DEBUG_MODE_ALL
)) {
1116 yang_dnode_get_path(cb
->dnode
, xpath
, sizeof(xpath
));
1117 nb_log_callback(NB_EV_APPLY
, NB_OP_APPLY_FINISH
, xpath
,
1121 (*cb
->nb_node
->cbs
.apply_finish
)(cb
->dnode
);
1124 /* Release memory. */
1125 while (!RB_EMPTY(nb_config_cbs
, &cbs
)) {
1126 cb
= RB_ROOT(nb_config_cbs
, &cbs
);
1127 RB_REMOVE(nb_config_cbs
, &cbs
, cb
);
1128 XFREE(MTYPE_TMP
, cb
);
1132 static int nb_oper_data_iter_children(const struct lys_node
*snode
,
1133 const char *xpath
, const void *list_entry
,
1134 const struct yang_list_keys
*list_keys
,
1135 struct yang_translator
*translator
,
1136 bool first
, uint32_t flags
,
1137 nb_oper_data_cb cb
, void *arg
)
1139 struct lys_node
*child
;
1141 LY_TREE_FOR (snode
->child
, child
) {
1144 ret
= nb_oper_data_iter_node(child
, xpath
, list_entry
,
1145 list_keys
, translator
, false,
1154 static int nb_oper_data_iter_leaf(const struct nb_node
*nb_node
,
1155 const char *xpath
, const void *list_entry
,
1156 const struct yang_list_keys
*list_keys
,
1157 struct yang_translator
*translator
,
1158 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1160 struct yang_data
*data
;
1162 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1165 /* Ignore list keys. */
1166 if (lys_is_key((struct lys_node_leaf
*)nb_node
->snode
, NULL
))
1169 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1171 /* Leaf of type "empty" is not present. */
1174 return (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1177 static int nb_oper_data_iter_container(const struct nb_node
*nb_node
,
1179 const void *list_entry
,
1180 const struct yang_list_keys
*list_keys
,
1181 struct yang_translator
*translator
,
1182 uint32_t flags
, nb_oper_data_cb cb
,
1185 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1188 /* Presence containers. */
1189 if (nb_node
->cbs
.get_elem
) {
1190 struct yang_data
*data
;
1193 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1195 /* Presence container is not present. */
1198 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1203 /* Iterate over the child nodes. */
1204 return nb_oper_data_iter_children(nb_node
->snode
, xpath
, list_entry
,
1205 list_keys
, translator
, false, flags
,
1210 nb_oper_data_iter_leaflist(const struct nb_node
*nb_node
, const char *xpath
,
1211 const void *parent_list_entry
,
1212 const struct yang_list_keys
*parent_list_keys
,
1213 struct yang_translator
*translator
, uint32_t flags
,
1214 nb_oper_data_cb cb
, void *arg
)
1216 const void *list_entry
= NULL
;
1218 if (CHECK_FLAG(nb_node
->snode
->flags
, LYS_CONFIG_W
))
1222 struct yang_data
*data
;
1225 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1228 /* End of the list. */
1231 data
= nb_callback_get_elem(nb_node
, xpath
, list_entry
);
1235 ret
= (*cb
)(nb_node
->snode
, translator
, data
, arg
);
1238 } while (list_entry
);
1243 static int nb_oper_data_iter_list(const struct nb_node
*nb_node
,
1244 const char *xpath_list
,
1245 const void *parent_list_entry
,
1246 const struct yang_list_keys
*parent_list_keys
,
1247 struct yang_translator
*translator
,
1248 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1250 struct lys_node_list
*slist
= (struct lys_node_list
*)nb_node
->snode
;
1251 const void *list_entry
= NULL
;
1252 uint32_t position
= 1;
1254 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1257 /* Iterate over all list entries. */
1259 struct yang_list_keys list_keys
;
1260 char xpath
[XPATH_MAXLEN
* 2];
1263 /* Obtain list entry. */
1264 list_entry
= nb_callback_get_next(nb_node
, parent_list_entry
,
1267 /* End of the list. */
1270 if (!CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
)) {
1271 /* Obtain the list entry keys. */
1272 if (nb_callback_get_keys(nb_node
, list_entry
,
1275 flog_warn(EC_LIB_NB_CB_STATE
,
1276 "%s: failed to get list keys",
1281 /* Build XPath of the list entry. */
1282 strlcpy(xpath
, xpath_list
, sizeof(xpath
));
1283 for (unsigned int i
= 0; i
< list_keys
.num
; i
++) {
1284 snprintf(xpath
+ strlen(xpath
),
1285 sizeof(xpath
) - strlen(xpath
),
1286 "[%s='%s']", slist
->keys
[i
]->name
,
1291 * Keyless list - build XPath using a positional index.
1293 snprintf(xpath
, sizeof(xpath
), "%s[%u]", xpath_list
,
1298 /* Iterate over the child nodes. */
1299 ret
= nb_oper_data_iter_children(
1300 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1301 translator
, false, flags
, cb
, arg
);
1304 } while (list_entry
);
1309 static int nb_oper_data_iter_node(const struct lys_node
*snode
,
1310 const char *xpath_parent
,
1311 const void *list_entry
,
1312 const struct yang_list_keys
*list_keys
,
1313 struct yang_translator
*translator
,
1314 bool first
, uint32_t flags
,
1315 nb_oper_data_cb cb
, void *arg
)
1317 struct nb_node
*nb_node
;
1318 char xpath
[XPATH_MAXLEN
];
1321 if (!first
&& CHECK_FLAG(flags
, NB_OPER_DATA_ITER_NORECURSE
)
1322 && CHECK_FLAG(snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
))
1326 strlcpy(xpath
, xpath_parent
, sizeof(xpath
));
1327 if (!first
&& snode
->nodetype
!= LYS_USES
) {
1328 struct lys_node
*parent
;
1330 /* Get the real parent. */
1331 parent
= snode
->parent
;
1332 while (parent
&& parent
->nodetype
== LYS_USES
)
1333 parent
= parent
->parent
;
1336 * When necessary, include the namespace of the augmenting
1339 if (parent
&& parent
->nodetype
== LYS_AUGMENT
)
1340 snprintf(xpath
+ strlen(xpath
),
1341 sizeof(xpath
) - strlen(xpath
), "/%s:%s",
1342 snode
->module
->name
, snode
->name
);
1344 snprintf(xpath
+ strlen(xpath
),
1345 sizeof(xpath
) - strlen(xpath
), "/%s",
1349 nb_node
= snode
->priv
;
1350 switch (snode
->nodetype
) {
1352 ret
= nb_oper_data_iter_container(nb_node
, xpath
, list_entry
,
1353 list_keys
, translator
, flags
,
1357 ret
= nb_oper_data_iter_leaf(nb_node
, xpath
, list_entry
,
1358 list_keys
, translator
, flags
, cb
,
1362 ret
= nb_oper_data_iter_leaflist(nb_node
, xpath
, list_entry
,
1363 list_keys
, translator
, flags
,
1367 ret
= nb_oper_data_iter_list(nb_node
, xpath
, list_entry
,
1368 list_keys
, translator
, flags
, cb
,
1372 ret
= nb_oper_data_iter_children(snode
, xpath
, list_entry
,
1373 list_keys
, translator
, false,
1383 int nb_oper_data_iterate(const char *xpath
, struct yang_translator
*translator
,
1384 uint32_t flags
, nb_oper_data_cb cb
, void *arg
)
1386 struct nb_node
*nb_node
;
1387 const void *list_entry
= NULL
;
1388 struct yang_list_keys list_keys
;
1389 struct list
*list_dnodes
;
1390 struct lyd_node
*dnode
, *dn
;
1391 struct listnode
*ln
;
1394 nb_node
= nb_node_find(xpath
);
1396 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1397 "%s: unknown data path: %s", __func__
, xpath
);
1401 /* For now this function works only with containers and lists. */
1402 if (!CHECK_FLAG(nb_node
->snode
->nodetype
, LYS_CONTAINER
| LYS_LIST
)) {
1404 EC_LIB_NB_OPERATIONAL_DATA
,
1405 "%s: can't iterate over YANG leaf or leaf-list [xpath %s]",
1411 * Create a data tree from the XPath so that we can parse the keys of
1412 * all YANG lists (if any).
1415 dnode
= lyd_new_path(NULL
, ly_native_ctx
, xpath
, NULL
, 0,
1416 LYD_PATH_OPT_UPDATE
| LYD_PATH_OPT_NOPARENTRET
);
1418 flog_warn(EC_LIB_LIBYANG
, "%s: lyd_new_path() failed",
1424 * Create a linked list to sort the data nodes starting from the root.
1426 list_dnodes
= list_new();
1427 for (dn
= dnode
; dn
; dn
= dn
->parent
) {
1428 if (dn
->schema
->nodetype
!= LYS_LIST
|| !dn
->child
)
1430 listnode_add_head(list_dnodes
, dn
);
1433 * Use the northbound callbacks to find list entry pointer corresponding
1434 * to the given XPath.
1436 for (ALL_LIST_ELEMENTS_RO(list_dnodes
, ln
, dn
)) {
1437 struct lyd_node
*child
;
1441 /* Obtain the list entry keys. */
1442 memset(&list_keys
, 0, sizeof(list_keys
));
1443 LY_TREE_FOR (dn
->child
, child
) {
1444 if (!lys_is_key((struct lys_node_leaf
*)child
->schema
,
1447 strlcpy(list_keys
.key
[n
],
1448 yang_dnode_get_string(child
, NULL
),
1449 sizeof(list_keys
.key
[n
]));
1454 != ((struct lys_node_list
*)dn
->schema
)->keys_size
) {
1455 list_delete(&list_dnodes
);
1456 yang_dnode_free(dnode
);
1457 return NB_ERR_NOT_FOUND
;
1460 /* Find the list entry pointer. */
1461 nn
= dn
->schema
->priv
;
1463 nb_callback_lookup_entry(nn
, list_entry
, &list_keys
);
1464 if (list_entry
== NULL
) {
1465 list_delete(&list_dnodes
);
1466 yang_dnode_free(dnode
);
1467 return NB_ERR_NOT_FOUND
;
1471 /* If a list entry was given, iterate over that list entry only. */
1472 if (dnode
->schema
->nodetype
== LYS_LIST
&& dnode
->child
)
1473 ret
= nb_oper_data_iter_children(
1474 nb_node
->snode
, xpath
, list_entry
, &list_keys
,
1475 translator
, true, flags
, cb
, arg
);
1477 ret
= nb_oper_data_iter_node(nb_node
->snode
, xpath
, list_entry
,
1478 &list_keys
, translator
, true,
1481 list_delete(&list_dnodes
);
1482 yang_dnode_free(dnode
);
1487 bool nb_operation_is_valid(enum nb_operation operation
,
1488 const struct lys_node
*snode
)
1490 struct nb_node
*nb_node
= snode
->priv
;
1491 struct lys_node_container
*scontainer
;
1492 struct lys_node_leaf
*sleaf
;
1494 switch (operation
) {
1496 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1499 switch (snode
->nodetype
) {
1501 sleaf
= (struct lys_node_leaf
*)snode
;
1502 if (sleaf
->type
.base
!= LY_TYPE_EMPTY
)
1506 scontainer
= (struct lys_node_container
*)snode
;
1507 if (!scontainer
->presence
)
1518 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1521 switch (snode
->nodetype
) {
1523 sleaf
= (struct lys_node_leaf
*)snode
;
1524 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
1527 /* List keys can't be modified. */
1528 if (lys_is_key(sleaf
, NULL
))
1536 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1539 switch (snode
->nodetype
) {
1541 sleaf
= (struct lys_node_leaf
*)snode
;
1543 /* List keys can't be deleted. */
1544 if (lys_is_key(sleaf
, NULL
))
1548 * Only optional leafs can be deleted, or leafs whose
1549 * parent is a case statement.
1551 if (snode
->parent
->nodetype
== LYS_CASE
)
1555 if (CHECK_FLAG(sleaf
->flags
, LYS_MAND_TRUE
)
1560 scontainer
= (struct lys_node_container
*)snode
;
1561 if (!scontainer
->presence
)
1572 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1575 switch (snode
->nodetype
) {
1578 if (!CHECK_FLAG(snode
->flags
, LYS_USERORDERED
))
1585 case NB_OP_PRE_VALIDATE
:
1586 case NB_OP_APPLY_FINISH
:
1587 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1590 case NB_OP_GET_ELEM
:
1591 if (!CHECK_FLAG(snode
->flags
, LYS_CONFIG_R
))
1594 switch (snode
->nodetype
) {
1599 scontainer
= (struct lys_node_container
*)snode
;
1600 if (!scontainer
->presence
)
1607 case NB_OP_GET_NEXT
:
1608 switch (snode
->nodetype
) {
1610 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1614 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
))
1621 case NB_OP_GET_KEYS
:
1622 case NB_OP_LOOKUP_ENTRY
:
1623 switch (snode
->nodetype
) {
1625 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_CONFIG_ONLY
))
1627 if (CHECK_FLAG(nb_node
->flags
, F_NB_NODE_KEYLESS_LIST
))
1635 if (CHECK_FLAG(snode
->flags
, LYS_CONFIG_W
| LYS_CONFIG_R
))
1638 switch (snode
->nodetype
) {
1651 DEFINE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
1652 (xpath
, arguments
));
1654 int nb_notification_send(const char *xpath
, struct list
*arguments
)
1658 DEBUGD(&nb_dbg_notif
, "northbound notification: %s", xpath
);
1660 ret
= hook_call(nb_notification_send
, xpath
, arguments
);
1662 list_delete(&arguments
);
1667 /* Running configuration user pointers management. */
1668 struct nb_config_entry
{
1669 char xpath
[XPATH_MAXLEN
];
1673 static bool running_config_entry_cmp(const void *value1
, const void *value2
)
1675 const struct nb_config_entry
*c1
= value1
;
1676 const struct nb_config_entry
*c2
= value2
;
1678 return strmatch(c1
->xpath
, c2
->xpath
);
1681 static unsigned int running_config_entry_key_make(const void *value
)
1683 return string_hash_make(value
);
1686 static void *running_config_entry_alloc(void *p
)
1688 struct nb_config_entry
*new, *key
= p
;
1690 new = XCALLOC(MTYPE_NB_CONFIG_ENTRY
, sizeof(*new));
1691 strlcpy(new->xpath
, key
->xpath
, sizeof(new->xpath
));
1696 static void running_config_entry_free(void *arg
)
1698 XFREE(MTYPE_NB_CONFIG_ENTRY
, arg
);
1701 void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
)
1703 struct nb_config_entry
*config
, s
;
1705 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1706 config
= hash_get(running_config_entries
, &s
,
1707 running_config_entry_alloc
);
1708 config
->entry
= entry
;
1711 static void *nb_running_unset_entry_helper(const struct lyd_node
*dnode
)
1713 struct nb_config_entry
*config
, s
;
1714 struct lyd_node
*child
;
1717 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1718 config
= hash_release(running_config_entries
, &s
);
1720 entry
= config
->entry
;
1721 running_config_entry_free(config
);
1724 /* Unset user pointers from the child nodes. */
1725 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
)) {
1726 LY_TREE_FOR (dnode
->child
, child
) {
1727 (void)nb_running_unset_entry_helper(child
);
1734 void *nb_running_unset_entry(const struct lyd_node
*dnode
)
1738 entry
= nb_running_unset_entry_helper(dnode
);
1744 void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
1745 bool abort_if_not_found
)
1747 const struct lyd_node
*orig_dnode
= dnode
;
1748 char xpath_buf
[XPATH_MAXLEN
];
1750 assert(dnode
|| xpath
);
1753 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
1756 struct nb_config_entry
*config
, s
;
1758 yang_dnode_get_path(dnode
, s
.xpath
, sizeof(s
.xpath
));
1759 config
= hash_lookup(running_config_entries
, &s
);
1761 return config
->entry
;
1763 dnode
= dnode
->parent
;
1766 if (!abort_if_not_found
)
1769 yang_dnode_get_path(orig_dnode
, xpath_buf
, sizeof(xpath_buf
));
1770 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
1771 "%s: failed to find entry [xpath %s]", __func__
, xpath_buf
);
1772 zlog_backtrace(LOG_ERR
);
1776 /* Logging functions. */
1777 const char *nb_event_name(enum nb_event event
)
1780 case NB_EV_VALIDATE
:
1793 const char *nb_operation_name(enum nb_operation operation
)
1795 switch (operation
) {
1804 case NB_OP_PRE_VALIDATE
:
1805 return "pre_validate";
1806 case NB_OP_APPLY_FINISH
:
1807 return "apply_finish";
1808 case NB_OP_GET_ELEM
:
1810 case NB_OP_GET_NEXT
:
1812 case NB_OP_GET_KEYS
:
1814 case NB_OP_LOOKUP_ENTRY
:
1815 return "lookup_entry";
1823 const char *nb_err_name(enum nb_error error
)
1829 return "generic error";
1830 case NB_ERR_NO_CHANGES
:
1831 return "no changes";
1832 case NB_ERR_NOT_FOUND
:
1833 return "element not found";
1835 return "resource is locked";
1836 case NB_ERR_VALIDATION
:
1837 return "validation error";
1838 case NB_ERR_RESOURCE
:
1839 return "failed to allocate resource";
1840 case NB_ERR_INCONSISTENCY
:
1841 return "internal inconsistency";
1847 const char *nb_client_name(enum nb_client client
)
1852 case NB_CLIENT_CONFD
:
1854 case NB_CLIENT_SYSREPO
:
1856 case NB_CLIENT_GRPC
:
1863 static void nb_load_callbacks(const struct frr_yang_module_info
*module
)
1865 for (size_t i
= 0; module
->nodes
[i
].xpath
; i
++) {
1866 struct nb_node
*nb_node
;
1869 nb_node
= nb_node_find(module
->nodes
[i
].xpath
);
1871 flog_warn(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
1872 "%s: unknown data path: %s", __func__
,
1873 module
->nodes
[i
].xpath
);
1877 nb_node
->cbs
= module
->nodes
[i
].cbs
;
1878 priority
= module
->nodes
[i
].priority
;
1880 nb_node
->priority
= priority
;
1884 void nb_init(struct thread_master
*tm
,
1885 const struct frr_yang_module_info
*const modules
[],
1888 unsigned int errors
= 0;
1890 /* Load YANG modules. */
1891 for (size_t i
= 0; i
< nmodules
; i
++)
1892 yang_module_load(modules
[i
]->name
);
1894 /* Create a nb_node for all YANG schema nodes. */
1897 /* Load northbound callbacks. */
1898 for (size_t i
= 0; i
< nmodules
; i
++)
1899 nb_load_callbacks(modules
[i
]);
1901 /* Validate northbound callbacks. */
1902 yang_snodes_iterate_all(nb_node_validate
, 0, &errors
);
1905 EC_LIB_NB_CBS_VALIDATION
,
1906 "%s: failed to validate northbound callbacks: %u error(s)",
1911 /* Create an empty running configuration. */
1912 running_config
= nb_config_new(NULL
);
1913 running_config_entries
= hash_create(running_config_entry_key_make
,
1914 running_config_entry_cmp
,
1915 "Running Configuration Entries");
1916 pthread_mutex_init(&running_config_mgmt_lock
.mtx
, NULL
);
1918 /* Initialize the northbound CLI. */
1922 void nb_terminate(void)
1924 /* Terminate the northbound CLI. */
1927 /* Delete all nb_node's from all YANG modules. */
1930 /* Delete the running configuration. */
1931 hash_clean(running_config_entries
, running_config_entry_free
);
1932 hash_free(running_config_entries
);
1933 nb_config_free(running_config
);
1934 pthread_mutex_destroy(&running_config_mgmt_lock
.mtx
);