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