blob: 712f300f813843989e94d18e1baaad3021d105d4 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions for data trees
5 *
6 * Copyright (c) 2015 - 2018 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 */
Radek Krejcie7b95092019-05-15 11:03:07 +020014
15#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
21#include "context.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020022#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "log.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020024#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020026#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020029#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree_schema.h"
31
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032struct lyd_node *
33lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +020034 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +020035{
36 const struct lysc_node *siter = NULL;
37 struct lyd_node *match = NULL;
38
39 assert(parent || module);
40 assert(!last || (slast && *slast));
41
42 if (slast) {
43 siter = *slast;
44 }
45
46 if (last && last->next && (last->next->schema == siter)) {
47 /* return next data instance */
48 return last->next;
49 }
50
51 /* find next schema node data instance */
52 while ((siter = lys_getnext(siter, parent, module, 0))) {
53 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
54 break;
55 }
56 }
57
58 if (slast) {
59 *slast = siter;
60 }
61 return match;
62}
63
Radek Krejcie7b95092019-05-15 11:03:07 +020064struct lyd_node **
65lyd_node_children_p(struct lyd_node *node)
66{
67 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +010068
69 if (!node->schema) {
70 return &((struct lyd_node_opaq *)node)->child;
71 } else {
72 switch (node->schema->nodetype) {
73 case LYS_CONTAINER:
74 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +010075 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +010076 case LYS_ACTION:
77 case LYS_NOTIF:
78 return &((struct lyd_node_inner *)node)->child;
79 default:
80 return NULL;
81 }
Radek Krejcie7b95092019-05-15 11:03:07 +020082 }
83}
84
Radek Krejcidae0ee82020-05-06 16:53:24 +020085API struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +020086lyd_parent(const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +020087{
Radek Krejcia1c1e542020-09-29 16:06:52 +020088 if (!node) {
89 return NULL;
90 }
91
92 return (struct lyd_node *)(node)->parent;
93}
94
95API struct lyd_node *
96lyd_child(const struct lyd_node *node)
97{
98 struct lyd_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +020099
100 if (!node) {
101 return NULL;
102 }
103
Radek Krejcia1c1e542020-09-29 16:06:52 +0200104 if (!node->schema) {
105 /* opaq node */
106 return ((struct lyd_node_opaq *)(node))->child;
107 }
108
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200109 children = lyd_node_children_p((struct lyd_node *)node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200110 if (children) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200111 return *children;
112 } else {
113 return NULL;
114 }
115}
116
117API struct lyd_node *
118lyd_child_no_keys(const struct lyd_node *node)
119{
120 struct lyd_node **children;
121
122 if (!node) {
123 return NULL;
124 }
125
126 if (!node->schema) {
127 /* opaq node */
128 return ((struct lyd_node_opaq *)(node))->child;
129 }
130
131 children = lyd_node_children_p((struct lyd_node *)node);
132 if (children) {
133 struct lyd_node *child = *children;
134 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
135 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200136 }
137 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200138 } else {
139 return NULL;
140 }
141}
Michal Vasko9b368d32020-02-14 13:53:31 +0100142
Michal Vaskoc193ce92020-03-06 11:04:48 +0100143API const struct lys_module *
144lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100145{
146 const struct lysc_node *schema;
147
Michal Vasko52927e22020-03-16 17:26:14 +0100148 if (!node || !node->schema) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100149 return NULL;
150 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100151
Radek Krejci1e008d22020-08-17 11:37:37 +0200152 for (schema = node->schema; schema->parent; schema = schema->parent) {}
Michal Vasko9b368d32020-02-14 13:53:31 +0100153 return schema->module;
154}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100155
156const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200157lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i,
Radek Krejci0f969882020-08-21 16:56:47 +0200158 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100159{
160 struct lyd_node *iter;
161 const struct lys_module *mod;
162
163 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200164 if (module) {
165 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100166 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200167 } else {
168 mod = module;
169 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100170 }
171 } else {
172 do {
173 mod = ly_ctx_get_module_iter(ctx, i);
174 } while (mod && !mod->implemented);
175 }
176
177 /* find its data */
178 *first = NULL;
179 if (mod) {
180 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100181 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100182 *first = iter;
183 break;
184 }
185 }
186 }
187
188 return mod;
189}
190
191const struct lys_module *
192lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
193{
194 const struct lys_module *mod;
195
196 if (!*next) {
197 /* all data traversed */
198 *first = NULL;
199 return NULL;
200 }
201
202 *first = *next;
203
204 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100205 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100206 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100207 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100208 break;
209 }
210 }
211
212 return mod;
213}
Michal Vasko9f96a052020-03-10 09:41:45 +0100214
215LY_ERR
216lyd_parse_check_keys(struct lyd_node *node)
217{
218 const struct lysc_node *skey = NULL;
219 const struct lyd_node *key;
220
221 assert(node->schema->nodetype == LYS_LIST);
222
Radek Krejcia1c1e542020-09-29 16:06:52 +0200223 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
225 if (!key || (key->schema != skey)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200226 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100227 return LY_EVALID;
228 }
229
230 key = key->next;
231 }
232
233 return LY_SUCCESS;
234}
Michal Vasko60ea6352020-06-29 13:39:39 +0200235
236void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200237lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200238{
239 struct lyd_meta *meta2, *prev_meta = NULL;
240
241 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
Radek Krejci7931b192020-06-25 17:05:03 +0200242 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200243 /* just set it to true */
244 node->flags |= LYD_WHEN_TRUE;
245 } else {
246 /* remember we need to evaluate this node's when */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200247 LY_CHECK_RET(ly_set_add(when_check, node, LY_SET_OPT_USEASLIST, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200248 }
249 }
250
Radek Krejci7931b192020-06-25 17:05:03 +0200251 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200252 /* node is valid */
253 node->flags &= ~LYD_NEW;
254 }
255
256 LY_LIST_FOR(*meta, meta2) {
257 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults")
258 && meta2->value.boolean) {
259 /* node is default according to the metadata */
260 node->flags |= LYD_DEFAULT;
261
262 /* delete the metadata */
263 if (prev_meta) {
264 prev_meta->next = meta2->next;
265 } else {
266 *meta = (*meta)->next;
267 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200268 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200269 break;
270 }
271
272 prev_meta = meta2;
273 }
274}
275
Michal Vaskoc0004272020-08-06 08:32:34 +0200276API LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200277lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
278{
279 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200280
281 assert(trg->schema->nodetype & LYS_ANYDATA);
282
283 t = (struct lyd_node_any *)trg;
284
285 /* free trg */
286 switch (t->value_type) {
287 case LYD_ANYDATA_DATATREE:
288 lyd_free_all(t->value.tree);
289 break;
290 case LYD_ANYDATA_STRING:
291 case LYD_ANYDATA_XML:
292 case LYD_ANYDATA_JSON:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200293 FREE_STRING(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200294 break;
295 case LYD_ANYDATA_LYB:
296 free(t->value.mem);
297 break;
298 }
299 t->value.str = NULL;
300
301 if (!value) {
302 /* only free value in this case */
303 return LY_SUCCESS;
304 }
305
306 /* copy src */
307 t->value_type = value_type;
308 switch (value_type) {
309 case LYD_ANYDATA_DATATREE:
310 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200311 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200312 }
313 break;
314 case LYD_ANYDATA_STRING:
315 case LYD_ANYDATA_XML:
316 case LYD_ANYDATA_JSON:
317 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200318 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200319 }
320 break;
321 case LYD_ANYDATA_LYB:
322 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200323 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200324 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200325 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200326 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200327 memcpy(t->value.mem, value->mem, len);
328 }
329 break;
330 }
331
332 return LY_SUCCESS;
333}
334
Michal Vasko60ea6352020-06-29 13:39:39 +0200335LYB_HASH
336lyb_hash(struct lysc_node *sibling, uint8_t collision_id)
337{
338 const struct lys_module *mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200339 uint32_t full_hash;
340 LYB_HASH hash;
341
342 if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) {
343 return sibling->hash[collision_id];
344 }
345
346 mod = sibling->module;
347
348 full_hash = dict_hash_multi(0, mod->name, strlen(mod->name));
349 full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name));
350 if (collision_id) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200351 size_t ext_len;
352
Michal Vasko60ea6352020-06-29 13:39:39 +0200353 if (collision_id > strlen(mod->name)) {
354 /* fine, we will not hash more bytes, just use more bits from the hash than previously */
355 ext_len = strlen(mod->name);
356 } else {
357 /* use one more byte from the module name than before */
358 ext_len = collision_id;
359 }
360 full_hash = dict_hash_multi(full_hash, mod->name, ext_len);
361 }
362 full_hash = dict_hash_multi(full_hash, NULL, 0);
363
364 /* use the shortened hash */
365 hash = full_hash & (LYB_HASH_MASK >> collision_id);
366 /* add colision identificator */
367 hash |= LYB_HASH_COLLISION_ID >> collision_id;
368
369 /* save this hash */
370 if (collision_id < LYS_NODE_HASH_COUNT) {
371 sibling->hash[collision_id] = hash;
372 }
373
374 return hash;
375}
376
Radek Krejci857189e2020-09-01 13:26:36 +0200377ly_bool
Michal Vasko60ea6352020-06-29 13:39:39 +0200378lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models)
379{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200380 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200381
382 LY_ARRAY_FOR(models, u) {
383 if (sibling->module == models[u]) {
384 return 1;
385 }
386 }
387
388 return 0;
389}