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
20 #ifndef _FRR_NORTHBOUND_H_
21 #define _FRR_NORTHBOUND_H_
26 #include "openbsd-tree.h"
28 #include "yang_translator.h"
34 /* Forward declaration(s). */
38 /* Northbound events. */
41 * The configuration callback is supposed to verify that the changes are
42 * valid and can be applied.
47 * The configuration callback is supposed to prepare all resources
48 * required to apply the changes.
53 * Transaction has failed, the configuration callback needs to release
54 * all resources previously allocated.
59 * The configuration changes need to be applied. The changes can't be
60 * rejected at this point (errors are logged and ignored).
66 * Northbound operations.
68 * Refer to the documentation comments of nb_callbacks for more details.
91 * Configuration callback.
93 * A presence container, list entry, leaf-list entry or leaf of type
94 * empty has been created.
96 * For presence-containers and list entries, the callback is supposed to
97 * initialize the default values of its children (if any) from the YANG
101 * The transaction phase. Refer to the documentation comments of
102 * nb_event for more details.
105 * libyang data node that is being created.
108 * Pointer to store resource(s) allocated during the NB_EV_PREPARE
109 * phase. The same pointer can be used during the NB_EV_ABORT and
110 * NB_EV_APPLY phases to either release or make use of the allocated
111 * resource(s). It's set to NULL when the event is NB_EV_VALIDATE.
114 * - NB_OK on success.
115 * - NB_ERR_VALIDATION when a validation error occurred.
116 * - NB_ERR_RESOURCE when the callback failed to allocate a resource.
117 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
118 * - NB_ERR for other errors.
120 int (*create
)(enum nb_event event
, const struct lyd_node
*dnode
,
121 union nb_resource
*resource
);
124 * Configuration callback.
126 * The value of a leaf has been modified.
128 * List keys don't need to implement this callback. When a list key is
129 * modified, the northbound treats this as if the list was deleted and a
130 * new one created with the updated key value.
133 * The transaction phase. Refer to the documentation comments of
134 * nb_event for more details.
137 * libyang data node that is being modified
140 * Pointer to store resource(s) allocated during the NB_EV_PREPARE
141 * phase. The same pointer can be used during the NB_EV_ABORT and
142 * NB_EV_APPLY phases to either release or make use of the allocated
143 * resource(s). It's set to NULL when the event is NB_EV_VALIDATE.
146 * - NB_OK on success.
147 * - NB_ERR_VALIDATION when a validation error occurred.
148 * - NB_ERR_RESOURCE when the callback failed to allocate a resource.
149 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
150 * - NB_ERR for other errors.
152 int (*modify
)(enum nb_event event
, const struct lyd_node
*dnode
,
153 union nb_resource
*resource
);
156 * Configuration callback.
158 * A presence container, list entry, leaf-list entry or optional leaf
161 * The callback is supposed to delete the entire configuration object,
162 * including its children when they exist.
165 * The transaction phase. Refer to the documentation comments of
166 * nb_event for more details.
169 * libyang data node that is being deleted.
172 * - NB_OK on success.
173 * - NB_ERR_VALIDATION when a validation error occurred.
174 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
175 * - NB_ERR for other errors.
177 int (*destroy
)(enum nb_event event
, const struct lyd_node
*dnode
);
180 * Configuration callback.
182 * A list entry or leaf-list entry has been moved. Only applicable when
183 * the "ordered-by user" statement is present.
186 * The transaction phase. Refer to the documentation comments of
187 * nb_event for more details.
190 * libyang data node that is being moved.
193 * - NB_OK on success.
194 * - NB_ERR_VALIDATION when a validation error occurred.
195 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
196 * - NB_ERR for other errors.
198 int (*move
)(enum nb_event event
, const struct lyd_node
*dnode
);
201 * Optional configuration callback.
203 * This callback can be used to validate subsections of the
204 * configuration being committed before validating the configuration
205 * changes themselves. It's useful to perform more complex validations
206 * that depend on the relationship between multiple nodes.
209 * libyang data node associated with the 'pre_validate' callback.
211 int (*pre_validate
)(const struct lyd_node
*dnode
);
214 * Optional configuration callback.
216 * The 'apply_finish' callbacks are called after all other callbacks
217 * during the apply phase (NB_EV_APPLY). These callbacks are called only
218 * under one of the following two cases:
219 * - The data node has been created or modified (but not deleted);
220 * - Any change was made within the descendants of the data node (e.g. a
221 * child leaf was modified, created or deleted).
223 * In the second case above, the 'apply_finish' callback is called only
224 * once even if multiple changes occurred within the descendants of the
228 * libyang data node associated with the 'apply_finish' callback.
230 void (*apply_finish
)(const struct lyd_node
*dnode
);
233 * Operational data callback.
235 * The callback function should return the value of a specific leaf,
236 * leaf-list entry or inform if a typeless value (presence containers or
237 * leafs of type empty) exists or not.
240 * YANG data path of the data we want to get.
243 * Pointer to list entry (might be NULL).
246 * Pointer to newly created yang_data structure, or NULL to indicate
247 * the absence of data.
249 struct yang_data
*(*get_elem
)(const char *xpath
,
250 const void *list_entry
);
253 * Operational data callback for YANG lists and leaf-lists.
255 * The callback function should return the next entry in the list or
256 * leaf-list. The 'list_entry' parameter will be NULL on the first
260 * Pointer to parent list entry.
263 * Pointer to (leaf-)list entry.
266 * Pointer to the next entry in the (leaf-)list, or NULL to signal
267 * that the end of the (leaf-)list was reached.
269 const void *(*get_next
)(const void *parent_list_entry
,
270 const void *list_entry
);
273 * Operational data callback for YANG lists.
275 * The callback function should fill the 'keys' parameter based on the
276 * given list_entry. Keyless lists don't need to implement this
280 * Pointer to list entry.
283 * Structure to be filled based on the attributes of the provided
287 * NB_OK on success, NB_ERR otherwise.
289 int (*get_keys
)(const void *list_entry
, struct yang_list_keys
*keys
);
292 * Operational data callback for YANG lists.
294 * The callback function should return a list entry based on the list
295 * keys given as a parameter. Keyless lists don't need to implement this
299 * Pointer to parent list entry.
302 * Structure containing the keys of the list entry.
305 * Pointer to the list entry if found, or NULL if not found.
307 const void *(*lookup_entry
)(const void *parent_list_entry
,
308 const struct yang_list_keys
*keys
);
311 * RPC and action callback.
313 * Both 'input' and 'output' are lists of 'yang_data' structures. The
314 * callback should fetch all the input parameters from the 'input' list,
315 * and add output parameters to the 'output' list if necessary.
318 * XPath of the YANG RPC or action.
321 * Read-only list of input parameters.
324 * List of output parameters to be populated by the callback.
327 * NB_OK on success, NB_ERR otherwise.
329 int (*rpc
)(const char *xpath
, const struct list
*input
,
330 struct list
*output
);
333 * Optional callback to show the CLI command associated to the given
337 * The vty terminal to dump the configuration to.
340 * libyang data node that should be shown in the form of a CLI
344 * Specify whether to display default configuration values or not.
345 * This parameter can be ignored most of the time since the
346 * northbound doesn't call this callback for default leaves or
347 * non-presence containers that contain only default child nodes.
348 * The exception are commands associated to multiple configuration
349 * nodes, in which case it might be desirable to hide one or more
350 * parts of the command when this parameter is set to false.
352 void (*cli_show
)(struct vty
*vty
, struct lyd_node
*dnode
,
356 * Optional callback to show the CLI node end for lists or containers.
359 * The vty terminal to dump the configuration to.
362 * libyang data node that should be shown in the form of a CLI
365 void (*cli_show_end
)(struct vty
*vty
, struct lyd_node
*dnode
);
369 * Northbound-specific data that is allocated for each schema node of the native
373 /* Back pointer to the libyang schema node. */
374 const struct lys_node
*snode
;
376 /* Data path of this YANG node. */
377 char xpath
[XPATH_MAXLEN
];
379 /* Priority - lower priorities are processed first. */
382 /* Callbacks implemented for this node. */
383 struct nb_callbacks cbs
;
386 * Pointer to the parent node (disconsidering non-presence containers).
388 struct nb_node
*parent
;
390 /* Pointer to the nearest parent list, if any. */
391 struct nb_node
*parent_list
;
397 /* ConfD hash value corresponding to this YANG path. */
401 /* The YANG container or list contains only config data. */
402 #define F_NB_NODE_CONFIG_ONLY 0x01
403 /* The YANG list doesn't contain key leafs. */
404 #define F_NB_NODE_KEYLESS_LIST 0x02
406 struct frr_yang_module_info
{
407 /* YANG module name. */
410 /* Northbound callbacks. */
412 /* Data path of this YANG node. */
415 /* Callbacks implemented for this node. */
416 struct nb_callbacks cbs
;
418 /* Priority - lower priorities are processed first. */
423 /* Northbound error codes. */
432 NB_ERR_INCONSISTENCY
,
435 /* Default priority. */
436 #define NB_DFLT_PRIORITY (UINT32_MAX / 2)
438 /* Default maximum of configuration rollbacks to store. */
439 #define NB_DLFT_MAX_CONFIG_ROLLBACKS 20
441 /* Northbound clients. */
450 /* Northbound configuration. */
452 struct lyd_node
*dnode
;
456 /* Northbound configuration callback. */
457 struct nb_config_cb
{
458 RB_ENTRY(nb_config_cb
) entry
;
459 enum nb_operation operation
;
461 const struct nb_node
*nb_node
;
462 const struct lyd_node
*dnode
;
464 RB_HEAD(nb_config_cbs
, nb_config_cb
);
465 RB_PROTOTYPE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
467 /* Northbound configuration change. */
468 struct nb_config_change
{
469 struct nb_config_cb cb
;
470 union nb_resource resource
;
474 /* Northbound configuration transaction. */
475 struct nb_transaction
{
476 enum nb_client client
;
478 struct nb_config
*config
;
479 struct nb_config_cbs changes
;
482 /* Callback function used by nb_oper_data_iterate(). */
483 typedef int (*nb_oper_data_cb
)(const struct lys_node
*snode
,
484 struct yang_translator
*translator
,
485 struct yang_data
*data
, void *arg
);
487 /* Iterate over direct child nodes only. */
488 #define NB_OPER_DATA_ITER_NORECURSE 0x0001
491 DECLARE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
493 DECLARE_HOOK(nb_client_debug_config_write
, (struct vty
*vty
), (vty
))
494 DECLARE_HOOK(nb_client_debug_set_all
, (uint32_t flags
, bool set
), (flags
, set
))
496 /* Northbound debugging records */
497 extern struct debug nb_dbg_cbs_config
;
498 extern struct debug nb_dbg_cbs_state
;
499 extern struct debug nb_dbg_cbs_rpc
;
500 extern struct debug nb_dbg_notif
;
501 extern struct debug nb_dbg_events
;
503 /* Global running configuration. */
504 extern struct nb_config
*running_config
;
506 /* Wrappers for the northbound callbacks. */
507 extern struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
509 const void *list_entry
);
510 extern const void *nb_callback_get_next(const struct nb_node
*nb_node
,
511 const void *parent_list_entry
,
512 const void *list_entry
);
513 extern int nb_callback_get_keys(const struct nb_node
*nb_node
,
514 const void *list_entry
,
515 struct yang_list_keys
*keys
);
516 extern const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
517 const void *parent_list_entry
,
518 const struct yang_list_keys
*keys
);
519 extern int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
520 const struct list
*input
, struct list
*output
);
523 * Create a northbound node for all YANG schema nodes.
525 void nb_nodes_create(void);
528 * Delete all northbound nodes from all YANG schema nodes.
530 void nb_nodes_delete(void);
533 * Find the northbound node corresponding to a YANG data path.
536 * XPath to search for (with or without predicates).
539 * Pointer to northbound node if found, NULL otherwise.
541 extern struct nb_node
*nb_node_find(const char *xpath
);
544 * Create a new northbound configuration.
547 * Pointer to a libyang data node containing the configuration data. If NULL
548 * is given, an empty configuration will be created.
551 * Pointer to newly created northbound configuration.
553 extern struct nb_config
*nb_config_new(struct lyd_node
*dnode
);
556 * Delete a northbound configuration.
559 * Pointer to the config that is going to be deleted.
561 extern void nb_config_free(struct nb_config
*config
);
564 * Duplicate a northbound configuration.
567 * Northbound configuration to duplicate.
570 * Pointer to duplicated configuration.
572 extern struct nb_config
*nb_config_dup(const struct nb_config
*config
);
575 * Merge one configuration into another.
578 * Configuration to merge to.
581 * Configuration to merge config_dst with.
584 * Specify whether config_src should be deleted or not after the merge
588 * NB_OK on success, NB_ERR otherwise.
590 extern int nb_config_merge(struct nb_config
*config_dst
,
591 struct nb_config
*config_src
, bool preserve_source
);
594 * Replace one configuration by another.
597 * Configuration to be replaced.
600 * Configuration to replace config_dst.
603 * Specify whether config_src should be deleted or not after the replace
606 extern void nb_config_replace(struct nb_config
*config_dst
,
607 struct nb_config
*config_src
,
608 bool preserve_source
);
611 * Edit a candidate configuration.
614 * Candidate configuration to edit.
617 * Northbound node associated to the configuration being edited.
620 * Operation to apply.
623 * XPath of the configuration node being edited.
626 * Previous value of the configuration node. Should be used only when the
627 * operation is NB_OP_MOVE, otherwise this parameter is ignored.
630 * New value of the configuration node.
633 * - NB_OK on success.
634 * - NB_ERR_NOT_FOUND when the element to be deleted was not found.
635 * - NB_ERR for other errors.
637 extern int nb_candidate_edit(struct nb_config
*candidate
,
638 const struct nb_node
*nb_node
,
639 enum nb_operation operation
, const char *xpath
,
640 const struct yang_data
*previous
,
641 const struct yang_data
*data
);
644 * Check if a candidate configuration is outdated and needs to be updated.
647 * Candidate configuration to check.
650 * true if the candidate is outdated, false otherwise.
652 extern bool nb_candidate_needs_update(const struct nb_config
*candidate
);
655 * Update a candidate configuration by rebasing the changes on top of the latest
656 * running configuration. Resolve conflicts automatically by giving preference
657 * to the changes done in the candidate configuration.
660 * Candidate configuration to update.
663 * NB_OK on success, NB_ERR otherwise.
665 extern int nb_candidate_update(struct nb_config
*candidate
);
668 * Validate a candidate configuration. Perform both YANG syntactic/semantic
669 * validation and code-level validation using the northbound callbacks.
671 * WARNING: the candidate can be modified as part of the validation process
672 * (e.g. add default nodes).
675 * Candidate configuration to validate.
678 * NB_OK on success, NB_ERR_VALIDATION otherwise.
680 extern int nb_candidate_validate(struct nb_config
*candidate
);
683 * Create a new configuration transaction but do not commit it yet. Only
684 * validate the candidate and prepare all resources required to apply the
685 * configuration changes.
688 * Candidate configuration to commit.
691 * Northbound client performing the commit.
694 * Northbound user performing the commit (can be NULL).
697 * Optional comment describing the commit.
700 * Output parameter providing the created transaction when one is created
701 * successfully. In this case, it must be either aborted using
702 * nb_candidate_commit_abort() or committed using
703 * nb_candidate_commit_apply().
706 * - NB_OK on success.
707 * - NB_ERR_NO_CHANGES when the candidate is identical to the running
709 * - NB_ERR_LOCKED when there's already another transaction in progress.
710 * - NB_ERR_VALIDATION when the candidate fails the validation checks.
711 * - NB_ERR_RESOURCE when the system fails to allocate resources to apply
712 * the candidate configuration.
713 * - NB_ERR for other errors.
715 extern int nb_candidate_commit_prepare(struct nb_config
*candidate
,
716 enum nb_client client
, const void *user
,
718 struct nb_transaction
**transaction
);
721 * Abort a previously created configuration transaction, releasing all resources
722 * allocated during the preparation phase.
725 * Candidate configuration to abort. It's consumed by this function.
727 extern void nb_candidate_commit_abort(struct nb_transaction
*transaction
);
730 * Commit a previously created configuration transaction.
733 * Configuration transaction to commit. It's consumed by this function.
736 * Specify whether the transaction should be recorded in the transactions log
740 * Optional output parameter providing the ID of the committed transaction.
742 extern void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
743 bool save_transaction
,
744 uint32_t *transaction_id
);
747 * Create a new transaction to commit a candidate configuration. This is a
748 * convenience function that performs the two-phase commit protocol
749 * transparently to the user. The cost is reduced flexibility, since
750 * network-wide and multi-daemon transactions require the network manager to
751 * take into account the results of the preparation phase of multiple managed
755 * Candidate configuration to commit. It's preserved regardless if the commit
756 * operation fails or not.
759 * Northbound client performing the commit.
762 * Northbound user performing the commit (can be NULL).
765 * Specify whether the transaction should be recorded in the transactions log
769 * Optional comment describing the commit.
772 * Optional output parameter providing the ID of the committed transaction.
775 * - NB_OK on success.
776 * - NB_ERR_NO_CHANGES when the candidate is identical to the running
778 * - NB_ERR_LOCKED when there's already another transaction in progress.
779 * - NB_ERR_VALIDATION when the candidate fails the validation checks.
780 * - NB_ERR_RESOURCE when the system fails to allocate resources to apply
781 * the candidate configuration.
782 * - NB_ERR for other errors.
784 extern int nb_candidate_commit(struct nb_config
*candidate
,
785 enum nb_client client
, const void *user
,
786 bool save_transaction
, const char *comment
,
787 uint32_t *transaction_id
);
790 * Lock the running configuration.
796 * Northbound user (can be NULL).
799 * 0 on success, -1 when the running configuration is already locked.
801 extern int nb_running_lock(enum nb_client client
, const void *user
);
804 * Unlock the running configuration.
810 * Northbound user (can be NULL).
813 * 0 on success, -1 when the running configuration is already unlocked or
814 * locked by another client/user.
816 extern int nb_running_unlock(enum nb_client client
, const void *user
);
819 * Check if the running configuration is locked or not for the given
826 * Northbound user (can be NULL).
829 * 0 if the running configuration is unlocked or if the client/user owns the
830 * lock, -1 otherwise.
832 extern int nb_running_lock_check(enum nb_client client
, const void *user
);
835 * Iterate over operational data.
838 * Data path of the YANG data we want to iterate over.
841 * YANG module translator (might be NULL).
844 * NB_OPER_DATA_ITER_ flags to control how the iteration is performed.
847 * Function to call with each data node.
850 * Arbitrary argument passed as the fourth parameter in each call to 'cb'.
853 * NB_OK on success, NB_ERR otherwise.
855 extern int nb_oper_data_iterate(const char *xpath
,
856 struct yang_translator
*translator
,
857 uint32_t flags
, nb_oper_data_cb cb
, void *arg
);
860 * Validate if the northbound operation is valid for the given node.
863 * Operation we want to check.
866 * libyang schema node we want to check.
869 * true if the operation is valid, false otherwise.
871 extern bool nb_operation_is_valid(enum nb_operation operation
,
872 const struct lys_node
*snode
);
875 * Send a YANG notification. This is a no-op unless the 'nb_notification_send'
876 * hook was registered by a northbound plugin.
879 * XPath of the YANG notification.
882 * Linked list containing the arguments that should be sent. This list is
883 * deleted after being used.
886 * NB_OK on success, NB_ERR otherwise.
888 extern int nb_notification_send(const char *xpath
, struct list
*arguments
);
891 * Associate a user pointer to a configuration node.
893 * This should be called by northbound 'create' callbacks in the NB_EV_APPLY
897 * libyang data node - only its XPath is used.
900 * Arbitrary user-specified pointer.
902 extern void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
);
905 * Unset the user pointer associated to a configuration node.
907 * This should be called by northbound 'destroy' callbacks in the NB_EV_APPLY
911 * libyang data node - only its XPath is used.
914 * The user pointer that was unset.
916 extern void *nb_running_unset_entry(const struct lyd_node
*dnode
);
919 * Find the user pointer (if any) associated to a configuration node.
921 * The XPath associated to the configuration node can be provided directly or
922 * indirectly through a libyang data node.
924 * If an user point is not found, this function follows the parent nodes in the
925 * running configuration until an user pointer is found or until the root node
929 * libyang data node - only its XPath is used (can be NULL if 'xpath' is
933 * XPath of the configuration node (can be NULL if 'dnode' is provided).
936 * When set to true, abort the program if no user pointer is found.
938 * As a rule of thumb, this parameter should be set to true in the following
940 * - Calling this function from any northbound configuration callback during
941 * the NB_EV_APPLY phase.
942 * - Calling this function from a 'delete' northbound configuration callback
945 * In both the above cases, the given configuration node should contain an
946 * user pointer except when there's a bug in the code, in which case it's
947 * better to abort the program right away and eliminate the need for
948 * unnecessary NULL checks.
950 * In all other cases, this parameter should be set to false and the caller
951 * should check if the function returned NULL or not.
954 * User pointer if found, NULL otherwise.
956 extern void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
957 bool abort_if_not_found
);
960 * Return a human-readable string representing a northbound event.
966 * String representation of the given northbound event.
968 extern const char *nb_event_name(enum nb_event event
);
971 * Return a human-readable string representing a northbound operation.
974 * Northbound operation.
977 * String representation of the given northbound operation.
979 extern const char *nb_operation_name(enum nb_operation operation
);
982 * Return a human-readable string representing a northbound error.
988 * String representation of the given northbound error.
990 extern const char *nb_err_name(enum nb_error error
);
993 * Return a human-readable string representing a northbound client.
999 * String representation of the given northbound client.
1001 extern const char *nb_client_name(enum nb_client client
);
1004 * Initialize the northbound layer. Should be called only once during the
1005 * daemon initialization process.
1008 * Array of YANG modules to parse and initialize.
1011 * Size of the modules array.
1013 extern void nb_init(struct thread_master
*tm
,
1014 const struct frr_yang_module_info
*const modules
[],
1018 * Finish the northbound layer gracefully. Should be called only when the daemon
1021 extern void nb_terminate(void);
1027 #endif /* _FRR_NORTHBOUND_H_ */