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
25 #include <libyang/libyang.h>
30 #include "yang_wrappers.h"
36 /* Maximum XPath length. */
37 #define XPATH_MAXLEN 256
39 /* Maximum list key length. */
40 #define LIST_MAXKEYS 8
42 /* Maximum list key length. */
43 #define LIST_MAXKEYLEN 128
45 /* Maximum string length of an YANG value. */
46 #define YANG_VALUE_MAXLEN 1024
48 struct yang_module_embed
{
49 struct yang_module_embed
*next
;
50 const char *mod_name
, *mod_rev
;
56 RB_ENTRY(yang_module
) entry
;
58 const struct lys_module
*info
;
63 sr_subscription_ctx_t
*sr_subscription
;
66 RB_HEAD(yang_modules
, yang_module
);
67 RB_PROTOTYPE(yang_modules
, yang_module
, entry
, yang_module_compare
);
70 /* XPath identifier of the data element. */
71 char xpath
[XPATH_MAXLEN
];
73 /* Value encoded as a raw string. */
77 struct yang_list_keys
{
78 /* Number os keys (max: LIST_MAXKEYS). */
81 /* Value encoded as a raw string. */
82 char key
[LIST_MAXKEYS
][LIST_MAXKEYLEN
];
90 enum yang_iter_flags
{
91 /* Filter non-presence containers. */
92 YANG_ITER_FILTER_NPCONTAINERS
= (1<<0),
94 /* Filter list keys (leafs). */
95 YANG_ITER_FILTER_LIST_KEYS
= (1<<1),
97 /* Filter RPC input/output nodes. */
98 YANG_ITER_FILTER_INPUT_OUTPUT
= (1<<2),
100 /* Filter implicitely created nodes. */
101 YANG_ITER_FILTER_IMPLICIT
= (1<<3),
103 /* Allow iteration over augmentations. */
104 YANG_ITER_ALLOW_AUGMENTATIONS
= (1<<4),
107 /* Callback used by the yang_snodes_iterate_*() family of functions. */
108 typedef int (*yang_iterate_cb
)(const struct lys_node
*snode
, void *arg
);
110 /* Return values of the 'yang_iterate_cb' callback. */
111 #define YANG_ITER_CONTINUE 0
112 #define YANG_ITER_STOP -1
114 /* Global libyang context for native FRR models. */
115 extern struct ly_ctx
*ly_native_ctx
;
117 /* Tree of all loaded YANG modules. */
118 extern struct yang_modules yang_modules
;
121 * Create a new YANG module and load it using libyang. If the YANG module is not
122 * found in the YANG_MODELS_PATH directory, the program will exit with an error.
123 * Once loaded, a YANG module can't be unloaded anymore.
126 * Name of the YANG module.
129 * Pointer to newly created YANG module.
131 extern struct yang_module
*yang_module_load(const char *module_name
);
134 * Load all FRR native YANG models.
136 extern void yang_module_load_all(void);
139 * Find a YANG module by its name.
142 * Name of the YANG module.
145 * Pointer to YANG module if found, NULL otherwise.
147 extern struct yang_module
*yang_module_find(const char *module_name
);
150 * Register a YANG module embedded in the binary file. Should be called
151 * from a constructor function.
154 * YANG module embedding structure to register. (static global provided
157 extern void yang_module_embed(struct yang_module_embed
*embed
);
160 * Iterate recursively over all children of a schema node.
163 * YANG schema node to operate on.
166 * Function to call with each schema node.
169 * YANG_ITER_* flags to control how the iteration is performed.
172 * Arbitrary argument passed as the second parameter in each call to 'cb'.
175 * The return value of the last called callback.
177 extern int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
178 yang_iterate_cb cb
, uint16_t flags
,
182 * Iterate over all libyang schema nodes from the given YANG module.
185 * YANG module to operate on.
188 * Function to call with each schema node.
191 * YANG_ITER_* flags to control how the iteration is performed.
194 * Arbitrary argument passed as the second parameter in each call to 'cb'.
197 * The return value of the last called callback.
199 extern int yang_snodes_iterate_module(const struct lys_module
*module
,
200 yang_iterate_cb cb
, uint16_t flags
,
204 * Iterate over all libyang schema nodes from all loaded YANG modules.
207 * Function to call with each schema node.
210 * YANG_ITER_* flags to control how the iteration is performed.
213 * Arbitrary argument passed as the second parameter in each call to 'cb'.
216 * The return value of the last called callback.
218 extern int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
,
222 * Build schema path or data path of the schema node.
225 * libyang schema node to be processed.
228 * Specify whether a schema path or a data path should be built.
231 * Pointer to previously allocated buffer.
234 * Size of the xpath buffer.
236 extern void yang_snode_get_path(const struct lys_node
*snode
,
237 enum yang_path_type type
, char *xpath
,
241 * Find first parent schema node which is a presence-container or a list
242 * (non-presence containers are ignored).
245 * libyang schema node to operate on.
248 * The parent libyang schema node if found, or NULL if not found.
250 extern struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
);
253 * Find first parent schema node which is a list.
256 * libyang schema node to operate on.
259 * The parent libyang schema node (list) if found, or NULL if not found.
261 extern struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
);
264 * Check if the libyang schema node represents typeless data (e.g. containers,
265 * leafs of type empty, etc).
268 * libyang schema node to operate on.
271 * true if the schema node represents typeless data, false otherwise.
273 extern bool yang_snode_is_typeless_data(const struct lys_node
*snode
);
276 * Get the default value associated to a YANG leaf or leaf-list.
279 * libyang schema node to operate on.
282 * The default value if it exists, NULL otherwise.
284 extern const char *yang_snode_get_default(const struct lys_node
*snode
);
287 * Get the type structure of a leaf of leaf-list. If the type is a leafref, the
288 * final (if there is a chain of leafrefs) target's type is found.
291 * libyang schema node to operate on.
294 * The found type if the schema node represents a leaf or a leaf-list, NULL
297 extern const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
);
300 * Build data path of the data node.
303 * libyang data node to be processed.
306 * Pointer to previously allocated buffer.
309 * Size of the xpath buffer.
311 extern void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
315 * Return the schema name of the given libyang data node.
321 * Optional XPath expression (absolute or relative) to specify a different
322 * data node to operate on in the same data tree.
325 * Schema name of the libyang data node.
327 extern const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
328 const char *xpath_fmt
, ...);
331 * Find a libyang data node by its YANG data path.
334 * Base libyang data node to operate on.
337 * XPath expression (absolute or relative).
340 * The libyang data node if found, or NULL if not found.
342 extern struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
343 const char *xpath_fmt
, ...);
346 * Check if a libyang data node exists.
349 * Base libyang data node to operate on.
352 * XPath expression (absolute or relative).
355 * true if the libyang data node was found, false otherwise.
357 extern bool yang_dnode_exists(const struct lyd_node
*dnode
,
358 const char *xpath_fmt
, ...);
361 * Check if the libyang data node contains a default value. Non-presence
362 * containers are assumed to always contain a default value.
365 * Base libyang data node to operate on.
368 * Optional XPath expression (absolute or relative) to specify a different
369 * data node to operate on in the same data tree.
372 * true if the data node contains the default value, false otherwise.
374 extern bool yang_dnode_is_default(const struct lyd_node
*dnode
,
375 const char *xpath_fmt
, ...);
378 * Check if the libyang data node and all of its children contain default
379 * values. Non-presence containers are assumed to always contain a default
383 * libyang data node to operate on.
386 * true if the data node and all of its children contain default values,
389 extern bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
);
392 * Change the value of a libyang leaf node.
395 * libyang data node to operate on.
398 * String representing the new value.
400 extern void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
);
403 * Create a new libyang data node.
406 * libyang context to operate on.
409 * Specify whether the data node will contain only configuration data (true)
410 * or both configuration data and state data (false).
413 * Pointer to newly created libyang data node.
415 extern struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
);
418 * Duplicate a libyang data node.
421 * libyang data node to duplicate.
424 * Pointer to duplicated libyang data node.
426 extern struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
);
429 * Delete a libyang data node.
432 * Pointer to the libyang data node that is going to be deleted.
434 extern void yang_dnode_free(struct lyd_node
*dnode
);
437 * Create a new yang_data structure.
440 * Data path of the YANG data.
443 * String representing the value of the YANG data.
446 * Pointer to newly created yang_data structure.
448 extern struct yang_data
*yang_data_new(const char *xpath
, const char *value
);
451 * Delete a yang_data structure.
454 * yang_data to delete.
456 extern void yang_data_free(struct yang_data
*data
);
459 * Create a new linked list of yang_data structures. The list 'del' callback is
460 * initialized appropriately so that the entire list can be deleted safely with
461 * list_delete_and_null().
464 * Pointer to newly created linked list.
466 extern struct list
*yang_data_list_new(void);
469 * Find the yang_data structure corresponding to an XPath in a list.
472 * list of yang_data structures to operate on.
475 * XPath to search for (format string).
478 * Pointer to yang_data if found, NULL otherwise.
480 extern struct yang_data
*yang_data_list_find(const struct list
*list
,
481 const char *xpath_fmt
, ...);
484 * Create and set up a libyang context (for use by the translator)
486 extern struct ly_ctx
*yang_ctx_new_setup(void);
489 * Enable or disable libyang verbose debugging.
492 * When set to true, enable libyang verbose debugging, otherwise disable it.
494 extern void yang_debugging_set(bool enable
);
497 * Initialize the YANG subsystem. Should be called only once during the
498 * daemon initialization process.
500 extern void yang_init(void);
503 * Finish the YANG subsystem gracefully. Should be called only when the daemon
506 extern void yang_terminate(void);
512 #endif /* _FRR_YANG_H_ */