blob: b35befc7fb7fb2f2f964bf84dadb510d189f46ea [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 */
14
15#include "common.h"
16
Radek Krejci084289f2019-07-09 17:35:30 +020017#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdarg.h>
22#include <stdint.h>
23#include <string.h>
24#include <unistd.h>
25
26#include "log.h"
27#include "tree.h"
28#include "tree_data.h"
29#include "tree_data_internal.h"
Michal Vaskod3678892020-05-21 10:06:58 +020030#include "tree_schema_internal.h"
Michal Vasko90932a92020-02-12 14:33:03 +010031#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "tree_schema.h"
Michal Vaskod3678892020-05-21 10:06:58 +020033#include "xpath.h"
Michal Vasko52927e22020-03-16 17:26:14 +010034#include "xml.h"
Radek Krejci38d85362019-09-05 16:26:38 +020035#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010036#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037
Michal Vaskoe444f752020-02-10 12:20:06 +010038struct ly_keys {
39 char *str;
40 struct {
41 const struct lysc_node_leaf *schema;
42 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010043 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010044 } *keys;
45 size_t key_count;
46};
47
Radek Krejci084289f2019-07-09 17:35:30 +020048LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010049lyd_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 +010050 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020051{
Michal Vasko90932a92020-02-12 14:33:03 +010052 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020053 struct ly_err_item *err = NULL;
54 struct ly_ctx *ctx;
55 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020056 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010057 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020058 assert(node);
59
60 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020061
Radek Krejci73dead22019-07-11 16:46:16 +020062 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020063 if (!second) {
64 node->value.realtype = type;
65 }
Michal Vasko90932a92020-02-12 14:33:03 +010066 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vaskof03ed032020-03-04 13:31:44 +010067 tree ? (void *)node : (void *)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +020068 &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010069 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020070 if (err) {
Michal Vaskofea12c62020-03-30 11:00:15 +020071 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
Radek Krejci73dead22019-07-11 16:46:16 +020072 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020073 }
Radek Krejci73dead22019-07-11 16:46:16 +020074 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010075 } else if (dynamic) {
76 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020077 }
78
79error:
80 return ret;
81}
82
Michal Vasko90932a92020-02-12 14:33:03 +010083/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
84static LY_ERR
85lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
86 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
87{
88 LY_ERR ret = LY_SUCCESS;
89 struct ly_err_item *err = NULL;
90 struct ly_ctx *ctx;
91 struct lysc_type *type;
92 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
93
94 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
95
96 ctx = schema->module->ctx;
97 type = ((struct lysc_node_leaf *)schema)->type;
98 val->realtype = type;
99 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
100 val, NULL, &err);
101 if (ret == LY_EINCOMPLETE) {
102 /* this is fine, we do not need it resolved */
103 ret = LY_SUCCESS;
104 } else if (ret && err) {
105 ly_err_print(err);
106 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
107 ly_err_free(err);
108 }
109 if (!ret && dynamic) {
110 *dynamic = 0;
111 }
112
113 return ret;
114}
115
Radek Krejci38d85362019-09-05 16:26:38 +0200116LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100117lyd_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 +0100118 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100119 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200120{
Michal Vasko90932a92020-02-12 14:33:03 +0100121 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200122 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200123 struct lyext_metadata *ant;
124 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100125 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200126
Michal Vasko9f96a052020-03-10 09:41:45 +0100127 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100128
Michal Vasko9f96a052020-03-10 09:41:45 +0100129 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200130
131 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100132 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200133 }
Michal Vasko90932a92020-02-12 14:33:03 +0100134 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100135 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100136 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200137 if (err) {
138 ly_err_print(err);
139 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
140 ly_err_free(err);
141 }
142 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100143 } else if (dynamic) {
144 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200145 }
146
147error:
148 return ret;
149}
150
Radek Krejci084289f2019-07-09 17:35:30 +0200151API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100152lys_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 +0200153 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
154{
155 LY_ERR rc = LY_SUCCESS;
156 struct ly_err_item *err = NULL;
157 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200158
159 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
160
161 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
162 LOGARG(ctx, node);
163 return LY_EINVAL;
164 }
165
166 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200167 /* just validate, no storing of enything */
168 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
169 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
170 if (rc == LY_EINCOMPLETE) {
171 /* actually success since we do not provide the context tree and call validation with
172 * LY_TYPE_OPTS_INCOMPLETE_DATA */
173 rc = LY_SUCCESS;
174 } else if (rc && err) {
175 if (ctx) {
176 /* log only in case the ctx was provided as input parameter */
177 ly_err_print(err);
178 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200179 }
Radek Krejci73dead22019-07-11 16:46:16 +0200180 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200181 }
182
183 return rc;
184}
185
186API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100187lyd_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 +0100188 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 +0200189{
190 LY_ERR rc;
191 struct ly_err_item *err = NULL;
192 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100193 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200194
195 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
196
197 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200198 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100199 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200200 NULL, NULL, &err);
201 if (rc == LY_EINCOMPLETE) {
202 return rc;
203 } else if (rc) {
204 if (err) {
205 if (ctx) {
206 ly_err_print(err);
207 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200208 }
Radek Krejci73dead22019-07-11 16:46:16 +0200209 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200210 }
Radek Krejci73dead22019-07-11 16:46:16 +0200211 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200212 }
213
214 return LY_SUCCESS;
215}
216
217API LY_ERR
218lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100219 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 +0200220{
221 LY_ERR ret = LY_SUCCESS, rc;
222 struct ly_err_item *err = NULL;
223 struct ly_ctx *ctx;
224 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200225 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100226 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200227
228 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
229
230 ctx = node->schema->module->ctx;
231 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200232 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 +0100233 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200234 if (rc == LY_EINCOMPLETE) {
235 ret = rc;
236 /* continue with comparing, just remember what to return if storing is ok */
237 } else if (rc) {
238 /* value to compare is invalid */
239 ret = LY_EINVAL;
240 if (err) {
241 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200242 }
Radek Krejci73dead22019-07-11 16:46:16 +0200243 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200244 }
245
246 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200247 if (type->plugin->compare(&node->value, &data)) {
248 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
249 ret = LY_EVALID;
250 }
Radek Krejci084289f2019-07-09 17:35:30 +0200251
252cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200253 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200254
255 return ret;
256}
257
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200258API const char *
259lyd_value2str(const struct lyd_node_term *node, int *dynamic)
260{
261 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
262
263 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
264}
265
266API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100267lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200268{
Michal Vasko9f96a052020-03-10 09:41:45 +0100269 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200270
Michal Vasko9f96a052020-03-10 09:41:45 +0100271 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200272}
273
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200274API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100275lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200276{
Radek Krejcie7b95092019-05-15 11:03:07 +0200277 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200278#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200280#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200281
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200282 LY_CHECK_ARG_RET(ctx, ctx, NULL);
283
Michal Vasko5b37a352020-03-06 13:38:33 +0100284 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
285 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
286 return NULL;
287 }
288
Michal Vaskoa3881362020-01-21 15:57:35 +0100289#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200291 /* first item in trees is mandatory - the RPC/action request */
292 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
293 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
294 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
295 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200296 return NULL;
297 }
298 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejcie7b95092019-05-15 11:03:07 +0200300 if (options & LYD_OPT_DATA_TEMPLATE) {
301 yang_data_name = va_arg(ap, const char *);
302 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200303#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200304
305 if (!format) {
306 /* TODO try to detect format from the content */
307 }
308
309 switch (format) {
310 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100311 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200312 break;
313#if 0
314 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200315 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 break;
317 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200318 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 break;
320#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100321 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200322 LOGINT(ctx);
323 break;
324 }
325
Radek Krejcie7b95092019-05-15 11:03:07 +0200326 return result;
327}
328
329API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100330lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200331{
332 struct lyd_node *result;
333 size_t length;
334 char *addr;
335
336 LY_CHECK_ARG_RET(ctx, ctx, NULL);
337 if (fd < 0) {
338 LOGARG(ctx, fd);
339 return NULL;
340 }
341
342 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100343 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200344 if (addr) {
345 ly_munmap(addr, length);
346 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200347
348 return result;
349}
350
351API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100352lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200353{
354 int fd;
355 struct lyd_node *result;
356 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200357
358 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
359
360 fd = open(path, O_RDONLY);
361 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
362
363 if (!format) {
364 /* unknown format - try to detect it from filename's suffix */
365 len = strlen(path);
366
367 /* ignore trailing whitespaces */
368 for (; len > 0 && isspace(path[len - 1]); len--);
369
370 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
371 format = LYD_XML;
372#if 0
373 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
374 format = LYD_JSON;
375 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
376 format = LYD_LYB;
377#endif
378 } /* else still unknown, try later to detect it from the content */
379 }
380
Michal Vaskoa3881362020-01-21 15:57:35 +0100381 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200382 close(fd);
383
384 return result;
385}
Radek Krejci084289f2019-07-09 17:35:30 +0200386
Michal Vasko90932a92020-02-12 14:33:03 +0100387LY_ERR
388lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
389 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
390{
391 LY_ERR ret;
392 struct lyd_node_term *term;
393
Michal Vasko9b368d32020-02-14 13:53:31 +0100394 assert(schema->nodetype & LYD_NODE_TERM);
395
Michal Vasko90932a92020-02-12 14:33:03 +0100396 term = calloc(1, sizeof *term);
397 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
398
399 term->schema = schema;
400 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100401 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100402
403 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
404 if (ret && (ret != LY_EINCOMPLETE)) {
405 free(term);
406 return ret;
407 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100408 lyd_hash((struct lyd_node *)term);
409
410 *node = (struct lyd_node *)term;
411 return ret;
412}
413
414LY_ERR
415lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
416{
417 LY_ERR ret;
418 struct lyd_node_term *term;
419 struct lysc_type *type;
420
421 assert(schema->nodetype & LYD_NODE_TERM);
422
423 term = calloc(1, sizeof *term);
424 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
425
426 term->schema = schema;
427 term->prev = (struct lyd_node *)term;
428 term->flags = LYD_NEW;
429
430 type = ((struct lysc_node_leaf *)schema)->type;
431 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
432 if (ret) {
433 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
434 free(term);
435 return ret;
436 }
437 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100438
439 *node = (struct lyd_node *)term;
440 return ret;
441}
442
443LY_ERR
444lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
445{
446 struct lyd_node_inner *in;
447
Michal Vasko9b368d32020-02-14 13:53:31 +0100448 assert(schema->nodetype & LYD_NODE_INNER);
449
Michal Vasko90932a92020-02-12 14:33:03 +0100450 in = calloc(1, sizeof *in);
451 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
452
453 in->schema = schema;
454 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100455 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100456
Michal Vasko9b368d32020-02-14 13:53:31 +0100457 /* do not hash list with keys, we need them for the hash */
458 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
459 lyd_hash((struct lyd_node *)in);
460 }
Michal Vasko90932a92020-02-12 14:33:03 +0100461
462 *node = (struct lyd_node *)in;
463 return LY_SUCCESS;
464}
465
466static void
467ly_keys_clean(struct ly_keys *keys)
468{
469 size_t i;
470
471 for (i = 0; i < keys->key_count; ++i) {
472 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
473 }
474 free(keys->str);
475 free(keys->keys);
476}
477
478static char *
479ly_keys_parse_next(char **next_key, char **key_name)
480{
481 char *ptr, *ptr2, *val, quot;
482
483 ptr = *next_key;
484
485 /* "[" */
486 LY_CHECK_GOTO(ptr[0] != '[', error);
487 ++ptr;
488
489 /* key name */
490 ptr2 = strchr(ptr, '=');
491 LY_CHECK_GOTO(!ptr2, error);
492
493 *key_name = ptr;
494 ptr2[0] = '\0';
495
496 /* \0, was '=' */
497 ptr = ptr2 + 1;
498
499 /* quote */
500 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
501 quot = ptr[0];
502 ++ptr;
503
504 /* value, terminate it */
505 val = ptr;
506 ptr2 = strchr(ptr, quot);
507 LY_CHECK_GOTO(!ptr2, error);
508 ptr2[0] = '\0';
509
510 /* \0, was quote */
511 ptr = ptr2 + 1;
512
513 /* "]" */
514 LY_CHECK_GOTO(ptr[0] != ']', error);
515 ++ptr;
516
517 *next_key = ptr;
518 return val;
519
520error:
521 *next_key = ptr;
522 return NULL;
523}
524
Michal Vaskod3678892020-05-21 10:06:58 +0200525/* fill keys structure that is expected to be zeroed and must always be cleaned (even on error);
526 * if store is set, fill also each val */
Michal Vasko90932a92020-02-12 14:33:03 +0100527static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200528ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, int log,
529 struct ly_keys *keys)
Michal Vasko90932a92020-02-12 14:33:03 +0100530{
531 LY_ERR ret = LY_SUCCESS;
532 char *next_key, *name;
533 const struct lysc_node *key;
534 size_t i;
535
536 assert(list->nodetype == LYS_LIST);
537
Michal Vaskof03ed032020-03-04 13:31:44 +0100538 if (!keys_str) {
539 /* nothing to parse */
540 return LY_SUCCESS;
541 }
542
543 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100544 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
545
546 next_key = keys->str;
547 while (next_key[0]) {
548 /* new key */
549 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
550 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
551
552 /* fill */
553 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
554 if (!keys->keys[keys->key_count].value) {
Michal Vaskod3678892020-05-21 10:06:58 +0200555 if (log) {
556 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
557 }
Michal Vasko90932a92020-02-12 14:33:03 +0100558 ret = LY_EINVAL;
559 goto cleanup;
560 }
561
562 /* find schema node */
563 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
564 if (!key) {
Michal Vaskod3678892020-05-21 10:06:58 +0200565 if (log) {
566 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
567 }
Michal Vasko90932a92020-02-12 14:33:03 +0100568 ret = LY_EINVAL;
569 goto cleanup;
570 }
571 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
572
573 /* check that we do not have it already */
574 for (i = 0; i < keys->key_count; ++i) {
575 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
Michal Vaskod3678892020-05-21 10:06:58 +0200576 if (log) {
577 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
578 }
Michal Vasko90932a92020-02-12 14:33:03 +0100579 ret = LY_EINVAL;
580 goto cleanup;
581 }
582 }
583
584 if (store) {
585 /* store the value */
586 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
587 lydjson_resolve_prefix, NULL, LYD_JSON);
588 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae420e92020-05-21 10:00:40 +0200589 } else {
590 memset(&keys->keys[keys->key_count].val, 0, sizeof keys->keys[keys->key_count].val);
Michal Vasko90932a92020-02-12 14:33:03 +0100591 }
592
593 /* another valid key */
594 ++keys->key_count;
595 }
596
597cleanup:
Michal Vasko90932a92020-02-12 14:33:03 +0100598 return ret;
599}
600
601LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200602lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, LYD_FORMAT keys_format, int log,
603 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100604{
605 LY_ERR ret = LY_SUCCESS;
606 const struct lysc_node *key_s;
607 struct lyd_node *list = NULL, *key;
608 struct ly_keys keys = {0};
609 size_t i;
610
Michal Vaskod3678892020-05-21 10:06:58 +0200611 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS) && (keys_format != LYD_XML));
Michal Vasko90932a92020-02-12 14:33:03 +0100612
613 /* parse keys */
Michal Vaskod3678892020-05-21 10:06:58 +0200614 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, log, &keys), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100615
616 /* create list */
617 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
618
619 /* everything was checked except that all keys are set */
620 i = 0;
621 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
622 ++i;
623 }
624 if (i != keys.key_count) {
Michal Vaskod3678892020-05-21 10:06:58 +0200625 if (log) {
626 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
627 }
Michal Vasko90932a92020-02-12 14:33:03 +0100628 ret = LY_EINVAL;
629 goto cleanup;
630 }
631
632 /* create and insert all the keys */
633 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +0200634 if (keys_format == LYD_JSON) {
635 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
636 NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
637 } else {
638 assert(keys_format == LYD_SCHEMA);
639 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
640 NULL, lys_resolve_prefix, NULL, LYD_SCHEMA, &key);
641 }
642 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100643 lyd_insert_node(list, NULL, key);
644 }
645
Michal Vasko9b368d32020-02-14 13:53:31 +0100646 /* hash having all the keys */
647 lyd_hash(list);
648
Michal Vasko90932a92020-02-12 14:33:03 +0100649 /* success */
650 *node = list;
651 list = NULL;
652
653cleanup:
654 lyd_free_tree(list);
655 ly_keys_clean(&keys);
656 return ret;
657}
658
659LY_ERR
660lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
661{
662 struct lyd_node_any *any;
663
Michal Vasko9b368d32020-02-14 13:53:31 +0100664 assert(schema->nodetype & LYD_NODE_ANY);
665
Michal Vasko90932a92020-02-12 14:33:03 +0100666 any = calloc(1, sizeof *any);
667 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
668
669 any->schema = schema;
670 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100671 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100672
673 any->value.xml = value;
674 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100675 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100676
677 *node = (struct lyd_node *)any;
678 return LY_SUCCESS;
679}
680
Michal Vasko52927e22020-03-16 17:26:14 +0100681LY_ERR
682lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
683 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
684 const char *ns, struct lyd_node **node)
685{
686 struct lyd_node_opaq *opaq;
687
688 assert(ctx && name && name_len && ns);
689
690 if (!value_len) {
691 value = "";
692 }
693
694 opaq = calloc(1, sizeof *opaq);
695 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
696
697 opaq->prev = (struct lyd_node *)opaq;
698
699 opaq->name = lydict_insert(ctx, name, name_len);
700 opaq->format = format;
701 if (pref_len) {
702 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
703 }
704 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
705 opaq->val_prefs = val_prefs;
706 if (dynamic && *dynamic) {
707 opaq->value = lydict_insert_zc(ctx, (char *)value);
708 *dynamic = 0;
709 } else {
710 opaq->value = lydict_insert(ctx, value, value_len);
711 }
712 opaq->ctx = ctx;
713
714 *node = (struct lyd_node *)opaq;
715 return LY_SUCCESS;
716}
717
Michal Vasko013a8182020-03-03 10:46:53 +0100718API struct lyd_node *
719lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
720{
721 struct lyd_node *ret = NULL;
722 const struct lysc_node *schema;
723 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
724
725 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
726
Michal Vaskof03ed032020-03-04 13:31:44 +0100727 if (!module) {
728 module = parent->schema->module;
729 }
730
Michal Vasko1bf09392020-03-27 12:38:10 +0100731 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 +0100732 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL);
733
734 if (!lyd_create_inner(schema, &ret) && parent) {
735 lyd_insert_node(parent, NULL, ret);
736 }
737 return ret;
738}
739
740API struct lyd_node *
741lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
742{
743 struct lyd_node *ret = NULL, *key;
744 const struct lysc_node *schema, *key_s;
745 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
746 va_list ap;
747 const char *key_val;
748 LY_ERR rc = LY_SUCCESS;
749
750 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
751
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 if (!module) {
753 module = parent->schema->module;
754 }
755
Michal Vasko013a8182020-03-03 10:46:53 +0100756 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
757 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
758
759 /* create list inner node */
760 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
761
762 va_start(ap, name);
763
764 /* create and insert all the keys */
765 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
766 key_val = va_arg(ap, const char *);
767
Michal Vaskof03ed032020-03-04 13:31:44 +0100768 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
769 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100770 lyd_insert_node(ret, NULL, key);
771 }
772
773 /* hash having all the keys */
774 lyd_hash(ret);
775
776 if (parent) {
777 lyd_insert_node(parent, NULL, ret);
778 }
779
780cleanup:
781 if (rc) {
782 lyd_free_tree(ret);
783 ret = NULL;
784 }
785 va_end(ap);
786 return ret;
787}
788
789API struct lyd_node *
790lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
791{
792 struct lyd_node *ret = NULL;
793 const struct lysc_node *schema;
794 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
795
796 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
797
Michal Vaskof03ed032020-03-04 13:31:44 +0100798 if (!module) {
799 module = parent->schema->module;
800 }
801
Michal Vasko013a8182020-03-03 10:46:53 +0100802 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
803 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
804
Michal Vaskod3678892020-05-21 10:06:58 +0200805 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, LYD_JSON, 1, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100806 lyd_insert_node(parent, NULL, ret);
807 }
808 return ret;
809}
810
811API struct lyd_node *
812lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
813{
814 struct lyd_node *ret = NULL;
815 const struct lysc_node *schema;
816 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
817
818 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
819
Michal Vaskof03ed032020-03-04 13:31:44 +0100820 if (!module) {
821 module = parent->schema->module;
822 }
823
Michal Vasko013a8182020-03-03 10:46:53 +0100824 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
825 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
826
Michal Vaskof03ed032020-03-04 13:31:44 +0100827 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
828 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100829 lyd_insert_node(parent, NULL, ret);
830 }
831 return ret;
832}
833
834API struct lyd_node *
835lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
836 LYD_ANYDATA_VALUETYPE value_type)
837{
838 struct lyd_node *ret = NULL;
839 const struct lysc_node *schema;
840 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
841
842 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
843
Michal Vaskof03ed032020-03-04 13:31:44 +0100844 if (!module) {
845 module = parent->schema->module;
846 }
847
Michal Vasko013a8182020-03-03 10:46:53 +0100848 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
849 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
850
851 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
852 lyd_insert_node(parent, NULL, ret);
853 }
854 return ret;
855}
856
Michal Vasko90932a92020-02-12 14:33:03 +0100857struct lyd_node *
858lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
859{
860 const struct lysc_node *prev_key;
861 struct lyd_node *match = NULL;
862
863 if (!first_sibling) {
864 return NULL;
865 }
866
867 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
868 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
869 }
870
871 return match;
872}
873
874/**
875 * @brief Insert node after a sibling.
876 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100877 * Handles inserting into NP containers and key-less lists.
878 *
Michal Vasko90932a92020-02-12 14:33:03 +0100879 * @param[in] sibling Sibling to insert after.
880 * @param[in] node Node to insert.
881 */
882static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100883lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100884{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100885 struct lyd_node_inner *par;
886
Michal Vasko90932a92020-02-12 14:33:03 +0100887 assert(!node->next && (node->prev == node));
888
889 node->next = sibling->next;
890 node->prev = sibling;
891 sibling->next = node;
892 if (node->next) {
893 /* sibling had a succeeding node */
894 node->next->prev = node;
895 } else {
896 /* sibling was last, find first sibling and change its prev */
897 if (sibling->parent) {
898 sibling = sibling->parent->child;
899 } else {
900 for (; sibling->prev->next != node; sibling = sibling->prev);
901 }
902 sibling->prev = node;
903 }
904 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100905
Michal Vasko9f96a052020-03-10 09:41:45 +0100906 for (par = node->parent; par; par = par->parent) {
907 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
908 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100909 par->flags &= ~LYD_DEFAULT;
910 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100911 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
912 /* rehash key-less list */
913 lyd_hash((struct lyd_node *)par);
914 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100915 }
916
917 /* insert into hash table */
918 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100919}
920
921/**
922 * @brief Insert node before a sibling.
923 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100924 * Handles inserting into NP containers and key-less lists.
925 *
Michal Vasko90932a92020-02-12 14:33:03 +0100926 * @param[in] sibling Sibling to insert before.
927 * @param[in] node Node to insert.
928 */
929static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100930lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100931{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100932 struct lyd_node_inner *par;
933
Michal Vasko90932a92020-02-12 14:33:03 +0100934 assert(!node->next && (node->prev == node));
935
936 node->next = sibling;
937 /* covers situation of sibling being first */
938 node->prev = sibling->prev;
939 sibling->prev = node;
940 if (node->prev->next) {
941 /* sibling had a preceding node */
942 node->prev->next = node;
943 } else if (sibling->parent) {
944 /* sibling was first and we must also change parent child pointer */
945 sibling->parent->child = node;
946 }
947 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100948
Michal Vasko9f96a052020-03-10 09:41:45 +0100949 for (par = node->parent; par; par = par->parent) {
950 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
951 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100952 par->flags &= ~LYD_DEFAULT;
953 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100954 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
955 /* rehash key-less list */
956 lyd_hash((struct lyd_node *)par);
957 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100958 }
959
960 /* insert into hash table */
961 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100962}
963
964/**
965 * @brief Insert node as the last child of a parent.
966 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100967 * Handles inserting into NP containers and key-less lists.
968 *
Michal Vasko90932a92020-02-12 14:33:03 +0100969 * @param[in] parent Parent to insert into.
970 * @param[in] node Node to insert.
971 */
972static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100973lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100974{
975 struct lyd_node_inner *par;
976
Michal Vasko0249f7c2020-03-05 16:36:40 +0100977 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100978 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100979
980 par = (struct lyd_node_inner *)parent;
981
982 if (!par->child) {
983 par->child = node;
984 } else {
985 node->prev = par->child->prev;
986 par->child->prev->next = node;
987 par->child->prev = node;
988 }
989 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100990
Michal Vasko9f96a052020-03-10 09:41:45 +0100991 for (; par; par = par->parent) {
992 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
993 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100994 par->flags &= ~LYD_DEFAULT;
995 }
Michal Vasko52927e22020-03-16 17:26:14 +0100996 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100997 /* rehash key-less list */
998 lyd_hash((struct lyd_node *)par);
999 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001000 }
1001
1002 /* insert into hash table */
1003 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001004}
1005
1006void
Michal Vasko9b368d32020-02-14 13:53:31 +01001007lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001008{
Michal Vasko9b368d32020-02-14 13:53:31 +01001009 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001010 const struct lysc_node *skey = NULL;
1011 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001012
Michal Vasko52927e22020-03-16 17:26:14 +01001013 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001014
1015 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1016 parent = (struct lyd_node *)(*first_sibling)->parent;
1017 }
Michal Vasko90932a92020-02-12 14:33:03 +01001018
1019 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001020 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001021 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001022 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1023 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001024 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001025 } else if (lyd_node_children(parent)) {
Radek Krejcidae0ee82020-05-06 16:53:24 +02001026 lyd_insert_before_node(lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001027 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001028 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001029 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001030
1031 /* hash list if all its keys were added */
1032 assert(parent->schema->nodetype == LYS_LIST);
Radek Krejcidae0ee82020-05-06 16:53:24 +02001033 anchor = lyd_node_children(parent);
Michal Vasko9f96a052020-03-10 09:41:45 +01001034 has_keys = 1;
1035 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1036 if (!anchor || (anchor->schema != skey)) {
1037 /* key missing */
1038 has_keys = 0;
1039 break;
1040 }
1041
1042 anchor = anchor->next;
1043 }
1044 if (has_keys) {
1045 lyd_hash(parent);
1046 }
1047
Michal Vasko90932a92020-02-12 14:33:03 +01001048 } else {
1049 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001050 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001051 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001052 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001053 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001054 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001055 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001056 anchor = anchor->prev;
1057 }
1058
Michal Vaskoc193ce92020-03-06 11:04:48 +01001059 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001060 /* insert after last sibling from this module */
1061 lyd_insert_after_node(anchor, node);
1062 } else {
1063 /* no data from this module, insert at the last position */
1064 lyd_insert_after_node((*first_sibling)->prev, node);
1065 }
Michal Vasko90932a92020-02-12 14:33:03 +01001066 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001067 /* the only sibling */
1068 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001069 }
Michal Vasko90932a92020-02-12 14:33:03 +01001070}
1071
Michal Vaskof03ed032020-03-04 13:31:44 +01001072static LY_ERR
1073lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1074{
1075 const struct lysc_node *par2;
1076
1077 assert(schema);
1078
1079 /* adjust parent first */
1080 while (parent && (parent->nodetype & (LYS_CASE | LYS_CHOICE))) {
1081 parent = parent->parent;
1082 }
1083
1084 /* find schema parent */
1085 for (par2 = schema->parent; par2 && (par2->nodetype & (LYS_CASE | LYS_CHOICE)); par2 = par2->parent);
1086
1087 if (parent) {
1088 /* inner node */
1089 if (par2 != parent) {
1090 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1091 return LY_EINVAL;
1092 }
1093 } else {
1094 /* top-level node */
1095 if (par2) {
1096 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1097 return LY_EINVAL;
1098 }
1099 }
1100
1101 return LY_SUCCESS;
1102}
1103
1104API LY_ERR
1105lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1106{
1107 struct lyd_node *iter;
1108
1109 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1110
1111 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1112
1113 if (node->schema->flags & LYS_KEY) {
1114 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1115 return LY_EINVAL;
1116 }
1117
1118 if (node->parent || node->prev->next) {
1119 lyd_unlink_tree(node);
1120 }
1121
1122 while (node) {
1123 iter = node->next;
1124 lyd_unlink_tree(node);
1125 lyd_insert_node(parent, NULL, node);
1126 node = iter;
1127 }
1128 return LY_SUCCESS;
1129}
1130
1131API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001132lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1133{
1134 struct lyd_node *iter;
1135
1136 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1137
1138 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1139
1140 if (node->schema->flags & LYS_KEY) {
1141 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1142 return LY_EINVAL;
1143 }
1144
1145 if (node->parent || node->prev->next) {
1146 lyd_unlink_tree(node);
1147 }
1148
1149 while (node) {
1150 iter = node->next;
1151 lyd_unlink_tree(node);
1152 lyd_insert_node(NULL, &sibling, node);
1153 node = iter;
1154 }
1155 return LY_SUCCESS;
1156}
1157
Michal Vasko0249f7c2020-03-05 16:36:40 +01001158static LY_ERR
1159lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1160{
1161 if (sibling->parent) {
1162 /* nested, we do not care for the order */
1163 return LY_SUCCESS;
1164 }
1165
1166 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001167 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1168 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001169 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 +01001170 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001171 return LY_EINVAL;
1172 }
1173
Michal Vaskoc193ce92020-03-06 11:04:48 +01001174 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1175 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001176 /* inserting before/after its module data */
1177 return LY_SUCCESS;
1178 }
1179 }
1180
1181 /* find first sibling */
1182 while (sibling->prev->next) {
1183 sibling = sibling->prev;
1184 }
1185
1186 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001187 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001188 /* inserting before its module data */
1189 return LY_SUCCESS;
1190 }
1191 }
1192
1193 /* check there are no data of this module */
1194 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001195 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001196 /* some data of this module found */
1197 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001198 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001199 return LY_EINVAL;
1200 }
1201 }
1202
1203 return LY_SUCCESS;
1204}
1205
Michal Vaskob1b5c262020-03-05 14:29:47 +01001206API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001207lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1208{
1209 struct lyd_node *iter;
1210
1211 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1212
1213 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1214
1215 if (node->schema->flags & LYS_KEY) {
1216 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1217 return LY_EINVAL;
1218 } else if (sibling->schema->flags & LYS_KEY) {
1219 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1220 return LY_EINVAL;
1221 }
1222
Michal Vasko0249f7c2020-03-05 16:36:40 +01001223 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1224
Michal Vaskof03ed032020-03-04 13:31:44 +01001225 if (node->parent || node->prev->next) {
1226 lyd_unlink_tree(node);
1227 }
1228
1229 /* insert in reverse order to get the original order */
1230 node = node->prev;
1231 while (node) {
1232 iter = node->prev;
1233 lyd_unlink_tree(node);
1234
1235 lyd_insert_before_node(sibling, node);
1236 /* move the anchor accordingly */
1237 sibling = node;
1238
1239 node = (iter == node) ? NULL : iter;
1240 }
1241 return LY_SUCCESS;
1242}
1243
1244API LY_ERR
1245lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1246{
1247 struct lyd_node *iter;
1248
1249 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1250
1251 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1252
1253 if (node->schema->flags & LYS_KEY) {
1254 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1255 return LY_EINVAL;
1256 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1257 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1258 return LY_EINVAL;
1259 }
1260
Michal Vasko0249f7c2020-03-05 16:36:40 +01001261 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1262
Michal Vaskof03ed032020-03-04 13:31:44 +01001263 if (node->parent || node->prev->next) {
1264 lyd_unlink_tree(node);
1265 }
1266
1267 while (node) {
1268 iter = node->next;
1269 lyd_unlink_tree(node);
1270
1271 lyd_insert_after_node(sibling, node);
1272 /* move the anchor accordingly */
1273 sibling = node;
1274
1275 node = iter;
1276 }
1277 return LY_SUCCESS;
1278}
1279
1280API void
1281lyd_unlink_tree(struct lyd_node *node)
1282{
1283 struct lyd_node *iter;
1284
1285 if (!node) {
1286 return;
1287 }
1288
1289 /* unlink from siblings */
1290 if (node->prev->next) {
1291 node->prev->next = node->next;
1292 }
1293 if (node->next) {
1294 node->next->prev = node->prev;
1295 } else {
1296 /* unlinking the last node */
1297 if (node->parent) {
1298 iter = node->parent->child;
1299 } else {
1300 iter = node->prev;
1301 while (iter->prev != node) {
1302 iter = iter->prev;
1303 }
1304 }
1305 /* update the "last" pointer from the first node */
1306 iter->prev = node->prev;
1307 }
1308
1309 /* unlink from parent */
1310 if (node->parent) {
1311 if (node->parent->child == node) {
1312 /* the node is the first child */
1313 node->parent->child = node->next;
1314 }
1315
1316 lyd_unlink_hash(node);
1317
1318 /* check for keyless list and update its hash */
1319 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001320 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001321 lyd_hash(iter);
1322 }
1323 }
1324
1325 node->parent = NULL;
1326 }
1327
1328 node->next = NULL;
1329 node->prev = node;
1330}
1331
Michal Vasko90932a92020-02-12 14:33:03 +01001332LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001333lyd_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 +01001334 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 +01001335 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001336{
1337 LY_ERR ret;
1338 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001339 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001340 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001341
Michal Vasko9f96a052020-03-10 09:41:45 +01001342 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001343
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001344 LY_ARRAY_FOR(mod->compiled->exts, u) {
1345 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1346 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001347 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001348 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001349 break;
1350 }
1351 }
1352 if (!ant) {
1353 /* attribute is not defined as a metadata annotation (RFC 7952) */
1354 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1355 mod->name, name_len, name);
1356 return LY_EINVAL;
1357 }
1358
Michal Vasko9f96a052020-03-10 09:41:45 +01001359 mt = calloc(1, sizeof *mt);
1360 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1361 mt->parent = parent;
1362 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001363 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 +01001364 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001365 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001366 return ret;
1367 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001368 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001369
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001370 /* insert as the last attribute */
1371 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001372 if (parent->meta) {
1373 for (last = parent->meta; last->next; last = last->next);
1374 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001375 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001376 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001377 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001378 } else if (*meta) {
1379 for (last = *meta; last->next; last = last->next);
1380 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001381 }
1382
1383 /* remove default flags from NP containers */
1384 while (parent && (parent->flags & LYD_DEFAULT)) {
1385 parent->flags &= ~LYD_DEFAULT;
1386 parent = (struct lyd_node *)parent->parent;
1387 }
1388
Michal Vasko9f96a052020-03-10 09:41:45 +01001389 if (meta) {
1390 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001391 }
1392 return ret;
1393}
1394
Michal Vasko52927e22020-03-16 17:26:14 +01001395LY_ERR
1396ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1397 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1398 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1399{
1400 struct ly_attr *at, *last;
1401 struct lyd_node_opaq *opaq;
1402
1403 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1404 assert(name && name_len);
1405 assert((prefix_len && ns) || (!prefix_len && !ns));
1406
1407 if (!value_len) {
1408 value = "";
1409 }
1410
1411 at = calloc(1, sizeof *at);
1412 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1413 at->parent = (struct lyd_node_opaq *)parent;
1414 at->name = lydict_insert(ctx, name, name_len);
1415 if (dynamic && *dynamic) {
1416 at->value = lydict_insert_zc(ctx, (char *)value);
1417 *dynamic = 0;
1418 } else {
1419 at->value = lydict_insert(ctx, value, value_len);
1420 }
1421
1422 at->format = format;
1423 at->val_prefs = val_prefs;
1424 if (ns) {
1425 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1426 at->prefix.ns = lydict_insert(ctx, ns, 0);
1427 }
1428
1429 /* insert as the last attribute */
1430 if (parent) {
1431 opaq = (struct lyd_node_opaq *)parent;
1432 if (opaq->attr) {
1433 for (last = opaq->attr; last->next; last = last->next);
1434 last->next = at;
1435 } else {
1436 opaq->attr = at;
1437 }
1438 } else if (*attr) {
1439 for (last = *attr; last->next; last = last->next);
1440 last->next = at;
1441 }
1442
1443 if (attr) {
1444 *attr = at;
1445 }
1446 return LY_SUCCESS;
1447}
1448
Radek Krejci084289f2019-07-09 17:35:30 +02001449API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001450lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001451{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001452 LY_ARRAY_SIZE_TYPE u, v;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001453 const struct lyd_node *start_sibling;
Michal Vaskoe444f752020-02-10 12:20:06 +01001454 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001455 uint64_t pos = 1;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001456 int match;
Radek Krejci084289f2019-07-09 17:35:30 +02001457
Michal Vaskof03ed032020-03-04 13:31:44 +01001458 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001459
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001460 /* first iteration */
1461 start_sibling = tree;
1462 u = 0;
1463 while (u < LY_ARRAY_SIZE(path)) {
1464 /* find next node instance */
Michal Vasko84c9f1b2020-05-14 12:08:11 +02001465 if (start_sibling && !start_sibling->prev->next && !(path[u].node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
1466 /* starting from the beginning using hashes */
1467 lyd_find_sibling_val(start_sibling, path[u].node, NULL, 0, &node);
1468 } else {
1469 /* next matching sibling */
1470 lyd_find_sibling_next2(start_sibling, path[u].node, NULL, 0, &node);
1471 }
Radek Krejci084289f2019-07-09 17:35:30 +02001472 if (!node) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001473 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001474 }
1475
1476 /* check predicate if any */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001477 match = 1;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001478 LY_ARRAY_FOR(path[u].predicates, v) {
1479 if (path[u].predicates[v].type == 0) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001480 assert(LY_ARRAY_SIZE(path[u].predicates) == 1);
Radek Krejci084289f2019-07-09 17:35:30 +02001481 /* position predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001482 if (pos != path[u].predicates[v].position) {
Radek Krejci084289f2019-07-09 17:35:30 +02001483 pos++;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001484 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001485 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001486 } else if (path[u].predicates[v].type == 1) {
Radek Krejci084289f2019-07-09 17:35:30 +02001487 /* key-predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001488 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].predicates[v].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001489 struct lyd_node *key;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001490
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001491 lyd_find_sibling_val(lyd_node_children(node), path[u].predicates[v].key, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001492 if (!key) {
1493 /* probably error and we shouldn't be here due to previous checks when creating path */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001494 match = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001495 } else if (type->plugin->compare(&((struct lyd_node_term *)key)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001496 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001497 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001498 } else if (path[u].predicates[v].type == 2) {
Radek Krejci084289f2019-07-09 17:35:30 +02001499 /* leaf-list-predicate */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001500 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].node)->type;
1501
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001502 if (type->plugin->compare(&((struct lyd_node_term *)node)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001503 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001504 }
1505 } else {
1506 LOGINT(NULL);
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001507 return NULL;
1508 }
1509
1510 if (!match) {
1511 /* useless to check more predicates */
1512 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001513 }
1514 }
1515
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001516 if (!match) {
1517 /* try to match next sibling */
1518 start_sibling = node->next;
1519 } else {
1520 /* matched, move to the next path segment */
1521 ++u;
1522 start_sibling = lyd_node_children(node);
1523 pos = 1;
1524 }
Radek Krejci084289f2019-07-09 17:35:30 +02001525 }
1526
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001527 return (const struct lyd_node_term *)node;
Radek Krejci084289f2019-07-09 17:35:30 +02001528}
1529
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001530API LY_ERR
1531lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1532{
1533 const struct lyd_node *iter1, *iter2;
1534 struct lyd_node_term *term1, *term2;
1535 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001536 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001537 struct lysc_type *type;
1538 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001539
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001540 if (!node1 || !node2) {
1541 if (node1 == node2) {
1542 return LY_SUCCESS;
1543 } else {
1544 return LY_ENOT;
1545 }
1546 }
1547
Michal Vasko52927e22020-03-16 17:26:14 +01001548 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001549 return LY_ENOT;
1550 }
1551
1552 if (node1->hash != node2->hash) {
1553 return LY_ENOT;
1554 }
Michal Vasko52927e22020-03-16 17:26:14 +01001555 /* 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 +02001556
Michal Vasko52927e22020-03-16 17:26:14 +01001557 if (!node1->schema) {
1558 opaq1 = (struct lyd_node_opaq *)node1;
1559 opaq2 = (struct lyd_node_opaq *)node2;
1560 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001561 return LY_ENOT;
1562 }
Michal Vasko52927e22020-03-16 17:26:14 +01001563 switch (opaq1->format) {
1564 case LYD_XML:
1565 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1566 return LY_ENOT;
1567 }
1568 break;
1569 case LYD_SCHEMA:
1570 /* not allowed */
1571 LOGINT(LYD_NODE_CTX(node1));
1572 return LY_EINT;
1573 }
1574 if (options & LYD_COMPARE_FULL_RECURSION) {
1575 iter1 = opaq1->child;
1576 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001577 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001578 }
1579 return LY_SUCCESS;
1580 } else {
1581 switch (node1->schema->nodetype) {
1582 case LYS_LEAF:
1583 case LYS_LEAFLIST:
1584 if (options & LYD_COMPARE_DEFAULTS) {
1585 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1586 return LY_ENOT;
1587 }
1588 }
1589
1590 term1 = (struct lyd_node_term*)node1;
1591 term2 = (struct lyd_node_term*)node2;
1592 type = ((struct lysc_node_leaf*)node1->schema)->type;
1593
1594 return type->plugin->compare(&term1->value, &term2->value);
1595 case LYS_CONTAINER:
1596 if (options & LYD_COMPARE_DEFAULTS) {
1597 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1598 return LY_ENOT;
1599 }
1600 }
1601 if (options & LYD_COMPARE_FULL_RECURSION) {
1602 iter1 = ((struct lyd_node_inner*)node1)->child;
1603 iter2 = ((struct lyd_node_inner*)node2)->child;
1604 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001605 }
1606 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001607 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001608 case LYS_ACTION:
1609 if (options & LYD_COMPARE_FULL_RECURSION) {
1610 /* TODO action/RPC
1611 goto all_children_compare;
1612 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001613 }
1614 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001615 case LYS_NOTIF:
1616 if (options & LYD_COMPARE_FULL_RECURSION) {
1617 /* TODO Notification
1618 goto all_children_compare;
1619 */
1620 }
1621 return LY_SUCCESS;
1622 case LYS_LIST:
1623 iter1 = ((struct lyd_node_inner*)node1)->child;
1624 iter2 = ((struct lyd_node_inner*)node2)->child;
1625
1626 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1627 /* lists with keys, their equivalence is based on their keys */
1628 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1629 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1630 key = key->next) {
1631 if (lyd_compare(iter1, iter2, options)) {
1632 return LY_ENOT;
1633 }
1634 iter1 = iter1->next;
1635 iter2 = iter2->next;
1636 }
1637 } else {
1638 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1639
1640 all_children_compare:
1641 if (!iter1 && !iter2) {
1642 /* no children, nothing to compare */
1643 return LY_SUCCESS;
1644 }
1645
1646 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1647 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1648 return LY_ENOT;
1649 }
1650 }
1651 if (iter1 || iter2) {
1652 return LY_ENOT;
1653 }
1654 }
1655 return LY_SUCCESS;
1656 case LYS_ANYXML:
1657 case LYS_ANYDATA:
1658 any1 = (struct lyd_node_any*)node1;
1659 any2 = (struct lyd_node_any*)node2;
1660
1661 if (any1->value_type != any2->value_type) {
1662 return LY_ENOT;
1663 }
1664 switch (any1->value_type) {
1665 case LYD_ANYDATA_DATATREE:
1666 iter1 = any1->value.tree;
1667 iter2 = any2->value.tree;
1668 goto all_children_compare;
1669 case LYD_ANYDATA_STRING:
1670 case LYD_ANYDATA_XML:
1671 case LYD_ANYDATA_JSON:
1672 len1 = strlen(any1->value.str);
1673 len2 = strlen(any2->value.str);
1674 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1675 return LY_ENOT;
1676 }
1677 return LY_SUCCESS;
1678 #if 0 /* TODO LYB format */
1679 case LYD_ANYDATA_LYB:
1680 int len1 = lyd_lyb_data_length(any1->value.mem);
1681 int len2 = lyd_lyb_data_length(any2->value.mem);
1682 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1683 return LY_ENOT;
1684 }
1685 return LY_SUCCESS;
1686 #endif
1687 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001688 }
1689 }
1690
Michal Vasko52927e22020-03-16 17:26:14 +01001691 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001692 return LY_EINT;
1693}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001694
1695/**
Michal Vasko52927e22020-03-16 17:26:14 +01001696 * @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 +02001697 *
1698 * 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 +02001699 *
1700 * @param[in] node Original node to duplicate
1701 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1702 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1703 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1704 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1705 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001706 */
Michal Vasko52927e22020-03-16 17:26:14 +01001707static LY_ERR
1708lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1709 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001710{
Michal Vasko52927e22020-03-16 17:26:14 +01001711 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001712 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001713 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001714
Michal Vasko52927e22020-03-16 17:26:14 +01001715 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001716
Michal Vasko52927e22020-03-16 17:26:14 +01001717 if (!node->schema) {
1718 dup = calloc(1, sizeof(struct lyd_node_opaq));
1719 } else {
1720 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001721 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001722 case LYS_ACTION:
1723 case LYS_NOTIF:
1724 case LYS_CONTAINER:
1725 case LYS_LIST:
1726 dup = calloc(1, sizeof(struct lyd_node_inner));
1727 break;
1728 case LYS_LEAF:
1729 case LYS_LEAFLIST:
1730 dup = calloc(1, sizeof(struct lyd_node_term));
1731 break;
1732 case LYS_ANYDATA:
1733 case LYS_ANYXML:
1734 dup = calloc(1, sizeof(struct lyd_node_any));
1735 break;
1736 default:
1737 LOGINT(LYD_NODE_CTX(node));
1738 ret = LY_EINT;
1739 goto error;
1740 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001741 }
Michal Vasko52927e22020-03-16 17:26:14 +01001742 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001743
1744 /* TODO implement LYD_DUP_WITH_WHEN */
1745 dup->flags = node->flags;
1746 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001747 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001748
1749 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1750
1751 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001752 if (!dup->schema) {
1753 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1754 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1755 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001756
1757 if (options & LYD_DUP_RECURSIVE) {
1758 /* duplicate all the children */
1759 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001760 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1761 }
1762 }
1763 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1764 opaq->format = orig->format;
1765 if (orig->prefix.pref) {
1766 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1767 }
1768 if (orig->prefix.ns) {
1769 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1770 }
1771 if (orig->val_prefs) {
1772 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1773 LY_ARRAY_FOR(orig->val_prefs, u) {
1774 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1775 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1776 LY_ARRAY_INCREMENT(opaq->val_prefs);
1777 }
1778 }
1779 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1780 opaq->ctx = orig->ctx;
1781 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1782 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1783 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1784
1785 term->hash = orig->hash;
1786 term->value.realtype = orig->value.realtype;
1787 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1788 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1789 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1790 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1791 struct lyd_node *child;
1792
1793 if (options & LYD_DUP_RECURSIVE) {
1794 /* duplicate all the children */
1795 LY_LIST_FOR(orig->child, child) {
1796 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001797 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001798 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001799 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001800 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001801 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001802 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1803 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001804 if (!child) {
1805 /* possibly not keys are present in filtered tree */
1806 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001807 } else if (child->schema != key) {
1808 /* possibly not all keys are present in filtered tree,
1809 * but there can be also some non-key nodes */
1810 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001811 }
Michal Vasko52927e22020-03-16 17:26:14 +01001812 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001813 child = child->next;
1814 }
1815 }
1816 lyd_hash(dup);
1817 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001818 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1819 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001820
1821 any->hash = orig->hash;
1822 any->value_type = orig->value_type;
1823 switch (any->value_type) {
1824 case LYD_ANYDATA_DATATREE:
1825 if (orig->value.tree) {
1826 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02001827 if (!any->value.tree) {
1828 /* get the last error's error code recorded by lyd_dup */
1829 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
1830 ret = ei ? ei->prev->no : LY_EOTHER;
1831 goto error;
1832 }
1833 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0 ,error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001834 }
1835 break;
1836 case LYD_ANYDATA_STRING:
1837 case LYD_ANYDATA_XML:
1838 case LYD_ANYDATA_JSON:
1839 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001840 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001841 }
1842 break;
1843 }
1844 }
1845
Michal Vasko52927e22020-03-16 17:26:14 +01001846 /* insert */
1847 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001848 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001849
1850 if (dup_p) {
1851 *dup_p = dup;
1852 }
1853 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001854
1855error:
Michal Vasko52927e22020-03-16 17:26:14 +01001856 lyd_free_tree(dup);
1857 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001858}
1859
1860API struct lyd_node *
1861lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1862{
1863 struct ly_ctx *ctx;
1864 const struct lyd_node *orig; /* original node to be duplicated */
1865 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001866 struct lyd_node *top = NULL; /* the most higher created node */
1867 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1868 int keyless_parent_list = 0;
1869
1870 LY_CHECK_ARG_RET(NULL, node, NULL);
1871 ctx = node->schema->module->ctx;
1872
1873 if (options & LYD_DUP_WITH_PARENTS) {
1874 struct lyd_node_inner *orig_parent, *iter;
1875 int repeat = 1;
1876 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1877 if (parent && parent->schema == orig_parent->schema) {
1878 /* stop creating parents, connect what we have into the provided parent */
1879 iter = parent;
1880 repeat = 0;
1881 /* get know if there is a keyless list which we will have to rehash */
1882 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001883 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001884 keyless_parent_list = 1;
1885 break;
1886 }
1887 }
1888 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001889 iter = NULL;
1890 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1891 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001892 }
1893 if (!local_parent) {
1894 local_parent = iter;
1895 }
1896 if (iter->child) {
1897 /* 1) list - add after keys
1898 * 2) provided parent with some children */
1899 iter->child->prev->next = top;
1900 if (top) {
1901 top->prev = iter->child->prev;
1902 iter->child->prev = top;
1903 }
1904 } else {
1905 iter->child = top;
1906 if (iter->schema->nodetype == LYS_LIST) {
1907 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1908 keyless_parent_list = 1;
1909 }
1910 }
1911 if (top) {
1912 top->parent = iter;
1913 }
1914 top = (struct lyd_node*)iter;
1915 }
1916 if (repeat && parent) {
1917 /* given parent and created parents chain actually do not interconnect */
1918 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1919 goto error;
1920 }
1921 } else {
1922 local_parent = parent;
1923 }
1924
Radek Krejci22ebdba2019-07-25 13:59:43 +02001925 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001926 /* if there is no local parent, it will be inserted into first */
1927 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 +02001928 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1929 break;
1930 }
1931 }
1932 if (keyless_parent_list) {
1933 /* rehash */
1934 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001935 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001936 lyd_hash((struct lyd_node*)local_parent);
1937 }
1938 }
1939 }
1940 return first;
1941
1942error:
1943 if (top) {
1944 lyd_free_tree(top);
1945 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001946 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001947 }
1948 return NULL;
1949}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001950
1951static LY_ERR
1952lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
1953{
Michal Vasko14654712020-02-06 08:35:21 +01001954 /* ending \0 */
1955 ++reqlen;
1956
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001957 if (reqlen > *buflen) {
1958 if (is_static) {
1959 return LY_EINCOMPLETE;
1960 }
1961
1962 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
1963 if (!*buffer) {
1964 return LY_EMEM;
1965 }
1966
1967 *buflen = reqlen;
1968 }
1969
1970 return LY_SUCCESS;
1971}
1972
1973/**
1974 * @brief Append all list key predicates to path.
1975 *
1976 * @param[in] node Node with keys to print.
1977 * @param[in,out] buffer Buffer to print to.
1978 * @param[in,out] buflen Current buffer length.
1979 * @param[in,out] bufused Current number of characters used in @p buffer.
1980 * @param[in] is_static Whether buffer is static or can be reallocated.
1981 * @return LY_ERR
1982 */
1983static LY_ERR
1984lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1985{
1986 const struct lyd_node *key;
1987 int dynamic = 0;
1988 size_t len;
1989 const char *val;
1990 char quot;
1991 LY_ERR rc;
1992
Michal Vasko14654712020-02-06 08:35:21 +01001993 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001994 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
1995 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
1996 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1997 if (rc != LY_SUCCESS) {
1998 if (dynamic) {
1999 free((char *)val);
2000 }
2001 return rc;
2002 }
2003
2004 quot = '\'';
2005 if (strchr(val, '\'')) {
2006 quot = '"';
2007 }
2008 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2009
2010 if (dynamic) {
2011 free((char *)val);
2012 }
2013 }
2014
2015 return LY_SUCCESS;
2016}
2017
2018/**
2019 * @brief Append leaf-list value predicate to path.
2020 *
2021 * @param[in] node Node to print.
2022 * @param[in,out] buffer Buffer to print to.
2023 * @param[in,out] buflen Current buffer length.
2024 * @param[in,out] bufused Current number of characters used in @p buffer.
2025 * @param[in] is_static Whether buffer is static or can be reallocated.
2026 * @return LY_ERR
2027 */
2028static LY_ERR
2029lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2030{
2031 int dynamic = 0;
2032 size_t len;
2033 const char *val;
2034 char quot;
2035 LY_ERR rc;
2036
2037 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2038 len = 4 + strlen(val) + 2;
2039 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2040 if (rc != LY_SUCCESS) {
2041 goto cleanup;
2042 }
2043
2044 quot = '\'';
2045 if (strchr(val, '\'')) {
2046 quot = '"';
2047 }
2048 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2049
2050cleanup:
2051 if (dynamic) {
2052 free((char *)val);
2053 }
2054 return rc;
2055}
2056
2057/**
2058 * @brief Append node position (relative to its other instances) predicate to path.
2059 *
2060 * @param[in] node Node to print.
2061 * @param[in,out] buffer Buffer to print to.
2062 * @param[in,out] buflen Current buffer length.
2063 * @param[in,out] bufused Current number of characters used in @p buffer.
2064 * @param[in] is_static Whether buffer is static or can be reallocated.
2065 * @return LY_ERR
2066 */
2067static LY_ERR
2068lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2069{
2070 const struct lyd_node *first, *iter;
2071 size_t len;
2072 int pos;
2073 char *val = NULL;
2074 LY_ERR rc;
2075
2076 if (node->parent) {
2077 first = node->parent->child;
2078 } else {
2079 for (first = node; node->prev->next; node = node->prev);
2080 }
2081 pos = 1;
2082 for (iter = first; iter != node; iter = iter->next) {
2083 if (iter->schema == node->schema) {
2084 ++pos;
2085 }
2086 }
2087 if (asprintf(&val, "%d", pos) == -1) {
2088 return LY_EMEM;
2089 }
2090
2091 len = 1 + strlen(val) + 1;
2092 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2093 if (rc != LY_SUCCESS) {
2094 goto cleanup;
2095 }
2096
2097 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2098
2099cleanup:
2100 free(val);
2101 return rc;
2102}
2103
2104API char *
2105lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2106{
Michal Vasko14654712020-02-06 08:35:21 +01002107 int is_static = 0, i, depth;
2108 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002109 const struct lyd_node *iter;
2110 const struct lys_module *mod;
2111 LY_ERR rc;
2112
2113 LY_CHECK_ARG_RET(NULL, node, NULL);
2114 if (buffer) {
2115 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2116 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002117 } else {
2118 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002119 }
2120
2121 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002122 case LYD_PATH_LOG:
2123 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002124 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2125 ++depth;
2126 }
2127
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002128 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002129 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002130 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002131 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002132iter_print:
2133 /* print prefix and name */
2134 mod = NULL;
2135 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2136 mod = iter->schema->module;
2137 }
2138
2139 /* realloc string */
2140 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2141 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2142 if (rc != LY_SUCCESS) {
2143 break;
2144 }
2145
2146 /* print next node */
2147 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2148
2149 switch (iter->schema->nodetype) {
2150 case LYS_LIST:
2151 if (iter->schema->flags & LYS_KEYLESS) {
2152 /* print its position */
2153 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2154 } else {
2155 /* print all list keys in predicates */
2156 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2157 }
2158 break;
2159 case LYS_LEAFLIST:
2160 if (iter->schema->flags & LYS_CONFIG_W) {
2161 /* print leaf-list value */
2162 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2163 } else {
2164 /* print its position */
2165 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2166 }
2167 break;
2168 default:
2169 /* nothing to print more */
2170 rc = LY_SUCCESS;
2171 break;
2172 }
2173 if (rc != LY_SUCCESS) {
2174 break;
2175 }
2176
Michal Vasko14654712020-02-06 08:35:21 +01002177 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002178 }
2179 break;
2180 }
2181
2182 return buffer;
2183}
Michal Vaskoe444f752020-02-10 12:20:06 +01002184
Michal Vasko9b368d32020-02-14 13:53:31 +01002185LY_ERR
2186lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2187 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002188{
2189 LY_ERR rc;
2190 const struct lyd_node *node = NULL;
2191 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002192 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002193 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002194 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002195
Michal Vasko9b368d32020-02-14 13:53:31 +01002196 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002197
2198 if (!first) {
2199 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002200 if (match) {
2201 *match = NULL;
2202 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002203 return LY_ENOTFOUND;
2204 }
2205
Michal Vaskoe444f752020-02-10 12:20:06 +01002206 if (key_or_value && !val_len) {
2207 val_len = strlen(key_or_value);
2208 }
2209
2210 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002211 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002212 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 +01002213 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2214 /* parse keys into canonical values */
Michal Vaskod3678892020-05-21 10:06:58 +02002215 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, 1, &keys), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002216 }
2217
2218 /* find first matching value */
2219 LY_LIST_FOR(first, node) {
2220 if (node->schema != schema) {
2221 continue;
2222 }
2223
2224 if ((schema->nodetype == LYS_LIST) && keys.str) {
2225 /* compare all set keys */
2226 for (i = 0; i < keys.key_count; ++i) {
2227 /* find key */
2228 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2229 (struct lyd_node **)&term);
2230 if (rc == LY_ENOTFOUND) {
2231 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002232 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002233 }
2234 LY_CHECK_GOTO(rc, cleanup);
2235
2236 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002237 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002238 break;
2239 }
2240 }
2241
2242 if (i < keys.key_count) {
2243 /* not a match */
2244 continue;
2245 }
Michal Vasko90932a92020-02-12 14:33:03 +01002246 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002247 term = (struct lyd_node_term *)node;
2248
2249 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002250 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002251 /* not a match */
2252 continue;
2253 }
2254 }
2255
2256 /* all criteria passed */
2257 break;
2258 }
2259
2260 if (!node) {
2261 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002262 if (match) {
2263 *match = NULL;
2264 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002265 goto cleanup;
2266 }
2267
2268 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002269 if (match) {
2270 *match = (struct lyd_node *)node;
2271 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002272 rc = LY_SUCCESS;
2273
2274cleanup:
2275 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002276 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002277 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002278 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002279 return rc;
2280}
2281
2282API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002283lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2284 const char *key_or_value, size_t val_len, struct lyd_node **match)
2285{
2286 const struct lysc_node *schema;
2287
2288 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2289
2290 if (!first) {
2291 /* no data */
2292 *match = NULL;
2293 return LY_ENOTFOUND;
2294 }
2295
2296 /* find schema */
2297 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2298 if (!schema) {
2299 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2300 return LY_EINVAL;
2301 }
2302
2303 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2304}
2305
2306API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002307lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2308{
2309 struct lyd_node **match_p;
2310 struct lyd_node_inner *parent;
2311
Michal Vaskof03ed032020-03-04 13:31:44 +01002312 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002313
2314 if (!siblings) {
2315 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002316 if (match) {
2317 *match = NULL;
2318 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002319 return LY_ENOTFOUND;
2320 }
2321
2322 /* find first sibling */
2323 if (siblings->parent) {
2324 siblings = siblings->parent->child;
2325 } else {
2326 while (siblings->prev->next) {
2327 siblings = siblings->prev;
2328 }
2329 }
2330
2331 parent = (struct lyd_node_inner *)siblings->parent;
2332 if (parent && parent->children_ht) {
2333 assert(target->hash);
2334
2335 /* find by hash */
2336 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2337 siblings = *match_p;
2338 } else {
2339 /* not found */
2340 siblings = NULL;
2341 }
2342 } else {
2343 /* no children hash table */
2344 for (; siblings; siblings = siblings->next) {
2345 if (!lyd_compare(siblings, target, 0)) {
2346 break;
2347 }
2348 }
2349 }
2350
2351 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002352 if (match) {
2353 *match = NULL;
2354 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002355 return LY_ENOTFOUND;
2356 }
2357
Michal Vasko9b368d32020-02-14 13:53:31 +01002358 if (match) {
2359 *match = (struct lyd_node *)siblings;
2360 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002361 return LY_SUCCESS;
2362}
2363
2364API LY_ERR
2365lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2366{
2367 struct lyd_node_inner *parent;
2368 struct lyd_node *match;
2369 struct lyd_node **match_p;
2370 struct ly_set *ret;
2371
2372 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2373
2374 if (!siblings) {
2375 /* no data */
2376 return LY_ENOTFOUND;
2377 }
2378
2379 ret = ly_set_new();
2380 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2381
2382 /* find first sibling */
2383 if (siblings->parent) {
2384 siblings = siblings->parent->child;
2385 } else {
2386 while (siblings->prev->next) {
2387 siblings = siblings->prev;
2388 }
2389 }
2390
2391 parent = (struct lyd_node_inner *)siblings->parent;
2392 if (parent && parent->children_ht) {
2393 assert(target->hash);
2394
2395 /* find by hash */
2396 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2397 match = *match_p;
2398 } else {
2399 /* not found */
2400 match = NULL;
2401 }
2402 while (match) {
2403 /* add all found nodes into the return set */
2404 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2405 goto error;
2406 }
2407
2408 /* find next instance */
2409 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2410 match = NULL;
2411 } else {
2412 match = *match_p;
2413 }
2414 }
2415 } else {
2416 /* no children hash table */
2417 for (; siblings; siblings = siblings->next) {
2418 if (!lyd_compare(siblings, target, 0)) {
2419 /* a match */
2420 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2421 goto error;
2422 }
2423 }
2424 }
2425 }
2426
2427 if (!ret->count) {
2428 ly_set_free(ret, NULL);
2429 return LY_ENOTFOUND;
2430 }
2431
2432 *set = ret;
2433 return LY_SUCCESS;
2434
2435error:
2436 ly_set_free(ret, NULL);
2437 return LY_EMEM;
2438}
2439
Michal Vasko90932a92020-02-12 14:33:03 +01002440static int
2441lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2442{
2443 struct lysc_node *val1;
2444 struct lyd_node *val2;
2445
2446 val1 = *((struct lysc_node **)val1_p);
2447 val2 = *((struct lyd_node **)val2_p);
2448
2449 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2450
2451 if (val1 == val2->schema) {
2452 /* schema match is enough */
2453 return 1;
2454 } else {
2455 return 0;
2456 }
2457}
2458
2459static LY_ERR
2460lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2461{
2462 struct lyd_node **match_p;
2463 struct lyd_node_inner *parent;
2464 uint32_t hash;
2465 values_equal_cb ht_cb;
2466
2467 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2468
2469 /* find first sibling */
2470 if (siblings->parent) {
2471 siblings = siblings->parent->child;
2472 } else {
2473 while (siblings->prev->next) {
2474 siblings = siblings->prev;
2475 }
2476 }
2477
2478 parent = (struct lyd_node_inner *)siblings->parent;
2479 if (parent && parent->children_ht) {
2480 /* calculate our hash */
2481 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2482 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2483 hash = dict_hash_multi(hash, NULL, 0);
2484
2485 /* use special hash table function */
2486 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2487
2488 /* find by hash */
2489 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2490 siblings = *match_p;
2491 } else {
2492 /* not found */
2493 siblings = NULL;
2494 }
2495
2496 /* set the original hash table compare function back */
2497 lyht_set_cb(parent->children_ht, ht_cb);
2498 } else {
2499 /* no children hash table */
2500 for (; siblings; siblings = siblings->next) {
2501 if (siblings->schema == schema) {
2502 /* schema match is enough */
2503 break;
2504 }
2505 }
2506 }
2507
2508 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002509 if (match) {
2510 *match = NULL;
2511 }
Michal Vasko90932a92020-02-12 14:33:03 +01002512 return LY_ENOTFOUND;
2513 }
2514
Michal Vasko9b368d32020-02-14 13:53:31 +01002515 if (match) {
2516 *match = (struct lyd_node *)siblings;
2517 }
Michal Vasko90932a92020-02-12 14:33:03 +01002518 return LY_SUCCESS;
2519}
2520
Michal Vaskoe444f752020-02-10 12:20:06 +01002521API LY_ERR
2522lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2523 size_t val_len, struct lyd_node **match)
2524{
2525 LY_ERR rc;
2526 struct lyd_node *target = NULL;
2527
2528 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
2529 if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) {
2530 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2531 return LY_EINVAL;
2532 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2533 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2534 return LY_EINVAL;
2535 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2536 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2537 lys_nodetype2str(schema->nodetype), __func__);
2538 return LY_EINVAL;
2539 }
2540
2541 if (!siblings) {
2542 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002543 if (match) {
2544 *match = NULL;
2545 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002546 return LY_ENOTFOUND;
2547 }
2548
Michal Vaskof03ed032020-03-04 13:31:44 +01002549 if (key_or_value && !val_len) {
2550 val_len = strlen(key_or_value);
2551 }
2552
Michal Vasko90932a92020-02-12 14:33:03 +01002553 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002554 switch (schema->nodetype) {
2555 case LYS_CONTAINER:
2556 case LYS_ANYXML:
2557 case LYS_ANYDATA:
2558 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01002559 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01002560 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002561 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002562 /* find it based on schema only */
2563 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002564 break;
2565 case LYS_LEAFLIST:
2566 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002567 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 +01002568 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002569 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002570 if (schema->nodetype == LYS_LIST) {
2571 /* target used attributes: schema, hash, child (all keys) */
Michal Vaskod3678892020-05-21 10:06:58 +02002572 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, LYD_JSON, 1, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002573 }
2574
2575 /* find it */
2576 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002577 break;
2578 default:
2579 /* unreachable */
2580 LOGINT(schema->module->ctx);
2581 return LY_EINT;
2582 }
2583
Michal Vaskoe444f752020-02-10 12:20:06 +01002584 lyd_free_tree(target);
2585 return rc;
2586}