Coverage Report

Created: 2026-01-25 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/plugins_exts/metadata.c
Line
Count
Source
1
/**
2
 * @file metadata.c
3
 * @author Radek Krejci <rkrejci@cesnet.cz>
4
 * @brief libyang extension plugin - Metadata (RFC 7952)
5
 *
6
 * Copyright (c) 2019 CESNET, z.s.p.o.
7
 *
8
 * This source code is licensed under BSD 3-Clause License (the "License").
9
 * You may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     https://opensource.org/licenses/BSD-3-Clause
13
 */
14
15
#include "metadata.h"
16
17
#include <stdint.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "libyang.h"
22
#include "plugins_exts.h"
23
24
/**
25
 * @brief Representation of the compiled metadata substatements - simplify storage for the items available via
26
 * ::lysc_ext_substmt.
27
 */
28
struct lyext_metadata {
29
    struct lysc_type *type;            /**< type of the metadata (mandatory) */
30
    const char *units;                 /**< units of the leaf's type */
31
    struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
32
    const char *dsc;                   /**< description */
33
    const char *ref;                   /**< reference */
34
    uint16_t flags;                    /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
35
};
36
37
/**
38
 * @brief Compile annotation extension instances.
39
 *
40
 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
41
 */
42
static LY_ERR
43
annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
44
0
{
45
0
    LY_ERR ret;
46
0
    struct lyext_metadata *annotation;
47
0
    struct lysc_module *mod_c;
48
0
    LY_ARRAY_COUNT_TYPE u;
49
50
    /* annotations can appear only at the top level of a YANG module or submodule */
51
0
    if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
52
0
        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
53
0
                "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
54
0
                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
55
0
        return LY_EVALID;
56
0
    }
57
    /* check mandatory argument */
58
0
    if (!c_ext->argument) {
59
0
        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
60
0
                "Extension %s is instantiated without mandatory argument representing metadata name.", p_ext->name);
61
0
        return LY_EVALID;
62
0
    }
63
64
0
    mod_c = (struct lysc_module *)c_ext->parent;
65
66
    /* check for duplication */
67
0
    LY_ARRAY_FOR(mod_c->exts, u) {
68
0
        if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
69
            /* duplication of the same annotation extension in a single module */
70
0
            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
71
0
            return LY_EVALID;
72
0
        }
73
0
    }
74
75
    /* compile annotation substatements */
76
0
    c_ext->data = annotation = calloc(1, sizeof *annotation);
77
0
    if (!annotation) {
78
0
        goto emem;
79
0
    }
80
0
    LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), c_ext->substmts, 6, ret, emem);
81
82
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
83
0
    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE;
84
0
    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].cardinality = LY_STMT_CARD_ANY;
85
0
    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
86
87
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
88
0
    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].stmt = LY_STMT_UNITS;
89
0
    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].cardinality = LY_STMT_CARD_OPT;
90
0
    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
91
92
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
93
0
    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].stmt = LY_STMT_STATUS;
94
0
    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].cardinality = LY_STMT_CARD_OPT;
95
0
    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
96
97
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
98
0
    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].stmt = LY_STMT_TYPE;
99
0
    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].cardinality = LY_STMT_CARD_MAND;
100
0
    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
101
102
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
103
0
    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].stmt = LY_STMT_DESCRIPTION;
104
0
    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].cardinality = LY_STMT_CARD_OPT;
105
0
    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].storage = &annotation->dsc;
106
107
0
    LY_ARRAY_INCREMENT(c_ext->substmts);
108
0
    c_ext->substmts[ANNOTATION_SUBSTMT_REF].stmt = LY_STMT_REFERENCE;
109
0
    c_ext->substmts[ANNOTATION_SUBSTMT_REF].cardinality = LY_STMT_CARD_OPT;
110
0
    c_ext->substmts[ANNOTATION_SUBSTMT_REF].storage = &annotation->ref;
111
112
0
    ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
113
0
    return ret;
114
115
0
emem:
116
0
    lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
117
0
    return LY_EMEM;
118
0
}
119
120
/**
121
 * @brief INFO printer
122
 *
123
 * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter
124
 */
125
static LY_ERR
126
annotation_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
127
0
{
128
0
    lysc_print_extension_instance(ctx, ext, flag);
129
130
0
    return LY_SUCCESS;
131
0
}
132
133
/**
134
 * @brief Free annotation extension instances' data.
135
 *
136
 * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
137
 */
138
static void
139
annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
140
0
{
141
0
    if (!ext->substmts) {
142
0
        return;
143
0
    }
144
145
0
    lyplg_ext_instance_substatements_free(ctx, ext->substmts);
146
0
    free(ext->data);
147
0
}
148
149
/**
150
 * @brief Plugin descriptions for the Metadata's annotation extension
151
 *
152
 * Note that external plugins are supposed to use:
153
 *
154
 *   LYPLG_EXTENSIONS = {
155
 */
156
const struct lyplg_ext_record plugins_metadata[] = {
157
    {
158
        .module = "ietf-yang-metadata",
159
        .revision = "2016-08-05",
160
        .name = "annotation",
161
162
        .plugin.id = "libyang 2 - metadata, version 1",
163
        .plugin.compile = &annotation_compile,
164
        .plugin.validate = NULL,
165
        .plugin.sprinter = &annotation_schema_printer,
166
        .plugin.free = annotation_free
167
    },
168
    {0}     /* terminating zeroed record */
169
};