]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1c2facd1 RW |
2 | /* |
3 | * Copyright (C) 2018 NetDEF, Inc. | |
4 | * Renato Westphal | |
1c2facd1 RW |
5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
9 | #include "log.h" | |
1c2facd1 RW |
10 | #include "lib_errors.h" |
11 | #include "yang.h" | |
12 | #include "yang_translator.h" | |
13 | #include "northbound.h" | |
14 | ||
bf8d3d6a DL |
15 | DEFINE_MTYPE_STATIC(LIB, YANG_MODULE, "YANG module"); |
16 | DEFINE_MTYPE_STATIC(LIB, YANG_DATA, "YANG data structure"); | |
1c2facd1 RW |
17 | |
18 | /* libyang container. */ | |
19 | struct ly_ctx *ly_native_ctx; | |
20 | ||
3a11599c DL |
21 | static struct yang_module_embed *embeds, **embedupd = &embeds; |
22 | ||
23 | void yang_module_embed(struct yang_module_embed *embed) | |
24 | { | |
25 | embed->next = NULL; | |
26 | *embedupd = embed; | |
27 | embedupd = &embed->next; | |
28 | } | |
29 | ||
3bb513c3 CH |
30 | static LY_ERR yang_module_imp_clb(const char *mod_name, const char *mod_rev, |
31 | const char *submod_name, | |
32 | const char *submod_rev, void *user_data, | |
33 | LYS_INFORMAT *format, | |
34 | const char **module_data, | |
35 | void (**free_module_data)(void *, void *)) | |
3a11599c DL |
36 | { |
37 | struct yang_module_embed *e; | |
38 | ||
908e4c85 DL |
39 | if (!strcmp(mod_name, "ietf-inet-types") || |
40 | !strcmp(mod_name, "ietf-yang-types")) | |
41 | /* libyang has these built in, don't try finding them here */ | |
42 | return LY_ENOTFOUND; | |
43 | ||
3a11599c | 44 | for (e = embeds; e; e = e->next) { |
65de8bc8 | 45 | if (e->sub_mod_name && submod_name) { |
46 | if (strcmp(e->sub_mod_name, submod_name)) | |
47 | continue; | |
48 | ||
49 | if (submod_rev && strcmp(e->sub_mod_rev, submod_rev)) | |
50 | continue; | |
51 | } else { | |
52 | if (strcmp(e->mod_name, mod_name)) | |
53 | continue; | |
54 | ||
55 | if (mod_rev && strcmp(e->mod_rev, mod_rev)) | |
56 | continue; | |
57 | } | |
3a11599c DL |
58 | |
59 | *format = e->format; | |
3bb513c3 CH |
60 | *module_data = e->data; |
61 | return LY_SUCCESS; | |
3a11599c DL |
62 | } |
63 | ||
3bb513c3 CH |
64 | /* We get here for indirect modules like ietf-inet-types */ |
65 | zlog_debug( | |
65de8bc8 | 66 | "YANG model \"%s@%s\" \"%s@%s\"not embedded, trying external file", |
67 | mod_name, mod_rev ? mod_rev : "*", | |
68 | submod_name ? submod_name : "*", submod_rev ? submod_rev : "*"); | |
3bb513c3 CH |
69 | |
70 | return LY_ENOTFOUND; | |
3a11599c DL |
71 | } |
72 | ||
40664f16 | 73 | /* clang-format off */ |
96f2c009 | 74 | static const char *const frr_native_modules[] = { |
a1b5f469 | 75 | "frr-interface", |
bc867a5d | 76 | "frr-vrf", |
6a7fb29c | 77 | "frr-routing", |
05a12619 | 78 | "frr-affinity-map", |
6a7fb29c CS |
79 | "frr-route-map", |
80 | "frr-nexthop", | |
a1b5f469 | 81 | "frr-ripd", |
e9ce224b | 82 | "frr-ripngd", |
96f2c009 | 83 | "frr-isisd", |
f495425b | 84 | "frr-vrrpd", |
0d2e2bd1 | 85 | "frr-zebra", |
4d7b695d | 86 | "frr-pathd", |
a1b5f469 | 87 | }; |
40664f16 | 88 | /* clang-format on */ |
a1b5f469 | 89 | |
1c2facd1 RW |
90 | /* Generate the yang_modules tree. */ |
91 | static inline int yang_module_compare(const struct yang_module *a, | |
92 | const struct yang_module *b) | |
93 | { | |
94 | return strcmp(a->name, b->name); | |
95 | } | |
96 | RB_GENERATE(yang_modules, yang_module, entry, yang_module_compare) | |
97 | ||
98 | struct yang_modules yang_modules = RB_INITIALIZER(&yang_modules); | |
99 | ||
100 | struct yang_module *yang_module_load(const char *module_name) | |
101 | { | |
102 | struct yang_module *module; | |
103 | const struct lys_module *module_info; | |
104 | ||
3bb513c3 CH |
105 | module_info = |
106 | ly_ctx_load_module(ly_native_ctx, module_name, NULL, NULL); | |
1c2facd1 RW |
107 | if (!module_info) { |
108 | flog_err(EC_LIB_YANG_MODULE_LOAD, | |
109 | "%s: failed to load data model: %s", __func__, | |
110 | module_name); | |
111 | exit(1); | |
112 | } | |
113 | ||
114 | module = XCALLOC(MTYPE_YANG_MODULE, sizeof(*module)); | |
115 | module->name = module_name; | |
116 | module->info = module_info; | |
117 | ||
118 | if (RB_INSERT(yang_modules, &yang_modules, module) != NULL) { | |
119 | flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY, | |
120 | "%s: YANG module is loaded already: %s", __func__, | |
121 | module_name); | |
122 | exit(1); | |
123 | } | |
124 | ||
125 | return module; | |
126 | } | |
127 | ||
a1b5f469 RW |
128 | void yang_module_load_all(void) |
129 | { | |
130 | for (size_t i = 0; i < array_size(frr_native_modules); i++) | |
131 | yang_module_load(frr_native_modules[i]); | |
132 | } | |
133 | ||
1c2facd1 RW |
134 | struct yang_module *yang_module_find(const char *module_name) |
135 | { | |
136 | struct yang_module s; | |
137 | ||
138 | s.name = module_name; | |
139 | return RB_FIND(yang_modules, &yang_modules, &s); | |
140 | } | |
141 | ||
3bb513c3 | 142 | int yang_snodes_iterate_subtree(const struct lysc_node *snode, |
8a923b48 | 143 | const struct lys_module *module, |
e0ccfad2 | 144 | yang_iterate_cb cb, uint16_t flags, void *arg) |
1c2facd1 | 145 | { |
3bb513c3 | 146 | const struct lysc_node *child; |
e0ccfad2 | 147 | int ret = YANG_ITER_CONTINUE; |
1c2facd1 | 148 | |
8a923b48 RW |
149 | if (module && snode->module != module) |
150 | goto next; | |
151 | ||
1c2facd1 RW |
152 | switch (snode->nodetype) { |
153 | case LYS_CONTAINER: | |
154 | if (CHECK_FLAG(flags, YANG_ITER_FILTER_NPCONTAINERS)) { | |
3bb513c3 | 155 | if (!CHECK_FLAG(snode->flags, LYS_PRESENCE)) |
1c2facd1 RW |
156 | goto next; |
157 | } | |
158 | break; | |
159 | case LYS_LEAF: | |
160 | if (CHECK_FLAG(flags, YANG_ITER_FILTER_LIST_KEYS)) { | |
1c2facd1 | 161 | /* Ignore list keys. */ |
3bb513c3 | 162 | if (lysc_is_key(snode)) |
1c2facd1 RW |
163 | goto next; |
164 | } | |
165 | break; | |
1c2facd1 RW |
166 | case LYS_INPUT: |
167 | case LYS_OUTPUT: | |
168 | if (CHECK_FLAG(flags, YANG_ITER_FILTER_INPUT_OUTPUT)) | |
169 | goto next; | |
170 | break; | |
171 | default: | |
3bb513c3 CH |
172 | assert(snode->nodetype != LYS_AUGMENT |
173 | && snode->nodetype != LYS_GROUPING | |
174 | && snode->nodetype != LYS_USES); | |
1c2facd1 RW |
175 | break; |
176 | } | |
177 | ||
e0ccfad2 RW |
178 | ret = (*cb)(snode, arg); |
179 | if (ret == YANG_ITER_STOP) | |
180 | return ret; | |
1c2facd1 RW |
181 | |
182 | next: | |
183 | /* | |
3bb513c3 | 184 | * YANG leafs and leaf-lists can't have child nodes. |
1c2facd1 | 185 | */ |
db452508 | 186 | if (CHECK_FLAG(snode->nodetype, LYS_LEAF | LYS_LEAFLIST)) |
e0ccfad2 | 187 | return YANG_ITER_CONTINUE; |
1c2facd1 | 188 | |
3bb513c3 | 189 | LY_LIST_FOR (lysc_node_child(snode), child) { |
8a923b48 RW |
190 | ret = yang_snodes_iterate_subtree(child, module, cb, flags, |
191 | arg); | |
e0ccfad2 RW |
192 | if (ret == YANG_ITER_STOP) |
193 | return ret; | |
1c2facd1 | 194 | } |
e0ccfad2 | 195 | return ret; |
1c2facd1 RW |
196 | } |
197 | ||
8d869d37 RW |
198 | int yang_snodes_iterate(const struct lys_module *module, yang_iterate_cb cb, |
199 | uint16_t flags, void *arg) | |
1c2facd1 | 200 | { |
9bde0b25 RW |
201 | const struct lys_module *module_iter; |
202 | uint32_t idx = 0; | |
e0ccfad2 | 203 | int ret = YANG_ITER_CONTINUE; |
1c2facd1 | 204 | |
9bde0b25 RW |
205 | idx = ly_ctx_internal_modules_count(ly_native_ctx); |
206 | while ((module_iter = ly_ctx_get_module_iter(ly_native_ctx, &idx))) { | |
3bb513c3 | 207 | struct lysc_node *snode; |
1c2facd1 | 208 | |
9bde0b25 RW |
209 | if (!module_iter->implemented) |
210 | continue; | |
211 | ||
3bb513c3 CH |
212 | LY_LIST_FOR (module_iter->compiled->data, snode) { |
213 | ret = yang_snodes_iterate_subtree(snode, module, cb, | |
214 | flags, arg); | |
215 | if (ret == YANG_ITER_STOP) | |
216 | return ret; | |
217 | } | |
218 | LY_LIST_FOR (&module_iter->compiled->rpcs->node, snode) { | |
219 | ret = yang_snodes_iterate_subtree(snode, module, cb, | |
220 | flags, arg); | |
221 | if (ret == YANG_ITER_STOP) | |
222 | return ret; | |
223 | } | |
224 | LY_LIST_FOR (&module_iter->compiled->notifs->node, snode) { | |
9bde0b25 RW |
225 | ret = yang_snodes_iterate_subtree(snode, module, cb, |
226 | flags, arg); | |
227 | if (ret == YANG_ITER_STOP) | |
228 | return ret; | |
229 | } | |
1c2facd1 | 230 | } |
e0ccfad2 RW |
231 | |
232 | return ret; | |
1c2facd1 RW |
233 | } |
234 | ||
3bb513c3 CH |
235 | void yang_snode_get_path(const struct lysc_node *snode, |
236 | enum yang_path_type type, char *xpath, | |
237 | size_t xpath_len) | |
1c2facd1 | 238 | { |
1c2facd1 RW |
239 | switch (type) { |
240 | case YANG_PATH_SCHEMA: | |
3bb513c3 | 241 | (void)lysc_path(snode, LYSC_PATH_LOG, xpath, xpath_len); |
1c2facd1 RW |
242 | break; |
243 | case YANG_PATH_DATA: | |
3bb513c3 | 244 | (void)lysc_path(snode, LYSC_PATH_DATA, xpath, xpath_len); |
1c2facd1 RW |
245 | break; |
246 | default: | |
247 | flog_err(EC_LIB_DEVELOPMENT, "%s: unknown yang path type: %u", | |
248 | __func__, type); | |
249 | exit(1); | |
250 | } | |
1c2facd1 RW |
251 | } |
252 | ||
9e0241c8 CH |
253 | struct lysc_node *yang_find_snode(struct ly_ctx *ly_ctx, const char *xpath, |
254 | uint32_t options) | |
255 | { | |
256 | struct lysc_node *snode; | |
257 | struct ly_set *set; | |
258 | LY_ERR err; | |
259 | ||
260 | err = lys_find_xpath(ly_native_ctx, NULL, xpath, options, &set); | |
261 | if (err || !set->count) | |
262 | return NULL; | |
263 | ||
264 | snode = set->snodes[0]; | |
265 | ly_set_free(set, NULL); | |
266 | ||
267 | return snode; | |
268 | } | |
269 | ||
3bb513c3 | 270 | struct lysc_node *yang_snode_real_parent(const struct lysc_node *snode) |
1c2facd1 | 271 | { |
3bb513c3 | 272 | struct lysc_node *parent = snode->parent; |
1c2facd1 RW |
273 | |
274 | while (parent) { | |
1c2facd1 RW |
275 | switch (parent->nodetype) { |
276 | case LYS_CONTAINER: | |
3bb513c3 | 277 | if (CHECK_FLAG(parent->flags, LYS_PRESENCE)) |
1c2facd1 RW |
278 | return parent; |
279 | break; | |
280 | case LYS_LIST: | |
281 | return parent; | |
282 | default: | |
283 | break; | |
284 | } | |
285 | parent = parent->parent; | |
286 | } | |
287 | ||
288 | return NULL; | |
289 | } | |
290 | ||
3bb513c3 | 291 | struct lysc_node *yang_snode_parent_list(const struct lysc_node *snode) |
1c2facd1 | 292 | { |
3bb513c3 | 293 | struct lysc_node *parent = snode->parent; |
1c2facd1 RW |
294 | |
295 | while (parent) { | |
296 | switch (parent->nodetype) { | |
297 | case LYS_LIST: | |
298 | return parent; | |
299 | default: | |
300 | break; | |
301 | } | |
302 | parent = parent->parent; | |
303 | } | |
304 | ||
305 | return NULL; | |
306 | } | |
307 | ||
3bb513c3 | 308 | bool yang_snode_is_typeless_data(const struct lysc_node *snode) |
1c2facd1 | 309 | { |
3bb513c3 | 310 | const struct lysc_node_leaf *sleaf; |
1c2facd1 RW |
311 | |
312 | switch (snode->nodetype) { | |
313 | case LYS_LEAF: | |
3bb513c3 CH |
314 | sleaf = (struct lysc_node_leaf *)snode; |
315 | if (sleaf->type->basetype == LY_TYPE_EMPTY) | |
1c2facd1 RW |
316 | return true; |
317 | return false; | |
318 | case LYS_LEAFLIST: | |
319 | return false; | |
320 | default: | |
321 | return true; | |
322 | } | |
323 | } | |
324 | ||
3bb513c3 | 325 | const char *yang_snode_get_default(const struct lysc_node *snode) |
1c2facd1 | 326 | { |
3bb513c3 | 327 | const struct lysc_node_leaf *sleaf; |
1c2facd1 RW |
328 | |
329 | switch (snode->nodetype) { | |
330 | case LYS_LEAF: | |
3bb513c3 CH |
331 | sleaf = (const struct lysc_node_leaf *)snode; |
332 | return sleaf->dflt ? lyd_value_get_canonical(sleaf->module->ctx, | |
333 | sleaf->dflt) | |
334 | : NULL; | |
1c2facd1 RW |
335 | case LYS_LEAFLIST: |
336 | /* TODO: check leaf-list default values */ | |
337 | return NULL; | |
338 | default: | |
339 | return NULL; | |
340 | } | |
341 | } | |
342 | ||
3bb513c3 | 343 | const struct lysc_type *yang_snode_get_type(const struct lysc_node *snode) |
1c2facd1 | 344 | { |
3bb513c3 CH |
345 | struct lysc_node_leaf *sleaf = (struct lysc_node_leaf *)snode; |
346 | struct lysc_type *type; | |
1c2facd1 | 347 | |
db452508 | 348 | if (!CHECK_FLAG(sleaf->nodetype, LYS_LEAF | LYS_LEAFLIST)) |
1c2facd1 RW |
349 | return NULL; |
350 | ||
3bb513c3 CH |
351 | type = sleaf->type; |
352 | while (type->basetype == LY_TYPE_LEAFREF) | |
353 | type = ((struct lysc_type_leafref *)type)->realtype; | |
1c2facd1 RW |
354 | |
355 | return type; | |
356 | } | |
357 | ||
3bb513c3 CH |
358 | unsigned int yang_snode_num_keys(const struct lysc_node *snode) |
359 | { | |
360 | const struct lysc_node_leaf *skey; | |
361 | uint count = 0; | |
362 | ||
363 | if (!CHECK_FLAG(snode->nodetype, LYS_LIST)) | |
364 | return 0; | |
365 | ||
366 | /* Walk list of children */ | |
367 | LY_FOR_KEYS (snode, skey) { | |
368 | count++; | |
369 | } | |
370 | return count; | |
371 | } | |
372 | ||
1c2facd1 RW |
373 | void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath, |
374 | size_t xpath_len) | |
375 | { | |
3bb513c3 | 376 | lyd_path(dnode, LYD_PATH_STD, xpath, xpath_len); |
1c2facd1 RW |
377 | } |
378 | ||
3f662078 RW |
379 | const char *yang_dnode_get_schema_name(const struct lyd_node *dnode, |
380 | const char *xpath_fmt, ...) | |
381 | { | |
382 | if (xpath_fmt) { | |
383 | va_list ap; | |
384 | char xpath[XPATH_MAXLEN]; | |
385 | ||
386 | va_start(ap, xpath_fmt); | |
387 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
388 | va_end(ap); | |
389 | ||
390 | dnode = yang_dnode_get(dnode, xpath); | |
391 | if (!dnode) { | |
392 | flog_err(EC_LIB_YANG_DNODE_NOT_FOUND, | |
393 | "%s: couldn't find %s", __func__, xpath); | |
394 | zlog_backtrace(LOG_ERR); | |
395 | abort(); | |
396 | } | |
397 | } | |
398 | ||
399 | return dnode->schema->name; | |
400 | } | |
401 | ||
3bb513c3 | 402 | struct lyd_node *yang_dnode_get(const struct lyd_node *dnode, const char *xpath) |
1c2facd1 | 403 | { |
3bb513c3 | 404 | struct ly_set *set = NULL; |
1c2facd1 RW |
405 | struct lyd_node *dnode_ret = NULL; |
406 | ||
3bb513c3 CH |
407 | /* |
408 | * XXX a lot of the code uses this for style I guess. It shouldn't, as | |
409 | * it adds to the xpath parsing complexity in libyang. | |
410 | */ | |
411 | if (xpath[0] == '.' && xpath[1] == '/') | |
412 | xpath += 2; | |
1c2facd1 | 413 | |
3bb513c3 | 414 | if (lyd_find_xpath(dnode, xpath, &set)) { |
74335ceb YR |
415 | /* |
416 | * Commenting out the below assert failure as it crashes mgmtd | |
417 | * when bad xpath is passed. | |
418 | * | |
419 | * assert(0); XXX replicates old libyang1 base code | |
420 | */ | |
3bb513c3 CH |
421 | goto exit; |
422 | } | |
423 | if (set->count == 0) | |
1c2facd1 RW |
424 | goto exit; |
425 | ||
3bb513c3 | 426 | if (set->count > 1) { |
1c2facd1 RW |
427 | flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND, |
428 | "%s: found %u elements (expected 0 or 1) [xpath %s]", | |
3bb513c3 | 429 | __func__, set->count, xpath); |
1c2facd1 RW |
430 | goto exit; |
431 | } | |
432 | ||
3bb513c3 | 433 | dnode_ret = set->dnodes[0]; |
1c2facd1 RW |
434 | |
435 | exit: | |
3bb513c3 | 436 | ly_set_free(set, NULL); |
1c2facd1 RW |
437 | |
438 | return dnode_ret; | |
439 | } | |
440 | ||
3bb513c3 CH |
441 | struct lyd_node *yang_dnode_getf(const struct lyd_node *dnode, |
442 | const char *xpath_fmt, ...) | |
1c2facd1 RW |
443 | { |
444 | va_list ap; | |
445 | char xpath[XPATH_MAXLEN]; | |
1c2facd1 RW |
446 | |
447 | va_start(ap, xpath_fmt); | |
448 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
449 | va_end(ap); | |
450 | ||
3bb513c3 CH |
451 | return yang_dnode_get(dnode, xpath); |
452 | } | |
453 | ||
454 | bool yang_dnode_exists(const struct lyd_node *dnode, const char *xpath) | |
455 | { | |
456 | struct ly_set *set = NULL; | |
457 | bool exists = false; | |
1c2facd1 | 458 | |
3bb513c3 CH |
459 | if (xpath[0] == '.' && xpath[1] == '/') |
460 | xpath += 2; | |
461 | if (lyd_find_xpath(dnode, xpath, &set)) | |
462 | return false; | |
463 | exists = set->count > 0; | |
464 | ly_set_free(set, NULL); | |
465 | return exists; | |
466 | } | |
467 | ||
468 | bool yang_dnode_existsf(const struct lyd_node *dnode, const char *xpath_fmt, | |
469 | ...) | |
470 | { | |
471 | va_list ap; | |
472 | char xpath[XPATH_MAXLEN]; | |
473 | ||
474 | va_start(ap, xpath_fmt); | |
475 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
476 | va_end(ap); | |
477 | ||
478 | return yang_dnode_exists(dnode, xpath); | |
1c2facd1 RW |
479 | } |
480 | ||
7b611145 RW |
481 | void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg, |
482 | const struct lyd_node *dnode, const char *xpath_fmt, | |
483 | ...) | |
484 | { | |
485 | va_list ap; | |
486 | char xpath[XPATH_MAXLEN]; | |
487 | struct ly_set *set; | |
488 | ||
489 | va_start(ap, xpath_fmt); | |
490 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
491 | va_end(ap); | |
492 | ||
3bb513c3 CH |
493 | if (lyd_find_xpath(dnode, xpath, &set)) { |
494 | assert(0); /* XXX libyang2: ly1 code asserted success */ | |
495 | return; | |
496 | } | |
497 | for (unsigned int i = 0; i < set->count; i++) { | |
7b611145 RW |
498 | int ret; |
499 | ||
3bb513c3 | 500 | ret = (*cb)(set->dnodes[i], arg); |
7b611145 | 501 | if (ret == YANG_ITER_STOP) |
4e32d023 | 502 | break; |
7b611145 RW |
503 | } |
504 | ||
3bb513c3 | 505 | ly_set_free(set, NULL); |
7b611145 RW |
506 | } |
507 | ||
3bb513c3 | 508 | bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath) |
1c2facd1 | 509 | { |
3bb513c3 CH |
510 | const struct lysc_node *snode; |
511 | struct lysc_node_leaf *sleaf; | |
1c2facd1 | 512 | |
3bb513c3 | 513 | if (xpath) |
1c2facd1 | 514 | dnode = yang_dnode_get(dnode, xpath); |
1c2facd1 RW |
515 | |
516 | assert(dnode); | |
517 | snode = dnode->schema; | |
518 | switch (snode->nodetype) { | |
519 | case LYS_LEAF: | |
3bb513c3 CH |
520 | sleaf = (struct lysc_node_leaf *)snode; |
521 | if (sleaf->type->basetype == LY_TYPE_EMPTY) | |
1c2facd1 | 522 | return false; |
3bb513c3 | 523 | return lyd_is_default(dnode); |
1c2facd1 RW |
524 | case LYS_LEAFLIST: |
525 | /* TODO: check leaf-list default values */ | |
526 | return false; | |
527 | case LYS_CONTAINER: | |
3bb513c3 | 528 | if (CHECK_FLAG(snode->flags, LYS_PRESENCE)) |
1c2facd1 RW |
529 | return false; |
530 | return true; | |
531 | default: | |
532 | return false; | |
533 | } | |
534 | } | |
535 | ||
3bb513c3 CH |
536 | bool yang_dnode_is_defaultf(const struct lyd_node *dnode, const char *xpath_fmt, |
537 | ...) | |
1c2facd1 | 538 | { |
3bb513c3 | 539 | if (!xpath_fmt) |
1c2facd1 | 540 | return yang_dnode_is_default(dnode, NULL); |
3bb513c3 CH |
541 | else { |
542 | va_list ap; | |
543 | char xpath[XPATH_MAXLEN]; | |
544 | ||
545 | va_start(ap, xpath_fmt); | |
546 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
547 | va_end(ap); | |
548 | ||
549 | return yang_dnode_is_default(dnode, xpath); | |
550 | } | |
551 | } | |
552 | ||
553 | bool yang_dnode_is_default_recursive(const struct lyd_node *dnode) | |
554 | { | |
555 | struct lyd_node *root, *dnode_iter; | |
1c2facd1 RW |
556 | |
557 | if (!yang_dnode_is_default(dnode, NULL)) | |
558 | return false; | |
559 | ||
3bb513c3 CH |
560 | if (CHECK_FLAG(dnode->schema->nodetype, LYS_LEAF | LYS_LEAFLIST)) |
561 | return true; | |
562 | ||
563 | LY_LIST_FOR (lyd_child(dnode), root) { | |
564 | LYD_TREE_DFS_BEGIN (root, dnode_iter) { | |
1c2facd1 RW |
565 | if (!yang_dnode_is_default(dnode_iter, NULL)) |
566 | return false; | |
567 | ||
3bb513c3 | 568 | LYD_TREE_DFS_END(root, dnode_iter); |
1c2facd1 RW |
569 | } |
570 | } | |
571 | ||
572 | return true; | |
573 | } | |
574 | ||
575 | void yang_dnode_change_leaf(struct lyd_node *dnode, const char *value) | |
576 | { | |
577 | assert(dnode->schema->nodetype == LYS_LEAF); | |
3bb513c3 | 578 | lyd_change_term(dnode, value); |
1c2facd1 RW |
579 | } |
580 | ||
5e02643a | 581 | struct lyd_node *yang_dnode_new(struct ly_ctx *ly_ctx, bool config_only) |
1c2facd1 | 582 | { |
3bb513c3 CH |
583 | struct lyd_node *dnode = NULL; |
584 | int options = config_only ? LYD_VALIDATE_NO_STATE : 0; | |
1c2facd1 | 585 | |
3bb513c3 | 586 | if (lyd_validate_all(&dnode, ly_ctx, options, NULL) != 0) { |
1c2facd1 RW |
587 | /* Should never happen. */ |
588 | flog_err(EC_LIB_LIBYANG, "%s: lyd_validate() failed", __func__); | |
589 | exit(1); | |
590 | } | |
591 | ||
592 | return dnode; | |
593 | } | |
594 | ||
595 | struct lyd_node *yang_dnode_dup(const struct lyd_node *dnode) | |
596 | { | |
3bb513c3 CH |
597 | struct lyd_node *dup = NULL; |
598 | LY_ERR err; | |
599 | err = lyd_dup_siblings(dnode, NULL, LYD_DUP_RECURSIVE, &dup); | |
600 | assert(!err); | |
601 | return dup; | |
1c2facd1 RW |
602 | } |
603 | ||
604 | void yang_dnode_free(struct lyd_node *dnode) | |
605 | { | |
e5dc8a44 | 606 | while (dnode->parent) |
3bb513c3 CH |
607 | dnode = lyd_parent(dnode); |
608 | lyd_free_all(dnode); | |
1c2facd1 RW |
609 | } |
610 | ||
611 | struct yang_data *yang_data_new(const char *xpath, const char *value) | |
612 | { | |
1c2facd1 RW |
613 | struct yang_data *data; |
614 | ||
1c2facd1 RW |
615 | data = XCALLOC(MTYPE_YANG_DATA, sizeof(*data)); |
616 | strlcpy(data->xpath, xpath, sizeof(data->xpath)); | |
1c2facd1 RW |
617 | if (value) |
618 | data->value = strdup(value); | |
619 | ||
620 | return data; | |
621 | } | |
622 | ||
623 | void yang_data_free(struct yang_data *data) | |
624 | { | |
625 | if (data->value) | |
626 | free(data->value); | |
627 | XFREE(MTYPE_YANG_DATA, data); | |
628 | } | |
629 | ||
630 | struct list *yang_data_list_new(void) | |
631 | { | |
632 | struct list *list; | |
633 | ||
634 | list = list_new(); | |
635 | list->del = (void (*)(void *))yang_data_free; | |
636 | ||
637 | return list; | |
638 | } | |
639 | ||
fcb7bffd RW |
640 | struct yang_data *yang_data_list_find(const struct list *list, |
641 | const char *xpath_fmt, ...) | |
642 | { | |
643 | char xpath[XPATH_MAXLEN]; | |
644 | struct yang_data *data; | |
645 | struct listnode *node; | |
646 | va_list ap; | |
647 | ||
648 | va_start(ap, xpath_fmt); | |
649 | vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); | |
650 | va_end(ap); | |
651 | ||
652 | for (ALL_LIST_ELEMENTS_RO(list, node, data)) | |
653 | if (strmatch(data->xpath, xpath)) | |
654 | return data; | |
655 | ||
656 | return NULL; | |
657 | } | |
658 | ||
1c2facd1 RW |
659 | /* Make libyang log its errors using FRR logging infrastructure. */ |
660 | static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path) | |
661 | { | |
96679938 | 662 | int priority = LOG_ERR; |
1c2facd1 RW |
663 | |
664 | switch (level) { | |
665 | case LY_LLERR: | |
666 | priority = LOG_ERR; | |
667 | break; | |
668 | case LY_LLWRN: | |
669 | priority = LOG_WARNING; | |
670 | break; | |
671 | case LY_LLVRB: | |
96679938 | 672 | case LY_LLDBG: |
1c2facd1 RW |
673 | priority = LOG_DEBUG; |
674 | break; | |
1c2facd1 RW |
675 | } |
676 | ||
677 | if (path) | |
678 | zlog(priority, "libyang: %s (%s)", msg, path); | |
679 | else | |
680 | zlog(priority, "libyang: %s", msg); | |
681 | } | |
682 | ||
df5eda3d RW |
683 | const char *yang_print_errors(struct ly_ctx *ly_ctx, char *buf, size_t buf_len) |
684 | { | |
685 | struct ly_err_item *ei; | |
686 | const char *path; | |
687 | ||
688 | ei = ly_err_first(ly_ctx); | |
689 | if (!ei) | |
690 | return ""; | |
691 | ||
692 | strlcpy(buf, "YANG error(s):\n", buf_len); | |
693 | for (; ei; ei = ei->next) { | |
694 | strlcat(buf, " ", buf_len); | |
695 | strlcat(buf, ei->msg, buf_len); | |
696 | strlcat(buf, "\n", buf_len); | |
697 | } | |
698 | ||
699 | path = ly_errpath(ly_ctx); | |
700 | if (path) { | |
701 | strlcat(buf, " YANG path: ", buf_len); | |
702 | strlcat(buf, path, buf_len); | |
703 | strlcat(buf, "\n", buf_len); | |
704 | } | |
705 | ||
706 | ly_err_clean(ly_ctx, NULL); | |
707 | ||
708 | return buf; | |
709 | } | |
710 | ||
62ae9ade RW |
711 | void yang_debugging_set(bool enable) |
712 | { | |
713 | if (enable) { | |
3bb513c3 CH |
714 | ly_log_level(LY_LLDBG); |
715 | ly_log_dbg_groups(0xFF); | |
62ae9ade | 716 | } else { |
3bb513c3 CH |
717 | ly_log_level(LY_LLERR); |
718 | ly_log_dbg_groups(0); | |
62ae9ade RW |
719 | } |
720 | } | |
721 | ||
3bb513c3 | 722 | struct ly_ctx *yang_ctx_new_setup(bool embedded_modules, bool explicit_compile) |
591f57cf | 723 | { |
3bb513c3 | 724 | struct ly_ctx *ctx = NULL; |
591f57cf | 725 | const char *yang_models_path = YANG_MODELS_PATH; |
3bb513c3 | 726 | LY_ERR err; |
591f57cf DL |
727 | |
728 | if (access(yang_models_path, R_OK | X_OK)) { | |
729 | yang_models_path = NULL; | |
730 | if (errno == ENOENT) | |
731 | zlog_info("yang model directory \"%s\" does not exist", | |
732 | YANG_MODELS_PATH); | |
733 | else | |
734 | flog_err_sys(EC_LIB_LIBYANG, | |
735 | "cannot access yang model directory \"%s\"", | |
736 | YANG_MODELS_PATH); | |
737 | } | |
738 | ||
3bb513c3 CH |
739 | uint options = LY_CTX_NO_YANGLIBRARY | LY_CTX_DISABLE_SEARCHDIR_CWD; |
740 | if (explicit_compile) | |
741 | options |= LY_CTX_EXPLICIT_COMPILE; | |
742 | err = ly_ctx_new(yang_models_path, options, &ctx); | |
743 | if (err) | |
591f57cf | 744 | return NULL; |
b90204a8 RW |
745 | |
746 | if (embedded_modules) | |
747 | ly_ctx_set_module_imp_clb(ctx, yang_module_imp_clb, NULL); | |
748 | ||
591f57cf DL |
749 | return ctx; |
750 | } | |
751 | ||
3bb513c3 | 752 | void yang_init(bool embedded_modules, bool defer_compile) |
1c2facd1 | 753 | { |
1c2facd1 RW |
754 | /* Initialize libyang global parameters that affect all containers. */ |
755 | ly_set_log_clb(ly_log_cb, 1); | |
756 | ly_log_options(LY_LOLOG | LY_LOSTORE); | |
757 | ||
758 | /* Initialize libyang container for native models. */ | |
3bb513c3 | 759 | ly_native_ctx = yang_ctx_new_setup(embedded_modules, defer_compile); |
1c2facd1 RW |
760 | if (!ly_native_ctx) { |
761 | flog_err(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__); | |
762 | exit(1); | |
763 | } | |
1c2facd1 | 764 | |
1c2facd1 RW |
765 | yang_translator_init(); |
766 | } | |
767 | ||
3bb513c3 CH |
768 | void yang_init_loading_complete(void) |
769 | { | |
770 | /* Compile everything */ | |
771 | if (ly_ctx_compile(ly_native_ctx) != LY_SUCCESS) { | |
772 | flog_err(EC_LIB_YANG_MODULE_LOAD, | |
773 | "%s: failed to compile loaded modules: %s", __func__, | |
774 | ly_errmsg(ly_native_ctx)); | |
775 | exit(1); | |
776 | } | |
777 | } | |
778 | ||
1c2facd1 RW |
779 | void yang_terminate(void) |
780 | { | |
781 | struct yang_module *module; | |
782 | ||
783 | yang_translator_terminate(); | |
784 | ||
785 | while (!RB_EMPTY(yang_modules, &yang_modules)) { | |
786 | module = RB_ROOT(yang_modules, &yang_modules); | |
787 | ||
788 | /* | |
789 | * We shouldn't call ly_ctx_remove_module() here because this | |
790 | * function also removes other modules that depend on it. | |
791 | * | |
792 | * ly_ctx_destroy() will release all memory for us. | |
793 | */ | |
794 | RB_REMOVE(yang_modules, &yang_modules, module); | |
795 | XFREE(MTYPE_YANG_MODULE, module); | |
796 | } | |
797 | ||
3bb513c3 | 798 | ly_ctx_destroy(ly_native_ctx); |
1c2facd1 | 799 | } |
27802d3f | 800 | |
801 | const struct lyd_node *yang_dnode_get_parent(const struct lyd_node *dnode, | |
802 | const char *name) | |
803 | { | |
804 | const struct lyd_node *orig_dnode = dnode; | |
805 | ||
806 | while (orig_dnode) { | |
807 | switch (orig_dnode->schema->nodetype) { | |
808 | case LYS_LIST: | |
809 | case LYS_CONTAINER: | |
810 | if (!strcmp(orig_dnode->schema->name, name)) | |
811 | return orig_dnode; | |
812 | break; | |
813 | default: | |
814 | break; | |
815 | } | |
816 | ||
3bb513c3 | 817 | orig_dnode = lyd_parent(orig_dnode); |
27802d3f | 818 | } |
819 | ||
820 | return NULL; | |
821 | } | |
822 | ||
cf740d2e | 823 | bool yang_is_last_list_dnode(const struct lyd_node *dnode) |
27802d3f | 824 | { |
825 | return (((dnode->next == NULL) | |
826 | || (dnode->next | |
827 | && (strcmp(dnode->next->schema->name, dnode->schema->name) | |
828 | != 0))) | |
829 | && dnode->prev | |
830 | && ((dnode->prev == dnode) | |
831 | || (strcmp(dnode->prev->schema->name, dnode->schema->name) | |
832 | != 0))); | |
833 | } | |
834 | ||
cf740d2e | 835 | bool yang_is_last_level_dnode(const struct lyd_node *dnode) |
27802d3f | 836 | { |
837 | const struct lyd_node *parent; | |
27802d3f | 838 | const struct lyd_node *key_leaf; |
839 | uint8_t keys_size; | |
840 | ||
841 | switch (dnode->schema->nodetype) { | |
842 | case LYS_LIST: | |
843 | assert(dnode->parent); | |
3bb513c3 CH |
844 | parent = lyd_parent(dnode); |
845 | uint snode_num_keys = yang_snode_num_keys(parent->schema); | |
846 | /* XXX libyang2: q: really don't understand this code. */ | |
27802d3f | 847 | key_leaf = dnode->prev; |
3bb513c3 | 848 | for (keys_size = 1; keys_size < snode_num_keys; keys_size++) |
27802d3f | 849 | key_leaf = key_leaf->prev; |
850 | if (key_leaf->prev == dnode) | |
851 | return true; | |
852 | break; | |
853 | case LYS_CONTAINER: | |
854 | return true; | |
855 | default: | |
856 | break; | |
857 | } | |
858 | ||
859 | return false; | |
860 | } | |
861 | ||
27802d3f | 862 | const struct lyd_node * |
863 | yang_get_subtree_with_no_sibling(const struct lyd_node *dnode) | |
864 | { | |
865 | bool parent = true; | |
866 | const struct lyd_node *node; | |
27802d3f | 867 | |
868 | node = dnode; | |
869 | if (node->schema->nodetype != LYS_LIST) | |
870 | return node; | |
871 | ||
872 | while (parent) { | |
873 | switch (node->schema->nodetype) { | |
874 | case LYS_CONTAINER: | |
3bb513c3 | 875 | if (!CHECK_FLAG(node->schema->flags, LYS_PRESENCE)) { |
27802d3f | 876 | if (node->parent |
877 | && (node->parent->schema->module | |
878 | == dnode->schema->module)) | |
3bb513c3 | 879 | node = lyd_parent(node); |
27802d3f | 880 | else |
881 | parent = false; | |
882 | } else | |
883 | parent = false; | |
884 | break; | |
885 | case LYS_LIST: | |
886 | if (yang_is_last_list_dnode(node) | |
887 | && yang_is_last_level_dnode(node)) { | |
888 | if (node->parent | |
889 | && (node->parent->schema->module | |
890 | == dnode->schema->module)) | |
3bb513c3 | 891 | node = lyd_parent(node); |
27802d3f | 892 | else |
893 | parent = false; | |
894 | } else | |
895 | parent = false; | |
896 | break; | |
897 | default: | |
898 | parent = false; | |
899 | break; | |
900 | } | |
901 | } | |
902 | return node; | |
903 | } | |
904 | ||
905 | uint32_t yang_get_list_pos(const struct lyd_node *node) | |
906 | { | |
907 | return lyd_list_pos(node); | |
908 | } | |
909 | ||
910 | uint32_t yang_get_list_elements_count(const struct lyd_node *node) | |
911 | { | |
912 | unsigned int count; | |
3bb513c3 | 913 | const struct lysc_node *schema; |
27802d3f | 914 | |
915 | if (!node | |
916 | || ((node->schema->nodetype != LYS_LIST) | |
917 | && (node->schema->nodetype != LYS_LEAFLIST))) { | |
918 | return 0; | |
919 | } | |
920 | ||
921 | schema = node->schema; | |
922 | count = 0; | |
923 | do { | |
924 | if (node->schema == schema) | |
925 | ++count; | |
926 | node = node->next; | |
927 | } while (node); | |
928 | return count; | |
929 | } |