blob: 129af231519be0b402611b6a902e0e5ef67d9970 [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
163lyd_validate_children_r(struct lyd_node *sibling, const struct lysc_node *sparent, const struct lysc_module *mod, int options)
164{
165 LY_ERR ret;
166 const struct lysc_node *snode = NULL;
167 struct lyd_node *node;
168
169 while ((snode = lys_getnext(snode, sparent, mod, 0))) {
170 /* TODO mandatory - mandatory snode must exist */
171 /* TODO min/max elem - check snode element count */
172 /* TODO unique - check snode unique */
173 /* TODO choice - case duplicites/mandatory */
174 }
175
176 for (node = sibling; node; node = node->next) {
177 /* TODO node's must */
178 /* TODO node instance duplicites */
179 /* TODO node status */
180
181 /* validate all children */
182 LY_CHECK_RET(lyd_validate_children_r((struct lyd_node *)lyd_node_children(sibling), node->schema, mod, options));
183 }
184
185 return LY_SUCCESS;
186}
187
188LY_ERR
189lyd_validate_modules(const struct lyd_node **trees, const struct lys_module **modules, int mod_count, struct ly_ctx *ctx,
190 int options)
191{
192 LY_ERR ret;
193 uint32_t i = 0, j;
194 const struct lys_module *mod;
195 struct lyd_node *tree;
196
197 while ((mod = lyd_val_next_module(modules, mod_count, ctx, &i))) {
198 if (!mod->implemented) {
199 continue;
200 }
201
202 /* find data of this module, if any */
203 tree = NULL;
204 if (trees) {
205 for (j = 0; j < LY_ARRAY_SIZE(trees); ++j) {
206 if (trees[j]->schema->module == mod) {
207 tree = (struct lyd_node *)trees[j];
208 break;
209 }
210 }
211 }
212
213 /* validate all top-level nodes and then inner nodes recursively */
214 LY_CHECK_RET(lyd_validate_children_r(tree, NULL, mod->compiled, options));
215 }
216
217 return LY_SUCCESS;
218}