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); |
| 1801 | LY_CHECK_GOTO(!any->value.tree, error); |
| 1802 | } |
| 1803 | break; |
| 1804 | case LYD_ANYDATA_STRING: |
| 1805 | case LYD_ANYDATA_XML: |
| 1806 | case LYD_ANYDATA_JSON: |
| 1807 | if (orig->value.str) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1808 | 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] | 1809 | } |
| 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1814 | /* insert */ |
| 1815 | lyd_insert_node(parent, first, dup); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1816 | lyd_insert_hash(dup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1817 | |
| 1818 | if (dup_p) { |
| 1819 | *dup_p = dup; |
| 1820 | } |
| 1821 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1822 | |
| 1823 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1824 | lyd_free_tree(dup); |
| 1825 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | API struct lyd_node * |
| 1829 | lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options) |
| 1830 | { |
| 1831 | struct ly_ctx *ctx; |
| 1832 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 1833 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1834 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 1835 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
| 1836 | int keyless_parent_list = 0; |
| 1837 | |
| 1838 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 1839 | ctx = node->schema->module->ctx; |
| 1840 | |
| 1841 | if (options & LYD_DUP_WITH_PARENTS) { |
| 1842 | struct lyd_node_inner *orig_parent, *iter; |
| 1843 | int repeat = 1; |
| 1844 | for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 1845 | if (parent && parent->schema == orig_parent->schema) { |
| 1846 | /* stop creating parents, connect what we have into the provided parent */ |
| 1847 | iter = parent; |
| 1848 | repeat = 0; |
| 1849 | /* get know if there is a keyless list which we will have to rehash */ |
| 1850 | for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1851 | if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1852 | keyless_parent_list = 1; |
| 1853 | break; |
| 1854 | } |
| 1855 | } |
| 1856 | } else { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1857 | iter = NULL; |
| 1858 | LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0, |
| 1859 | (struct lyd_node **)&iter), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1860 | } |
| 1861 | if (!local_parent) { |
| 1862 | local_parent = iter; |
| 1863 | } |
| 1864 | if (iter->child) { |
| 1865 | /* 1) list - add after keys |
| 1866 | * 2) provided parent with some children */ |
| 1867 | iter->child->prev->next = top; |
| 1868 | if (top) { |
| 1869 | top->prev = iter->child->prev; |
| 1870 | iter->child->prev = top; |
| 1871 | } |
| 1872 | } else { |
| 1873 | iter->child = top; |
| 1874 | if (iter->schema->nodetype == LYS_LIST) { |
| 1875 | /* keyless list - we will need to rehash it since we are going to add nodes into it */ |
| 1876 | keyless_parent_list = 1; |
| 1877 | } |
| 1878 | } |
| 1879 | if (top) { |
| 1880 | top->parent = iter; |
| 1881 | } |
| 1882 | top = (struct lyd_node*)iter; |
| 1883 | } |
| 1884 | if (repeat && parent) { |
| 1885 | /* given parent and created parents chain actually do not interconnect */ |
| 1886 | LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
| 1887 | goto error; |
| 1888 | } |
| 1889 | } else { |
| 1890 | local_parent = parent; |
| 1891 | } |
| 1892 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1893 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1894 | /* if there is no local parent, it will be inserted into first */ |
| 1895 | 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] | 1896 | if (!(options & LYD_DUP_WITH_SIBLINGS)) { |
| 1897 | break; |
| 1898 | } |
| 1899 | } |
| 1900 | if (keyless_parent_list) { |
| 1901 | /* rehash */ |
| 1902 | for (; local_parent; local_parent = local_parent->parent) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 1903 | 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] | 1904 | lyd_hash((struct lyd_node*)local_parent); |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | return first; |
| 1909 | |
| 1910 | error: |
| 1911 | if (top) { |
| 1912 | lyd_free_tree(top); |
| 1913 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1914 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1915 | } |
| 1916 | return NULL; |
| 1917 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1918 | |
| 1919 | static LY_ERR |
| 1920 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static) |
| 1921 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1922 | /* ending \0 */ |
| 1923 | ++reqlen; |
| 1924 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1925 | if (reqlen > *buflen) { |
| 1926 | if (is_static) { |
| 1927 | return LY_EINCOMPLETE; |
| 1928 | } |
| 1929 | |
| 1930 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 1931 | if (!*buffer) { |
| 1932 | return LY_EMEM; |
| 1933 | } |
| 1934 | |
| 1935 | *buflen = reqlen; |
| 1936 | } |
| 1937 | |
| 1938 | return LY_SUCCESS; |
| 1939 | } |
| 1940 | |
| 1941 | /** |
| 1942 | * @brief Append all list key predicates to path. |
| 1943 | * |
| 1944 | * @param[in] node Node with keys to print. |
| 1945 | * @param[in,out] buffer Buffer to print to. |
| 1946 | * @param[in,out] buflen Current buffer length. |
| 1947 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 1948 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 1949 | * @return LY_ERR |
| 1950 | */ |
| 1951 | static LY_ERR |
| 1952 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 1953 | { |
| 1954 | const struct lyd_node *key; |
| 1955 | int dynamic = 0; |
| 1956 | size_t len; |
| 1957 | const char *val; |
| 1958 | char quot; |
| 1959 | LY_ERR rc; |
| 1960 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1961 | 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] | 1962 | val = lyd_value2str((struct lyd_node_term *)key, &dynamic); |
| 1963 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
| 1964 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 1965 | if (rc != LY_SUCCESS) { |
| 1966 | if (dynamic) { |
| 1967 | free((char *)val); |
| 1968 | } |
| 1969 | return rc; |
| 1970 | } |
| 1971 | |
| 1972 | quot = '\''; |
| 1973 | if (strchr(val, '\'')) { |
| 1974 | quot = '"'; |
| 1975 | } |
| 1976 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
| 1977 | |
| 1978 | if (dynamic) { |
| 1979 | free((char *)val); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | return LY_SUCCESS; |
| 1984 | } |
| 1985 | |
| 1986 | /** |
| 1987 | * @brief Append leaf-list value predicate to path. |
| 1988 | * |
| 1989 | * @param[in] node Node to print. |
| 1990 | * @param[in,out] buffer Buffer to print to. |
| 1991 | * @param[in,out] buflen Current buffer length. |
| 1992 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 1993 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 1994 | * @return LY_ERR |
| 1995 | */ |
| 1996 | static LY_ERR |
| 1997 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 1998 | { |
| 1999 | int dynamic = 0; |
| 2000 | size_t len; |
| 2001 | const char *val; |
| 2002 | char quot; |
| 2003 | LY_ERR rc; |
| 2004 | |
| 2005 | val = lyd_value2str((struct lyd_node_term *)node, &dynamic); |
| 2006 | len = 4 + strlen(val) + 2; |
| 2007 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2008 | if (rc != LY_SUCCESS) { |
| 2009 | goto cleanup; |
| 2010 | } |
| 2011 | |
| 2012 | quot = '\''; |
| 2013 | if (strchr(val, '\'')) { |
| 2014 | quot = '"'; |
| 2015 | } |
| 2016 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2017 | |
| 2018 | cleanup: |
| 2019 | if (dynamic) { |
| 2020 | free((char *)val); |
| 2021 | } |
| 2022 | return rc; |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2027 | * |
| 2028 | * @param[in] node Node to print. |
| 2029 | * @param[in,out] buffer Buffer to print to. |
| 2030 | * @param[in,out] buflen Current buffer length. |
| 2031 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2032 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2033 | * @return LY_ERR |
| 2034 | */ |
| 2035 | static LY_ERR |
| 2036 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static) |
| 2037 | { |
| 2038 | const struct lyd_node *first, *iter; |
| 2039 | size_t len; |
| 2040 | int pos; |
| 2041 | char *val = NULL; |
| 2042 | LY_ERR rc; |
| 2043 | |
| 2044 | if (node->parent) { |
| 2045 | first = node->parent->child; |
| 2046 | } else { |
| 2047 | for (first = node; node->prev->next; node = node->prev); |
| 2048 | } |
| 2049 | pos = 1; |
| 2050 | for (iter = first; iter != node; iter = iter->next) { |
| 2051 | if (iter->schema == node->schema) { |
| 2052 | ++pos; |
| 2053 | } |
| 2054 | } |
| 2055 | if (asprintf(&val, "%d", pos) == -1) { |
| 2056 | return LY_EMEM; |
| 2057 | } |
| 2058 | |
| 2059 | len = 1 + strlen(val) + 1; |
| 2060 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2061 | if (rc != LY_SUCCESS) { |
| 2062 | goto cleanup; |
| 2063 | } |
| 2064 | |
| 2065 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2066 | |
| 2067 | cleanup: |
| 2068 | free(val); |
| 2069 | return rc; |
| 2070 | } |
| 2071 | |
| 2072 | API char * |
| 2073 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 2074 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2075 | int is_static = 0, i, depth; |
| 2076 | size_t bufused = 0, len; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2077 | const struct lyd_node *iter; |
| 2078 | const struct lys_module *mod; |
| 2079 | LY_ERR rc; |
| 2080 | |
| 2081 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 2082 | if (buffer) { |
| 2083 | LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL); |
| 2084 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2085 | } else { |
| 2086 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | switch (pathtype) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2090 | case LYD_PATH_LOG: |
| 2091 | depth = 1; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2092 | for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) { |
| 2093 | ++depth; |
| 2094 | } |
| 2095 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2096 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2097 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2098 | /* find the right node */ |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2099 | 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] | 2100 | iter_print: |
| 2101 | /* print prefix and name */ |
| 2102 | mod = NULL; |
| 2103 | if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) { |
| 2104 | mod = iter->schema->module; |
| 2105 | } |
| 2106 | |
| 2107 | /* realloc string */ |
| 2108 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name); |
| 2109 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 2110 | if (rc != LY_SUCCESS) { |
| 2111 | break; |
| 2112 | } |
| 2113 | |
| 2114 | /* print next node */ |
| 2115 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name); |
| 2116 | |
| 2117 | switch (iter->schema->nodetype) { |
| 2118 | case LYS_LIST: |
| 2119 | if (iter->schema->flags & LYS_KEYLESS) { |
| 2120 | /* print its position */ |
| 2121 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2122 | } else { |
| 2123 | /* print all list keys in predicates */ |
| 2124 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2125 | } |
| 2126 | break; |
| 2127 | case LYS_LEAFLIST: |
| 2128 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 2129 | /* print leaf-list value */ |
| 2130 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2131 | } else { |
| 2132 | /* print its position */ |
| 2133 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2134 | } |
| 2135 | break; |
| 2136 | default: |
| 2137 | /* nothing to print more */ |
| 2138 | rc = LY_SUCCESS; |
| 2139 | break; |
| 2140 | } |
| 2141 | if (rc != LY_SUCCESS) { |
| 2142 | break; |
| 2143 | } |
| 2144 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2145 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2146 | } |
| 2147 | break; |
| 2148 | } |
| 2149 | |
| 2150 | return buffer; |
| 2151 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2152 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2153 | LY_ERR |
| 2154 | lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value, |
| 2155 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2156 | { |
| 2157 | LY_ERR rc; |
| 2158 | const struct lyd_node *node = NULL; |
| 2159 | struct lyd_node_term *term; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2160 | struct ly_keys keys = {0}; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2161 | struct lyd_value val = {0}; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2162 | size_t i; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2163 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2164 | LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2165 | |
| 2166 | if (!first) { |
| 2167 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2168 | if (match) { |
| 2169 | *match = NULL; |
| 2170 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2171 | return LY_ENOTFOUND; |
| 2172 | } |
| 2173 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2174 | if (key_or_value && !val_len) { |
| 2175 | val_len = strlen(key_or_value); |
| 2176 | } |
| 2177 | |
| 2178 | if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2179 | /* store the value */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2180 | 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] | 2181 | } else if (key_or_value && (schema->nodetype == LYS_LIST)) { |
| 2182 | /* parse keys into canonical values */ |
| 2183 | LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, &keys), cleanup); |
| 2184 | } |
| 2185 | |
| 2186 | /* find first matching value */ |
| 2187 | LY_LIST_FOR(first, node) { |
| 2188 | if (node->schema != schema) { |
| 2189 | continue; |
| 2190 | } |
| 2191 | |
| 2192 | if ((schema->nodetype == LYS_LIST) && keys.str) { |
| 2193 | /* compare all set keys */ |
| 2194 | for (i = 0; i < keys.key_count; ++i) { |
| 2195 | /* find key */ |
| 2196 | rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0, |
| 2197 | (struct lyd_node **)&term); |
| 2198 | if (rc == LY_ENOTFOUND) { |
| 2199 | /* all keys must always exist */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2200 | LOGINT_RET(schema->module->ctx); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2201 | } |
| 2202 | LY_CHECK_GOTO(rc, cleanup); |
| 2203 | |
| 2204 | /* compare values */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2205 | if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2206 | break; |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | if (i < keys.key_count) { |
| 2211 | /* not a match */ |
| 2212 | continue; |
| 2213 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2214 | } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2215 | term = (struct lyd_node_term *)node; |
| 2216 | |
| 2217 | /* compare values */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2218 | if (!term->value.realtype->plugin->compare(&term->value, &val)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2219 | /* not a match */ |
| 2220 | continue; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | /* all criteria passed */ |
| 2225 | break; |
| 2226 | } |
| 2227 | |
| 2228 | if (!node) { |
| 2229 | rc = LY_ENOTFOUND; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2230 | if (match) { |
| 2231 | *match = NULL; |
| 2232 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2233 | goto cleanup; |
| 2234 | } |
| 2235 | |
| 2236 | /* success */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2237 | if (match) { |
| 2238 | *match = (struct lyd_node *)node; |
| 2239 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2240 | rc = LY_SUCCESS; |
| 2241 | |
| 2242 | cleanup: |
| 2243 | ly_keys_clean(&keys); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2244 | if (val.realtype) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2245 | val.realtype->plugin->free(schema->module->ctx, &val); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2246 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2247 | return rc; |
| 2248 | } |
| 2249 | |
| 2250 | API LY_ERR |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2251 | lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len, |
| 2252 | const char *key_or_value, size_t val_len, struct lyd_node **match) |
| 2253 | { |
| 2254 | const struct lysc_node *schema; |
| 2255 | |
| 2256 | LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL); |
| 2257 | |
| 2258 | if (!first) { |
| 2259 | /* no data */ |
| 2260 | *match = NULL; |
| 2261 | return LY_ENOTFOUND; |
| 2262 | } |
| 2263 | |
| 2264 | /* find schema */ |
| 2265 | schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0); |
| 2266 | if (!schema) { |
| 2267 | LOGERR(module->ctx, LY_EINVAL, "Schema node not found."); |
| 2268 | return LY_EINVAL; |
| 2269 | } |
| 2270 | |
| 2271 | return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match); |
| 2272 | } |
| 2273 | |
| 2274 | API LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2275 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 2276 | { |
| 2277 | struct lyd_node **match_p; |
| 2278 | struct lyd_node_inner *parent; |
| 2279 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2280 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2281 | |
| 2282 | if (!siblings) { |
| 2283 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2284 | if (match) { |
| 2285 | *match = NULL; |
| 2286 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2287 | return LY_ENOTFOUND; |
| 2288 | } |
| 2289 | |
| 2290 | /* find first sibling */ |
| 2291 | if (siblings->parent) { |
| 2292 | siblings = siblings->parent->child; |
| 2293 | } else { |
| 2294 | while (siblings->prev->next) { |
| 2295 | siblings = siblings->prev; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2300 | if (parent && parent->children_ht) { |
| 2301 | assert(target->hash); |
| 2302 | |
| 2303 | /* find by hash */ |
| 2304 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2305 | siblings = *match_p; |
| 2306 | } else { |
| 2307 | /* not found */ |
| 2308 | siblings = NULL; |
| 2309 | } |
| 2310 | } else { |
| 2311 | /* no children hash table */ |
| 2312 | for (; siblings; siblings = siblings->next) { |
| 2313 | if (!lyd_compare(siblings, target, 0)) { |
| 2314 | break; |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2320 | if (match) { |
| 2321 | *match = NULL; |
| 2322 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2323 | return LY_ENOTFOUND; |
| 2324 | } |
| 2325 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2326 | if (match) { |
| 2327 | *match = (struct lyd_node *)siblings; |
| 2328 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2329 | return LY_SUCCESS; |
| 2330 | } |
| 2331 | |
| 2332 | API LY_ERR |
| 2333 | lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 2334 | { |
| 2335 | struct lyd_node_inner *parent; |
| 2336 | struct lyd_node *match; |
| 2337 | struct lyd_node **match_p; |
| 2338 | struct ly_set *ret; |
| 2339 | |
| 2340 | LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL); |
| 2341 | |
| 2342 | if (!siblings) { |
| 2343 | /* no data */ |
| 2344 | return LY_ENOTFOUND; |
| 2345 | } |
| 2346 | |
| 2347 | ret = ly_set_new(); |
| 2348 | LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM); |
| 2349 | |
| 2350 | /* find first sibling */ |
| 2351 | if (siblings->parent) { |
| 2352 | siblings = siblings->parent->child; |
| 2353 | } else { |
| 2354 | while (siblings->prev->next) { |
| 2355 | siblings = siblings->prev; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2360 | if (parent && parent->children_ht) { |
| 2361 | assert(target->hash); |
| 2362 | |
| 2363 | /* find by hash */ |
| 2364 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2365 | match = *match_p; |
| 2366 | } else { |
| 2367 | /* not found */ |
| 2368 | match = NULL; |
| 2369 | } |
| 2370 | while (match) { |
| 2371 | /* add all found nodes into the return set */ |
| 2372 | if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) { |
| 2373 | goto error; |
| 2374 | } |
| 2375 | |
| 2376 | /* find next instance */ |
| 2377 | if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) { |
| 2378 | match = NULL; |
| 2379 | } else { |
| 2380 | match = *match_p; |
| 2381 | } |
| 2382 | } |
| 2383 | } else { |
| 2384 | /* no children hash table */ |
| 2385 | for (; siblings; siblings = siblings->next) { |
| 2386 | if (!lyd_compare(siblings, target, 0)) { |
| 2387 | /* a match */ |
| 2388 | if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) { |
| 2389 | goto error; |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | if (!ret->count) { |
| 2396 | ly_set_free(ret, NULL); |
| 2397 | return LY_ENOTFOUND; |
| 2398 | } |
| 2399 | |
| 2400 | *set = ret; |
| 2401 | return LY_SUCCESS; |
| 2402 | |
| 2403 | error: |
| 2404 | ly_set_free(ret, NULL); |
| 2405 | return LY_EMEM; |
| 2406 | } |
| 2407 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2408 | static int |
| 2409 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 2410 | { |
| 2411 | struct lysc_node *val1; |
| 2412 | struct lyd_node *val2; |
| 2413 | |
| 2414 | val1 = *((struct lysc_node **)val1_p); |
| 2415 | val2 = *((struct lyd_node **)val2_p); |
| 2416 | |
| 2417 | assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF)); |
| 2418 | |
| 2419 | if (val1 == val2->schema) { |
| 2420 | /* schema match is enough */ |
| 2421 | return 1; |
| 2422 | } else { |
| 2423 | return 0; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | static LY_ERR |
| 2428 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 2429 | { |
| 2430 | struct lyd_node **match_p; |
| 2431 | struct lyd_node_inner *parent; |
| 2432 | uint32_t hash; |
| 2433 | values_equal_cb ht_cb; |
| 2434 | |
| 2435 | assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF))); |
| 2436 | |
| 2437 | /* find first sibling */ |
| 2438 | if (siblings->parent) { |
| 2439 | siblings = siblings->parent->child; |
| 2440 | } else { |
| 2441 | while (siblings->prev->next) { |
| 2442 | siblings = siblings->prev; |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | parent = (struct lyd_node_inner *)siblings->parent; |
| 2447 | if (parent && parent->children_ht) { |
| 2448 | /* calculate our hash */ |
| 2449 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 2450 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 2451 | hash = dict_hash_multi(hash, NULL, 0); |
| 2452 | |
| 2453 | /* use special hash table function */ |
| 2454 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 2455 | |
| 2456 | /* find by hash */ |
| 2457 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 2458 | siblings = *match_p; |
| 2459 | } else { |
| 2460 | /* not found */ |
| 2461 | siblings = NULL; |
| 2462 | } |
| 2463 | |
| 2464 | /* set the original hash table compare function back */ |
| 2465 | lyht_set_cb(parent->children_ht, ht_cb); |
| 2466 | } else { |
| 2467 | /* no children hash table */ |
| 2468 | for (; siblings; siblings = siblings->next) { |
| 2469 | if (siblings->schema == schema) { |
| 2470 | /* schema match is enough */ |
| 2471 | break; |
| 2472 | } |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2477 | if (match) { |
| 2478 | *match = NULL; |
| 2479 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2480 | return LY_ENOTFOUND; |
| 2481 | } |
| 2482 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2483 | if (match) { |
| 2484 | *match = (struct lyd_node *)siblings; |
| 2485 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2486 | return LY_SUCCESS; |
| 2487 | } |
| 2488 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2489 | API LY_ERR |
| 2490 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
| 2491 | size_t val_len, struct lyd_node **match) |
| 2492 | { |
| 2493 | LY_ERR rc; |
| 2494 | struct lyd_node *target = NULL; |
| 2495 | |
| 2496 | LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL); |
| 2497 | if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) { |
| 2498 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__); |
| 2499 | return LY_EINVAL; |
| 2500 | } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) { |
| 2501 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__); |
| 2502 | return LY_EINVAL; |
| 2503 | } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 2504 | LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).", |
| 2505 | lys_nodetype2str(schema->nodetype), __func__); |
| 2506 | return LY_EINVAL; |
| 2507 | } |
| 2508 | |
| 2509 | if (!siblings) { |
| 2510 | /* no data */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2511 | if (match) { |
| 2512 | *match = NULL; |
| 2513 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2514 | return LY_ENOTFOUND; |
| 2515 | } |
| 2516 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2517 | if (key_or_value && !val_len) { |
| 2518 | val_len = strlen(key_or_value); |
| 2519 | } |
| 2520 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2521 | /* create data node if needed and find it */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2522 | switch (schema->nodetype) { |
| 2523 | case LYS_CONTAINER: |
| 2524 | case LYS_ANYXML: |
| 2525 | case LYS_ANYDATA: |
| 2526 | case LYS_NOTIF: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2527 | case LYS_RPC: |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2528 | case LYS_ACTION: |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2529 | case LYS_LEAF: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2530 | /* find it based on schema only */ |
| 2531 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2532 | break; |
| 2533 | case LYS_LEAFLIST: |
| 2534 | /* target used attributes: schema, hash, value */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2535 | 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] | 2536 | /* fallthrough */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2537 | case LYS_LIST: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2538 | if (schema->nodetype == LYS_LIST) { |
| 2539 | /* target used attributes: schema, hash, child (all keys) */ |
| 2540 | 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] | 2541 | } |
| 2542 | |
| 2543 | /* find it */ |
| 2544 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2545 | break; |
| 2546 | default: |
| 2547 | /* unreachable */ |
| 2548 | LOGINT(schema->module->ctx); |
| 2549 | return LY_EINT; |
| 2550 | } |
| 2551 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2552 | lyd_free_tree(target); |
| 2553 | return rc; |
| 2554 | } |