Coverage Report

Created: 2026-06-07 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/plugins_exts/yangdata.c
Line
Count
Source
1
/**
2
 * @file yangdata.c
3
 * @author Radek Krejci <rkrejci@cesnet.cz>
4
 * @author Michal Vasko <mvasko@cesnet.cz>
5
 * @brief libyang extension plugin - yang-data (RFC 8040)
6
 *
7
 * Copyright (c) 2021 - 2026 CESNET, z.s.p.o.
8
 *
9
 * This source code is licensed under BSD 3-Clause License (the "License").
10
 * You may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     https://opensource.org/licenses/BSD-3-Clause
14
 */
15
16
#include <assert.h>
17
#include <stdint.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "compat.h"
22
#include "libyang.h"
23
#include "ly_common.h"
24
#include "plugins_exts.h"
25
#include "plugins_types.h"
26
27
static LY_ERR yangdata_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
28
        const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name,
29
        uint32_t name_len, const struct lysc_node **snode);
30
static void yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext);
31
32
/**
33
 * @brief Parse yang-data extension instances.
34
 *
35
 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
36
 */
37
static LY_ERR
38
yangdata_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
39
0
{
40
0
    LY_ERR ret;
41
0
    LY_ARRAY_COUNT_TYPE u;
42
0
    struct lysp_module *pmod;
43
44
    /* yang-data can appear only at the top level of a YANG module or submodule */
45
0
    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
46
0
        lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is ignored since it appears as a non top-level statement "
47
0
                "in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
48
0
        return LY_ENOT;
49
0
    }
50
51
0
    pmod = ext->parent;
52
53
    /* check for duplication */
54
0
    LY_ARRAY_FOR(pmod->exts, u) {
55
0
        if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
56
            /* duplication of the same yang-data extension in a single module */
57
0
            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
58
0
            return LY_EVALID;
59
0
        }
60
0
    }
61
62
    /* parse yang-data substatements */
63
0
    LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 3, ret, emem);
64
0
    LY_ARRAY_INCREMENT(ext->substmts);
65
0
    ext->substmts[0].stmt = LY_STMT_CONTAINER;
66
0
    ext->substmts[0].storage_p = (void **)&ext->parsed;
67
68
0
    LY_ARRAY_INCREMENT(ext->substmts);
69
0
    ext->substmts[1].stmt = LY_STMT_CHOICE;
70
0
    ext->substmts[1].storage_p = (void **)&ext->parsed;
71
72
0
    LY_ARRAY_INCREMENT(ext->substmts);
73
0
    ext->substmts[2].stmt = LY_STMT_USES;
74
0
    ext->substmts[2].storage_p = (void **)&ext->parsed;
75
76
0
    if ((ret = lyplg_ext_parse_extension_instance(pctx, ext))) {
77
0
        return ret;
78
0
    }
79
80
0
    return LY_SUCCESS;
81
82
0
emem:
83
0
    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
84
0
    return LY_EMEM;
85
0
}
86
87
/**
88
 * @brief Compile yang-data extension instances.
89
 *
90
 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
91
 */
92
static LY_ERR
93
yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
94
0
{
95
0
    LY_ERR ret;
96
0
    const struct lysc_node *child;
97
0
    ly_bool valid = 1;
98
0
    uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);
99
100
    /* compile yangg-data substatements */
101
0
    LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 3, ret, emem);
102
0
    LY_ARRAY_INCREMENT(ext->substmts);
103
0
    ext->substmts[0].stmt = LY_STMT_CONTAINER;
104
0
    ext->substmts[0].storage_p = (void **)&ext->compiled;
105
106
0
    LY_ARRAY_INCREMENT(ext->substmts);
107
0
    ext->substmts[1].stmt = LY_STMT_CHOICE;
108
0
    ext->substmts[1].storage_p = (void **)&ext->compiled;
109
110
0
    LY_ARRAY_INCREMENT(ext->substmts);
111
0
    ext->substmts[2].stmt = LY_STMT_USES;
112
0
    ext->substmts[2].storage_p = (void **)&ext->compiled;
113
114
0
    *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
115
0
    ret = lyplg_ext_compile_extension_instance(cctx, extp, ext, NULL);
116
0
    *lyplg_ext_compile_get_options(cctx) = prev_options;
117
0
    if (ret) {
118
0
        return ret;
119
0
    }
120
121
    /* check that we have really just a single container data definition in the top */
122
0
    child = ext->compiled;
123
0
    if (!child) {
124
0
        valid = 0;
125
0
        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
126
0
                "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.",
127
0
                extp->name);
128
0
    } else if (child->next) {
129
0
        valid = 0;
130
0
        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
131
0
                "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.",
132
0
                extp->name);
133
0
    } else if (child->nodetype == LYS_CHOICE) {
134
        /* all the choice's case are expected to result to a single container node */
135
0
        struct lysc_module *mod_c = ext->parent;
136
0
        const struct lysc_node *snode = NULL;
137
138
0
        while ((snode = lys_getnext(snode, child, mod_c, 0))) {
139
0
            if (snode->next) {
140
0
                valid = 0;
141
0
                lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
142
0
                        "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), "
143
0
                        "but only a single container data node is allowed.", extp->name);
144
0
                break;
145
0
            } else if (snode->nodetype != LYS_CONTAINER) {
146
0
                valid = 0;
147
0
                lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
148
0
                        "Extension %s is instantiated with %s top level data node (inside a choice), "
149
0
                        "but only a single container data node is allowed.", extp->name, lys_nodetype2str(snode->nodetype));
150
0
                break;
151
0
            }
