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 DEFINE_MTYPE(LIB
, YANG_MODULE
, "YANG module")
30 DEFINE_MTYPE(LIB
, YANG_DATA
, "YANG data structure")
32 /* libyang container. */
33 struct ly_ctx
*ly_native_ctx
;
35 static struct yang_module_embed
*embeds
, **embedupd
= &embeds
;
37 void yang_module_embed(struct yang_module_embed
*embed
)
41 embedupd
= &embed
->next
;
44 static const char *yang_module_imp_clb(const char *mod_name
,
46 const char *submod_name
,
47 const char *submod_rev
,
50 void (**free_module_data
)
53 struct yang_module_embed
*e
;
55 if (submod_name
|| submod_rev
)
58 for (e
= embeds
; e
; e
= e
->next
) {
59 if (strcmp(e
->mod_name
, mod_name
))
61 if (mod_rev
&& strcmp(e
->mod_rev
, mod_rev
))
68 flog_warn(EC_LIB_YANG_MODULE_LOAD
,
69 "YANG model \"%s@%s\" not embedded, trying external file",
70 mod_name
, mod_rev
? mod_rev
: "*");
74 static const char * const frr_native_modules
[] = {
80 /* Generate the yang_modules tree. */
81 static inline int yang_module_compare(const struct yang_module
*a
,
82 const struct yang_module
*b
)
84 return strcmp(a
->name
, b
->name
);
86 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
88 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
90 struct yang_module
*yang_module_load(const char *module_name
)
92 struct yang_module
*module
;
93 const struct lys_module
*module_info
;
95 module_info
= ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
);
97 flog_err(EC_LIB_YANG_MODULE_LOAD
,
98 "%s: failed to load data model: %s", __func__
,
103 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
104 module
->name
= module_name
;
105 module
->info
= module_info
;
107 if (RB_INSERT(yang_modules
, &yang_modules
, module
) != NULL
) {
108 flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY
,
109 "%s: YANG module is loaded already: %s", __func__
,
117 void yang_module_load_all(void)
119 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
120 yang_module_load(frr_native_modules
[i
]);
123 struct yang_module
*yang_module_find(const char *module_name
)
125 struct yang_module s
;
127 s
.name
= module_name
;
128 return RB_FIND(yang_modules
, &yang_modules
, &s
);
131 int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
132 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
134 struct lys_node
*child
;
135 int ret
= YANG_ITER_CONTINUE
;
137 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_IMPLICIT
)) {
138 switch (snode
->nodetype
) {
142 if (CHECK_FLAG(snode
->flags
, LYS_IMPLICIT
))
150 switch (snode
->nodetype
) {
152 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
153 struct lys_node_container
*scontainer
;
155 scontainer
= (struct lys_node_container
*)snode
;
156 if (!scontainer
->presence
)
161 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
162 struct lys_node_leaf
*sleaf
;
164 /* Ignore list keys. */
165 sleaf
= (struct lys_node_leaf
*)snode
;
166 if (lys_is_key(sleaf
, NULL
))
171 /* Return since we're not interested in the grouping subtree. */
172 return YANG_ITER_CONTINUE
;
175 /* Always ignore nodes of these types. */
179 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
186 ret
= (*cb
)(snode
, arg
);
187 if (ret
== YANG_ITER_STOP
)
192 * YANG leafs and leaf-lists can't have child nodes, and trying to
193 * access snode->child is undefined behavior.
195 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
196 return YANG_ITER_CONTINUE
;
198 LY_TREE_FOR (snode
->child
, child
) {
199 if (!CHECK_FLAG(flags
, YANG_ITER_ALLOW_AUGMENTATIONS
)
200 && child
->parent
!= snode
)
203 ret
= yang_snodes_iterate_subtree(child
, cb
, flags
, arg
);
204 if (ret
== YANG_ITER_STOP
)
211 int yang_snodes_iterate_module(const struct lys_module
*module
,
212 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
214 struct lys_node
*snode
;
215 int ret
= YANG_ITER_CONTINUE
;
217 LY_TREE_FOR (module
->data
, snode
) {
218 ret
= yang_snodes_iterate_subtree(snode
, cb
, flags
, arg
);
219 if (ret
== YANG_ITER_STOP
)
223 for (uint8_t i
= 0; i
< module
->augment_size
; i
++) {
224 ret
= yang_snodes_iterate_subtree(
225 (const struct lys_node
*)&module
->augment
[i
], cb
, flags
,
227 if (ret
== YANG_ITER_STOP
)
234 int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
, void *arg
)
236 struct yang_module
*module
;
237 int ret
= YANG_ITER_CONTINUE
;
239 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
240 ret
= yang_snodes_iterate_module(module
->info
, cb
, flags
, arg
);
241 if (ret
== YANG_ITER_STOP
)
248 void yang_snode_get_path(const struct lys_node
*snode
, enum yang_path_type type
,
249 char *xpath
, size_t xpath_len
)
254 case YANG_PATH_SCHEMA
:
255 xpath_ptr
= lys_path(snode
, 0);
258 xpath_ptr
= lys_data_path(snode
);
261 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
265 strlcpy(xpath
, xpath_ptr
, xpath_len
);
269 struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
)
271 struct lys_node
*parent
= snode
->parent
;
274 struct lys_node_container
*scontainer
;
276 switch (parent
->nodetype
) {
278 scontainer
= (struct lys_node_container
*)parent
;
279 if (scontainer
->presence
)
287 parent
= parent
->parent
;
293 struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
)
295 struct lys_node
*parent
= snode
->parent
;
298 switch (parent
->nodetype
) {
304 parent
= parent
->parent
;
310 bool yang_snode_is_typeless_data(const struct lys_node
*snode
)
312 struct lys_node_leaf
*sleaf
;
314 switch (snode
->nodetype
) {
316 sleaf
= (struct lys_node_leaf
*)snode
;
317 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
327 const char *yang_snode_get_default(const struct lys_node
*snode
)
329 struct lys_node_leaf
*sleaf
;
331 switch (snode
->nodetype
) {
333 sleaf
= (struct lys_node_leaf
*)snode
;
335 /* NOTE: this might be null. */
338 /* TODO: check leaf-list default values */
345 const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
)
347 struct lys_node_leaf
*sleaf
= (struct lys_node_leaf
*)snode
;
348 struct lys_type
*type
;
350 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
354 while (type
->base
== LY_TYPE_LEAFREF
)
355 type
= &type
->info
.lref
.target
->type
;
360 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
365 xpath_ptr
= lyd_path(dnode
);
366 strlcpy(xpath
, xpath_ptr
, xpath_len
);
370 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
371 const char *xpath_fmt
, ...)
375 char xpath
[XPATH_MAXLEN
];
377 va_start(ap
, xpath_fmt
);
378 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
381 dnode
= yang_dnode_get(dnode
, xpath
);
383 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
384 "%s: couldn't find %s", __func__
, xpath
);
385 zlog_backtrace(LOG_ERR
);
390 return dnode
->schema
->name
;
393 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
394 const char *xpath_fmt
, ...)
397 char xpath
[XPATH_MAXLEN
];
399 struct lyd_node
*dnode_ret
= NULL
;
401 va_start(ap
, xpath_fmt
);
402 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
405 set
= lyd_find_path(dnode
, xpath
);
407 if (set
->number
== 0)
410 if (set
->number
> 1) {
411 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND
,
412 "%s: found %u elements (expected 0 or 1) [xpath %s]",
413 __func__
, set
->number
, xpath
);
417 dnode_ret
= set
->set
.d
[0];
425 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath_fmt
, ...)
428 char xpath
[XPATH_MAXLEN
];
432 va_start(ap
, xpath_fmt
);
433 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
436 set
= lyd_find_path(dnode
, xpath
);
438 found
= (set
->number
> 0);
444 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath_fmt
,
447 struct lys_node
*snode
;
448 struct lys_node_leaf
*sleaf
;
449 struct lys_node_container
*scontainer
;
453 char xpath
[XPATH_MAXLEN
];
455 va_start(ap
, xpath_fmt
);
456 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
459 dnode
= yang_dnode_get(dnode
, xpath
);
463 snode
= dnode
->schema
;
464 switch (snode
->nodetype
) {
466 sleaf
= (struct lys_node_leaf
*)snode
;
467 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
469 return lyd_wd_default((struct lyd_node_leaf_list
*)dnode
);
471 /* TODO: check leaf-list default values */
474 scontainer
= (struct lys_node_container
*)snode
;
475 if (scontainer
->presence
)
483 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
485 struct lys_node
*snode
;
486 struct lyd_node
*root
, *next
, *dnode_iter
;
488 snode
= dnode
->schema
;
489 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
490 return yang_dnode_is_default(dnode
, NULL
);
492 if (!yang_dnode_is_default(dnode
, NULL
))
495 LY_TREE_FOR (dnode
->child
, root
) {
496 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
497 if (!yang_dnode_is_default(dnode_iter
, NULL
))
500 LY_TREE_DFS_END(root
, next
, dnode_iter
);
507 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
509 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
510 lyd_change_leaf((struct lyd_node_leaf_list
*)dnode
, value
);
513 void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
)
515 assert(CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
));
516 lyd_set_private(dnode
, entry
);
519 void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
520 bool abort_if_not_found
)
522 const struct lyd_node
*orig_dnode
= dnode
;
523 char xpath
[XPATH_MAXLEN
];
526 switch (dnode
->schema
->nodetype
) {
536 dnode
= dnode
->parent
;
539 if (!abort_if_not_found
)
542 yang_dnode_get_path(orig_dnode
, xpath
, sizeof(xpath
));
543 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
544 "%s: failed to find entry [xpath %s]", __func__
, xpath
);
545 zlog_backtrace(LOG_ERR
);
549 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
551 struct lyd_node
*dnode
;
555 options
= LYD_OPT_CONFIG
;
557 options
= LYD_OPT_DATA
| LYD_OPT_DATA_NO_YANGLIB
;
560 if (lyd_validate(&dnode
, options
, ly_ctx
) != 0) {
561 /* Should never happen. */
562 flog_err(EC_LIB_LIBYANG
, "%s: lyd_validate() failed", __func__
);
569 struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
)
571 return lyd_dup_withsiblings(dnode
, 1);
574 void yang_dnode_free(struct lyd_node
*dnode
)
576 while (dnode
->parent
)
577 dnode
= dnode
->parent
;
578 lyd_free_withsiblings(dnode
);
581 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
583 struct yang_data
*data
;
585 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
586 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
588 data
->value
= strdup(value
);
593 void yang_data_free(struct yang_data
*data
)
597 XFREE(MTYPE_YANG_DATA
, data
);
600 struct list
*yang_data_list_new(void)
605 list
->del
= (void (*)(void *))yang_data_free
;
610 static void *ly_dup_cb(const void *priv
)
612 /* Make a shallow copy of the priv pointer. */
616 /* Make libyang log its errors using FRR logging infrastructure. */
617 static void ly_log_cb(LY_LOG_LEVEL level
, const char *msg
, const char *path
)
626 priority
= LOG_WARNING
;
629 priority
= LOG_DEBUG
;
636 zlog(priority
, "libyang: %s (%s)", msg
, path
);
638 zlog(priority
, "libyang: %s", msg
);
643 static char ly_plugin_dir
[PATH_MAX
];
644 const char *const *ly_loaded_plugins
;
645 const char *ly_plugin
;
646 bool found_ly_frr_types
= false;
648 /* Tell libyang where to find its plugins. */
649 snprintf(ly_plugin_dir
, sizeof(ly_plugin_dir
), "%s=%s",
650 "LIBYANG_USER_TYPES_PLUGINS_DIR", LIBYANG_PLUGINS_PATH
);
651 putenv(ly_plugin_dir
);
653 /* Initialize libyang global parameters that affect all containers. */
654 ly_set_log_clb(ly_log_cb
, 1);
655 ly_log_options(LY_LOLOG
| LY_LOSTORE
);
657 /* Initialize libyang container for native models. */
659 ly_ctx_new(YANG_MODELS_PATH
, LY_CTX_DISABLE_SEARCHDIR_CWD
);
660 if (!ly_native_ctx
) {
661 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
664 ly_ctx_set_module_imp_clb(ly_native_ctx
, yang_module_imp_clb
, NULL
);
665 ly_ctx_set_priv_dup_clb(ly_native_ctx
, ly_dup_cb
);
667 /* Detect if the required libyang plugin(s) were loaded successfully. */
668 ly_loaded_plugins
= ly_get_loaded_plugins();
669 for (size_t i
= 0; (ly_plugin
= ly_loaded_plugins
[i
]); i
++) {
670 if (strmatch(ly_plugin
, "frr_user_types")) {
671 found_ly_frr_types
= true;
675 if (!found_ly_frr_types
) {
676 flog_err(EC_LIB_LIBYANG_PLUGIN_LOAD
,
677 "%s: failed to load frr_user_types.so", __func__
);
681 yang_translator_init();
684 void yang_terminate(void)
686 struct yang_module
*module
;
688 yang_translator_terminate();
690 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
691 module
= RB_ROOT(yang_modules
, &yang_modules
);
694 * We shouldn't call ly_ctx_remove_module() here because this
695 * function also removes other modules that depend on it.
697 * ly_ctx_destroy() will release all memory for us.
699 RB_REMOVE(yang_modules
, &yang_modules
, module
);
700 XFREE(MTYPE_YANG_MODULE
, module
);
703 ly_ctx_destroy(ly_native_ctx
, NULL
);