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
[] = {
79 /* Generate the yang_modules tree. */
80 static inline int yang_module_compare(const struct yang_module
*a
,
81 const struct yang_module
*b
)
83 return strcmp(a
->name
, b
->name
);
85 RB_GENERATE(yang_modules
, yang_module
, entry
, yang_module_compare
)
87 struct yang_modules yang_modules
= RB_INITIALIZER(&yang_modules
);
89 struct yang_module
*yang_module_load(const char *module_name
)
91 struct yang_module
*module
;
92 const struct lys_module
*module_info
;
94 module_info
= ly_ctx_load_module(ly_native_ctx
, module_name
, NULL
);
96 flog_err(EC_LIB_YANG_MODULE_LOAD
,
97 "%s: failed to load data model: %s", __func__
,
102 module
= XCALLOC(MTYPE_YANG_MODULE
, sizeof(*module
));
103 module
->name
= module_name
;
104 module
->info
= module_info
;
106 if (RB_INSERT(yang_modules
, &yang_modules
, module
) != NULL
) {
107 flog_err(EC_LIB_YANG_MODULE_LOADED_ALREADY
,
108 "%s: YANG module is loaded already: %s", __func__
,
116 void yang_module_load_all(void)
118 for (size_t i
= 0; i
< array_size(frr_native_modules
); i
++)
119 yang_module_load(frr_native_modules
[i
]);
122 struct yang_module
*yang_module_find(const char *module_name
)
124 struct yang_module s
;
126 s
.name
= module_name
;
127 return RB_FIND(yang_modules
, &yang_modules
, &s
);
130 int yang_snodes_iterate_subtree(const struct lys_node
*snode
,
131 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
133 struct lys_node
*child
;
134 int ret
= YANG_ITER_CONTINUE
;
136 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_IMPLICIT
)) {
137 switch (snode
->nodetype
) {
141 if (CHECK_FLAG(snode
->flags
, LYS_IMPLICIT
))
149 switch (snode
->nodetype
) {
151 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_NPCONTAINERS
)) {
152 struct lys_node_container
*scontainer
;
154 scontainer
= (struct lys_node_container
*)snode
;
155 if (!scontainer
->presence
)
160 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_LIST_KEYS
)) {
161 struct lys_node_leaf
*sleaf
;
163 /* Ignore list keys. */
164 sleaf
= (struct lys_node_leaf
*)snode
;
165 if (lys_is_key(sleaf
, NULL
))
170 /* Return since we're not interested in the grouping subtree. */
171 return YANG_ITER_CONTINUE
;
174 /* Always ignore nodes of these types. */
178 if (CHECK_FLAG(flags
, YANG_ITER_FILTER_INPUT_OUTPUT
))
185 ret
= (*cb
)(snode
, arg
);
186 if (ret
== YANG_ITER_STOP
)
191 * YANG leafs and leaf-lists can't have child nodes, and trying to
192 * access snode->child is undefined behavior.
194 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
195 return YANG_ITER_CONTINUE
;
197 LY_TREE_FOR (snode
->child
, child
) {
198 if (!CHECK_FLAG(flags
, YANG_ITER_ALLOW_AUGMENTATIONS
)
199 && child
->parent
!= snode
)
202 ret
= yang_snodes_iterate_subtree(child
, cb
, flags
, arg
);
203 if (ret
== YANG_ITER_STOP
)
210 int yang_snodes_iterate_module(const struct lys_module
*module
,
211 yang_iterate_cb cb
, uint16_t flags
, void *arg
)
213 struct lys_node
*snode
;
214 int ret
= YANG_ITER_CONTINUE
;
216 LY_TREE_FOR (module
->data
, snode
) {
217 ret
= yang_snodes_iterate_subtree(snode
, cb
, flags
, arg
);
218 if (ret
== YANG_ITER_STOP
)
222 for (uint8_t i
= 0; i
< module
->augment_size
; i
++) {
223 ret
= yang_snodes_iterate_subtree(
224 (const struct lys_node
*)&module
->augment
[i
], cb
, flags
,
226 if (ret
== YANG_ITER_STOP
)
233 int yang_snodes_iterate_all(yang_iterate_cb cb
, uint16_t flags
, void *arg
)
235 struct yang_module
*module
;
236 int ret
= YANG_ITER_CONTINUE
;
238 RB_FOREACH (module
, yang_modules
, &yang_modules
) {
239 ret
= yang_snodes_iterate_module(module
->info
, cb
, flags
, arg
);
240 if (ret
== YANG_ITER_STOP
)
247 void yang_snode_get_path(const struct lys_node
*snode
, enum yang_path_type type
,
248 char *xpath
, size_t xpath_len
)
253 case YANG_PATH_SCHEMA
:
254 xpath_ptr
= lys_path(snode
, 0);
257 xpath_ptr
= lys_data_path(snode
);
260 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown yang path type: %u",
264 strlcpy(xpath
, xpath_ptr
, xpath_len
);
268 struct lys_node
*yang_snode_real_parent(const struct lys_node
*snode
)
270 struct lys_node
*parent
= snode
->parent
;
273 struct lys_node_container
*scontainer
;
275 switch (parent
->nodetype
) {
277 scontainer
= (struct lys_node_container
*)parent
;
278 if (scontainer
->presence
)
286 parent
= parent
->parent
;
292 struct lys_node
*yang_snode_parent_list(const struct lys_node
*snode
)
294 struct lys_node
*parent
= snode
->parent
;
297 switch (parent
->nodetype
) {
303 parent
= parent
->parent
;
309 bool yang_snode_is_typeless_data(const struct lys_node
*snode
)
311 struct lys_node_leaf
*sleaf
;
313 switch (snode
->nodetype
) {
315 sleaf
= (struct lys_node_leaf
*)snode
;
316 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
326 const char *yang_snode_get_default(const struct lys_node
*snode
)
328 struct lys_node_leaf
*sleaf
;
330 switch (snode
->nodetype
) {
332 sleaf
= (struct lys_node_leaf
*)snode
;
334 /* NOTE: this might be null. */
337 /* TODO: check leaf-list default values */
344 const struct lys_type
*yang_snode_get_type(const struct lys_node
*snode
)
346 struct lys_node_leaf
*sleaf
= (struct lys_node_leaf
*)snode
;
347 struct lys_type
*type
;
349 if (!CHECK_FLAG(sleaf
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
353 while (type
->base
== LY_TYPE_LEAFREF
)
354 type
= &type
->info
.lref
.target
->type
;
359 void yang_dnode_get_path(const struct lyd_node
*dnode
, char *xpath
,
364 xpath_ptr
= lyd_path(dnode
);
365 strlcpy(xpath
, xpath_ptr
, xpath_len
);
369 const char *yang_dnode_get_schema_name(const struct lyd_node
*dnode
,
370 const char *xpath_fmt
, ...)
374 char xpath
[XPATH_MAXLEN
];
376 va_start(ap
, xpath_fmt
);
377 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
380 dnode
= yang_dnode_get(dnode
, xpath
);
382 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
383 "%s: couldn't find %s", __func__
, xpath
);
384 zlog_backtrace(LOG_ERR
);
389 return dnode
->schema
->name
;
392 struct lyd_node
*yang_dnode_get(const struct lyd_node
*dnode
,
393 const char *xpath_fmt
, ...)
396 char xpath
[XPATH_MAXLEN
];
398 struct lyd_node
*dnode_ret
= NULL
;
400 va_start(ap
, xpath_fmt
);
401 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
404 set
= lyd_find_path(dnode
, xpath
);
406 if (set
->number
== 0)
409 if (set
->number
> 1) {
410 flog_warn(EC_LIB_YANG_DNODE_NOT_FOUND
,
411 "%s: found %u elements (expected 0 or 1) [xpath %s]",
412 __func__
, set
->number
, xpath
);
416 dnode_ret
= set
->set
.d
[0];
424 bool yang_dnode_exists(const struct lyd_node
*dnode
, const char *xpath_fmt
, ...)
427 char xpath
[XPATH_MAXLEN
];
431 va_start(ap
, xpath_fmt
);
432 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
435 set
= lyd_find_path(dnode
, xpath
);
437 found
= (set
->number
> 0);
443 bool yang_dnode_is_default(const struct lyd_node
*dnode
, const char *xpath_fmt
,
446 struct lys_node
*snode
;
447 struct lys_node_leaf
*sleaf
;
448 struct lys_node_container
*scontainer
;
452 char xpath
[XPATH_MAXLEN
];
454 va_start(ap
, xpath_fmt
);
455 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
458 dnode
= yang_dnode_get(dnode
, xpath
);
462 snode
= dnode
->schema
;
463 switch (snode
->nodetype
) {
465 sleaf
= (struct lys_node_leaf
*)snode
;
466 if (sleaf
->type
.base
== LY_TYPE_EMPTY
)
468 return lyd_wd_default((struct lyd_node_leaf_list
*)dnode
);
470 /* TODO: check leaf-list default values */
473 scontainer
= (struct lys_node_container
*)snode
;
474 if (scontainer
->presence
)
482 bool yang_dnode_is_default_recursive(const struct lyd_node
*dnode
)
484 struct lys_node
*snode
;
485 struct lyd_node
*root
, *next
, *dnode_iter
;
487 snode
= dnode
->schema
;
488 if (CHECK_FLAG(snode
->nodetype
, LYS_LEAF
| LYS_LEAFLIST
))
489 return yang_dnode_is_default(dnode
, NULL
);
491 if (!yang_dnode_is_default(dnode
, NULL
))
494 LY_TREE_FOR (dnode
->child
, root
) {
495 LY_TREE_DFS_BEGIN (root
, next
, dnode_iter
) {
496 if (!yang_dnode_is_default(dnode_iter
, NULL
))
499 LY_TREE_DFS_END(root
, next
, dnode_iter
);
506 void yang_dnode_change_leaf(struct lyd_node
*dnode
, const char *value
)
508 assert(dnode
->schema
->nodetype
== LYS_LEAF
);
509 lyd_change_leaf((struct lyd_node_leaf_list
*)dnode
, value
);
512 void yang_dnode_set_entry(const struct lyd_node
*dnode
, void *entry
)
514 assert(CHECK_FLAG(dnode
->schema
->nodetype
, LYS_LIST
| LYS_CONTAINER
));
515 lyd_set_private(dnode
, entry
);
518 void *yang_dnode_get_entry(const struct lyd_node
*dnode
,
519 bool abort_if_not_found
)
521 const struct lyd_node
*orig_dnode
= dnode
;
522 char xpath
[XPATH_MAXLEN
];
525 switch (dnode
->schema
->nodetype
) {
535 dnode
= dnode
->parent
;
538 if (!abort_if_not_found
)
541 yang_dnode_get_path(orig_dnode
, xpath
, sizeof(xpath
));
542 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND
,
543 "%s: failed to find entry [xpath %s]", __func__
, xpath
);
544 zlog_backtrace(LOG_ERR
);
548 struct lyd_node
*yang_dnode_new(struct ly_ctx
*ly_ctx
, bool config_only
)
550 struct lyd_node
*dnode
;
554 options
= LYD_OPT_CONFIG
;
556 options
= LYD_OPT_DATA
| LYD_OPT_DATA_NO_YANGLIB
;
559 if (lyd_validate(&dnode
, options
, ly_ctx
) != 0) {
560 /* Should never happen. */
561 flog_err(EC_LIB_LIBYANG
, "%s: lyd_validate() failed", __func__
);
568 struct lyd_node
*yang_dnode_dup(const struct lyd_node
*dnode
)
570 return lyd_dup_withsiblings(dnode
, 1);
573 void yang_dnode_free(struct lyd_node
*dnode
)
575 while (dnode
->parent
)
576 dnode
= dnode
->parent
;
577 lyd_free_withsiblings(dnode
);
580 struct yang_data
*yang_data_new(const char *xpath
, const char *value
)
582 struct yang_data
*data
;
584 data
= XCALLOC(MTYPE_YANG_DATA
, sizeof(*data
));
585 strlcpy(data
->xpath
, xpath
, sizeof(data
->xpath
));
587 data
->value
= strdup(value
);
592 void yang_data_free(struct yang_data
*data
)
596 XFREE(MTYPE_YANG_DATA
, data
);
599 struct list
*yang_data_list_new(void)
604 list
->del
= (void (*)(void *))yang_data_free
;
609 static void *ly_dup_cb(const void *priv
)
611 /* Make a shallow copy of the priv pointer. */
615 /* Make libyang log its errors using FRR logging infrastructure. */
616 static void ly_log_cb(LY_LOG_LEVEL level
, const char *msg
, const char *path
)
625 priority
= LOG_WARNING
;
628 priority
= LOG_DEBUG
;
635 zlog(priority
, "libyang: %s (%s)", msg
, path
);
637 zlog(priority
, "libyang: %s", msg
);
642 static char ly_plugin_dir
[PATH_MAX
];
643 const char *const *ly_loaded_plugins
;
644 const char *ly_plugin
;
645 bool found_ly_frr_types
= false;
647 /* Tell libyang where to find its plugins. */
648 snprintf(ly_plugin_dir
, sizeof(ly_plugin_dir
), "%s=%s",
649 "LIBYANG_USER_TYPES_PLUGINS_DIR", LIBYANG_PLUGINS_PATH
);
650 putenv(ly_plugin_dir
);
652 /* Initialize libyang global parameters that affect all containers. */
653 ly_set_log_clb(ly_log_cb
, 1);
654 ly_log_options(LY_LOLOG
| LY_LOSTORE
);
656 /* Initialize libyang container for native models. */
658 ly_ctx_new(YANG_MODELS_PATH
, LY_CTX_DISABLE_SEARCHDIR_CWD
);
659 if (!ly_native_ctx
) {
660 flog_err(EC_LIB_LIBYANG
, "%s: ly_ctx_new() failed", __func__
);
663 ly_ctx_set_module_imp_clb(ly_native_ctx
, yang_module_imp_clb
, NULL
);
664 ly_ctx_set_priv_dup_clb(ly_native_ctx
, ly_dup_cb
);
666 /* Detect if the required libyang plugin(s) were loaded successfully. */
667 ly_loaded_plugins
= ly_get_loaded_plugins();
668 for (size_t i
= 0; (ly_plugin
= ly_loaded_plugins
[i
]); i
++) {
669 if (strmatch(ly_plugin
, "frr_user_types")) {
670 found_ly_frr_types
= true;
674 if (!found_ly_frr_types
) {
675 flog_err(EC_LIB_LIBYANG_PLUGIN_LOAD
,
676 "%s: failed to load frr_user_types.so", __func__
);
680 yang_translator_init();
683 void yang_terminate(void)
685 struct yang_module
*module
;
687 yang_translator_terminate();
689 while (!RB_EMPTY(yang_modules
, &yang_modules
)) {
690 module
= RB_ROOT(yang_modules
, &yang_modules
);
693 * We shouldn't call ly_ctx_remove_module() here because this
694 * function also removes other modules that depend on it.
696 * ly_ctx_destroy() will release all memory for us.
698 RB_REMOVE(yang_modules
, &yang_modules
, module
);
699 XFREE(MTYPE_YANG_MODULE
, module
);
702 ly_ctx_destroy(ly_native_ctx
, NULL
);