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"
32 DECLARE_MTYPE(YANG_MODULE
)
33 DECLARE_MTYPE(YANG_DATA
)
35 /* Maximum XPath length. */
36 #define XPATH_MAXLEN 256
38 /* Maximum list key length. */
39 #define LIST_MAXKEYS 8
41 /* Maximum list key length. */
42 #define LIST_MAXKEYLEN 128
44 /* Maximum string length of an YANG value. */
45 #define YANG_VALUE_MAXLEN 1024
47 struct yang_module_embed
{
48 struct yang_module_embed
*next
;
49 const char *mod_name
, *mod_rev
;
55 RB_ENTRY(yang_module
) entry
;
57 const struct lys_module
*info
;
62 sr_subscription_ctx_t
*sr_subscription
;
65 RB_HEAD(yang_modules
, yang_module
);
66 RB_PROTOTYPE(yang_modules
, yang_module
, entry
, yang_module_compare
);
69 /* XPath identifier of the data element. */
70 char xpath
[XPATH_MAXLEN
];
73 * Schema information (necessary to interpret certain values like
76 const struct lys_node
*snode
;
78 /* Value encoded as a raw string. */
82 struct yang_list_keys
{
83 /* Number os keys (max: LIST_MAXKEYS). */
88 * Schema information (necessary to interpret certain values
91 struct lys_node
*snode
;
93 /* Value encoded as a raw string. */
94 char value
[LIST_MAXKEYLEN
];
103 /* Filter non-presence containers. */
104 #define YANG_ITER_FILTER_NPCONTAINERS 0x0001
105 /* Filter list keys (leafs). */
106 #define YANG_ITER_FILTER_LIST_KEYS 0x0002
107 /* Filter RPC input/output nodes. */
108 #define YANG_ITER_FILTER_INPUT_OUTPUT 0x0004
109 /* Filter implicitely created nodes. */
110 #define YANG_ITER_FILTER_IMPLICIT 0x0008
112 /* Global libyang context for native FRR models. */
113 extern struct ly_ctx
*ly_native_ctx
;
115 /* Tree of all loaded YANG modules. */
116 extern struct yang_modules yang_modules
;
119 * Create a new YANG module and load it using libyang. If the YANG module is not
120 * found in the YANG_MODELS_PATH directory, the program will exit with an error.
121 * Once loaded, a YANG module can't be unloaded anymore.
124 * Name of the YANG module.
127 * Pointer to newly created YANG module.
129 extern struct yang_module
*yang_module_load(const char *module_name
);
132 * Find a YANG module by its name.
135 * Name of the YANG module.
138 * Pointer to YANG module if found, NULL otherwise.
140 extern struct yang_module
*yang_module_find(const char *module_name
);
143 * Register a YANG module embedded in the binary file. Should be called
144 * from a constructor function.
147 * YANG module embedding structure to register. (static global provided
150 extern void yang_module_embed(struct yang_module_embed
*embed
);
153 * Iterate over all libyang schema nodes from the given YANG module.
156 * YANG module to operate on.
159 * Function to call with each schema node.
162 * YANG_ITER_FILTER_* flags to specify node types that should be filtered.
165 * Arbitrary argument passed as the second parameter in each call to 'func'.
168 * Arbitrary argument passed as the third parameter in each call to 'func'.
170 extern void yang_module_snodes_iterate(const struct lys_module
*module
,
171 void (*func
)(const struct lys_node
*,
173 uint16_t flags
, void *arg1
, void *arg2
);
176 * Iterate over all libyang schema nodes from all loaded YANG modules.
179 * Function to call with each schema node.
182 * YANG_ITER_FILTER_* flags to specify node types that should be filtered.
185 * Arbitrary argument passed as the second parameter in each call to 'func'.
188 * Arbitrary argument passed as the third parameter in each call to 'func'.
190 extern void yang_all_snodes_iterate(void (*func
)(const struct lys_node
*,
192 uint16_t flags
, void *arg1
, void *arg2
);
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 lys_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 lys_node
*yang_snode_real_parent(const struct lys_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 lys_node
*yang_snode_parent_list(const struct lys_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 lys_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 lys_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 lys_type
*yang_snode_get_type(const struct lys_node
*snode
);
273 * Build data path of the data node.
276 * libyang data node to be processed.
279 * Pointer to previously allocated buffer.
282 * Size of the xpath buffer.
284 extern void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
288 * Return the schema name of the given libyang data node.
294 * Optional XPath expression (absolute or relative) to specify a different
295 * data node to operate on in the same data tree.
298 * Schema name of the libyang data node.
300 extern const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
301 const char *xpath_fmt
, ...);
304 * Find a libyang data node by its YANG data path.
307 * Base libyang data node to operate on.
310 * XPath expression (absolute or relative).
313 * The libyang data node if found, or NULL if not found.
315 extern struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
316 const char *xpath_fmt
, ...);
319 * Check if a libyang data node exists.
322 * Base libyang data node to operate on.
325 * XPath expression (absolute or relative).
328 * true if the libyang data node was found, false otherwise.
330 extern bool yang_dnode_exists(const struct lyd_node
*dnode
,
331 const char *xpath_fmt
, ...);
334 * Check if the libyang data node contains a default value. Non-presence
335 * containers are assumed to always contain a default value.
338 * Base libyang data node to operate on.
341 * Optional XPath expression (absolute or relative) to specify a different
342 * data node to operate on in the same data tree.
345 * true if the data node contains the default value, false otherwise.
347 extern bool yang_dnode_is_default(const struct lyd_node
*dnode
,
348 const char *xpath_fmt
, ...);
351 * Check if the libyang data node and all of its children contain default
352 * values. Non-presence containers are assumed to always contain a default
356 * libyang data node to operate on.
359 * true if the data node and all of its children contain default values,
362 extern bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
);
365 * Change the value of a libyang leaf node.
368 * libyang data node to operate on.
371 * String representing the new value.
373 extern void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
);
376 * Set the libyang private pointer to a user pointer. Can only be used on YANG
377 * lists and containers.
380 * libyang data node to operate on.
383 * Arbitrary user-specified pointer.
385 extern void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
);
388 * Find the user pointer associated to the given libyang data node.
390 * The data node is traversed by following the parent pointers until an user
391 * pointer is found or until the root node is reached.
394 * libyang data node to operate on.
397 * When set to true, abort the program if no user pointer is found.
399 * As a rule of thumb, this parameter should be set to true in the following
401 * - Calling this function from any northbound configuration callback during
402 * the NB_EV_APPLY phase.
403 * - Calling this function from a 'delete' northbound configuration callback
406 * In both the above cases, the libyang data node should contain an user
407 * pointer except when there's a bug in the code, in which case it's better
408 * to abort the program right away and eliminate the need for unnecessary
411 * In all other cases, this parameter should be set to false and the caller
412 * should check if the function returned NULL or not.
415 * User pointer if found, NULL otherwise.
417 extern void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
418 bool abort_if_not_found
);
421 * Create a new libyang data node.
424 * libyang context to operate on.
427 * Pointer to newly created libyang data node.
429 extern struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
);
432 * Duplicate a libyang data node.
435 * libyang data node to duplicate.
438 * Pointer to duplicated libyang data node.
440 extern struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
);
443 * Delete a libyang data node.
446 * Pointer to the libyang data node that is going to be deleted.
448 extern void yang_dnode_free(struct lyd_node
*dnode
);
451 * Create a new yang_data structure.
454 * Data path of the YANG data.
457 * String representing the value of the YANG data.
460 * Pointer to newly created yang_data structure.
462 extern struct yang_data
*yang_data_new(const char *xpath
, const char *value
);
465 * Delete a yang_data structure.
468 * yang_data to delete.
470 extern void yang_data_free(struct yang_data
*data
);
473 * Create a new linked list of yang_data structures. The list 'del' callback is
474 * initialized appropriately so that the entire list can be deleted safely with
475 * list_delete_and_null().
478 * Pointer to newly created linked list.
480 extern struct list
*yang_data_list_new(void);
483 * Initialize the YANG subsystem. Should be called only once during the
484 * daemon initialization process.
486 extern void yang_init(void);
489 * Finish the YANG subsystem gracefully. Should be called only when the daemon
492 extern void yang_terminate(void);
494 #endif /* _FRR_YANG_H_ */