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