From 3f662078965405c6363dcecc2cc43c658c108229 Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Sat, 24 Nov 2018 21:56:48 -0200 Subject: [PATCH] lib: introduce function to retrieve the schema name of a data node In some cases it might be desirable to obtain the schema name of a libyang data node. Introduce the yang_dnode_get_schema_name() function for this purpose. Signed-off-by: Renato Westphal --- lib/yang.c | 23 +++++++++++++++++++++++ lib/yang.h | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/yang.c b/lib/yang.c index 6236dc5a66..f41c645758 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -344,6 +344,29 @@ void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath, free(xpath_ptr); } +const char *yang_dnode_get_schema_name(const struct lyd_node *dnode, + const char *xpath_fmt, ...) +{ + if (xpath_fmt) { + va_list ap; + char xpath[XPATH_MAXLEN]; + + va_start(ap, xpath_fmt); + vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); + va_end(ap); + + dnode = yang_dnode_get(dnode, xpath); + if (!dnode) { + flog_err(EC_LIB_YANG_DNODE_NOT_FOUND, + "%s: couldn't find %s", __func__, xpath); + zlog_backtrace(LOG_ERR); + abort(); + } + } + + return dnode->schema->name; +} + struct lyd_node *yang_dnode_get(const struct lyd_node *dnode, const char *xpath_fmt, ...) { diff --git a/lib/yang.h b/lib/yang.h index b0348e320b..eac9796df9 100644 --- a/lib/yang.h +++ b/lib/yang.h @@ -284,6 +284,22 @@ extern const struct lys_type *yang_snode_get_type(const struct lys_node *snode); extern void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath, size_t xpath_len); +/* + * Return the schema name of the given libyang data node. + * + * dnode + * libyang data node. + * + * xpath_fmt + * Optional XPath expression (absolute or relative) to specify a different + * data node to operate on in the same data tree. + * + * Returns: + * Schema name of the libyang data node. + */ +extern const char *yang_dnode_get_schema_name(const struct lyd_node *dnode, + const char *xpath_fmt, ...); + /* * Find a libyang data node by its YANG data path. * -- 2.39.5