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
[] = {
81 /* Generate the yang_modules tree. */
82 static inline int yang_module_compare(const struct yang_module
*a
,
83 const struct yang_module
*b
)
85 return strcmp(a
->name
, b
->name
);
87 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
89 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
91 struct yang_module
*yang_module_load(const char *module_name
)
93 struct yang_module
*module
;
94 const struct lys_module
*module_info
;
96 module_info
= ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
);
98 flog_err(EC_LIB_YANG_MODULE_LOAD
,
99 "%s: failed to load data model: %s", __func__
,
104 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
105 module
->name
= module_name
;
106 module
->info
= module_info
;
108 if (RB_INSERT(yang_modules
, &yang_modules
, module
) != NULL
) {
109 flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY
,
110 "%s: YANG module is loaded already: %s", __func__
,
118 void yang_module_load_all(void)
120 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
121 yang_module_load(frr_native_modules
[i
]);
124 struct yang_module
*yang_module_find(const char *module_name
)
126 struct yang_module s
;
128 s
.name
= module_name
;
129 return RB_FIND(yang_modules
, &yang_modules
, &s
);
132 int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
133 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
135 struct lys_node
*child
;
136 int ret
= YANG_ITER_CONTINUE
;
138 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_IMPLICIT
)) {
139 switch (snode
->nodetype
) {
143 if (CHECK_FLAG(snode
->flags
, LYS_IMPLICIT
))
151 switch (snode
->nodetype
) {
153 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
154 struct lys_node_container
*scontainer
;
156 scontainer
= (struct lys_node_container
*)snode
;
157 if (!scontainer
->presence
)
162 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
163 struct lys_node_leaf
*sleaf
;
165 /* Ignore list keys. */
166 sleaf
= (struct lys_node_leaf
*)snode
;
167 if (lys_is_key(sleaf
, NULL
))
172 /* Return since we're not interested in the grouping subtree. */
173 return YANG_ITER_CONTINUE
;
176 /* Always ignore nodes of these types. */
180 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
187 ret
= (*cb
)(snode
, arg
);
188 if (ret
== YANG_ITER_STOP
)
193 * YANG leafs and leaf-lists can't have child nodes, and trying to
194 * access snode->child is undefined behavior.
196 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
197 return YANG_ITER_CONTINUE
;
199 LY_TREE_FOR (snode
->child
, child
) {
200 if (!CHECK_FLAG(flags
, YANG_ITER_ALLOW_AUGMENTATIONS
)
201 && child
->parent
!= snode
)
204 ret
= yang_snodes_iterate_subtree(child
, cb
, flags
, arg
);
205 if (ret
== YANG_ITER_STOP
)
212 int yang_snodes_iterate_module(const struct lys_module
*module
,
213 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
215 struct lys_node
*snode
;
216 int ret
= YANG_ITER_CONTINUE
;
218 LY_TREE_FOR (module
->data
, snode
) {
219 ret
= yang_snodes_iterate_subtree(snode
, cb
, flags
, arg
);
220 if (ret
== YANG_ITER_STOP
)
224 for (uint8_t i
= 0; i
< module
->augment_size
; i
++) {
225 ret
= yang_snodes_iterate_subtree(
226 (const struct lys_node
*)&module
->augment
[i
], cb
, flags
,
228 if (ret
== YANG_ITER_STOP
)
235 int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
, void *arg
)
237 struct yang_module
*module
;
238 int ret
= YANG_ITER_CONTINUE
;
240 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
241 ret
= yang_snodes_iterate_module(module
->info
, cb
, flags
, arg
);
242 if (ret
== YANG_ITER_STOP
)
249 void yang_snode_get_path(const struct lys_node
*snode
, enum yang_path_type type
,
250 char *xpath
, size_t xpath_len
)
255 case YANG_PATH_SCHEMA
:
256 xpath_ptr
= lys_path(snode
, 0);
259 xpath_ptr
= lys_data_path(snode
);
262 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
266 strlcpy(xpath
, xpath_ptr
, xpath_len
);
270 struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
)
272 struct lys_node
*parent
= snode
->parent
;
275 struct lys_node_container
*scontainer
;
277 switch (parent
->nodetype
) {
279 scontainer
= (struct lys_node_container
*)parent
;
280 if (scontainer
->presence
)
288 parent
= parent
->parent
;
294 struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
)
296 struct lys_node
*parent
= snode
->parent
;
299 switch (parent
->nodetype
) {
305 parent
= parent
->parent
;
311 bool yang_snode_is_typeless_data(const struct lys_node
*snode
)
313 struct lys_node_leaf
*sleaf
;
315 switch (snode
->nodetype
) {
317 sleaf
= (struct lys_node_leaf
*)snode
;
318 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
328 const char *yang_snode_get_default(const struct lys_node
*snode
)
330 struct lys_node_leaf
*sleaf
;
332 switch (snode
->nodetype
) {
334 sleaf
= (struct lys_node_leaf
*)snode
;
336 /* NOTE: this might be null. */
339 /* TODO: check leaf-list default values */
346 const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
)
348 struct lys_node_leaf
*sleaf
= (struct lys_node_leaf
*)snode
;
349 struct lys_type
*type
;
351 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
355 while (type
->base
== LY_TYPE_LEAFREF
)
356 type
= &type
->info
.lref
.target
->type
;
361 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
366 xpath_ptr
= lyd_path(dnode
);
367 strlcpy(xpath
, xpath_ptr
, xpath_len
);
371 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
372 const char *xpath_fmt
, ...)
376 char xpath
[XPATH_MAXLEN
];
378 va_start(ap
, xpath_fmt
);
379 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
382 dnode
= yang_dnode_get(dnode
, xpath
);
384 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
385 "%s: couldn't find %s", __func__
, xpath
);
386 zlog_backtrace(LOG_ERR
);
391 return dnode
->schema
->name
;
394 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
395 const char *xpath_fmt
, ...)
398 char xpath
[XPATH_MAXLEN
];
400 struct lyd_node
*dnode_ret
= NULL
;
402 va_start(ap
, xpath_fmt
);
403 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
406 set
= lyd_find_path(dnode
, xpath
);
408 if (set
->number
== 0)
411 if (set
->number
> 1) {
412 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND
,
413 "%s: found %u elements (expected 0 or 1) [xpath %s]",
414 __func__
, set
->number
, xpath
);
418 dnode_ret
= set
->set
.d
[0];
426 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath_fmt
, ...)
429 char xpath
[XPATH_MAXLEN
];
433 va_start(ap
, xpath_fmt
);
434 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
437 set
= lyd_find_path(dnode
, xpath
);
439 found
= (set
->number
> 0);
445 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath_fmt
,
448 struct lys_node
*snode
;
449 struct lys_node_leaf
*sleaf
;
450 struct lys_node_container
*scontainer
;
454 char xpath
[XPATH_MAXLEN
];
456 va_start(ap
, xpath_fmt
);
457 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
460 dnode
= yang_dnode_get(dnode
, xpath
);
464 snode
= dnode
->schema
;
465 switch (snode
->nodetype
) {
467 sleaf
= (struct lys_node_leaf
*)snode
;
468 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
470 return lyd_wd_default((struct lyd_node_leaf_list
*)dnode
);
472 /* TODO: check leaf-list default values */
475 scontainer
= (struct lys_node_container
*)snode
;
476 if (scontainer
->presence
)
484 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
486 struct lys_node
*snode
;
487 struct lyd_node
*root
, *next
, *dnode_iter
;
489 snode
= dnode
->schema
;
490 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
491 return yang_dnode_is_default(dnode
, NULL
);
493 if (!yang_dnode_is_default(dnode
, NULL
))
496 LY_TREE_FOR (dnode
->child
, root
) {
497 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
498 if (!yang_dnode_is_default(dnode_iter
, NULL
))
501 LY_TREE_DFS_END(root
, next
, dnode_iter
);
508 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
510 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
511 lyd_change_leaf((struct lyd_node_leaf_list
*)dnode
, value
);
514 void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
)
516 assert(CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
));
517 lyd_set_private(dnode
, entry
);
520 void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
521 bool abort_if_not_found
)
523 const struct lyd_node
*orig_dnode
= dnode
;
524 char xpath
[XPATH_MAXLEN
];
527 switch (dnode
->schema
->nodetype
) {
537 dnode
= dnode
->parent
;
540 if (!abort_if_not_found
)
543 yang_dnode_get_path(orig_dnode
, xpath
, sizeof(xpath
));
544 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
545 "%s: failed to find entry [xpath %s]", __func__
, xpath
);
546 zlog_backtrace(LOG_ERR
);
550 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
552 struct lyd_node
*dnode
;
556 options
= LYD_OPT_CONFIG
;
558 options
= LYD_OPT_DATA
| LYD_OPT_DATA_NO_YANGLIB
;
561 if (lyd_validate(&dnode
, options
, ly_ctx
) != 0) {
562 /* Should never happen. */
563 flog_err(EC_LIB_LIBYANG
, "%s: lyd_validate() failed", __func__
);
570 struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
)
572 return lyd_dup_withsiblings(dnode
, 1);
575 void yang_dnode_free(struct lyd_node
*dnode
)
577 while (dnode
->parent
)
578 dnode
= dnode
->parent
;
579 lyd_free_withsiblings(dnode
);
582 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
584 struct yang_data
*data
;
586 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
587 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
589 data
->value
= strdup(value
);
594 void yang_data_free(struct yang_data
*data
)
598 XFREE(MTYPE_YANG_DATA
, data
);
601 struct list
*yang_data_list_new(void)
606 list
->del
= (void (*)(void *))yang_data_free
;
611 static void *ly_dup_cb(const void *priv
)
613 /* Make a shallow copy of the priv pointer. */
617 /* Make libyang log its errors using FRR logging infrastructure. */
618 static void ly_log_cb(LY_LOG_LEVEL level
, const char *msg
, const char *path
)
627 priority
= LOG_WARNING
;
630 priority
= LOG_DEBUG
;
637 zlog(priority
, "libyang: %s (%s)", msg
, path
);
639 zlog(priority
, "libyang: %s", msg
);
644 static char ly_plugin_dir
[PATH_MAX
];
645 const char *const *ly_loaded_plugins
;
646 const char *ly_plugin
;
647 bool found_ly_frr_types
= false;
649 /* Tell libyang where to find its plugins. */
650 snprintf(ly_plugin_dir
, sizeof(ly_plugin_dir
), "%s=%s",
651 "LIBYANG_USER_TYPES_PLUGINS_DIR", LIBYANG_PLUGINS_PATH
);
652 putenv(ly_plugin_dir
);
654 /* Initialize libyang global parameters that affect all containers. */
655 ly_set_log_clb(ly_log_cb
, 1);
656 ly_log_options(LY_LOLOG
| LY_LOSTORE
);
658 /* Initialize libyang container for native models. */
660 ly_ctx_new(YANG_MODELS_PATH
, LY_CTX_DISABLE_SEARCHDIR_CWD
);
661 if (!ly_native_ctx
) {
662 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
665 ly_ctx_set_module_imp_clb(ly_native_ctx
, yang_module_imp_clb
, NULL
);
666 ly_ctx_set_priv_dup_clb(ly_native_ctx
, ly_dup_cb
);
668 /* Detect if the required libyang plugin(s) were loaded successfully. */
669 ly_loaded_plugins
= ly_get_loaded_plugins();
670 for (size_t i
= 0; (ly_plugin
= ly_loaded_plugins
[i
]); i
++) {
671 if (strmatch(ly_plugin
, "frr_user_types")) {
672 found_ly_frr_types
= true;
676 if (!found_ly_frr_types
) {
677 flog_err(EC_LIB_LIBYANG_PLUGIN_LOAD
,
678 "%s: failed to load frr_user_types.so", __func__
);
682 yang_translator_init();
685 void yang_terminate(void)
687 struct yang_module
*module
;
689 yang_translator_terminate();
691 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
692 module
= RB_ROOT(yang_modules
, &yang_modules
);
695 * We shouldn't call ly_ctx_remove_module() here because this
696 * function also removes other modules that depend on it.
698 * ly_ctx_destroy() will release all memory for us.
700 RB_REMOVE(yang_modules
, &yang_modules
, module
);
701 XFREE(MTYPE_YANG_MODULE
, module
);
704 ly_ctx_destroy(ly_native_ctx
, NULL
);