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 DECLARE_MTYPE(YANG_MODULE
)
37 DECLARE_MTYPE(YANG_DATA
)
39 /* Maximum XPath length. */
40 #define XPATH_MAXLEN 256
42 /* Maximum list key length. */
43 #define LIST_MAXKEYS 8
45 /* Maximum list key length. */
46 #define LIST_MAXKEYLEN 128
48 /* Maximum string length of an YANG value. */
49 #define YANG_VALUE_MAXLEN 1024
51 struct yang_module_embed
{
52 struct yang_module_embed
*next
;
53 const char *mod_name
, *mod_rev
;
59 RB_ENTRY(yang_module
) entry
;
61 const struct lys_module
*info
;
66 sr_subscription_ctx_t
*sr_subscription
;
69 RB_HEAD(yang_modules
, yang_module
);
70 RB_PROTOTYPE(yang_modules
, yang_module
, entry
, yang_module_compare
);
73 /* XPath identifier of the data element. */
74 char xpath
[XPATH_MAXLEN
];
76 /* Value encoded as a raw string. */
80 struct yang_list_keys
{
81 /* Number os keys (max: LIST_MAXKEYS). */
84 /* Value encoded as a raw string. */
85 char key
[LIST_MAXKEYS
][LIST_MAXKEYLEN
];
93 enum yang_iter_flags
{
94 /* Filter non-presence containers. */
95 YANG_ITER_FILTER_NPCONTAINERS
= (1<<0),
97 /* Filter list keys (leafs). */
98 YANG_ITER_FILTER_LIST_KEYS
= (1<<1),
100 /* Filter RPC input/output nodes. */
101 YANG_ITER_FILTER_INPUT_OUTPUT
= (1<<2),
103 /* Filter implicitely created nodes. */
104 YANG_ITER_FILTER_IMPLICIT
= (1<<3),
106 /* Allow iteration over augmentations. */
107 YANG_ITER_ALLOW_AUGMENTATIONS
= (1<<4),
110 /* Callback used by the yang_snodes_iterate_*() family of functions. */
111 typedef int (*yang_iterate_cb
)(const struct lys_node
*snode
, void *arg
);
113 /* Return values of the 'yang_iterate_cb' callback. */
114 #define YANG_ITER_CONTINUE 0
115 #define YANG_ITER_STOP -1
117 /* Global libyang context for native FRR models. */
118 extern struct ly_ctx
*ly_native_ctx
;
120 /* Tree of all loaded YANG modules. */
121 extern struct yang_modules yang_modules
;
124 * Create a new YANG module and load it using libyang. If the YANG module is not
125 * found in the YANG_MODELS_PATH directory, the program will exit with an error.
126 * Once loaded, a YANG module can't be unloaded anymore.
129 * Name of the YANG module.
132 * Pointer to newly created YANG module.
134 extern struct yang_module
*yang_module_load(const char *module_name
);
137 * Load all FRR native YANG models.
139 extern void yang_module_load_all(void);
142 * Find a YANG module by its name.
145 * Name of the YANG module.
148 * Pointer to YANG module if found, NULL otherwise.
150 extern struct yang_module
*yang_module_find(const char *module_name
);
153 * Register a YANG module embedded in the binary file. Should be called
154 * from a constructor function.
157 * YANG module embedding structure to register. (static global provided
160 extern void yang_module_embed(struct yang_module_embed
*embed
);
163 * Iterate recursively over all children of a schema node.
166 * YANG schema node to operate on.
169 * Function to call with each schema node.
172 * YANG_ITER_* flags to control how the iteration is performed.
175 * Arbitrary argument passed as the second parameter in each call to 'cb'.
178 * The return value of the last called callback.
180 extern int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
181 yang_iterate_cb cb
, uint16_t flags
,
185 * Iterate over all libyang schema nodes from the given YANG module.
188 * YANG module to operate on.
191 * Function to call with each schema node.
194 * YANG_ITER_* flags to control how the iteration is performed.
197 * Arbitrary argument passed as the second parameter in each call to 'cb'.
200 * The return value of the last called callback.
202 extern int yang_snodes_iterate_module(const struct lys_module
*module
,
203 yang_iterate_cb cb
, uint16_t flags
,
207 * Iterate over all libyang schema nodes from all loaded YANG modules.
210 * Function to call with each schema node.
213 * YANG_ITER_* flags to control how the iteration is performed.
216 * Arbitrary argument passed as the second parameter in each call to 'cb'.
219 * The return value of the last called callback.
221 extern int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
,
225 * Build schema path or data path of the schema node.
228 * libyang schema node to be processed.
231 * Specify whether a schema path or a data path should be built.
234 * Pointer to previously allocated buffer.
237 * Size of the xpath buffer.
239 extern void yang_snode_get_path(const struct lys_node
*snode
,
240 enum yang_path_type type
, char *xpath
,
244 * Find first parent schema node which is a presence-container or a list
245 * (non-presence containers are ignored).
248 * libyang schema node to operate on.
251 * The parent libyang schema node if found, or NULL if not found.
253 extern struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
);
256 * Find first parent schema node which is a list.
259 * libyang schema node to operate on.
262 * The parent libyang schema node (list) if found, or NULL if not found.
264 extern struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
);
267 * Check if the libyang schema node represents typeless data (e.g. containers,
268 * leafs of type empty, etc).
271 * libyang schema node to operate on.
274 * true if the schema node represents typeless data, false otherwise.
276 extern bool yang_snode_is_typeless_data(const struct lys_node
*snode
);
279 * Get the default value associated to a YANG leaf or leaf-list.
282 * libyang schema node to operate on.
285 * The default value if it exists, NULL otherwise.
287 extern const char *yang_snode_get_default(const struct lys_node
*snode
);
290 * Get the type structure of a leaf of leaf-list. If the type is a leafref, the
291 * final (if there is a chain of leafrefs) target's type is found.
294 * libyang schema node to operate on.
297 * The found type if the schema node represents a leaf or a leaf-list, NULL
300 extern const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
);
303 * Build data path of the data node.
306 * libyang data node to be processed.
309 * Pointer to previously allocated buffer.
312 * Size of the xpath buffer.
314 extern void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
318 * Return the schema name of the given libyang data node.
324 * Optional XPath expression (absolute or relative) to specify a different
325 * data node to operate on in the same data tree.
328 * Schema name of the libyang data node.
330 extern const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
331 const char *xpath_fmt
, ...);
334 * Find a libyang data node by its YANG data path.
337 * Base libyang data node to operate on.
340 * XPath expression (absolute or relative).
343 * The libyang data node if found, or NULL if not found.
345 extern struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
346 const char *xpath_fmt
, ...);
349 * Check if a libyang data node exists.
352 * Base libyang data node to operate on.
355 * XPath expression (absolute or relative).
358 * true if the libyang data node was found, false otherwise.
360 extern bool yang_dnode_exists(const struct lyd_node
*dnode
,
361 const char *xpath_fmt
, ...);
364 * Check if the libyang data node contains a default value. Non-presence
365 * containers are assumed to always contain a default value.
368 * Base libyang data node to operate on.
371 * Optional XPath expression (absolute or relative) to specify a different
372 * data node to operate on in the same data tree.
375 * true if the data node contains the default value, false otherwise.
377 extern bool yang_dnode_is_default(const struct lyd_node
*dnode
,
378 const char *xpath_fmt
, ...);
381 * Check if the libyang data node and all of its children contain default
382 * values. Non-presence containers are assumed to always contain a default
386 * libyang data node to operate on.
389 * true if the data node and all of its children contain default values,
392 extern bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
);
395 * Change the value of a libyang leaf node.
398 * libyang data node to operate on.
401 * String representing the new value.
403 extern void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
);
406 * Set the libyang private pointer to a user pointer. Can only be used on YANG
407 * lists and containers.
410 * libyang data node to operate on.
413 * Arbitrary user-specified pointer.
415 extern void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
);
418 * Find the user pointer associated to the given libyang data node.
420 * The data node is traversed by following the parent pointers until an user
421 * pointer is found or until the root node is reached.
424 * libyang data node to operate on.
427 * When set to true, abort the program if no user pointer is found.
429 * As a rule of thumb, this parameter should be set to true in the following
431 * - Calling this function from any northbound configuration callback during
432 * the NB_EV_APPLY phase.
433 * - Calling this function from a 'delete' northbound configuration callback
436 * In both the above cases, the libyang data node should contain an user
437 * pointer except when there's a bug in the code, in which case it's better
438 * to abort the program right away and eliminate the need for unnecessary
441 * In all other cases, this parameter should be set to false and the caller
442 * should check if the function returned NULL or not.
445 * User pointer if found, NULL otherwise.
447 extern void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
448 bool abort_if_not_found
);
451 * Create a new libyang data node.
454 * libyang context to operate on.
457 * Specify whether the data node will contain only configuration data (true)
458 * or both configuration data and state data (false).
461 * Pointer to newly created libyang data node.
463 extern struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
);
466 * Duplicate a libyang data node.
469 * libyang data node to duplicate.
472 * Pointer to duplicated libyang data node.
474 extern struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
);
477 * Delete a libyang data node.
480 * Pointer to the libyang data node that is going to be deleted.
482 extern void yang_dnode_free(struct lyd_node
*dnode
);
485 * Create a new yang_data structure.
488 * Data path of the YANG data.
491 * String representing the value of the YANG data.
494 * Pointer to newly created yang_data structure.
496 extern struct yang_data
*yang_data_new(const char *xpath
, const char *value
);
499 * Delete a yang_data structure.
502 * yang_data to delete.
504 extern void yang_data_free(struct yang_data
*data
);
507 * Create a new linked list of yang_data structures. The list 'del' callback is
508 * initialized appropriately so that the entire list can be deleted safely with
509 * list_delete_and_null().
512 * Pointer to newly created linked list.
514 extern struct list
*yang_data_list_new(void);
517 * Initialize the YANG subsystem. Should be called only once during the
518 * daemon initialization process.
520 extern void yang_init(void);
523 * Finish the YANG subsystem gracefully. Should be called only when the daemon
526 extern void yang_terminate(void);
532 #endif /* _FRR_YANG_H_ */