blob: 5a0d98f82fe3e8240c2d71b4bf44863f9e58dbd9 [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 Krejci47fab892020-11-05 17:02:41 +010022#include "dict.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020023#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "log.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020026#include "parser_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020027#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020030#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree_schema.h"
32
Michal Vaskoa6669ba2020-08-06 16:14:26 +020033struct lyd_node *
34lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +020035 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +020036{
37 const struct lysc_node *siter = NULL;
38 struct lyd_node *match = NULL;
39
40 assert(parent || module);
41 assert(!last || (slast && *slast));
42
43 if (slast) {
44 siter = *slast;
45 }
46
47 if (last && last->next && (last->next->schema == siter)) {
48 /* return next data instance */
49 return last->next;
50 }
51
52 /* find next schema node data instance */
53 while ((siter = lys_getnext(siter, parent, module, 0))) {
54 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
55 break;
56 }
57 }
58
59 if (slast) {
60 *slast = siter;
61 }
62 return match;
63}
64
Radek Krejcie7b95092019-05-15 11:03:07 +020065struct lyd_node **
66lyd_node_children_p(struct lyd_node *node)
67{
68 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +010069
70 if (!node->schema) {
71 return &((struct lyd_node_opaq *)node)->child;
72 } else {
73 switch (node->schema->nodetype) {
74 case LYS_CONTAINER:
75 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +010076 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +010077 case LYS_ACTION:
78 case LYS_NOTIF:
79 return &((struct lyd_node_inner *)node)->child;
80 default:
81 return NULL;
82 }
Radek Krejcie7b95092019-05-15 11:03:07 +020083 }
84}
85
Radek Krejcidae0ee82020-05-06 16:53:24 +020086API struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +020087lyd_parent(const struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +020088{
Radek Krejcia1c1e542020-09-29 16:06:52 +020089 if (!node) {
90 return NULL;
91 }
92
93 return (struct lyd_node *)(node)->parent;
94}
95
96API struct lyd_node *
97lyd_child(const struct lyd_node *node)
98{
99 struct lyd_node **children;
Radek Krejcie7b95092019-05-15 11:03:07 +0200100
101 if (!node) {
102 return NULL;
103 }
104
Radek Krejcia1c1e542020-09-29 16:06:52 +0200105 if (!node->schema) {
106 /* opaq node */
107 return ((struct lyd_node_opaq *)(node))->child;
108 }
109
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200110 children = lyd_node_children_p((struct lyd_node *)node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200111 if (children) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200112 return *children;
113 } else {
114 return NULL;
115 }
116}
117
118API struct lyd_node *
119lyd_child_no_keys(const struct lyd_node *node)
120{
121 struct lyd_node **children;
122
123 if (!node) {
124 return NULL;
125 }
126
127 if (!node->schema) {
128 /* opaq node */
129 return ((struct lyd_node_opaq *)(node))->child;
130 }
131
132 children = lyd_node_children_p((struct lyd_node *)node);
133 if (children) {
134 struct lyd_node *child = *children;
135 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
136 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200137 }
138 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200139 } else {
140 return NULL;
141 }
142}
Michal Vasko9b368d32020-02-14 13:53:31 +0100143
Michal Vaskoc193ce92020-03-06 11:04:48 +0100144API const struct lys_module *
145lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100146{
147 const struct lysc_node *schema;
148
Michal Vasko52927e22020-03-16 17:26:14 +0100149 if (!node || !node->schema) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100150 return NULL;
151 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100152
Radek Krejci1e008d22020-08-17 11:37:37 +0200153 for (schema = node->schema; schema->parent; schema = schema->parent) {}
Michal Vasko9b368d32020-02-14 13:53:31 +0100154 return schema->module;
155}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100156
157const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200158lyd_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 +0200159 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100160{
161 struct lyd_node *iter;
162 const struct lys_module *mod;
163
164 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200165 if (module) {
166 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100167 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200168 } else {
169 mod = module;
170 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100171 }
172 } else {
173 do {
174 mod = ly_ctx_get_module_iter(ctx, i);
175 } while (mod && !mod->implemented);
176 }
177
178 /* find its data */
179 *first = NULL;
180 if (mod) {
181 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100182 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100183 *first = iter;
184 break;
185 }
186 }
187 }
188
189 return mod;
190}
191
192const struct lys_module *
193lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
194{
195 const struct lys_module *mod;
196
197 if (!*next) {
198 /* all data traversed */
199 *first = NULL;
200 return NULL;
201 }
202
203 *first = *next;
204
205 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100206 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100207 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100208 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100209 break;
210 }
211 }
212
213 return mod;
214}
Michal Vasko9f96a052020-03-10 09:41:45 +0100215
216LY_ERR
217lyd_parse_check_keys(struct lyd_node *node)
218{
219 const struct lysc_node *skey = NULL;
220 const struct lyd_node *key;
221
222 assert(node->schema->nodetype == LYS_LIST);
223
Radek Krejcia1c1e542020-09-29 16:06:52 +0200224 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100225 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
226 if (!key || (key->schema != skey)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200227 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100228 return LY_EVALID;
229 }
230
231 key = key->next;
232 }
233
234 return LY_SUCCESS;
235}
Michal Vasko60ea6352020-06-29 13:39:39 +0200236
237void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200238lyd_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 +0200239{
240 struct lyd_meta *meta2, *prev_meta = NULL;
241
242 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
Radek Krejci7931b192020-06-25 17:05:03 +0200243 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200244 /* just set it to true */
245 node->flags |= LYD_WHEN_TRUE;
246 } else {
247 /* remember we need to evaluate this node's when */
Radek Krejci3d92e442020-10-12 12:48:13 +0200248 LY_CHECK_RET(ly_set_add(when_check, node, 1, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200249 }
250 }
251
Radek Krejci7931b192020-06-25 17:05:03 +0200252 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200253 /* node is valid */
254 node->flags &= ~LYD_NEW;
255 }
256
257 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200258 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
259 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200260 /* node is default according to the metadata */
261 node->flags |= LYD_DEFAULT;
262
263 /* delete the metadata */
264 if (prev_meta) {
265 prev_meta->next = meta2->next;
266 } else {
267 *meta = (*meta)->next;
268 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200269 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200270 break;
271 }
272
273 prev_meta = meta2;
274 }
275}
276
Michal Vaskoc0004272020-08-06 08:32:34 +0200277API LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200278lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
279{
280 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200281
282 assert(trg->schema->nodetype & LYS_ANYDATA);
283
284 t = (struct lyd_node_any *)trg;
285
286 /* free trg */
287 switch (t->value_type) {
288 case LYD_ANYDATA_DATATREE:
289 lyd_free_all(t->value.tree);
290 break;
291 case LYD_ANYDATA_STRING:
292 case LYD_ANYDATA_XML:
293 case LYD_ANYDATA_JSON:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200294 FREE_STRING(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200295 break;
296 case LYD_ANYDATA_LYB:
297 free(t->value.mem);
298 break;
299 }
300 t->value.str = NULL;
301
302 if (!value) {
303 /* only free value in this case */
304 return LY_SUCCESS;
305 }
306
307 /* copy src */
308 t->value_type = value_type;
309 switch (value_type) {
310 case LYD_ANYDATA_DATATREE:
311 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200312 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200313 }
314 break;
315 case LYD_ANYDATA_STRING:
316 case LYD_ANYDATA_XML:
317 case LYD_ANYDATA_JSON:
318 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200319 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200320 }
321 break;
322 case LYD_ANYDATA_LYB:
323 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200324 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200325 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200326 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200327 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200328 memcpy(t->value.mem, value->mem, len);
329 }
330 break;
331 }
332
333 return LY_SUCCESS;
334}
335
Michal Vasko60ea6352020-06-29 13:39:39 +0200336LYB_HASH
337lyb_hash(struct lysc_node *sibling, uint8_t collision_id)
338{
339 const struct lys_module *mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 uint32_t full_hash;
341 LYB_HASH hash;
342
343 if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) {
344 return sibling->hash[collision_id];
345 }
346
347 mod = sibling->module;
348
349 full_hash = dict_hash_multi(0, mod->name, strlen(mod->name));
350 full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name));
351 if (collision_id) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200352 size_t ext_len;
353
Michal Vasko60ea6352020-06-29 13:39:39 +0200354 if (collision_id > strlen(mod->name)) {
355 /* fine, we will not hash more bytes, just use more bits from the hash than previously */
356 ext_len = strlen(mod->name);
357 } else {
358 /* use one more byte from the module name than before */
359 ext_len = collision_id;
360 }
361 full_hash = dict_hash_multi(full_hash, mod->name, ext_len);
362 }
363 full_hash = dict_hash_multi(full_hash, NULL, 0);
364
365 /* use the shortened hash */
366 hash = full_hash & (LYB_HASH_MASK >> collision_id);
367 /* add colision identificator */
368 hash |= LYB_HASH_COLLISION_ID >> collision_id;
369
370 /* save this hash */
371 if (collision_id < LYS_NODE_HASH_COUNT) {
372 sibling->hash[collision_id] = hash;
373 }
374
375 return hash;
376}
377
Radek Krejci857189e2020-09-01 13:26:36 +0200378ly_bool
Michal Vasko60ea6352020-06-29 13:39:39 +0200379lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models)
380{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200381 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200382
383 LY_ARRAY_FOR(models, u) {
384 if (sibling->module == models[u]) {
385 return 1;
386 }
387 }
388
389 return 0;
390}