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 * Find a libyang data node by its YANG data path.
291 * Base libyang data node to operate on.
294 * XPath expression (absolute or relative).
297 * The libyang data node if found, or NULL if not found.
299 extern struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
300 const char *xpath_fmt
, ...);
303 * Check if a libyang data node exists.
306 * Base libyang data node to operate on.
309 * XPath expression (absolute or relative).
312 * true if the libyang data node was found, false otherwise.
314 extern bool yang_dnode_exists(const struct lyd_node
*dnode
,
315 const char *xpath_fmt
, ...);
318 * Check if the libyang data node contains a default value. Non-presence
319 * containers are assumed to always contain a default value.
322 * Base libyang data node to operate on.
325 * Optional XPath expression (absolute or relative) to specify a different
326 * data node to operate on in the same data tree.
329 * true if the data node contains the default value, false otherwise.
331 extern bool yang_dnode_is_default(const struct lyd_node
*dnode
,
332 const char *xpath_fmt
, ...);
335 * Check if the libyang data node and all of its children contain default
336 * values. Non-presence containers are assumed to always contain a default
340 * libyang data node to operate on.
343 * true if the data node and all of its children contain default values,
346 extern bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
);
349 * Change the value of a libyang leaf node.
352 * libyang data node to operate on.
355 * String representing the new value.
357 extern void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
);
360 * Set the libyang private pointer to a user pointer. Can only be used on YANG
361 * lists and containers.
364 * libyang data node to operate on.
367 * Arbitrary user-specified pointer.
369 extern void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
);
372 * Find the closest data node that contains an user pointer and return it.
375 * libyang data node to operate on.
378 * User pointer if found, NULL otherwise.
380 extern void *yang_dnode_get_entry(const struct lyd_node
*dnode
);
383 * Create a new libyang data node.
386 * libyang context to operate on.
389 * Pointer to newly created libyang data node.
391 extern struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
);
394 * Duplicate a libyang data node.
397 * libyang data node to duplicate.
400 * Pointer to duplicated libyang data node.
402 extern struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
);
405 * Delete a libyang data node.
408 * Pointer to the libyang data node that is going to be deleted.
410 extern void yang_dnode_free(struct lyd_node
*dnode
);
413 * Create a new yang_data structure.
416 * Data path of the YANG data.
419 * String representing the value of the YANG data.
422 * Pointer to newly created yang_data structure.
424 extern struct yang_data
*yang_data_new(const char *xpath
, const char *value
);
427 * Delete a yang_data structure.
430 * yang_data to delete.
432 extern void yang_data_free(struct yang_data
*data
);
435 * Create a new linked list of yang_data structures. The list 'del' callback is
436 * initialized appropriately so that the entire list can be deleted safely with
437 * list_delete_and_null().
440 * Pointer to newly created linked list.
442 extern struct list
*yang_data_list_new(void);
445 * Initialize the YANG subsystem. Should be called only once during the
446 * daemon initialization process.
448 extern void yang_init(void);
451 * Finish the YANG subsystem gracefully. Should be called only when the daemon
454 extern void yang_terminate(void);
456 #endif /* _FRR_YANG_H_ */