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
24 #include "lib_errors.h"
26 #include "yang_translator.h"
27 #include "northbound.h"
29 #include <libyang/user_types.h>
31 DEFINE_MTYPE(LIB
, YANG_MODULE
, "YANG module")
32 DEFINE_MTYPE(LIB
, YANG_DATA
, "YANG data structure")
34 /* libyang container. */
35 struct ly_ctx
*ly_native_ctx
;
37 static struct yang_module_embed
*embeds
, **embedupd
= &embeds
;
39 void yang_module_embed(struct yang_module_embed
*embed
)
43 embedupd
= &embed
->next
;
46 static const char *yang_module_imp_clb(const char *mod_name
,
48 const char *submod_name
,
49 const char *submod_rev
,
52 void (**free_module_data
)
55 struct yang_module_embed
*e
;
57 if (submod_name
|| submod_rev
)
60 for (e
= embeds
; e
; e
= e
->next
) {
61 if (strcmp(e
->mod_name
, mod_name
))
63 if (mod_rev
&& strcmp(e
->mod_rev
, mod_rev
))
70 flog_warn(EC_LIB_YANG_MODULE_LOAD
,
71 "YANG model \"%s@%s\" not embedded, trying external file",
72 mod_name
, mod_rev
? mod_rev
: "*");
76 static const char *const frr_native_modules
[] = {
83 /* Generate the yang_modules tree. */
84 static inline int yang_module_compare(const struct yang_module
*a
,
85 const struct yang_module
*b
)
87 return strcmp(a
->name
, b
->name
);
89 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
91 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
93 struct yang_module
*yang_module_load(const char *module_name
)
95 struct yang_module
*module
;
96 const struct lys_module
*module_info
;
98 module_info
= ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
);
100 flog_err(EC_LIB_YANG_MODULE_LOAD
,
101 "%s: failed to load data model: %s", __func__
,
106 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
107 module
->name
= module_name
;
108 module
->info
= module_info
;
110 if (RB_INSERT(yang_modules
, &yang_modules
, module
) != NULL
) {
111 flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY
,
112 "%s: YANG module is loaded already: %s", __func__
,
120 void yang_module_load_all(void)
122 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
123 yang_module_load(frr_native_modules
[i
]);
126 struct yang_module
*yang_module_find(const char *module_name
)
128 struct yang_module s
;
130 s
.name
= module_name
;
131 return RB_FIND(yang_modules
, &yang_modules
, &s
);
134 int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
135 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
137 struct lys_node
*child
;
138 int ret
= YANG_ITER_CONTINUE
;
140 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_IMPLICIT
)) {
141 switch (snode
->nodetype
) {
145 if (CHECK_FLAG(snode
->flags
, LYS_IMPLICIT
))
153 switch (snode
->nodetype
) {
155 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
156 struct lys_node_container
*scontainer
;
158 scontainer
= (struct lys_node_container
*)snode
;
159 if (!scontainer
->presence
)
164 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
165 struct lys_node_leaf
*sleaf
;
167 /* Ignore list keys. */
168 sleaf
= (struct lys_node_leaf
*)snode
;
169 if (lys_is_key(sleaf
, NULL
))
174 /* Return since we're not interested in the grouping subtree. */
175 return YANG_ITER_CONTINUE
;
178 /* Always ignore nodes of these types. */
182 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
189 ret
= (*cb
)(snode
, arg
);
190 if (ret
== YANG_ITER_STOP
)
195 * YANG leafs and leaf-lists can't have child nodes, and trying to
196 * access snode->child is undefined behavior.
198 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
199 return YANG_ITER_CONTINUE
;
201 LY_TREE_FOR (snode
->child
, child
) {
202 if (!CHECK_FLAG(flags
, YANG_ITER_ALLOW_AUGMENTATIONS
)
203 && child
->parent
!= snode
)
206 ret
= yang_snodes_iterate_subtree(child
, cb
, flags
, arg
);
207 if (ret
== YANG_ITER_STOP
)
214 int yang_snodes_iterate_module(const struct lys_module
*module
,
215 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
217 struct lys_node
*snode
;
218 int ret
= YANG_ITER_CONTINUE
;
220 LY_TREE_FOR (module
->data
, snode
) {
221 ret
= yang_snodes_iterate_subtree(snode
, cb
, flags
, arg
);
222 if (ret
== YANG_ITER_STOP
)
226 for (uint8_t i
= 0; i
< module
->augment_size
; i
++) {
227 ret
= yang_snodes_iterate_subtree(
228 (const struct lys_node
*)&module
->augment
[i
], cb
, flags
,
230 if (ret
== YANG_ITER_STOP
)
237 int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
, void *arg
)
239 struct yang_module
*module
;
240 int ret
= YANG_ITER_CONTINUE
;
242 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
243 ret
= yang_snodes_iterate_module(module
->info
, cb
, flags
, arg
);
244 if (ret
== YANG_ITER_STOP
)
251 void yang_snode_get_path(const struct lys_node
*snode
, enum yang_path_type type
,
252 char *xpath
, size_t xpath_len
)
257 case YANG_PATH_SCHEMA
:
258 xpath_ptr
= lys_path(snode
, 0);
261 xpath_ptr
= lys_data_path(snode
);
264 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
268 strlcpy(xpath
, xpath_ptr
, xpath_len
);
272 struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
)
274 struct lys_node
*parent
= snode
->parent
;
277 struct lys_node_container
*scontainer
;
279 switch (parent
->nodetype
) {
281 scontainer
= (struct lys_node_container
*)parent
;
282 if (scontainer
->presence
)
290 parent
= parent
->parent
;
296 struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
)
298 struct lys_node
*parent
= snode
->parent
;
301 switch (parent
->nodetype
) {
307 parent
= parent
->parent
;
313 bool yang_snode_is_typeless_data(const struct lys_node
*snode
)
315 struct lys_node_leaf
*sleaf
;
317 switch (snode
->nodetype
) {
319 sleaf
= (struct lys_node_leaf
*)snode
;
320 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
330 const char *yang_snode_get_default(const struct lys_node
*snode
)
332 struct lys_node_leaf
*sleaf
;
334 switch (snode
->nodetype
) {
336 sleaf
= (struct lys_node_leaf
*)snode
;
338 /* NOTE: this might be null. */
341 /* TODO: check leaf-list default values */
348 const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
)
350 struct lys_node_leaf
*sleaf
= (struct lys_node_leaf
*)snode
;
351 struct lys_type
*type
;
353 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
357 while (type
->base
== LY_TYPE_LEAFREF
)
358 type
= &type
->info
.lref
.target
->type
;
363 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
368 xpath_ptr
= lyd_path(dnode
);
369 strlcpy(xpath
, xpath_ptr
, xpath_len
);
373 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
374 const char *xpath_fmt
, ...)
378 char xpath
[XPATH_MAXLEN
];
380 va_start(ap
, xpath_fmt
);
381 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
384 dnode
= yang_dnode_get(dnode
, xpath
);
386 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
387 "%s: couldn't find %s", __func__
, xpath
);
388 zlog_backtrace(LOG_ERR
);
393 return dnode
->schema
->name
;
396 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
397 const char *xpath_fmt
, ...)
400 char xpath
[XPATH_MAXLEN
];
402 struct lyd_node
*dnode_ret
= NULL
;
404 va_start(ap
, xpath_fmt
);
405 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
408 set
= lyd_find_path(dnode
, xpath
);
410 if (set
->number
== 0)
413 if (set
->number
> 1) {
414 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND
,
415 "%s: found %u elements (expected 0 or 1) [xpath %s]",
416 __func__
, set
->number
, xpath
);
420 dnode_ret
= set
->set
.d
[0];
428 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath_fmt
, ...)
431 char xpath
[XPATH_MAXLEN
];
435 va_start(ap
, xpath_fmt
);
436 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
439 set
= lyd_find_path(dnode
, xpath
);
441 found
= (set
->number
> 0);
447 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath_fmt
,
450 struct lys_node
*snode
;
451 struct lys_node_leaf
*sleaf
;
452 struct lys_node_container
*scontainer
;
456 char xpath
[XPATH_MAXLEN
];
458 va_start(ap
, xpath_fmt
);
459 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
462 dnode
= yang_dnode_get(dnode
, xpath
);
466 snode
= dnode
->schema
;
467 switch (snode
->nodetype
) {
469 sleaf
= (struct lys_node_leaf
*)snode
;
470 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
472 return lyd_wd_default((struct lyd_node_leaf_list
*)dnode
);
474 /* TODO: check leaf-list default values */
477 scontainer
= (struct lys_node_container
*)snode
;
478 if (scontainer
->presence
)
486 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
488 struct lys_node
*snode
;
489 struct lyd_node
*root
, *next
, *dnode_iter
;
491 snode
= dnode
->schema
;
492 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
493 return yang_dnode_is_default(dnode
, NULL
);
495 if (!yang_dnode_is_default(dnode
, NULL
))
498 LY_TREE_FOR (dnode
->child
, root
) {
499 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
500 if (!yang_dnode_is_default(dnode_iter
, NULL
))
503 LY_TREE_DFS_END(root
, next
, dnode_iter
);
510 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
512 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
513 lyd_change_leaf((struct lyd_node_leaf_list
*)dnode
, value
);
516 void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
)
518 assert(CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
));
519 lyd_set_private(dnode
, entry
);
522 void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
523 bool abort_if_not_found
)
525 const struct lyd_node
*orig_dnode
= dnode
;
526 char xpath
[XPATH_MAXLEN
];
529 switch (dnode
->schema
->nodetype
) {
539 dnode
= dnode
->parent
;
542 if (!abort_if_not_found
)
545 yang_dnode_get_path(orig_dnode
, xpath
, sizeof(xpath
));
546 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
547 "%s: failed to find entry [xpath %s]", __func__
, xpath
);
548 zlog_backtrace(LOG_ERR
);
552 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
554 struct lyd_node
*dnode
;
558 options
= LYD_OPT_CONFIG
;
560 options
= LYD_OPT_DATA
| LYD_OPT_DATA_NO_YANGLIB
;
563 if (lyd_validate(&dnode
, options
, ly_ctx
) != 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 return lyd_dup_withsiblings(dnode
, 1);
577 void yang_dnode_free(struct lyd_node
*dnode
)
579 while (dnode
->parent
)
580 dnode
= dnode
->parent
;
581 lyd_free_withsiblings(dnode
);
584 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
586 struct yang_data
*data
;
588 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
589 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
591 data
->value
= strdup(value
);
596 void yang_data_free(struct yang_data
*data
)
600 XFREE(MTYPE_YANG_DATA
, data
);
603 struct list
*yang_data_list_new(void)
608 list
->del
= (void (*)(void *))yang_data_free
;
613 static void *ly_dup_cb(const void *priv
)
615 /* Make a shallow copy of the priv pointer. */
619 /* Make libyang log its errors using FRR logging infrastructure. */
620 static void ly_log_cb(LY_LOG_LEVEL level
, const char *msg
, const char *path
)
629 priority
= LOG_WARNING
;
632 priority
= LOG_DEBUG
;
639 zlog(priority
, "libyang: %s (%s)", msg
, path
);
641 zlog(priority
, "libyang: %s", msg
);
644 #if CONFDATE > 20190401
645 CPP_NOTICE("lib/yang: time to remove non-LIBYANG_EXT_BUILTIN support")
648 #ifdef LIBYANG_EXT_BUILTIN
649 extern struct lytype_plugin_list frr_user_types
[];
652 struct ly_ctx
*yang_ctx_new_setup(void)
655 const char *yang_models_path
= YANG_MODELS_PATH
;
657 if (access(yang_models_path
, R_OK
| X_OK
)) {
658 yang_models_path
= NULL
;
660 zlog_info("yang model directory \"%s\" does not exist",
663 flog_err_sys(EC_LIB_LIBYANG
,
664 "cannot access yang model directory \"%s\"",
668 ctx
= ly_ctx_new(yang_models_path
, LY_CTX_DISABLE_SEARCHDIR_CWD
);
671 ly_ctx_set_module_imp_clb(ctx
, yang_module_imp_clb
, NULL
);
677 #ifndef LIBYANG_EXT_BUILTIN
678 CPP_NOTICE("lib/yang: deprecated libyang <0.16.74 extension loading in use!")
679 static char ly_plugin_dir
[PATH_MAX
];
680 const char *const *ly_loaded_plugins
;
681 const char *ly_plugin
;
682 bool found_ly_frr_types
= false;
684 /* Tell libyang where to find its plugins. */
685 snprintf(ly_plugin_dir
, sizeof(ly_plugin_dir
), "%s=%s",
686 "LIBYANG_USER_TYPES_PLUGINS_DIR", LIBYANG_PLUGINS_PATH
);
687 putenv(ly_plugin_dir
);
690 /* Initialize libyang global parameters that affect all containers. */
691 ly_set_log_clb(ly_log_cb
, 1);
692 ly_log_options(LY_LOLOG
| LY_LOSTORE
);
694 #ifdef LIBYANG_EXT_BUILTIN
695 if (ly_register_types(frr_user_types
, "frr_user_types")) {
696 flog_err(EC_LIB_LIBYANG_PLUGIN_LOAD
,
697 "ly_register_types() failed");
702 /* Initialize libyang container for native models. */
703 ly_native_ctx
= yang_ctx_new_setup();
704 if (!ly_native_ctx
) {
705 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
708 ly_ctx_set_priv_dup_clb(ly_native_ctx
, ly_dup_cb
);
710 #ifndef LIBYANG_EXT_BUILTIN
711 /* Detect if the required libyang plugin(s) were loaded successfully. */
712 ly_loaded_plugins
= ly_get_loaded_plugins();
713 for (size_t i
= 0; (ly_plugin
= ly_loaded_plugins
[i
]); i
++) {
714 if (strmatch(ly_plugin
, "frr_user_types")) {
715 found_ly_frr_types
= true;
719 if (!found_ly_frr_types
) {
720 flog_err(EC_LIB_LIBYANG_PLUGIN_LOAD
,
721 "%s: failed to load frr_user_types.so", __func__
);
726 yang_translator_init();
729 void yang_terminate(void)
731 struct yang_module
*module
;
733 yang_translator_terminate();
735 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
736 module
= RB_ROOT(yang_modules
, &yang_modules
);
739 * We shouldn't call ly_ctx_remove_module() here because this
740 * function also removes other modules that depend on it.
742 * ly_ctx_destroy() will release all memory for us.
744 RB_REMOVE(yang_modules
, &yang_modules
, module
);
745 XFREE(MTYPE_YANG_MODULE
, module
);
748 ly_ctx_destroy(ly_native_ctx
, NULL
);