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