Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 14 | |
| 15 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 18 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "common.h" |
| 21 | #include "context.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 22 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "log.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 24 | #include "lyb.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 25 | #include "parser_data.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 26 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 29 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "tree_schema.h" |
| 31 | |
| 32 | struct lyd_node ** |
| 33 | lyd_node_children_p(struct lyd_node *node) |
| 34 | { |
| 35 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 36 | |
| 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 Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 43 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 44 | case LYS_ACTION: |
| 45 | case LYS_NOTIF: |
| 46 | return &((struct lyd_node_inner *)node)->child; |
| 47 | default: |
| 48 | return NULL; |
| 49 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 53 | API struct lyd_node * |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 54 | lyd_node_children(const struct lyd_node *node, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 55 | { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 56 | struct lyd_node **children, *child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 57 | |
| 58 | if (!node) { |
| 59 | return NULL; |
| 60 | } |
| 61 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 62 | children = lyd_node_children_p((struct lyd_node *)node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 63 | if (children) { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 64 | child = *children; |
| 65 | if (options & LYD_CHILDREN_SKIP_KEYS) { |
Michal Vasko | 0409164 | 2020-06-29 11:50:14 +0200 | [diff] [blame] | 66 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 67 | child = child->next; |
| 68 | } |
| 69 | } |
| 70 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 71 | } else { |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 75 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 76 | API const struct lys_module * |
| 77 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 78 | { |
| 79 | const struct lysc_node *schema; |
| 80 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 81 | if (!node || !node->schema) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 84 | |
| 85 | for (schema = node->schema; schema->parent; schema = schema->parent); |
| 86 | return schema->module; |
| 87 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 88 | |
| 89 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 90 | lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i, |
| 91 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 92 | { |
| 93 | struct lyd_node *iter; |
| 94 | const struct lys_module *mod; |
| 95 | |
| 96 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 97 | if (module) { |
| 98 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 99 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 100 | } else { |
| 101 | mod = module; |
| 102 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 103 | } |
| 104 | } else { |
| 105 | do { |
| 106 | mod = ly_ctx_get_module_iter(ctx, i); |
| 107 | } while (mod && !mod->implemented); |
| 108 | } |
| 109 | |
| 110 | /* find its data */ |
| 111 | *first = NULL; |
| 112 | if (mod) { |
| 113 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 114 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 115 | *first = iter; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return mod; |
| 122 | } |
| 123 | |
| 124 | const struct lys_module * |
| 125 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 126 | { |
| 127 | const struct lys_module *mod; |
| 128 | |
| 129 | if (!*next) { |
| 130 | /* all data traversed */ |
| 131 | *first = NULL; |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | *first = *next; |
| 136 | |
| 137 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 138 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 139 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 140 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return mod; |
| 146 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 147 | |
| 148 | LY_ERR |
| 149 | lyd_parse_check_keys(struct lyd_node *node) |
| 150 | { |
| 151 | const struct lysc_node *skey = NULL; |
| 152 | const struct lyd_node *key; |
| 153 | |
| 154 | assert(node->schema->nodetype == LYS_LIST); |
| 155 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 156 | key = lyd_node_children(node, 0); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 157 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 158 | if (!key || (key->schema != skey)) { |
| 159 | LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name); |
| 160 | return LY_EVALID; |
| 161 | } |
| 162 | |
| 163 | key = key->next; |
| 164 | } |
| 165 | |
| 166 | return LY_SUCCESS; |
| 167 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 168 | |
| 169 | void |
| 170 | lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, int options) |
| 171 | { |
| 172 | struct lyd_meta *meta2, *prev_meta = NULL; |
| 173 | |
| 174 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 175 | if (options & LYD_PARSE_TRUSTED) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 176 | /* just set it to true */ |
| 177 | node->flags |= LYD_WHEN_TRUE; |
| 178 | } else { |
| 179 | /* remember we need to evaluate this node's when */ |
| 180 | ly_set_add(when_check, node, LY_SET_OPT_USEASLIST); |
| 181 | } |
| 182 | } |
| 183 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 184 | if (options & LYD_PARSE_TRUSTED) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 185 | /* node is valid */ |
| 186 | node->flags &= ~LYD_NEW; |
| 187 | } |
| 188 | |
| 189 | LY_LIST_FOR(*meta, meta2) { |
| 190 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") |
| 191 | && meta2->value.boolean) { |
| 192 | /* node is default according to the metadata */ |
| 193 | node->flags |= LYD_DEFAULT; |
| 194 | |
| 195 | /* delete the metadata */ |
| 196 | if (prev_meta) { |
| 197 | prev_meta->next = meta2->next; |
| 198 | } else { |
| 199 | *meta = (*meta)->next; |
| 200 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 201 | lyd_free_meta_single(meta2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
| 205 | prev_meta = meta2; |
| 206 | } |
| 207 | } |
| 208 | |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 209 | LY_ERR |
| 210 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 211 | { |
| 212 | struct lyd_node_any *t; |
| 213 | int len; |
| 214 | |
| 215 | assert(trg->schema->nodetype & LYS_ANYDATA); |
| 216 | |
| 217 | t = (struct lyd_node_any *)trg; |
| 218 | |
| 219 | /* free trg */ |
| 220 | switch (t->value_type) { |
| 221 | case LYD_ANYDATA_DATATREE: |
| 222 | lyd_free_all(t->value.tree); |
| 223 | break; |
| 224 | case LYD_ANYDATA_STRING: |
| 225 | case LYD_ANYDATA_XML: |
| 226 | case LYD_ANYDATA_JSON: |
| 227 | FREE_STRING(LYD_NODE_CTX(trg), t->value.str); |
| 228 | break; |
| 229 | case LYD_ANYDATA_LYB: |
| 230 | free(t->value.mem); |
| 231 | break; |
| 232 | } |
| 233 | t->value.str = NULL; |
| 234 | |
| 235 | if (!value) { |
| 236 | /* only free value in this case */ |
| 237 | return LY_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | /* copy src */ |
| 241 | t->value_type = value_type; |
| 242 | switch (value_type) { |
| 243 | case LYD_ANYDATA_DATATREE: |
| 244 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 245 | LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree)); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 246 | } |
| 247 | break; |
| 248 | case LYD_ANYDATA_STRING: |
| 249 | case LYD_ANYDATA_XML: |
| 250 | case LYD_ANYDATA_JSON: |
| 251 | if (value->str) { |
| 252 | t->value.str = lydict_insert(LYD_NODE_CTX(trg), value->str, 0); |
| 253 | } |
| 254 | break; |
| 255 | case LYD_ANYDATA_LYB: |
| 256 | if (value->mem) { |
| 257 | len = lyd_lyb_data_length(value->mem); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 258 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 259 | t->value.mem = malloc(len); |
| 260 | LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_NODE_CTX(trg)), LY_EMEM); |
| 261 | memcpy(t->value.mem, value->mem, len); |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | return LY_SUCCESS; |
| 267 | } |
| 268 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 269 | LYB_HASH |
| 270 | lyb_hash(struct lysc_node *sibling, uint8_t collision_id) |
| 271 | { |
| 272 | const struct lys_module *mod; |
| 273 | int ext_len; |
| 274 | uint32_t full_hash; |
| 275 | LYB_HASH hash; |
| 276 | |
| 277 | if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) { |
| 278 | return sibling->hash[collision_id]; |
| 279 | } |
| 280 | |
| 281 | mod = sibling->module; |
| 282 | |
| 283 | full_hash = dict_hash_multi(0, mod->name, strlen(mod->name)); |
| 284 | full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name)); |
| 285 | if (collision_id) { |
| 286 | if (collision_id > strlen(mod->name)) { |
| 287 | /* fine, we will not hash more bytes, just use more bits from the hash than previously */ |
| 288 | ext_len = strlen(mod->name); |
| 289 | } else { |
| 290 | /* use one more byte from the module name than before */ |
| 291 | ext_len = collision_id; |
| 292 | } |
| 293 | full_hash = dict_hash_multi(full_hash, mod->name, ext_len); |
| 294 | } |
| 295 | full_hash = dict_hash_multi(full_hash, NULL, 0); |
| 296 | |
| 297 | /* use the shortened hash */ |
| 298 | hash = full_hash & (LYB_HASH_MASK >> collision_id); |
| 299 | /* add colision identificator */ |
| 300 | hash |= LYB_HASH_COLLISION_ID >> collision_id; |
| 301 | |
| 302 | /* save this hash */ |
| 303 | if (collision_id < LYS_NODE_HASH_COUNT) { |
| 304 | sibling->hash[collision_id] = hash; |
| 305 | } |
| 306 | |
| 307 | return hash; |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models) |
| 312 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 313 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 314 | |
| 315 | LY_ARRAY_FOR(models, u) { |
| 316 | if (sibling->module == models[u]) { |
| 317 | return 1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |