blob: 180de16a26758ad0d86a08c0bbf62d3f480d2acf [file] [log] [blame]
Radek Krejci5fa32a32021-02-08 18:12:38 +01001/**
2 * @file plugins_exts_yangdata.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang extension plugin - yang-data (RFC 8040)
5 *
6 * Copyright (c) 2021 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 <stdlib.h>
16
Radek Krejci5fa32a32021-02-08 18:12:38 +010017#include "plugins_exts.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010018
19#include "tree_edit.h"
Radek Krejci5fa32a32021-02-08 18:12:38 +010020#include "tree_schema.h"
21
22/**
23 * @brief Storage for ID used to check plugin API version compatibility.
24 * Ignored here in the internal plugin.
25LYEXT_VERSION_CHECK
26 */
27
28/**
29 * @brief Free yang-data extension instances' data.
30 *
31 * Implementation of ::lyext_clb_free callback set as lyext_plugin::free.
32 */
33void
34yangdata_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
35{
36 lysc_extension_instance_substatements_free(ctx, ext->substmts);
37}
38
39/**
40 * @brief Compile yang-data extension instances.
41 *
42 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
43 */
44LY_ERR
45yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
46{
47 LY_ERR ret;
48 LY_ARRAY_COUNT_TYPE u;
49 struct lysc_module *mod_c;
50 const struct lysc_node *child;
51 ly_bool valid = 1;
Radek Krejci5f9a3672021-03-05 21:35:22 +010052 uint32_t prev_options = *lysc_ctx_get_options(cctx);
Radek Krejci5fa32a32021-02-08 18:12:38 +010053
54 /* yang-data can appear only at the top level of a YANG module or submodule */
Radek Krejciab430862021-03-02 20:13:40 +010055 if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
Radek Krejci5f9a3672021-03-05 21:35:22 +010056 lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +010057 "Extension %s is ignored since it appears as a non top-level statement in \"%s\" statement.",
Radek Krejciab430862021-03-02 20:13:40 +010058 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
Radek Krejci5fa32a32021-02-08 18:12:38 +010059 return LY_ENOT;
60 }
Radek Krejci5fa32a32021-02-08 18:12:38 +010061
62 mod_c = (struct lysc_module *)c_ext->parent;
63
64 /* check for duplication */
65 LY_ARRAY_FOR(mod_c->exts, u) {
66 if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
67 /* duplication of the same yang-data extension in a single module */
Radek Krejci5f9a3672021-03-05 21:35:22 +010068 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
Radek Krejci5fa32a32021-02-08 18:12:38 +010069 return LY_EVALID;
70 }
71 }
72
73 /* compile annotation substatements
74 * To let the compilation accept different statements possibly leading to the container top-level node, there are 3
75 * allowed substatements pointing to a single storage. But when compiled, the substaments list is compressed just to
76 * a single item providing the schema tree. */
Radek Krejci859a15a2021-03-05 20:56:59 +010077 LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 3, ret, emem);
Radek Krejci5fa32a32021-02-08 18:12:38 +010078 LY_ARRAY_INCREMENT(c_ext->substmts);
79 c_ext->substmts[0].stmt = LY_STMT_CONTAINER;
80 c_ext->substmts[0].cardinality = LY_STMT_CARD_OPT;
81 c_ext->substmts[0].storage = &c_ext->data;
82
83 LY_ARRAY_INCREMENT(c_ext->substmts);
84 c_ext->substmts[1].stmt = LY_STMT_CHOICE;
85 c_ext->substmts[1].cardinality = LY_STMT_CARD_OPT;
86 c_ext->substmts[1].storage = &c_ext->data;
87
88 LY_ARRAY_INCREMENT(c_ext->substmts);
89 c_ext->substmts[2].stmt = LY_STMT_USES;
90 c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT;
91 c_ext->substmts[2].storage = &c_ext->data;
92
Radek Krejci5f9a3672021-03-05 21:35:22 +010093 *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
Radek Krejci5fa32a32021-02-08 18:12:38 +010094 ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
Radek Krejci5f9a3672021-03-05 21:35:22 +010095 *lysc_ctx_get_options(cctx) = prev_options;
Radek Krejci5fa32a32021-02-08 18:12:38 +010096 LY_ARRAY_DECREMENT(c_ext->substmts);
97 LY_ARRAY_DECREMENT(c_ext->substmts);
Radek Krejci5f9a3672021-03-05 21:35:22 +010098 if (ret) {
99 return ret;
100 }
Radek Krejci5fa32a32021-02-08 18:12:38 +0100101
102 /* check that we have really just a single container data definition in the top */
103 child = *(struct lysc_node **)c_ext->substmts[0].storage;
104 if (!child) {
105 valid = 0;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100106 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +0100107 "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.",
108 p_ext->name);
109 } else if (child->next) {
110 valid = 0;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100111 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +0100112 "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.",
113 p_ext->name);
114 } else if (child->nodetype == LYS_CHOICE) {
115 /* all the choice's case are expected to result to a single container node */
116 const struct lysc_node *snode = NULL;
117
118 while ((snode = lys_getnext(snode, child, mod_c, 0))) {
119 if (snode->next) {
120 valid = 0;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100121 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +0100122 "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), "
123 "but only a single container data node is allowed.", p_ext->name);
124 break;
125 } else if (snode->nodetype != LYS_CONTAINER) {
126 valid = 0;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100127 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +0100128 "Extension %s is instantiated with %s top level data node (inside a choice), "
129 "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype));
130 break;
131 }
132 }
133 } else if (child->nodetype != LYS_CONTAINER) {
134 /* via uses */
135 valid = 0;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100136 lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
Radek Krejci5fa32a32021-02-08 18:12:38 +0100137 "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.",
138 p_ext->name, lys_nodetype2str(child->nodetype));
139 }
140
141 if (!valid) {
Radek Krejci5f9a3672021-03-05 21:35:22 +0100142 yangdata_free(lysc_ctx_get_ctx(cctx), c_ext);
Radek Krejci5fa32a32021-02-08 18:12:38 +0100143 c_ext->data = c_ext->substmts = NULL;
144 return LY_EVALID;
145 }
146
147 return LY_SUCCESS;
Radek Krejci859a15a2021-03-05 20:56:59 +0100148
149emem:
Radek Krejci5f9a3672021-03-05 21:35:22 +0100150 lyext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
Radek Krejci859a15a2021-03-05 20:56:59 +0100151 return LY_EMEM;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100152}
153
154/**
Radek Krejciadcf63d2021-02-09 10:21:18 +0100155 * @brief INFO printer
156 *
157 * Implementation of ::lyext_clb_schema_printer set as ::lyext_plugin::sprinter
158 */
159LY_ERR
Radek Krejcif8d7f9a2021-03-10 14:32:36 +0100160yangdata_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
Radek Krejciadcf63d2021-02-09 10:21:18 +0100161{
162 lysc_print_extension_instance(ctx, ext, flag);
163 return LY_SUCCESS;
164}
165
166/**
Radek Krejci5fa32a32021-02-08 18:12:38 +0100167 * @brief Plugin for the yang-data extension
168 */
169struct lyext_plugin yangdata_plugin = {
170 .id = "libyang 2 - yang-data, version 1",
171 .compile = &yangdata_compile,
172 .validate = NULL,
Radek Krejciadcf63d2021-02-09 10:21:18 +0100173 .sprinter = &yangdata_schema_printer,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100174 .free = yangdata_free
175};