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