blob: 599a5bc2121e6bec24ed3a2a0937cd5a7de2f470 [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
32struct lyd_node **
33lyd_node_children_p(struct lyd_node *node)
34{
35 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +010036
37 if (!node->schema) {
38 return &((struct lyd_node_opaq *)node)->child;
39 } else {
40 switch (node->schema->nodetype) {
41 case LYS_CONTAINER:
42 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +010043 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +010044 case LYS_ACTION:
45 case LYS_NOTIF:
46 return &((struct lyd_node_inner *)node)->child;
47 default:
48 return NULL;
49 }
Radek Krejcie7b95092019-05-15 11:03:07 +020050 }
51}
52
Radek Krejcidae0ee82020-05-06 16:53:24 +020053API struct lyd_node *
Michal Vasko5bfd4be2020-06-23 13:26:19 +020054lyd_node_children(const struct lyd_node *node, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020055{
Michal Vasko5bfd4be2020-06-23 13:26:19 +020056 struct lyd_node **children, *child;
Radek Krejcie7b95092019-05-15 11:03:07 +020057
58 if (!node) {
59 return NULL;
60 }
61
Michal Vasko5bfd4be2020-06-23 13:26:19 +020062 children = lyd_node_children_p((struct lyd_node *)node);
Radek Krejcie7b95092019-05-15 11:03:07 +020063 if (children) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +020064 child = *children;
65 if (options & LYD_CHILDREN_SKIP_KEYS) {
Michal Vasko04091642020-06-29 11:50:14 +020066 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +020067 child = child->next;
68 }
69 }
70 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +020071 } else {
72 return NULL;
73 }
74}
Michal Vasko9b368d32020-02-14 13:53:31 +010075
Michal Vaskoc193ce92020-03-06 11:04:48 +010076API const struct lys_module *
77lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +010078{
79 const struct lysc_node *schema;
80
Michal Vasko52927e22020-03-16 17:26:14 +010081 if (!node || !node->schema) {
Michal Vaskoc193ce92020-03-06 11:04:48 +010082 return NULL;
83 }
Michal Vasko9b368d32020-02-14 13:53:31 +010084
85 for (schema = node->schema; schema->parent; schema = schema->parent);
86 return schema->module;
87}
Michal Vaskob1b5c262020-03-05 14:29:47 +010088
89const struct lys_module *
90lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
91 uint32_t *i, struct lyd_node **first)
92{
93 struct lyd_node *iter;
94 const struct lys_module *mod;
95
96 /* get the next module */
97 if (modules && mod_count) {
98 if (*i < (unsigned)mod_count) {
99 mod = modules[(*i)++];
100 } else {
101 mod = NULL;
102 }
103 } else {
104 do {
105 mod = ly_ctx_get_module_iter(ctx, i);
106 } while (mod && !mod->implemented);
107 }
108
109 /* find its data */
110 *first = NULL;
111 if (mod) {
112 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100113 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100114 *first = iter;
115 break;
116 }
117 }
118 }
119
120 return mod;
121}
122
123const struct lys_module *
124lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
125{
126 const struct lys_module *mod;
127
128 if (!*next) {
129 /* all data traversed */
130 *first = NULL;
131 return NULL;
132 }
133
134 *first = *next;
135
136 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100137 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100138 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100139 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100140 break;
141 }
142 }
143
144 return mod;
145}
Michal Vasko9f96a052020-03-10 09:41:45 +0100146
147LY_ERR
148lyd_parse_check_keys(struct lyd_node *node)
149{
150 const struct lysc_node *skey = NULL;
151 const struct lyd_node *key;
152
153 assert(node->schema->nodetype == LYS_LIST);
154
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200155 key = lyd_node_children(node, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +0100156 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
157 if (!key || (key->schema != skey)) {
158 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name);
159 return LY_EVALID;
160 }
161
162 key = key->next;
163 }
164
165 return LY_SUCCESS;
166}
Michal Vasko60ea6352020-06-29 13:39:39 +0200167
168void
169lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, int options)
170{
171 struct lyd_meta *meta2, *prev_meta = NULL;
172
173 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
Radek Krejci7931b192020-06-25 17:05:03 +0200174 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200175 /* just set it to true */
176 node->flags |= LYD_WHEN_TRUE;
177 } else {
178 /* remember we need to evaluate this node's when */
179 ly_set_add(when_check, node, LY_SET_OPT_USEASLIST);
180 }
181 }
182
Radek Krejci7931b192020-06-25 17:05:03 +0200183 if (options & LYD_PARSE_TRUSTED) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200184 /* node is valid */
185 node->flags &= ~LYD_NEW;
186 }
187
188 LY_LIST_FOR(*meta, meta2) {
189 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults")
190 && meta2->value.boolean) {
191 /* node is default according to the metadata */
192 node->flags |= LYD_DEFAULT;
193
194 /* delete the metadata */
195 if (prev_meta) {
196 prev_meta->next = meta2->next;
197 } else {
198 *meta = (*meta)->next;
199 }
200 lyd_free_meta(LYD_NODE_CTX(node), meta2, 0);
201 break;
202 }
203
204 prev_meta = meta2;
205 }
206}
207
208LYB_HASH
209lyb_hash(struct lysc_node *sibling, uint8_t collision_id)
210{
211 const struct lys_module *mod;
212 int ext_len;
213 uint32_t full_hash;
214 LYB_HASH hash;
215
216 if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) {
217 return sibling->hash[collision_id];
218 }
219
220 mod = sibling->module;
221
222 full_hash = dict_hash_multi(0, mod->name, strlen(mod->name));
223 full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name));
224 if (collision_id) {
225 if (collision_id > strlen(mod->name)) {
226 /* fine, we will not hash more bytes, just use more bits from the hash than previously */
227 ext_len = strlen(mod->name);
228 } else {
229 /* use one more byte from the module name than before */
230 ext_len = collision_id;
231 }
232 full_hash = dict_hash_multi(full_hash, mod->name, ext_len);
233 }
234 full_hash = dict_hash_multi(full_hash, NULL, 0);
235
236 /* use the shortened hash */
237 hash = full_hash & (LYB_HASH_MASK >> collision_id);
238 /* add colision identificator */
239 hash |= LYB_HASH_COLLISION_ID >> collision_id;
240
241 /* save this hash */
242 if (collision_id < LYS_NODE_HASH_COUNT) {
243 sibling->hash[collision_id] = hash;
244 }
245
246 return hash;
247}
248
249int
250lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models)
251{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200252 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200253
254 LY_ARRAY_FOR(models, u) {
255 if (sibling->module == models[u]) {
256 return 1;
257 }
258 }
259
260 return 0;
261}