]> git.proxmox.com Git - mirror_frr.git/blob - lib/yang.c
*: auto-convert to SPDX License IDs
[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-route-map",
79 "frr-nexthop",
80 "frr-ripd",
81 "frr-ripngd",
82 "frr-isisd",
83 "frr-vrrpd",
84 "frr-zebra",
85 "frr-pathd",
86 };
87 /* clang-format on */
88
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
104 module_info =
105 ly_ctx_load_module(ly_native_ctx, module_name, NULL, NULL);
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
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
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
141 int yang_snodes_iterate_subtree(const struct lysc_node *snode,
142 const struct lys_module *module,
143 yang_iterate_cb cb, uint16_t flags, void *arg)
144 {
145 const struct lysc_node *child;
146 int ret = YANG_ITER_CONTINUE;
147
148 if (module && snode->module != module)
149 goto next;
150
151 switch (snode->nodetype) {
152 case LYS_CONTAINER:
153 if (CHECK_FLAG(flags, YANG_ITER_FILTER_NPCONTAINERS)) {
154 if (!CHECK_FLAG(snode->flags, LYS_PRESENCE))
155 goto next;
156 }
157 break;
158 case LYS_LEAF:
159 if (CHECK_FLAG(flags, YANG_ITER_FILTER_LIST_KEYS)) {
160 /* Ignore list keys. */
161 if (lysc_is_key(snode))
162 goto next;
163 }
164 break;
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:
171 assert(snode->nodetype != LYS_AUGMENT
172 && snode->nodetype != LYS_GROUPING
173 && snode->nodetype != LYS_USES);
174 break;
175 }
176
177 ret = (*cb)(snode, arg);
178 if (ret == YANG_ITER_STOP)
179 return ret;
180
181 next:
182 /*
183 * YANG leafs and leaf-lists can't have child nodes.
184 */
185 if (CHECK_FLAG(snode->nodetype, LYS_LEAF | LYS_LEAFLIST))
186 return YANG_ITER_CONTINUE;
187
188 LY_LIST_FOR (lysc_node_child(snode), child) {
189 ret = yang_snodes_iterate_subtree(child, module, cb, flags,
190 arg);
191 if (ret == YANG_ITER_STOP)
192 return ret;
193 }
194 return ret;
195 }
196
197 int yang_snodes_iterate(const struct lys_module *module, yang_iterate_cb cb,
198 uint16_t flags, void *arg)
199 {
200 const struct lys_module *module_iter;
201 uint32_t idx = 0;
202 int ret = YANG_ITER_CONTINUE;
203
204 idx = ly_ctx_internal_modules_count(ly_native_ctx);
205 while ((module_iter = ly_ctx_get_module_iter(ly_native_ctx, &idx))) {
206 struct lysc_node *snode;
207
208 if (!module_iter->implemented)
209 continue;
210
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) {
224 ret = yang_snodes_iterate_subtree(snode, module, cb,
225 flags, arg);
226 if (ret == YANG_ITER_STOP)
227 return ret;
228 }
229 }
230
231 return ret;
232 }
233
234 void yang_snode_get_path(const struct lysc_node *snode,
235 enum yang_path_type type, char *xpath,
236 size_t xpath_len)
237 {
238 switch (type) {
239 case YANG_PATH_SCHEMA:
240 (void)lysc_path(snode, LYSC_PATH_LOG, xpath, xpath_len);
241 break;
242 case YANG_PATH_DATA:
243 (void)lysc_path(snode, LYSC_PATH_DATA, xpath, xpath_len);
244 break;
245 default:
246 flog_err(EC_LIB_DEVELOPMENT, "%s: unknown yang path type: %u",
247 __func__, type);
248 exit(1);
249 }
250 }
251
252 struct lysc_node *yang_snode_real_parent(const struct lysc_node *snode)
253 {
254 struct lysc_node *parent = snode->parent;
255
256 while (parent) {
257 switch (parent->nodetype) {
258 case LYS_CONTAINER:
259 if (CHECK_FLAG(parent->flags, LYS_PRESENCE))
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
273 struct lysc_node *yang_snode_parent_list(const struct lysc_node *snode)
274 {
275 struct lysc_node *parent = snode->parent;
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
290 bool yang_snode_is_typeless_data(const struct lysc_node *snode)
291 {
292 const struct lysc_node_leaf *sleaf;
293
294 switch (snode->nodetype) {
295 case LYS_LEAF:
296 sleaf = (struct lysc_node_leaf *)snode;
297 if (sleaf->type->basetype == LY_TYPE_EMPTY)
298 return true;
299 return false;
300 case LYS_LEAFLIST:
301 return false;
302 default:
303 return true;
304 }
305 }
306
307 const char *yang_snode_get_default(const struct lysc_node *snode)
308 {
309 const struct lysc_node_leaf *sleaf;
310
311 switch (snode->nodetype) {
312 case LYS_LEAF:
313 sleaf = (const struct lysc_node_leaf *)snode;
314 return sleaf->dflt ? lyd_value_get_canonical(sleaf->module->ctx,
315 sleaf->dflt)
316 : NULL;
317 case LYS_LEAFLIST:
318 /* TODO: check leaf-list default values */
319 return NULL;
320 default:
321 return NULL;
322 }
323 }
324
325 const struct lysc_type *yang_snode_get_type(const struct lysc_node *snode)
326 {
327 struct lysc_node_leaf *sleaf = (struct lysc_node_leaf *)snode;
328 struct lysc_type *type;
329
330 if (!CHECK_FLAG(sleaf->nodetype, LYS_LEAF | LYS_LEAFLIST))
331 return NULL;
332
333 type = sleaf->type;
334 while (type->basetype == LY_TYPE_LEAFREF)
335 type = ((struct lysc_type_leafref *)type)->realtype;
336
337 return type;
338 }
339
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
355 void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath,
356 size_t xpath_len)
357 {
358 lyd_path(dnode, LYD_PATH_STD, xpath, xpath_len);
359 }
360
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
384 struct lyd_node *yang_dnode_get(const struct lyd_node *dnode, const char *xpath)
385 {
386 struct ly_set *set = NULL;
387 struct lyd_node *dnode_ret = NULL;
388
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;
395
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)
401 goto exit;
402
403 if (set->count > 1) {
404 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND,
405 "%s: found %u elements (expected 0 or 1) [xpath %s]",
406 __func__, set->count, xpath);
407 goto exit;
408 }
409
410 dnode_ret = set->dnodes[0];
411
412 exit:
413 ly_set_free(set, NULL);
414
415 return dnode_ret;
416 }
417
418 struct lyd_node *yang_dnode_getf(const struct lyd_node *dnode,
419 const char *xpath_fmt, ...)
420 {
421 va_list ap;
422 char xpath[XPATH_MAXLEN];
423
424 va_start(ap, xpath_fmt);
425 vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap);
426 va_end(ap);
427
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;
435
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);
456 }
457
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
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++) {
475 int ret;
476
477 ret = (*cb)(set->dnodes[i], arg);
478 if (ret == YANG_ITER_STOP)
479 break;
480 }
481
482 ly_set_free(set, NULL);
483 }
484
485 bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath)
486 {
487 const struct lysc_node *snode;
488 struct lysc_node_leaf *sleaf;
489
490 if (xpath)
491 dnode = yang_dnode_get(dnode, xpath);
492
493 assert(dnode);
494 snode = dnode->schema;
495 switch (snode->nodetype) {
496 case LYS_LEAF:
497 sleaf = (struct lysc_node_leaf *)snode;
498 if (sleaf->type->basetype == LY_TYPE_EMPTY)
499 return false;
500 return lyd_is_default(dnode);
501 case LYS_LEAFLIST:
502 /* TODO: check leaf-list default values */
503 return false;
504 case LYS_CONTAINER:
505 if (CHECK_FLAG(snode->flags, LYS_PRESENCE))
506 return false;
507 return true;
508 default:
509 return false;
510 }
511 }
512
513 bool yang_dnode_is_defaultf(const struct lyd_node *dnode, const char *xpath_fmt,
514 ...)
515 {
516 if (!xpath_fmt)
517 return yang_dnode_is_default(dnode, NULL);
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;
533
534 if (!yang_dnode_is_default(dnode, NULL))
535 return false;
536
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) {
542 if (!yang_dnode_is_default(dnode_iter, NULL))
543 return false;
544
545 LYD_TREE_DFS_END(root, dnode_iter);
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);
555 lyd_change_term(dnode, value);
556 }
557
558 struct lyd_node *yang_dnode_new(struct ly_ctx *ly_ctx, bool config_only)
559 {
560 struct lyd_node *dnode = NULL;
561 int options = config_only ? LYD_VALIDATE_NO_STATE : 0;
562
563 if (lyd_validate_all(&dnode, ly_ctx, options, NULL) != 0) {
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 {
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;
579 }
580
581 void yang_dnode_free(struct lyd_node *dnode)
582 {
583 while (dnode->parent)
584 dnode = lyd_parent(dnode);
585 lyd_free_all(dnode);
586 }
587
588 struct yang_data *yang_data_new(const char *xpath, const char *value)
589 {
590 struct yang_data *data;
591
592 data = XCALLOC(MTYPE_YANG_DATA, sizeof(*data));
593 strlcpy(data->xpath, xpath, sizeof(data->xpath));
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
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
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 {
639 int priority = LOG_ERR;
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:
649 case LY_LLDBG:
650 priority = LOG_DEBUG;
651 break;
652 }
653
654 if (path)
655 zlog(priority, "libyang: %s (%s)", msg, path);
656 else
657 zlog(priority, "libyang: %s", msg);
658 }
659
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
688 void yang_debugging_set(bool enable)
689 {
690 if (enable) {
691 ly_log_level(LY_LLDBG);
692 ly_log_dbg_groups(0xFF);
693 } else {
694 ly_log_level(LY_LLERR);
695 ly_log_dbg_groups(0);
696 }
697 }
698
699 struct ly_ctx *yang_ctx_new_setup(bool embedded_modules, bool explicit_compile)
700 {
701 struct ly_ctx *ctx = NULL;
702 const char *yang_models_path = YANG_MODELS_PATH;
703 LY_ERR err;
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
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)
721 return NULL;
722
723 if (embedded_modules)
724 ly_ctx_set_module_imp_clb(ctx, yang_module_imp_clb, NULL);
725
726 return ctx;
727 }
728
729 void yang_init(bool embedded_modules, bool defer_compile)
730 {
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. */
736 ly_native_ctx = yang_ctx_new_setup(embedded_modules, defer_compile);
737 if (!ly_native_ctx) {
738 flog_err(EC_LIB_LIBYANG, "%s: ly_ctx_new() failed", __func__);
739 exit(1);
740 }
741
742 yang_translator_init();
743 }
744
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
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
775 ly_ctx_destroy(ly_native_ctx);
776 }
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
794 orig_dnode = lyd_parent(orig_dnode);
795 }
796
797 return NULL;
798 }
799
800 bool yang_is_last_list_dnode(const struct lyd_node *dnode)
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
812 bool yang_is_last_level_dnode(const struct lyd_node *dnode)
813 {
814 const struct lyd_node *parent;
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);
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. */
824 key_leaf = dnode->prev;
825 for (keys_size = 1; keys_size < snode_num_keys; keys_size++)
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
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;
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:
852 if (!CHECK_FLAG(node->schema->flags, LYS_PRESENCE)) {
853 if (node->parent
854 && (node->parent->schema->module
855 == dnode->schema->module))
856 node = lyd_parent(node);
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))
868 node = lyd_parent(node);
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;
890 const struct lysc_node *schema;
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 }