1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 NetDEF, Inc.
10 #include "lib_errors.h"
12 #include "yang_translator.h"
13 #include "northbound.h"
15 DEFINE_MTYPE_STATIC(LIB
, YANG_MODULE
, "YANG module");
16 DEFINE_MTYPE_STATIC(LIB
, YANG_DATA
, "YANG data structure");
18 /* libyang container. */
19 struct ly_ctx
*ly_native_ctx
;
21 static struct yang_module_embed
*embeds
, **embedupd
= &embeds
;
23 void yang_module_embed(struct yang_module_embed
*embed
)
27 embedupd
= &embed
->next
;
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
,
34 const char **module_data
,
35 void (**free_module_data
)(void *, void *))
37 struct yang_module_embed
*e
;
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 */
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
))
49 if (submod_rev
&& strcmp(e
->sub_mod_rev
, submod_rev
))
52 if (strcmp(e
->mod_name
, mod_name
))
55 if (mod_rev
&& strcmp(e
->mod_rev
, mod_rev
))
60 *module_data
= e
->data
;
64 /* We get here for indirect modules like ietf-inet-types */
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
: "*");
73 /* clang-format off */
74 static const char *const frr_native_modules
[] = {
89 /* Generate the yang_modules tree. */
90 static inline int yang_module_compare(const struct yang_module
*a
,
91 const struct yang_module
*b
)
93 return strcmp(a
->name
, b
->name
);
95 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
97 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
99 struct yang_module
*yang_module_load(const char *module_name
)
101 struct yang_module
*module
;
102 const struct lys_module
*module_info
;
105 ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
, NULL
);
107 flog_err(EC_LIB_YANG_MODULE_LOAD
,
108 "%s: failed to load data model: %s", __func__
,
113 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
114 module
->name
= module_name
;
115 module
->info
= module_info
;
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__
,
127 void yang_module_load_all(void)
129 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
130 yang_module_load(frr_native_modules
[i
]);
133 struct yang_module
*yang_module_find(const char *module_name
)
135 struct yang_module s
;
137 s
.name
= module_name
;
138 return RB_FIND(yang_modules
, &yang_modules
, &s
);
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
)
145 const struct lysc_node
*child
;
146 int ret
= YANG_ITER_CONTINUE
;
148 if (module
&& snode
->module
!= module
)
151 switch (snode
->nodetype
) {
153 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
154 if (!CHECK_FLAG(snode
->flags
, LYS_PRESENCE
))
159 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
160 /* Ignore list keys. */
161 if (lysc_is_key(snode
))
167 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
171 assert(snode
->nodetype
!= LYS_AUGMENT
172 && snode
->nodetype
!= LYS_GROUPING
173 && snode
->nodetype
!= LYS_USES
);
177 ret
= (*cb
)(snode
, arg
);
178 if (ret
== YANG_ITER_STOP
)
183 * YANG leafs and leaf-lists can't have child nodes.
185 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
186 return YANG_ITER_CONTINUE
;
188 LY_LIST_FOR (lysc_node_child(snode
), child
) {
189 ret
= yang_snodes_iterate_subtree(child
, module
, cb
, flags
,
191 if (ret
== YANG_ITER_STOP
)
197 int yang_snodes_iterate(const struct lys_module
*module
, yang_iterate_cb cb
,
198 uint16_t flags
, void *arg
)
200 const struct lys_module
*module_iter
;
202 int ret
= YANG_ITER_CONTINUE
;
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
;
208 if (!module_iter
->implemented
)
211 LY_LIST_FOR (module_iter
->compiled
->data
, snode
) {
212 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
214 if (ret
== YANG_ITER_STOP
)
217 LY_LIST_FOR (&module_iter
->compiled
->rpcs
->node
, snode
) {
218 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
220 if (ret
== YANG_ITER_STOP
)
223 LY_LIST_FOR (&module_iter
->compiled
->notifs
->node
, snode
) {
224 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
226 if (ret
== YANG_ITER_STOP
)
234 void yang_snode_get_path(const struct lysc_node
*snode
,
235 enum yang_path_type type
, char *xpath
,
239 case YANG_PATH_SCHEMA
:
240 (void)lysc_path(snode
, LYSC_PATH_LOG
, xpath
, xpath_len
);
243 (void)lysc_path(snode
, LYSC_PATH_DATA
, xpath
, xpath_len
);
246 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
252 struct lysc_node
*yang_snode_real_parent(const struct lysc_node
*snode
)
254 struct lysc_node
*parent
= snode
->parent
;
257 switch (parent
->nodetype
) {
259 if (CHECK_FLAG(parent
->flags
, LYS_PRESENCE
))
267 parent
= parent
->parent
;
273 struct lysc_node
*yang_snode_parent_list(const struct lysc_node
*snode
)
275 struct lysc_node
*parent
= snode
->parent
;
278 switch (parent
->nodetype
) {
284 parent
= parent
->parent
;
290 bool yang_snode_is_typeless_data(const struct lysc_node
*snode
)
292 const struct lysc_node_leaf
*sleaf
;
294 switch (snode
->nodetype
) {
296 sleaf
= (struct lysc_node_leaf
*)snode
;
297 if (sleaf
->type
->basetype
== LY_TYPE_EMPTY
)
307 const char *yang_snode_get_default(const struct lysc_node
*snode
)
309 const struct lysc_node_leaf
*sleaf
;
311 switch (snode
->nodetype
) {
313 sleaf
= (const struct lysc_node_leaf
*)snode
;
314 return sleaf
->dflt
? lyd_value_get_canonical(sleaf
->module
->ctx
,
318 /* TODO: check leaf-list default values */
325 const struct lysc_type
*yang_snode_get_type(const struct lysc_node
*snode
)
327 struct lysc_node_leaf
*sleaf
= (struct lysc_node_leaf
*)snode
;
328 struct lysc_type
*type
;
330 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
334 while (type
->basetype
== LY_TYPE_LEAFREF
)
335 type
= ((struct lysc_type_leafref
*)type
)->realtype
;
340 unsigned int yang_snode_num_keys(const struct lysc_node
*snode
)
342 const struct lysc_node_leaf
*skey
;
345 if (!CHECK_FLAG(snode
->nodetype
, LYS_LIST
))
348 /* Walk list of children */
349 LY_FOR_KEYS (snode
, skey
) {
355 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
358 lyd_path(dnode
, LYD_PATH_STD
, xpath
, xpath_len
);
361 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
362 const char *xpath_fmt
, ...)
366 char xpath
[XPATH_MAXLEN
];
368 va_start(ap
, xpath_fmt
);
369 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
372 dnode
= yang_dnode_get(dnode
, xpath
);
374 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
375 "%s: couldn't find %s", __func__
, xpath
);
376 zlog_backtrace(LOG_ERR
);
381 return dnode
->schema
->name
;
384 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
, const char *xpath
)
386 struct ly_set
*set
= NULL
;
387 struct lyd_node
*dnode_ret
= NULL
;
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.
393 if (xpath
[0] == '.' && xpath
[1] == '/')
396 if (lyd_find_xpath(dnode
, xpath
, &set
)) {
397 assert(0); /* XXX replicates old libyang1 base code */
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
);
410 dnode_ret
= set
->dnodes
[0];
413 ly_set_free(set
, NULL
);
418 struct lyd_node
*yang_dnode_getf(const struct lyd_node
*dnode
,
419 const char *xpath_fmt
, ...)
422 char xpath
[XPATH_MAXLEN
];
424 va_start(ap
, xpath_fmt
);
425 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
428 return yang_dnode_get(dnode
, xpath
);
431 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath
)
433 struct ly_set
*set
= NULL
;
436 if (xpath
[0] == '.' && xpath
[1] == '/')
438 if (lyd_find_xpath(dnode
, xpath
, &set
))
440 exists
= set
->count
> 0;
441 ly_set_free(set
, NULL
);
445 bool yang_dnode_existsf(const struct lyd_node
*dnode
, const char *xpath_fmt
,
449 char xpath
[XPATH_MAXLEN
];
451 va_start(ap
, xpath_fmt
);
452 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
455 return yang_dnode_exists(dnode
, xpath
);
458 void yang_dnode_iterate(yang_dnode_iter_cb cb
, void *arg
,
459 const struct lyd_node
*dnode
, const char *xpath_fmt
,
463 char xpath
[XPATH_MAXLEN
];
466 va_start(ap
, xpath_fmt
);
467 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
470 if (lyd_find_xpath(dnode
, xpath
, &set
)) {
471 assert(0); /* XXX libyang2: ly1 code asserted success */
474 for (unsigned int i
= 0; i
< set
->count
; i
++) {
477 ret
= (*cb
)(set
->dnodes
[i
], arg
);
478 if (ret
== YANG_ITER_STOP
)
482 ly_set_free(set
, NULL
);
485 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath
)
487 const struct lysc_node
*snode
;
488 struct lysc_node_leaf
*sleaf
;
491 dnode
= yang_dnode_get(dnode
, xpath
);
494 snode
= dnode
->schema
;
495 switch (snode
->nodetype
) {
497 sleaf
= (struct lysc_node_leaf
*)snode
;
498 if (sleaf
->type
->basetype
== LY_TYPE_EMPTY
)
500 return lyd_is_default(dnode
);
502 /* TODO: check leaf-list default values */
505 if (CHECK_FLAG(snode
->flags
, LYS_PRESENCE
))
513 bool yang_dnode_is_defaultf(const struct lyd_node
*dnode
, const char *xpath_fmt
,
517 return yang_dnode_is_default(dnode
, NULL
);
520 char xpath
[XPATH_MAXLEN
];
522 va_start(ap
, xpath_fmt
);
523 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
526 return yang_dnode_is_default(dnode
, xpath
);
530 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
532 struct lyd_node
*root
, *dnode_iter
;
534 if (!yang_dnode_is_default(dnode
, NULL
))
537 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
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
))
545 LYD_TREE_DFS_END(root
, dnode_iter
);
552 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
554 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
555 lyd_change_term(dnode
, value
);
558 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
560 struct lyd_node
*dnode
= NULL
;
561 int options
= config_only
? LYD_VALIDATE_NO_STATE
: 0;
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__
);
572 struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
)
574 struct lyd_node
*dup
= NULL
;
576 err
= lyd_dup_siblings(dnode
, NULL
, LYD_DUP_RECURSIVE
, &dup
);
581 void yang_dnode_free(struct lyd_node
*dnode
)
583 while (dnode
->parent
)
584 dnode
= lyd_parent(dnode
);
588 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
590 struct yang_data
*data
;
592 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
593 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
595 data
->value
= strdup(value
);
600 void yang_data_free(struct yang_data
*data
)
604 XFREE(MTYPE_YANG_DATA
, data
);
607 struct list
*yang_data_list_new(void)
612 list
->del
= (void (*)(void *))yang_data_free
;
617 struct yang_data
*yang_data_list_find(const struct list
*list
,
618 const char *xpath_fmt
, ...)
620 char xpath
[XPATH_MAXLEN
];
621 struct yang_data
*data
;
622 struct listnode
*node
;
625 va_start(ap
, xpath_fmt
);
626 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
629 for (ALL_LIST_ELEMENTS_RO(list
, node
, data
))
630 if (strmatch(data
->xpath
, xpath
))
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
)
639 int priority
= LOG_ERR
;
646 priority
= LOG_WARNING
;
650 priority
= LOG_DEBUG
;
655 zlog(priority
, "libyang: %s (%s)", msg
, path
);
657 zlog(priority
, "libyang: %s", msg
);
660 const char *yang_print_errors(struct ly_ctx
*ly_ctx
, char *buf
, size_t buf_len
)
662 struct ly_err_item
*ei
;
665 ei
= ly_err_first(ly_ctx
);
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
);
676 path
= ly_errpath(ly_ctx
);
678 strlcat(buf
, " YANG path: ", buf_len
);
679 strlcat(buf
, path
, buf_len
);
680 strlcat(buf
, "\n", buf_len
);
683 ly_err_clean(ly_ctx
, NULL
);
688 void yang_debugging_set(bool enable
)
691 ly_log_level(LY_LLDBG
);
692 ly_log_dbg_groups(0xFF);
694 ly_log_level(LY_LLERR
);
695 ly_log_dbg_groups(0);
699 struct ly_ctx
*yang_ctx_new_setup(bool embedded_modules
, bool explicit_compile
)
701 struct ly_ctx
*ctx
= NULL
;
702 const char *yang_models_path
= YANG_MODELS_PATH
;
705 if (access(yang_models_path
, R_OK
| X_OK
)) {
706 yang_models_path
= NULL
;
708 zlog_info("yang model directory \"%s\" does not exist",
711 flog_err_sys(EC_LIB_LIBYANG
,
712 "cannot access yang model directory \"%s\"",
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
);
723 if (embedded_modules
)
724 ly_ctx_set_module_imp_clb(ctx
, yang_module_imp_clb
, NULL
);
729 void yang_init(bool embedded_modules
, bool defer_compile
)
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
);
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__
);
742 yang_translator_init();
745 void yang_init_loading_complete(void)
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
));
756 void yang_terminate(void)
758 struct yang_module
*module
;
760 yang_translator_terminate();
762 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
763 module
= RB_ROOT(yang_modules
, &yang_modules
);
766 * We shouldn't call ly_ctx_remove_module() here because this
767 * function also removes other modules that depend on it.
769 * ly_ctx_destroy() will release all memory for us.
771 RB_REMOVE(yang_modules
, &yang_modules
, module
);
772 XFREE(MTYPE_YANG_MODULE
, module
);
775 ly_ctx_destroy(ly_native_ctx
);
778 const struct lyd_node
*yang_dnode_get_parent(const struct lyd_node
*dnode
,
781 const struct lyd_node
*orig_dnode
= dnode
;
784 switch (orig_dnode
->schema
->nodetype
) {
787 if (!strcmp(orig_dnode
->schema
->name
, name
))
794 orig_dnode
= lyd_parent(orig_dnode
);
800 bool yang_is_last_list_dnode(const struct lyd_node
*dnode
)
802 return (((dnode
->next
== NULL
)
804 && (strcmp(dnode
->next
->schema
->name
, dnode
->schema
->name
)
807 && ((dnode
->prev
== dnode
)
808 || (strcmp(dnode
->prev
->schema
->name
, dnode
->schema
->name
)
812 bool yang_is_last_level_dnode(const struct lyd_node
*dnode
)
814 const struct lyd_node
*parent
;
815 const struct lyd_node
*key_leaf
;
818 switch (dnode
->schema
->nodetype
) {
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
)
839 const struct lyd_node
*
840 yang_get_subtree_with_no_sibling(const struct lyd_node
*dnode
)
843 const struct lyd_node
*node
;
846 if (node
->schema
->nodetype
!= LYS_LIST
)
850 switch (node
->schema
->nodetype
) {
852 if (!CHECK_FLAG(node
->schema
->flags
, LYS_PRESENCE
)) {
854 && (node
->parent
->schema
->module
855 == dnode
->schema
->module
))
856 node
= lyd_parent(node
);
863 if (yang_is_last_list_dnode(node
)
864 && yang_is_last_level_dnode(node
)) {
866 && (node
->parent
->schema
->module
867 == dnode
->schema
->module
))
868 node
= lyd_parent(node
);
882 uint32_t yang_get_list_pos(const struct lyd_node
*node
)
884 return lyd_list_pos(node
);
887 uint32_t yang_get_list_elements_count(const struct lyd_node
*node
)
890 const struct lysc_node
*schema
;
893 || ((node
->schema
->nodetype
!= LYS_LIST
)
894 && (node
->schema
->nodetype
!= LYS_LEAFLIST
))) {
898 schema
= node
->schema
;
901 if (node
->schema
== schema
)