152
0
        }
153
0
    } else if (child->nodetype != LYS_CONTAINER) {
154
        /* via uses */
155
0
        valid = 0;
156
0
        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
157
0
                "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.",
158
0
                extp->name, lys_nodetype2str(child->nodetype));
159
0
    }
160
161
0
    if (!valid) {
162
0
        yangdata_cfree(lyplg_ext_compile_get_ctx(cctx), ext);
163
0
        ext->compiled = ext->substmts = NULL;
164
0
        return LY_EVALID;
165
0
    }
166
167
0
    return LY_SUCCESS;
168
169
0
emem:
170
0
    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
171
0
    return LY_EMEM;
172
0
}
173
174
/**
175
 * @brief INFO printer
176
 *
177
 * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info
178
 */
179
static LY_ERR
180
yangdata_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
181
0
{
182
0
    lyplg_ext_print_info_extension_instance(ctx, ext, flag);
183
0
    return LY_SUCCESS;
184
0
}
185
186
/**
187
 * @brief Snode xpath callback for yang-data.
188
 */
189
static LY_ERR
190
yangdata_snode_xpath(struct lysc_ext_instance *ext, const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format,
191
        void *prefix_data, const char *name, uint32_t name_len, const struct lysc_node **snode)
192
0
{
193
0
    return yangdata_snode(ext, NULL, NULL, prefix, prefix_len, format, prefix_data, name, name_len, snode);
194
0
}
195
196
/**
197
 * @brief Snode callback for yang-data.
198
 */
199
static LY_ERR
200
yangdata_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
201
        const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name,
202
        uint32_t name_len, const struct lysc_node **snode)
203
0
{
204
0
    const struct lysc_node *schema = ext->compiled;
205
0
    const struct lys_module *mod;
206
207
0
    if (parent || sparent) {
208
        /* matches only top-level data */
209
0
        return LY_ENOT;
210
0
    }
211
212
0
    if (prefix && prefix_len) {
213
        /* check module */
214
0
        mod = lys_find_module(ext->module->ctx, NULL, prefix, prefix_len, format, prefix_data);
215
0
        if (!mod || (ext->module != mod)) {
216
0
            return LY_ENOT;
217
0
        }
218
0
    }
219
220
    /* check name */
221
0
    if (name && name_len) {
222
0
        LY_LIST_FOR(schema, schema) {
223
0
            if (!ly_strncmp(schema->name, name, name_len)) {
224
0
                break;
225
0
            }
226
0
        }
227
0
    }
228
229
    /* first top-level node or the found one, if any */
230
0
    *snode = schema;
231
0
    return schema ? LY_SUCCESS : LY_ENOT;
232
0
}
233
234
/**
235
 * @brief Validate callback for yang-data.
236
 */
237
static LY_ERR
238
yangdata_validate(struct lysc_ext_instance *ext, struct lyd_node *node, const struct lyd_node *UNUSED(dep_tree),
239
        enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
240
0
{
241
0
    if (data_type != LYD_TYPE_DATA_YANG) {
242
        /* not supported */
243
0
        return LY_ENOT;
244
0
    }
245
246
    /* validate all the modules with data */
247
0
    return lyd_validate_ext(&node, ext, val_opts, diff);
248
0
}
249
250
/**
251
 * @brief Free parsed yang-data extension instance data.
252
 *
253
 * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
254
 */
255
static void
256
yangdata_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
257
0
{
258
0
    lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
259
0
}
260
261
/**
262
 * @brief Free compiled yang-data extension instance data.
263
 *
264
 * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
265
 */
266
static void
267
yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
268
0
{
269
0
    lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
270
0
}
271
272
static int
273
yandgata_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr_ht)
274
0
{
275
0
    return lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
276
0
}
277
278
static LY_ERR
279
yangdata_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_ext_instance *ext, struct ly_ht *addr_ht,
280
        struct ly_set *ptr_set, void **mem)
281
0
{
282
0
    ext->compiled = NULL;
283
0
    ext->substmts[0].storage_p = (void **)&ext->compiled;
284
0
    ext->substmts[1].storage_p = (void **)&ext->compiled;
285
0
    ext->substmts[2].storage_p = (void **)&ext->compiled;
286
287
0
    return lyplg_ext_compiled_stmts_storage_print(orig_ext->substmts, ext->substmts, addr_ht, ptr_set, mem);
288
0
}
289
290
/**
291
 * @brief Plugin descriptions for the yang-data extension
292
 *
293
 * Note that external plugins are supposed to use:
294
 *
295
 *   LYPLG_EXTENSIONS = {
296
 */
297
const struct lyplg_ext_record plugins_yangdata[] = {
298
    {
299
        .module = "ietf-restconf",
300
        .revision = "2017-01-26",
301
        .name = "yang-data",
302
303
        .plugin.id = "ly2 yang-data",
304
        .plugin.parse = yangdata_parse,
305
        .plugin.compile = yangdata_compile,
306
        .plugin.printer_info = yangdata_printer_info,
307
        .plugin.node_xpath = NULL,
308
        .plugin.snode_xpath = yangdata_snode_xpath,
309
        .plugin.snode = yangdata_snode,
310
        .plugin.validate = yangdata_validate,
311
        .plugin.pfree = yangdata_pfree,
312
        .plugin.cfree = yangdata_cfree,
313
        .plugin.compiled_size = yandgata_compiled_size,
314
        .plugin.compiled_print = yangdata_compiled_print
315
    },
316
    {0}     /* terminating zeroed record */
317
};