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