Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 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 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "tree.h" |
| 28 | #include "tree_data.h" |
| 29 | #include "tree_data_internal.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 30 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "tree_schema.h" |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 32 | #include "xml.h" |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 33 | #include "plugins_exts_metadata.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 34 | #include "plugins_exts_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 36 | struct ly_keys { |
| 37 | char *str; |
| 38 | struct { |
| 39 | const struct lysc_node_leaf *schema; |
| 40 | char *value; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 41 | struct lyd_value val; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 42 | } *keys; |
| 43 | size_t key_count; |
| 44 | }; |
| 45 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 46 | LY_ERR |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 47 | lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 48 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 49 | { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 50 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 51 | struct ly_err_item *err = NULL; |
| 52 | struct ly_ctx *ctx; |
| 53 | struct lysc_type *type; |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 54 | int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 55 | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 56 | assert(node); |
| 57 | |
| 58 | ctx = node->schema->module->ctx; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 59 | |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 60 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 61 | if (!second) { |
| 62 | node->value.realtype = type; |
| 63 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 64 | ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 65 | tree ? (void *)node : (void *)node->schema, tree, |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 66 | &node->value, NULL, &err); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 67 | if (ret && (ret != LY_EINCOMPLETE)) { |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 68 | if (err) { |
| 69 | ly_err_print(err); |
| 70 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 71 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 72 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 73 | goto error; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 74 | } else if (dynamic) { |
| 75 | *dynamic = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | error: |
| 79 | return ret; |
| 80 | } |
| 81 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 82 | /* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */ |
| 83 | static LY_ERR |
| 84 | lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, |
| 85 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format) |
| 86 | { |
| 87 | LY_ERR ret = LY_SUCCESS; |
| 88 | struct ly_err_item *err = NULL; |
| 89 | struct ly_ctx *ctx; |
| 90 | struct lysc_type *type; |
| 91 | int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0); |
| 92 | |
| 93 | assert(val && schema && (schema->nodetype & LYD_NODE_TERM)); |
| 94 | |
| 95 | ctx = schema->module->ctx; |
| 96 | type = ((struct lysc_node_leaf *)schema)->type; |
| 97 | val->realtype = type; |
| 98 | ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL, |
| 99 | val, NULL, &err); |
| 100 | if (ret == LY_EINCOMPLETE) { |
| 101 | /* this is fine, we do not need it resolved */ |
| 102 | ret = LY_SUCCESS; |
| 103 | } else if (ret && err) { |
| 104 | ly_err_print(err); |
| 105 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 106 | ly_err_free(err); |
| 107 | } |
| 108 | if (!ret && dynamic) { |
| 109 | *dynamic = 0; |
| 110 | } |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 115 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 116 | lyd_value_parse_meta(struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic, |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 117 | int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 118 | const struct lysc_node *ctx_snode, const struct lyd_node *tree) |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 119 | { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 120 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 121 | struct ly_err_item *err = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 122 | struct lyext_metadata *ant; |
| 123 | int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 124 | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 125 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 126 | assert(ctx && meta && ((tree && meta->parent) || ctx_snode)); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 127 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 128 | ant = meta->annotation->data; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 129 | |
| 130 | if (!second) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 131 | meta->value.realtype = ant->type; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 132 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 133 | ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format, |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 134 | tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 135 | if (ret && (ret != LY_EINCOMPLETE)) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 136 | if (err) { |
| 137 | ly_err_print(err); |
| 138 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 139 | ly_err_free(err); |
| 140 | } |
| 141 | goto error; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 142 | } else if (dynamic) { |
| 143 | *dynamic = 0; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | error: |
| 147 | return ret; |
| 148 | } |
| 149 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 150 | API LY_ERR |
| 151 | lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
| 152 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format) |
| 153 | { |
| 154 | LY_ERR rc = LY_SUCCESS; |
| 155 | struct ly_err_item *err = NULL; |
| 156 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 157 | |
| 158 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 159 | |
| 160 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 161 | LOGARG(ctx, node); |
| 162 | return LY_EINVAL; |
| 163 | } |
| 164 | |
| 165 | type = ((struct lysc_node_leaf*)node)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 166 | /* just validate, no storing of enything */ |
| 167 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA, |
| 168 | get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err); |
| 169 | if (rc == LY_EINCOMPLETE) { |
| 170 | /* actually success since we do not provide the context tree and call validation with |
| 171 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 172 | rc = LY_SUCCESS; |
| 173 | } else if (rc && err) { |
| 174 | if (ctx) { |
| 175 | /* log only in case the ctx was provided as input parameter */ |
| 176 | ly_err_print(err); |
| 177 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 178 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 179 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | API LY_ERR |
| 186 | lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 187 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 188 | { |
| 189 | LY_ERR rc; |
| 190 | struct ly_err_item *err = NULL; |
| 191 | struct lysc_type *type; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 192 | int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 193 | |
| 194 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 195 | |
| 196 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 197 | rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 198 | get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree, |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 199 | NULL, NULL, &err); |
| 200 | if (rc == LY_EINCOMPLETE) { |
| 201 | return rc; |
| 202 | } else if (rc) { |
| 203 | if (err) { |
| 204 | if (ctx) { |
| 205 | ly_err_print(err); |
| 206 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 207 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 208 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 209 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 210 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return LY_SUCCESS; |
| 214 | } |
| 215 | |
| 216 | API LY_ERR |
| 217 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 218 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 219 | { |
| 220 | LY_ERR ret = LY_SUCCESS, rc; |
| 221 | struct ly_err_item *err = NULL; |
| 222 | struct ly_ctx *ctx; |
| 223 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 224 | struct lyd_value data = {0}; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 225 | int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 226 | |
| 227 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 228 | |
| 229 | ctx = node->schema->module->ctx; |
| 230 | type = ((struct lysc_node_leaf*)node->schema)->type; |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 231 | rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 232 | tree, &data, NULL, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 233 | if (rc == LY_EINCOMPLETE) { |
| 234 | ret = rc; |
| 235 | /* continue with comparing, just remember what to return if storing is ok */ |
| 236 | } else if (rc) { |
| 237 | /* value to compare is invalid */ |
| 238 | ret = LY_EINVAL; |
| 239 | if (err) { |
| 240 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 241 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 242 | goto cleanup; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* compare data */ |
Radek Krejci | 5af0484 | 2019-07-12 11:32:07 +0200 | [diff] [blame] | 246 | if (type->plugin->compare(&node->value, &data)) { |
| 247 | /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */ |
| 248 | ret = LY_EVALID; |
| 249 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 250 | |
| 251 | cleanup: |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 252 | type->plugin->free(ctx, &data); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 257 | API const char * |
| 258 | lyd_value2str(const struct lyd_node_term *node, int *dynamic) |
| 259 | { |
| 260 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL); |
| 261 | |
| 262 | return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic); |
| 263 | } |
| 264 | |
| 265 | API const char * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 266 | lyd_meta2str(const struct lyd_meta *meta, int *dynamic) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 267 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 268 | LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 269 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 270 | return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 273 | API struct lyd_node * |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 274 | lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 275 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 276 | struct lyd_node *result = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 277 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 278 | const char *yang_data_name = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 279 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 280 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 281 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 282 | |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 283 | if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) { |
| 284 | LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed."); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 288 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 289 | if (options & LYD_OPT_RPCREPLY) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 290 | /* first item in trees is mandatory - the RPC/action request */ |
| 291 | LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL); |
| 292 | if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) { |
| 293 | LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.", |
| 294 | lyd_parse_options_type2str(options)); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 295 | return NULL; |
| 296 | } |
| 297 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 298 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 299 | if (options & LYD_OPT_DATA_TEMPLATE) { |
| 300 | yang_data_name = va_arg(ap, const char *); |
| 301 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 302 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 303 | |
| 304 | if (!format) { |
| 305 | /* TODO try to detect format from the content */ |
| 306 | } |
| 307 | |
| 308 | switch (format) { |
| 309 | case LYD_XML: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 310 | lyd_parse_xml_data(ctx, data, options, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 311 | break; |
| 312 | #if 0 |
| 313 | case LYD_JSON: |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 314 | lyd_parse_json(ctx, data, options, trees, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 315 | break; |
| 316 | case LYD_LYB: |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 317 | lyd_parse_lyb(ctx, data, options, trees, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 318 | break; |
| 319 | #endif |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 320 | case LYD_SCHEMA: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 321 | LOGINT(ctx); |
| 322 | break; |
| 323 | } |
| 324 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 325 | return result; |
| 326 | } |
| 327 | |
| 328 | API struct lyd_node * |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 329 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 330 | { |
| 331 | struct lyd_node *result; |
| 332 | size_t length; |
| 333 | char *addr; |
| 334 | |
| 335 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 336 | if (fd < 0) { |
| 337 | LOGARG(ctx, fd); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 342 | result = lyd_parse_mem(ctx, addr ? addr : "", format, options); |
Radek Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 343 | if (addr) { |
| 344 | ly_munmap(addr, length); |
| 345 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 346 | |
| 347 | return result; |
| 348 | } |
| 349 | |
| 350 | API struct lyd_node * |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 351 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 352 | { |
| 353 | int fd; |
| 354 | struct lyd_node *result; |
| 355 | size_t len; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 356 | |
| 357 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 358 | |
| 359 | fd = open(path, O_RDONLY); |
| 360 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 361 | |
| 362 | if (!format) { |
| 363 | /* unknown format - try to detect it from filename's suffix */ |
| 364 | len = strlen(path); |
| 365 | |
| 366 | /* ignore trailing whitespaces */ |
| 367 | for (; len > 0 && isspace(path[len - 1]); len--); |
| 368 | |
| 369 | if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) { |
| 370 | format = LYD_XML; |
| 371 | #if 0 |
| 372 | } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) { |
| 373 | format = LYD_JSON; |
| 374 | } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) { |
| 375 | format = LYD_LYB; |
| 376 | #endif |
| 377 | } /* else still unknown, try later to detect it from the content */ |
| 378 | } |
| 379 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 380 | result = lyd_parse_fd(ctx, fd, format, options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 381 | close(fd); |
| 382 | |
| 383 | return result; |
| 384 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 385 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 386 | LY_ERR |
| 387 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, |
| 388 | ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node) |
| 389 | { |
| 390 | LY_ERR ret; |
| 391 | struct lyd_node_term *term; |
| 392 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 393 | assert(schema->nodetype & LYD_NODE_TERM); |
| 394 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 395 | term = calloc(1, sizeof *term); |
| 396 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 397 | |
| 398 | term->schema = schema; |
| 399 | term->prev = (struct lyd_node *)term; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 400 | term->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 401 | |
| 402 | ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL); |
| 403 | if (ret && (ret != LY_EINCOMPLETE)) { |
| 404 | free(term); |
| 405 | return ret; |
| 406 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 407 | lyd_hash((struct lyd_node *)term); |
| 408 | |
| 409 | *node = (struct lyd_node *)term; |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | LY_ERR |
| 414 | lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node) |
| 415 | { |
| 416 | LY_ERR ret; |
| 417 | struct lyd_node_term *term; |
| 418 | struct lysc_type *type; |
| 419 | |
| 420 | assert(schema->nodetype & LYD_NODE_TERM); |
| 421 | |
| 422 | term = calloc(1, sizeof *term); |
| 423 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 424 | |
| 425 | term->schema = schema; |
| 426 | term->prev = (struct lyd_node *)term; |
| 427 | term->flags = LYD_NEW; |
| 428 | |
| 429 | type = ((struct lysc_node_leaf *)schema)->type; |
| 430 | ret = type->plugin->duplicate(schema->module->ctx, val, &term->value); |
| 431 | if (ret) { |
| 432 | LOGERR(schema->module->ctx, ret, "Value duplication failed."); |
| 433 | free(term); |
| 434 | return ret; |
| 435 | } |
| 436 | lyd_hash((struct lyd_node *)term); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 437 | |
| 438 | *node = (struct lyd_node *)term; |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | LY_ERR |
| 443 | lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node) |
| 444 | { |
| 445 | struct lyd_node_inner *in; |
| 446 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 447 | assert(schema->nodetype & LYD_NODE_INNER); |
| 448 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 449 | in = calloc(1, sizeof *in); |
| 450 | LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM); |
| 451 | |
| 452 | in->schema = schema; |
| 453 | in->prev = (struct lyd_node *)in; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 454 | in->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 455 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 456 | /* do not hash list with keys, we need them for the hash */ |
| 457 | if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) { |
| 458 | lyd_hash((struct lyd_node *)in); |
| 459 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 460 | |
| 461 | *node = (struct lyd_node *)in; |
| 462 | return LY_SUCCESS; |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | ly_keys_clean(struct ly_keys *keys) |
| 467 | { |
| 468 | size_t i; |
| 469 | |
| 470 | for (i = 0; i < keys->key_count; ++i) { |
| 471 | keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val); |
| 472 | } |
| 473 | free(keys->str); |
| 474 | free(keys->keys); |
| 475 | } |
| 476 | |
| 477 | static char * |
| 478 | ly_keys_parse_next(char **next_key, char **key_name) |
| 479 | { |
| 480 | char *ptr, *ptr2, *val, quot; |
| 481 | |
| 482 | ptr = *next_key; |
| 483 | |
| 484 | /* "[" */ |
| 485 | LY_CHECK_GOTO(ptr[0] != '[', error); |
| 486 | ++ptr; |
| 487 | |
| 488 | /* key name */ |
| 489 | ptr2 = strchr(ptr, '='); |
| 490 | LY_CHECK_GOTO(!ptr2, error); |
| 491 | |
| 492 | *key_name = ptr; |
| 493 | ptr2[0] = '\0'; |
| 494 | |
| 495 | /* \0, was '=' */ |
| 496 | ptr = ptr2 + 1; |
| 497 | |
| 498 | /* quote */ |
| 499 | LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error); |
| 500 | quot = ptr[0]; |
| 501 | ++ptr; |
| 502 | |
| 503 | /* value, terminate it */ |
| 504 | val = ptr; |
| 505 | ptr2 = strchr(ptr, quot); |
| 506 | LY_CHECK_GOTO(!ptr2, error); |
| 507 | ptr2[0] = '\0'; |
| 508 | |
| 509 | /* \0, was quote */ |
| 510 | ptr = ptr2 + 1; |
| 511 | |
| 512 | /* "]" */ |
| 513 | LY_CHECK_GOTO(ptr[0] != ']', error); |
| 514 | ++ptr; |
| 515 | |
| 516 | *next_key = ptr; |
| 517 | return val; |
| 518 | |
| 519 | error: |
| 520 | *next_key = ptr; |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | /* fill keys structure; if store is set, fill also each val */ |
| 525 | static LY_ERR |
| 526 | ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, struct ly_keys *keys) |
| 527 | { |
| 528 | LY_ERR ret = LY_SUCCESS; |
| 529 | char *next_key, *name; |
| 530 | const struct lysc_node *key; |
| 531 | size_t i; |
| 532 | |
| 533 | assert(list->nodetype == LYS_LIST); |
| 534 | |
| 535 | memset(keys, 0, sizeof *keys); |
| 536 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 537 | if (!keys_str) { |
| 538 | /* nothing to parse */ |
| 539 | return LY_SUCCESS; |
| 540 | } |
| 541 | |
| 542 | keys->str = strndup(keys_str, keys_len); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 543 | LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup); |
| 544 | |
| 545 | next_key = keys->str; |
| 546 | while (next_key[0]) { |
| 547 | /* new key */ |
| 548 | keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys); |
| 549 | LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup); |
| 550 | |
| 551 | /* fill */ |
| 552 | keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name); |
| 553 | if (!keys->keys[keys->key_count].value) { |
| 554 | LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key); |
| 555 | ret = LY_EINVAL; |
| 556 | goto cleanup; |
| 557 | } |
| 558 | |
| 559 | /* find schema node */ |
| 560 | key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0); |
| 561 | if (!key) { |
| 562 | LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name); |
| 563 | ret = LY_EINVAL; |
| 564 | goto cleanup; |
| 565 | } |
| 566 | keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key; |
| 567 | |
| 568 | /* check that we do not have it already */ |
| 569 | for (i = 0; i < keys->key_count; ++i) { |
| 570 | if (keys->keys[i].schema == keys->keys[keys->key_count].schema) { |
| 571 | LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name); |
| 572 | ret = LY_EINVAL; |
| 573 | goto cleanup; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (store) { |
| 578 | /* store the value */ |
| 579 | ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0, |
| 580 | lydjson_resolve_prefix, NULL, LYD_JSON); |
| 581 | LY_CHECK_GOTO(ret, cleanup); |
| 582 | } |
| 583 | |
| 584 | /* another valid key */ |
| 585 | ++keys->key_count; |
| 586 | } |
| 587 | |
| 588 | cleanup: |
| 589 | ly_keys_clean(keys); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | LY_ERR |
| 594 | lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node) |
| 595 | { |
| 596 | LY_ERR ret = LY_SUCCESS; |
| 597 | const struct lysc_node *key_s; |
| 598 | struct lyd_node *list = NULL, *key; |
| 599 | struct ly_keys keys = {0}; |
| 600 | size_t i; |
| 601 | |
| 602 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
| 603 | |
| 604 | /* parse keys */ |
| 605 | LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, &keys), cleanup); |
| 606 | |
| 607 | /* create list */ |
| 608 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 609 | |
| 610 | /* everything was checked except that all keys are set */ |
| 611 | i = 0; |
| 612 | for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 613 | ++i; |
| 614 | } |
| 615 | if (i != keys.key_count) { |
| 616 | LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name); |
| 617 | ret = LY_EINVAL; |
| 618 | goto cleanup; |
| 619 | } |
| 620 | |
| 621 | /* create and insert all the keys */ |
| 622 | for (i = 0; i < keys.key_count; ++i) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 623 | LY_CHECK_GOTO(ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, |
| 624 | strlen(keys.keys[i].value), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 625 | lyd_insert_node(list, NULL, key); |
| 626 | } |
| 627 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 628 | /* hash having all the keys */ |
| 629 | lyd_hash(list); |
| 630 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 631 | /* success */ |
| 632 | *node = list; |
| 633 | list = NULL; |
| 634 | |
| 635 | cleanup: |
| 636 | lyd_free_tree(list); |
| 637 | ly_keys_clean(&keys); |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | LY_ERR |
| 642 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 643 | { |
| 644 | struct lyd_node_any *any; |
| 645 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 646 | assert(schema->nodetype & LYD_NODE_ANY); |
| 647 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 648 | any = calloc(1, sizeof *any); |
| 649 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 650 | |
| 651 | any->schema = schema; |
| 652 | any->prev = (struct lyd_node *)any; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 653 | any->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 654 | |
| 655 | any->value.xml = value; |
| 656 | any->value_type = value_type; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 657 | lyd_hash((struct lyd_node *)any); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 658 | |
| 659 | *node = (struct lyd_node *)any; |
| 660 | return LY_SUCCESS; |
| 661 | } |
| 662 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 663 | LY_ERR |
| 664 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len, |
| 665 | int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len, |
| 666 | const char *ns, struct lyd_node **node) |
| 667 | { |
| 668 | struct lyd_node_opaq *opaq; |
| 669 | |
| 670 | assert(ctx && name && name_len && ns); |
| 671 | |
| 672 | if (!value_len) { |
| 673 | value = ""; |
| 674 | } |
| 675 | |
| 676 | opaq = calloc(1, sizeof *opaq); |
| 677 | LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM); |
| 678 | |
| 679 | opaq->prev = (struct lyd_node *)opaq; |
| 680 | |
| 681 | opaq->name = lydict_insert(ctx, name, name_len); |
| 682 | opaq->format = format; |
| 683 | if (pref_len) { |
| 684 | opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len); |
| 685 | } |
| 686 | opaq->prefix.ns = lydict_insert(ctx, ns, 0); |
| 687 | opaq->val_prefs = val_prefs; |
| 688 | if (dynamic && *dynamic) { |
| 689 | opaq->value = lydict_insert_zc(ctx, (char *)value); |
| 690 | *dynamic = 0; |
| 691 | } else { |
| 692 | opaq->value = lydict_insert(ctx, value, value_len); |
| 693 | } |
| 694 | opaq->ctx = ctx; |
| 695 | |
| 696 | *node = (struct lyd_node *)opaq; |
| 697 | return LY_SUCCESS; |
| 698 | } |
| 699 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 700 | API struct lyd_node * |
| 701 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name) |
| 702 | { |
| 703 | struct lyd_node *ret = NULL; |
| 704 | const struct lysc_node *schema; |
| 705 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 706 | |
| 707 | LY_CHECK_ARG_RET(ctx, parent || module, name, NULL); |
| 708 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 709 | if (!module) { |
| 710 | module = parent->schema->module; |
| 711 | } |
| 712 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 713 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_ACTION, 0); |
| 714 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL); |
| 715 | |
| 716 | if (!lyd_create_inner(schema, &ret) && parent) { |
| 717 | lyd_insert_node(parent, NULL, ret); |
| 718 | } |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | API struct lyd_node * |
| 723 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...) |
| 724 | { |
| 725 | struct lyd_node *ret = NULL, *key; |
| 726 | const struct lysc_node *schema, *key_s; |
| 727 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 728 | va_list ap; |
| 729 | const char *key_val; |
| 730 | LY_ERR rc = LY_SUCCESS; |
| 731 | |
| 732 | LY_CHECK_ARG_RET(ctx, parent || module, name, NULL); |
| 733 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 734 | if (!module) { |
| 735 | module = parent->schema->module; |
| 736 | } |
| 737 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 738 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0); |
| 739 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL); |
| 740 | |
| 741 | /* create list inner node */ |
| 742 | LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL); |
| 743 | |
| 744 | va_start(ap, name); |
| 745 | |
| 746 | /* create and insert all the keys */ |
| 747 | for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 748 | key_val = va_arg(ap, const char *); |
| 749 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 750 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key); |
| 751 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 752 | lyd_insert_node(ret, NULL, key); |
| 753 | } |
| 754 | |
| 755 | /* hash having all the keys */ |
| 756 | lyd_hash(ret); |
| 757 | |
| 758 | if (parent) { |
| 759 | lyd_insert_node(parent, NULL, ret); |
| 760 | } |
| 761 | |
| 762 | cleanup: |
| 763 | if (rc) { |
| 764 | lyd_free_tree(ret); |
| 765 | ret = NULL; |
| 766 | } |
| 767 | va_end(ap); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | API struct lyd_node * |
| 772 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys) |
| 773 | { |
| 774 | struct lyd_node *ret = NULL; |
| 775 | const struct lysc_node *schema; |
| 776 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 777 | |
| 778 | LY_CHECK_ARG_RET(ctx, parent || module, name, NULL); |
| 779 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 780 | if (!module) { |
| 781 | module = parent->schema->module; |
| 782 | } |
| 783 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 784 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0); |
| 785 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL); |
| 786 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 787 | if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, &ret) && parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 788 | lyd_insert_node(parent, NULL, ret); |
| 789 | } |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | API struct lyd_node * |
| 794 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str) |
| 795 | { |
| 796 | struct lyd_node *ret = NULL; |
| 797 | const struct lysc_node *schema; |
| 798 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 799 | |
| 800 | LY_CHECK_ARG_RET(ctx, parent || module, name, NULL); |
| 801 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 802 | if (!module) { |
| 803 | module = parent->schema->module; |
| 804 | } |
| 805 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 806 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0); |
| 807 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL); |
| 808 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 809 | if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret) |
| 810 | && parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 811 | lyd_insert_node(parent, NULL, ret); |
| 812 | } |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | API struct lyd_node * |
| 817 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 818 | LYD_ANYDATA_VALUETYPE value_type) |
| 819 | { |
| 820 | struct lyd_node *ret = NULL; |
| 821 | const struct lysc_node *schema; |
| 822 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 823 | |
| 824 | LY_CHECK_ARG_RET(ctx, parent || module, name, NULL); |
| 825 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 826 | if (!module) { |
| 827 | module = parent->schema->module; |
| 828 | } |
| 829 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 830 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0); |
| 831 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL); |
| 832 | |
| 833 | if (!lyd_create_any(schema, value, value_type, &ret) && parent) { |
| 834 | lyd_insert_node(parent, NULL, ret); |
| 835 | } |
| 836 | return ret; |
| 837 | } |
| 838 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 839 | struct lyd_node * |
| 840 | lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key) |
| 841 | { |
| 842 | const struct lysc_node *prev_key; |
| 843 | struct lyd_node *match = NULL; |
| 844 | |
| 845 | if (!first_sibling) { |
| 846 | return NULL; |
| 847 | } |
| 848 | |
| 849 | for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) { |
| 850 | lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match); |
| 851 | } |
| 852 | |
| 853 | return match; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * @brief Insert node after a sibling. |
| 858 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 859 | * Handles inserting into NP containers and key-less lists. |
| 860 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 861 | * @param[in] sibling Sibling to insert after. |
| 862 | * @param[in] node Node to insert. |
| 863 | */ |
| 864 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 865 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 866 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 867 | struct lyd_node_inner *par; |
| 868 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 869 | assert(!node->next && (node->prev == node)); |
| 870 | |
| 871 | node->next = sibling->next; |
| 872 | node->prev = sibling; |
| 873 | sibling->next = node; |
| 874 | if (node->next) { |
| 875 | /* sibling had a succeeding node */ |
| 876 | node->next->prev = node; |
| 877 | } else { |
| 878 | /* sibling was last, find first sibling and change its prev */ |
| 879 | if (sibling->parent) { |
| 880 | sibling = sibling->parent->child; |
| 881 | } else { |
| 882 | for (; sibling->prev->next != node; sibling = sibling->prev); |
| 883 | } |
| 884 | sibling->prev = node; |
| 885 | } |
| 886 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 887 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 888 | for (par = node->parent; par; par = par->parent) { |
| 889 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 890 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 891 | par->flags &= ~LYD_DEFAULT; |
| 892 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 893 | if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
| 894 | /* rehash key-less list */ |
| 895 | lyd_hash((struct lyd_node *)par); |
| 896 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* insert into hash table */ |
| 900 | lyd_insert_hash(node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** |
| 904 | * @brief Insert node before a sibling. |
| 905 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 906 | * Handles inserting into NP containers and key-less lists. |
| 907 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 908 | * @param[in] sibling Sibling to insert before. |
| 909 | * @param[in] node Node to insert. |
| 910 | */ |
| 911 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 912 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 913 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 914 | struct lyd_node_inner *par; |
| 915 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 916 | assert(!node->next && (node->prev == node)); |
| 917 | |
| 918 | node->next = sibling; |
| 919 | /* covers situation of sibling being first */ |
| 920 | node->prev = sibling->prev; |
| 921 | sibling->prev = node; |
| 922 | if (node->prev->next) { |
| 923 | /* sibling had a preceding node */ |
| 924 | node->prev->next = node; |
| 925 | } else if (sibling->parent) { |
| 926 | /* sibling was first and we must also change parent child pointer */ |
| 927 | sibling->parent->child = node; |
| 928 | } |
| 929 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 930 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 931 | for (par = node->parent; par; par = par->parent) { |
| 932 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 933 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 934 | par->flags &= ~LYD_DEFAULT; |
| 935 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 936 | if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
| 937 | /* rehash key-less list */ |
| 938 | lyd_hash((struct lyd_node *)par); |
| 939 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* insert into hash table */ |
| 943 | lyd_insert_hash(node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /** |
| 947 | * @brief Insert node as the last child of a parent. |
| 948 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 949 | * Handles inserting into NP containers and key-less lists. |
| 950 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 951 | * @param[in] parent Parent to insert into. |
| 952 | * @param[in] node Node to insert. |
| 953 | */ |
| 954 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 955 | lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 956 | { |
| 957 | struct lyd_node_inner *par; |
| 958 | |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 959 | assert(parent && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 960 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 961 | |
| 962 | par = (struct lyd_node_inner *)parent; |
| 963 | |
| 964 | if (!par->child) { |
| 965 | par->child = node; |
| 966 | } else { |
| 967 | node->prev = par->child->prev; |
| 968 | par->child->prev->next = node; |
| 969 | par->child->prev = node; |
| 970 | } |
| 971 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 972 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 973 | for (; par; par = par->parent) { |
| 974 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 975 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 976 | par->flags &= ~LYD_DEFAULT; |
| 977 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 978 | if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 979 | /* rehash key-less list */ |
| 980 | lyd_hash((struct lyd_node *)par); |
| 981 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* insert into hash table */ |
| 985 | lyd_insert_hash(node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | void |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 989 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 990 | { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 991 | struct lyd_node *anchor; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 992 | const struct lysc_node *skey = NULL; |
| 993 | int has_keys; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 994 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 995 | assert((parent || first_sibling) && node && (node->hash || !node->schema)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 996 | |
| 997 | if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) { |
| 998 | parent = (struct lyd_node *)(*first_sibling)->parent; |
| 999 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1000 | |
| 1001 | if (parent) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1002 | if (node->schema && (node->schema->flags & LYS_KEY)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1003 | /* it is key and we need to insert it at the correct place */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1004 | anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema); |
| 1005 | if (anchor) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1006 | lyd_insert_after_node(anchor, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1007 | } else if (lyd_node_children(parent)) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1008 | lyd_insert_before_node((struct lyd_node *)lyd_node_children(parent), node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1009 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1010 | lyd_insert_last_node(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1011 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1012 | |
| 1013 | /* hash list if all its keys were added */ |
| 1014 | assert(parent->schema->nodetype == LYS_LIST); |
| 1015 | anchor = (struct lyd_node *)lyd_node_children(parent); |
| 1016 | has_keys = 1; |
| 1017 | while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 1018 | if (!anchor || (anchor->schema != skey)) { |
| 1019 | /* key missing */ |
| 1020 | has_keys = 0; |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | anchor = anchor->next; |
| 1025 | } |
| 1026 | if (has_keys) { |
| 1027 | lyd_hash(parent); |
| 1028 | } |
| 1029 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1030 | } else { |
| 1031 | /* last child */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1032 | lyd_insert_last_node(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1033 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1034 | } else if (*first_sibling) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1035 | /* top-level siblings */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1036 | anchor = (*first_sibling)->prev; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1037 | while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1038 | anchor = anchor->prev; |
| 1039 | } |
| 1040 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1041 | if (lyd_owner_module(anchor) == lyd_owner_module(node)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1042 | /* insert after last sibling from this module */ |
| 1043 | lyd_insert_after_node(anchor, node); |
| 1044 | } else { |
| 1045 | /* no data from this module, insert at the last position */ |
| 1046 | lyd_insert_after_node((*first_sibling)->prev, node); |
| 1047 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1048 | } else { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1049 | /* the only sibling */ |
| 1050 | *first_sibling = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1051 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1054 | static LY_ERR |
| 1055 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema) |
| 1056 | { |
| 1057 | const struct lysc_node *par2; |
| 1058 | |
| 1059 | assert(schema); |
| 1060 | |
| 1061 | /* adjust parent first */ |
| 1062 | while (parent && (parent->nodetype & (LYS_CASE | LYS_CHOICE))) { |
| 1063 | parent = parent->parent; |
| 1064 | } |
| 1065 | |
| 1066 | /* find schema parent */ |
| 1067 | for (par2 = schema->parent; par2 && (par2->nodetype & (LYS_CASE | LYS_CHOICE)); par2 = par2->parent); |
| 1068 | |
| 1069 | if (parent) { |
| 1070 | /* inner node */ |
| 1071 | if (par2 != parent) { |
| 1072 | LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name); |
| 1073 | return LY_EINVAL; |
| 1074 | } |
| 1075 | } else { |
| 1076 | /* top-level node */ |
| 1077 | if (par2) { |
| 1078 | LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name); |
| 1079 | return LY_EINVAL; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return LY_SUCCESS; |
| 1084 | } |
| 1085 | |
| 1086 | API LY_ERR |
| 1087 | lyd_insert(struct lyd_node *parent, struct lyd_node *node) |
| 1088 | { |
| 1089 | struct lyd_node *iter; |
| 1090 | |
| 1091 | LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
| 1092 | |
| 1093 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema)); |
| 1094 | |
| 1095 | if (node->schema->flags & LYS_KEY) { |
| 1096 | LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1097 | return LY_EINVAL; |
| 1098 | } |
| 1099 | |
| 1100 | if (node->parent || node->prev->next) { |
| 1101 | lyd_unlink_tree(node); |
| 1102 | } |
| 1103 | |
| 1104 | while (node) { |
| 1105 | iter = node->next; |
| 1106 | lyd_unlink_tree(node); |
| 1107 | lyd_insert_node(parent, NULL, node); |
| 1108 | node = iter; |
| 1109 | } |
| 1110 | return LY_SUCCESS; |
| 1111 | } |
| 1112 | |
| 1113 | API LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1114 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node) |
| 1115 | { |
| 1116 | struct lyd_node *iter; |
| 1117 | |
| 1118 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1119 | |
| 1120 | LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema)); |
| 1121 | |
| 1122 | if (node->schema->flags & LYS_KEY) { |
| 1123 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1124 | return LY_EINVAL; |
| 1125 | } |
| 1126 | |
| 1127 | if (node->parent || node->prev->next) { |
| 1128 | lyd_unlink_tree(node); |
| 1129 | } |
| 1130 | |
| 1131 | while (node) { |
| 1132 | iter = node->next; |
| 1133 | lyd_unlink_tree(node); |
| 1134 | lyd_insert_node(NULL, &sibling, node); |
| 1135 | node = iter; |
| 1136 | } |
| 1137 | return LY_SUCCESS; |
| 1138 | } |
| 1139 | |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1140 | static LY_ERR |
| 1141 | lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node) |
| 1142 | { |
| 1143 | if (sibling->parent) { |
| 1144 | /* nested, we do not care for the order */ |
| 1145 | return LY_SUCCESS; |
| 1146 | } |
| 1147 | |
| 1148 | if (anchor) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1149 | if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next)) |
| 1150 | && (lyd_owner_module(node) != lyd_owner_module(anchor))) { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1151 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert top-level module \"%s\" data into module \"%s\" data.", |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1152 | lyd_owner_module(node)->name, lyd_owner_module(anchor)->name); |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1153 | return LY_EINVAL; |
| 1154 | } |
| 1155 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1156 | if ((lyd_owner_module(node) == lyd_owner_module(anchor)) |
| 1157 | || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1158 | /* inserting before/after its module data */ |
| 1159 | return LY_SUCCESS; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /* find first sibling */ |
| 1164 | while (sibling->prev->next) { |
| 1165 | sibling = sibling->prev; |
| 1166 | } |
| 1167 | |
| 1168 | if (!anchor) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1169 | if (lyd_owner_module(node) == lyd_owner_module(sibling)) { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1170 | /* inserting before its module data */ |
| 1171 | return LY_SUCCESS; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* check there are no data of this module */ |
| 1176 | LY_LIST_FOR(sibling, sibling) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1177 | if (lyd_owner_module(node) == lyd_owner_module(sibling)) { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1178 | /* some data of this module found */ |
| 1179 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist," |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1180 | " they must be directly connected.", lyd_owner_module(node)->name); |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1181 | return LY_EINVAL; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return LY_SUCCESS; |
| 1186 | } |
| 1187 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1188 | API LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1189 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 1190 | { |
| 1191 | struct lyd_node *iter; |
| 1192 | |
| 1193 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1194 | |
| 1195 | LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema)); |
| 1196 | |
| 1197 | if (node->schema->flags & LYS_KEY) { |
| 1198 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1199 | return LY_EINVAL; |
| 1200 | } else if (sibling->schema->flags & LYS_KEY) { |
| 1201 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys."); |
| 1202 | return LY_EINVAL; |
| 1203 | } |
| 1204 | |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1205 | LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node)); |
| 1206 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1207 | if (node->parent || node->prev->next) { |
| 1208 | lyd_unlink_tree(node); |
| 1209 | } |
| 1210 | |
| 1211 | /* insert in reverse order to get the original order */ |
| 1212 | node = node->prev; |
| 1213 | while (node) { |
| 1214 | iter = node->prev; |
| 1215 | lyd_unlink_tree(node); |
| 1216 | |
| 1217 | lyd_insert_before_node(sibling, node); |
| 1218 | /* move the anchor accordingly */ |
| 1219 | sibling = node; |
| 1220 | |
| 1221 | node = (iter == node) ? NULL : iter; |
| 1222 | } |
| 1223 | return LY_SUCCESS; |
| 1224 | } |
| 1225 | |
| 1226 | API LY_ERR |
| 1227 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 1228 | { |
| 1229 | struct lyd_node *iter; |
| 1230 | |
| 1231 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1232 | |
| 1233 | LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema)); |
| 1234 | |
| 1235 | if (node->schema->flags & LYS_KEY) { |
| 1236 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1237 | return LY_EINVAL; |
| 1238 | } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) { |
| 1239 | LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys."); |
| 1240 | return LY_EINVAL; |
| 1241 | } |
| 1242 | |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1243 | LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node)); |
| 1244 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1245 | if (node->parent || node->prev->next) { |
| 1246 | lyd_unlink_tree(node); |
| 1247 | } |
| 1248 | |
| 1249 | while (node) { |
| 1250 | iter = node->next; |
| 1251 | lyd_unlink_tree(node); |
| 1252 | |
| 1253 | lyd_insert_after_node(sibling, node); |
| 1254 | /* move the anchor accordingly */ |
| 1255 | sibling = node; |
| 1256 | |
| 1257 | node = iter; |
| 1258 | } |
| 1259 | return LY_SUCCESS; |
| 1260 | } |
| 1261 | |
| 1262 | API void |
| 1263 | lyd_unlink_tree(struct lyd_node *node) |
| 1264 | { |
| 1265 | struct lyd_node *iter; |
| 1266 | |
| 1267 | if (!node) { |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | /* unlink from siblings */ |
| 1272 | if (node->prev->next) { |
| 1273 | node->prev->next = node->next; |
| 1274 | } |
| 1275 | if (node->next) { |
| 1276 | node->next->prev = node->prev; |
| 1277 | } else { |
| 1278 | /* unlinking the last node */ |
| 1279 | if (node->parent) { |
| 1280 | iter = node->parent->child; |
| 1281 | } else { |
| 1282 | iter = node->prev; |
| 1283 | while (iter->prev != node) { |
| 1284 | iter = iter->prev; |
| 1285 | } |
| 1286 | } |
| 1287 | /* update the "last" pointer from the first node */ |
| 1288 | iter->prev = node->prev; |
| 1289 | } |
| 1290 | |
| 1291 | /* unlink from parent */ |
| 1292 | if (node->parent) { |
| 1293 | if (node->parent->child == node) { |
| 1294 | /* the node is the first child */ |
| 1295 | node->parent->child = node->next; |
| 1296 | } |
| 1297 | |
| 1298 | lyd_unlink_hash(node); |
| 1299 | |
| 1300 | /* check for keyless list and update its hash */ |
| 1301 | for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) { |
| 1302 | if (iter->schema->flags & LYS_KEYLESS) { |
| 1303 | lyd_hash(iter); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | node->parent = NULL; |
| 1308 | } |
| 1309 | |
| 1310 | node->next = NULL; |
| 1311 | node->prev = node; |
| 1312 | } |
| 1313 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1314 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1315 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1316 | size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix resolve_prefix, |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 1317 | void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1318 | { |
| 1319 | LY_ERR ret; |
| 1320 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1321 | struct lyd_meta *mt, *last; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1322 | uint32_t v; |
| 1323 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1324 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1325 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1326 | LY_ARRAY_FOR(mod->compiled->exts, v) { |
| 1327 | if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin && |
| 1328 | !ly_strncmp(mod->compiled->exts[v].argument, name, name_len)) { |
| 1329 | /* we have the annotation definition */ |
| 1330 | ant = &mod->compiled->exts[v]; |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | if (!ant) { |
| 1335 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
| 1336 | LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.", |
| 1337 | mod->name, name_len, name); |
| 1338 | return LY_EINVAL; |
| 1339 | } |
| 1340 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1341 | mt = calloc(1, sizeof *mt); |
| 1342 | LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM); |
| 1343 | mt->parent = parent; |
| 1344 | mt->annotation = ant; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1345 | ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, resolve_prefix, prefix_data, format, ctx_snode, NULL); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1346 | if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1347 | free(mt); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1348 | return ret; |
| 1349 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1350 | mt->name = lydict_insert(mod->ctx, name, name_len); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1351 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1352 | /* insert as the last attribute */ |
| 1353 | if (parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1354 | if (parent->meta) { |
| 1355 | for (last = parent->meta; last->next; last = last->next); |
| 1356 | last->next = mt; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1357 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1358 | parent->meta = mt; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1359 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1360 | } else if (*meta) { |
| 1361 | for (last = *meta; last->next; last = last->next); |
| 1362 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* remove default flags from NP containers */ |
| 1366 | while (parent && (parent->flags & LYD_DEFAULT)) { |
| 1367 | parent->flags &= ~LYD_DEFAULT; |
| 1368 | parent = (struct lyd_node *)parent->parent; |
| 1369 | } |
| 1370 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1371 | if (meta) { |
| 1372 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1373 | } |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1377 | LY_ERR |
| 1378 | ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name, |
| 1379 | size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format, |
| 1380 | struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns) |
| 1381 | { |
| 1382 | struct ly_attr *at, *last; |
| 1383 | struct lyd_node_opaq *opaq; |
| 1384 | |
| 1385 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
| 1386 | assert(name && name_len); |
| 1387 | assert((prefix_len && ns) || (!prefix_len && !ns)); |
| 1388 | |
| 1389 | if (!value_len) { |
| 1390 | value = ""; |
| 1391 | } |
| 1392 | |
| 1393 | at = calloc(1, sizeof *at); |
| 1394 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM); |
| 1395 | at->parent = (struct lyd_node_opaq *)parent; |
| 1396 | at->name = lydict_insert(ctx, name, name_len); |
| 1397 | if (dynamic && *dynamic) { |
| 1398 | at->value = lydict_insert_zc(ctx, (char *)value); |
| 1399 | *dynamic = 0; |
| 1400 | } else { |
| 1401 | at->value = lydict_insert(ctx, value, value_len); |
| 1402 | } |
| 1403 | |
| 1404 | at->format = format; |
| 1405 | at->val_prefs = val_prefs; |
| 1406 | if (ns) { |
| 1407 | at->prefix.pref = lydict_insert(ctx, prefix, prefix_len); |
| 1408 | at->prefix.ns = lydict_insert(ctx, ns, 0); |
| 1409 | } |
| 1410 | |
| 1411 | /* insert as the last attribute */ |
| 1412 | if (parent) { |
| 1413 | opaq = (struct lyd_node_opaq *)parent; |
| 1414 | if (opaq->attr) { |
| 1415 | for (last = opaq->attr; last->next; last = last->next); |
| 1416 | last->next = at; |
| 1417 | } else { |
| 1418 | opaq->attr = at; |
| 1419 | } |
| 1420 | } else if (*attr) { |
| 1421 | for (last = *attr; last->next; last = last->next); |
| 1422 | last->next = at; |
| 1423 | } |
| 1424 | |
| 1425 | if (attr) { |
| 1426 | *attr = at; |
| 1427 | } |
| 1428 | return LY_SUCCESS; |
| 1429 | } |
| 1430 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1431 | API const struct lyd_node_term * |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1432 | lyd_target(struct lyd_value_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1433 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1434 | unsigned int u, x; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1435 | const struct lyd_node *parent = NULL, *start_search; |
| 1436 | struct lyd_node *node = NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1437 | uint64_t pos = 1; |
| 1438 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1439 | LY_CHECK_ARG_RET(NULL, path, tree, NULL); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1440 | |
| 1441 | LY_ARRAY_FOR(path, u) { |
| 1442 | if (parent) { |
| 1443 | start_search = lyd_node_children(parent); |
| 1444 | search_inner: |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1445 | lyd_find_sibling_next(start_search, path[u].node->module, path[u].node->name, 0, NULL, 0, &node); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1446 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1447 | start_search = tree; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1448 | search_toplevel: |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1449 | /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */ |
| 1450 | lyd_find_sibling_next(start_search, path[u].node->module, path[u].node->name, 0, NULL, 0, &node); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1451 | } |
| 1452 | if (!node) { |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | |
| 1456 | /* check predicate if any */ |
| 1457 | LY_ARRAY_FOR(path[u].predicates, x) { |
| 1458 | if (path[u].predicates[x].type == 0) { |
| 1459 | /* position predicate */ |
| 1460 | if (pos != path[u].predicates[x].position) { |
| 1461 | pos++; |
| 1462 | goto search_repeat; |
| 1463 | } |
| 1464 | /* done, no more predicates are allowed here */ |
| 1465 | break; |
| 1466 | } else if (path[u].predicates[x].type == 1) { |
| 1467 | /* key-predicate */ |
| 1468 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1469 | struct lyd_node *key; |
| 1470 | lyd_find_sibling_next(lyd_node_children(node), path[u].predicates[x].key->module, |
| 1471 | path[u].predicates[x].key->name, 0, NULL, 0, &key); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1472 | if (!key) { |
| 1473 | /* probably error and we shouldn't be here due to previous checks when creating path */ |
| 1474 | goto search_repeat; |
| 1475 | } |
| 1476 | if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) { |
| 1477 | goto search_repeat; |
| 1478 | } |
| 1479 | } else if (path[u].predicates[x].type == 2) { |
| 1480 | /* leaf-list-predicate */ |
| 1481 | struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type; |
| 1482 | if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) { |
| 1483 | goto search_repeat; |
| 1484 | } |
| 1485 | } else { |
| 1486 | LOGINT(NULL); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | parent = node; |
| 1491 | } |
| 1492 | |
| 1493 | return (const struct lyd_node_term*)node; |
| 1494 | |
| 1495 | search_repeat: |
| 1496 | start_search = node->next; |
| 1497 | if (parent) { |
| 1498 | goto search_inner; |
| 1499 | } else { |
| 1500 | goto search_toplevel; |
| 1501 | } |
| 1502 | } |
| 1503 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1504 | API LY_ERR |
| 1505 | lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options) |
| 1506 | { |
| 1507 | const struct lyd_node *iter1, *iter2; |
| 1508 | struct lyd_node_term *term1, *term2; |
| 1509 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1510 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1511 | struct lysc_type *type; |
| 1512 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1513 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1514 | if (!node1 || !node2) { |
| 1515 | if (node1 == node2) { |
| 1516 | return LY_SUCCESS; |
| 1517 | } else { |
| 1518 | return LY_ENOT; |
| 1519 | } |
| 1520 | } |
| 1521 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1522 | if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1523 | return LY_ENOT; |
| 1524 | } |
| 1525 | |
| 1526 | if (node1->hash != node2->hash) { |
| 1527 | return LY_ENOT; |
| 1528 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1529 | /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1530 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1531 | if (!node1->schema) { |
| 1532 | opaq1 = (struct lyd_node_opaq *)node1; |
| 1533 | opaq2 = (struct lyd_node_opaq *)node2; |
| 1534 | if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1535 | return LY_ENOT; |
| 1536 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1537 | switch (opaq1->format) { |
| 1538 | case LYD_XML: |
| 1539 | if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) { |
| 1540 | return LY_ENOT; |
| 1541 | } |
| 1542 | break; |
| 1543 | case LYD_SCHEMA: |
| 1544 | /* not allowed */ |
| 1545 | LOGINT(LYD_NODE_CTX(node1)); |
| 1546 | return LY_EINT; |
| 1547 | } |
| 1548 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 1549 | iter1 = opaq1->child; |
| 1550 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1551 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1552 | } |
| 1553 | return LY_SUCCESS; |
| 1554 | } else { |
| 1555 | switch (node1->schema->nodetype) { |
| 1556 | case LYS_LEAF: |
| 1557 | case LYS_LEAFLIST: |
| 1558 | if (options & LYD_COMPARE_DEFAULTS) { |
| 1559 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 1560 | return LY_ENOT; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | term1 = (struct lyd_node_term*)node1; |
| 1565 | term2 = (struct lyd_node_term*)node2; |
| 1566 | type = ((struct lysc_node_leaf*)node1->schema)->type; |
| 1567 | |
| 1568 | return type->plugin->compare(&term1->value, &term2->value); |
| 1569 | case LYS_CONTAINER: |
| 1570 | if (options & LYD_COMPARE_DEFAULTS) { |
| 1571 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 1572 | return LY_ENOT; |
| 1573 | } |
| 1574 | } |
| 1575 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 1576 | iter1 = ((struct lyd_node_inner*)node1)->child; |
| 1577 | iter2 = ((struct lyd_node_inner*)node2)->child; |
| 1578 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1579 | } |
| 1580 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1581 | case LYS_ACTION: |
| 1582 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 1583 | /* TODO action/RPC |
| 1584 | goto all_children_compare; |
| 1585 | */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1586 | } |
| 1587 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1588 | case LYS_NOTIF: |
| 1589 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 1590 | /* TODO Notification |
| 1591 | goto all_children_compare; |
| 1592 | */ |
| 1593 | } |
| 1594 | return LY_SUCCESS; |
| 1595 | case LYS_LIST: |
| 1596 | iter1 = ((struct lyd_node_inner*)node1)->child; |
| 1597 | iter2 = ((struct lyd_node_inner*)node2)->child; |
| 1598 | |
| 1599 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 1600 | /* lists with keys, their equivalence is based on their keys */ |
| 1601 | for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child; |
| 1602 | key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); |
| 1603 | key = key->next) { |
| 1604 | if (lyd_compare(iter1, iter2, options)) { |
| 1605 | return LY_ENOT; |
| 1606 | } |
| 1607 | iter1 = iter1->next; |
| 1608 | iter2 = iter2->next; |
| 1609 | } |
| 1610 | } else { |
| 1611 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 1612 | |
| 1613 | all_children_compare: |
| 1614 | if (!iter1 && !iter2) { |
| 1615 | /* no children, nothing to compare */ |
| 1616 | return LY_SUCCESS; |
| 1617 | } |
| 1618 | |
| 1619 | for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
| 1620 | if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) { |
| 1621 | return LY_ENOT; |
| 1622 | } |
| 1623 | } |
| 1624 | if (iter1 || iter2) { |
| 1625 | return LY_ENOT; |
| 1626 | } |
| 1627 | } |
| 1628 | return LY_SUCCESS; |
| 1629 | case LYS_ANYXML: |
| 1630 | case LYS_ANYDATA: |
| 1631 | any1 = (struct lyd_node_any*)node1; |
| 1632 | any2 = (struct lyd_node_any*)node2; |
| 1633 | |
| 1634 | if (any1->value_type != any2->value_type) { |
| 1635 | return LY_ENOT; |
| 1636 | } |
| 1637 | switch (any1->value_type) { |
| 1638 | case LYD_ANYDATA_DATATREE: |
| 1639 | iter1 = any1->value.tree; |
| 1640 | iter2 = any2->value.tree; |
| 1641 | goto all_children_compare; |
| 1642 | case LYD_ANYDATA_STRING: |
| 1643 | case LYD_ANYDATA_XML: |
| 1644 | case LYD_ANYDATA_JSON: |
| 1645 | len1 = strlen(any1->value.str); |
| 1646 | len2 = strlen(any2->value.str); |
| 1647 | if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) { |
| 1648 | return LY_ENOT; |
| 1649 | } |
| 1650 | return LY_SUCCESS; |
| 1651 | #if 0 /* TODO LYB format */ |
| 1652 | case LYD_ANYDATA_LYB: |
| 1653 | int len1 = lyd_lyb_data_length(any1->value.mem); |
| 1654 | int len2 = lyd_lyb_data_length(any2->value.mem); |
| 1655 | if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) { |
| 1656 | return LY_ENOT; |
| 1657 | } |
| 1658 | return LY_SUCCESS; |
| 1659 | #endif |
| 1660 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1664 | LOGINT(LYD_NODE_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1665 | return LY_EINT; |
| 1666 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1667 | |
| 1668 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1669 | * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1670 | * |
| 1671 | * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup(). |
| 1672 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1673 | static LY_ERR |
| 1674 | lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options, |
| 1675 | struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1676 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1677 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1678 | struct lyd_node *dup = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1679 | uint32_t u; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1680 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1681 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1682 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1683 | if (!node->schema) { |
| 1684 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
| 1685 | } else { |
| 1686 | switch (node->schema->nodetype) { |
| 1687 | case LYS_ACTION: |
| 1688 | case LYS_NOTIF: |
| 1689 | case LYS_CONTAINER: |
| 1690 | case LYS_LIST: |
| 1691 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 1692 | break; |
| 1693 | case LYS_LEAF: |
| 1694 | case LYS_LEAFLIST: |
| 1695 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 1696 | break; |
| 1697 | case LYS_ANYDATA: |
| 1698 | case LYS_ANYXML: |
| 1699 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 1700 | break; |
| 1701 | default: |
| 1702 | LOGINT(LYD_NODE_CTX(node)); |
| 1703 | ret = LY_EINT; |
| 1704 | goto error; |
| 1705 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1706 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1707 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1708 | |
| 1709 | /* TODO implement LYD_DUP_WITH_WHEN */ |
| 1710 | dup->flags = node->flags; |
| 1711 | dup->schema = node->schema; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1712 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1713 | |
| 1714 | /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */ |
| 1715 | |
| 1716 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1717 | if (!dup->schema) { |
| 1718 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 1719 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 1720 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1721 | |
| 1722 | if (options & LYD_DUP_RECURSIVE) { |
| 1723 | /* duplicate all the children */ |
| 1724 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1725 | LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error); |
| 1726 | } |
| 1727 | } |
| 1728 | opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0); |
| 1729 | opaq->format = orig->format; |
| 1730 | if (orig->prefix.pref) { |
| 1731 | opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0); |
| 1732 | } |
| 1733 | if (orig->prefix.ns) { |
| 1734 | opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0); |
| 1735 | } |
| 1736 | if (orig->val_prefs) { |
| 1737 | LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error); |
| 1738 | LY_ARRAY_FOR(orig->val_prefs, u) { |
| 1739 | opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0); |
| 1740 | opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0); |
| 1741 | LY_ARRAY_INCREMENT(opaq->val_prefs); |
| 1742 | } |
| 1743 | } |
| 1744 | opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0); |
| 1745 | opaq->ctx = orig->ctx; |
| 1746 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 1747 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 1748 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 1749 | |
| 1750 | term->hash = orig->hash; |
| 1751 | term->value.realtype = orig->value.realtype; |
| 1752 | LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value), |
| 1753 | LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error); |
| 1754 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 1755 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 1756 | struct lyd_node *child; |
| 1757 | |
| 1758 | if (options & LYD_DUP_RECURSIVE) { |
| 1759 | /* duplicate all the children */ |
| 1760 | LY_LIST_FOR(orig->child, child) { |
| 1761 | LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1762 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1763 | } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1764 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1765 | child = orig->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1766 | for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1767 | key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); |
| 1768 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1769 | if (!child) { |
| 1770 | /* possibly not keys are present in filtered tree */ |
| 1771 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1772 | } else if (child->schema != key) { |
| 1773 | /* possibly not all keys are present in filtered tree, |
| 1774 | * but there can be also some non-key nodes */ |
| 1775 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1776 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1777 | LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1778 | child = child->next; |
| 1779 | } |
| 1780 | } |
| 1781 | lyd_hash(dup); |
| 1782 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1783 | struct lyd_node_any *any = (struct lyd_node_any *)dup; |
| 1784 | struct lyd_node_any *orig = (struct lyd_node_any *)node; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1785 | |
| 1786 | any->hash = orig->hash; |
| 1787 | any->value_type = orig->value_type; |
| 1788 | switch (any->value_type) { |
| 1789 | case LYD_ANYDATA_DATATREE: |
| 1790 | if (orig->value.tree) { |
| 1791 | any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS); |
| 1792 | LY_CHECK_GOTO(!any->value.tree, error); |
| 1793 | } |
| 1794 | break; |
| 1795 | case LYD_ANYDATA_STRING: |
| 1796 | case LYD_ANYDATA_XML: |
| 1797 | case LYD_ANYDATA_JSON: |
| 1798 | if (orig->value.str) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1799 | any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1800 | } |
| 1801 | break; |
| 1802 | } |
| 1803 | } |
| 1804 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1805 | /* insert */ |
| 1806 | lyd_insert_node(parent, first, dup); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1807 | lyd_insert_hash(dup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1808 | |
| 1809 | if (dup_p) { |
| 1810 | *dup_p = dup; |
| 1811 | } |
| 1812 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1813 | |
| 1814 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1815 | lyd_free_tree(dup); |
| 1816 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | API struct lyd_node * |
| 1820 | lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options) |
| 1821 | { |
| 1822 | struct ly_ctx *ctx; |
| 1823 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 1824 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1825 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 1826 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
| 1827 | int keyless_parent_list = 0; |
| 1828 | |
| 1829 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 1830 | ctx = node->schema->module->ctx; |
| 1831 | |
| 1832 | if (options & LYD_DUP_WITH_PARENTS) { |
| 1833 | struct lyd_node_inner *orig_parent, *iter; |
| 1834 | int repeat = 1; |
| 1835 | for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 1836 | if (parent && parent->schema == orig_parent->schema) { |
| 1837 | /* stop creating parents, connect what we have into the provided parent */ |
| 1838 | iter = parent; |
| 1839 | repeat = 0; |
| 1840 | /* get know if there is a keyless list which we will have to rehash */ |
| 1841 | for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1842 | if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1843 | keyless_parent_list = 1; |
| 1844 | break; |
| 1845 | } |
| 1846 | } |
| 1847 | } else { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1848 | iter = NULL; |
| 1849 | LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0, |
| 1850 | (struct lyd_node **)&iter), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1851 | } |
| 1852 | if (!local_parent) { |
| 1853 | local_parent = iter; |
| 1854 | } |
| 1855 | if (iter->child) { |
| 1856 | /* 1) list - add after keys |
| 1857 | * 2) provided parent with some children */ |
| 1858 | iter->child->prev->next = top; |
| 1859 | if (top) { |
| 1860 | top->prev = iter->child->prev; |
| 1861 | iter->child->prev = top; |
| 1862 | } |
| 1863 | } else { |
| 1864 | iter->child = top; |
| 1865 | if (iter->schema->nodetype == LYS_LIST) { |
| 1866 | /* keyless list - we will need to rehash it since we are going to add nodes into it */ |
| 1867 | keyless_parent_list = 1; |
| 1868 | } |
| 1869 | } |
| 1870 | if (top) { |
| 1871 | top->parent = iter; |
| 1872 | } |
| 1873 | top = (struct lyd_node*)iter; |
| 1874 | } |
| 1875 | if (repeat && parent) { |
| 1876 | /* given parent and created parents chain actually do not interconnect */ |
| 1877 | LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
| 1878 | goto error; |
| 1879 | } |
| 1880 | } else { |
| 1881 | local_parent = parent; |
| 1882 | } |
| 1883 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1884 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame^] | 1885 | /* if there is no local parent, it will be inserted into first */ |
| 1886 | LY_CHECK_GOTO(lyd_dup_recursive(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1887 | if (!(options & LYD_DUP_WITH_SIBLINGS)) { |
| 1888 | break; |
| 1889 | } |
| 1890 | } |
| 1891 | if (keyless_parent_list) { |
| 1892 | /* rehash */ |
| 1893 | for (; local_parent; local_parent = local_parent->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1894 | if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1895 | lyd_hash((struct lyd_node*)local_parent); |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | return first; |
| 1900 | |
| 1901 | error: |
| 1902 | if (top) { |
| 1903 | lyd_free_tree(top); |
| 1904 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1905 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1906 | } |
| 1907 | return NULL; |
| 1908 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1909 | |
| 1910 | static LY_ERR |
| 1911 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static) |
| 1912 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1913 | /* ending \0 */ |
| 1914 | ++reqlen; |
| 1915 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1916 | if (reqlen > *buflen) { |
| 1917 | if (is_static) { |
| 1918 | return LY_EINCOMPLETE; |
| 1919 | } |
| 1920 | |
| 1921 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 1922 | if (!*buffer) { |
| 1923 | return LY_EMEM; |
| 1924 | } |
| 1925 | |
| 1926 | *buflen = reqlen; |
| 1927 | } |
| 1928 | |
| 1929 | return LY_SUCCESS; |
| 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * @brief Append all list key predicates to path. |
| 1934 | * |
| 1935 | * @param[in] node Node with keys to print. |
| 1936 | * @param[in,out] buffer Buffer to print to. |
| 1937 | * @param[in,out] buflen Current buffer length. |
| 1938 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 1939 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 1940 | * @return LY_ERR |
| 1941 | */ |
| 1942 | static LY_ERR |
| 1943 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 1944 | { |
| 1945 | const struct lyd_node *key; |
| 1946 | int dynamic = 0; |
| 1947 | size_t len; |
| 1948 | const char *val; |
| 1949 | char quot; |
| 1950 | LY_ERR rc; |
| 1951 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1952 | for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1953 | val = lyd_value2str((struct lyd_node_term *)key, &dynamic); |
| 1954 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
| 1955 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 1956 | if (rc != LY_SUCCESS) { |
| 1957 | if (dynamic) { |
| 1958 | free((char *)val); |
| 1959 | } |
| 1960 | return rc; |
| 1961 | } |
| 1962 | |
| 1963 | quot = '\''; |
| 1964 | if (strchr(val, '\'')) { |
| 1965 | quot = '"'; |
| 1966 | } |
| 1967 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
| 1968 | |
| 1969 | if (dynamic) { |
| 1970 | free((char *)val); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | return LY_SUCCESS; |
| 1975 | } |
| 1976 | |
| 1977 | /** |
| 1978 | * @brief Append leaf-list value predicate to path. |
| 1979 | * |
| 1980 | * @param[in] node Node to print. |
| 1981 | * @param[in,out] buffer Buffer to print to. |
| 1982 | * @param[in,out] buflen Current buffer length. |
| 1983 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 1984 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 1985 | * @return LY_ERR |
| 1986 | */ |
| 1987 | static LY_ERR |
| 1988 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 1989 | { |
| 1990 | int dynamic = 0; |
| 1991 | size_t len; |
| 1992 | const char *val; |
| 1993 | char quot; |
| 1994 | LY_ERR rc; |
| 1995 | |
| 1996 | val = lyd_value2str((struct lyd_node_term *)node, &dynamic); |
| 1997 | len = 4 + strlen(val) + 2; |
| 1998 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 1999 | if (rc != LY_SUCCESS) { |
| 2000 | goto cleanup; |
| 2001 | } |
| 2002 | |
| 2003 | quot = '\''; |
| 2004 | if (strchr(val, '\'')) { |
| 2005 | quot = '"'; |
| 2006 | } |
| 2007 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2008 | |
| 2009 | cleanup: |
| 2010 | if (dynamic) { |
| 2011 | free((char *)val); |
| 2012 | } |
| 2013 | return rc; |
| 2014 | } |
| 2015 | |
| 2016 | /** |
| 2017 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2018 | * |
| 2019 | * @param[in] node Node to print. |
| 2020 | * @param[in,out] buffer Buffer to print to. |
| 2021 | * @param[in,out] buflen Current buffer length. |
| 2022 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2023 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2024 | * @return LY_ERR |
| 2025 | */ |
| 2026 | static LY_ERR |
| 2027 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 2028 | { |
| 2029 | const struct lyd_node *first, *iter; |
| 2030 | size_t len; |
| 2031 | int pos; |
| 2032 | char *val = NULL; |
| 2033 | LY_ERR rc; |
| 2034 | |
| 2035 | if (node->parent) { |
| 2036 | first = node->parent->child; |
| 2037 | } else { |
| 2038 | for (first = node; node->prev->next; node = node->prev); |
| 2039 | } |
| 2040 | pos = 1; |
| 2041 | for (iter = first; iter != node; iter = iter->next) { |
| 2042 | if (iter->schema == node->schema) { |
| 2043 | ++pos; |
| 2044 | } |
| 2045 | } |
| 2046 | if (asprintf(&val, "%d", pos) == -1) { |
| 2047 | return LY_EMEM; |
| 2048 | } |
| 2049 | |
| 2050 | len = 1 + strlen(val) + 1; |
| 2051 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2052 | if (rc != LY_SUCCESS) { |
| 2053 | goto cleanup; |
| 2054 | } |
| 2055 | |
| 2056 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2057 | |
| 2058 | cleanup: |
| 2059 | free(val); |
| 2060 | return rc; |
| 2061 | } |
| 2062 | |
| 2063 | API char * |
| 2064 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 2065 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2066 | int is_static = 0, i, depth; |
| 2067 | size_t bufused = 0, len; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2068 | const struct lyd_node *iter; |
| 2069 | const struct lys_module *mod; |
| 2070 | LY_ERR rc; |
| 2071 | |
| 2072 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 2073 | if (buffer) { |
| 2074 | LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL); |
| 2075 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2076 | } else { |
| 2077 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | switch (pathtype) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2081 | case LYD_PATH_LOG: |
| 2082 | depth = 1; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2083 | for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) { |
| 2084 | ++depth; |
| 2085 | } |
| 2086 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2087 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2088 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2089 | /* find the right node */ |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2090 | for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2091 | iter_print: |
| 2092 | /* print prefix and name */ |
| 2093 | mod = NULL; |
| 2094 | if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) { |
| 2095 | mod = iter->schema->module; |
| 2096 | } |
| 2097 | |
| 2098 | /* realloc string */ |
| 2099 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name); |
| 2100 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 2101 | if (rc != LY_SUCCESS) { |
| 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | /* print next node */ |
| 2106 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name); |
| 2107 | |
| 2108 | switch (iter->schema->nodetype) { |
| 2109 | case LYS_LIST: |
| 2110 | if (iter->schema->flags & LYS_KEYLESS) { |
| 2111 | /* print its position */ |
| 2112 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2113 | } else { |
| 2114 | /* print all list keys in predicates */ |
| 2115 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2116 | } |
| 2117 | break; |
| 2118 | case LYS_LEAFLIST: |
| 2119 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 2120 | /* print leaf-list value */ |
| 2121 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2122 | } else { |
| 2123 | /* print its position */ |
| 2124 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2125 | } |
| 2126 | break; |
| 2127 | default: |
| 2128 | /* nothing to print more */ |
| 2129 | rc = LY_SUCCESS; |
| 2130 | break; |
| 2131 | } |
| 2132 | if (rc != LY_SUCCESS) { |
| 2133 | break; |
| 2134 | } |
| 2135 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2136 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2137 | } |
| 2138 | break; |
| 2139 | } |
| 2140 | |
| 2141 | return buffer; |
| 2142 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2143 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2144 | LY_ERR |
| 2145 | lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value, |
| 2146 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2147 | { |
| 2148 | LY_ERR rc; |
| 2149 | const struct lyd_node *node = NULL; |
| 2150 | struct lyd_node_term *term; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2151 | struct ly_keys keys = {0}; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2152 | struct lyd_value val = {0}; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2153 | size_t i; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2154 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2155 | LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2156 | |
| 2157 | if (!first) { |
| 2158 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2159 | if (match) { |
| 2160 | *match = NULL; |
| 2161 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2162 | return LY_ENOTFOUND; |
| 2163 | } |
| 2164 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2165 | if (key_or_value && !val_len) { |
| 2166 | val_len = strlen(key_or_value); |
| 2167 | } |
| 2168 | |
| 2169 | if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2170 | /* store the value */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2171 | LY_CHECK_GOTO(rc = lyd_value_store(&val, schema, key_or_value, val_len, 0, lydjson_resolve_prefix, NULL, LYD_JSON), cleanup); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2172 | } else if (key_or_value && (schema->nodetype == LYS_LIST)) { |
| 2173 | /* parse keys into canonical values */ |
| 2174 | LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, &keys), cleanup); |
| 2175 | } |
| 2176 | |
| 2177 | /* find first matching value */ |
| 2178 | LY_LIST_FOR(first, node) { |
| 2179 | if (node->schema != schema) { |
| 2180 | continue; |
| 2181 | } |
| 2182 | |
| 2183 | if ((schema->nodetype == LYS_LIST) && keys.str) { |
| 2184 | /* compare all set keys */ |
| 2185 | for (i = 0; i < keys.key_count; ++i) { |
| 2186 | /* find key */ |
| 2187 | rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0, |
| 2188 | (struct lyd_node **)&term); |
| 2189 | if (rc == LY_ENOTFOUND) { |
| 2190 | /* all keys must always exist */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2191 | LOGINT_RET(schema->module->ctx); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2192 | } |
| 2193 | LY_CHECK_GOTO(rc, cleanup); |
| 2194 | |
| 2195 | /* compare values */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2196 | if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | if (i < keys.key_count) { |
| 2202 | /* not a match */ |
| 2203 | continue; |
| 2204 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2205 | } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2206 | term = (struct lyd_node_term *)node; |
| 2207 | |
| 2208 | /* compare values */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2209 | if (!term->value.realtype->plugin->compare(&term->value, &val)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2210 | /* not a match */ |
| 2211 | continue; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | /* all criteria passed */ |
| 2216 | break; |
| 2217 | } |
| 2218 | |
| 2219 | if (!node) { |
| 2220 | rc = LY_ENOTFOUND; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2221 | if (match) { |
| 2222 | *match = NULL; |
| 2223 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2224 | goto cleanup; |
| 2225 | } |
| 2226 | |
| 2227 | /* success */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2228 | if (match) { |
| 2229 | *match = (struct lyd_node *)node; |
| 2230 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2231 | rc = LY_SUCCESS; |
| 2232 | |
| 2233 | cleanup: |
| 2234 | ly_keys_clean(&keys); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2235 | if (val.realtype) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2236 | val.realtype->plugin->free(schema->module->ctx, &val); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2237 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2238 | return rc; |
| 2239 | } |
| 2240 | |
| 2241 | API LY_ERR |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2242 | lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len, |
| 2243 | const char *key_or_value, size_t val_len, struct lyd_node **match) |
| 2244 | { |
| 2245 | const struct lysc_node *schema; |
| 2246 | |
| 2247 | LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL); |
| 2248 | |
| 2249 | if (!first) { |
| 2250 | /* no data */ |
| 2251 | *match = NULL; |
| 2252 | return LY_ENOTFOUND; |
| 2253 | } |
| 2254 | |
| 2255 | /* find schema */ |
| 2256 | schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0); |
| 2257 | if (!schema) { |
| 2258 | LOGERR(module->ctx, LY_EINVAL, "Schema node not found."); |
| 2259 | return LY_EINVAL; |
| 2260 | } |
| 2261 | |
| 2262 | return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match); |
| 2263 | } |
| 2264 | |
| 2265 | API LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2266 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 2267 | { |
| 2268 | struct lyd_node **match_p; |
| 2269 | struct lyd_node_inner *parent; |
| 2270 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2271 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2272 | |
| 2273 | if (!siblings) { |
| 2274 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2275 | if (match) { |
| 2276 | *match = NULL; |
| 2277 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2278 | return LY_ENOTFOUND; |
| 2279 | } |
| 2280 | |
| 2281 | /* find first sibling */ |
| 2282 | if (siblings->parent) { |
| 2283 | siblings = siblings->parent->child; |
| 2284 | } else { |
| 2285 | while (siblings->prev->next) { |
| 2286 | siblings = siblings->prev; |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2291 | if (parent && parent->children_ht) { |
| 2292 | assert(target->hash); |
| 2293 | |
| 2294 | /* find by hash */ |
| 2295 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2296 | siblings = *match_p; |
| 2297 | } else { |
| 2298 | /* not found */ |
| 2299 | siblings = NULL; |
| 2300 | } |
| 2301 | } else { |
| 2302 | /* no children hash table */ |
| 2303 | for (; siblings; siblings = siblings->next) { |
| 2304 | if (!lyd_compare(siblings, target, 0)) { |
| 2305 | break; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2311 | if (match) { |
| 2312 | *match = NULL; |
| 2313 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2314 | return LY_ENOTFOUND; |
| 2315 | } |
| 2316 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2317 | if (match) { |
| 2318 | *match = (struct lyd_node *)siblings; |
| 2319 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2320 | return LY_SUCCESS; |
| 2321 | } |
| 2322 | |
| 2323 | API LY_ERR |
| 2324 | lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 2325 | { |
| 2326 | struct lyd_node_inner *parent; |
| 2327 | struct lyd_node *match; |
| 2328 | struct lyd_node **match_p; |
| 2329 | struct ly_set *ret; |
| 2330 | |
| 2331 | LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL); |
| 2332 | |
| 2333 | if (!siblings) { |
| 2334 | /* no data */ |
| 2335 | return LY_ENOTFOUND; |
| 2336 | } |
| 2337 | |
| 2338 | ret = ly_set_new(); |
| 2339 | LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM); |
| 2340 | |
| 2341 | /* find first sibling */ |
| 2342 | if (siblings->parent) { |
| 2343 | siblings = siblings->parent->child; |
| 2344 | } else { |
| 2345 | while (siblings->prev->next) { |
| 2346 | siblings = siblings->prev; |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2351 | if (parent && parent->children_ht) { |
| 2352 | assert(target->hash); |
| 2353 | |
| 2354 | /* find by hash */ |
| 2355 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2356 | match = *match_p; |
| 2357 | } else { |
| 2358 | /* not found */ |
| 2359 | match = NULL; |
| 2360 | } |
| 2361 | while (match) { |
| 2362 | /* add all found nodes into the return set */ |
| 2363 | if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) { |
| 2364 | goto error; |
| 2365 | } |
| 2366 | |
| 2367 | /* find next instance */ |
| 2368 | if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) { |
| 2369 | match = NULL; |
| 2370 | } else { |
| 2371 | match = *match_p; |
| 2372 | } |
| 2373 | } |
| 2374 | } else { |
| 2375 | /* no children hash table */ |
| 2376 | for (; siblings; siblings = siblings->next) { |
| 2377 | if (!lyd_compare(siblings, target, 0)) { |
| 2378 | /* a match */ |
| 2379 | if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) { |
| 2380 | goto error; |
| 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | if (!ret->count) { |
| 2387 | ly_set_free(ret, NULL); |
| 2388 | return LY_ENOTFOUND; |
| 2389 | } |
| 2390 | |
| 2391 | *set = ret; |
| 2392 | return LY_SUCCESS; |
| 2393 | |
| 2394 | error: |
| 2395 | ly_set_free(ret, NULL); |
| 2396 | return LY_EMEM; |
| 2397 | } |
| 2398 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2399 | static int |
| 2400 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 2401 | { |
| 2402 | struct lysc_node *val1; |
| 2403 | struct lyd_node *val2; |
| 2404 | |
| 2405 | val1 = *((struct lysc_node **)val1_p); |
| 2406 | val2 = *((struct lyd_node **)val2_p); |
| 2407 | |
| 2408 | assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF)); |
| 2409 | |
| 2410 | if (val1 == val2->schema) { |
| 2411 | /* schema match is enough */ |
| 2412 | return 1; |
| 2413 | } else { |
| 2414 | return 0; |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | static LY_ERR |
| 2419 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 2420 | { |
| 2421 | struct lyd_node **match_p; |
| 2422 | struct lyd_node_inner *parent; |
| 2423 | uint32_t hash; |
| 2424 | values_equal_cb ht_cb; |
| 2425 | |
| 2426 | assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF))); |
| 2427 | |
| 2428 | /* find first sibling */ |
| 2429 | if (siblings->parent) { |
| 2430 | siblings = siblings->parent->child; |
| 2431 | } else { |
| 2432 | while (siblings->prev->next) { |
| 2433 | siblings = siblings->prev; |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2438 | if (parent && parent->children_ht) { |
| 2439 | /* calculate our hash */ |
| 2440 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 2441 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 2442 | hash = dict_hash_multi(hash, NULL, 0); |
| 2443 | |
| 2444 | /* use special hash table function */ |
| 2445 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 2446 | |
| 2447 | /* find by hash */ |
| 2448 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 2449 | siblings = *match_p; |
| 2450 | } else { |
| 2451 | /* not found */ |
| 2452 | siblings = NULL; |
| 2453 | } |
| 2454 | |
| 2455 | /* set the original hash table compare function back */ |
| 2456 | lyht_set_cb(parent->children_ht, ht_cb); |
| 2457 | } else { |
| 2458 | /* no children hash table */ |
| 2459 | for (; siblings; siblings = siblings->next) { |
| 2460 | if (siblings->schema == schema) { |
| 2461 | /* schema match is enough */ |
| 2462 | break; |
| 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2468 | if (match) { |
| 2469 | *match = NULL; |
| 2470 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2471 | return LY_ENOTFOUND; |
| 2472 | } |
| 2473 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2474 | if (match) { |
| 2475 | *match = (struct lyd_node *)siblings; |
| 2476 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2477 | return LY_SUCCESS; |
| 2478 | } |
| 2479 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2480 | API LY_ERR |
| 2481 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
| 2482 | size_t val_len, struct lyd_node **match) |
| 2483 | { |
| 2484 | LY_ERR rc; |
| 2485 | struct lyd_node *target = NULL; |
| 2486 | |
| 2487 | LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL); |
| 2488 | if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) { |
| 2489 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__); |
| 2490 | return LY_EINVAL; |
| 2491 | } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) { |
| 2492 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__); |
| 2493 | return LY_EINVAL; |
| 2494 | } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 2495 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).", |
| 2496 | lys_nodetype2str(schema->nodetype), __func__); |
| 2497 | return LY_EINVAL; |
| 2498 | } |
| 2499 | |
| 2500 | if (!siblings) { |
| 2501 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2502 | if (match) { |
| 2503 | *match = NULL; |
| 2504 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2505 | return LY_ENOTFOUND; |
| 2506 | } |
| 2507 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2508 | if (key_or_value && !val_len) { |
| 2509 | val_len = strlen(key_or_value); |
| 2510 | } |
| 2511 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2512 | /* create data node if needed and find it */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2513 | switch (schema->nodetype) { |
| 2514 | case LYS_CONTAINER: |
| 2515 | case LYS_ANYXML: |
| 2516 | case LYS_ANYDATA: |
| 2517 | case LYS_NOTIF: |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2518 | case LYS_ACTION: |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2519 | case LYS_LEAF: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2520 | /* find it based on schema only */ |
| 2521 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2522 | break; |
| 2523 | case LYS_LEAFLIST: |
| 2524 | /* target used attributes: schema, hash, value */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2525 | LY_CHECK_RET(lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2526 | /* fallthrough */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2527 | case LYS_LIST: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2528 | if (schema->nodetype == LYS_LIST) { |
| 2529 | /* target used attributes: schema, hash, child (all keys) */ |
| 2530 | LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | /* find it */ |
| 2534 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2535 | break; |
| 2536 | default: |
| 2537 | /* unreachable */ |
| 2538 | LOGINT(schema->module->ctx); |
| 2539 | return LY_EINT; |
| 2540 | } |
| 2541 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2542 | lyd_free_tree(target); |
| 2543 | return rc; |
| 2544 | } |