blob: d79723975bd849ffee246612e93cd5a90105662c [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vaskod86997b2020-05-26 15:19:54 +020014#define _POSIX_C_SOURCE 200809L /* strndup */
Radek Krejcie7b95092019-05-15 11:03:07 +020015
16#include "common.h"
17
Radek Krejci084289f2019-07-09 17:35:30 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <stdarg.h>
23#include <stdint.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "log.h"
28#include "tree.h"
29#include "tree_data.h"
30#include "tree_data_internal.h"
Michal Vaskod3678892020-05-21 10:06:58 +020031#include "tree_schema_internal.h"
Michal Vasko90932a92020-02-12 14:33:03 +010032#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "tree_schema.h"
Michal Vaskod3678892020-05-21 10:06:58 +020034#include "xpath.h"
Michal Vasko52927e22020-03-16 17:26:14 +010035#include "xml.h"
Radek Krejci38d85362019-09-05 16:26:38 +020036#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010037#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038
Michal Vaskoe444f752020-02-10 12:20:06 +010039struct ly_keys {
40 char *str;
41 struct {
42 const struct lysc_node_leaf *schema;
43 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010044 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010045 } *keys;
46 size_t key_count;
47};
48
Radek Krejci084289f2019-07-09 17:35:30 +020049LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010050lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010051 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020052{
Michal Vasko90932a92020-02-12 14:33:03 +010053 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020054 struct ly_err_item *err = NULL;
55 struct ly_ctx *ctx;
56 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020057 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010058 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020059 assert(node);
60
61 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020062
Radek Krejci73dead22019-07-11 16:46:16 +020063 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020064 if (!second) {
65 node->value.realtype = type;
66 }
Michal Vasko90932a92020-02-12 14:33:03 +010067 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vaskof03ed032020-03-04 13:31:44 +010068 tree ? (void *)node : (void *)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +020069 &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010070 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020071 if (err) {
Michal Vaskofea12c62020-03-30 11:00:15 +020072 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
Radek Krejci73dead22019-07-11 16:46:16 +020073 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020074 }
Radek Krejci73dead22019-07-11 16:46:16 +020075 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010076 } else if (dynamic) {
77 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020078 }
79
80error:
81 return ret;
82}
83
Michal Vasko90932a92020-02-12 14:33:03 +010084/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
85static LY_ERR
86lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
87 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
88{
89 LY_ERR ret = LY_SUCCESS;
90 struct ly_err_item *err = NULL;
91 struct ly_ctx *ctx;
92 struct lysc_type *type;
93 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
94
95 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
96
97 ctx = schema->module->ctx;
98 type = ((struct lysc_node_leaf *)schema)->type;
99 val->realtype = type;
100 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
101 val, NULL, &err);
102 if (ret == LY_EINCOMPLETE) {
103 /* this is fine, we do not need it resolved */
104 ret = LY_SUCCESS;
105 } else if (ret && err) {
106 ly_err_print(err);
107 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
108 ly_err_free(err);
109 }
110 if (!ret && dynamic) {
111 *dynamic = 0;
112 }
113
114 return ret;
115}
116
Radek Krejci38d85362019-09-05 16:26:38 +0200117LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100118lyd_value_parse_meta(struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100119 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100120 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200121{
Michal Vasko90932a92020-02-12 14:33:03 +0100122 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200123 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200124 struct lyext_metadata *ant;
125 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100126 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200127
Michal Vasko9f96a052020-03-10 09:41:45 +0100128 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100129
Michal Vasko9f96a052020-03-10 09:41:45 +0100130 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200131
132 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100133 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200134 }
Michal Vasko90932a92020-02-12 14:33:03 +0100135 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100136 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100137 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200138 if (err) {
139 ly_err_print(err);
140 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
141 ly_err_free(err);
142 }
143 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100144 } else if (dynamic) {
145 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200146 }
147
148error:
149 return ret;
150}
151
Radek Krejci084289f2019-07-09 17:35:30 +0200152API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100153lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci084289f2019-07-09 17:35:30 +0200154 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
155{
156 LY_ERR rc = LY_SUCCESS;
157 struct ly_err_item *err = NULL;
158 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200159
160 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
161
162 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
163 LOGARG(ctx, node);
164 return LY_EINVAL;
165 }
166
167 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200168 /* just validate, no storing of enything */
169 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
170 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
171 if (rc == LY_EINCOMPLETE) {
172 /* actually success since we do not provide the context tree and call validation with
173 * LY_TYPE_OPTS_INCOMPLETE_DATA */
174 rc = LY_SUCCESS;
175 } else if (rc && err) {
176 if (ctx) {
177 /* log only in case the ctx was provided as input parameter */
178 ly_err_print(err);
179 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200180 }
Radek Krejci73dead22019-07-11 16:46:16 +0200181 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200182 }
183
184 return rc;
185}
186
187API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100188lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100189 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200190{
191 LY_ERR rc;
192 struct ly_err_item *err = NULL;
193 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100194 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200195
196 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
197
198 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200199 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100200 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200201 NULL, NULL, &err);
202 if (rc == LY_EINCOMPLETE) {
203 return rc;
204 } else if (rc) {
205 if (err) {
206 if (ctx) {
207 ly_err_print(err);
208 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200209 }
Radek Krejci73dead22019-07-11 16:46:16 +0200210 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200211 }
Radek Krejci73dead22019-07-11 16:46:16 +0200212 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200213 }
214
215 return LY_SUCCESS;
216}
217
218API LY_ERR
219lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100220 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200221{
222 LY_ERR ret = LY_SUCCESS, rc;
223 struct ly_err_item *err = NULL;
224 struct ly_ctx *ctx;
225 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200226 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100227 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200228
229 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
230
231 ctx = node->schema->module->ctx;
232 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200233 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
Michal Vaskof03ed032020-03-04 13:31:44 +0100234 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200235 if (rc == LY_EINCOMPLETE) {
236 ret = rc;
237 /* continue with comparing, just remember what to return if storing is ok */
238 } else if (rc) {
239 /* value to compare is invalid */
240 ret = LY_EINVAL;
241 if (err) {
242 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200243 }
Radek Krejci73dead22019-07-11 16:46:16 +0200244 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200245 }
246
247 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200248 if (type->plugin->compare(&node->value, &data)) {
249 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
250 ret = LY_EVALID;
251 }
Radek Krejci084289f2019-07-09 17:35:30 +0200252
253cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200254 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200255
256 return ret;
257}
258
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200259API const char *
260lyd_value2str(const struct lyd_node_term *node, int *dynamic)
261{
262 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
263
264 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
265}
266
267API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100268lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200269{
Michal Vasko9f96a052020-03-10 09:41:45 +0100270 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200271
Michal Vasko9f96a052020-03-10 09:41:45 +0100272 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200273}
274
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200275API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100276lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200277{
Radek Krejcie7b95092019-05-15 11:03:07 +0200278 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200279#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200280 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200281#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200282
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200283 LY_CHECK_ARG_RET(ctx, ctx, NULL);
284
Michal Vasko5b37a352020-03-06 13:38:33 +0100285 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
286 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
287 return NULL;
288 }
289
Michal Vaskoa3881362020-01-21 15:57:35 +0100290#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200291 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200292 /* first item in trees is mandatory - the RPC/action request */
293 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
294 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
295 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
296 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200297 return NULL;
298 }
299 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200300
Radek Krejcie7b95092019-05-15 11:03:07 +0200301 if (options & LYD_OPT_DATA_TEMPLATE) {
302 yang_data_name = va_arg(ap, const char *);
303 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200304#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200305
306 if (!format) {
307 /* TODO try to detect format from the content */
308 }
309
310 switch (format) {
311 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100312 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 break;
314#if 0
315 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200316 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 break;
318 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200319 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 break;
321#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100322 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200323 LOGINT(ctx);
324 break;
325 }
326
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 return result;
328}
329
330API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100331lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200332{
333 struct lyd_node *result;
334 size_t length;
335 char *addr;
336
337 LY_CHECK_ARG_RET(ctx, ctx, NULL);
338 if (fd < 0) {
339 LOGARG(ctx, fd);
340 return NULL;
341 }
342
343 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100344 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200345 if (addr) {
346 ly_munmap(addr, length);
347 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200348
349 return result;
350}
351
352API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100353lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200354{
355 int fd;
356 struct lyd_node *result;
357 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200358
359 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
360
361 fd = open(path, O_RDONLY);
362 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
363
364 if (!format) {
365 /* unknown format - try to detect it from filename's suffix */
366 len = strlen(path);
367
368 /* ignore trailing whitespaces */
369 for (; len > 0 && isspace(path[len - 1]); len--);
370
371 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
372 format = LYD_XML;
373#if 0
374 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
375 format = LYD_JSON;
376 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
377 format = LYD_LYB;
378#endif
379 } /* else still unknown, try later to detect it from the content */
380 }
381
Michal Vaskoa3881362020-01-21 15:57:35 +0100382 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200383 close(fd);
384
385 return result;
386}
Radek Krejci084289f2019-07-09 17:35:30 +0200387
Michal Vasko90932a92020-02-12 14:33:03 +0100388LY_ERR
389lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
390 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
391{
392 LY_ERR ret;
393 struct lyd_node_term *term;
394
Michal Vasko9b368d32020-02-14 13:53:31 +0100395 assert(schema->nodetype & LYD_NODE_TERM);
396
Michal Vasko90932a92020-02-12 14:33:03 +0100397 term = calloc(1, sizeof *term);
398 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
399
400 term->schema = schema;
401 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100402 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100403
404 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
405 if (ret && (ret != LY_EINCOMPLETE)) {
406 free(term);
407 return ret;
408 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100409 lyd_hash((struct lyd_node *)term);
410
411 *node = (struct lyd_node *)term;
412 return ret;
413}
414
415LY_ERR
416lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
417{
418 LY_ERR ret;
419 struct lyd_node_term *term;
420 struct lysc_type *type;
421
422 assert(schema->nodetype & LYD_NODE_TERM);
423
424 term = calloc(1, sizeof *term);
425 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
426
427 term->schema = schema;
428 term->prev = (struct lyd_node *)term;
429 term->flags = LYD_NEW;
430
431 type = ((struct lysc_node_leaf *)schema)->type;
432 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
433 if (ret) {
434 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
435 free(term);
436 return ret;
437 }
438 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100439
440 *node = (struct lyd_node *)term;
441 return ret;
442}
443
444LY_ERR
445lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
446{
447 struct lyd_node_inner *in;
448
Michal Vasko9b368d32020-02-14 13:53:31 +0100449 assert(schema->nodetype & LYD_NODE_INNER);
450
Michal Vasko90932a92020-02-12 14:33:03 +0100451 in = calloc(1, sizeof *in);
452 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
453
454 in->schema = schema;
455 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100456 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100457
Michal Vasko9b368d32020-02-14 13:53:31 +0100458 /* do not hash list with keys, we need them for the hash */
459 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
460 lyd_hash((struct lyd_node *)in);
461 }
Michal Vasko90932a92020-02-12 14:33:03 +0100462
463 *node = (struct lyd_node *)in;
464 return LY_SUCCESS;
465}
466
467static void
468ly_keys_clean(struct ly_keys *keys)
469{
470 size_t i;
471
472 for (i = 0; i < keys->key_count; ++i) {
473 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
474 }
475 free(keys->str);
476 free(keys->keys);
477}
478
479static char *
480ly_keys_parse_next(char **next_key, char **key_name)
481{
482 char *ptr, *ptr2, *val, quot;
Michal Vaskoc2f11292020-05-22 16:43:47 +0200483 const char *pref;
484 size_t pref_len, key_len;
485 int have_equal = 0;
Michal Vasko90932a92020-02-12 14:33:03 +0100486
487 ptr = *next_key;
488
489 /* "[" */
490 LY_CHECK_GOTO(ptr[0] != '[', error);
491 ++ptr;
492
Michal Vaskoc2f11292020-05-22 16:43:47 +0200493 /* skip WS */
494 while (isspace(ptr[0])) {
495 ++ptr;
496 }
Michal Vasko90932a92020-02-12 14:33:03 +0100497
Michal Vaskoc2f11292020-05-22 16:43:47 +0200498 /* key name without prefix */
499 LY_CHECK_GOTO(ly_parse_nodeid((const char **)&ptr, &pref, &pref_len, (const char **)key_name, &key_len), error);
500 if (pref) {
501 goto error;
502 }
Michal Vasko90932a92020-02-12 14:33:03 +0100503
Michal Vaskoc2f11292020-05-22 16:43:47 +0200504 /* terminate it */
505 LY_CHECK_GOTO((ptr[0] != '=') && !isspace(ptr[0]), error);
506 if (ptr[0] == '=') {
507 have_equal = 1;
508 }
509 ptr[0] = '\0';
510 ++ptr;
511
512 if (!have_equal) {
513 /* skip WS */
514 while (isspace(ptr[0])) {
515 ++ptr;
516 }
517
518 /* '=' */
519 LY_CHECK_GOTO(ptr[0] != '=', error);
520 ++ptr;
521 }
522
523 /* skip WS */
524 while (isspace(ptr[0])) {
525 ++ptr;
526 }
Michal Vasko90932a92020-02-12 14:33:03 +0100527
528 /* quote */
529 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
530 quot = ptr[0];
531 ++ptr;
532
533 /* value, terminate it */
534 val = ptr;
535 ptr2 = strchr(ptr, quot);
536 LY_CHECK_GOTO(!ptr2, error);
537 ptr2[0] = '\0';
538
539 /* \0, was quote */
540 ptr = ptr2 + 1;
541
Michal Vaskoc2f11292020-05-22 16:43:47 +0200542 /* skip WS */
543 while (isspace(ptr[0])) {
544 ++ptr;
545 }
546
Michal Vasko90932a92020-02-12 14:33:03 +0100547 /* "]" */
548 LY_CHECK_GOTO(ptr[0] != ']', error);
549 ++ptr;
550
551 *next_key = ptr;
552 return val;
553
554error:
555 *next_key = ptr;
556 return NULL;
557}
558
Michal Vaskod3678892020-05-21 10:06:58 +0200559/* fill keys structure that is expected to be zeroed and must always be cleaned (even on error);
560 * if store is set, fill also each val */
Michal Vasko90932a92020-02-12 14:33:03 +0100561static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200562ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, int log,
563 struct ly_keys *keys)
Michal Vasko90932a92020-02-12 14:33:03 +0100564{
565 LY_ERR ret = LY_SUCCESS;
566 char *next_key, *name;
567 const struct lysc_node *key;
568 size_t i;
569
570 assert(list->nodetype == LYS_LIST);
571
Michal Vaskof03ed032020-03-04 13:31:44 +0100572 if (!keys_str) {
573 /* nothing to parse */
574 return LY_SUCCESS;
575 }
576
577 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100578 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
579
580 next_key = keys->str;
581 while (next_key[0]) {
582 /* new key */
583 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
584 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
585
586 /* fill */
587 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
588 if (!keys->keys[keys->key_count].value) {
Michal Vaskod3678892020-05-21 10:06:58 +0200589 if (log) {
590 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
591 }
Michal Vasko90932a92020-02-12 14:33:03 +0100592 ret = LY_EINVAL;
593 goto cleanup;
594 }
595
596 /* find schema node */
597 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
598 if (!key) {
Michal Vaskod3678892020-05-21 10:06:58 +0200599 if (log) {
600 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
601 }
Michal Vasko90932a92020-02-12 14:33:03 +0100602 ret = LY_EINVAL;
603 goto cleanup;
604 }
605 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
606
607 /* check that we do not have it already */
608 for (i = 0; i < keys->key_count; ++i) {
609 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
Michal Vaskod3678892020-05-21 10:06:58 +0200610 if (log) {
611 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
612 }
Michal Vasko90932a92020-02-12 14:33:03 +0100613 ret = LY_EINVAL;
614 goto cleanup;
615 }
616 }
617
618 if (store) {
619 /* store the value */
620 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
621 lydjson_resolve_prefix, NULL, LYD_JSON);
622 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae420e92020-05-21 10:00:40 +0200623 } else {
624 memset(&keys->keys[keys->key_count].val, 0, sizeof keys->keys[keys->key_count].val);
Michal Vasko90932a92020-02-12 14:33:03 +0100625 }
626
627 /* another valid key */
628 ++keys->key_count;
629 }
630
631cleanup:
Michal Vasko90932a92020-02-12 14:33:03 +0100632 return ret;
633}
634
635LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200636lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, LYD_FORMAT keys_format, int log,
637 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100638{
639 LY_ERR ret = LY_SUCCESS;
640 const struct lysc_node *key_s;
641 struct lyd_node *list = NULL, *key;
642 struct ly_keys keys = {0};
643 size_t i;
644
Michal Vaskod3678892020-05-21 10:06:58 +0200645 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS) && (keys_format != LYD_XML));
Michal Vasko90932a92020-02-12 14:33:03 +0100646
647 /* parse keys */
Michal Vaskod3678892020-05-21 10:06:58 +0200648 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, log, &keys), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100649
650 /* create list */
651 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
652
653 /* everything was checked except that all keys are set */
654 i = 0;
655 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
656 ++i;
657 }
658 if (i != keys.key_count) {
Michal Vaskod3678892020-05-21 10:06:58 +0200659 if (log) {
660 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
661 }
Michal Vasko90932a92020-02-12 14:33:03 +0100662 ret = LY_EINVAL;
663 goto cleanup;
664 }
665
666 /* create and insert all the keys */
667 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +0200668 if (keys_format == LYD_JSON) {
669 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
670 NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
671 } else {
672 assert(keys_format == LYD_SCHEMA);
673 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
674 NULL, lys_resolve_prefix, NULL, LYD_SCHEMA, &key);
675 }
676 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100677 lyd_insert_node(list, NULL, key);
678 }
679
Michal Vasko9b368d32020-02-14 13:53:31 +0100680 /* hash having all the keys */
681 lyd_hash(list);
682
Michal Vasko90932a92020-02-12 14:33:03 +0100683 /* success */
684 *node = list;
685 list = NULL;
686
687cleanup:
688 lyd_free_tree(list);
689 ly_keys_clean(&keys);
690 return ret;
691}
692
693LY_ERR
694lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
695{
696 struct lyd_node_any *any;
697
Michal Vasko9b368d32020-02-14 13:53:31 +0100698 assert(schema->nodetype & LYD_NODE_ANY);
699
Michal Vasko90932a92020-02-12 14:33:03 +0100700 any = calloc(1, sizeof *any);
701 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
702
703 any->schema = schema;
704 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100705 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100706
707 any->value.xml = value;
708 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100709 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100710
711 *node = (struct lyd_node *)any;
712 return LY_SUCCESS;
713}
714
Michal Vasko52927e22020-03-16 17:26:14 +0100715LY_ERR
716lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
717 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
718 const char *ns, struct lyd_node **node)
719{
720 struct lyd_node_opaq *opaq;
721
722 assert(ctx && name && name_len && ns);
723
724 if (!value_len) {
725 value = "";
726 }
727
728 opaq = calloc(1, sizeof *opaq);
729 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
730
731 opaq->prev = (struct lyd_node *)opaq;
732
733 opaq->name = lydict_insert(ctx, name, name_len);
734 opaq->format = format;
735 if (pref_len) {
736 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
737 }
738 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
739 opaq->val_prefs = val_prefs;
740 if (dynamic && *dynamic) {
741 opaq->value = lydict_insert_zc(ctx, (char *)value);
742 *dynamic = 0;
743 } else {
744 opaq->value = lydict_insert(ctx, value, value_len);
745 }
746 opaq->ctx = ctx;
747
748 *node = (struct lyd_node *)opaq;
749 return LY_SUCCESS;
750}
751
Michal Vasko013a8182020-03-03 10:46:53 +0100752API struct lyd_node *
753lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
754{
755 struct lyd_node *ret = NULL;
756 const struct lysc_node *schema;
757 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
758
759 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
760
Michal Vaskof03ed032020-03-04 13:31:44 +0100761 if (!module) {
762 module = parent->schema->module;
763 }
764
Michal Vasko1bf09392020-03-27 12:38:10 +0100765 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100766 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL);
767
768 if (!lyd_create_inner(schema, &ret) && parent) {
769 lyd_insert_node(parent, NULL, ret);
770 }
771 return ret;
772}
773
774API struct lyd_node *
775lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
776{
777 struct lyd_node *ret = NULL, *key;
778 const struct lysc_node *schema, *key_s;
779 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
780 va_list ap;
781 const char *key_val;
782 LY_ERR rc = LY_SUCCESS;
783
784 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
785
Michal Vaskof03ed032020-03-04 13:31:44 +0100786 if (!module) {
787 module = parent->schema->module;
788 }
789
Michal Vasko013a8182020-03-03 10:46:53 +0100790 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
791 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
792
793 /* create list inner node */
794 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
795
796 va_start(ap, name);
797
798 /* create and insert all the keys */
799 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
800 key_val = va_arg(ap, const char *);
801
Michal Vaskof03ed032020-03-04 13:31:44 +0100802 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
803 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100804 lyd_insert_node(ret, NULL, key);
805 }
806
807 /* hash having all the keys */
808 lyd_hash(ret);
809
810 if (parent) {
811 lyd_insert_node(parent, NULL, ret);
812 }
813
814cleanup:
815 if (rc) {
816 lyd_free_tree(ret);
817 ret = NULL;
818 }
819 va_end(ap);
820 return ret;
821}
822
823API struct lyd_node *
824lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
825{
826 struct lyd_node *ret = NULL;
827 const struct lysc_node *schema;
828 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
829
830 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
831
Michal Vaskof03ed032020-03-04 13:31:44 +0100832 if (!module) {
833 module = parent->schema->module;
834 }
835
Michal Vasko013a8182020-03-03 10:46:53 +0100836 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
837 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
838
Michal Vaskod3678892020-05-21 10:06:58 +0200839 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, LYD_JSON, 1, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100840 lyd_insert_node(parent, NULL, ret);
841 }
842 return ret;
843}
844
845API struct lyd_node *
846lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
847{
848 struct lyd_node *ret = NULL;
849 const struct lysc_node *schema;
850 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
851
852 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
853
Michal Vaskof03ed032020-03-04 13:31:44 +0100854 if (!module) {
855 module = parent->schema->module;
856 }
857
Michal Vasko013a8182020-03-03 10:46:53 +0100858 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
859 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
860
Michal Vaskof03ed032020-03-04 13:31:44 +0100861 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
862 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100863 lyd_insert_node(parent, NULL, ret);
864 }
865 return ret;
866}
867
868API struct lyd_node *
869lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
870 LYD_ANYDATA_VALUETYPE value_type)
871{
872 struct lyd_node *ret = NULL;
873 const struct lysc_node *schema;
874 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
875
876 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
877
Michal Vaskof03ed032020-03-04 13:31:44 +0100878 if (!module) {
879 module = parent->schema->module;
880 }
881
Michal Vasko013a8182020-03-03 10:46:53 +0100882 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
883 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
884
885 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
886 lyd_insert_node(parent, NULL, ret);
887 }
888 return ret;
889}
890
Michal Vaskod86997b2020-05-26 15:19:54 +0200891API struct lyd_meta *
892lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
893{
894 struct lyd_meta *ret = NULL;
895 struct ly_ctx *ctx = parent->schema->module->ctx;
896 const char *prefix, *tmp;
897 char *str;
898 size_t pref_len, name_len;
899
900 LY_CHECK_ARG_RET(ctx, parent, name, module || strchr(name, ':'), NULL);
901
902 /* parse the name */
903 tmp = name;
904 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
905 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
906 return NULL;
907 }
908
909 /* find the module */
910 if (prefix) {
911 str = strndup(name, name_len);
912 module = ly_ctx_get_module_implemented(ctx, str);
913 free(str);
914 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%*.s\" not found.", pref_len, prefix), NULL);
915 }
916
917 /* set value if none */
918 if (!val_str) {
919 val_str = "";
920 }
921
922 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
923 LYD_JSON, parent->schema);
924 return ret;
925}
926
Michal Vasko90932a92020-02-12 14:33:03 +0100927struct lyd_node *
928lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
929{
930 const struct lysc_node *prev_key;
931 struct lyd_node *match = NULL;
932
933 if (!first_sibling) {
934 return NULL;
935 }
936
937 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
938 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
939 }
940
941 return match;
942}
943
944/**
945 * @brief Insert node after a sibling.
946 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100947 * Handles inserting into NP containers and key-less lists.
948 *
Michal Vasko90932a92020-02-12 14:33:03 +0100949 * @param[in] sibling Sibling to insert after.
950 * @param[in] node Node to insert.
951 */
952static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100953lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100954{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100955 struct lyd_node_inner *par;
956
Michal Vasko90932a92020-02-12 14:33:03 +0100957 assert(!node->next && (node->prev == node));
958
959 node->next = sibling->next;
960 node->prev = sibling;
961 sibling->next = node;
962 if (node->next) {
963 /* sibling had a succeeding node */
964 node->next->prev = node;
965 } else {
966 /* sibling was last, find first sibling and change its prev */
967 if (sibling->parent) {
968 sibling = sibling->parent->child;
969 } else {
970 for (; sibling->prev->next != node; sibling = sibling->prev);
971 }
972 sibling->prev = node;
973 }
974 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100975
Michal Vasko9f96a052020-03-10 09:41:45 +0100976 for (par = node->parent; par; par = par->parent) {
977 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
978 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100979 par->flags &= ~LYD_DEFAULT;
980 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100981 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
982 /* rehash key-less list */
983 lyd_hash((struct lyd_node *)par);
984 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100985 }
986
987 /* insert into hash table */
988 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100989}
990
991/**
992 * @brief Insert node before a sibling.
993 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100994 * Handles inserting into NP containers and key-less lists.
995 *
Michal Vasko90932a92020-02-12 14:33:03 +0100996 * @param[in] sibling Sibling to insert before.
997 * @param[in] node Node to insert.
998 */
999static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001000lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001001{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001002 struct lyd_node_inner *par;
1003
Michal Vasko90932a92020-02-12 14:33:03 +01001004 assert(!node->next && (node->prev == node));
1005
1006 node->next = sibling;
1007 /* covers situation of sibling being first */
1008 node->prev = sibling->prev;
1009 sibling->prev = node;
1010 if (node->prev->next) {
1011 /* sibling had a preceding node */
1012 node->prev->next = node;
1013 } else if (sibling->parent) {
1014 /* sibling was first and we must also change parent child pointer */
1015 sibling->parent->child = node;
1016 }
1017 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001018
Michal Vasko9f96a052020-03-10 09:41:45 +01001019 for (par = node->parent; par; par = par->parent) {
1020 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1021 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001022 par->flags &= ~LYD_DEFAULT;
1023 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001024 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1025 /* rehash key-less list */
1026 lyd_hash((struct lyd_node *)par);
1027 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001028 }
1029
1030 /* insert into hash table */
1031 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001032}
1033
1034/**
1035 * @brief Insert node as the last child of a parent.
1036 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001037 * Handles inserting into NP containers and key-less lists.
1038 *
Michal Vasko90932a92020-02-12 14:33:03 +01001039 * @param[in] parent Parent to insert into.
1040 * @param[in] node Node to insert.
1041 */
1042static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001043lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001044{
1045 struct lyd_node_inner *par;
1046
Michal Vasko0249f7c2020-03-05 16:36:40 +01001047 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001048 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001049
1050 par = (struct lyd_node_inner *)parent;
1051
1052 if (!par->child) {
1053 par->child = node;
1054 } else {
1055 node->prev = par->child->prev;
1056 par->child->prev->next = node;
1057 par->child->prev = node;
1058 }
1059 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001060
Michal Vasko9f96a052020-03-10 09:41:45 +01001061 for (; par; par = par->parent) {
1062 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1063 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001064 par->flags &= ~LYD_DEFAULT;
1065 }
Michal Vasko52927e22020-03-16 17:26:14 +01001066 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001067 /* rehash key-less list */
1068 lyd_hash((struct lyd_node *)par);
1069 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001070 }
1071
1072 /* insert into hash table */
1073 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001074}
1075
1076void
Michal Vasko9b368d32020-02-14 13:53:31 +01001077lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001078{
Michal Vasko9b368d32020-02-14 13:53:31 +01001079 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001080 const struct lysc_node *skey = NULL;
1081 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001082
Michal Vasko52927e22020-03-16 17:26:14 +01001083 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001084
1085 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1086 parent = (struct lyd_node *)(*first_sibling)->parent;
1087 }
Michal Vasko90932a92020-02-12 14:33:03 +01001088
1089 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001090 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001091 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001092 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1093 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001094 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001095 } else if (lyd_node_children(parent)) {
Radek Krejcidae0ee82020-05-06 16:53:24 +02001096 lyd_insert_before_node(lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001097 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001098 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001099 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001100
1101 /* hash list if all its keys were added */
1102 assert(parent->schema->nodetype == LYS_LIST);
Radek Krejcidae0ee82020-05-06 16:53:24 +02001103 anchor = lyd_node_children(parent);
Michal Vasko9f96a052020-03-10 09:41:45 +01001104 has_keys = 1;
1105 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1106 if (!anchor || (anchor->schema != skey)) {
1107 /* key missing */
1108 has_keys = 0;
1109 break;
1110 }
1111
1112 anchor = anchor->next;
1113 }
1114 if (has_keys) {
1115 lyd_hash(parent);
1116 }
1117
Michal Vasko90932a92020-02-12 14:33:03 +01001118 } else {
1119 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001120 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001121 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001122 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001123 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001124 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001125 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001126 anchor = anchor->prev;
1127 }
1128
Michal Vaskoc193ce92020-03-06 11:04:48 +01001129 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001130 /* insert after last sibling from this module */
1131 lyd_insert_after_node(anchor, node);
1132 } else {
1133 /* no data from this module, insert at the last position */
1134 lyd_insert_after_node((*first_sibling)->prev, node);
1135 }
Michal Vasko90932a92020-02-12 14:33:03 +01001136 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001137 /* the only sibling */
1138 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001139 }
Michal Vasko90932a92020-02-12 14:33:03 +01001140}
1141
Michal Vaskof03ed032020-03-04 13:31:44 +01001142static LY_ERR
1143lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1144{
1145 const struct lysc_node *par2;
1146
1147 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001148 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001149
1150 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001151 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001152
1153 if (parent) {
1154 /* inner node */
1155 if (par2 != parent) {
1156 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1157 return LY_EINVAL;
1158 }
1159 } else {
1160 /* top-level node */
1161 if (par2) {
1162 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1163 return LY_EINVAL;
1164 }
1165 }
1166
1167 return LY_SUCCESS;
1168}
1169
1170API LY_ERR
1171lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1172{
1173 struct lyd_node *iter;
1174
1175 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1176
1177 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1178
1179 if (node->schema->flags & LYS_KEY) {
1180 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1181 return LY_EINVAL;
1182 }
1183
1184 if (node->parent || node->prev->next) {
1185 lyd_unlink_tree(node);
1186 }
1187
1188 while (node) {
1189 iter = node->next;
1190 lyd_unlink_tree(node);
1191 lyd_insert_node(parent, NULL, node);
1192 node = iter;
1193 }
1194 return LY_SUCCESS;
1195}
1196
1197API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001198lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1199{
1200 struct lyd_node *iter;
1201
1202 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1203
Michal Vasko62ed12d2020-05-21 10:08:25 +02001204 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001205
1206 if (node->schema->flags & LYS_KEY) {
1207 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1208 return LY_EINVAL;
1209 }
1210
1211 if (node->parent || node->prev->next) {
1212 lyd_unlink_tree(node);
1213 }
1214
1215 while (node) {
1216 iter = node->next;
1217 lyd_unlink_tree(node);
1218 lyd_insert_node(NULL, &sibling, node);
1219 node = iter;
1220 }
1221 return LY_SUCCESS;
1222}
1223
Michal Vasko0249f7c2020-03-05 16:36:40 +01001224static LY_ERR
1225lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1226{
1227 if (sibling->parent) {
1228 /* nested, we do not care for the order */
1229 return LY_SUCCESS;
1230 }
1231
1232 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001233 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1234 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001235 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert top-level module \"%s\" data into module \"%s\" data.",
Michal Vaskoc193ce92020-03-06 11:04:48 +01001236 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001237 return LY_EINVAL;
1238 }
1239
Michal Vaskoc193ce92020-03-06 11:04:48 +01001240 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1241 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001242 /* inserting before/after its module data */
1243 return LY_SUCCESS;
1244 }
1245 }
1246
1247 /* find first sibling */
1248 while (sibling->prev->next) {
1249 sibling = sibling->prev;
1250 }
1251
1252 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001253 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001254 /* inserting before its module data */
1255 return LY_SUCCESS;
1256 }
1257 }
1258
1259 /* check there are no data of this module */
1260 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001261 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001262 /* some data of this module found */
1263 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001264 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001265 return LY_EINVAL;
1266 }
1267 }
1268
1269 return LY_SUCCESS;
1270}
1271
Michal Vaskob1b5c262020-03-05 14:29:47 +01001272API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001273lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1274{
1275 struct lyd_node *iter;
1276
1277 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1278
Michal Vasko62ed12d2020-05-21 10:08:25 +02001279 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001280
1281 if (node->schema->flags & LYS_KEY) {
1282 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1283 return LY_EINVAL;
1284 } else if (sibling->schema->flags & LYS_KEY) {
1285 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1286 return LY_EINVAL;
1287 }
1288
Michal Vasko0249f7c2020-03-05 16:36:40 +01001289 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1290
Michal Vaskof03ed032020-03-04 13:31:44 +01001291 if (node->parent || node->prev->next) {
1292 lyd_unlink_tree(node);
1293 }
1294
1295 /* insert in reverse order to get the original order */
1296 node = node->prev;
1297 while (node) {
1298 iter = node->prev;
1299 lyd_unlink_tree(node);
1300
1301 lyd_insert_before_node(sibling, node);
1302 /* move the anchor accordingly */
1303 sibling = node;
1304
1305 node = (iter == node) ? NULL : iter;
1306 }
1307 return LY_SUCCESS;
1308}
1309
1310API LY_ERR
1311lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1312{
1313 struct lyd_node *iter;
1314
1315 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1316
Michal Vasko62ed12d2020-05-21 10:08:25 +02001317 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001318
1319 if (node->schema->flags & LYS_KEY) {
1320 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1321 return LY_EINVAL;
1322 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1323 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1324 return LY_EINVAL;
1325 }
1326
Michal Vasko0249f7c2020-03-05 16:36:40 +01001327 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1328
Michal Vaskof03ed032020-03-04 13:31:44 +01001329 if (node->parent || node->prev->next) {
1330 lyd_unlink_tree(node);
1331 }
1332
1333 while (node) {
1334 iter = node->next;
1335 lyd_unlink_tree(node);
1336
1337 lyd_insert_after_node(sibling, node);
1338 /* move the anchor accordingly */
1339 sibling = node;
1340
1341 node = iter;
1342 }
1343 return LY_SUCCESS;
1344}
1345
1346API void
1347lyd_unlink_tree(struct lyd_node *node)
1348{
1349 struct lyd_node *iter;
1350
1351 if (!node) {
1352 return;
1353 }
1354
1355 /* unlink from siblings */
1356 if (node->prev->next) {
1357 node->prev->next = node->next;
1358 }
1359 if (node->next) {
1360 node->next->prev = node->prev;
1361 } else {
1362 /* unlinking the last node */
1363 if (node->parent) {
1364 iter = node->parent->child;
1365 } else {
1366 iter = node->prev;
1367 while (iter->prev != node) {
1368 iter = iter->prev;
1369 }
1370 }
1371 /* update the "last" pointer from the first node */
1372 iter->prev = node->prev;
1373 }
1374
1375 /* unlink from parent */
1376 if (node->parent) {
1377 if (node->parent->child == node) {
1378 /* the node is the first child */
1379 node->parent->child = node->next;
1380 }
1381
1382 lyd_unlink_hash(node);
1383
1384 /* check for keyless list and update its hash */
1385 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001386 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001387 lyd_hash(iter);
1388 }
1389 }
1390
1391 node->parent = NULL;
1392 }
1393
1394 node->next = NULL;
1395 node->prev = node;
1396}
1397
Michal Vasko90932a92020-02-12 14:33:03 +01001398LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001399lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko52927e22020-03-16 17:26:14 +01001400 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix resolve_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01001401 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001402{
1403 LY_ERR ret;
1404 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001405 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001406 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001407
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001409
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001410 LY_ARRAY_FOR(mod->compiled->exts, u) {
1411 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1412 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001413 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001414 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001415 break;
1416 }
1417 }
1418 if (!ant) {
1419 /* attribute is not defined as a metadata annotation (RFC 7952) */
1420 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1421 mod->name, name_len, name);
1422 return LY_EINVAL;
1423 }
1424
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 mt = calloc(1, sizeof *mt);
1426 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1427 mt->parent = parent;
1428 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001429 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, resolve_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01001430 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001432 return ret;
1433 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001434 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001435
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001436 /* insert as the last attribute */
1437 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001438 if (parent->meta) {
1439 for (last = parent->meta; last->next; last = last->next);
1440 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001441 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001443 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 } else if (*meta) {
1445 for (last = *meta; last->next; last = last->next);
1446 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001447 }
1448
1449 /* remove default flags from NP containers */
1450 while (parent && (parent->flags & LYD_DEFAULT)) {
1451 parent->flags &= ~LYD_DEFAULT;
1452 parent = (struct lyd_node *)parent->parent;
1453 }
1454
Michal Vasko9f96a052020-03-10 09:41:45 +01001455 if (meta) {
1456 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001457 }
1458 return ret;
1459}
1460
Michal Vasko52927e22020-03-16 17:26:14 +01001461LY_ERR
1462ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1463 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1464 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1465{
1466 struct ly_attr *at, *last;
1467 struct lyd_node_opaq *opaq;
1468
1469 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1470 assert(name && name_len);
1471 assert((prefix_len && ns) || (!prefix_len && !ns));
1472
1473 if (!value_len) {
1474 value = "";
1475 }
1476
1477 at = calloc(1, sizeof *at);
1478 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1479 at->parent = (struct lyd_node_opaq *)parent;
1480 at->name = lydict_insert(ctx, name, name_len);
1481 if (dynamic && *dynamic) {
1482 at->value = lydict_insert_zc(ctx, (char *)value);
1483 *dynamic = 0;
1484 } else {
1485 at->value = lydict_insert(ctx, value, value_len);
1486 }
1487
1488 at->format = format;
1489 at->val_prefs = val_prefs;
1490 if (ns) {
1491 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1492 at->prefix.ns = lydict_insert(ctx, ns, 0);
1493 }
1494
1495 /* insert as the last attribute */
1496 if (parent) {
1497 opaq = (struct lyd_node_opaq *)parent;
1498 if (opaq->attr) {
1499 for (last = opaq->attr; last->next; last = last->next);
1500 last->next = at;
1501 } else {
1502 opaq->attr = at;
1503 }
1504 } else if (*attr) {
1505 for (last = *attr; last->next; last = last->next);
1506 last->next = at;
1507 }
1508
1509 if (attr) {
1510 *attr = at;
1511 }
1512 return LY_SUCCESS;
1513}
1514
Radek Krejci084289f2019-07-09 17:35:30 +02001515API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001516lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001517{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001518 LY_ARRAY_SIZE_TYPE u, v;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001519 const struct lyd_node *start_sibling;
Michal Vaskoe444f752020-02-10 12:20:06 +01001520 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001521 uint64_t pos = 1;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001522 int match;
Radek Krejci084289f2019-07-09 17:35:30 +02001523
Michal Vaskof03ed032020-03-04 13:31:44 +01001524 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001525
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001526 /* first iteration */
1527 start_sibling = tree;
1528 u = 0;
1529 while (u < LY_ARRAY_SIZE(path)) {
1530 /* find next node instance */
Michal Vasko84c9f1b2020-05-14 12:08:11 +02001531 if (start_sibling && !start_sibling->prev->next && !(path[u].node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
1532 /* starting from the beginning using hashes */
1533 lyd_find_sibling_val(start_sibling, path[u].node, NULL, 0, &node);
1534 } else {
1535 /* next matching sibling */
1536 lyd_find_sibling_next2(start_sibling, path[u].node, NULL, 0, &node);
1537 }
Radek Krejci084289f2019-07-09 17:35:30 +02001538 if (!node) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001539 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001540 }
1541
1542 /* check predicate if any */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001543 match = 1;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001544 LY_ARRAY_FOR(path[u].predicates, v) {
1545 if (path[u].predicates[v].type == 0) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001546 assert(LY_ARRAY_SIZE(path[u].predicates) == 1);
Radek Krejci084289f2019-07-09 17:35:30 +02001547 /* position predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001548 if (pos != path[u].predicates[v].position) {
Radek Krejci084289f2019-07-09 17:35:30 +02001549 pos++;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001550 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001551 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001552 } else if (path[u].predicates[v].type == 1) {
Radek Krejci084289f2019-07-09 17:35:30 +02001553 /* key-predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001554 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].predicates[v].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001555 struct lyd_node *key;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001556
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001557 lyd_find_sibling_val(lyd_node_children(node), path[u].predicates[v].key, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001558 if (!key) {
1559 /* probably error and we shouldn't be here due to previous checks when creating path */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001560 match = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001561 } else if (type->plugin->compare(&((struct lyd_node_term *)key)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001562 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001563 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001564 } else if (path[u].predicates[v].type == 2) {
Radek Krejci084289f2019-07-09 17:35:30 +02001565 /* leaf-list-predicate */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001566 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].node)->type;
1567
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001568 if (type->plugin->compare(&((struct lyd_node_term *)node)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001569 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001570 }
1571 } else {
1572 LOGINT(NULL);
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001573 return NULL;
1574 }
1575
1576 if (!match) {
1577 /* useless to check more predicates */
1578 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001579 }
1580 }
1581
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001582 if (!match) {
1583 /* try to match next sibling */
1584 start_sibling = node->next;
1585 } else {
1586 /* matched, move to the next path segment */
1587 ++u;
1588 start_sibling = lyd_node_children(node);
1589 pos = 1;
1590 }
Radek Krejci084289f2019-07-09 17:35:30 +02001591 }
1592
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001593 return (const struct lyd_node_term *)node;
Radek Krejci084289f2019-07-09 17:35:30 +02001594}
1595
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001596API LY_ERR
1597lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1598{
1599 const struct lyd_node *iter1, *iter2;
1600 struct lyd_node_term *term1, *term2;
1601 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001602 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001603 struct lysc_type *type;
1604 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001605
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001606 if (!node1 || !node2) {
1607 if (node1 == node2) {
1608 return LY_SUCCESS;
1609 } else {
1610 return LY_ENOT;
1611 }
1612 }
1613
Michal Vasko52927e22020-03-16 17:26:14 +01001614 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001615 return LY_ENOT;
1616 }
1617
1618 if (node1->hash != node2->hash) {
1619 return LY_ENOT;
1620 }
Michal Vasko52927e22020-03-16 17:26:14 +01001621 /* 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 Krejci1f05b6a2019-07-18 16:15:06 +02001622
Michal Vasko52927e22020-03-16 17:26:14 +01001623 if (!node1->schema) {
1624 opaq1 = (struct lyd_node_opaq *)node1;
1625 opaq2 = (struct lyd_node_opaq *)node2;
1626 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001627 return LY_ENOT;
1628 }
Michal Vasko52927e22020-03-16 17:26:14 +01001629 switch (opaq1->format) {
1630 case LYD_XML:
1631 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1632 return LY_ENOT;
1633 }
1634 break;
1635 case LYD_SCHEMA:
1636 /* not allowed */
1637 LOGINT(LYD_NODE_CTX(node1));
1638 return LY_EINT;
1639 }
1640 if (options & LYD_COMPARE_FULL_RECURSION) {
1641 iter1 = opaq1->child;
1642 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001643 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001644 }
1645 return LY_SUCCESS;
1646 } else {
1647 switch (node1->schema->nodetype) {
1648 case LYS_LEAF:
1649 case LYS_LEAFLIST:
1650 if (options & LYD_COMPARE_DEFAULTS) {
1651 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1652 return LY_ENOT;
1653 }
1654 }
1655
1656 term1 = (struct lyd_node_term*)node1;
1657 term2 = (struct lyd_node_term*)node2;
1658 type = ((struct lysc_node_leaf*)node1->schema)->type;
1659
1660 return type->plugin->compare(&term1->value, &term2->value);
1661 case LYS_CONTAINER:
1662 if (options & LYD_COMPARE_DEFAULTS) {
1663 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1664 return LY_ENOT;
1665 }
1666 }
1667 if (options & LYD_COMPARE_FULL_RECURSION) {
1668 iter1 = ((struct lyd_node_inner*)node1)->child;
1669 iter2 = ((struct lyd_node_inner*)node2)->child;
1670 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001671 }
1672 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001673 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001674 case LYS_ACTION:
1675 if (options & LYD_COMPARE_FULL_RECURSION) {
1676 /* TODO action/RPC
1677 goto all_children_compare;
1678 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001679 }
1680 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001681 case LYS_NOTIF:
1682 if (options & LYD_COMPARE_FULL_RECURSION) {
1683 /* TODO Notification
1684 goto all_children_compare;
1685 */
1686 }
1687 return LY_SUCCESS;
1688 case LYS_LIST:
1689 iter1 = ((struct lyd_node_inner*)node1)->child;
1690 iter2 = ((struct lyd_node_inner*)node2)->child;
1691
1692 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1693 /* lists with keys, their equivalence is based on their keys */
1694 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1695 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1696 key = key->next) {
1697 if (lyd_compare(iter1, iter2, options)) {
1698 return LY_ENOT;
1699 }
1700 iter1 = iter1->next;
1701 iter2 = iter2->next;
1702 }
1703 } else {
1704 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1705
1706 all_children_compare:
1707 if (!iter1 && !iter2) {
1708 /* no children, nothing to compare */
1709 return LY_SUCCESS;
1710 }
1711
1712 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1713 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1714 return LY_ENOT;
1715 }
1716 }
1717 if (iter1 || iter2) {
1718 return LY_ENOT;
1719 }
1720 }
1721 return LY_SUCCESS;
1722 case LYS_ANYXML:
1723 case LYS_ANYDATA:
1724 any1 = (struct lyd_node_any*)node1;
1725 any2 = (struct lyd_node_any*)node2;
1726
1727 if (any1->value_type != any2->value_type) {
1728 return LY_ENOT;
1729 }
1730 switch (any1->value_type) {
1731 case LYD_ANYDATA_DATATREE:
1732 iter1 = any1->value.tree;
1733 iter2 = any2->value.tree;
1734 goto all_children_compare;
1735 case LYD_ANYDATA_STRING:
1736 case LYD_ANYDATA_XML:
1737 case LYD_ANYDATA_JSON:
1738 len1 = strlen(any1->value.str);
1739 len2 = strlen(any2->value.str);
1740 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1741 return LY_ENOT;
1742 }
1743 return LY_SUCCESS;
1744 #if 0 /* TODO LYB format */
1745 case LYD_ANYDATA_LYB:
1746 int len1 = lyd_lyb_data_length(any1->value.mem);
1747 int len2 = lyd_lyb_data_length(any2->value.mem);
1748 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1749 return LY_ENOT;
1750 }
1751 return LY_SUCCESS;
1752 #endif
1753 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001754 }
1755 }
1756
Michal Vasko52927e22020-03-16 17:26:14 +01001757 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001758 return LY_EINT;
1759}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001760
1761/**
Michal Vasko52927e22020-03-16 17:26:14 +01001762 * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001763 *
1764 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001765 *
1766 * @param[in] node Original node to duplicate
1767 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1768 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1769 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1770 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1771 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001772 */
Michal Vasko52927e22020-03-16 17:26:14 +01001773static LY_ERR
1774lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1775 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001776{
Michal Vasko52927e22020-03-16 17:26:14 +01001777 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001778 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001779 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001780
Michal Vasko52927e22020-03-16 17:26:14 +01001781 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001782
Michal Vasko52927e22020-03-16 17:26:14 +01001783 if (!node->schema) {
1784 dup = calloc(1, sizeof(struct lyd_node_opaq));
1785 } else {
1786 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001787 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001788 case LYS_ACTION:
1789 case LYS_NOTIF:
1790 case LYS_CONTAINER:
1791 case LYS_LIST:
1792 dup = calloc(1, sizeof(struct lyd_node_inner));
1793 break;
1794 case LYS_LEAF:
1795 case LYS_LEAFLIST:
1796 dup = calloc(1, sizeof(struct lyd_node_term));
1797 break;
1798 case LYS_ANYDATA:
1799 case LYS_ANYXML:
1800 dup = calloc(1, sizeof(struct lyd_node_any));
1801 break;
1802 default:
1803 LOGINT(LYD_NODE_CTX(node));
1804 ret = LY_EINT;
1805 goto error;
1806 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001807 }
Michal Vasko52927e22020-03-16 17:26:14 +01001808 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001809
1810 /* TODO implement LYD_DUP_WITH_WHEN */
1811 dup->flags = node->flags;
1812 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001813 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001814
1815 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1816
1817 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001818 if (!dup->schema) {
1819 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1820 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1821 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001822
1823 if (options & LYD_DUP_RECURSIVE) {
1824 /* duplicate all the children */
1825 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001826 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1827 }
1828 }
1829 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1830 opaq->format = orig->format;
1831 if (orig->prefix.pref) {
1832 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1833 }
1834 if (orig->prefix.ns) {
1835 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1836 }
1837 if (orig->val_prefs) {
1838 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1839 LY_ARRAY_FOR(orig->val_prefs, u) {
1840 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1841 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1842 LY_ARRAY_INCREMENT(opaq->val_prefs);
1843 }
1844 }
1845 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1846 opaq->ctx = orig->ctx;
1847 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1848 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1849 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1850
1851 term->hash = orig->hash;
1852 term->value.realtype = orig->value.realtype;
1853 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1854 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1855 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1856 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1857 struct lyd_node *child;
1858
1859 if (options & LYD_DUP_RECURSIVE) {
1860 /* duplicate all the children */
1861 LY_LIST_FOR(orig->child, child) {
1862 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001863 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001864 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001865 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001866 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001867 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001868 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1869 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001870 if (!child) {
1871 /* possibly not keys are present in filtered tree */
1872 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001873 } else if (child->schema != key) {
1874 /* possibly not all keys are present in filtered tree,
1875 * but there can be also some non-key nodes */
1876 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001877 }
Michal Vasko52927e22020-03-16 17:26:14 +01001878 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001879 child = child->next;
1880 }
1881 }
1882 lyd_hash(dup);
1883 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001884 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1885 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001886
1887 any->hash = orig->hash;
1888 any->value_type = orig->value_type;
1889 switch (any->value_type) {
1890 case LYD_ANYDATA_DATATREE:
1891 if (orig->value.tree) {
1892 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02001893 if (!any->value.tree) {
1894 /* get the last error's error code recorded by lyd_dup */
1895 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
1896 ret = ei ? ei->prev->no : LY_EOTHER;
1897 goto error;
1898 }
1899 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0 ,error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001900 }
1901 break;
1902 case LYD_ANYDATA_STRING:
1903 case LYD_ANYDATA_XML:
1904 case LYD_ANYDATA_JSON:
1905 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001906 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001907 }
1908 break;
1909 }
1910 }
1911
Michal Vasko52927e22020-03-16 17:26:14 +01001912 /* insert */
1913 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001914 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001915
1916 if (dup_p) {
1917 *dup_p = dup;
1918 }
1919 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001920
1921error:
Michal Vasko52927e22020-03-16 17:26:14 +01001922 lyd_free_tree(dup);
1923 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001924}
1925
1926API struct lyd_node *
1927lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1928{
1929 struct ly_ctx *ctx;
1930 const struct lyd_node *orig; /* original node to be duplicated */
1931 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001932 struct lyd_node *top = NULL; /* the most higher created node */
1933 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1934 int keyless_parent_list = 0;
1935
1936 LY_CHECK_ARG_RET(NULL, node, NULL);
1937 ctx = node->schema->module->ctx;
1938
1939 if (options & LYD_DUP_WITH_PARENTS) {
1940 struct lyd_node_inner *orig_parent, *iter;
1941 int repeat = 1;
1942 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1943 if (parent && parent->schema == orig_parent->schema) {
1944 /* stop creating parents, connect what we have into the provided parent */
1945 iter = parent;
1946 repeat = 0;
1947 /* get know if there is a keyless list which we will have to rehash */
1948 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001949 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001950 keyless_parent_list = 1;
1951 break;
1952 }
1953 }
1954 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001955 iter = NULL;
1956 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1957 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001958 }
1959 if (!local_parent) {
1960 local_parent = iter;
1961 }
1962 if (iter->child) {
1963 /* 1) list - add after keys
1964 * 2) provided parent with some children */
1965 iter->child->prev->next = top;
1966 if (top) {
1967 top->prev = iter->child->prev;
1968 iter->child->prev = top;
1969 }
1970 } else {
1971 iter->child = top;
1972 if (iter->schema->nodetype == LYS_LIST) {
1973 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1974 keyless_parent_list = 1;
1975 }
1976 }
1977 if (top) {
1978 top->parent = iter;
1979 }
1980 top = (struct lyd_node*)iter;
1981 }
1982 if (repeat && parent) {
1983 /* given parent and created parents chain actually do not interconnect */
1984 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1985 goto error;
1986 }
1987 } else {
1988 local_parent = parent;
1989 }
1990
Radek Krejci22ebdba2019-07-25 13:59:43 +02001991 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001992 /* if there is no local parent, it will be inserted into first */
1993 LY_CHECK_GOTO(lyd_dup_recursive(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001994 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1995 break;
1996 }
1997 }
1998 if (keyless_parent_list) {
1999 /* rehash */
2000 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002001 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002002 lyd_hash((struct lyd_node*)local_parent);
2003 }
2004 }
2005 }
2006 return first;
2007
2008error:
2009 if (top) {
2010 lyd_free_tree(top);
2011 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002012 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002013 }
2014 return NULL;
2015}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002016
2017static LY_ERR
2018lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2019{
Michal Vasko14654712020-02-06 08:35:21 +01002020 /* ending \0 */
2021 ++reqlen;
2022
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002023 if (reqlen > *buflen) {
2024 if (is_static) {
2025 return LY_EINCOMPLETE;
2026 }
2027
2028 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2029 if (!*buffer) {
2030 return LY_EMEM;
2031 }
2032
2033 *buflen = reqlen;
2034 }
2035
2036 return LY_SUCCESS;
2037}
2038
2039/**
2040 * @brief Append all list key predicates to path.
2041 *
2042 * @param[in] node Node with keys to print.
2043 * @param[in,out] buffer Buffer to print to.
2044 * @param[in,out] buflen Current buffer length.
2045 * @param[in,out] bufused Current number of characters used in @p buffer.
2046 * @param[in] is_static Whether buffer is static or can be reallocated.
2047 * @return LY_ERR
2048 */
2049static LY_ERR
2050lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2051{
2052 const struct lyd_node *key;
2053 int dynamic = 0;
2054 size_t len;
2055 const char *val;
2056 char quot;
2057 LY_ERR rc;
2058
Michal Vasko14654712020-02-06 08:35:21 +01002059 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002060 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2061 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2062 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2063 if (rc != LY_SUCCESS) {
2064 if (dynamic) {
2065 free((char *)val);
2066 }
2067 return rc;
2068 }
2069
2070 quot = '\'';
2071 if (strchr(val, '\'')) {
2072 quot = '"';
2073 }
2074 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2075
2076 if (dynamic) {
2077 free((char *)val);
2078 }
2079 }
2080
2081 return LY_SUCCESS;
2082}
2083
2084/**
2085 * @brief Append leaf-list value predicate to path.
2086 *
2087 * @param[in] node Node to print.
2088 * @param[in,out] buffer Buffer to print to.
2089 * @param[in,out] buflen Current buffer length.
2090 * @param[in,out] bufused Current number of characters used in @p buffer.
2091 * @param[in] is_static Whether buffer is static or can be reallocated.
2092 * @return LY_ERR
2093 */
2094static LY_ERR
2095lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2096{
2097 int dynamic = 0;
2098 size_t len;
2099 const char *val;
2100 char quot;
2101 LY_ERR rc;
2102
2103 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2104 len = 4 + strlen(val) + 2;
2105 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2106 if (rc != LY_SUCCESS) {
2107 goto cleanup;
2108 }
2109
2110 quot = '\'';
2111 if (strchr(val, '\'')) {
2112 quot = '"';
2113 }
2114 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2115
2116cleanup:
2117 if (dynamic) {
2118 free((char *)val);
2119 }
2120 return rc;
2121}
2122
2123/**
2124 * @brief Append node position (relative to its other instances) predicate to path.
2125 *
2126 * @param[in] node Node to print.
2127 * @param[in,out] buffer Buffer to print to.
2128 * @param[in,out] buflen Current buffer length.
2129 * @param[in,out] bufused Current number of characters used in @p buffer.
2130 * @param[in] is_static Whether buffer is static or can be reallocated.
2131 * @return LY_ERR
2132 */
2133static LY_ERR
2134lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2135{
2136 const struct lyd_node *first, *iter;
2137 size_t len;
2138 int pos;
2139 char *val = NULL;
2140 LY_ERR rc;
2141
2142 if (node->parent) {
2143 first = node->parent->child;
2144 } else {
2145 for (first = node; node->prev->next; node = node->prev);
2146 }
2147 pos = 1;
2148 for (iter = first; iter != node; iter = iter->next) {
2149 if (iter->schema == node->schema) {
2150 ++pos;
2151 }
2152 }
2153 if (asprintf(&val, "%d", pos) == -1) {
2154 return LY_EMEM;
2155 }
2156
2157 len = 1 + strlen(val) + 1;
2158 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2159 if (rc != LY_SUCCESS) {
2160 goto cleanup;
2161 }
2162
2163 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2164
2165cleanup:
2166 free(val);
2167 return rc;
2168}
2169
2170API char *
2171lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2172{
Michal Vasko14654712020-02-06 08:35:21 +01002173 int is_static = 0, i, depth;
2174 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002175 const struct lyd_node *iter;
2176 const struct lys_module *mod;
2177 LY_ERR rc;
2178
2179 LY_CHECK_ARG_RET(NULL, node, NULL);
2180 if (buffer) {
2181 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2182 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002183 } else {
2184 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002185 }
2186
2187 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002188 case LYD_PATH_LOG:
2189 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002190 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2191 ++depth;
2192 }
2193
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002194 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002195 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002196 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002197 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002198iter_print:
2199 /* print prefix and name */
2200 mod = NULL;
2201 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2202 mod = iter->schema->module;
2203 }
2204
2205 /* realloc string */
2206 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2207 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2208 if (rc != LY_SUCCESS) {
2209 break;
2210 }
2211
2212 /* print next node */
2213 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2214
2215 switch (iter->schema->nodetype) {
2216 case LYS_LIST:
2217 if (iter->schema->flags & LYS_KEYLESS) {
2218 /* print its position */
2219 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2220 } else {
2221 /* print all list keys in predicates */
2222 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2223 }
2224 break;
2225 case LYS_LEAFLIST:
2226 if (iter->schema->flags & LYS_CONFIG_W) {
2227 /* print leaf-list value */
2228 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2229 } else {
2230 /* print its position */
2231 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2232 }
2233 break;
2234 default:
2235 /* nothing to print more */
2236 rc = LY_SUCCESS;
2237 break;
2238 }
2239 if (rc != LY_SUCCESS) {
2240 break;
2241 }
2242
Michal Vasko14654712020-02-06 08:35:21 +01002243 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002244 }
2245 break;
2246 }
2247
2248 return buffer;
2249}
Michal Vaskoe444f752020-02-10 12:20:06 +01002250
Michal Vasko9b368d32020-02-14 13:53:31 +01002251LY_ERR
2252lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2253 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002254{
2255 LY_ERR rc;
2256 const struct lyd_node *node = NULL;
2257 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002258 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002259 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002260 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002261
Michal Vasko9b368d32020-02-14 13:53:31 +01002262 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002263
2264 if (!first) {
2265 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002266 if (match) {
2267 *match = NULL;
2268 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002269 return LY_ENOTFOUND;
2270 }
2271
Michal Vaskoe444f752020-02-10 12:20:06 +01002272 if (key_or_value && !val_len) {
2273 val_len = strlen(key_or_value);
2274 }
2275
2276 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002277 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002278 LY_CHECK_GOTO(rc = lyd_value_store(&val, schema, key_or_value, val_len, 0, lydjson_resolve_prefix, NULL, LYD_JSON), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002279 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2280 /* parse keys into canonical values */
Michal Vaskod3678892020-05-21 10:06:58 +02002281 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, 1, &keys), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002282 }
2283
2284 /* find first matching value */
2285 LY_LIST_FOR(first, node) {
2286 if (node->schema != schema) {
2287 continue;
2288 }
2289
2290 if ((schema->nodetype == LYS_LIST) && keys.str) {
2291 /* compare all set keys */
2292 for (i = 0; i < keys.key_count; ++i) {
2293 /* find key */
2294 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2295 (struct lyd_node **)&term);
2296 if (rc == LY_ENOTFOUND) {
2297 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002298 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002299 }
2300 LY_CHECK_GOTO(rc, cleanup);
2301
2302 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002303 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002304 break;
2305 }
2306 }
2307
2308 if (i < keys.key_count) {
2309 /* not a match */
2310 continue;
2311 }
Michal Vasko90932a92020-02-12 14:33:03 +01002312 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002313 term = (struct lyd_node_term *)node;
2314
2315 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002316 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002317 /* not a match */
2318 continue;
2319 }
2320 }
2321
2322 /* all criteria passed */
2323 break;
2324 }
2325
2326 if (!node) {
2327 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002328 if (match) {
2329 *match = NULL;
2330 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002331 goto cleanup;
2332 }
2333
2334 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002335 if (match) {
2336 *match = (struct lyd_node *)node;
2337 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002338 rc = LY_SUCCESS;
2339
2340cleanup:
2341 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002342 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002343 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002344 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002345 return rc;
2346}
2347
2348API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002349lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2350 const char *key_or_value, size_t val_len, struct lyd_node **match)
2351{
2352 const struct lysc_node *schema;
2353
2354 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2355
2356 if (!first) {
2357 /* no data */
2358 *match = NULL;
2359 return LY_ENOTFOUND;
2360 }
2361
2362 /* find schema */
2363 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2364 if (!schema) {
2365 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2366 return LY_EINVAL;
2367 }
2368
2369 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2370}
2371
2372API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002373lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2374{
2375 struct lyd_node **match_p;
2376 struct lyd_node_inner *parent;
2377
Michal Vaskof03ed032020-03-04 13:31:44 +01002378 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002379
Michal Vasko62ed12d2020-05-21 10:08:25 +02002380 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2381 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002382 if (match) {
2383 *match = NULL;
2384 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002385 return LY_ENOTFOUND;
2386 }
2387
2388 /* find first sibling */
2389 if (siblings->parent) {
2390 siblings = siblings->parent->child;
2391 } else {
2392 while (siblings->prev->next) {
2393 siblings = siblings->prev;
2394 }
2395 }
2396
2397 parent = (struct lyd_node_inner *)siblings->parent;
2398 if (parent && parent->children_ht) {
2399 assert(target->hash);
2400
2401 /* find by hash */
2402 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2403 siblings = *match_p;
2404 } else {
2405 /* not found */
2406 siblings = NULL;
2407 }
2408 } else {
2409 /* no children hash table */
2410 for (; siblings; siblings = siblings->next) {
2411 if (!lyd_compare(siblings, target, 0)) {
2412 break;
2413 }
2414 }
2415 }
2416
2417 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002418 if (match) {
2419 *match = NULL;
2420 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002421 return LY_ENOTFOUND;
2422 }
2423
Michal Vasko9b368d32020-02-14 13:53:31 +01002424 if (match) {
2425 *match = (struct lyd_node *)siblings;
2426 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002427 return LY_SUCCESS;
2428}
2429
2430API LY_ERR
2431lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2432{
2433 struct lyd_node_inner *parent;
2434 struct lyd_node *match;
2435 struct lyd_node **match_p;
2436 struct ly_set *ret;
2437
2438 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2439
Michal Vasko62ed12d2020-05-21 10:08:25 +02002440 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2441 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01002442 return LY_ENOTFOUND;
2443 }
2444
2445 ret = ly_set_new();
2446 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2447
2448 /* find first sibling */
2449 if (siblings->parent) {
2450 siblings = siblings->parent->child;
2451 } else {
2452 while (siblings->prev->next) {
2453 siblings = siblings->prev;
2454 }
2455 }
2456
2457 parent = (struct lyd_node_inner *)siblings->parent;
2458 if (parent && parent->children_ht) {
2459 assert(target->hash);
2460
2461 /* find by hash */
2462 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2463 match = *match_p;
2464 } else {
2465 /* not found */
2466 match = NULL;
2467 }
2468 while (match) {
2469 /* add all found nodes into the return set */
2470 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2471 goto error;
2472 }
2473
2474 /* find next instance */
2475 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2476 match = NULL;
2477 } else {
2478 match = *match_p;
2479 }
2480 }
2481 } else {
2482 /* no children hash table */
2483 for (; siblings; siblings = siblings->next) {
2484 if (!lyd_compare(siblings, target, 0)) {
2485 /* a match */
2486 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2487 goto error;
2488 }
2489 }
2490 }
2491 }
2492
2493 if (!ret->count) {
2494 ly_set_free(ret, NULL);
2495 return LY_ENOTFOUND;
2496 }
2497
2498 *set = ret;
2499 return LY_SUCCESS;
2500
2501error:
2502 ly_set_free(ret, NULL);
2503 return LY_EMEM;
2504}
2505
Michal Vasko90932a92020-02-12 14:33:03 +01002506static int
2507lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2508{
2509 struct lysc_node *val1;
2510 struct lyd_node *val2;
2511
2512 val1 = *((struct lysc_node **)val1_p);
2513 val2 = *((struct lyd_node **)val2_p);
2514
2515 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2516
2517 if (val1 == val2->schema) {
2518 /* schema match is enough */
2519 return 1;
2520 } else {
2521 return 0;
2522 }
2523}
2524
2525static LY_ERR
2526lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2527{
2528 struct lyd_node **match_p;
2529 struct lyd_node_inner *parent;
2530 uint32_t hash;
2531 values_equal_cb ht_cb;
2532
2533 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2534
2535 /* find first sibling */
2536 if (siblings->parent) {
2537 siblings = siblings->parent->child;
2538 } else {
2539 while (siblings->prev->next) {
2540 siblings = siblings->prev;
2541 }
2542 }
2543
2544 parent = (struct lyd_node_inner *)siblings->parent;
2545 if (parent && parent->children_ht) {
2546 /* calculate our hash */
2547 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2548 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2549 hash = dict_hash_multi(hash, NULL, 0);
2550
2551 /* use special hash table function */
2552 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2553
2554 /* find by hash */
2555 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2556 siblings = *match_p;
2557 } else {
2558 /* not found */
2559 siblings = NULL;
2560 }
2561
2562 /* set the original hash table compare function back */
2563 lyht_set_cb(parent->children_ht, ht_cb);
2564 } else {
2565 /* no children hash table */
2566 for (; siblings; siblings = siblings->next) {
2567 if (siblings->schema == schema) {
2568 /* schema match is enough */
2569 break;
2570 }
2571 }
2572 }
2573
2574 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002575 if (match) {
2576 *match = NULL;
2577 }
Michal Vasko90932a92020-02-12 14:33:03 +01002578 return LY_ENOTFOUND;
2579 }
2580
Michal Vasko9b368d32020-02-14 13:53:31 +01002581 if (match) {
2582 *match = (struct lyd_node *)siblings;
2583 }
Michal Vasko90932a92020-02-12 14:33:03 +01002584 return LY_SUCCESS;
2585}
2586
Michal Vaskoe444f752020-02-10 12:20:06 +01002587API LY_ERR
2588lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2589 size_t val_len, struct lyd_node **match)
2590{
2591 LY_ERR rc;
2592 struct lyd_node *target = NULL;
2593
2594 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02002595 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002596 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2597 return LY_EINVAL;
2598 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2599 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2600 return LY_EINVAL;
2601 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2602 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2603 lys_nodetype2str(schema->nodetype), __func__);
2604 return LY_EINVAL;
2605 }
2606
Michal Vasko62ed12d2020-05-21 10:08:25 +02002607 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2608 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002609 if (match) {
2610 *match = NULL;
2611 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002612 return LY_ENOTFOUND;
2613 }
2614
Michal Vaskof03ed032020-03-04 13:31:44 +01002615 if (key_or_value && !val_len) {
2616 val_len = strlen(key_or_value);
2617 }
2618
Michal Vasko90932a92020-02-12 14:33:03 +01002619 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002620 switch (schema->nodetype) {
2621 case LYS_CONTAINER:
2622 case LYS_ANYXML:
2623 case LYS_ANYDATA:
2624 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01002625 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01002626 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002627 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002628 /* find it based on schema only */
2629 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002630 break;
2631 case LYS_LEAFLIST:
2632 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002633 LY_CHECK_RET(lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002634 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002635 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002636 if (schema->nodetype == LYS_LIST) {
2637 /* target used attributes: schema, hash, child (all keys) */
Michal Vaskod3678892020-05-21 10:06:58 +02002638 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, LYD_JSON, 1, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002639 }
2640
2641 /* find it */
2642 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002643 break;
2644 default:
2645 /* unreachable */
2646 LOGINT(schema->module->ctx);
2647 return LY_EINT;
2648 }
2649
Michal Vaskoe444f752020-02-10 12:20:06 +01002650 lyd_free_tree(target);
2651 return rc;
2652}
Michal Vaskoccc02342020-05-21 10:09:21 +02002653
2654API LY_ERR
2655lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2656{
2657 LY_ERR ret = LY_SUCCESS;
2658 struct lyxp_set xp_set;
2659 struct lyxp_expr *exp;
2660 uint32_t i;
2661
2662 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2663
2664 memset(&xp_set, 0, sizeof xp_set);
2665
2666 /* compile expression */
2667 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath);
2668 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
2669
2670 /* evaluate expression */
2671 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
2672 LY_CHECK_GOTO(ret, cleanup);
2673
2674 /* allocate return set */
2675 *set = ly_set_new();
2676 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2677
2678 /* transform into ly_set */
2679 if (xp_set.type == LYXP_SET_NODE_SET) {
2680 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
2681 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2682 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2683 (*set)->size = xp_set.used;
2684
2685 for (i = 0; i < xp_set.used; ++i) {
2686 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2687 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
2688 }
2689 }
2690 }
2691
2692cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002693 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002694 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
2695 return ret;
2696}