blob: 0ed4c6229ffd348d65ce21f9bc84442111b5765e [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
172 for (node = sibling; node; node = node->next) {
173 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{
194 /* TODO snode count in siblings */
195 return LY_SUCCESS;
196}
197
198static LY_ERR
199lyd_validate_unique(const struct lysc_node *snode, struct lysc_node_leaf ***uniques, struct lyd_node *sibling)
200{
201 /* TODO check uniques in siblings and children */
202 return LY_SUCCESS;
203}
204
205static LY_ERR
206lyd_validate_cases(const struct lysc_node_case *cases, struct lyd_node *sibling)
207{
208 /* TODO check there are nodes only from a single case,
209 * what if not? validation error or autodelete */
210 return LY_SUCCESS;
211}
212
213static LY_ERR
214lyd_validate_siblings_r(struct lyd_node *sibling, const struct lysc_node *sparent, const struct lysc_module *mod, int options)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100215{
216 LY_ERR ret;
217 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100218 struct lysc_node_list *slist;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100219 struct lyd_node *node;
220
Michal Vaskoa3881362020-01-21 15:57:35 +0100221 /* disabled nodes are skipped by lys_getnext */
222 while ((snode = lys_getnext(snode, sparent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
223 /* check mandatory existence */
224 if (snode->flags & LYS_MAND_TRUE) {
225 LY_CHECK_RET(lyd_validate_mandatory(snode, sibling));
226 }
227
228 /* check min-elements and max-elements */
229 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
230 slist = (struct lysc_node_list *)snode;
231 if (slist->min || slist->max) {
232 LY_CHECK_RET(lyd_validate_minmax(snode, slist->min, slist->max, sibling));
233 }
234 }
235
236 /* check unique */
237 if (snode->nodetype == LYS_LIST) {
238 slist = (struct lysc_node_list *)snode;
239 if (slist->uniques) {
240 LY_CHECK_RET(lyd_validate_unique(snode, slist->uniques, sibling));
241 }
242 }
243
244 /* check case duplicites */
245 if (snode->nodetype == LYS_CHOICE) {
246 LY_CHECK_RET(lyd_validate_cases(((struct lysc_node_choice *)snode)->cases, sibling));
247 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100248 }
249
250 for (node = sibling; node; node = node->next) {
251 /* TODO node's must */
252 /* TODO node instance duplicites */
253 /* TODO node status */
Michal Vaskoa3881362020-01-21 15:57:35 +0100254 /* TODO node's if-features */
255 /* TODO node list keys */
256 /* node value including if-feature is checked by plugins */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100257
258 /* validate all children */
Michal Vaskoa3881362020-01-21 15:57:35 +0100259 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 +0100260 }
261
262 return LY_SUCCESS;
263}
264
265LY_ERR
266lyd_validate_modules(const struct lyd_node **trees, const struct lys_module **modules, int mod_count, struct ly_ctx *ctx,
267 int options)
268{
269 LY_ERR ret;
270 uint32_t i = 0, j;
271 const struct lys_module *mod;
272 struct lyd_node *tree;
273
Michal Vaskoa3881362020-01-21 15:57:35 +0100274 if (options & LYD_OPT_VAL_DATA_ONLY) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100275 if (trees) {
276 for (j = 0; j < LY_ARRAY_SIZE(trees); ++j) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100277 tree = (struct lyd_node *)trees[j];
278
279 /* validate all top-level nodes and then inner nodes recursively */
280 LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, tree->schema->module->compiled, options));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100281 }
282 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100283 } else {
284 while ((mod = lyd_val_next_module(modules, mod_count, ctx, &i))) {
285 if (!mod->implemented) {
286 continue;
287 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100288
Michal Vaskoa3881362020-01-21 15:57:35 +0100289 /* find data of this module, if any */
290 tree = NULL;
291 if (trees) {
292 for (j = 0; j < LY_ARRAY_SIZE(trees); ++j) {
293 if (trees[j]->schema->module == mod) {
294 tree = (struct lyd_node *)trees[j];
295 break;
296 }
297 }
298 }
299
300 /* validate all top-level nodes and then inner nodes recursively */
301 LY_CHECK_RET(lyd_validate_siblings_r(tree, NULL, mod->compiled, options));
302 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100303 }
304
305 return LY_SUCCESS;
306}