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