blob: 6979586f9e51f7c87a8c699b85a94dfe1c90daf5 [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) {
Michal Vasko0f3377d2020-11-09 20:56:11 +0100243 if (!(options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200244 /* remember we need to evaluate this node's when */
Radek Krejci3d92e442020-10-12 12:48:13 +0200245 LY_CHECK_RET(ly_set_add(when_check, node, 1, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200246 }
247 }
248
Michal Vasko60ea6352020-06-29 13:39:39 +0200249 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200250 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
251 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200252 /* node is default according to the metadata */
253 node->flags |= LYD_DEFAULT;
254
255 /* delete the metadata */
256 if (prev_meta) {
257 prev_meta->next = meta2->next;
258 } else {
259 *meta = (*meta)->next;
260 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200261 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200262 break;
263 }
264
265 prev_meta = meta2;
266 }
267}
268
Michal Vaskoc0004272020-08-06 08:32:34 +0200269API LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200270lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
271{
272 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200273
274 assert(trg->schema->nodetype & LYS_ANYDATA);
275
276 t = (struct lyd_node_any *)trg;
277
278 /* free trg */
279 switch (t->value_type) {
280 case LYD_ANYDATA_DATATREE:
281 lyd_free_all(t->value.tree);
282 break;
283 case LYD_ANYDATA_STRING:
284 case LYD_ANYDATA_XML:
285 case LYD_ANYDATA_JSON:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200286 FREE_STRING(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200287 break;
288 case LYD_ANYDATA_LYB:
289 free(t->value.mem);
290 break;
291 }
292 t->value.str = NULL;
293
294 if (!value) {
295 /* only free value in this case */
296 return LY_SUCCESS;
297 }
298
299 /* copy src */
300 t->value_type = value_type;
301 switch (value_type) {
302 case LYD_ANYDATA_DATATREE:
303 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200304 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200305 }
306 break;
307 case LYD_ANYDATA_STRING:
308 case LYD_ANYDATA_XML:
309 case LYD_ANYDATA_JSON:
310 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200311 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200312 }
313 break;
314 case LYD_ANYDATA_LYB:
315 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200316 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200317 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200318 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200319 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200320 memcpy(t->value.mem, value->mem, len);
321 }
322 break;
323 }
324
325 return LY_SUCCESS;
326}
327
Michal Vasko60ea6352020-06-29 13:39:39 +0200328LYB_HASH
329lyb_hash(struct lysc_node *sibling, uint8_t collision_id)
330{
331 const struct lys_module *mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200332 uint32_t full_hash;
333 LYB_HASH hash;
334
335 if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) {
336 return sibling->hash[collision_id];
337 }
338
339 mod = sibling->module;
340
341 full_hash = dict_hash_multi(0, mod->name, strlen(mod->name));
342 full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name));
343 if (collision_id) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200344 size_t ext_len;
345
Michal Vasko60ea6352020-06-29 13:39:39 +0200346 if (collision_id > strlen(mod->name)) {
347 /* fine, we will not hash more bytes, just use more bits from the hash than previously */
348 ext_len = strlen(mod->name);
349 } else {
350 /* use one more byte from the module name than before */
351 ext_len = collision_id;
352 }
353 full_hash = dict_hash_multi(full_hash, mod->name, ext_len);
354 }
355 full_hash = dict_hash_multi(full_hash, NULL, 0);
356
357 /* use the shortened hash */
358 hash = full_hash & (LYB_HASH_MASK >> collision_id);
359 /* add colision identificator */
360 hash |= LYB_HASH_COLLISION_ID >> collision_id;
361
362 /* save this hash */
363 if (collision_id < LYS_NODE_HASH_COUNT) {
364 sibling->hash[collision_id] = hash;
365 }
366
367 return hash;
368}
369
Radek Krejci857189e2020-09-01 13:26:36 +0200370ly_bool
Michal Vasko60ea6352020-06-29 13:39:39 +0200371lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models)
372{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200373 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200374
375 LY_ARRAY_FOR(models, u) {
376 if (sibling->module == models[u]) {
377 return 1;
378 }
379 }
380
381 return 0;
382}