blob: e232883ff502e6ee29f9f4b6909d4e571c9cd2f5 [file] [log] [blame]
Michal Vaskocde73ac2019-11-14 16:10:27 +01001/**
2 * @file validation.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Validation
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 <assert.h>
16#include <string.h>
17
18#include "common.h"
19#include "xpath.h"
20#include "tree_data_internal.h"
21
22/**
23 * @brief Evaluate a single "when" condition.
24 *
25 * @param[in] when When to evaluate.
26 * @param[in] node Node whose existence depends on this when.
27 * @param[in] trees Array of all data trees.
28 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
29 */
30static LY_ERR
31lyd_val_when(struct lysc_when *when, struct lyd_node *node, const struct lyd_node **trees)
32{
33 LY_ERR ret;
34 const struct lyd_node *ctx_node;
35 struct lyxp_set xp_set;
36
37 memset(&xp_set, 0, sizeof xp_set);
38
39 if (when->context == node->schema) {
40 ctx_node = node;
41 } else {
42 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
43 ctx_node = (struct lyd_node *)node->parent;
44 }
45
46 /* evaluate when */
47 ret = lyxp_eval(when->cond, LYD_UNKNOWN, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG,
48 trees, &xp_set, LYXP_SCHEMA);
49 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
50
51 /* return error or LY_EINCOMPLETE for dependant unresolved when */
52 LY_CHECK_RET(ret);
53
54 /* take action based on the result */
55 if (!xp_set.val.bool) {
56 if (node->flags & LYD_WHEN_TRUE) {
57 /* autodelete */
58 lyd_free_tree(node);
59 } else {
60 /* invalid data */
61 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
62 ret = LY_EVALID;
63 }
64 } else {
65 /* remember that when evaluated to true */
66 node->flags |= LYD_WHEN_TRUE;
67 }
68
69 return ret;
70}
71
72LY_ERR
73lyd_validate_unres(struct ly_set *node_types, struct ly_set *attr_types, struct ly_set *node_when, LYD_FORMAT format,
74 ly_clb_resolve_prefix get_prefix_clb, void *parser_data, const struct lyd_node **trees)
75{
76 LY_ERR ret = LY_SUCCESS;
77 uint32_t u;
78
79 /* finish incompletely validated terminal values */
80 for (u = 0; node_types && (u < node_types->count); u++) {
81 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[u];
82
83 /* validate and store the value of the node */
84 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
85 parser_data, format, trees);
86 LY_CHECK_RET(ret);
87 }
88
89 /* ... and attribute values */
90 for (u = 0; attr_types && (u < attr_types->count); u++) {
91 struct lyd_attr *attr = (struct lyd_attr *)attr_types->objs[u];
92
93 /* validate and store the value of the node */
94 ret = lyd_value_parse_attr(attr, attr->value.original, strlen(attr->value.original), 0, 1, get_prefix_clb,
95 parser_data, format, trees);
96 LY_CHECK_RET(ret);
97 }
98
99 /* no when conditions */
100 if (!node_when || !node_when->count) {
101 return ret;
102 }
103
104 /* evaluate all when conditions */
105 uint32_t prev_count;
106 do {
107 prev_count = node_when->count;
108 u = 0;
109 while (u < node_when->count) {
110 /* evaluate all when expressions that affect this node's existence */
111 struct lyd_node *node = (struct lyd_node *)node_when->objs[u];
112 const struct lysc_node *schema = node->schema;
113 int unres_when = 0;
114
115 do {
116 uint32_t i;
117 LY_ARRAY_FOR(schema->when, i) {
118 ret = lyd_val_when(schema->when[i], node, trees);
119 if (ret) {
120 break;
121 }
122 }
123 if (ret == LY_EINCOMPLETE) {
124 /* could not evaluate this when */
125 unres_when = 1;
126 break;
127 } else if (ret) {
128 /* error */
129 return ret;
130 }
131 schema = schema->parent;
132 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
133
134 if (unres_when) {
135 /* keep in set and go to the next node */
136 ++u;
137 } else {
138 /* remove this node from the set */
139 ly_set_rm_index(node_when, u, NULL);
140 }
141 }
142
143 /* there must have been some when conditions resolved */
144 } while (prev_count > node_when->count);
145
146 /* there could have been no cyclic when dependencies, checked during compilation */
147 assert(!node_when->count);
148
149 return ret;
150}
151
152static const struct lys_module *
153lyd_val_next_module(const struct lys_module **modules, int mod_count, struct ly_ctx *ctx, uint32_t *i)
154{
155 if (modules && mod_count) {
156 return modules[(*i)++];
157 }
158
159 return ly_ctx_get_module_iter(ctx, i);
160}
161
162static LY_ERR
Michal Vaskoa3881362020-01-21 15:57:35 +0100163lyd_validate_mandatory(const struct lysc_node *snode, struct lyd_node *sibling)
164{
165 struct lyd_node *node;
166 int is_choice = 0;
167
168 if (snode->nodetype == LYS_CHOICE) {
169 is_choice = 1;
170 }
171
Michal Vaskoacd83e72020-02-04 14:12:01 +0100172 LY_LIST_FOR(sibling, node) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100173 if (is_choice) {
174 if (node->schema->parent && (node->schema->parent->nodetype & LYS_CASE) && (node->schema->parent->parent == snode)) {
175 /* case data instance found */
176 return LY_SUCCESS;
177 }
178 } else {
179 if (node->schema == snode) {
180 /* data instance found */
181 return LY_SUCCESS;
182 }
183 }
184 }
185
186 /* node instance not found */
187 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
188 return LY_EVALID;
189}
190
191static LY_ERR
192lyd_validate_minmax(const struct lysc_node *snode, uint32_t min, uint32_t max, struct lyd_node *sibling)
193{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100194 uint32_t count = 0;
195 struct lyd_node *iter;
196
197 LY_LIST_FOR(sibling, iter) {
198 if (iter->schema == snode) {
199 ++count;
200 }
201 }
202
203 if (min && (count < min)) {
204 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
205 return LY_EVALID;
206 } else if (max && (count > max)) {
207 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
208 return LY_EVALID;
209 }
210
Michal Vaskoa3881362020-01-21 15:57:35 +0100211 return LY_SUCCESS;
212}
213
214static LY_ERR
215lyd_validate_unique(const struct lysc_node *snode, struct lysc_node_leaf ***uniques, struct lyd_node *sibling)
216{
217 /* TODO check uniques in siblings and children */
218 return LY_SUCCESS;
219}
220
221static LY_ERR
222lyd_validate_cases(const struct lysc_node_case *cases, struct lyd_node *sibling)
223{
224 /* TODO check there are nodes only from a single case,
225 * what if not? validation error or autodelete */
226 return LY_SUCCESS;
227}
228
229static LY_ERR
Michal Vaskoacd83e72020-02-04 14:12:01 +0100230lyd_validate_siblings_schema_r(struct lyd_node *sibling, const struct lysc_node *sparent, const struct lysc_module *mod,
231 int options)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100232{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100233 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100234 struct lysc_node_list *slist;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100235
Michal Vaskoa3881362020-01-21 15:57:35 +0100236 /* disabled nodes are skipped by lys_getnext */
237 while ((snode = lys_getnext(snode, sparent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100238 /* check min-elements and max-elements */
239 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
240 slist = (struct lysc_node_list *)snode;
241 if (slist->min || slist->max) {
242 LY_CHECK_RET(lyd_validate_minmax(snode, slist->min, slist->max, sibling));
243 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100244
245 /* check generic mandatory existence */
246 } else if (snode->flags & LYS_MAND_TRUE) {
247 LY_CHECK_RET(lyd_validate_mandatory(snode, sibling));
Michal Vaskoa3881362020-01-21 15:57:35 +0100248 }
249
250 /* check unique */
251 if (snode->nodetype == LYS_LIST) {
252 slist = (struct lysc_node_list *)snode;
253 if (slist->uniques) {
254 LY_CHECK_RET(lyd_validate_unique(snode, slist->uniques, sibling));
255 }
256 }
257
258 /* check case duplicites */
259 if (snode->nodetype == LYS_CHOICE) {
260 LY_CHECK_RET(lyd_validate_cases(((struct lysc_node_choice *)snode)->cases, sibling));
261 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100262
263 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
264 /* go recursively for schema-only nodes */
265 LY_CHECK_RET(lyd_validate_siblings_schema_r(sibling, snode, mod, options));
266 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100267 }
268
Michal Vaskoacd83e72020-02-04 14:12:01 +0100269 return LY_SUCCESS;
270}
271
272static LY_ERR
273lyd_validate_siblings_r(struct lyd_node *sibling, const struct lysc_node *sparent, const struct lysc_module *mod, int options)
274{
275 struct lyd_node *node;
276
277 /* validate schema-based restrictions */
278 LY_CHECK_RET(lyd_validate_siblings_schema_r(sibling, sparent, mod, options));
279
280 LY_LIST_FOR(sibling, node) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100281 /* TODO node's must */
282 /* TODO node instance duplicites */
283 /* TODO node status */
Michal Vaskoa3881362020-01-21 15:57:35 +0100284 /* TODO node's if-features */
285 /* TODO node list keys */
286 /* node value including if-feature is checked by plugins */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100287
288 /* validate all children */
Michal Vaskoa3881362020-01-21 15:57:35 +0100289 LY_CHECK_RET(lyd_validate_siblings_r((struct lyd_node *)lyd_node_children(sibling), node->schema, mod, options));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100290 }
291
292 return LY_SUCCESS;
293}
294
295LY_ERR
Michal Vaskoacd83e72020-02-04 14:12:01 +0100296lyd_validate_data(const struct lyd_node **trees, const struct lys_module **modules, int mod_count, struct ly_ctx *ctx,
297 int options)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100298{
299 LY_ERR ret;
300 uint32_t i = 0, j;
301 const struct lys_module *mod;
302 struct lyd_node *tree;
303
Michal Vaskoa3881362020-01-21 15:57:35 +0100304 if (options & LYD_OPT_VAL_DATA_ONLY) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100305 if (trees) {
306 for (j = 0; j < LY_ARRAY_SIZE(trees); ++j) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100307 tree = (struct lyd_node *)trees[j];
308
309 /* validate all top-level nodes and then inner nodes recursively */
310 LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, tree->schema->module->compiled, options));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100311 }
312 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100313 } else {
314 while ((mod = lyd_val_next_module(modules, mod_count, ctx, &i))) {
315 if (!mod->implemented) {
316 continue;
317 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100318
Michal Vaskoa3881362020-01-21 15:57:35 +0100319 /* find data of this module, if any */
320 tree = NULL;
321 if (trees) {
322 for (j = 0; j < LY_ARRAY_SIZE(trees); ++j) {
323 if (trees[j]->schema->module == mod) {
324 tree = (struct lyd_node *)trees[j];
325 break;
326 }
327 }
328 }
329
330 /* validate all top-level nodes and then inner nodes recursively */
331 LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, mod->compiled, options));
332 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100333 }
334
335 return LY_SUCCESS;
336}