]> git.proxmox.com Git - mirror_frr.git/blob - lib/yang.h
lib: rework the yang schema node iteration functions
[mirror_frr.git] / lib / yang.h
1 /*
2 * Copyright (C) 2018 NetDEF, Inc.
3 * Renato Westphal
4 *
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)
8 * any later version.
9 *
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
13 * more details.
14 *
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
18 */
19
20 #ifndef _FRR_YANG_H_
21 #define _FRR_YANG_H_
22
23 #include "memory.h"
24
25 #include <libyang/libyang.h>
26 #ifdef HAVE_SYSREPO
27 #include <sysrepo.h>
28 #endif
29
30 #include "yang_wrappers.h"
31
32 DECLARE_MTYPE(YANG_MODULE)
33 DECLARE_MTYPE(YANG_DATA)
34
35 /* Maximum XPath length. */
36 #define XPATH_MAXLEN 256
37
38 /* Maximum list key length. */
39 #define LIST_MAXKEYS 8
40
41 /* Maximum list key length. */
42 #define LIST_MAXKEYLEN 128
43
44 /* Maximum string length of an YANG value. */
45 #define YANG_VALUE_MAXLEN 1024
46
47 struct yang_module_embed {
48 struct yang_module_embed *next;
49 const char *mod_name, *mod_rev;
50 const char *data;
51 LYS_INFORMAT format;
52 };
53
54 struct yang_module {
55 RB_ENTRY(yang_module) entry;
56 const char *name;
57 const struct lys_module *info;
58 #ifdef HAVE_CONFD
59 int confd_hash;
60 #endif
61 #ifdef HAVE_SYSREPO
62 sr_subscription_ctx_t *sr_subscription;
63 #endif
64 };
65 RB_HEAD(yang_modules, yang_module);
66 RB_PROTOTYPE(yang_modules, yang_module, entry, yang_module_compare);
67
68 struct yang_data {
69 /* XPath identifier of the data element. */
70 char xpath[XPATH_MAXLEN];
71
72 /* Value encoded as a raw string. */
73 char *value;
74 };
75
76 struct yang_list_keys {
77 /* Number os keys (max: LIST_MAXKEYS). */
78 uint8_t num;
79
80 /* Value encoded as a raw string. */
81 char key[LIST_MAXKEYS][LIST_MAXKEYLEN];
82 };
83
84 enum yang_path_type {
85 YANG_PATH_SCHEMA = 0,
86 YANG_PATH_DATA,
87 };
88
89 enum yang_iter_flags {
90 /* Filter non-presence containers. */
91 YANG_ITER_FILTER_NPCONTAINERS = (1<<0),
92
93 /* Filter list keys (leafs). */
94 YANG_ITER_FILTER_LIST_KEYS = (1<<1),
95
96 /* Filter RPC input/output nodes. */
97 YANG_ITER_FILTER_INPUT_OUTPUT = (1<<2),
98
99 /* Filter implicitely created nodes. */
100 YANG_ITER_FILTER_IMPLICIT = (1<<3),
101 };
102
103 /* Callback used by the yang_snodes_iterate_*() family of functions. */
104 typedef int (*yang_iterate_cb)(const struct lys_node *snode, void *arg);
105
106 /* Return values of the 'yang_iterate_cb' callback. */
107 #define YANG_ITER_CONTINUE 0
108 #define YANG_ITER_STOP -1
109
110 /* Global libyang context for native FRR models. */
111 extern struct ly_ctx *ly_native_ctx;
112
113 /* Tree of all loaded YANG modules. */
114 extern struct yang_modules yang_modules;
115
116 /*
117 * Create a new YANG module and load it using libyang. If the YANG module is not
118 * found in the YANG_MODELS_PATH directory, the program will exit with an error.
119 * Once loaded, a YANG module can't be unloaded anymore.
120 *
121 * module_name
122 * Name of the YANG module.
123 *
124 * Returns:
125 * Pointer to newly created YANG module.
126 */
127 extern struct yang_module *yang_module_load(const char *module_name);
128
129 /*
130 * Load all FRR native YANG models.
131 */
132 extern void yang_module_load_all(void);
133
134 /*
135 * Find a YANG module by its name.
136 *
137 * module_name
138 * Name of the YANG module.
139 *
140 * Returns:
141 * Pointer to YANG module if found, NULL otherwise.
142 */
143 extern struct yang_module *yang_module_find(const char *module_name);
144
145 /*
146 * Register a YANG module embedded in the binary file. Should be called
147 * from a constructor function.
148 *
149 * embed
150 * YANG module embedding structure to register. (static global provided
151 * by caller.)
152 */
153 extern void yang_module_embed(struct yang_module_embed *embed);
154
155 /*
156 * Iterate recursively over all children of a schema node.
157 *
158 * snode
159 * YANG schema node to operate on.
160 *
161 * cb
162 * Function to call with each schema node.
163 *
164 * flags
165 * YANG_ITER_* flags to control how the iteration is performed.
166 *
167 * arg
168 * Arbitrary argument passed as the second parameter in each call to 'cb'.
169 *
170 * Returns:
171 * The return value of the last called callback.
172 */
173 extern int yang_snodes_iterate_subtree(const struct lys_node *snode,
174 yang_iterate_cb cb, uint16_t flags,
175 void *arg);
176
177 /*
178 * Iterate over all libyang schema nodes from the given YANG module.
179 *
180 * module
181 * YANG module to operate on.
182 *
183 * cb
184 * Function to call with each schema node.
185 *
186 * flags
187 * YANG_ITER_* flags to control how the iteration is performed.
188 *
189 * arg
190 * Arbitrary argument passed as the second parameter in each call to 'cb'.
191 *
192 * Returns:
193 * The return value of the last called callback.
194 */
195 extern int yang_snodes_iterate_module(const struct lys_module *module,
196 yang_iterate_cb cb, uint16_t flags,
197 void *arg);
198
199 /*
200 * Iterate over all libyang schema nodes from all loaded YANG modules.
201 *
202 * cb
203 * Function to call with each schema node.
204 *
205 * flags
206 * YANG_ITER_* flags to control how the iteration is performed.
207 *
208 * arg
209 * Arbitrary argument passed as the second parameter in each call to 'cb'.
210 *
211 * Returns:
212 * The return value of the last called callback.
213 */
214 extern int yang_snodes_iterate_all(yang_iterate_cb cb, uint16_t flags,
215 void *arg);
216
217 /*
218 * Build schema path or data path of the schema node.
219 *
220 * snode
221 * libyang schema node to be processed.
222 *
223 * type
224 * Specify whether a schema path or a data path should be built.
225 *
226 * xpath
227 * Pointer to previously allocated buffer.
228 *
229 * xpath_len
230 * Size of the xpath buffer.
231 */
232 extern void yang_snode_get_path(const struct lys_node *snode,
233 enum yang_path_type type, char *xpath,
234 size_t xpath_len);
235
236 /*
237 * Find first parent schema node which is a presence-container or a list
238 * (non-presence containers are ignored).
239 *
240 * snode
241 * libyang schema node to operate on.
242 *
243 * Returns:
244 * The parent libyang schema node if found, or NULL if not found.
245 */
246 extern struct lys_node *yang_snode_real_parent(const struct lys_node *snode);
247
248 /*
249 * Find first parent schema node which is a list.
250 *
251 * snode
252 * libyang schema node to operate on.
253 *
254 * Returns:
255 * The parent libyang schema node (list) if found, or NULL if not found.
256 */
257 extern struct lys_node *yang_snode_parent_list(const struct lys_node *snode);
258
259 /*
260 * Check if the libyang schema node represents typeless data (e.g. containers,
261 * leafs of type empty, etc).
262 *
263 * snode
264 * libyang schema node to operate on.
265 *
266 * Returns:
267 * true if the schema node represents typeless data, false otherwise.
268 */
269 extern bool yang_snode_is_typeless_data(const struct lys_node *snode);
270
271 /*
272 * Get the default value associated to a YANG leaf or leaf-list.
273 *
274 * snode
275 * libyang schema node to operate on.
276 *
277 * Returns:
278 * The default value if it exists, NULL otherwise.
279 */
280 extern const char *yang_snode_get_default(const struct lys_node *snode);
281
282 /*
283 * Get the type structure of a leaf of leaf-list. If the type is a leafref, the
284 * final (if there is a chain of leafrefs) target's type is found.
285 *
286 * snode
287 * libyang schema node to operate on.
288 *
289 * Returns:
290 * The found type if the schema node represents a leaf or a leaf-list, NULL
291 * otherwise.
292 */
293 extern const struct lys_type *yang_snode_get_type(const struct lys_node *snode);
294
295 /*
296 * Build data path of the data node.
297 *
298 * dnode
299 * libyang data node to be processed.
300 *
301 * xpath
302 * Pointer to previously allocated buffer.
303 *
304 * xpath_len
305 * Size of the xpath buffer.
306 */
307 extern void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath,
308 size_t xpath_len);
309
310 /*
311 * Find a libyang data node by its YANG data path.
312 *
313 * dnode
314 * Base libyang data node to operate on.
315 *
316 * xpath_fmt
317 * XPath expression (absolute or relative).
318 *
319 * Returns:
320 * The libyang data node if found, or NULL if not found.
321 */
322 extern struct lyd_node *yang_dnode_get(const struct lyd_node *dnode,
323 const char *xpath_fmt, ...);
324
325 /*
326 * Check if a libyang data node exists.
327 *
328 * dnode
329 * Base libyang data node to operate on.
330 *
331 * xpath_fmt
332 * XPath expression (absolute or relative).
333 *
334 * Returns:
335 * true if the libyang data node was found, false otherwise.
336 */
337 extern bool yang_dnode_exists(const struct lyd_node *dnode,
338 const char *xpath_fmt, ...);
339
340 /*
341 * Check if the libyang data node contains a default value. Non-presence
342 * containers are assumed to always contain a default value.
343 *
344 * dnode
345 * Base libyang data node to operate on.
346 *
347 * xpath_fmt
348 * Optional XPath expression (absolute or relative) to specify a different
349 * data node to operate on in the same data tree.
350 *
351 * Returns:
352 * true if the data node contains the default value, false otherwise.
353 */
354 extern bool yang_dnode_is_default(const struct lyd_node *dnode,
355 const char *xpath_fmt, ...);
356
357 /*
358 * Check if the libyang data node and all of its children contain default
359 * values. Non-presence containers are assumed to always contain a default
360 * value.
361 *
362 * dnode
363 * libyang data node to operate on.
364 *
365 * Returns:
366 * true if the data node and all of its children contain default values,
367 * false otherwise.
368 */
369 extern bool yang_dnode_is_default_recursive(const struct lyd_node *dnode);
370
371 /*
372 * Change the value of a libyang leaf node.
373 *
374 * dnode
375 * libyang data node to operate on.
376 *
377 * value
378 * String representing the new value.
379 */
380 extern void yang_dnode_change_leaf(struct lyd_node *dnode, const char *value);
381
382 /*
383 * Set the libyang private pointer to a user pointer. Can only be used on YANG
384 * lists and containers.
385 *
386 * dnode
387 * libyang data node to operate on.
388 *
389 * entry
390 * Arbitrary user-specified pointer.
391 */
392 extern void yang_dnode_set_entry(const struct lyd_node *dnode, void *entry);
393
394 /*
395 * Find the closest data node that contains an user pointer and return it.
396 *
397 * dnode
398 * libyang data node to operate on.
399 *
400 * Returns:
401 * User pointer if found, NULL otherwise.
402 */
403 extern void *yang_dnode_get_entry(const struct lyd_node *dnode);
404
405 /*
406 * Create a new libyang data node.
407 *
408 * ly_ctx
409 * libyang context to operate on.
410 *
411 * config
412 * Specify whether the data node will contain only configuration data (true)
413 * or both configuration data and state data (false).
414 *
415 * Returns:
416 * Pointer to newly created libyang data node.
417 */
418 extern struct lyd_node *yang_dnode_new(struct ly_ctx *ly_ctx, bool config_only);
419
420 /*
421 * Duplicate a libyang data node.
422 *
423 * dnode
424 * libyang data node to duplicate.
425 *
426 * Returns:
427 * Pointer to duplicated libyang data node.
428 */
429 extern struct lyd_node *yang_dnode_dup(const struct lyd_node *dnode);
430
431 /*
432 * Delete a libyang data node.
433 *
434 * dnode
435 * Pointer to the libyang data node that is going to be deleted.
436 */
437 extern void yang_dnode_free(struct lyd_node *dnode);
438
439 /*
440 * Create a new yang_data structure.
441 *
442 * xpath
443 * Data path of the YANG data.
444 *
445 * value
446 * String representing the value of the YANG data.
447 *
448 * Returns:
449 * Pointer to newly created yang_data structure.
450 */
451 extern struct yang_data *yang_data_new(const char *xpath, const char *value);
452
453 /*
454 * Delete a yang_data structure.
455 *
456 * data
457 * yang_data to delete.
458 */
459 extern void yang_data_free(struct yang_data *data);
460
461 /*
462 * Create a new linked list of yang_data structures. The list 'del' callback is
463 * initialized appropriately so that the entire list can be deleted safely with
464 * list_delete_and_null().
465 *
466 * Returns:
467 * Pointer to newly created linked list.
468 */
469 extern struct list *yang_data_list_new(void);
470
471 /*
472 * Initialize the YANG subsystem. Should be called only once during the
473 * daemon initialization process.
474 */
475 extern void yang_init(void);
476
477 /*
478 * Finish the YANG subsystem gracefully. Should be called only when the daemon
479 * is exiting.
480 */
481 extern void yang_terminate(void);
482
483 #endif /* _FRR_YANG_H_ */