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