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