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.
90 * Configuration callback.
92 * A presence container, list entry, leaf-list entry or leaf of type
93 * empty has been created.
95 * For presence-containers and list entries, the callback is supposed to
96 * initialize the default values of its children (if any) from the YANG
100 * The transaction phase. Refer to the documentation comments of
101 * nb_event for more details.
104 * libyang data node that is being created.
107 * Pointer to store resource(s) allocated during the NB_EV_PREPARE
108 * phase. The same pointer can be used during the NB_EV_ABORT and
109 * NB_EV_APPLY phases to either release or make use of the allocated
110 * resource(s). It's set to NULL when the event is NB_EV_VALIDATE.
113 * - NB_OK on success.
114 * - NB_ERR_VALIDATION when a validation error occurred.
115 * - NB_ERR_RESOURCE when the callback failed to allocate a resource.
116 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
117 * - NB_ERR for other errors.
119 int (*create
)(enum nb_event event
, const struct lyd_node
*dnode
,
120 union nb_resource
*resource
);
123 * Configuration callback.
125 * The value of a leaf has been modified.
127 * List keys don't need to implement this callback. When a list key is
128 * modified, the northbound treats this as if the list was deleted and a
129 * new one created with the updated key value.
132 * The transaction phase. Refer to the documentation comments of
133 * nb_event for more details.
136 * libyang data node that is being modified
139 * Pointer to store resource(s) allocated during the NB_EV_PREPARE
140 * phase. The same pointer can be used during the NB_EV_ABORT and
141 * NB_EV_APPLY phases to either release or make use of the allocated
142 * resource(s). It's set to NULL when the event is NB_EV_VALIDATE.
145 * - NB_OK on success.
146 * - NB_ERR_VALIDATION when a validation error occurred.
147 * - NB_ERR_RESOURCE when the callback failed to allocate a resource.
148 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
149 * - NB_ERR for other errors.
151 int (*modify
)(enum nb_event event
, const struct lyd_node
*dnode
,
152 union nb_resource
*resource
);
155 * Configuration callback.
157 * A presence container, list entry, leaf-list entry or optional leaf
160 * The callback is supposed to delete the entire configuration object,
161 * including its children when they exist.
164 * The transaction phase. Refer to the documentation comments of
165 * nb_event for more details.
168 * libyang data node that is being deleted.
171 * - NB_OK on success.
172 * - NB_ERR_VALIDATION when a validation error occurred.
173 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
174 * - NB_ERR for other errors.
176 int (*destroy
)(enum nb_event event
, const struct lyd_node
*dnode
);
179 * Configuration callback.
181 * A list entry or leaf-list entry has been moved. Only applicable when
182 * the "ordered-by user" statement is present.
185 * The transaction phase. Refer to the documentation comments of
186 * nb_event for more details.
189 * libyang data node that is being moved.
192 * - NB_OK on success.
193 * - NB_ERR_VALIDATION when a validation error occurred.
194 * - NB_ERR_INCONSISTENCY when an inconsistency was detected.
195 * - NB_ERR for other errors.
197 int (*move
)(enum nb_event event
, const struct lyd_node
*dnode
);
200 * Optional configuration callback.
202 * The 'apply_finish' callbacks are called after all other callbacks
203 * during the apply phase (NB_EV_APPLY). These callbacks are called only
204 * under one of the following two cases:
205 * - The data node has been created or modified (but not deleted);
206 * - Any change was made within the descendants of the data node (e.g. a
207 * child leaf was modified, created or deleted).
209 * In the second case above, the 'apply_finish' callback is called only
210 * once even if multiple changes occurred within the descendants of the
214 * libyang data node associated with the 'apply_finish' callback.
216 void (*apply_finish
)(const struct lyd_node
*dnode
);
219 * Operational data callback.
221 * The callback function should return the value of a specific leaf,
222 * leaf-list entry or inform if a typeless value (presence containers or
223 * leafs of type empty) exists or not.
226 * YANG data path of the data we want to get.
229 * Pointer to list entry (might be NULL).
232 * Pointer to newly created yang_data structure, or NULL to indicate
233 * the absence of data.
235 struct yang_data
*(*get_elem
)(const char *xpath
,
236 const void *list_entry
);
239 * Operational data callback for YANG lists and leaf-lists.
241 * The callback function should return the next entry in the list or
242 * leaf-list. The 'list_entry' parameter will be NULL on the first
246 * Pointer to parent list entry.
249 * Pointer to (leaf-)list entry.
252 * Pointer to the next entry in the (leaf-)list, or NULL to signal
253 * that the end of the (leaf-)list was reached.
255 const void *(*get_next
)(const void *parent_list_entry
,
256 const void *list_entry
);
259 * Operational data callback for YANG lists.
261 * The callback function should fill the 'keys' parameter based on the
262 * given list_entry. Keyless lists don't need to implement this
266 * Pointer to list entry.
269 * Structure to be filled based on the attributes of the provided
273 * NB_OK on success, NB_ERR otherwise.
275 int (*get_keys
)(const void *list_entry
, struct yang_list_keys
*keys
);
278 * Operational data callback for YANG lists.
280 * The callback function should return a list entry based on the list
281 * keys given as a parameter. Keyless lists don't need to implement this
285 * Pointer to parent list entry.
288 * Structure containing the keys of the list entry.
291 * Pointer to the list entry if found, or NULL if not found.
293 const void *(*lookup_entry
)(const void *parent_list_entry
,
294 const struct yang_list_keys
*keys
);
297 * RPC and action callback.
299 * Both 'input' and 'output' are lists of 'yang_data' structures. The
300 * callback should fetch all the input parameters from the 'input' list,
301 * and add output parameters to the 'output' list if necessary.
304 * XPath of the YANG RPC or action.
307 * Read-only list of input parameters.
310 * List of output parameters to be populated by the callback.
313 * NB_OK on success, NB_ERR otherwise.
315 int (*rpc
)(const char *xpath
, const struct list
*input
,
316 struct list
*output
);
319 * Optional callback to show the CLI command associated to the given
323 * The vty terminal to dump the configuration to.
326 * libyang data node that should be shown in the form of a CLI
330 * Specify whether to display default configuration values or not.
331 * This parameter can be ignored most of the time since the
332 * northbound doesn't call this callback for default leaves or
333 * non-presence containers that contain only default child nodes.
334 * The exception are commands associated to multiple configuration
335 * nodes, in which case it might be desirable to hide one or more
336 * parts of the command when this parameter is set to false.
338 void (*cli_show
)(struct vty
*vty
, struct lyd_node
*dnode
,
343 * Northbound-specific data that is allocated for each schema node of the native
347 /* Back pointer to the libyang schema node. */
348 const struct lys_node
*snode
;
350 /* Data path of this YANG node. */
351 char xpath
[XPATH_MAXLEN
];
353 /* Priority - lower priorities are processed first. */
356 /* Callbacks implemented for this node. */
357 struct nb_callbacks cbs
;
360 * Pointer to the parent node (disconsidering non-presence containers).
362 struct nb_node
*parent
;
364 /* Pointer to the nearest parent list, if any. */
365 struct nb_node
*parent_list
;
371 /* ConfD hash value corresponding to this YANG path. */
375 /* The YANG container or list contains only config data. */
376 #define F_NB_NODE_CONFIG_ONLY 0x01
377 /* The YANG list doesn't contain key leafs. */
378 #define F_NB_NODE_KEYLESS_LIST 0x02
380 struct frr_yang_module_info
{
381 /* YANG module name. */
384 /* Northbound callbacks. */
386 /* Data path of this YANG node. */
389 /* Callbacks implemented for this node. */
390 struct nb_callbacks cbs
;
392 /* Priority - lower priorities are processed first. */
397 /* Northbound error codes. */
406 NB_ERR_INCONSISTENCY
,
409 /* Default priority. */
410 #define NB_DFLT_PRIORITY (UINT32_MAX / 2)
412 /* Default maximum of configuration rollbacks to store. */
413 #define NB_DLFT_MAX_CONFIG_ROLLBACKS 20
415 /* Northbound clients. */
422 /* Northbound configuration. */
424 struct lyd_node
*dnode
;
428 /* Northbound configuration callback. */
429 struct nb_config_cb
{
430 RB_ENTRY(nb_config_cb
) entry
;
431 enum nb_operation operation
;
432 char xpath
[XPATH_MAXLEN
];
433 const struct nb_node
*nb_node
;
434 const struct lyd_node
*dnode
;
436 RB_HEAD(nb_config_cbs
, nb_config_cb
);
437 RB_PROTOTYPE(nb_config_cbs
, nb_config_cb
, entry
, nb_config_cb_compare
);
439 /* Northbound configuration change. */
440 struct nb_config_change
{
441 struct nb_config_cb cb
;
442 union nb_resource resource
;
446 /* Northbound configuration transaction. */
447 struct nb_transaction
{
448 enum nb_client client
;
450 struct nb_config
*config
;
451 struct nb_config_cbs changes
;
454 /* Callback function used by nb_oper_data_iterate(). */
455 typedef int (*nb_oper_data_cb
)(const struct lys_node
*snode
,
456 struct yang_translator
*translator
,
457 struct yang_data
*data
, void *arg
);
459 /* Iterate over direct child nodes only. */
460 #define NB_OPER_DATA_ITER_NORECURSE 0x0001
463 DECLARE_HOOK(nb_notification_send
, (const char *xpath
, struct list
*arguments
),
465 DECLARE_HOOK(nb_client_debug_config_write
, (struct vty
*vty
), (vty
))
466 DECLARE_HOOK(nb_client_debug_set_all
, (uint32_t flags
, bool set
), (flags
, set
))
468 /* Northbound debugging records */
469 extern struct debug nb_dbg_cbs_config
;
470 extern struct debug nb_dbg_cbs_state
;
471 extern struct debug nb_dbg_cbs_rpc
;
472 extern struct debug nb_dbg_notif
;
473 extern struct debug nb_dbg_events
;
475 /* Global running configuration. */
476 extern struct nb_config
*running_config
;
478 /* Wrappers for the northbound callbacks. */
479 extern struct yang_data
*nb_callback_get_elem(const struct nb_node
*nb_node
,
481 const void *list_entry
);
482 extern const void *nb_callback_get_next(const struct nb_node
*nb_node
,
483 const void *parent_list_entry
,
484 const void *list_entry
);
485 extern int nb_callback_get_keys(const struct nb_node
*nb_node
,
486 const void *list_entry
,
487 struct yang_list_keys
*keys
);
488 extern const void *nb_callback_lookup_entry(const struct nb_node
*nb_node
,
489 const void *parent_list_entry
,
490 const struct yang_list_keys
*keys
);
491 extern int nb_callback_rpc(const struct nb_node
*nb_node
, const char *xpath
,
492 const struct list
*input
, struct list
*output
);
495 * Create a northbound node for all YANG schema nodes.
497 void nb_nodes_create(void);
500 * Delete all northbound nodes from all YANG schema nodes.
502 void nb_nodes_delete(void);
505 * Find the northbound node corresponding to a YANG data path.
508 * XPath to search for (with or without predicates).
511 * Pointer to northbound node if found, NULL otherwise.
513 extern struct nb_node
*nb_node_find(const char *xpath
);
516 * Create a new northbound configuration.
519 * Pointer to a libyang data node containing the configuration data. If NULL
520 * is given, an empty configuration will be created.
523 * Pointer to newly created northbound configuration.
525 extern struct nb_config
*nb_config_new(struct lyd_node
*dnode
);
528 * Delete a northbound configuration.
531 * Pointer to the config that is going to be deleted.
533 extern void nb_config_free(struct nb_config
*config
);
536 * Duplicate a northbound configuration.
539 * Northbound configuration to duplicate.
542 * Pointer to duplicated configuration.
544 extern struct nb_config
*nb_config_dup(const struct nb_config
*config
);
547 * Merge one configuration into another.
550 * Configuration to merge to.
553 * Configuration to merge config_dst with.
556 * Specify whether config_src should be deleted or not after the merge
560 * NB_OK on success, NB_ERR otherwise.
562 extern int nb_config_merge(struct nb_config
*config_dst
,
563 struct nb_config
*config_src
, bool preserve_source
);
566 * Replace one configuration by another.
569 * Configuration to be replaced.
572 * Configuration to replace config_dst.
575 * Specify whether config_src should be deleted or not after the replace
578 extern void nb_config_replace(struct nb_config
*config_dst
,
579 struct nb_config
*config_src
,
580 bool preserve_source
);
583 * Edit a candidate configuration.
586 * Candidate configuration to edit.
589 * Northbound node associated to the configuration being edited.
592 * Operation to apply.
595 * XPath of the configuration node being edited.
598 * Previous value of the configuration node. Should be used only when the
599 * operation is NB_OP_MOVE, otherwise this parameter is ignored.
602 * New value of the configuration node.
605 * - NB_OK on success.
606 * - NB_ERR_NOT_FOUND when the element to be deleted was not found.
607 * - NB_ERR for other errors.
609 extern int nb_candidate_edit(struct nb_config
*candidate
,
610 const struct nb_node
*nb_node
,
611 enum nb_operation operation
, const char *xpath
,
612 const struct yang_data
*previous
,
613 const struct yang_data
*data
);
616 * Check if a candidate configuration is outdated and needs to be updated.
619 * Candidate configuration to check.
622 * true if the candidate is outdated, false otherwise.
624 extern bool nb_candidate_needs_update(const struct nb_config
*candidate
);
627 * Update a candidate configuration by rebasing the changes on top of the latest
628 * running configuration. Resolve conflicts automatically by giving preference
629 * to the changes done in the candidate configuration.
632 * Candidate configuration to update.
635 * NB_OK on success, NB_ERR otherwise.
637 extern int nb_candidate_update(struct nb_config
*candidate
);
640 * Validate a candidate configuration. Perform both YANG syntactic/semantic
641 * validation and code-level validation using the northbound callbacks.
643 * WARNING: the candidate can be modified as part of the validation process
644 * (e.g. add default nodes).
647 * Candidate configuration to validate.
650 * NB_OK on success, NB_ERR_VALIDATION otherwise.
652 extern int nb_candidate_validate(struct nb_config
*candidate
);
655 * Create a new configuration transaction but do not commit it yet. Only
656 * validate the candidate and prepare all resources required to apply the
657 * configuration changes.
660 * Candidate configuration to commit.
663 * Northbound client performing the commit.
666 * Optional comment describing the commit.
669 * Output parameter providing the created transaction when one is created
670 * successfully. In this case, it must be either aborted using
671 * nb_candidate_commit_abort() or committed using
672 * nb_candidate_commit_apply().
675 * - NB_OK on success.
676 * - NB_ERR_NO_CHANGES when the candidate is identical to the running
678 * - NB_ERR_LOCKED when there's already another transaction in progress.
679 * - NB_ERR_VALIDATION when the candidate fails the validation checks.
680 * - NB_ERR_RESOURCE when the system fails to allocate resources to apply
681 * the candidate configuration.
682 * - NB_ERR for other errors.
684 extern int nb_candidate_commit_prepare(struct nb_config
*candidate
,
685 enum nb_client client
,
687 struct nb_transaction
**transaction
);
690 * Abort a previously created configuration transaction, releasing all resources
691 * allocated during the preparation phase.
694 * Candidate configuration to abort. It's consumed by this function.
696 extern void nb_candidate_commit_abort(struct nb_transaction
*transaction
);
699 * Commit a previously created configuration transaction.
702 * Configuration transaction to commit. It's consumed by this function.
705 * Specify whether the transaction should be recorded in the transactions log
709 * Optional output parameter providing the ID of the committed transaction.
711 extern void nb_candidate_commit_apply(struct nb_transaction
*transaction
,
712 bool save_transaction
,
713 uint32_t *transaction_id
);
716 * Create a new transaction to commit a candidate configuration. This is a
717 * convenience function that performs the two-phase commit protocol
718 * transparently to the user. The cost is reduced flexibility, since
719 * network-wide and multi-daemon transactions require the network manager to
720 * take into account the results of the preparation phase of multiple managed
724 * Candidate configuration to commit. It's preserved regardless if the commit
725 * operation fails or not.
728 * Northbound client performing the commit.
731 * Specify whether the transaction should be recorded in the transactions log
735 * Optional comment describing the commit.
738 * Optional output parameter providing the ID of the committed transaction.
741 * - NB_OK on success.
742 * - NB_ERR_NO_CHANGES when the candidate is identical to the running
744 * - NB_ERR_LOCKED when there's already another transaction in progress.
745 * - NB_ERR_VALIDATION when the candidate fails the validation checks.
746 * - NB_ERR_RESOURCE when the system fails to allocate resources to apply
747 * the candidate configuration.
748 * - NB_ERR for other errors.
750 extern int nb_candidate_commit(struct nb_config
*candidate
,
751 enum nb_client client
, bool save_transaction
,
752 const char *comment
, uint32_t *transaction_id
);
755 * Iterate over operetional data.
758 * Data path of the YANG data we want to iterate over.
761 * YANG module translator (might be NULL).
764 * NB_OPER_DATA_ITER_ flags to control how the iteration is performed.
767 * Function to call with each data node.
770 * Arbitrary argument passed as the fourth parameter in each call to 'cb'.
773 * NB_OK on success, NB_ERR otherwise.
775 extern int nb_oper_data_iterate(const char *xpath
,
776 struct yang_translator
*translator
,
777 uint32_t flags
, nb_oper_data_cb cb
, void *arg
);
780 * Validate if the northbound operation is valid for the given node.
783 * Operation we want to check.
786 * libyang schema node we want to check.
789 * true if the operation is valid, false otherwise.
791 extern bool nb_operation_is_valid(enum nb_operation operation
,
792 const struct lys_node
*snode
);
795 * Send a YANG notification. This is a no-op unless the 'nb_notification_send'
796 * hook was registered by a northbound plugin.
799 * XPath of the YANG notification.
802 * Linked list containing the arguments that should be sent. This list is
803 * deleted after being used.
806 * NB_OK on success, NB_ERR otherwise.
808 extern int nb_notification_send(const char *xpath
, struct list
*arguments
);
811 * Associate a user pointer to a configuration node.
813 * This should be called by northbound 'create' callbacks in the NB_EV_APPLY
817 * libyang data node - only its XPath is used.
820 * Arbitrary user-specified pointer.
822 extern void nb_running_set_entry(const struct lyd_node
*dnode
, void *entry
);
825 * Unset the user pointer associated to a configuration node.
827 * This should be called by northbound 'destroy' callbacks in the NB_EV_APPLY
831 * libyang data node - only its XPath is used.
834 * The user pointer that was unset.
836 extern void *nb_running_unset_entry(const struct lyd_node
*dnode
);
839 * Find the user pointer (if any) associated to a configuration node.
841 * The XPath associated to the configuration node can be provided directly or
842 * indirectly through a libyang data node.
844 * If an user point is not found, this function follows the parent nodes in the
845 * running configuration until an user pointer is found or until the root node
849 * libyang data node - only its XPath is used (can be NULL if 'xpath' is
853 * XPath of the configuration node (can be NULL if 'dnode' is provided).
856 * When set to true, abort the program if no user pointer is found.
858 * As a rule of thumb, this parameter should be set to true in the following
860 * - Calling this function from any northbound configuration callback during
861 * the NB_EV_APPLY phase.
862 * - Calling this function from a 'delete' northbound configuration callback
865 * In both the above cases, the given configuration node should contain an
866 * user pointer except when there's a bug in the code, in which case it's
867 * better to abort the program right away and eliminate the need for
868 * unnecessary NULL checks.
870 * In all other cases, this parameter should be set to false and the caller
871 * should check if the function returned NULL or not.
874 * User pointer if found, NULL otherwise.
876 extern void *nb_running_get_entry(const struct lyd_node
*dnode
, const char *xpath
,
877 bool abort_if_not_found
);
880 * Return a human-readable string representing a northbound event.
886 * String representation of the given northbound event.
888 extern const char *nb_event_name(enum nb_event event
);
891 * Return a human-readable string representing a northbound operation.
894 * Northbound operation.
897 * String representation of the given northbound operation.
899 extern const char *nb_operation_name(enum nb_operation operation
);
902 * Return a human-readable string representing a northbound error.
908 * String representation of the given northbound error.
910 extern const char *nb_err_name(enum nb_error error
);
913 * Return a human-readable string representing a northbound client.
919 * String representation of the given northbound client.
921 extern const char *nb_client_name(enum nb_client client
);
924 * Initialize the northbound layer. Should be called only once during the
925 * daemon initialization process.
928 * Array of YANG modules to parse and initialize.
931 * Size of the modules array.
933 extern void nb_init(struct thread_master
*tm
, const struct frr_yang_module_info
*modules
[],
937 * Finish the northbound layer gracefully. Should be called only when the daemon
940 extern void nb_terminate(void);
946 #endif /* _FRR_NORTHBOUND_H_ */