1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
12 #include <libyang/libyang.h>
17 #include "yang_wrappers.h"
23 /* Maximum XPath length. */
24 #define XPATH_MAXLEN 1024
26 /* Maximum list key length. */
27 #define LIST_MAXKEYS 8
29 /* Maximum list key length. */
30 #define LIST_MAXKEYLEN 128
32 /* Maximum string length of an YANG value. */
33 #define YANG_VALUE_MAXLEN 1024
35 struct yang_module_embed
{
36 struct yang_module_embed
*next
;
37 const char *mod_name
, *mod_rev
;
38 const char *sub_mod_name
;
39 const char *sub_mod_rev
;
45 RB_ENTRY(yang_module
) entry
;
47 const struct lys_module
*info
;
52 sr_subscription_ctx_t
*sr_subscription
;
53 struct thread
*sr_thread
;
56 RB_HEAD(yang_modules
, yang_module
);
57 RB_PROTOTYPE(yang_modules
, yang_module
, entry
, yang_module_compare
);
60 /* XPath identifier of the data element. */
61 char xpath
[XPATH_MAXLEN
];
63 /* Value encoded as a raw string. */
67 struct yang_list_keys
{
68 /* Number os keys (max: LIST_MAXKEYS). */
71 /* Value encoded as a raw string. */
72 char key
[LIST_MAXKEYS
][LIST_MAXKEYLEN
];
80 enum yang_iter_flags
{
81 /* Filter non-presence containers. */
82 YANG_ITER_FILTER_NPCONTAINERS
= (1<<0),
84 /* Filter list keys (leafs). */
85 YANG_ITER_FILTER_LIST_KEYS
= (1<<1),
87 /* Filter RPC input/output nodes. */
88 YANG_ITER_FILTER_INPUT_OUTPUT
= (1<<2),
91 /* Callback used by the yang_snodes_iterate_*() family of functions. */
92 typedef int (*yang_iterate_cb
)(const struct lysc_node
*snode
, void *arg
);
94 /* Callback used by the yang_dnode_iterate() function. */
95 typedef int (*yang_dnode_iter_cb
)(const struct lyd_node
*dnode
, void *arg
);
97 /* Return values of the 'yang_iterate_cb' callback. */
98 #define YANG_ITER_CONTINUE 0
99 #define YANG_ITER_STOP -1
101 /* Global libyang context for native FRR models. */
102 extern struct ly_ctx
*ly_native_ctx
;
104 /* Tree of all loaded YANG modules. */
105 extern struct yang_modules yang_modules
;
108 * Create a new YANG module and load it using libyang. If the YANG module is not
109 * found in the YANG_MODELS_PATH directory, the program will exit with an error.
110 * Once loaded, a YANG module can't be unloaded anymore.
113 * Name of the YANG module.
116 * Pointer to newly created YANG module.
118 extern struct yang_module
*yang_module_load(const char *module_name
);
121 * Load all FRR native YANG models.
123 extern void yang_module_load_all(void);
126 * Find a YANG module by its name.
129 * Name of the YANG module.
132 * Pointer to YANG module if found, NULL otherwise.
134 extern struct yang_module
*yang_module_find(const char *module_name
);
137 * Register a YANG module embedded in the binary file. Should be called
138 * from a constructor function.
141 * YANG module embedding structure to register. (static global provided
144 extern void yang_module_embed(struct yang_module_embed
*embed
);
147 * Iterate recursively over all children of a schema node.
150 * YANG schema node to operate on.
153 * When set, iterate over all nodes of the specified module only.
156 * Function to call with each schema node.
159 * YANG_ITER_* flags to control how the iteration is performed.
162 * Arbitrary argument passed as the second parameter in each call to 'cb'.
165 * The return value of the last called callback.
167 extern int yang_snodes_iterate_subtree(const struct lysc_node
*snode
,
168 const struct lys_module
*module
,
169 yang_iterate_cb cb
, uint16_t flags
,
173 * Iterate over all libyang schema nodes from all loaded modules of the
177 * When set, iterate over all nodes of the specified module only.
180 * Function to call with each schema node.
183 * YANG_ITER_* flags to control how the iteration is performed.
186 * Arbitrary argument passed as the second parameter in each call to 'cb'.
189 * The return value of the last called callback.
191 extern int yang_snodes_iterate(const struct lys_module
*module
,
192 yang_iterate_cb cb
, uint16_t flags
, void *arg
);
195 * Build schema path or data path of the schema node.
198 * libyang schema node to be processed.
201 * Specify whether a schema path or a data path should be built.
204 * Pointer to previously allocated buffer.
207 * Size of the xpath buffer.
209 extern void yang_snode_get_path(const struct lysc_node
*snode
,
210 enum yang_path_type type
, char *xpath
,
214 * Find first parent schema node which is a presence-container or a list
215 * (non-presence containers are ignored).
218 * libyang schema node to operate on.
221 * The parent libyang schema node if found, or NULL if not found.
223 extern struct lysc_node
*yang_snode_real_parent(const struct lysc_node
*snode
);
226 * Find first parent schema node which is a list.
229 * libyang schema node to operate on.
232 * The parent libyang schema node (list) if found, or NULL if not found.
234 extern struct lysc_node
*yang_snode_parent_list(const struct lysc_node
*snode
);
237 * Check if the libyang schema node represents typeless data (e.g. containers,
238 * leafs of type empty, etc).
241 * libyang schema node to operate on.
244 * true if the schema node represents typeless data, false otherwise.
246 extern bool yang_snode_is_typeless_data(const struct lysc_node
*snode
);
249 * Get the default value associated to a YANG leaf or leaf-list.
252 * libyang schema node to operate on.
255 * The default value if it exists, NULL otherwise.
257 extern const char *yang_snode_get_default(const struct lysc_node
*snode
);
260 * Get the type structure of a leaf of leaf-list. If the type is a leafref, the
261 * final (if there is a chain of leafrefs) target's type is found.
264 * libyang schema node to operate on.
267 * The found type if the schema node represents a leaf or a leaf-list, NULL
270 extern const struct lysc_type
*
271 yang_snode_get_type(const struct lysc_node
*snode
);
274 * Get the number of key nodes for the given list.
277 * libyang (LYS_LIST) schema node to operate on.
280 * The number of key LYS_LEAFs as children of this list node.
282 extern unsigned int yang_snode_num_keys(const struct lysc_node
*snode
);
284 #define LY_FOR_KEYS(snode, skey) \
285 for ((skey) = (const struct lysc_node_leaf *)lysc_node_child((snode)); \
286 (skey); (skey) = (const struct lysc_node_leaf *)((skey)->next)) \
287 if (!lysc_is_key(skey)) { \
293 * Build data path of the data node.
296 * libyang data node to be processed.
299 * Pointer to previously allocated buffer.
302 * Size of the xpath buffer.
304 extern void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
308 * Return the schema name of the given libyang data node.
314 * Optional XPath expression (absolute or relative) to specify a different
315 * data node to operate on in the same data tree.
318 * Schema name of the libyang data node.
320 extern const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
321 const char *xpath_fmt
, ...)
325 * Find a libyang data node by its YANG data path.
328 * Base libyang data node to operate on.
331 * Limited XPath (absolute or relative) string. See Path in libyang
332 * documentation for restrictions.
335 * The libyang data node if found, or NULL if not found.
337 extern struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
341 * Find a libyang data node by its YANG data path.
344 * Base libyang data node to operate on.
347 * Limited XPath (absolute or relative) format string. See Path in libyang
348 * documentation for restrictions.
351 * any parameters for xpath_fmt.
354 * The libyang data node if found, or NULL if not found.
356 extern struct lyd_node
*yang_dnode_getf(const struct lyd_node
*dnode
,
357 const char *path_fmt
, ...)
361 * Check if a libyang data node exists.
364 * Base libyang data node to operate on.
367 * Limited XPath (absolute or relative) string. See Path in libyang
368 * documentation for restrictions.
371 * true if a libyang data node was found, false otherwise.
373 extern bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath
);
376 * Check if a libyang data node exists.
379 * Base libyang data node to operate on.
382 * Limited XPath (absolute or relative) format string. See Path in
383 * libyang documentation for restrictions.
386 * any parameters for xpath_fmt.
389 * true if a libyang data node was found, false otherwise.
391 extern bool yang_dnode_existsf(const struct lyd_node
*dnode
,
392 const char *xpath_fmt
, ...) PRINTFRR(2, 3);
395 * Iterate over all libyang data nodes that satisfy an XPath query.
398 * Function to call with each data node.
401 * Arbitrary argument passed as the second parameter in each call to 'cb'.
404 * Base libyang data node to operate on.
407 * XPath expression (absolute or relative).
410 * any parameters for xpath_fmt.
412 void yang_dnode_iterate(yang_dnode_iter_cb cb
, void *arg
,
413 const struct lyd_node
*dnode
, const char *xpath_fmt
,
417 * Check if the libyang data node contains a default value. Non-presence
418 * containers are assumed to always contain a default value.
421 * Base libyang data node to operate on.
424 * Optional XPath expression (absolute or relative) to specify a different
425 * data node to operate on in the same data tree.
428 * true if the data node contains the default value, false otherwise.
430 extern bool yang_dnode_is_default(const struct lyd_node
*dnode
,
434 * Check if the libyang data node contains a default value. Non-presence
435 * containers are assumed to always contain a default value.
438 * Base libyang data node to operate on.
441 * Optional limited XPath (absolute or relative) format string. See Path in
442 * libyang documentation for restrictions.
445 * any parameters for xpath_fmt.
448 * true if the data node contains the default value, false otherwise.
450 extern bool yang_dnode_is_defaultf(const struct lyd_node
*dnode
,
451 const char *xpath_fmt
, ...) PRINTFRR(2, 3);
454 * Check if the libyang data node and all of its children contain default
455 * values. Non-presence containers are assumed to always contain a default
459 * libyang data node to operate on.
462 * true if the data node and all of its children contain default values,
465 extern bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
);
468 * Change the value of a libyang leaf node.
471 * libyang data node to operate on.
474 * String representing the new value.
476 extern void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
);
479 * Create a new libyang data node.
482 * libyang context to operate on.
485 * Specify whether the data node will contain only configuration data (true)
486 * or both configuration data and state data (false).
489 * Pointer to newly created libyang data node.
491 extern struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
);
494 * Duplicate a libyang data node.
497 * libyang data node to duplicate.
500 * Pointer to duplicated libyang data node.
502 extern struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
);
505 * Delete a libyang data node.
508 * Pointer to the libyang data node that is going to be deleted along with
509 * the entire tree it belongs to.
511 extern void yang_dnode_free(struct lyd_node
*dnode
);
514 * Create a new yang_data structure.
517 * Data path of the YANG data.
520 * String representing the value of the YANG data.
523 * Pointer to newly created yang_data structure.
525 extern struct yang_data
*yang_data_new(const char *xpath
, const char *value
);
528 * Delete a yang_data structure.
531 * yang_data to delete.
533 extern void yang_data_free(struct yang_data
*data
);
536 * Create a new linked list of yang_data structures. The list 'del' callback is
537 * initialized appropriately so that the entire list can be deleted safely with
538 * list_delete_and_null().
541 * Pointer to newly created linked list.
543 extern struct list
*yang_data_list_new(void);
546 * Find the yang_data structure corresponding to an XPath in a list.
549 * list of yang_data structures to operate on.
552 * XPath to search for (format string).
555 * Pointer to yang_data if found, NULL otherwise.
557 extern struct yang_data
*yang_data_list_find(const struct list
*list
,
558 const char *xpath_fmt
, ...)
562 * Create and set up a libyang context (for use by the translator)
565 * Specify whether libyang should attempt to look for embedded YANG modules.
568 * True if the caller will later call ly_ctx_compile to compile all loaded
571 extern struct ly_ctx
*yang_ctx_new_setup(bool embedded_modules
,
572 bool explicit_compile
);
575 * Enable or disable libyang verbose debugging.
578 * When set to true, enable libyang verbose debugging, otherwise disable it.
580 extern void yang_debugging_set(bool enable
);
583 * Print libyang error messages into the provided buffer.
586 * libyang context to operate on.
589 * Buffer to store the libyang error messages.
595 * The provided buffer.
597 extern const char *yang_print_errors(struct ly_ctx
*ly_ctx
, char *buf
,
601 * Initialize the YANG subsystem. Should be called only once during the
602 * daemon initialization process.
605 * Specify whether libyang should attempt to look for embedded YANG modules.
607 * Hold off on compiling modules until yang_init_loading_complete is called.
609 extern void yang_init(bool embedded_modules
, bool defer_compile
);
612 * Should be called after yang_init and all yang_module_load()s have been done,
613 * compiles all modules loaded into the yang context.
615 extern void yang_init_loading_complete(void);
618 * Finish the YANG subsystem gracefully. Should be called only when the daemon
621 extern void yang_terminate(void);
624 * API to return the parent dnode having a given schema-node name
625 * Use case: One has to access the parent dnode's private pointer
626 * for a given child node.
627 * For that there is a need to find parent dnode first.
629 * dnode The starting node to work on
631 * name The name of container/list schema-node
633 * Returns The dnode matched with the given name
635 extern const struct lyd_node
*
636 yang_dnode_get_parent(const struct lyd_node
*dnode
, const char *name
);
640 * In some cases there is a need to auto delete the parent nodes
641 * if the given node is last in the list.
642 * It tries to delete all the parents in a given tree in a given module.
643 * The use case is with static routes and route maps
644 * example : ip route 1.1.1.1/32 ens33
645 * ip route 1.1.1.1/32 ens34
646 * After this no ip route 1.1.1.1/32 ens34 came, now staticd
647 * has to find out upto which level it has to delete the dnodes.
648 * For this case it has to send delete nexthop
649 * After this no ip route 1.1.1.1/32 ens33 came, now staticd has to
650 * clear nexthop, path and route nodes.
651 * The same scheme is required for routemaps also
652 * dnode The starting node to work on
654 * Returns The final parent node selected for deletion
656 extern const struct lyd_node
*
657 yang_get_subtree_with_no_sibling(const struct lyd_node
*dnode
);
659 /* To get the relative position of a node in list */
660 extern uint32_t yang_get_list_pos(const struct lyd_node
*node
);
662 /* To get the number of elements in a list
664 * dnode : The head of list
665 * Returns : The number of dnodes present in the list
667 extern uint32_t yang_get_list_elements_count(const struct lyd_node
*node
);
669 /* API to check if the given node is last node in the list */
670 bool yang_is_last_list_dnode(const struct lyd_node
*dnode
);
672 /* API to check if the given node is last node in the data tree level */
673 bool yang_is_last_level_dnode(const struct lyd_node
*dnode
);
679 #endif /* _FRR_YANG_H_ */