blob: 1e8364c9316f569c1ad497b08414d2da078b232f [file] [log] [blame]
/**
* @file plugins_exts_nacm.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @brief libyang extension plugin - Metadata (RFC 7952)
*
* Copyright (c) 2019 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 "common.h"
#include <stdlib.h>
#include "plugins_exts.h"
#include "plugins_exts_metadata.h"
#include "tree_schema.h"
/**
* @brief Storage for ID used to check plugin API version compatibility.
* Ignored here in the internal plugin.
LYEXT_VERSION_CHECK
*/
struct lysc_ext_substmt annotation_substmt[7] = {
{LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL},
{LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL},
{LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL},
{LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL},
{LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL},
{LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL},
{0, 0, 0} /* terminating item */
};
/**
* @brief Compile annotation extension instances.
*
* Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
*/
LY_ERR
annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
{
struct lyext_metadata *annotation;
struct lysc_module *mod_c;
LY_ARRAY_COUNT_TYPE u;
/* annotations can appear only at the top level of a YANG module or submodule */
if (c_ext->parent_type != LYEXT_PAR_MODULE) {
lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
p_ext->name, lyext_parent2str(c_ext->parent_type));
return LY_EVALID;
}
/* check mandatory argument */
if (!c_ext->argument) {
lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated without mandatory argument representing metadata name.",
p_ext->name);
return LY_EVALID;
}
mod_c = (struct lysc_module *)c_ext->parent;
/* check for duplication */
LY_ARRAY_FOR(mod_c->exts, u) {
if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
/* duplication of the same annotation extension in a single module */
lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
return LY_EVALID;
}
}
/* compile annotation substatements */
c_ext->data = annotation = calloc(1, sizeof *annotation);
LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM);
annotation_substmt[0].storage = &annotation->iffeatures;
annotation_substmt[1].storage = &annotation->units;
annotation_substmt[2].storage = &annotation->flags;
annotation_substmt[3].storage = &annotation->type;
/* description and reference are allowed, but not compiled */
LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt));
return LY_SUCCESS;
}
/**
* @brief Free annotation extension instances' data.
*
* Implementation of lyext_clb_free callback set as lyext_plugin::free.
*/
void
annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
{
if (!ext->data) {
return;
}
struct lyext_metadata *annotation = (struct lyext_metadata *)ext->data;
annotation_substmt[0].storage = &annotation->iffeatures;
annotation_substmt[1].storage = &annotation->units;
annotation_substmt[2].storage = &annotation->flags;
annotation_substmt[3].storage = &annotation->type;
lysc_extension_instance_free(ctx, annotation_substmt);
free(ext->data);
}
/**
* @brief Plugin for the Metadata's annotation extension
*/
struct lyext_plugin metadata_plugin = {
.id = "libyang 2 - metadata, version 1",
.compile = &annotation_compile,
.validate = NULL,
.free = annotation_free
};