diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:55:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:55:11 +0000 |
commit | cd07912073c951b4bbb871ed2653af1be2cfc714 (patch) | |
tree | 1073c2308492e6aea4c66cb7436ee92db2abfd42 /src/plugins_exts | |
parent | Initial commit. (diff) | |
download | libyang2-upstream.tar.xz libyang2-upstream.zip |
Adding upstream version 2.1.30.upstream/2.1.30upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/plugins_exts.c | 680 | ||||
-rw-r--r-- | src/plugins_exts.h | 1048 | ||||
-rw-r--r-- | src/plugins_exts/metadata.c | 243 | ||||
-rw-r--r-- | src/plugins_exts/metadata.h | 66 | ||||
-rw-r--r-- | src/plugins_exts/nacm.c | 223 | ||||
-rw-r--r-- | src/plugins_exts/schema_mount.c | 1332 | ||||
-rw-r--r-- | src/plugins_exts/structure.c | 558 | ||||
-rw-r--r-- | src/plugins_exts/yangdata.c | 277 |
8 files changed, 4427 insertions, 0 deletions
diff --git a/src/plugins_exts.c b/src/plugins_exts.c new file mode 100644 index 0000000..00970fa --- /dev/null +++ b/src/plugins_exts.c @@ -0,0 +1,680 @@ +/** + * @file plugins_exts.c + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief helper functions for extension plugins + * + * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include "plugins_exts.h" + +#include <assert.h> +#include <stdint.h> +#include <stdlib.h> + +#include "common.h" +#include "dict.h" +#include "parser_internal.h" +#include "printer_internal.h" +#include "schema_compile.h" +#include "schema_compile_amend.h" +#include "schema_compile_node.h" +#include "schema_features.h" +#include "tree_schema_internal.h" + +LIBYANG_API_DEF const struct lysp_module * +lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx) +{ + return PARSER_CUR_PMOD(pctx); +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_parse_extension_instance(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + LY_ERR rc = LY_SUCCESS; + LY_ARRAY_COUNT_TYPE u; + struct lysp_stmt *stmt; + + /* check for invalid substatements */ + LY_LIST_FOR(ext->child, stmt) { + if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { + continue; + } + LY_ARRAY_FOR(ext->substmts, u) { + if (ext->substmts[u].stmt == stmt->kw) { + break; + } + } + if (u == LY_ARRAY_COUNT(ext->substmts)) { + LOGVAL(PARSER_CTX(pctx), LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", + stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); + rc = LY_EVALID; + goto cleanup; + } + } + + /* parse all the known statements */ + LY_ARRAY_FOR(ext->substmts, u) { + LY_LIST_FOR(ext->child, stmt) { + if (ext->substmts[u].stmt != stmt->kw) { + continue; + } + + if ((rc = lys_parse_ext_instance_stmt(pctx, &ext->substmts[u], stmt))) { + goto cleanup; + } + } + } + +cleanup: + return rc; +} + +/** + * @brief Compile an instance extension statement. + * + * @param[in] ctx Compile context. + * @param[in] parsed Parsed ext instance substatement structure. + * @param[in] ext Compiled ext instance. + * @param[in] substmt Compled ext instance substatement info. + * @return LY_ERR value. + */ +static LY_ERR +lys_compile_ext_instance_stmt(struct lysc_ctx *ctx, const void *parsed, struct lysc_ext_instance *ext, + struct lysc_ext_substmt *substmt) +{ + LY_ERR rc = LY_SUCCESS; + ly_bool length_restr = 0; + LY_DATA_TYPE basetype; + + /* compilation wthout any storage */ + if (substmt->stmt == LY_STMT_IF_FEATURE) { + ly_bool enabled; + + /* evaluate */ + LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, parsed, &enabled), cleanup); + if (!enabled) { + /* it is disabled, remove the whole extension instance */ + rc = LY_ENOT; + } + } + + if (!substmt->storage || !parsed) { + /* nothing to store or nothing parsed to compile */ + goto cleanup; + } + + switch (substmt->stmt) { + case LY_STMT_NOTIFICATION: + case LY_STMT_INPUT: + case LY_STMT_OUTPUT: + case LY_STMT_ACTION: + case LY_STMT_RPC: + case LY_STMT_ANYDATA: + case LY_STMT_ANYXML: + case LY_STMT_CASE: + case LY_STMT_CHOICE: + case LY_STMT_CONTAINER: + case LY_STMT_LEAF: + case LY_STMT_LEAF_LIST: + case LY_STMT_LIST: + case LY_STMT_USES: { + const uint16_t flags; + struct lysp_node *pnodes, *pnode; + struct lysc_node *node; + + lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); + pnodes = (struct lysp_node *)parsed; + + /* compile nodes */ + LY_LIST_FOR(pnodes, pnode) { + if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) { + /* manual compile */ + node = calloc(1, sizeof(struct lysc_node_action_inout)); + LY_CHECK_ERR_GOTO(!node, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup); + LY_CHECK_GOTO(rc = lys_compile_node_action_inout(ctx, pnode, node), cleanup); + LY_CHECK_GOTO(rc = lys_compile_node_connect(ctx, NULL, node), cleanup); + } else { + /* ctx->ext substatement storage is used as the document root */ + LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, NULL, flags, NULL), cleanup); + } + } + break; + } + case LY_STMT_ARGUMENT: + case LY_STMT_CONTACT: + case LY_STMT_DESCRIPTION: + case LY_STMT_ERROR_APP_TAG: + case LY_STMT_ERROR_MESSAGE: + case LY_STMT_KEY: + case LY_STMT_MODIFIER: + case LY_STMT_NAMESPACE: + case LY_STMT_ORGANIZATION: + case LY_STMT_PRESENCE: + case LY_STMT_REFERENCE: + case LY_STMT_UNITS: + /* just make a copy */ + LY_CHECK_GOTO(rc = lydict_insert(ctx->ctx, parsed, 0, substmt->storage), cleanup); + break; + + case LY_STMT_BIT: + basetype = LY_TYPE_BITS; + /* fallthrough */ + case LY_STMT_ENUM: + if (substmt->stmt == LY_STMT_ENUM) { + basetype = LY_TYPE_ENUM; + } + + /* compile */ + LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, parsed, basetype, NULL, substmt->storage), cleanup); + break; + + case LY_STMT_CONFIG: { + uint16_t flags; + + if (!(ctx->compile_opts & LYS_COMPILE_NO_CONFIG)) { + memcpy(&flags, &parsed, 2); + if (flags & LYS_CONFIG_MASK) { + /* explicitly set */ + flags |= LYS_SET_CONFIG; + } else if (ext->parent_stmt & LY_STMT_DATA_NODE_MASK) { + /* inherit */ + flags = ((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_MASK; + } else { + /* default config */ + flags = LYS_CONFIG_W; + } + memcpy(substmt->storage, &flags, 2); + } /* else leave zero */ + break; + } + case LY_STMT_MUST: { + const struct lysp_restr *restrs = parsed; + + /* sized array */ + COMPILE_ARRAY_GOTO(ctx, restrs, *(struct lysc_must **)substmt->storage, lys_compile_must, rc, cleanup); + break; + } + case LY_STMT_WHEN: { + const uint16_t flags; + const struct lysp_when *when = parsed; + + /* read compiled status */ + lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); + + /* compile */ + LY_CHECK_GOTO(rc = lys_compile_when(ctx, when, flags, NULL, NULL, NULL, substmt->storage), cleanup); + break; + } + case LY_STMT_FRACTION_DIGITS: + case LY_STMT_REQUIRE_INSTANCE: + /* just make a copy */ + memcpy(substmt->storage, &parsed, 1); + break; + + case LY_STMT_MANDATORY: + case LY_STMT_ORDERED_BY: + case LY_STMT_STATUS: + /* just make a copy */ + memcpy(substmt->storage, &parsed, 2); + break; + + case LY_STMT_MAX_ELEMENTS: + case LY_STMT_MIN_ELEMENTS: + /* just make a copy */ + memcpy(substmt->storage, &parsed, 4); + break; + + case LY_STMT_POSITION: + case LY_STMT_VALUE: + /* just make a copy */ + memcpy(substmt->storage, &parsed, 8); + break; + + case LY_STMT_IDENTITY: + /* compile */ + LY_CHECK_GOTO(rc = lys_identity_precompile(ctx, NULL, NULL, parsed, substmt->storage), cleanup); + break; + + case LY_STMT_LENGTH: + length_restr = 1; + /* fallthrough */ + case LY_STMT_RANGE: + /* compile, use uint64 default range */ + LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, parsed, LY_TYPE_UINT64, length_restr, 0, NULL, substmt->storage), + cleanup); + break; + + case LY_STMT_PATTERN: + /* compile */ + LY_CHECK_GOTO(rc = lys_compile_type_patterns(ctx, parsed, NULL, substmt->storage), cleanup); + break; + + case LY_STMT_TYPE: { + const uint16_t flags; + const char *units; + const struct lysp_type *ptype = parsed; + + /* read compiled info */ + lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); + lyplg_ext_get_storage(ext, LY_STMT_UNITS, sizeof units, (const void **)&units); + + /* compile */ + LY_CHECK_GOTO(rc = lys_compile_type(ctx, NULL, flags, ext->def->name, ptype, substmt->storage, &units, NULL), cleanup); + break; + } + case LY_STMT_EXTENSION_INSTANCE: { + struct lysp_ext_instance *extps = (struct lysp_ext_instance *)parsed; + + /* compile sized array */ + COMPILE_EXTS_GOTO(ctx, extps, *(struct lysc_ext_instance **)substmt->storage, ext, rc, cleanup); + break; + } + case LY_STMT_AUGMENT: + case LY_STMT_GROUPING: + case LY_STMT_BASE: + case LY_STMT_BELONGS_TO: + case LY_STMT_DEFAULT: + case LY_STMT_DEVIATE: + case LY_STMT_DEVIATION: + case LY_STMT_EXTENSION: + case LY_STMT_FEATURE: + case LY_STMT_IF_FEATURE: + case LY_STMT_IMPORT: + case LY_STMT_INCLUDE: + case LY_STMT_MODULE: + case LY_STMT_PATH: + case LY_STMT_PREFIX: + case LY_STMT_REFINE: + case LY_STMT_REVISION: + case LY_STMT_REVISION_DATE: + case LY_STMT_SUBMODULE: + case LY_STMT_TYPEDEF: + case LY_STMT_UNIQUE: + case LY_STMT_YANG_VERSION: + case LY_STMT_YIN_ELEMENT: + LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" compilation is not supported.", lyplg_ext_stmt2str(substmt->stmt)); + rc = LY_EVALID; + goto cleanup; + + default: + LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension " + "(found in \"%s%s%s\") substatement.", lyplg_ext_stmt2str(substmt->stmt), ext->def->name, + ext->argument ? " " : "", ext->argument ? ext->argument : ""); + rc = LY_EVALID; + goto cleanup; + } + +cleanup: + return rc; +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp, + struct lysc_ext_instance *ext) +{ + LY_ERR rc = LY_SUCCESS; + LY_ARRAY_COUNT_TYPE u, v; + enum ly_stmt stmtp; + const void *storagep; + struct ly_set storagep_compiled = {0}; + + LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, extp, ext, LY_EINVAL); + + /* note into the compile context that we are processing extension now */ + ctx->ext = ext; + + LY_ARRAY_FOR(extp->substmts, u) { + stmtp = extp->substmts[u].stmt; + storagep = *(void **)extp->substmts[u].storage; + + if (ly_set_contains(&storagep_compiled, storagep, NULL)) { + /* this parsed statement has already been compiled (for example, if it is a linked list of parsed nodes) */ + continue; + } + + LY_ARRAY_FOR(ext->substmts, v) { + if (stmtp != ext->substmts[v].stmt) { + continue; + } + + if ((rc = lys_compile_ext_instance_stmt(ctx, storagep, ext, &ext->substmts[v]))) { + goto cleanup; + } + + /* parsed substatement compiled */ + break; + } + + /* compiled */ + ly_set_add(&storagep_compiled, storagep, 1, NULL); + } + +cleanup: + ctx->ext = NULL; + ly_set_erase(&storagep_compiled, NULL); + return rc; +} + +LIBYANG_API_DEF struct ly_ctx * +lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx) +{ + return ctx->ctx; +} + +LIBYANG_API_DEF uint32_t * +lyplg_ext_compile_get_options(const struct lysc_ctx *ctx) +{ + return &((struct lysc_ctx *)ctx)->compile_opts; +} + +LIBYANG_API_DEF const struct lys_module * +lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx) +{ + return ctx->cur_mod; +} + +LIBYANG_API_DEF struct lysp_module * +lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx) +{ + return ctx->pmod; +} + +LIBYANG_API_DEF struct ly_out ** +lyplg_ext_print_get_out(const struct lyspr_ctx *ctx) +{ + return &((struct lyspr_ctx *)ctx)->out; +} + +LIBYANG_API_DEF uint32_t * +lyplg_ext_print_get_options(const struct lyspr_ctx *ctx) +{ + return &((struct lyspr_ctx *)ctx)->options; +} + +LIBYANG_API_DEF uint16_t * +lyplg_ext_print_get_level(const struct lyspr_ctx *ctx) +{ + return &((struct lyspr_ctx *)ctx)->level; +} + +LIBYANG_API_DECL LY_ERR +lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_ext_instance *ext, + lyplg_ext_sprinter_ctree_override_clb clb) +{ + LY_ERR rc = LY_SUCCESS; + uint32_t i; + struct lysc_node *schema; + + LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL); + + LY_ARRAY_FOR(ext->substmts, i) { + switch (ext->substmts[i].stmt) { + case LY_STMT_NOTIFICATION: + case LY_STMT_INPUT: + case LY_STMT_OUTPUT: + case LY_STMT_ACTION: + case LY_STMT_RPC: + case LY_STMT_ANYDATA: + case LY_STMT_ANYXML: + case LY_STMT_CASE: + case LY_STMT_CHOICE: + case LY_STMT_CONTAINER: + case LY_STMT_LEAF: + case LY_STMT_LEAF_LIST: + case LY_STMT_LIST: + schema = *((struct lysc_node **)ext->substmts[i].storage); + if (schema) { + rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, schema, clb); + return rc; + } + default: + break; + } + } + + return rc; +} + +LIBYANG_API_DECL LY_ERR +lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_ext_instance *ext, + lyplg_ext_sprinter_ptree_override_clb clb) +{ + LY_ERR rc = LY_SUCCESS; + uint32_t i; + struct lysp_node *schema; + + LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL); + + LY_ARRAY_FOR(ext->substmts, i) { + switch (ext->substmts[i].stmt) { + case LY_STMT_NOTIFICATION: + case LY_STMT_INPUT: + case LY_STMT_OUTPUT: + case LY_STMT_ACTION: + case LY_STMT_RPC: + case LY_STMT_ANYDATA: + case LY_STMT_ANYXML: + case LY_STMT_CASE: + case LY_STMT_CHOICE: + case LY_STMT_CONTAINER: + case LY_STMT_LEAF: + case LY_STMT_LEAF_LIST: + case LY_STMT_LIST: + schema = *((struct lysp_node **)ext->substmts[i].storage); + if (schema) { + rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, schema, clb); + return rc; + } + default: + break; + } + } + + return rc; +} + +LIBYANG_API_DECL LY_ERR +lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes, + lyplg_ext_sprinter_ctree_override_clb clb) +{ + struct lyspr_tree_schema *new; + + LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); + + if (!nodes) { + return LY_SUCCESS; + } + + LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM); + new->compiled = 1; + new->ctree = nodes; + new->cn_overr = clb; + + return LY_SUCCESS; +} + +LIBYANG_API_DECL LY_ERR +lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes, + lyplg_ext_sprinter_ptree_override_clb clb) +{ + struct lyspr_tree_schema *new; + + LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); + + if (!nodes) { + return LY_SUCCESS; + } + + LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM); + new->compiled = 0; + new->ptree = nodes; + new->pn_overr = clb; + + return LY_SUCCESS; +} + +LIBYANG_API_DECL LY_ERR +lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, void (*free_clb)(void *plugin_priv)) +{ + LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); + + ((struct lyspr_tree_ctx *)ctx)->plugin_priv = plugin_priv; + ((struct lyspr_tree_ctx *)ctx)->free_plugin_priv = free_clb; + + return LY_SUCCESS; +} + +LIBYANG_API_DEF const char * +lyplg_ext_stmt2str(enum ly_stmt stmt) +{ + if (stmt == LY_STMT_EXTENSION_INSTANCE) { + return "extension instance"; + } else { + return lys_stmt_str(stmt); + } +} + +LIBYANG_API_DEF enum ly_stmt +lyplg_ext_nodetype2stmt(uint16_t nodetype) +{ + switch (nodetype) { + case LYS_CONTAINER: + return LY_STMT_CONTAINER; + case LYS_CHOICE: + return LY_STMT_CHOICE; + case LYS_LEAF: + return LY_STMT_LEAF; + case LYS_LEAFLIST: + return LY_STMT_LEAF_LIST; + case LYS_LIST: + return LY_STMT_LIST; + case LYS_ANYXML: + return LY_STMT_ANYXML; + case LYS_ANYDATA: + return LY_STMT_ANYDATA; + case LYS_CASE: + return LY_STMT_CASE; + case LYS_RPC: + return LY_STMT_RPC; + case LYS_ACTION: + return LY_STMT_ACTION; + case LYS_NOTIF: + return LY_STMT_NOTIFICATION; + case LYS_USES: + return LY_STMT_USES; + case LYS_INPUT: + return LY_STMT_INPUT; + case LYS_OUTPUT: + return LY_STMT_OUTPUT; + default: + return LY_STMT_NONE; + } +} + +LY_ERR +lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, const void ***storage_p) +{ + LY_ARRAY_COUNT_TYPE u; + enum ly_stmt match = 0; + + *storage_p = NULL; + + if (!(stmt & LY_STMT_NODE_MASK)) { + /* matching a non-node statement */ + match = stmt; + } + + LY_ARRAY_FOR(ext->substmts, u) { + if ((match && (ext->substmts[u].stmt == match)) || (!match && (ext->substmts[u].stmt & stmt))) { + *storage_p = ext->substmts[u].storage; + return LY_SUCCESS; + } + } + + return LY_ENOT; +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage) +{ + LY_ERR rc = LY_SUCCESS; + const void **s; + + /* get pointer to the storage, is set even on error */ + rc = lyplg_ext_get_storage_p(ext, stmt, &s); + + /* assign */ + if (s) { + memcpy(storage, s, storage_size); + } else { + memset(storage, 0, storage_size); + } + + return rc; +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage) +{ + LY_ARRAY_COUNT_TYPE u; + const struct lysp_ext_instance *extp = NULL; + enum ly_stmt match = 0; + const void **s = NULL; + + /* find the parsed ext instance */ + LY_ARRAY_FOR(ext->module->parsed->exts, u) { + extp = &ext->module->parsed->exts[u]; + + if (ext->def == extp->def->compiled) { + break; + } + extp = NULL; + } + assert(extp); + + if (!(stmt & LY_STMT_NODE_MASK)) { + /* matching a non-node statement */ + match = stmt; + } + + /* get the substatement */ + LY_ARRAY_FOR(extp->substmts, u) { + if ((match && (extp->substmts[u].stmt == match)) || (!match && (extp->substmts[u].stmt & stmt))) { + s = extp->substmts[u].storage; + break; + } + } + + /* assign */ + if (s) { + memcpy(storage, s, storage_size); + } else { + memset(storage, 0, storage_size); + } + + return LY_SUCCESS; +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, ly_bool *ext_data_free) +{ + LY_ERR rc; + + if (!ctx->ext_clb) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Failed to get extension data, no callback set."); + return LY_EINVAL; + } + + if ((rc = ctx->ext_clb(ext, ctx->ext_clb_data, ext_data, ext_data_free))) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Callback for getting ext data failed."); + } + return rc; +} diff --git a/src/plugins_exts.h b/src/plugins_exts.h new file mode 100644 index 0000000..f005391 --- /dev/null +++ b/src/plugins_exts.h @@ -0,0 +1,1048 @@ +/** + * @file plugins_exts.h + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief libyang support for YANG extensions implementation. + * + * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#ifndef LY_PLUGINS_EXTS_H_ +#define LY_PLUGINS_EXTS_H_ + +#include "log.h" +#include "parser_data.h" +#include "plugins.h" +#include "tree_data.h" +#include "tree_edit.h" +#include "tree_schema.h" + +struct ly_ctx; +struct ly_in; +struct lyd_node; +struct lysc_ctx; +struct lysc_ext_substmt; +struct lysp_ctx; +struct lyspr_ctx; +struct lyspr_tree_ctx; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @page howtoPluginsExtensions Extension Plugins + * + * Note that the part of the libyang API here is available only by including a separated `<libyang/plugins_exts.h>` header + * file. Also note that the extension plugins API is versioned separately from libyang itself, so backward incompatible + * changes can come even without changing libyang major version. + * + * YANG extensions are very complex. Usually only its description specifies how it is supposed to behave, what are the + * allowed substatements, their cardinality or if the standard YANG statements placed inside the extension differs somehow + * in their meaning or behavior. libyang provides the Extension plugins API to implement such extensions and add its support + * into libyang itself. However we tried our best, the API is not (and it cannot be) so universal and complete to cover all + * possibilities. There are definitely use cases which cannot be simply implemented only with this API. + * + * libyang implements 3 important extensions: [NACM](https://tools.ietf.org/html/rfc8341), [Metadata](@ref howtoDataMetadata) + * and [yang-data](@ref howtoDataYangdata). Despite the core implementation in all three cases is done via extension plugin + * API, also other parts of the libyang code had to be extended to cover complete scope of the extensions. + * + * We believe, that the API is capable to allow implementation of very wide range of YANG extensions. However, if you see + * limitations for the particular YANG extension, don't hesitate to contact the project developers to discuss all the + * options, including updating the API. + * + * The plugin's functionality is provided to libyang via a set of callbacks specified as an array of ::lyplg_ext_record + * structures using the ::LYPLG_EXTENSIONS macro. + * + * The most important callbacks are ::lyplg_ext.parse and ::lyplg_ext.compile. They are responsible for parsing and + * compiling extension instance. This should include validating all the substatements, their values, or placement of + * the extension instance itself. If needed, the processed data can be stored in some form into the compiled schema + * representation of the extension instance. To make this task as easy as possible, libyang provides several + * [parsing](@ref pluginsExtensionsParse) and [compilation](@ref pluginsExtensionsCompile) helper functions to process + * known YANG statements exactly as if they were standard YANG statements. + * + * The data validation callback ::lyplg_ext.validate is used for additional validation of a data nodes that contains the + * connected extension instance directly (as a substatement) or indirectly in case of terminal nodes via their type (no + * matter if the extension instance is placed directly in the leaf's/leaf-list's type or in the type of the referenced + * typedef). + * + * The ::lyplg_ext.printer_info callback implement printing the compiled extension instance data when the schema (module) is + * being printed in the ::LYS_OUT_YANG_COMPILED (info) format. As for compile callback, there are also + * [helper functions](@ref pluginsExtensionsSprinterInfo) to access printer's context and to print standard YANG statements + * placed in the extension instance by libyang itself. + * + * The ::lyplg_ext.printer_ctree and ::lyplg_ext.printer_ptree callbacks implement printing of YANG tree diagrams + * (RFC 8340) for extension instance data. These callbacks are called for extension instances that have + * parents of type ::LY_STMT_MODULE, ::LY_STMT_SUBMODULE. Or these callbacks are called if the printer_tree finds + * a compiled/parsed data-node containing an extension instance. The callbacks should then decide which nodes + * should be printed within the extension instance. In addition, it is possible to register additional callbacks + * to the printer_tree context to override the form of the each node in the extension instance. + * + * The last callback, ::lyplg_ext.cfree, is supposed to free all the data allocated by the ::lyplg_ext.compile callback. + * To free the data created by helper function ::lyplg_ext_compile_extension_instance(), the plugin can used + * ::lyplg_ext_cfree_instance_substatements(). + * + * The plugin information contains also the plugin identifier (::lyplg_type.id). This string can serve to identify the + * specific plugin responsible to storing data value. In case the user can recognize the id string, it can access the + * plugin specific data with the appropriate knowledge of its structure. + * + * Logging information from an extension plugin is possible via ::lyplg_ext_parse_log() and ::lyplg_ext_compile_log() functions. + */ + +/** + * @defgroup pluginsExtensions Plugins: Extensions + * + * Structures and functions to for libyang plugins implementing specific YANG extensions defined in YANG modules. For more + * information, see @ref howtoPluginsTypes. + * + * This part of libyang API is available by including `<libyang/plugins_ext.h>` header file. + * + * @{ + */ + +/** + * @brief Extensions API version + */ +#define LYPLG_EXT_API_VERSION 6 + +/** + * @brief Mask for an operation statement. + * + * This mask matches action and RPC. + */ +#define LY_STMT_OP_MASK (LY_STMT_ACTION | LY_STMT_RPC) + +/** + * @brief Mask for a data node statement. + * + * This mask matches anydata, anyxml, case, choice, container, leaf, leaf-list, and list. + */ +#define LY_STMT_DATA_NODE_MASK (LY_STMT_ANYDATA | LY_STMT_ANYXML | LY_STMT_CASE | LY_STMT_CHOICE | LY_STMT_CONTAINER |\ + LY_STMT_LEAF | LY_STMT_LEAF_LIST | LY_STMT_LIST) + +/** + * @brief Mask for a node statement. + * + * This mask matches notification, input, output, action, RPC, anydata, anyxml, augment, case, choice, container, + * grouping, leaf, leaf-list, list, and uses. + */ +#define LY_STMT_NODE_MASK 0xFFFF + +/** + * @brief List of YANG statements + * + * Their description mentions what types are stored for each statement. Note that extension instance storage + * always stores a pointer to the type, not the type itself. + */ +enum ly_stmt { + LY_STMT_NONE = 0, + + LY_STMT_NOTIFICATION = 0x0001, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_notif *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_notif *` */ + LY_STMT_INPUT = 0x0002, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action_inout *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action_inout *` */ + LY_STMT_OUTPUT = 0x0004, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action_inout *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action_inout *` */ + LY_STMT_ACTION = 0x0008, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action *` */ + LY_STMT_RPC = 0x0010, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action *` */ + LY_STMT_ANYDATA = 0x0020, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_anydata *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_anydata *` */ + LY_STMT_ANYXML = 0x0040, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_anydata *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_anydata *` */ + LY_STMT_AUGMENT = 0x0080, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_augment *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_CASE = 0x0100, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_case *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_case *` */ + LY_STMT_CHOICE = 0x0200, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_choice *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_choice *` */ + LY_STMT_CONTAINER = 0x0400, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_container *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_container *` */ + LY_STMT_GROUPING = 0x0800, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_grp *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_LEAF = 0x1000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_leaf *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_leaf *` */ + LY_STMT_LEAF_LIST = 0x2000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_leaflist *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_leaflist *` */ + LY_STMT_LIST = 0x4000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_list *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_list *` */ + LY_STMT_USES = 0x8000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_uses *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node *` */ + + LY_STMT_ARGUMENT = 0x10000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_ext *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_ext *` */ + LY_STMT_BASE = 0x20000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char **`[] + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_ident *` */ + LY_STMT_BELONGS_TO = 0x30000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_submodule *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_BIT = 0x40000, /**< ::lysp_ext_substmt.storage - `struct lysp_type_enum *`[] + ::lysp_ext_instance.parent - `struct lysp_type_enum *` + ::lysc_ext_substmt.storage - `struct lysc_type_bitenum_item *`[] + ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */ + LY_STMT_CONFIG = 0x50000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *` + ::lysc_ext_substmt.storage - `uint16_t *` + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_CONTACT = 0x60000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_(sub)module *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_DEFAULT = 0x70000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node *`, `struct lysc_type *` (typedef) */ + LY_STMT_DESCRIPTION = 0x80000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - compiled parent statement */ + LY_STMT_DEVIATE = 0x90000, /**< ::lysp_ext_substmt.storage - `struct lysp_deviate *`[] + ::lysp_ext_instance.parent - `struct lysp_deviate *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_DEVIATION = 0xA0000, /**< ::lysp_ext_substmt.storage - `struct lysp_deviation *`[] + ::lysp_ext_instance.parent - `struct lysp_deviation *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_ENUM = 0xB0000, /**< ::lysp_ext_substmt.storage - `struct lysp_type_enum *`[] + ::lysp_ext_instance.parent - `struct lysp_type_enum *` + ::lysc_ext_substmt.storage - `struct lysc_type_bitenum_item *`[] + ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */ + LY_STMT_ERROR_APP_TAG = 0xC0000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - compiled restriction structure */ + LY_STMT_ERROR_MESSAGE = 0xD0000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - compiled restriction structure */ + LY_STMT_EXTENSION = 0xE0000, /**< ::lysp_ext_substmt.storage - `struct lysp_ext *`[] + ::lysp_ext_instance.parent - `struct lysp_ext *` + ::lysc_ext_substmt.storage - not compiled explicitly + ::lysc_ext_instance.parent - `struct lysc_ext *` */ + LY_STMT_EXTENSION_INSTANCE = 0xF0000, /**< ::lysp_ext_substmt.storage - `struct lysp_ext_instance *`[] + ::lysc_ext_substmt.storage - `struct lysc_ext_instance *`[] */ + LY_STMT_FEATURE = 0x100000, /**< ::lysp_ext_substmt.storage - `struct lysp_feature *`[] + ::lysp_ext_instance.parent - `struct lysp_feature *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_FRACTION_DIGITS = 0x110000, /**< ::lysp_ext_substmt.storage - `uint8_t *` + ::lysp_ext_instance.parent - `struct lysp_type *` + ::lysc_ext_substmt.storage - `uint8_t *` + ::lysc_ext_instance.parent - `struct lysc_type *` */ + LY_STMT_IDENTITY = 0x120000, /**< ::lysp_ext_substmt.storage - `struct lysp_ident *`[] + ::lysp_ext_instance.parent - `struct lysp_ident *` + ::lysc_ext_substmt.storage - `struct lysc_ident *`[] + ::lysc_ext_instance.parent - `struct lysc_ident *` */ + LY_STMT_IF_FEATURE = 0x130000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *`[] + ::lysc_ext_substmt.storage - no storage, evaluated when compiled + ::lysc_ext_instance.parent - compiled parent statement */ + LY_STMT_IMPORT = 0x140000, /**< ::lysp_ext_substmt.storage - `struct lysp_import *`[] + ::lysp_ext_instance.parent - `struct lysp_import *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_INCLUDE = 0x150000, /**< ::lysp_ext_substmt.storage - `struct lysp_include *`[] + ::lysp_ext_instance.parent - `struct lysp_include *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_KEY = 0x160000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_node_list *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_node_list *` */ + LY_STMT_LENGTH = 0x170000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_range *` */ + LY_STMT_MANDATORY = 0x180000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *` + ::lysc_ext_substmt.storage - `uint16_t *` + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_MAX_ELEMENTS = 0x190000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint32_t *` + ::lysc_ext_substmt.storage - `uint32_t *` + ::lysc_ext_instance.parent - `struct lysc_node_list *` */ + LY_STMT_MIN_ELEMENTS = 0x1A0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint32_t *` + ::lysc_ext_substmt.storage - `uint32_t *` + ::lysc_ext_instance.parent - `struct lysc_node_list *` */ + LY_STMT_MODIFIER = 0x1B0000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_pattern *` */ + LY_STMT_MODULE = 0x1C0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_module *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_MUST = 0x1D0000, /**< ::lysp_ext_substmt.storage - `struct lysp_restr *`[] + ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage - `struct lysc_must *`[] + ::lysc_ext_instance.parent - `struct lysc_must *` */ + LY_STMT_NAMESPACE = 0x1E0000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_module *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_ORDERED_BY = 0x1F0000, /**< ::lysp_ext_substmt.storage - `uint16_t *` + ::lysp_ext_instance.parent - `struct lysp_node *` + ::lysc_ext_substmt.storage - `uint16_t *` + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_ORGANIZATION = 0x200000, /**< ::lysp_ext_substmt.storage - `const char *` + ::lysp_ext_instance.parent - `struct lysp_(sub)module *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_PATH = 0x210000, /**< ::lysp_ext_substmt.storage - `struct lyxp_expr *` + ::lysp_ext_instance.parent - `struct lysp_type *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_type *` */ + LY_STMT_PATTERN = 0x220000, /**< ::lysp_ext_substmt.storage - `struct lysp_restr *`[] + ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage - `struct lysc_pattern **`[] + ::lysc_ext_instance.parent - `struct lysc_pattern *` */ + LY_STMT_POSITION = 0x230000, /**< ::lysp_ext_substmt.storage - `int64_t *` + ::lysp_ext_instance.parent - `struct lysp_type_enum *` + ::lysc_ext_substmt.storage - `int64_t *` + ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */ + LY_STMT_PREFIX = 0x240000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_PRESENCE = 0x250000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_node_container *` */ + LY_STMT_RANGE = 0x260000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_restr *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_range *` */ + LY_STMT_REFERENCE = 0x270000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - compiled parent statement */ + LY_STMT_REFINE = 0x280000, /**< ::lysp_ext_substmt.storage - `struct lysp_refine *`[] + ::lysp_ext_instance.parent - `struct lysp_refine *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node *` */ + LY_STMT_REQUIRE_INSTANCE = 0x290000, /**< ::lysp_ext_substmt.storage - `uint8_t *` + ::lysp_ext_instance.parent - `struct lysp_type *` + ::lysc_ext_substmt.storage - `uint8_t *` + ::lysc_ext_instance.parent - `struct lysc_type *` */ + LY_STMT_REVISION = 0x2A0000, /**< ::lysp_ext_substmt.storage - `struct lysp_revision *`[] + ::lysp_ext_instance.parent - `struct lysp_revision *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_REVISION_DATE = 0x2B0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */ + LY_STMT_STATUS = 0x2C0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *` + ::lysc_ext_substmt.storage - `uint16_t *` + ::lysc_ext_instance.parent - compiled parent statement */ + LY_STMT_SUBMODULE = 0x2D0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_submodule *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_TYPE = 0x2E0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_type *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_type *` */ + LY_STMT_TYPEDEF = 0x2F0000, /**< ::lysp_ext_substmt.storage - `struct lysp_tpdf *`[] + ::lysp_ext_instance.parent - `struct lysp_tpdf *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_type *` */ + LY_STMT_UNIQUE = 0x300000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *`[] + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_node_list *` */ + LY_STMT_UNITS = 0x310000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *` + ::lysc_ext_substmt.storage - `const char *` + ::lysc_ext_instance.parent - `struct lysc_node *`, `struct lysc_type *` (typedef) */ + LY_STMT_VALUE = 0x320000, /**< ::lysp_ext_substmt.storage - `int64_t *` + ::lysp_ext_instance.parent - `struct lysp_type_enum *` + ::lysc_ext_substmt.storage - `int64_t *` + ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */ + LY_STMT_WHEN = 0x330000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_when *` + ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_when *` */ + LY_STMT_YANG_VERSION = 0x340000, /**< ::lysp_ext_substmt.storage - `uint8_t *` + ::lysp_ext_instance.parent - `struct lysp_(sub)module *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_module *` */ + LY_STMT_YIN_ELEMENT = 0x350000, /**< ::lysp_ext_substmt.storage - `uint16_t *` + ::lysp_ext_instance.parent - `struct lysp_ext *` + ::lysc_ext_substmt.storage - not compiled + ::lysc_ext_instance.parent - `struct lysc_ext *` */ + + /* separated from the list of statements + * the following tokens are part of the syntax and parsers have to work + * with them, but they are not a standard YANG statements + */ + LY_STMT_SYNTAX_SEMICOLON, + LY_STMT_SYNTAX_LEFT_BRACE, + LY_STMT_SYNTAX_RIGHT_BRACE, + + /* + * YIN-specific tokens, still they are part of the syntax, but not the standard statements + */ + LY_STMT_ARG_TEXT, + LY_STMT_ARG_VALUE +}; + +/** + * @brief Structure representing a generic parsed YANG substatement in an extension instance. + */ +struct lysp_stmt { + const char *stmt; /**< identifier of the statement */ + const char *arg; /**< statement's argument */ + LY_VALUE_FORMAT format; /**< prefix format of the identifier/argument (::LY_VALUE_XML is YIN format) */ + void *prefix_data; /**< Format-specific data for prefix resolution (see ly_resolve_prefix()) */ + + struct lysp_stmt *next; /**< link to the next statement */ + struct lysp_stmt *child; /**< list of the statement's substatements (linked list) */ + uint16_t flags; /**< statement flags, can be set to LYS_YIN_ATTR */ + enum ly_stmt kw; /**< numeric respresentation of the stmt value */ +}; + +/** + * @brief Structure representing a parsed known YANG substatement in an extension instance. + */ +struct lysp_ext_substmt { + enum ly_stmt stmt; /**< parsed substatement */ + void *storage; /**< pointer to the parsed storage of the statement according to the specific + lys_ext_substmt::stmt */ +}; + +/** + * @brief YANG extension parsed instance. + */ +struct lysp_ext_instance { + const char *name; /**< extension identifier, including possible prefix */ + const char *argument; /**< optional value of the extension's argument */ + LY_VALUE_FORMAT format; /**< prefix format of the extension name/argument (::LY_VALUE_XML is YIN format) */ + void *prefix_data; /**< format-specific data for prefix resolution (see ly_resolve_prefix()) */ + struct lysp_ext *def; /**< pointer to the extension definition */ + + void *parent; /**< pointer to the parent statement holding the extension instance(s), use + ::lysp_ext_instance#parent_stmt to access the value/structure */ + enum ly_stmt parent_stmt; /**< type of the parent statement */ + LY_ARRAY_COUNT_TYPE parent_stmt_index; /**< index of the stamenet in case the parent does not point to the parent + statement directly and it is an array */ + uint16_t flags; /**< ::LYS_INTERNAL value (@ref snodeflags) */ + + const struct lyplg_ext_record *record; /**< extension definition plugin record, if any */ + struct lysp_ext_substmt *substmts; /**< list of supported known YANG statements with the pointer to their + parsed data ([sized array](@ref sizedarrays)) */ + void *parsed; /**< private plugin parsed data */ + struct lysp_stmt *child; /**< list of generic (unknown) YANG statements */ +}; + +/** + * @brief Structure representing a compiled known YANG substatement in an extension instance. + */ +struct lysc_ext_substmt { + enum ly_stmt stmt; /**< compiled substatement */ + void *storage; /**< pointer to the compiled storage of the statement according to the specific + lys_ext_substmt::stmt */ +}; + +/** + * @brief YANG extension compiled instance. + */ +struct lysc_ext_instance { + struct lysc_ext *def; /**< pointer to the extension definition */ + const char *argument; /**< optional value of the extension's argument */ + struct lys_module *module; /**< module where the extension instantiated is defined */ + struct lysc_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */ + + void *parent; /**< pointer to the parent element holding the extension instance(s), use + ::lysc_ext_instance#parent_stmt to access the value/structure */ + enum ly_stmt parent_stmt; /**< type of the parent statement */ + LY_ARRAY_COUNT_TYPE parent_stmt_index; /**< index of the stamenet in case the parent does not point to the parent + statement directly and it is an array */ + + struct lysc_ext_substmt *substmts; /**< list of supported known YANG statements with the pointer to their + compiled data ([sized array](@ref sizedarrays)) */ + void *compiled; /**< private plugin compiled data */ +}; + +/** + * @brief Macro to define plugin information in external plugins + * + * Use as follows: + * LYPLG_EXTENSIONS = {{<filled information of ::lyplg_ext_record>}, ..., {0}}; + */ +#define LYPLG_EXTENSIONS \ + uint32_t plugins_extensions_apiver__ = LYPLG_EXT_API_VERSION; \ + const struct lyplg_ext_record plugins_extensions__[] + +/** + * @defgroup pluginsExtensionsParse Plugins: Extensions parsing support + * @ingroup pluginsExtensions + * + * Implementing extension plugin parse callback. + * + * @{ + */ + +/** + * @brief Callback for parsing extension instance substatements. + * + * All known YANG substatements can easily be parsed using ::lyplg_ext_parse_extension_instance. + * + * @param[in] pctx Parse context. + * @param[in,out] ext Parsed extension instance data. + * @return LY_SUCCESS on success. + * @return LY_ENOT if the extension instance is not supported and should be removed. + * @return LY_ERR error on error. + */ +typedef LY_ERR (*lyplg_ext_parse_clb)(struct lysp_ctx *pctx, struct lysp_ext_instance *ext); + +/** + * @brief Log a message from an extension plugin using the parsed extension instance. + * + * @param[in] pctx Parse context to use. + * @param[in] ext Parsed extensiopn instance. + * @param[in] level Log message level (error, warning, etc.) + * @param[in] err_no Error type code. + * @param[in] format Format string to print. + * @param[in] ... Format variable parameters. + */ +LIBYANG_API_DECL void lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, + LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...); + +/** + * @brief Get current parsed module from a parse context. + * + * @param[in] pctx Parse context. + * @return Current (local) parse mod. + */ +LIBYANG_API_DECL const struct lysp_module *lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx); + +/** + * @brief Parse substatements of an extension instance. + * + * Uses standard libyang schema compiler to transform YANG statements into the parsed schema structures. The plugins are + * supposed to use this function when the extension instance's substatements can be parsed in a standard way. + * + * @param[in] pctx Parse context. + * @param[in,out] ext Parsed extension instance with the prepared ::lysp_ext_instance.substmts array, which will be + * updated by storing the parsed data. + * @return LY_SUCCESS on success. + * @return LY_ERR error on error. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_parse_extension_instance(struct lysp_ctx *pctx, struct lysp_ext_instance *ext); + +/** @} pluginsExtensionsParse */ + +/** + * @defgroup pluginsExtensionsCompile Plugins: Extensions compilation support + * @ingroup pluginsExtensions + * + * Implementing extension plugin compile callback. + * + * @{ + */ + +/** + * @defgroup scflags Schema compile flags + * + * Flags to modify schema compilation process and change the way how the particular statements are being compiled. * + * @{ + */ +#define LYS_COMPILE_GROUPING 0x01 /**< Compiling (validation) of a non-instantiated grouping. + In this case not all the restrictions are checked since they can + be valid only in the real placement of the grouping. This is + the case of any restriction that needs to look out of the statements + themselves, since the context is not known. */ +#define LYS_COMPILE_DISABLED 0x02 /**< Compiling a disabled subtree (by its if-features). Meaning + it will be removed at the end of compilation and should not be + added to any unres sets. */ +#define LYS_COMPILE_NO_CONFIG 0x04 /**< ignore config statements, neither inherit config value */ +#define LYS_COMPILE_NO_DISABLED 0x08 /**< ignore if-feature statements */ + +#define LYS_COMPILE_RPC_INPUT (LYS_IS_INPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action input */ +#define LYS_COMPILE_RPC_OUTPUT (LYS_IS_OUTPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action output */ +#define LYS_COMPILE_NOTIFICATION (LYS_IS_NOTIF | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of Notification */ + +/** @} scflags */ + +/** + * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared + * and only the extension specific data are supposed to be added (if any). + * + * The parsed generic statements can be processed by the callback on its own or the ::lyplg_ext_compile_extension_instance() + * function can be used to let the compilation to libyang following the standard rules for processing the YANG statements. + * + * @param[in] cctx Current compile context. + * @param[in] extp Parsed extension instance data. + * @param[in,out] ext Prepared compiled extension instance structure where an addition, extension-specific, data are + * supposed to be placed for later use (data validation or use of external tool). + * @return LY_SUCCESS in case of success. + * @return LY_ENOT in case the extension instance is not supported and should be removed. + * @return LY_ERR error on error. + */ +typedef LY_ERR (*lyplg_ext_compile_clb)(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, + struct lysc_ext_instance *ext); + +/** + * @brief Log a message from an extension plugin using the compiled extension instance. + * + * @param[in] cctx Optional compile context to generate the path from. + * @param[in] ext Compiled extension instance. + * @param[in] level Log message level (error, warning, etc.) + * @param[in] err_no Error type code. + * @param[in] format Format string to print. + */ +LIBYANG_API_DECL void lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, + LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...); + +/** + * @brief Log a message from an extension plugin using the compiled extension instance with an explicit error path. + * + * @param[in] path Log error path to use. + * @param[in] ext Compiled extension instance. + * @param[in] level Log message level (error, warning, etc.) + * @param[in] err_no Error type code. + * @param[in] format Format string to print. + */ +LIBYANG_API_DECL void lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, + LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...); + +/** + * @brief YANG schema compilation context getter for libyang context. + * + * @param[in] ctx YANG schema compilation context. + * @return libyang context connected with the compilation context. + */ +LIBYANG_API_DECL struct ly_ctx *lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx); + +/** + * @brief YANG schema compilation context getter for compilation options. + * + * @param[in] ctx YANG schema compilation context. + * @return pointer to the compilation options to allow modifying them with @ref scflags values. + */ +LIBYANG_API_DECL uint32_t *lyplg_ext_compile_get_options(const struct lysc_ctx *ctx); + +/** + * @brief YANG schema compilation context getter for current module. + * + * @param[in] ctx YANG schema compilation context. + * @return current module. + */ +LIBYANG_API_DECL const struct lys_module *lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx); + +/** + * @brief YANG schema compilation context getter for currently processed module. + * + * @param[in] ctx YANG schema compilation context. + * @return Currently processed module. + */ +LIBYANG_API_DECL struct lysp_module *lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx); + +/** + * @brief Compile substatements of an extension instance. + * + * Uses standard libyang schema compiler to transform YANG statements into the compiled schema structures. The plugins are + * supposed to use this function when the extension instance's substatements are supposed to be compiled in a standard way + * (or if just the @ref scflags are enough to modify the compilation process). + * + * @param[in] ctx Compile context. + * @param[in] extp Parsed representation of the extension instance being processed. + * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated + * by storing the compiled data. + * @return LY_SUCCESS on success. + * @return LY_EVALID if compilation of the substatements fails. + * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp, + struct lysc_ext_instance *ext); + +/** @} pluginsExtensionsCompile */ + +/** + * @defgroup pluginsExtensionsSprinterInfo Plugins: Extensions schema info printer support + * @ingroup pluginsExtensions + * + * Implementing extension plugin schema info printer callback. + * + * @{ + */ + +/** + * @brief Callback to print the compiled extension instance's private data in the INFO format. + * + * @param[in] ctx YANG printer context to provide output handler and other information for printing. + * @param[in] ext The compiled extension instance, mainly to access the extensions. + * @param[in,out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, + * 1 otherwise. + * @return LY_SUCCESS when everything was fine, other LY_ERR values in case of failure + */ +typedef LY_ERR (*lyplg_ext_sprinter_info_clb)(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag); + +/** + * @brief YANG printer context getter for output handler. + * + * @param[in] ctx YANG printer context. + * @return Output handler where the data are being printed. Note that the address of the handler pointer in the context is + * returned to allow to modify the handler. + */ +LIBYANG_API_DECL struct ly_out **lyplg_ext_print_get_out(const struct lyspr_ctx *ctx); + +/** + * @brief YANG printer context getter for printer options. + * + * @param[in] ctx YANG printer context. + * @return pointer to the printer options to allow modifying them with @ref schemaprinterflags values. + */ +LIBYANG_API_DECL uint32_t *lyplg_ext_print_get_options(const struct lyspr_ctx *ctx); + +/** + * @brief YANG printer context getter for printer indentation level. + * + * @param[in] ctx YANG printer context. + * @return pointer to the printer's indentation level to allow modifying its value. + */ +LIBYANG_API_DECL uint16_t *lyplg_ext_print_get_level(const struct lyspr_ctx *ctx); + +/** + * @brief Print substatements of an extension instance in info format (compiled YANG). + * + * Generic function to access YANG printer functions from the extension plugins (::lyplg_ext_sprinter_info_clb). + * + * @param[in] ctx YANG printer context to provide output handler and other information for printing. + * @param[in] ext The compiled extension instance to access the extensions and substatements data. + * @param[in,out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, + * 1 otherwise. + */ +LIBYANG_API_DECL void lyplg_ext_print_info_extension_instance(struct lyspr_ctx *ctx, const struct lysc_ext_instance *ext, + ly_bool *flag); + +/** @} pluginsExtensionsSprinterInfo */ + +/** + * @defgroup pluginsExtensionsSprinterTree Plugins: Extensions schema parsed and compiled tree printer support + * @ingroup pluginsExtensions + * + * Implementing extension plugin schema parsed and compiled tree printer callback. + * + * @{ + */ + +/** + * @brief Callback to print parent node of @p ext or to print the contents of the extension. + * + * Function is called in two different cases. If the printer_tree needs the tree-diagram form of a parent node, + * then @p ctx is set to NULL. In the second case, if printer_tree needs to print the contents of the extension, + * then @p ctx is set and function must prepare the nodes that should be printed using the + * lyplg_ext_sprinter_tree* functions. + * + * @param[in] ext Extension instance. + * @param[in,out] ctx Context for the tree printer. Extension contents can be inserted into it by functions + * lyplg_ext_sprinter_ctree_add_ext_nodes(), lyplg_ext_sprinter_ctree_add_nodes() or by their ptree alternatives. + * It parameter is set to NULL, then @p flags and @p add_opts are used by printer_tree. + * @param[out] flags Optional override tree-diagram \<flags\> in a parent node. If @p ctx is set, ignore this parameter. + * @param[out] add_opts Additional tree-diagram \<opts\> string in a parent node which is printed before \<opts\>. If @p ctx + * is set, ignore this parameter. + * @return LY_ERR value. + */ +typedef LY_ERR (*lyplg_ext_sprinter_ctree_clb)(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **flags, const char **add_opts); + +/** + * @brief Callback for rewriting the tree-diagram form of a specific node. + * + * If this callback is set, then it is called for each node that belongs to the extension instance. + * + * @param[in] node Node whose tree-diagram form can be modified by the function. + * @param[in,out] plugin_priv Private context set by plugin. + * @param[out] skip Flag set to 1 removes the node from printed diagram. + * @param[out] flags Override tree-diagram \<flags\> string in the @p node. + * @param[out] add_opts Additional tree-diagram \<opts\> string in the @p node which is printed before \<opts\>. + * @return LY_ERR value. + */ +typedef LY_ERR (*lyplg_ext_sprinter_ctree_override_clb)(const struct lysc_node *node, const void *plugin_priv, + ly_bool *skip, const char **flags, const char **add_opts); + +/** + * @brief Registration of printing a group of nodes, which is already in the extension. + * + * @param[in] ctx Context of printer_tree in which the group of nodes is saved and later printed. + * @param[in] ext Extension in which the group of nodes will be searched. + * @param[in] clb Override function that will be applied to each delivered node. + * @return LY_ERR value. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, + struct lysc_ext_instance *ext, lyplg_ext_sprinter_ctree_override_clb clb); + +/** + * @brief Registration of printing the group of nodes which were defined in the plugin. + * + * @param[in] ctx Context of printer_tree in which the group of nodes is saved and later printed. + * @param[in] nodes Points to the first node in group. + * @param[in] clb Override function that will be applied to each delivered node. + * @return LY_ERR value. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes, + lyplg_ext_sprinter_ctree_override_clb clb); + +/** + * @brief Registration of plugin-private data defined by the plugin that is shared between override_clb calls. + * + * @param[in] ctx Context of printer_tree in which plugin-private data will be saved. + * @param[in] plugin_priv Plugin-private data shared between oberride_clb calls. + * @param[in] free_clb Release function for @p plugin_priv. + * @return LY_ERR value. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, + void (*free_clb)(void *plugin_priv)); + +/** + * @copydoc lyplg_ext_sprinter_ctree_clb + */ +typedef LY_ERR (*lyplg_ext_sprinter_ptree_clb)(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **flags, const char **add_opts); + +/** + * @copydoc lyplg_ext_sprinter_ctree_override_clb + */ +typedef LY_ERR (*lyplg_ext_sprinter_ptree_override_clb)(const struct lysp_node *node, const void *plugin_priv, + ly_bool *skip, const char **flags, const char **add_opts); + +/** + * @copydoc lyplg_ext_sprinter_ctree_add_ext_nodes + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, + struct lysp_ext_instance *ext, lyplg_ext_sprinter_ptree_override_clb clb); + +/** + * @copydoc lyplg_ext_sprinter_ctree_add_nodes + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes, + lyplg_ext_sprinter_ptree_override_clb clb); + +/** @} pluginsExtensionsSprinterTree */ + +/* + * data node + */ + +/** + * @brief Callback called for all data nodes connected to the extension instance. + * + * Can be used for additional data node validation. Is called only after the whole data tree is created and standard + * validation succeeds. Not called when parsing data and ::LYD_PARSE_ONLY is used. + * + * @param[in] ext Compiled extension instance. + * @param[in] node Data node to process. + * @param[in] validate_options Options used for the validation phase, see @ref datavalidationoptions. + * @return LY_SUCCESS on success. + * @return LY_ERR on error. + */ +typedef LY_ERR (*lyplg_ext_data_node_clb)(struct lysc_ext_instance *ext, struct lyd_node *node, uint32_t validate_options); + +/* + * snode + */ + +/** + * @brief Callback for getting a schema node for a new YANG instance data described by an extension instance. + * Needed only if the extension instance supports some nested standard YANG data. + * + * @param[in] ext Compiled extension instance. + * @param[in] parent Parsed parent data node. Set if @p sparent is NULL. + * @param[in] sparent Schema parent node. Set if @p parent is NULL. + * @param[in] prefix Element prefix, if any. + * @param[in] prefix_len Length of @p prefix. + * @param[in] format Format of @p prefix. + * @param[in] prefix_data Format-specific prefix data. + * @param[in] name Element name. + * @param[in] name_len Length of @p name. + * @param[out] snode Schema node to use for parsing the node. + * @return LY_SUCCESS on success. + * @return LY_ENOT if the data are not described by @p ext. + * @return LY_ERR on error. + */ +typedef LY_ERR (*lyplg_ext_data_snode_clb)(struct lysc_ext_instance *ext, const struct lyd_node *parent, + const struct lysc_node *sparent, const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, + const char *name, size_t name_len, const struct lysc_node **snode); + +/* + * validate + */ + +/** + * @brief Callback for validating parsed YANG instance data described by an extension instance. + * + * This callback is used only for nested data definition (with a standard YANG schema parent). + * + * @param[in] ext Compiled extension instance. + * @param[in] sibling First sibling with schema node returned by ::lyplg_ext_data_snode_clb. + * @param[in] dep_tree Tree to be used for validating references from the operation subtree, if operation. + * @param[in] data_type Validated data type, can be ::LYD_TYPE_DATA_YANG, ::LYD_TYPE_RPC_YANG, ::LYD_TYPE_NOTIF_YANG, + * or ::LYD_TYPE_REPLY_YANG. + * @param[in] val_opts Validation options, see @ref datavalidationoptions. + * @param[out] diff Optional diff with any changes made by the validation. + * @return LY_SUCCESS on success. + * @return LY_ERR on error. + */ +typedef LY_ERR (*lyplg_ext_data_validate_clb)(struct lysc_ext_instance *ext, struct lyd_node *sibling, + const struct lyd_node *dep_tree, enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff); + +/* + * parse free + */ + +/** + * @brief Callback to free the extension-specific data created by its parsing. + * + * @param[in] ctx libyang context. + * @param[in,out] ext Parsed extension structure to free. + */ +typedef void (*lyplg_ext_parse_free_clb)(const struct ly_ctx *ctx, struct lysp_ext_instance *ext); + +/** + * @brief Free the extension instance's data parsed with ::lyplg_ext_parse_extension_instance(). + * + * @param[in] ctx libyang context + * @param[in] substmts Extension instance substatements to free. + */ +LIBYANG_API_DECL void lyplg_ext_pfree_instance_substatements(const struct ly_ctx *ctx, struct lysp_ext_substmt *substmts); + +/* + * compile free + */ + +/** + * @brief Callback to free the extension-specific data created by its compilation. + * + * @param[in] ctx libyang context. + * @param[in,out] ext Compiled extension structure to free. + */ +typedef void (*lyplg_ext_compile_free_clb)(const struct ly_ctx *ctx, struct lysc_ext_instance *ext); + +/** + * @brief Free the extension instance's data compiled with ::lyplg_ext_compile_extension_instance(). + * + * @param[in] ctx libyang context + * @param[in] substmts Extension instance substatements to free. + */ +LIBYANG_API_DECL void lyplg_ext_cfree_instance_substatements(const struct ly_ctx *ctx, struct lysc_ext_substmt *substmts); + +/** + * @brief Extension plugin implementing various aspects of a YANG extension + */ +struct lyplg_ext { + const char *id; /**< plugin identification (mainly for distinguish incompatible versions + of the plugins for external tools) */ + lyplg_ext_parse_clb parse; /**< callback to parse the extension instance substatements */ + lyplg_ext_compile_clb compile; /**< callback to compile extension instance from the parsed data */ + lyplg_ext_sprinter_info_clb printer_info; /**< callback to print the compiled content (info format) of the extension + instance */ + lyplg_ext_sprinter_ctree_clb printer_ctree; /**< callback to print tree format of compiled node containing the + compiled content of the extension instance */ + lyplg_ext_sprinter_ptree_clb printer_ptree; /**< callback to print tree format of parsed node containing the + parsed content of the extension instance */ + lyplg_ext_data_node_clb node; /**< callback to validate most relevant data instance for the extension + instance */ + lyplg_ext_data_snode_clb snode; /**< callback to get schema node for nested YANG data */ + lyplg_ext_data_validate_clb validate; /**< callback to validate parsed data instances according to the extension + definition */ + + lyplg_ext_parse_free_clb pfree; /**< free the extension-specific data created by its parsing */ + lyplg_ext_compile_free_clb cfree; /**< free the extension-specific data created by its compilation */ +}; + +struct lyplg_ext_record { + /* plugin identification */ + const char *module; /**< name of the module where the extension is defined */ + const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision, + which is not an optimal approach due to a possible future revisions of the module. + Instead, there should be defined multiple items in the plugins list, each with the + different revision, but all with the same pointer to the plugin functions. The + only valid use case for the NULL revision is the case the module has no revision. */ + const char *name; /**< YANG name of the extension */ + + /* runtime data */ + struct lyplg_ext plugin; /**< data to utilize plugin implementation */ +}; + +/** + * @brief Stringify statement identifier. + * + * @param[in] stmt The statement identifier to stringify. + * @return Constant string representation of the given @p stmt. + */ +LIBYANG_API_DECL const char *lyplg_ext_stmt2str(enum ly_stmt stmt); + +/** + * @brief Convert nodetype to statement identifier + * + * @param[in] nodetype Nodetype to convert. + * @return Statement identifier representing the given @p nodetype. + */ +LIBYANG_API_DECL enum ly_stmt lyplg_ext_nodetype2stmt(uint16_t nodetype); + +/** + * @brief Get compiled ext instance storage for a specific statement. + * + * @param[in] ext Compiled ext instance. + * @param[in] stmt Compiled statement. Can be a mask when the first match is returned, it is expected the storage is + * the same for all the masked statements. + * @param[in] storage_size Size of the value at @p storage address (dereferenced). + * @param[out] storage Compiled ext instance substatement storage, NULL if was not compiled. + * @return LY_SUCCESS on success. + * @return LY_ENOT if the substatement is not supported. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, + const void **storage); + +/** + * @brief Get parsed ext instance storage for a specific statement. + * + * @param[in] ext Compiled ext instance. + * @param[in] stmt Parsed statement. Can be a mask when the first match is returned, it is expected the storage is + * the same for all the masked statements. + * @param[in] storage_size Size of the value at @p storage address (dereferenced). + * @param[out] storage Parsed ext instance substatement storage, NULL if was not parsed. + * @return LY_SUCCESS on success. + * @return LY_ENOT if the substatement is not supported. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, + uint32_t storage_size, const void **storage); + +/** + * @brief Get specific run-time extension instance data from a callback set by ::ly_ctx_set_ext_data_clb(). + * + * @param[in] ctx Context with the callback. + * @param[in] ext Compiled extension instance. + * @param[out] ext_data Provided extension instance data. + * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not. + * @return LY_SUCCESS on success. + * @return LY_ERR on error. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, + ly_bool *ext_data_free); + +/** + * @brief Insert extension instance data into a parent. + * + * @param[in] parent Parent node to insert into. + * @param[in] first First top-level sibling node to insert. + * @return LY_SUCCESS on success. + * @return LY_ERR error on error. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first); + +/** + * @brief Expand parent-reference xpath expressions + * + * @param[in] ext Context allocated for extension. + * @param[out] refs Set of schema node matching parent-reference XPaths. + * @return LY_ERR value. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs); + +/** + * @brief Allocate a new context for a particular instance of the yangmnt:mount-point extension. + * Caller is responsible for destroying the resulting context. + * + * @param[in] ext Compiled extension instance. + * @param[out] ctx A context with modules loaded from the list found in + * the extension data. + * @return LY_ERR value. + */ +LIBYANG_API_DECL LY_ERR lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx); + +/** @} pluginsExtensions */ + +#ifdef __cplusplus +} +#endif + +#endif /* LY_PLUGINS_EXTS_H_ */ diff --git a/src/plugins_exts/metadata.c b/src/plugins_exts/metadata.c new file mode 100644 index 0000000..9567e07 --- /dev/null +++ b/src/plugins_exts/metadata.c @@ -0,0 +1,243 @@ +/** + * @file metadata.c + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief libyang extension plugin - Metadata (RFC 7952) + * + * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include "metadata.h" + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "libyang.h" +#include "plugins_exts.h" + +struct lysp_ext_metadata { + struct lysp_type *type; /**< type of the metadata (mandatory) */ + const char *units; /**< units of the leaf's type */ + struct lysp_qname *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */ + const char *dsc; /**< description */ + const char *ref; /**< reference */ + uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */ +}; + +struct lysc_ext_metadata { + struct lysc_type *type; /**< type of the metadata (mandatory) */ + const char *units; /**< units of the leaf's type */ + const char *dsc; /**< description */ + const char *ref; /**< reference */ + uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */ +}; + +/** + * @brief Parse annotation extension instances. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +annotation_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + LY_ERR r; + struct lysp_ext_metadata *ann_pdata; + struct lysp_module *pmod; + LY_ARRAY_COUNT_TYPE u; + + /* annotations can appear only at the top level of a YANG module or submodule */ + if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is allowed only at the top level of a YANG module or " + "submodule, but it is placed in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt)); + return LY_EVALID; + } + + pmod = ext->parent; + + /* check for duplication */ + LY_ARRAY_FOR(pmod->exts, u) { + if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { + /* duplication of the same annotation extension in a single module */ + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); + return LY_EVALID; + } + } + + /* parse annotation substatements */ + ext->parsed = ann_pdata = calloc(1, sizeof *ann_pdata); + if (!ann_pdata) { + goto emem; + } + LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 6, r, emem); + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_IF_FEATURE; + ext->substmts[0].storage = &ann_pdata->iffeatures; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_UNITS; + ext->substmts[1].storage = &ann_pdata->units; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_STATUS; + ext->substmts[2].storage = &ann_pdata->flags; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[3].stmt = LY_STMT_TYPE; + ext->substmts[3].storage = &ann_pdata->type; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[4].stmt = LY_STMT_DESCRIPTION; + ext->substmts[4].storage = &ann_pdata->dsc; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[5].stmt = LY_STMT_REFERENCE; + ext->substmts[5].storage = &ann_pdata->ref; + + if ((r = lyplg_ext_parse_extension_instance(pctx, ext))) { + return r; + } + + /* check for mandatory substatements */ + if (!ann_pdata->type) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Missing mandatory keyword \"type\" as a child of \"%s %s\".", + ext->name, ext->argument); + return LY_EVALID; + } + + return LY_SUCCESS; + +emem: + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief Compile annotation extension instances. + * + * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. + */ +static LY_ERR +annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext) +{ + LY_ERR ret; + struct lysc_ext_metadata *ann_cdata; + + /* compile annotation substatements */ + ext->compiled = ann_cdata = calloc(1, sizeof *ann_cdata); + if (!ann_cdata) { + goto emem; + } + LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), ext->substmts, 6, ret, emem); + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_IF_FEATURE; + ext->substmts[0].storage = NULL; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_UNITS; + ext->substmts[1].storage = &ann_cdata->units; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_STATUS; + ext->substmts[2].storage = &ann_cdata->flags; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[3].stmt = LY_STMT_TYPE; + ext->substmts[3].storage = &ann_cdata->type; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[4].stmt = LY_STMT_DESCRIPTION; + ext->substmts[4].storage = &ann_cdata->dsc; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[5].stmt = LY_STMT_REFERENCE; + ext->substmts[5].storage = &ann_cdata->ref; + + ret = lyplg_ext_compile_extension_instance(cctx, extp, ext); + return ret; + +emem: + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief INFO printer + * + * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info + */ +static LY_ERR +annotation_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) +{ + lyplg_ext_print_info_extension_instance(ctx, ext, flag); + + return LY_SUCCESS; +} + +/** + * @brief Free parsed annotation extension instance data. + * + * Implementation of ::lyplg_ext_parse_free_clb callback set as ::lyext_plugin::pfree. + */ +static void +annotation_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) +{ + if (!ext->substmts) { + return; + } + + lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); + free(ext->parsed); +} + +/** + * @brief Free compiled annotation extension instance data. + * + * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree. + */ +static void +annotation_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) +{ + if (!ext->substmts) { + return; + } + + lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); + free(ext->compiled); +} + +/** + * @brief Plugin descriptions for the Metadata's annotation extension + * + * Note that external plugins are supposed to use: + * + * LYPLG_EXTENSIONS = { + */ +const struct lyplg_ext_record plugins_metadata[] = { + { + .module = "ietf-yang-metadata", + .revision = "2016-08-05", + .name = "annotation", + + .plugin.id = "ly2 metadata v1", + .plugin.parse = annotation_parse, + .plugin.compile = annotation_compile, + .plugin.printer_info = annotation_printer_info, + .plugin.printer_ctree = NULL, + .plugin.printer_ptree = NULL, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = annotation_pfree, + .plugin.cfree = annotation_cfree, + }, + {0} /* terminating zeroed record */ +}; diff --git a/src/plugins_exts/metadata.h b/src/plugins_exts/metadata.h new file mode 100644 index 0000000..59ea2bf --- /dev/null +++ b/src/plugins_exts/metadata.h @@ -0,0 +1,66 @@ +/** + * @file metadata.h + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief ietf-yang-metadata API + * + * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#ifndef LY_PLUGINS_EXTS_METADATA_H_ +#define LY_PLUGINS_EXTS_METADATA_H_ + +#include "plugins_exts.h" +#include "tree_data.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Metadata structure. + * + * The structure provides information about metadata of a data element. Such attributes must map to + * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) + * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, + * they are represented as JSON elements starting with the '@' character (for more information, see the + * YANG metadata RFC. + * + */ +struct lyd_meta { + struct lyd_node *parent; /**< data node where the metadata is placed */ + struct lyd_meta *next; /**< pointer to the next metadata of the same element */ + struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */ + const char *name; /**< metadata name */ + struct lyd_value value; /**< metadata value representation */ +}; + +/** + * @brief Get the (canonical) value of a metadata node. + * + * @param[in] meta Metadata node to use. + * @return Canonical value. + */ +static inline const char * +lyd_get_meta_value(const struct lyd_meta *meta) +{ + if (meta) { + const struct lyd_value *value = &meta->value; + + return value->_canonical ? value->_canonical : lyd_value_get_canonical(meta->annotation->module->ctx, value); + } + + return NULL; +} + +#ifdef __cplusplus +} +#endif + +#endif /* LY_PLUGINS_EXTS_METADATA_H_ */ diff --git a/src/plugins_exts/nacm.c b/src/plugins_exts/nacm.c new file mode 100644 index 0000000..5ab8daa --- /dev/null +++ b/src/plugins_exts/nacm.c @@ -0,0 +1,223 @@ +/** + * @file nacm.c + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief libyang extension plugin - NACM (RFC 6536) + * + * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "compat.h" +#include "libyang.h" +#include "plugins_exts.h" + +struct nacm_dfs_arg { + struct lysc_ext_instance *ext; + struct lysc_node *parent; +}; + +/** + * @brief DFS callback implementation for inheriting the NACM extension. + */ +static LY_ERR +nacm_inherit_clb(struct lysc_node *node, void *data, ly_bool *dfs_continue) +{ + LY_ERR ret; + struct nacm_dfs_arg *arg = data; + struct lysc_ext_instance *inherited; + LY_ARRAY_COUNT_TYPE u; + + /* ignore the parent from which we inherit and input/output nodes */ + if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { + /* check that the node does not have its own NACM extension instance */ + LY_ARRAY_FOR(node->exts, u) { + if (node->exts[u].def == arg->ext->def) { + /* the child already have its own NACM flag, so skip the subtree */ + *dfs_continue = 1; + return LY_SUCCESS; + } + } + + /* duplicate this one to inherit it to the child */ + LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem); + + inherited->def = arg->ext->def; + inherited->parent = node; + inherited->parent_stmt = lyplg_ext_nodetype2stmt(node->nodetype); + if (arg->ext->argument) { + if ((ret = lydict_insert(node->module->ctx, arg->ext->argument, 0, &inherited->argument))) { + return ret; + } + } + /* copy the pointer to the static variables */ + inherited->compiled = arg->ext->compiled; + } + + return LY_SUCCESS; + +emem: + lyplg_ext_compile_log(NULL, arg->ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return ret; +} + +/** + * @brief Parse NACM extension instances. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + struct lysp_node *parent = NULL; + LY_ARRAY_COUNT_TYPE u; + + /* check that the extension is instantiated at an allowed place - data node */ + if (!(ext->parent_stmt & LY_STMT_NODE_MASK)) { + lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is allowed only in a data nodes, but it is placed in " + "\"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt)); + return LY_ENOT; + } + + parent = ext->parent; + if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA | + LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF)) || (!strcmp(strchr(ext->name, ':') + 1, "default-deny-write") && + (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))) { + /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang + * passes all their extensions to their children nodes */ + lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is not allowed in %s statement.", ext->name, + lys_nodetype2str(parent->nodetype)); + return LY_ENOT; + } + + /* check for duplication */ + LY_ARRAY_FOR(parent->exts, u) { + if ((&parent->exts[u] != ext) && parent->exts[u].record && (parent->exts[u].record->plugin.id == ext->record->plugin.id)) { + /* duplication of a NACM extension on a single node + * We check for all NACM plugins since we want to catch even the situation that there is default-deny-all + * AND default-deny-write */ + if (parent->exts[u].name == ext->name) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); + } else { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, + "Extension nacm:default-deny-write is mixed with nacm:default-deny-all."); + } + return LY_EVALID; + } + } + + return LY_SUCCESS; +} + +/** + * @brief Compile NACM extension instances. + * + * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. + */ +static LY_ERR +nacm_compile(struct lysc_ctx *UNUSED(cctx), const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext) +{ + struct nacm_dfs_arg dfs_arg; + + static const uint8_t nacm_deny_all = 1; + static const uint8_t nacm_deny_write = 2; + + /* store the NACM flag */ + if (!strcmp(ext->def->name, "default-deny-write")) { + ext->compiled = (void *)&nacm_deny_write; + } else if (!strcmp(ext->def->name, "default-deny-all")) { + ext->compiled = (void *)&nacm_deny_all; + } else { + return LY_EINT; + } + + /* inherit the extension instance to all the children nodes */ + dfs_arg.ext = ext; + dfs_arg.parent = ext->parent; + return lysc_tree_dfs_full(ext->parent, nacm_inherit_clb, &dfs_arg); +} + +/** + * @brief Plugin descriptions for the NACM's default-deny-write and default-deny-all extensions + * + * Note that external plugins are supposed to use: + * + * LYPLG_EXTENSIONS = { + */ +const struct lyplg_ext_record plugins_nacm[] = { + { + .module = "ietf-netconf-acm", + .revision = "2012-02-22", + .name = "default-deny-write", + + .plugin.id = "ly2 NACM v1", + .plugin.parse = nacm_parse, + .plugin.compile = nacm_compile, + .plugin.printer_info = NULL, + .plugin.printer_ctree = NULL, + .plugin.printer_ptree = NULL, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = NULL, + .plugin.cfree = NULL + }, { + .module = "ietf-netconf-acm", + .revision = "2018-02-14", + .name = "default-deny-write", + + .plugin.id = "ly2 NACM v1", + .plugin.parse = nacm_parse, + .plugin.compile = nacm_compile, + .plugin.printer_info = NULL, + .plugin.printer_ctree = NULL, + .plugin.printer_ptree = NULL, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = NULL, + .plugin.cfree = NULL + }, { + .module = "ietf-netconf-acm", + .revision = "2012-02-22", + .name = "default-deny-all", + + .plugin.id = "ly2 NACM v1", + .plugin.parse = nacm_parse, + .plugin.compile = nacm_compile, + .plugin.printer_info = NULL, + .plugin.printer_ctree = NULL, + .plugin.printer_ptree = NULL, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = NULL, + .plugin.cfree = NULL + }, { + .module = "ietf-netconf-acm", + .revision = "2018-02-14", + .name = "default-deny-all", + + .plugin.id = "ly2 NACM v1", + .plugin.parse = nacm_parse, + .plugin.compile = nacm_compile, + .plugin.printer_info = NULL, + .plugin.printer_ctree = NULL, + .plugin.printer_ptree = NULL, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = NULL, + .plugin.cfree = NULL + }, + {0} /* terminating zeroed item */ +}; diff --git a/src/plugins_exts/schema_mount.c b/src/plugins_exts/schema_mount.c new file mode 100644 index 0000000..9800760 --- /dev/null +++ b/src/plugins_exts/schema_mount.c @@ -0,0 +1,1332 @@ +/** + * @file schema_mount.c + * @author Tadeas Vintrlik <xvintr04@stud.fit.vutbr.cz> + * @brief libyang extension plugin - Schema Mount (RFC 8528) + * + * Copyright (c) 2021 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#define _GNU_SOURCE + +#include <assert.h> +#include <pthread.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "common.h" +#include "compat.h" +#include "dict.h" +#include "libyang.h" +#include "log.h" +#include "parser_data.h" +#include "plugins_exts.h" +#include "plugins_types.h" +#include "tree_data.h" +#include "tree_schema.h" + +/** + * @brief Internal schema mount data structure for holding all the contexts of parsed data. + */ +struct lyplg_ext_sm { + pthread_mutex_t lock; /**< lock for accessing this shared structure */ + + struct lyplg_ext_sm_shared { + struct { + struct ly_ctx *ctx; /**< context shared between all data of this mount point */ + const char *mount_point; /**< mount point name */ + const char *content_id; /**< yang-library content-id (alternatively module-set-id), + stored in the dictionary of the ext instance context */ + } *schemas; /**< array of shared schema schemas */ + uint32_t schema_count; /**< length of schemas array */ + uint32_t ref_count; /**< number of references to this structure (mount-points with the same name + in the module) */ + } *shared; /**< shared schema mount points */ + + struct lyplg_ext_sm_inln { + struct { + struct ly_ctx *ctx; /**< context created for inline schema data, may be reused if possible */ + } *schemas; /**< array of inline schemas */ + uint32_t schema_count; /**< length of schemas array */ + } inln; /**< inline mount points */ +}; + +struct sprinter_tree_priv { + struct ly_ctx *ext_ctx; + struct ly_set *refs; +}; + +#define EXT_LOGERR_MEM_RET(cctx, ext) \ + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \ + return LY_EMEM + +#define EXT_LOGERR_MEM_GOTO(cctx, ext, rc, label) \ + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \ + rc = LY_EMEM; \ + goto label + +#define EXT_LOGERR_INT_RET(cctx, ext) \ + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \ + return LY_EINT + +/** + * @brief Check if given mount point is unique among its siblings + * + * @param[in] pctx Parse context. + * @param[in] ext Parsed extension instance. + * @return LY_SUCCESS if is unique; + * @return LY_EINVAL otherwise. + */ +static LY_ERR +schema_mount_parse_unique_mp(struct lysp_ctx *pctx, const struct lysp_ext_instance *ext) +{ + struct lysp_ext_instance *exts; + LY_ARRAY_COUNT_TYPE u; + struct lysp_node *parent; + + /* check if it is the only instance of the mount-point among its siblings */ + parent = ext->parent; + exts = parent->exts; + LY_ARRAY_FOR(exts, u) { + if (&exts[u] == ext) { + continue; + } + + if (!strcmp(exts[u].name, ext->name)) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Multiple extension \"%s\" instances.", ext->name); + return LY_EINVAL; + } + } + return LY_SUCCESS; +} + +/** + * @brief Schema mount parse. + * Checks if it can be a valid extension instance for yang schema mount. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +schema_mount_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + /* check YANG version 1.1 */ + if (lyplg_ext_parse_get_cur_pmod(pctx)->version != LYS_VERSION_1_1) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance not allowed in YANG version 1 module.", + ext->name); + return LY_EINVAL; + } + + /* check parent nodetype */ + if ((ext->parent_stmt != LY_STMT_CONTAINER) && (ext->parent_stmt != LY_STMT_LIST)) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance allowed only in container or list statement.", + ext->name); + return LY_EINVAL; + } + + /* check uniqueness */ + if (schema_mount_parse_unique_mp(pctx, ext)) { + return LY_EINVAL; + } + + /* nothing to actually parse */ + return LY_SUCCESS; +} + +struct lyplg_ext_sm_shared_cb_data { + const struct lysc_ext_instance *ext; + struct lyplg_ext_sm_shared *sm_shared; +}; + +static LY_ERR +schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue)) +{ + struct lyplg_ext_sm_shared_cb_data *cb_data = data; + struct lyplg_ext_sm *sm_data; + struct lysc_ext_instance *exts; + LY_ARRAY_COUNT_TYPE u; + + if (node == cb_data->ext->parent) { + /* parent of the current compiled extension, skip */ + return LY_SUCCESS; + } + + /* find the same mount point */ + exts = node->exts; + LY_ARRAY_FOR(exts, u) { + if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point") && + (exts[u].argument == cb_data->ext->argument)) { + /* same mount point, break the DFS search */ + sm_data = exts[u].compiled; + cb_data->sm_shared = sm_data->shared; + return LY_EEXIST; + } + } + + /* not found, continue search */ + return LY_SUCCESS; +} + +static struct lyplg_ext_sm_shared * +schema_mount_compile_find_shared(const struct lys_module *mod, const struct lysc_ext_instance *ext) +{ + struct lyplg_ext_sm_shared_cb_data cb_data; + LY_ERR r; + + /* prepare cb_data */ + cb_data.ext = ext; + cb_data.sm_shared = NULL; + + /* try to find the same mount point */ + r = lysc_module_dfs_full(mod, schema_mount_compile_mod_dfs_cb, &cb_data); + (void)r; + assert((!r && !cb_data.sm_shared) || ((r == LY_EEXIST) && cb_data.sm_shared)); + + return cb_data.sm_shared; +} + +/** + * @brief Schema mount compile. + * Checks if it can be a valid extension instance for yang schema mount. + * + * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. + */ +static LY_ERR +schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext) +{ + const struct lysc_node *node; + struct lyplg_ext_sm *sm_data; + + /* init internal data */ + sm_data = calloc(1, sizeof *sm_data); + if (!sm_data) { + EXT_LOGERR_MEM_RET(cctx, ext); + } + pthread_mutex_init(&sm_data->lock, NULL); + ext->compiled = sm_data; + + /* find the owner module */ + node = ext->parent; + while (node->parent) { + node = node->parent; + } + + /* reuse/init shared schema */ + sm_data->shared = schema_mount_compile_find_shared(node->module, ext); + if (sm_data->shared) { + ++sm_data->shared->ref_count; + } else { + sm_data->shared = calloc(1, sizeof *sm_data->shared); + if (!sm_data->shared) { + free(sm_data); + EXT_LOGERR_MEM_RET(cctx, ext); + } + sm_data->shared->ref_count = 1; + } + + return LY_SUCCESS; +} + +/** + * @brief Learn details about the current mount point. + * + * @param[in] ext Compiled extension instance. + * @param[in] ext_data Extension data retrieved by the callback. + * @param[out] config Whether the whole schema should keep its config or be set to false. + * @param[out] shared Optional flag whether the schema is shared or inline. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_get_smount(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool *config, + ly_bool *shared) +{ + struct lyd_node *mpoint, *node; + char *path = NULL; + LY_ERR r; + + /* find the mount point */ + if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']", ext->module->name, + ext->argument) == -1) { + EXT_LOGERR_MEM_RET(NULL, ext); + } + r = ext_data ? lyd_find_path(ext_data, path, 0, &mpoint) : LY_ENOTFOUND; + free(path); + if (r) { + /* missing mount-point, cannot be data for this extension (https://datatracker.ietf.org/doc/html/rfc8528#page-10) */ + return LY_ENOT; + } + + /* check config */ + *config = 1; + if (!lyd_find_path(mpoint, "config", 0, &node) && !strcmp(lyd_get_value(node), "false")) { + *config = 0; + } + assert((ext->parent_stmt == LY_STMT_CONTAINER) || (ext->parent_stmt == LY_STMT_LIST)); + if (((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_R) { + *config = 0; + } + + if (shared) { + /* check schema-ref */ + if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) { + if (lyd_find_path(mpoint, "inline", 0, NULL)) { + EXT_LOGERR_INT_RET(NULL, ext); + } + *shared = 0; + } else { + *shared = 1; + } + } + + return LY_SUCCESS; +} + +/** + * @brief Create schema (context) based on retrieved extension data. + * + * @param[in] ext Compiled extension instance. + * @param[in] ext_data Extension data retrieved by the callback. + * @param[in] config Whether the whole schema should keep its config or be set to false. + * @param[out] ext_ctx Schema to use for parsing the data. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_create_ctx(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, + struct ly_ctx **ext_ctx) +{ + LY_ERR rc = LY_SUCCESS; + const char * const *searchdirs; + char *sdirs = NULL; + const struct lys_module *mod; + struct lysc_node *root, *node; + uint32_t i, idx = 0; + + /* get searchdirs from the current context */ + searchdirs = ly_ctx_get_searchdirs(ext->module->ctx); + + if (searchdirs) { + /* append them all into a single string */ + for (i = 0; searchdirs[i]; ++i) { + if ((rc = ly_strcat(&sdirs, "%s" PATH_SEPARATOR, searchdirs[i]))) { + goto cleanup; + } + } + } + + /* create the context based on the data */ + if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Failed to create context for the schema-mount data."); + goto cleanup; + } + + if (!config) { + /* manually change the config of all schema nodes in all the modules */ + while ((mod = ly_ctx_get_module_iter(*ext_ctx, &idx))) { + if (!mod->implemented) { + continue; + } + + LY_LIST_FOR(mod->compiled->data, root) { + LYSC_TREE_DFS_BEGIN(root, node) { + node->flags &= ~LYS_CONFIG_W; + node->flags |= LYS_CONFIG_R; + + LYSC_TREE_DFS_END(root, node); + } + } + } + } + +cleanup: + free(sdirs); + return rc; +} + +/** + * @brief Get ietf-yang-library context-id from its data. + * + * @param[in] ext Compiled extension instance for logging. + * @param[in] ext_data Extension data retrieved by the callback with the yang-library data. + * @param[out] content_id Content ID in @p ext_data. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_get_content_id(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const char **content_id) +{ + struct lyd_node *node = NULL; + + *content_id = NULL; + + /* get yang-library content-id or module-set-id */ + if (ext_data) { + lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node); + if (!node) { + lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node); + } + if (node) { + *content_id = lyd_get_value(node); + } + } + + if (!*content_id) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, + "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data."); + return LY_EVALID; + } + return LY_SUCCESS; +} + +/** + * @brief Get schema (context) for a shared-schema mount point. + * + * @param[in] ext Compiled extension instance. + * @param[in] ext_data Extension data retrieved by the callback. + * @param[in] config Whether the whole schema should keep its config or be set to false. + * @param[out] ext_ctx Schema to use for parsing the data. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, + const struct ly_ctx **ext_ctx) +{ + struct lyplg_ext_sm *sm_data = ext->compiled; + LY_ERR rc = LY_SUCCESS, r; + struct ly_ctx *new_ctx = NULL; + uint32_t i; + const char *content_id; + void *mem; + + assert(sm_data && sm_data->shared); + + /* get yang-library content-id or module-set-id */ + if ((r = schema_mount_get_content_id(ext, ext_data, &content_id))) { + return r; + } + + /* LOCK */ + if ((r = pthread_mutex_lock(&sm_data->lock))) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r)); + return LY_ESYS; + } + + /* try to find this mount point */ + for (i = 0; i < sm_data->shared->schema_count; ++i) { + if (ext->argument == sm_data->shared->schemas[i].mount_point) { + break; + } + } + + if (i < sm_data->shared->schema_count) { + /* schema exists already */ + if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) { + lyplg_ext_compile_log_path("/ietf-yang-library:yang-library/content-id", ext, LY_LLERR, LY_EVALID, + "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.", + content_id, sm_data->shared->schemas[i].content_id); + rc = LY_EVALID; + goto cleanup; + } + } else { + /* no schema found, create it */ + if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { + rc = r; + goto cleanup; + } + + /* new entry */ + mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas); + if (!mem) { + ly_ctx_destroy(new_ctx); + EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup); + } + sm_data->shared->schemas = mem; + ++sm_data->shared->schema_count; + + /* fill entry */ + sm_data->shared->schemas[i].ctx = new_ctx; + sm_data->shared->schemas[i].mount_point = ext->argument; + lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id); + } + + /* use the context */ + *ext_ctx = sm_data->shared->schemas[i].ctx; + +cleanup: + /* UNLOCK */ + pthread_mutex_unlock(&sm_data->lock); + + return rc; +} + +/** + * @brief Check whether ietf-yang-library data describe an existing context meaning whether it includes + * at least exactly all the mentioned modules. + * + * @param[in] ext Compiled extension instance for logging. + * @param[in] ext_data Extension data retrieved by the callback with the yang-library data. + * @param[in] ctx Context to consider. + * @return LY_SUCCESS if the context matches. + * @return LY_ENOT if the context differs. + * @return LY_ERR on error. + */ +static LY_ERR +schema_mount_ctx_match(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const struct ly_ctx *ctx) +{ + struct ly_set *impl_mods = NULL, *imp_mods = NULL; + struct lyd_node *node; + const struct lys_module *mod; + const char *name, *revision; + LY_ERR rc = LY_ENOT, r; + uint32_t i; + + /* collect all the implemented and imported modules, we do not really care about content-id */ + if (!lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, NULL)) { + if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/module", &impl_mods))) { + rc = r; + goto cleanup; + } + if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/import-only-module", &imp_mods))) { + rc = r; + goto cleanup; + } + } else { + if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='implement']", &impl_mods))) { + rc = r; + goto cleanup; + } + if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='import']", &imp_mods))) { + rc = r; + goto cleanup; + } + } + + if (!impl_mods->count) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, "No implemented modules included in ietf-yang-library data."); + rc = LY_EVALID; + goto cleanup; + } + + /* check all the implemented modules */ + for (i = 0; i < impl_mods->count; ++i) { + lyd_find_path(impl_mods->dnodes[i], "name", 0, &node); + name = lyd_get_value(node); + + lyd_find_path(impl_mods->dnodes[i], "revision", 0, &node); + if (node && strlen(lyd_get_value(node))) { + revision = lyd_get_value(node); + } else { + revision = NULL; + } + + if (!(mod = ly_ctx_get_module(ctx, name, revision)) || !mod->implemented) { + /* unsuitable module */ + goto cleanup; + } + } + + /* check all the imported modules */ + for (i = 0; i < imp_mods->count; ++i) { + lyd_find_path(imp_mods->dnodes[i], "name", 0, &node); + name = lyd_get_value(node); + + lyd_find_path(imp_mods->dnodes[i], "revision", 0, &node); + if (node && strlen(lyd_get_value(node))) { + revision = lyd_get_value(node); + } else { + revision = NULL; + } + + if (!ly_ctx_get_module(ctx, name, revision)) { + /* unsuitable module */ + goto cleanup; + } + } + + /* context matches and can be reused */ + rc = LY_SUCCESS; + +cleanup: + ly_set_free(impl_mods, NULL); + ly_set_free(imp_mods, NULL); + return rc; +} + +/** + * @brief Get schema (context) for an inline mount point. + * + * @param[in] ext Compiled extension instance. + * @param[in] ext_data Extension data retrieved by the callback. + * @param[in] config Whether the whole schema should keep its config or be set to false. + * @param[out] ext_ctx Schema to use for parsing the data. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, + const struct ly_ctx **ext_ctx) +{ + struct lyplg_ext_sm *sm_data = ext->compiled; + struct ly_ctx *new_ctx = NULL; + uint32_t i; + void *mem; + LY_ERR rc = LY_SUCCESS, r; + + assert(sm_data && sm_data->shared); + + /* LOCK */ + if ((r = pthread_mutex_lock(&sm_data->lock))) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r)); + return LY_ESYS; + } + + /* try to find a context we can reuse */ + for (i = 0; i < sm_data->inln.schema_count; ++i) { + r = schema_mount_ctx_match(ext, ext_data, sm_data->inln.schemas[i].ctx); + if (!r) { + /* match */ + *ext_ctx = sm_data->inln.schemas[i].ctx; + goto cleanup; + } else if (r != LY_ENOT) { + /* error */ + rc = r; + goto cleanup; + } + } + + /* new schema required, create context */ + if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { + rc = r; + goto cleanup; + } + + /* new entry */ + mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas); + if (!mem) { + ly_ctx_destroy(new_ctx); + EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup); + } + sm_data->inln.schemas = mem; + ++sm_data->inln.schema_count; + + /* fill entry */ + sm_data->inln.schemas[i].ctx = new_ctx; + + /* use the context */ + *ext_ctx = sm_data->inln.schemas[i].ctx; + +cleanup: + /* UNLOCK */ + pthread_mutex_unlock(&sm_data->lock); + + return rc; +} + +/** + * @brief Get schema (context) for a mount point. + * + * @param[in] ext Compiled extension instance. + * @param[out] ext_ctx Schema to use for parsing the data. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx) +{ + LY_ERR ret = LY_SUCCESS, r; + struct lyd_node *iter, *ext_data = NULL; + ly_bool ext_data_free = 0, config, shared; + + *ext_ctx = NULL; + + /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ + if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { + ret = r; + goto cleanup; + } + + LY_LIST_FOR(ext_data, iter) { + if (iter->flags & LYD_NEW) { + /* must be validated for the parent-reference prefix data to be stored */ + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated."); + ret = LY_EINVAL; + goto cleanup; + } + } + + /* learn about this mount point */ + if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) { + ret = r; + goto cleanup; + } + + /* create/get the context for parsing the data */ + if (shared) { + r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx); + } else { + r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx); + } + if (r) { + ret = r; + goto cleanup; + } + +cleanup: + if (ext_data_free) { + lyd_free_all(ext_data); + } + return ret; +} + +/** + * @brief Snode callback for schema mount. + * Check if data are valid for schema mount and returns their schema node. + */ +static LY_ERR +schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent, + const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, + const struct lysc_node **snode) +{ + LY_ERR r; + const struct lys_module *mod; + const struct ly_ctx *ext_ctx = NULL; + + /* get context based on ietf-yang-library data */ + if ((r = schema_mount_get_ctx(ext, &ext_ctx))) { + return r; + } + + /* get the module */ + mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data); + if (!mod) { + return LY_ENOT; + } + + /* get the top-level schema node */ + *snode = lys_find_child(NULL, mod, name, name_len, 0, 0); + return *snode ? LY_SUCCESS : LY_ENOT; +} + +static LY_ERR +schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, + struct ly_set **set) +{ + LY_ERR ret = LY_SUCCESS; + char *path = NULL; + + /* get all parent references of this mount point */ + if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']" + "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) { + EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup); + } + if ((ret = lyd_find_xpath(ext_data, path, set))) { + goto cleanup; + } + +cleanup: + free(path); + return ret; +} + +/** + * @brief Duplicate all accessible parent references for a shared-schema mount point. + * + * @param[in] ext Compiled extension instance. + * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions. + * @param[in] ext_data Extension data retrieved by the callback. + * @param[in] trg_ctx Mounted data context to use for duplication. + * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty. + * @return LY_ERR value. + */ +static LY_ERR +schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node, + const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set) +{ + LY_ERR ret = LY_SUCCESS; + char *path = NULL; + struct ly_set *set = NULL, *par_set = NULL; + struct lyd_node_term *term; + struct lyd_node *dup = NULL, *top_node, *first; + struct lyd_value_xpath10 *xp_val; + uint32_t i, j; + + *ref_set = NULL; + + if (!ext_data) { + /* we expect the same ext data as before and there must be some for data to be parsed */ + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "No ext data provided."); + ret = LY_EINVAL; + goto cleanup; + } + + if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) { + goto cleanup; + } + + /* prepare result set */ + if ((ret = ly_set_new(ref_set))) { + goto cleanup; + } + + first = NULL; + for (i = 0; i < set->count; ++i) { + term = set->objs[i]; + + /* get the referenced nodes (subtrees) */ + LYD_VALUE_GET(&term->value, xp_val); + if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data, + NULL, &par_set))) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.", + lyxp_get_expr(xp_val->exp)); + goto cleanup; + } + + for (j = 0; j < par_set->count; ++j) { + /* duplicate with parents in the context of the mounted data */ + if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL, + LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) { + goto cleanup; + } + + /* go top-level */ + while (dup->parent) { + dup = lyd_parent(dup); + } + + /* check whether the top-level node exists */ + if (first) { + if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) { + goto cleanup; + } + } else { + top_node = NULL; + } + + if (top_node) { + /* merge */ + ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT); + dup = NULL; + if (ret) { + goto cleanup; + } + } else { + /* insert */ + if ((ret = lyd_insert_sibling(first, dup, &first))) { + goto cleanup; + } + + /* add into the result set because a new top-level node was added */ + if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) { + goto cleanup; + } + dup = NULL; + } + } + } + +cleanup: + free(path); + ly_set_free(set, NULL); + ly_set_free(par_set, NULL); + lyd_free_tree(dup); + if (ret && *ref_set) { + if ((*ref_set)->count) { + lyd_free_siblings((*ref_set)->dnodes[0]); + } + ly_set_free(*ref_set, NULL); + *ref_set = NULL; + } + return ret; +} + +LY_ERR +lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs) +{ + LY_ERR rc; + struct ly_set *pref_set = NULL; + struct ly_set *snode_set = NULL; + struct ly_set *results_set = NULL; + struct lyd_node *ext_data; + ly_bool ext_data_free; + + /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ + if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { + return rc; + } + + LY_CHECK_GOTO(rc = schema_mount_get_parent_ref(ext, ext_data, &pref_set), cleanup); + if (pref_set->count == 0) { + goto cleanup; + } + + LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup); + + for (uint32_t i = 0; i < pref_set->count; ++i) { + struct lyd_node_term *term; + struct lyd_value_xpath10 *xp_val; + char *value; + struct ly_err_item *err; + + term = (struct lyd_node_term *)pref_set->dnodes[i]; + LYD_VALUE_GET(&term->value, xp_val); + LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup); + LY_CHECK_ERR_GOTO(rc = lys_find_xpath(ext->module->ctx, NULL, value, 0, &snode_set), free(value), cleanup); + free(value); + for (uint32_t sn = 0; sn < snode_set->count; sn++) { + LY_CHECK_GOTO(rc = ly_set_add(results_set, snode_set->snodes[sn], 0, NULL), cleanup); + } + ly_set_free(snode_set, NULL); + snode_set = NULL; + } + + *refs = results_set; + +cleanup: + if (rc) { + ly_set_free(results_set, NULL); + } + ly_set_free(snode_set, NULL); + if (ext_data_free) { + lyd_free_all(ext_data); + } + ly_set_free(pref_set, NULL); + + return rc; +} + +/** + * @brief Validate callback for schema mount. + */ +static LY_ERR +schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree, + enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff) +{ + LY_ERR ret = LY_SUCCESS; + uint32_t temp_lo = LY_LOSTORE_LAST, i; + struct ly_err_item *err; + struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree; + struct lyd_node *ext_diff = NULL, *diff_parent = NULL; + ly_bool ext_data_free = 0; + struct ly_set *ref_set = NULL; + + if (!sibling) { + /* some data had to be parsed for this callback to be called */ + EXT_LOGERR_INT_RET(NULL, ext); + } + + /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ + if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { + goto cleanup; + } + + LY_LIST_FOR(ext_data, iter) { + if (iter->flags & LYD_NEW) { + /* must be validated for the parent-reference prefix data to be stored */ + lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated."); + ret = LY_EINVAL; + goto cleanup; + } + } + + /* duplicate the referenced parent nodes into ext context */ + if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) { + goto cleanup; + } + + if (data_type != LYD_TYPE_DATA_YANG) { + /* remember the operation data tree, it may be moved */ + op_tree = sibling; + } + + /* create accessible tree, remove LYD_EXT to not call this callback recursively */ + lyd_unlink_siblings(sibling); + LY_LIST_FOR(sibling, iter) { + iter->flags &= ~LYD_EXT; + } + if (ref_set->count) { + if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) { + goto cleanup; + } + } + + /* only store messages in the context, log as an extension */ + ly_temp_log_options(&temp_lo); + + if (data_type == LYD_TYPE_DATA_YANG) { + /* validate all the modules with data */ + ret = lyd_validate_all(&sibling, NULL, val_opts | LYD_VALIDATE_PRESENT, diff ? &ext_diff : NULL); + } else { + /* validate the operation */ + ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL); + } + + /* restore logging */ + ly_temp_log_options(NULL); + + /* restore sibling tree */ + for (i = 0; i < ref_set->count; ++i) { + if (ref_set->dnodes[i] == sibling) { + sibling = sibling->next; + } + lyd_free_tree(ref_set->dnodes[i]); + } + LY_LIST_FOR(sibling, iter) { + iter->flags |= LYD_EXT; + } + lyplg_ext_insert(orig_parent, sibling); + + if (ret) { + /* log the error in the original context */ + err = ly_err_first(LYD_CTX(sibling)); + if (!err) { + lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Unknown validation error (err code %d).", ret); + } else { + lyplg_ext_compile_log_path(err->path, ext, LY_LLERR, err->no, "%s", err->msg); + } + goto cleanup; + } + + /* create proper diff */ + if (diff && ext_diff) { + /* diff nodes from an extension instance */ + LY_LIST_FOR(ext_diff, iter) { + iter->flags |= LYD_EXT; + } + + /* create the parent and insert the diff */ + if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) { + goto cleanup; + } + if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) { + goto cleanup; + } + ext_diff = NULL; + + /* go top-level and set the operation */ + while (lyd_parent(diff_parent)) { + diff_parent = lyd_parent(diff_parent); + } + if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) { + goto cleanup; + } + + /* finally merge into the global diff */ + if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) { + goto cleanup; + } + } + +cleanup: + ly_set_free(ref_set, NULL); + lyd_free_siblings(ref_first); + lyd_free_tree(ext_diff); + lyd_free_all(diff_parent); + if (ext_data_free) { + lyd_free_all(ext_data); + } + return ret; +} + +/** + * @brief Schema mount compile free. + * + * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree. + */ +static void +schema_mount_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) +{ + struct lyplg_ext_sm *sm_data = ext->compiled; + uint32_t i; + + if (!sm_data) { + return; + } + + if (!--sm_data->shared->ref_count) { + for (i = 0; i < sm_data->shared->schema_count; ++i) { + ly_ctx_destroy(sm_data->shared->schemas[i].ctx); + lydict_remove(ctx, sm_data->shared->schemas[i].content_id); + } + free(sm_data->shared->schemas); + free(sm_data->shared); + } + + for (i = 0; i < sm_data->inln.schema_count; ++i) { + ly_ctx_destroy(sm_data->inln.schemas[i].ctx); + } + free(sm_data->inln.schemas); + + pthread_mutex_destroy(&sm_data->lock); + free(sm_data); +} + +LIBYANG_API_DEF LY_ERR +lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx) +{ + struct lyd_node *ext_data = NULL; + ly_bool ext_data_free = 0, config; + LY_ERR rc = LY_SUCCESS; + + if (!ext->module->ctx->ext_clb) { + return LY_EINVAL; + } + + if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) { + return LY_EINVAL; + } + + /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ + if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { + return rc; + } + + /* learn about this mount point */ + if ((rc = schema_mount_get_smount(ext, ext_data, &config, NULL))) { + goto cleanup; + } + + /* create the context */ + rc = schema_mount_create_ctx(ext, ext_data, config, ctx); + +cleanup: + if (ext_data_free) { + lyd_free_all(ext_data); + } + return rc; +} + +static void +schema_mount_spriter_tree_free(void *priv) +{ + struct sprinter_tree_priv *st_priv; + + st_priv = priv; + ly_set_free(st_priv->refs, NULL); + ly_ctx_destroy(st_priv->ext_ctx); + free(st_priv); +} + +static LY_ERR +schema_mount_sprinter_tree_cnode_override_mounted(const struct lysc_node *node, const void *UNUSED(plugin_priv), + ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) +{ + if (!node->parent) { + *add_opts = "/"; + } + + return LY_SUCCESS; +} + +static LY_ERR +schema_mount_sprinter_tree_pnode_override_mounted(const struct lysp_node *node, const void *UNUSED(plugin_priv), + ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) +{ + if (!node->parent) { + *add_opts = "/"; + } + + return LY_SUCCESS; +} + +static LY_ERR +schema_mount_sprinter_tree_node_override_parent_refs(const struct lysc_node *node, const void *plugin_priv, + ly_bool *skip, const char **UNUSED(flags), const char **add_opts) +{ + uint32_t i; + const struct ly_set *refs; + const struct lysc_module *mod; + struct lysc_node *ref, *iter; + + refs = ((struct sprinter_tree_priv *)plugin_priv)->refs; + mod = node->module->compiled; + + /* Assume the @p node will be skipped. */ + *skip = 1; + for (i = 0; (i < refs->count) && *skip; i++) { + ref = refs->snodes[i]; + if (ref->module->compiled != mod) { + /* parent-reference points to different module */ + continue; + } + + for (iter = ref; iter; iter = iter->parent) { + if (iter == node) { + /* @p node is not skipped because it is parent-rererence node or his parent */ + *skip = 0; + break; + } + } + } + + if (!*skip && !node->parent) { + /* top-node has additional opts */ + *add_opts = "@"; + } + + return LY_SUCCESS; +} + +/** + * @brief Schema mount schema parsed tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. + */ +static LY_ERR +schema_mount_sprinter_ptree(struct lysp_ext_instance *UNUSED(ext), const struct lyspr_tree_ctx *ctx, + const char **flags, const char **UNUSED(add_opts)) +{ + if (!ctx) { + *flags = "mp"; + } + + return LY_SUCCESS; +} + +/** + * @brief Schema mount schema compiled tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. + */ +static LY_ERR +schema_mount_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **flags, const char **UNUSED(add_opts)) +{ + LY_ERR rc = LY_SUCCESS; + struct ly_ctx *ext_ctx = NULL; + const struct lys_module *mod; + struct ly_set *refs = NULL; + struct lysc_node *tree1, *tree2; + uint32_t i, j; + ly_bool from_parent_ref, is_first; + struct sprinter_tree_priv *st_priv; + + if (!ctx) { + *flags = "mp"; + return LY_SUCCESS; + } + + if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) { + /* Void mount point */ + return LY_SUCCESS; + } + + rc = lyplg_ext_schema_mount_get_parent_ref(ext, &refs); + LY_CHECK_GOTO(rc, cleanup); + + /* build new list of modules to print. This list will omit internal + * modules, modules with no nodes (e.g., iana-if-types) and modules + * that were loaded as the result of a parent-reference. + */ + i = ly_ctx_internal_modules_count(ext_ctx); + while ((mod = ly_ctx_get_module_iter(ext_ctx, &i))) { + from_parent_ref = 0; + + for (j = 0; refs && j < refs->count; j++) { + if (!strcmp(mod->ns, refs->snodes[j]->module->ns)) { + from_parent_ref = 1; + break; + } + } + if (from_parent_ref) { + /* Modules loaded as the result of a parent-reference are added later. */ + continue; + } + + /* Add data nodes, rpcs and notifications. */ + if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) { + /* For compiled module. */ + rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, mod->compiled->data, + schema_mount_sprinter_tree_cnode_override_mounted); + LY_CHECK_GOTO(rc, cleanup); + if (mod->compiled->rpcs) { + rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->rpcs->node, + schema_mount_sprinter_tree_cnode_override_mounted); + } + LY_CHECK_GOTO(rc, cleanup); + if (mod->compiled->notifs) { + rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->notifs->node, + schema_mount_sprinter_tree_cnode_override_mounted); + } + LY_CHECK_GOTO(rc, cleanup); + } else { + /* For parsed module. */ + rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, mod->parsed->data, + schema_mount_sprinter_tree_pnode_override_mounted); + LY_CHECK_GOTO(rc, cleanup); + if (mod->parsed->rpcs) { + rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->rpcs->node, + schema_mount_sprinter_tree_pnode_override_mounted); + } + LY_CHECK_GOTO(rc, cleanup); + if (mod->parsed->notifs) { + rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->notifs->node, + schema_mount_sprinter_tree_pnode_override_mounted); + } + LY_CHECK_GOTO(rc, cleanup); + } + } + + /* Add modules loaded as the result of a parent-reference. */ + for (i = 0; refs && (i < refs->count); i++) { + tree1 = refs->snodes[i]->module->compiled->data; + + /* Add data nodes from the module only once. */ + is_first = 1; + for (j = 0; j < i; j++) { + tree2 = refs->snodes[j]->module->compiled->data; + if (tree1 == tree2) { + is_first = 0; + break; + } + } + if (is_first) { + /* Add all data nodes but unavailable nodes are skipped in the callback. */ + rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, tree1, schema_mount_sprinter_tree_node_override_parent_refs); + LY_CHECK_GOTO(rc, cleanup); + } + } + + /* add private plugin data */ + st_priv = calloc(1, sizeof(*st_priv)); + LY_CHECK_ERR_GOTO(!st_priv, rc = LY_EMEM, cleanup); + st_priv->ext_ctx = ext_ctx; + st_priv->refs = refs; + rc = lyplg_ext_sprinter_tree_set_priv(ctx, st_priv, schema_mount_spriter_tree_free); + +cleanup: + if (rc) { + ly_set_free(refs, NULL); + ly_ctx_destroy(ext_ctx); + } + + return rc; +} + +/** + * @brief Plugin descriptions for the Yang Schema Mount extension. + * + * Note that external plugins are supposed to use: + * + * LYPLG_EXTENSIONS = { + */ +const struct lyplg_ext_record plugins_schema_mount[] = { + { + .module = "ietf-yang-schema-mount", + .revision = "2019-01-14", + .name = "mount-point", + + .plugin.id = "ly2 schema mount v1", + .plugin.parse = schema_mount_parse, + .plugin.compile = schema_mount_compile, + .plugin.printer_info = NULL, + .plugin.printer_ctree = schema_mount_sprinter_ctree, + .plugin.printer_ptree = schema_mount_sprinter_ptree, + .plugin.node = NULL, + .plugin.snode = schema_mount_snode, + .plugin.validate = schema_mount_validate, + .plugin.pfree = NULL, + .plugin.cfree = schema_mount_cfree + }, + {0} /* terminating zeroed item */ +}; diff --git a/src/plugins_exts/structure.c b/src/plugins_exts/structure.c new file mode 100644 index 0000000..ee7a52e --- /dev/null +++ b/src/plugins_exts/structure.c @@ -0,0 +1,558 @@ +/** + * @file structure.c + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief libyang extension plugin - structure (RFC 8791) + * + * Copyright (c) 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include <assert.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "compat.h" +#include "libyang.h" +#include "plugins_exts.h" + +struct lysp_ext_instance_structure { + struct lysp_restr *musts; + uint16_t flags; + const char *dsc; + const char *ref; + struct lysp_tpdf *typedefs; + struct lysp_node_grp *groupings; + struct lysp_node *child; +}; + +struct lysc_ext_instance_structure { + struct lysc_must *musts; + uint16_t flags; + const char *dsc; + const char *ref; + struct lysc_node *child; +}; + +struct lysp_ext_instance_augment_structure { + uint16_t flags; + const char *dsc; + const char *ref; + struct lysp_node *child; + struct lysp_node_augment *aug; +}; + +/** + * @brief Parse structure extension instances. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + LY_ERR rc; + LY_ARRAY_COUNT_TYPE u; + struct lysp_module *pmod; + struct lysp_ext_instance_structure *struct_pdata; + + /* structure can appear only at the top level of a YANG module or submodule */ + if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, + "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, + lyplg_ext_stmt2str(ext->parent_stmt)); + return LY_EVALID; + } + + pmod = ext->parent; + + /* check for duplication */ + LY_ARRAY_FOR(pmod->exts, u) { + if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { + /* duplication of the same structure extension in a single module */ + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); + return LY_EVALID; + } + } + + /* allocate the storage */ + struct_pdata = calloc(1, sizeof *struct_pdata); + if (!struct_pdata) { + goto emem; + } + ext->parsed = struct_pdata; + LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 14, rc, emem); + + /* parse substatements */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_MUST; + ext->substmts[0].storage = &struct_pdata->musts; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_STATUS; + ext->substmts[1].storage = &struct_pdata->flags; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_DESCRIPTION; + ext->substmts[2].storage = &struct_pdata->dsc; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[3].stmt = LY_STMT_REFERENCE; + ext->substmts[3].storage = &struct_pdata->ref; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[4].stmt = LY_STMT_TYPEDEF; + ext->substmts[4].storage = &struct_pdata->typedefs; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[5].stmt = LY_STMT_GROUPING; + ext->substmts[5].storage = &struct_pdata->groupings; + + /* data-def-stmt */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[6].stmt = LY_STMT_CONTAINER; + ext->substmts[6].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[7].stmt = LY_STMT_LEAF; + ext->substmts[7].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[8].stmt = LY_STMT_LEAF_LIST; + ext->substmts[8].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[9].stmt = LY_STMT_LIST; + ext->substmts[9].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[10].stmt = LY_STMT_CHOICE; + ext->substmts[10].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[11].stmt = LY_STMT_ANYDATA; + ext->substmts[11].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[12].stmt = LY_STMT_ANYXML; + ext->substmts[12].storage = &struct_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[13].stmt = LY_STMT_USES; + ext->substmts[13].storage = &struct_pdata->child; + + rc = lyplg_ext_parse_extension_instance(pctx, ext); + return rc; + +emem: + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief Compile structure extension instances. + * + * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. + */ +static LY_ERR +structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext) +{ + LY_ERR rc; + struct lysc_module *mod_c; + const struct lysc_node *child; + struct lysc_ext_instance_structure *struct_cdata; + uint32_t prev_options = *lyplg_ext_compile_get_options(cctx); + + mod_c = ext->parent; + + /* check identifier namespace with the compiled nodes */ + LY_LIST_FOR(mod_c->data, child) { + if (!strcmp(child->name, ext->argument)) { + /* identifier collision */ + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, "Extension %s collides with a %s with the same identifier.", + extp->name, lys_nodetype2str(child->nodetype)); + return LY_EVALID; + } + } + + /* allocate the storage */ + struct_cdata = calloc(1, sizeof *struct_cdata); + if (!struct_cdata) { + goto emem; + } + ext->compiled = struct_cdata; + + /* compile substatements */ + LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem); + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_MUST; + ext->substmts[0].storage = &struct_cdata->musts; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_STATUS; + ext->substmts[1].storage = &struct_cdata->flags; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_DESCRIPTION; + ext->substmts[2].storage = &struct_cdata->dsc; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[3].stmt = LY_STMT_REFERENCE; + ext->substmts[3].storage = &struct_cdata->ref; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[4].stmt = LY_STMT_TYPEDEF; + ext->substmts[4].storage = NULL; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[5].stmt = LY_STMT_GROUPING; + ext->substmts[5].storage = NULL; + + /* data-def-stmt */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[6].stmt = LY_STMT_CONTAINER; + ext->substmts[6].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[7].stmt = LY_STMT_LEAF; + ext->substmts[7].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[8].stmt = LY_STMT_LEAF_LIST; + ext->substmts[8].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[9].stmt = LY_STMT_LIST; + ext->substmts[9].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[10].stmt = LY_STMT_CHOICE; + ext->substmts[10].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[11].stmt = LY_STMT_ANYDATA; + ext->substmts[11].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[12].stmt = LY_STMT_ANYXML; + ext->substmts[12].storage = &struct_cdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[13].stmt = LY_STMT_USES; + ext->substmts[13].storage = &struct_cdata->child; + + *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; + rc = lyplg_ext_compile_extension_instance(cctx, extp, ext); + *lyplg_ext_compile_get_options(cctx) = prev_options; + if (rc) { + return rc; + } + + return LY_SUCCESS; + +emem: + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief Structure schema info printer. + * + * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info + */ +static LY_ERR +structure_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) +{ + lyplg_ext_print_info_extension_instance(ctx, ext, flag); + return LY_SUCCESS; +} + +/** + * @brief Free parsed structure extension instance data. + * + * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree. + */ +static void +structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) +{ + lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); + free(ext->parsed); +} + +/** + * @brief Free compiled structure extension instance data. + * + * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree. + */ +static void +structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) +{ + lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); + free(ext->compiled); +} + +/** + * @brief Parse augment-structure extension instances. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + LY_ERR rc; + struct lysp_stmt *stmt; + struct lysp_ext_instance_augment_structure *aug_pdata; + const struct ly_ctx *ctx = lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx; + + /* augment-structure can appear only at the top level of a YANG module or submodule */ + if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, + "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, + lyplg_ext_stmt2str(ext->parent_stmt)); + return LY_EVALID; + } + + /* augment-structure must define some data-def-stmt */ + LY_LIST_FOR(ext->child, stmt) { + if (stmt->kw & LY_STMT_DATA_NODE_MASK) { + break; + } + } + if (!stmt) { + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.", + ext->name); + return LY_EVALID; + } + + /* allocate the storage */ + aug_pdata = calloc(1, sizeof *aug_pdata); + if (!aug_pdata) { + goto emem; + } + ext->parsed = aug_pdata; + LY_ARRAY_CREATE_GOTO(ctx, ext->substmts, 13, rc, emem); + + /* parse substatements */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_STATUS; + ext->substmts[0].storage = &aug_pdata->flags; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_DESCRIPTION; + ext->substmts[1].storage = &aug_pdata->dsc; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_REFERENCE; + ext->substmts[2].storage = &aug_pdata->ref; + + /* data-def-stmt */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[3].stmt = LY_STMT_CONTAINER; + ext->substmts[3].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[4].stmt = LY_STMT_LEAF; + ext->substmts[4].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[5].stmt = LY_STMT_LEAF_LIST; + ext->substmts[5].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[6].stmt = LY_STMT_LIST; + ext->substmts[6].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[7].stmt = LY_STMT_CHOICE; + ext->substmts[7].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[8].stmt = LY_STMT_ANYDATA; + ext->substmts[8].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[9].stmt = LY_STMT_ANYXML; + ext->substmts[9].storage = &aug_pdata->child; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[10].stmt = LY_STMT_USES; + ext->substmts[10].storage = &aug_pdata->child; + + /* case */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[11].stmt = LY_STMT_CASE; + ext->substmts[11].storage = &aug_pdata->child; + + if ((rc = lyplg_ext_parse_extension_instance(pctx, ext))) { + return rc; + } + + /* add fake parsed augment node */ + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[12].stmt = LY_STMT_AUGMENT; + ext->substmts[12].storage = &aug_pdata->aug; + + aug_pdata->aug = calloc(1, sizeof *aug_pdata->aug); + if (!aug_pdata->aug) { + goto emem; + } + aug_pdata->aug->nodetype = LYS_AUGMENT; + aug_pdata->aug->flags = aug_pdata->flags; + if (lydict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid)) { + goto emem; + } + aug_pdata->aug->child = aug_pdata->child; + /* avoid double free */ + aug_pdata->child = NULL; + + return LY_SUCCESS; + +emem: + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +static LY_ERR +structure_sprinter_pnode(const struct lysp_node *UNUSED(node), const void *UNUSED(plugin_priv), + ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) +{ + *flags = ""; + return LY_SUCCESS; +} + +static LY_ERR +structure_sprinter_cnode(const struct lysc_node *UNUSED(node), const void *UNUSED(plugin_priv), + ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) +{ + *flags = ""; + return LY_SUCCESS; +} + +/** + * @brief Structure schema compiled tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. + */ +static LY_ERR +structure_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **UNUSED(flags), const char **UNUSED(add_opts)) +{ + LY_ERR rc; + + rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, structure_sprinter_cnode); + return rc; +} + +/** + * @brief Structure schema parsed tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. + */ +static LY_ERR +structure_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **UNUSED(flags), const char **UNUSED(add_opts)) +{ + LY_ERR rc; + + rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, structure_sprinter_pnode); + return rc; +} + +/** + * @brief Augment structure schema parsed tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. + */ +static LY_ERR +structure_aug_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **UNUSED(flags), const char **UNUSED(add_opts)) +{ + LY_ERR rc = LY_SUCCESS; + struct lysp_node_augment **aug; + + assert(ctx); + + aug = ext->substmts[12].storage; + rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, (*aug)->child, structure_sprinter_pnode); + + return rc; +} + +/** + * @brief Augment structure schema compiled tree printer. + * + * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. + */ +static LY_ERR +structure_aug_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, const char **flags, + const char **add_opts) +{ + LY_ERR rc = LY_SUCCESS; + + LY_ARRAY_COUNT_TYPE i; + struct lysp_ext_instance *parsed_ext; + + assert(ctx); + + /* find the parsed ext structure */ + parsed_ext = ext->module->parsed->exts; + LY_ARRAY_FOR(parsed_ext, i) { + if (!strcmp(parsed_ext[i].name, "sx:augment-structure") && !strcmp(parsed_ext[i].argument, ext->argument)) { + break; + } + } + assert(i < LY_ARRAY_COUNT(parsed_ext)); + + /* for augments print the parsed tree */ + rc = structure_aug_sprinter_ptree(parsed_ext, ctx, flags, add_opts); + return rc; +} + +/** + * @brief Plugin descriptions for the structure extension + * + * Note that external plugins are supposed to use: + * + * LYPLG_EXTENSIONS = { + */ +const struct lyplg_ext_record plugins_structure[] = { + { + .module = "ietf-yang-structure-ext", + .revision = "2020-06-17", + .name = "structure", + + .plugin.id = "ly2 structure v1", + .plugin.parse = structure_parse, + .plugin.compile = structure_compile, + .plugin.printer_info = structure_printer_info, + .plugin.printer_ctree = structure_sprinter_ctree, + .plugin.printer_ptree = structure_sprinter_ptree, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = structure_pfree, + .plugin.cfree = structure_cfree + }, + { + .module = "ietf-yang-structure-ext", + .revision = "2020-06-17", + .name = "augment-structure", + + .plugin.id = "ly2 structure v1", + .plugin.parse = structure_aug_parse, + .plugin.compile = NULL, + .plugin.printer_info = NULL, + .plugin.printer_ctree = structure_aug_sprinter_ctree, + .plugin.printer_ptree = structure_aug_sprinter_ptree, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = structure_pfree, + .plugin.cfree = NULL + }, + {0} /* terminating zeroed record */ +}; diff --git a/src/plugins_exts/yangdata.c b/src/plugins_exts/yangdata.c new file mode 100644 index 0000000..0c8f37b --- /dev/null +++ b/src/plugins_exts/yangdata.c @@ -0,0 +1,277 @@ +/** + * @file yangdata.c + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief libyang extension plugin - yang-data (RFC 8040) + * + * Copyright (c) 2021 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#include <assert.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "compat.h" +#include "libyang.h" +#include "plugins_exts.h" + +static void yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext); + +/** + * @brief Parse yang-data extension instances. + * + * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. + */ +static LY_ERR +yangdata_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) +{ + LY_ERR ret; + LY_ARRAY_COUNT_TYPE u; + struct lysp_module *pmod; + + /* yang-data can appear only at the top level of a YANG module or submodule */ + if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { + lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is ignored since it appears as a non top-level statement " + "in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt)); + return LY_ENOT; + } + + pmod = ext->parent; + + /* check for duplication */ + LY_ARRAY_FOR(pmod->exts, u) { + if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { + /* duplication of the same yang-data extension in a single module */ + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); + return LY_EVALID; + } + } + + /* parse yang-data substatements */ + LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 3, ret, emem); + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_CONTAINER; + ext->substmts[0].storage = &ext->parsed; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_CHOICE; + ext->substmts[1].storage = &ext->parsed; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_USES; + ext->substmts[2].storage = &ext->parsed; + + if ((ret = lyplg_ext_parse_extension_instance(pctx, ext))) { + return ret; + } + + return LY_SUCCESS; + +emem: + lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief Compile yang-data extension instances. + * + * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. + */ +static LY_ERR +yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext) +{ + LY_ERR ret; + const struct lysc_node *child; + ly_bool valid = 1; + uint32_t prev_options = *lyplg_ext_compile_get_options(cctx); + + /* compile yangg-data substatements */ + LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 3, ret, emem); + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[0].stmt = LY_STMT_CONTAINER; + ext->substmts[0].storage = &ext->compiled; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[1].stmt = LY_STMT_CHOICE; + ext->substmts[1].storage = &ext->compiled; + + LY_ARRAY_INCREMENT(ext->substmts); + ext->substmts[2].stmt = LY_STMT_USES; + ext->substmts[2].storage = &ext->compiled; + + *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; + ret = lyplg_ext_compile_extension_instance(cctx, extp, ext); + *lyplg_ext_compile_get_options(cctx) = prev_options; + if (ret) { + return ret; + } + + /* check that we have really just a single container data definition in the top */ + child = ext->compiled; + if (!child) { + valid = 0; + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, + "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.", + extp->name); + } else if (child->next) { + valid = 0; + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, + "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.", + extp->name); + } else if (child->nodetype == LYS_CHOICE) { + /* all the choice's case are expected to result to a single container node */ + struct lysc_module *mod_c = ext->parent; + const struct lysc_node *snode = NULL; + + while ((snode = lys_getnext(snode, child, mod_c, 0))) { + if (snode->next) { + valid = 0; + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, + "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), " + "but only a single container data node is allowed.", extp->name); + break; + } else if (snode->nodetype != LYS_CONTAINER) { + valid = 0; + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, + "Extension %s is instantiated with %s top level data node (inside a choice), " + "but only a single container data node is allowed.", extp->name, lys_nodetype2str(snode->nodetype)); + break; + } + } + } else if (child->nodetype != LYS_CONTAINER) { + /* via uses */ + valid = 0; + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, + "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.", + extp->name, lys_nodetype2str(child->nodetype)); + } + + if (!valid) { + yangdata_cfree(lyplg_ext_compile_get_ctx(cctx), ext); + ext->compiled = ext->substmts = NULL; + return LY_EVALID; + } + + return LY_SUCCESS; + +emem: + lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); + return LY_EMEM; +} + +/** + * @brief INFO printer + * + * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info + */ +static LY_ERR +yangdata_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) +{ + lyplg_ext_print_info_extension_instance(ctx, ext, flag); + return LY_SUCCESS; +} + +/** + * @brief Free parsed yang-data extension instance data. + * + * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree. + */ +static void +yangdata_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) +{ + lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); +} + +/** + * @brief Free compiled yang-data extension instance data. + * + * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree. + */ +static void +yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) +{ + lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); +} + +static void +yangdata_sprinter_node(uint16_t nodetype, const char **flags) +{ + if (nodetype & LYS_USES) { + *flags = "-u"; + } else { + *flags = "--"; + } +} + +static LY_ERR +yangdata_sprinter_cnode(const struct lysc_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip), + const char **flags, const char **UNUSED(add_opts)) +{ + yangdata_sprinter_node(node->nodetype, flags); + return LY_SUCCESS; +} + +static LY_ERR +yangdata_sprinter_pnode(const struct lysp_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip), + const char **flags, const char **UNUSED(add_opts)) +{ + yangdata_sprinter_node(node->nodetype, flags); + return LY_SUCCESS; +} + +static LY_ERR +yangdata_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **UNUSED(flags), const char **UNUSED(add_opts)) +{ + LY_ERR rc = LY_SUCCESS; + + assert(ctx); + rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, yangdata_sprinter_cnode); + return rc; +} + +static LY_ERR +yangdata_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, + const char **UNUSED(flags), const char **UNUSED(add_opts)) +{ + LY_ERR rc = LY_SUCCESS; + + assert(ctx); + rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, yangdata_sprinter_pnode); + return rc; +} + +/** + * @brief Plugin descriptions for the yang-data extension + * + * Note that external plugins are supposed to use: + * + * LYPLG_EXTENSIONS = { + */ +const struct lyplg_ext_record plugins_yangdata[] = { + { + .module = "ietf-restconf", + .revision = "2017-01-26", + .name = "yang-data", + + .plugin.id = "ly2 yang-data v1", + .plugin.parse = yangdata_parse, + .plugin.compile = yangdata_compile, + .plugin.printer_info = yangdata_printer_info, + .plugin.printer_ctree = yangdata_sprinter_ctree, + .plugin.printer_ptree = yangdata_sprinter_ptree, + .plugin.node = NULL, + .plugin.snode = NULL, + .plugin.validate = NULL, + .plugin.pfree = yangdata_pfree, + .plugin.cfree = yangdata_cfree + }, + {0} /* terminating zeroed record */ +}; |