2 * Copyright (C) 2018 NetDEF, Inc.
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)
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
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
23 #include "lib_errors.h"
25 #include "yang_translator.h"
26 #include "northbound.h"
28 DEFINE_MTYPE_STATIC(LIB
, YANG_MODULE
, "YANG module");
29 DEFINE_MTYPE_STATIC(LIB
, YANG_DATA
, "YANG data structure");
31 /* libyang container. */
32 struct ly_ctx
*ly_native_ctx
;
34 static struct yang_module_embed
*embeds
, **embedupd
= &embeds
;
36 void yang_module_embed(struct yang_module_embed
*embed
)
40 embedupd
= &embed
->next
;
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
,
47 const char **module_data
,
48 void (**free_module_data
)(void *, void *))
50 struct yang_module_embed
*e
;
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 */
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
))
62 if (submod_rev
&& strcmp(e
->sub_mod_rev
, submod_rev
))
65 if (strcmp(e
->mod_name
, mod_name
))
68 if (mod_rev
&& strcmp(e
->mod_rev
, mod_rev
))
73 *module_data
= e
->data
;
77 /* We get here for indirect modules like ietf-inet-types */
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
: "*");
86 /* clang-format off */
87 static const char *const frr_native_modules
[] = {
100 /* clang-format on */
102 /* Generate the yang_modules tree. */
103 static inline int yang_module_compare(const struct yang_module
*a
,
104 const struct yang_module
*b
)
106 return strcmp(a
->name
, b
->name
);
108 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
110 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
112 struct yang_module
*yang_module_load(const char *module_name
)
114 struct yang_module
*module
;
115 const struct lys_module
*module_info
;
118 ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
, NULL
);
120 flog_err(EC_LIB_YANG_MODULE_LOAD
,
121 "%s: failed to load data model: %s", __func__
,
126 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
127 module
->name
= module_name
;
128 module
->info
= module_info
;
130 if (RB_INSERT(yang_modules
, &yang_modules
, module
) != NULL
) {
131 flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY
,
132 "%s: YANG module is loaded already: %s", __func__
,
140 void yang_module_load_all(void)
142 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
143 yang_module_load(frr_native_modules
[i
]);
146 struct yang_module
*yang_module_find(const char *module_name
)
148 struct yang_module s
;
150 s
.name
= module_name
;
151 return RB_FIND(yang_modules
, &yang_modules
, &s
);
154 int yang_snodes_iterate_subtree(const struct lysc_node
*snode
,
155 const struct lys_module
*module
,
156 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
158 const struct lysc_node
*child
;
159 int ret
= YANG_ITER_CONTINUE
;
161 if (module
&& snode
->module
!= module
)
164 switch (snode
->nodetype
) {
166 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
167 if (!CHECK_FLAG(snode
->flags
, LYS_PRESENCE
))
172 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
173 /* Ignore list keys. */
174 if (lysc_is_key(snode
))
180 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
184 assert(snode
->nodetype
!= LYS_AUGMENT
185 && snode
->nodetype
!= LYS_GROUPING
186 && snode
->nodetype
!= LYS_USES
);
190 ret
= (*cb
)(snode
, arg
);
191 if (ret
== YANG_ITER_STOP
)
196 * YANG leafs and leaf-lists can't have child nodes.
198 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
199 return YANG_ITER_CONTINUE
;
201 LY_LIST_FOR (lysc_node_child(snode
), child
) {
202 ret
= yang_snodes_iterate_subtree(child
, module
, cb
, flags
,
204 if (ret
== YANG_ITER_STOP
)
210 int yang_snodes_iterate(const struct lys_module
*module
, yang_iterate_cb cb
,
211 uint16_t flags
, void *arg
)
213 const struct lys_module
*module_iter
;
215 int ret
= YANG_ITER_CONTINUE
;
217 idx
= ly_ctx_internal_modules_count(ly_native_ctx
);
218 while ((module_iter
= ly_ctx_get_module_iter(ly_native_ctx
, &idx
))) {
219 struct lysc_node
*snode
;
221 if (!module_iter
->implemented
)
224 LY_LIST_FOR (module_iter
->compiled
->data
, snode
) {
225 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
227 if (ret
== YANG_ITER_STOP
)
230 LY_LIST_FOR (&module_iter
->compiled
->rpcs
->node
, snode
) {
231 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
233 if (ret
== YANG_ITER_STOP
)
236 LY_LIST_FOR (&module_iter
->compiled
->notifs
->node
, snode
) {
237 ret
= yang_snodes_iterate_subtree(snode
, module
, cb
,
239 if (ret
== YANG_ITER_STOP
)
247 void yang_snode_get_path(const struct lysc_node
*snode
,
248 enum yang_path_type type
, char *xpath
,
252 case YANG_PATH_SCHEMA
:
253 (void)lysc_path(snode
, LYSC_PATH_LOG
, xpath
, xpath_len
);
256 (void)lysc_path(snode
, LYSC_PATH_DATA
, xpath
, xpath_len
);
259 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
265 struct lysc_node
*yang_snode_real_parent(const struct lysc_node
*snode
)
267 struct lysc_node
*parent
= snode
->parent
;
270 switch (parent
->nodetype
) {
272 if (CHECK_FLAG(parent
->flags
, LYS_PRESENCE
))
280 parent
= parent
->parent
;
286 struct lysc_node
*yang_snode_parent_list(const struct lysc_node
*snode
)
288 struct lysc_node
*parent
= snode
->parent
;
291 switch (parent
->nodetype
) {
297 parent
= parent
->parent
;
303 bool yang_snode_is_typeless_data(const struct lysc_node
*snode
)
305 const struct lysc_node_leaf
*sleaf
;
307 switch (snode
->nodetype
) {
309 sleaf
= (struct lysc_node_leaf
*)snode
;
310 if (sleaf
->type
->basetype
== LY_TYPE_EMPTY
)
320 const char *yang_snode_get_default(const struct lysc_node
*snode
)
322 const struct lysc_node_leaf
*sleaf
;
324 switch (snode
->nodetype
) {
326 sleaf
= (const struct lysc_node_leaf
*)snode
;
327 return sleaf
->dflt
? lyd_value_get_canonical(sleaf
->module
->ctx
,
331 /* TODO: check leaf-list default values */
338 const struct lysc_type
*yang_snode_get_type(const struct lysc_node
*snode
)
340 struct lysc_node_leaf
*sleaf
= (struct lysc_node_leaf
*)snode
;
341 struct lysc_type
*type
;
343 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
347 while (type
->basetype
== LY_TYPE_LEAFREF
)
348 type
= ((struct lysc_type_leafref
*)type
)->realtype
;
353 unsigned int yang_snode_num_keys(const struct lysc_node
*snode
)
355 const struct lysc_node_leaf
*skey
;
358 if (!CHECK_FLAG(snode
->nodetype
, LYS_LIST
))
361 /* Walk list of children */
362 LY_FOR_KEYS (snode
, skey
) {
368 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
371 lyd_path(dnode
, LYD_PATH_STD
, xpath
, xpath_len
);
374 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
375 const char *xpath_fmt
, ...)
379 char xpath
[XPATH_MAXLEN
];
381 va_start(ap
, xpath_fmt
);
382 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
385 dnode
= yang_dnode_get(dnode
, xpath
);
387 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
388 "%s: couldn't find %s", __func__
, xpath
);
389 zlog_backtrace(LOG_ERR
);
394 return dnode
->schema
->name
;
397 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
, const char *xpath
)
399 struct ly_set
*set
= NULL
;
400 struct lyd_node
*dnode_ret
= NULL
;
403 * XXX a lot of the code uses this for style I guess. It shouldn't, as
404 * it adds to the xpath parsing complexity in libyang.
406 if (xpath
[0] == '.' && xpath
[1] == '/')
409 if (lyd_find_xpath(dnode
, xpath
, &set
)) {
410 assert(0); /* XXX replicates old libyang1 base code */
416 if (set
->count
> 1) {
417 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND
,
418 "%s: found %u elements (expected 0 or 1) [xpath %s]",
419 __func__
, set
->count
, xpath
);
423 dnode_ret
= set
->dnodes
[0];
426 ly_set_free(set
, NULL
);
431 struct lyd_node
*yang_dnode_getf(const struct lyd_node
*dnode
,
432 const char *xpath_fmt
, ...)
435 char xpath
[XPATH_MAXLEN
];
437 va_start(ap
, xpath_fmt
);
438 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
441 return yang_dnode_get(dnode
, xpath
);
444 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath
)
446 struct ly_set
*set
= NULL
;
449 if (xpath
[0] == '.' && xpath
[1] == '/')
451 if (lyd_find_xpath(dnode
, xpath
, &set
))
453 exists
= set
->count
> 0;
454 ly_set_free(set
, NULL
);
458 bool yang_dnode_existsf(const struct lyd_node
*dnode
, const char *xpath_fmt
,
462 char xpath
[XPATH_MAXLEN
];
464 va_start(ap
, xpath_fmt
);
465 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
468 return yang_dnode_exists(dnode
, xpath
);
471 void yang_dnode_iterate(yang_dnode_iter_cb cb
, void *arg
,
472 const struct lyd_node
*dnode
, const char *xpath_fmt
,
476 char xpath
[XPATH_MAXLEN
];
479 va_start(ap
, xpath_fmt
);
480 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
483 if (lyd_find_xpath(dnode
, xpath
, &set
)) {
484 assert(0); /* XXX libyang2: ly1 code asserted success */
487 for (unsigned int i
= 0; i
< set
->count
; i
++) {
490 ret
= (*cb
)(set
->dnodes
[i
], arg
);
491 if (ret
== YANG_ITER_STOP
)
495 ly_set_free(set
, NULL
);
498 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath
)
500 const struct lysc_node
*snode
;
501 struct lysc_node_leaf
*sleaf
;
504 dnode
= yang_dnode_get(dnode
, xpath
);
507 snode
= dnode
->schema
;
508 switch (snode
->nodetype
) {
510 sleaf
= (struct lysc_node_leaf
*)snode
;
511 if (sleaf
->type
->basetype
== LY_TYPE_EMPTY
)
513 return lyd_is_default(dnode
);
515 /* TODO: check leaf-list default values */
518 if (CHECK_FLAG(snode
->flags
, LYS_PRESENCE
))
526 bool yang_dnode_is_defaultf(const struct lyd_node
*dnode
, const char *xpath_fmt
,
530 return yang_dnode_is_default(dnode
, NULL
);
533 char xpath
[XPATH_MAXLEN
];
535 va_start(ap
, xpath_fmt
);
536 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
539 return yang_dnode_is_default(dnode
, xpath
);
543 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
545 struct lyd_node
*root
, *dnode_iter
;
547 if (!yang_dnode_is_default(dnode
, NULL
))
550 if (CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
553 LY_LIST_FOR (lyd_child(dnode
), root
) {
554 LYD_TREE_DFS_BEGIN (root
, dnode_iter
) {
555 if (!yang_dnode_is_default(dnode_iter
, NULL
))
558 LYD_TREE_DFS_END(root
, dnode_iter
);
565 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
567 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
568 lyd_change_term(dnode
, value
);
571 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
573 struct lyd_node
*dnode
= NULL
;
574 int options
= config_only
? LYD_VALIDATE_NO_STATE
: 0;
576 if (lyd_validate_all(&dnode
, ly_ctx
, options
, NULL
) != 0) {
577 /* Should never happen. */
578 flog_err(EC_LIB_LIBYANG
, "%s: lyd_validate() failed", __func__
);
585 struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
)
587 struct lyd_node
*dup
= NULL
;
589 err
= lyd_dup_siblings(dnode
, NULL
, LYD_DUP_RECURSIVE
, &dup
);
594 void yang_dnode_free(struct lyd_node
*dnode
)
596 while (dnode
->parent
)
597 dnode
= lyd_parent(dnode
);
601 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
603 struct yang_data
*data
;
605 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
606 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
608 data
->value
= strdup(value
);
613 void yang_data_free(struct yang_data
*data
)
617 XFREE(MTYPE_YANG_DATA
, data
);
620 struct list
*yang_data_list_new(void)
625 list
->del
= (void (*)(void *))yang_data_free
;
630 struct yang_data
*yang_data_list_find(const struct list
*list
,
631 const char *xpath_fmt
, ...)
633 char xpath
[XPATH_MAXLEN
];
634 struct yang_data
*data
;
635 struct listnode
*node
;
638 va_start(ap
, xpath_fmt
);
639 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
642 for (ALL_LIST_ELEMENTS_RO(list
, node
, data
))
643 if (strmatch(data
->xpath
, xpath
))
649 /* Make libyang log its errors using FRR logging infrastructure. */
650 static void ly_log_cb(LY_LOG_LEVEL level
, const char *msg
, const char *path
)
652 int priority
= LOG_ERR
;
659 priority
= LOG_WARNING
;
663 priority
= LOG_DEBUG
;
668 zlog(priority
, "libyang: %s (%s)", msg
, path
);
670 zlog(priority
, "libyang: %s", msg
);
673 const char *yang_print_errors(struct ly_ctx
*ly_ctx
, char *buf
, size_t buf_len
)
675 struct ly_err_item
*ei
;
678 ei
= ly_err_first(ly_ctx
);
682 strlcpy(buf
, "YANG error(s):\n", buf_len
);
683 for (; ei
; ei
= ei
->next
) {
684 strlcat(buf
, " ", buf_len
);
685 strlcat(buf
, ei
->msg
, buf_len
);
686 strlcat(buf
, "\n", buf_len
);
689 path
= ly_errpath(ly_ctx
);
691 strlcat(buf
, " YANG path: ", buf_len
);
692 strlcat(buf
, path
, buf_len
);
693 strlcat(buf
, "\n", buf_len
);
696 ly_err_clean(ly_ctx
, NULL
);
701 void yang_debugging_set(bool enable
)
704 ly_log_level(LY_LLDBG
);
705 ly_log_dbg_groups(0xFF);
707 ly_log_level(LY_LLERR
);
708 ly_log_dbg_groups(0);
712 struct ly_ctx
*yang_ctx_new_setup(bool embedded_modules
, bool explicit_compile
)
714 struct ly_ctx
*ctx
= NULL
;
715 const char *yang_models_path
= YANG_MODELS_PATH
;
718 if (access(yang_models_path
, R_OK
| X_OK
)) {
719 yang_models_path
= NULL
;
721 zlog_info("yang model directory \"%s\" does not exist",
724 flog_err_sys(EC_LIB_LIBYANG
,
725 "cannot access yang model directory \"%s\"",
729 uint options
= LY_CTX_NO_YANGLIBRARY
| LY_CTX_DISABLE_SEARCHDIR_CWD
;
730 if (explicit_compile
)
731 options
|= LY_CTX_EXPLICIT_COMPILE
;
732 err
= ly_ctx_new(yang_models_path
, options
, &ctx
);
736 if (embedded_modules
)
737 ly_ctx_set_module_imp_clb(ctx
, yang_module_imp_clb
, NULL
);
742 void yang_init(bool embedded_modules
, bool defer_compile
)
744 /* Initialize libyang global parameters that affect all containers. */
745 ly_set_log_clb(ly_log_cb
, 1);
746 ly_log_options(LY_LOLOG
| LY_LOSTORE
);
748 /* Initialize libyang container for native models. */
749 ly_native_ctx
= yang_ctx_new_setup(embedded_modules
, defer_compile
);
750 if (!ly_native_ctx
) {
751 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
755 yang_translator_init();
758 void yang_init_loading_complete(void)
760 /* Compile everything */
761 if (ly_ctx_compile(ly_native_ctx
) != LY_SUCCESS
) {
762 flog_err(EC_LIB_YANG_MODULE_LOAD
,
763 "%s: failed to compile loaded modules: %s", __func__
,
764 ly_errmsg(ly_native_ctx
));
769 void yang_terminate(void)
771 struct yang_module
*module
;
773 yang_translator_terminate();
775 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
776 module
= RB_ROOT(yang_modules
, &yang_modules
);
779 * We shouldn't call ly_ctx_remove_module() here because this
780 * function also removes other modules that depend on it.
782 * ly_ctx_destroy() will release all memory for us.
784 RB_REMOVE(yang_modules
, &yang_modules
, module
);
785 XFREE(MTYPE_YANG_MODULE
, module
);
788 ly_ctx_destroy(ly_native_ctx
);
791 const struct lyd_node
*yang_dnode_get_parent(const struct lyd_node
*dnode
,
794 const struct lyd_node
*orig_dnode
= dnode
;
797 switch (orig_dnode
->schema
->nodetype
) {
800 if (!strcmp(orig_dnode
->schema
->name
, name
))
807 orig_dnode
= lyd_parent(orig_dnode
);
813 bool yang_is_last_list_dnode(const struct lyd_node
*dnode
)
815 return (((dnode
->next
== NULL
)
817 && (strcmp(dnode
->next
->schema
->name
, dnode
->schema
->name
)
820 && ((dnode
->prev
== dnode
)
821 || (strcmp(dnode
->prev
->schema
->name
, dnode
->schema
->name
)
825 bool yang_is_last_level_dnode(const struct lyd_node
*dnode
)
827 const struct lyd_node
*parent
;
828 const struct lyd_node
*key_leaf
;
831 switch (dnode
->schema
->nodetype
) {
833 assert(dnode
->parent
);
834 parent
= lyd_parent(dnode
);
835 uint snode_num_keys
= yang_snode_num_keys(parent
->schema
);
836 /* XXX libyang2: q: really don't understand this code. */
837 key_leaf
= dnode
->prev
;
838 for (keys_size
= 1; keys_size
< snode_num_keys
; keys_size
++)
839 key_leaf
= key_leaf
->prev
;
840 if (key_leaf
->prev
== dnode
)
852 const struct lyd_node
*
853 yang_get_subtree_with_no_sibling(const struct lyd_node
*dnode
)
856 const struct lyd_node
*node
;
859 if (node
->schema
->nodetype
!= LYS_LIST
)
863 switch (node
->schema
->nodetype
) {
865 if (!CHECK_FLAG(node
->schema
->flags
, LYS_PRESENCE
)) {
867 && (node
->parent
->schema
->module
868 == dnode
->schema
->module
))
869 node
= lyd_parent(node
);
876 if (yang_is_last_list_dnode(node
)
877 && yang_is_last_level_dnode(node
)) {
879 && (node
->parent
->schema
->module
880 == dnode
->schema
->module
))
881 node
= lyd_parent(node
);
895 uint32_t yang_get_list_pos(const struct lyd_node
*node
)
897 return lyd_list_pos(node
);
900 uint32_t yang_get_list_elements_count(const struct lyd_node
*node
)
903 const struct lysc_node
*schema
;
906 || ((node
->schema
->nodetype
!= LYS_LIST
)
907 && (node
->schema
->nodetype
!= LYS_LEAFLIST
))) {
911 schema
= node
->schema
;
914 if (node
->schema
== schema
)