]>
Commit | Line | Data |
---|---|---|
1c2facd1 RW |
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 | ||
3a11599c DL |
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 | ||
1c2facd1 RW |
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 | ||
1c2facd1 RW |
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 | ||
80243aef RW |
80 | /* Value encoded as a raw string. */ |
81 | char key[LIST_MAXKEYS][LIST_MAXKEYLEN]; | |
1c2facd1 RW |
82 | }; |
83 | ||
84 | enum yang_path_type { | |
85 | YANG_PATH_SCHEMA = 0, | |
86 | YANG_PATH_DATA, | |
87 | }; | |
88 | ||
89 | /* Filter non-presence containers. */ | |
90 | #define YANG_ITER_FILTER_NPCONTAINERS 0x0001 | |
91 | /* Filter list keys (leafs). */ | |
92 | #define YANG_ITER_FILTER_LIST_KEYS 0x0002 | |
93 | /* Filter RPC input/output nodes. */ | |
94 | #define YANG_ITER_FILTER_INPUT_OUTPUT 0x0004 | |
95 | /* Filter implicitely created nodes. */ | |
96 | #define YANG_ITER_FILTER_IMPLICIT 0x0008 | |
97 | ||
98 | /* Global libyang context for native FRR models. */ | |
99 | extern struct ly_ctx *ly_native_ctx; | |
100 | ||
101 | /* Tree of all loaded YANG modules. */ | |
102 | extern struct yang_modules yang_modules; | |
103 | ||
104 | /* | |
105 | * Create a new YANG module and load it using libyang. If the YANG module is not | |
106 | * found in the YANG_MODELS_PATH directory, the program will exit with an error. | |
107 | * Once loaded, a YANG module can't be unloaded anymore. | |
108 | * | |
109 | * module_name | |
110 | * Name of the YANG module. | |
111 | * | |
112 | * Returns: | |
113 | * Pointer to newly created YANG module. | |
114 | */ | |
115 | extern struct yang_module *yang_module_load(const char *module_name); | |
116 | ||
117 | /* | |
118 | * Find a YANG module by its name. | |
119 | * | |
120 | * module_name | |
121 | * Name of the YANG module. | |
122 | * | |
123 | * Returns: | |
124 | * Pointer to YANG module if found, NULL otherwise. | |
125 | */ | |
126 | extern struct yang_module *yang_module_find(const char *module_name); | |
127 | ||
3a11599c DL |
128 | /* |
129 | * Register a YANG module embedded in the binary file. Should be called | |
130 | * from a constructor function. | |
131 | * | |
132 | * embed | |
133 | * YANG module embedding structure to register. (static global provided | |
134 | * by caller.) | |
135 | */ | |
136 | extern void yang_module_embed(struct yang_module_embed *embed); | |
137 | ||
1c2facd1 RW |
138 | /* |
139 | * Iterate over all libyang schema nodes from the given YANG module. | |
140 | * | |
141 | * module | |
142 | * YANG module to operate on. | |
143 | * | |
144 | * func | |
145 | * Function to call with each schema node. | |
146 | * | |
147 | * flags | |
148 | * YANG_ITER_FILTER_* flags to specify node types that should be filtered. | |
149 | * | |
150 | * arg1 | |
151 | * Arbitrary argument passed as the second parameter in each call to 'func'. | |
152 | * | |
153 | * arg2 | |
154 | * Arbitrary argument passed as the third parameter in each call to 'func'. | |
155 | */ | |
156 | extern void yang_module_snodes_iterate(const struct lys_module *module, | |
157 | void (*func)(const struct lys_node *, | |
158 | void *, void *), | |
159 | uint16_t flags, void *arg1, void *arg2); | |
160 | ||
161 | /* | |
162 | * Iterate over all libyang schema nodes from all loaded YANG modules. | |
163 | * | |
164 | * func | |
165 | * Function to call with each schema node. | |
166 | * | |
167 | * flags | |
168 | * YANG_ITER_FILTER_* flags to specify node types that should be filtered. | |
169 | * | |
170 | * arg1 | |
171 | * Arbitrary argument passed as the second parameter in each call to 'func'. | |
172 | * | |
173 | * arg2 | |
174 | * Arbitrary argument passed as the third parameter in each call to 'func'. | |
175 | */ | |
176 | extern void yang_all_snodes_iterate(void (*func)(const struct lys_node *, | |
177 | void *, void *), | |
178 | uint16_t flags, void *arg1, void *arg2); | |
179 | ||
180 | /* | |
181 | * Build schema path or data path of the schema node. | |
182 | * | |
183 | * snode | |
184 | * libyang schema node to be processed. | |
185 | * | |
186 | * type | |
187 | * Specify whether a schema path or a data path should be built. | |
188 | * | |
189 | * xpath | |
190 | * Pointer to previously allocated buffer. | |
191 | * | |
192 | * xpath_len | |
193 | * Size of the xpath buffer. | |
194 | */ | |
195 | extern void yang_snode_get_path(const struct lys_node *snode, | |
196 | enum yang_path_type type, char *xpath, | |
197 | size_t xpath_len); | |
198 | ||
199 | /* | |
200 | * Find first parent schema node which is a presence-container or a list | |
201 | * (non-presence containers are ignored). | |
202 | * | |
203 | * snode | |
204 | * libyang schema node to operate on. | |
205 | * | |
206 | * Returns: | |
207 | * The parent libyang schema node if found, or NULL if not found. | |
208 | */ | |
209 | extern struct lys_node *yang_snode_real_parent(const struct lys_node *snode); | |
210 | ||
211 | /* | |
212 | * Find first parent schema node which is a list. | |
213 | * | |
214 | * snode | |
215 | * libyang schema node to operate on. | |
216 | * | |
217 | * Returns: | |
218 | * The parent libyang schema node (list) if found, or NULL if not found. | |
219 | */ | |
220 | extern struct lys_node *yang_snode_parent_list(const struct lys_node *snode); | |
221 | ||
222 | /* | |
223 | * Check if the libyang schema node represents typeless data (e.g. containers, | |
224 | * leafs of type empty, etc). | |
225 | * | |
226 | * snode | |
227 | * libyang schema node to operate on. | |
228 | * | |
229 | * Returns: | |
230 | * true if the schema node represents typeless data, false otherwise. | |
231 | */ | |
232 | extern bool yang_snode_is_typeless_data(const struct lys_node *snode); | |
233 | ||
234 | /* | |
235 | * Get the default value associated to a YANG leaf or leaf-list. | |
236 | * | |
237 | * snode | |
238 | * libyang schema node to operate on. | |
239 | * | |
240 | * Returns: | |
241 | * The default value if it exists, NULL otherwise. | |
242 | */ | |
243 | extern const char *yang_snode_get_default(const struct lys_node *snode); | |
244 | ||
245 | /* | |
246 | * Get the type structure of a leaf of leaf-list. If the type is a leafref, the | |
247 | * final (if there is a chain of leafrefs) target's type is found. | |
248 | * | |
249 | * snode | |
250 | * libyang schema node to operate on. | |
251 | * | |
252 | * Returns: | |
253 | * The found type if the schema node represents a leaf or a leaf-list, NULL | |
254 | * otherwise. | |
255 | */ | |
256 | extern const struct lys_type *yang_snode_get_type(const struct lys_node *snode); | |
257 | ||
258 | /* | |
259 | * Build data path of the data node. | |
260 | * | |
261 | * dnode | |
262 | * libyang data node to be processed. | |
263 | * | |
264 | * xpath | |
265 | * Pointer to previously allocated buffer. | |
266 | * | |
267 | * xpath_len | |
268 | * Size of the xpath buffer. | |
269 | */ | |
270 | extern void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath, | |
271 | size_t xpath_len); | |
272 | ||
273 | /* | |
274 | * Find a libyang data node by its YANG data path. | |
275 | * | |
276 | * dnode | |
277 | * Base libyang data node to operate on. | |
278 | * | |
279 | * xpath_fmt | |
280 | * XPath expression (absolute or relative). | |
281 | * | |
282 | * Returns: | |
283 | * The libyang data node if found, or NULL if not found. | |
284 | */ | |
285 | extern struct lyd_node *yang_dnode_get(const struct lyd_node *dnode, | |
286 | const char *xpath_fmt, ...); | |
287 | ||
288 | /* | |
289 | * Check if a libyang data node exists. | |
290 | * | |
291 | * dnode | |
292 | * Base libyang data node to operate on. | |
293 | * | |
294 | * xpath_fmt | |
295 | * XPath expression (absolute or relative). | |
296 | * | |
297 | * Returns: | |
298 | * true if the libyang data node was found, false otherwise. | |
299 | */ | |
300 | extern bool yang_dnode_exists(const struct lyd_node *dnode, | |
301 | const char *xpath_fmt, ...); | |
302 | ||
303 | /* | |
304 | * Check if the libyang data node contains a default value. Non-presence | |
305 | * containers are assumed to always contain a default value. | |
306 | * | |
307 | * dnode | |
308 | * Base libyang data node to operate on. | |
309 | * | |
310 | * xpath_fmt | |
311 | * Optional XPath expression (absolute or relative) to specify a different | |
312 | * data node to operate on in the same data tree. | |
313 | * | |
314 | * Returns: | |
315 | * true if the data node contains the default value, false otherwise. | |
316 | */ | |
317 | extern bool yang_dnode_is_default(const struct lyd_node *dnode, | |
318 | const char *xpath_fmt, ...); | |
319 | ||
320 | /* | |
321 | * Check if the libyang data node and all of its children contain default | |
322 | * values. Non-presence containers are assumed to always contain a default | |
323 | * value. | |
324 | * | |
325 | * dnode | |
326 | * libyang data node to operate on. | |
327 | * | |
328 | * Returns: | |
329 | * true if the data node and all of its children contain default values, | |
330 | * false otherwise. | |
331 | */ | |
332 | extern bool yang_dnode_is_default_recursive(const struct lyd_node *dnode); | |
333 | ||
334 | /* | |
335 | * Change the value of a libyang leaf node. | |
336 | * | |
337 | * dnode | |
338 | * libyang data node to operate on. | |
339 | * | |
340 | * value | |
341 | * String representing the new value. | |
342 | */ | |
343 | extern void yang_dnode_change_leaf(struct lyd_node *dnode, const char *value); | |
344 | ||
345 | /* | |
346 | * Set the libyang private pointer to a user pointer. Can only be used on YANG | |
347 | * lists and containers. | |
348 | * | |
349 | * dnode | |
350 | * libyang data node to operate on. | |
351 | * | |
352 | * entry | |
353 | * Arbitrary user-specified pointer. | |
354 | */ | |
355 | extern void yang_dnode_set_entry(const struct lyd_node *dnode, void *entry); | |
356 | ||
357 | /* | |
358 | * Find the closest data node that contains an user pointer and return it. | |
359 | * | |
360 | * dnode | |
361 | * libyang data node to operate on. | |
362 | * | |
363 | * Returns: | |
364 | * User pointer if found, NULL otherwise. | |
365 | */ | |
366 | extern void *yang_dnode_get_entry(const struct lyd_node *dnode); | |
367 | ||
368 | /* | |
369 | * Create a new libyang data node. | |
370 | * | |
371 | * ly_ctx | |
372 | * libyang context to operate on. | |
373 | * | |
5e02643a RW |
374 | * config |
375 | * Specify whether the data node will contain only configuration data (true) | |
376 | * or both configuration data and state data (false). | |
377 | * | |
1c2facd1 RW |
378 | * Returns: |
379 | * Pointer to newly created libyang data node. | |
380 | */ | |
5e02643a | 381 | extern struct lyd_node *yang_dnode_new(struct ly_ctx *ly_ctx, bool config_only); |
1c2facd1 RW |
382 | |
383 | /* | |
384 | * Duplicate a libyang data node. | |
385 | * | |
386 | * dnode | |
387 | * libyang data node to duplicate. | |
388 | * | |
389 | * Returns: | |
390 | * Pointer to duplicated libyang data node. | |
391 | */ | |
392 | extern struct lyd_node *yang_dnode_dup(const struct lyd_node *dnode); | |
393 | ||
394 | /* | |
395 | * Delete a libyang data node. | |
396 | * | |
397 | * dnode | |
398 | * Pointer to the libyang data node that is going to be deleted. | |
399 | */ | |
400 | extern void yang_dnode_free(struct lyd_node *dnode); | |
401 | ||
402 | /* | |
403 | * Create a new yang_data structure. | |
404 | * | |
405 | * xpath | |
406 | * Data path of the YANG data. | |
407 | * | |
408 | * value | |
409 | * String representing the value of the YANG data. | |
410 | * | |
411 | * Returns: | |
412 | * Pointer to newly created yang_data structure. | |
413 | */ | |
414 | extern struct yang_data *yang_data_new(const char *xpath, const char *value); | |
415 | ||
416 | /* | |
417 | * Delete a yang_data structure. | |
418 | * | |
419 | * data | |
420 | * yang_data to delete. | |
421 | */ | |
422 | extern void yang_data_free(struct yang_data *data); | |
423 | ||
424 | /* | |
425 | * Create a new linked list of yang_data structures. The list 'del' callback is | |
426 | * initialized appropriately so that the entire list can be deleted safely with | |
427 | * list_delete_and_null(). | |
428 | * | |
429 | * Returns: | |
430 | * Pointer to newly created linked list. | |
431 | */ | |
432 | extern struct list *yang_data_list_new(void); | |
433 | ||
434 | /* | |
435 | * Initialize the YANG subsystem. Should be called only once during the | |
436 | * daemon initialization process. | |
437 | */ | |
438 | extern void yang_init(void); | |
439 | ||
440 | /* | |
441 | * Finish the YANG subsystem gracefully. Should be called only when the daemon | |
442 | * is exiting. | |
443 | */ | |
444 | extern void yang_terminate(void); | |
445 | ||
446 | #endif /* _FRR_YANG_H_ */ |