blob: 5026d4e13dc2d16f3a3d1597d85599559884f5d2 [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 Vasko90932a92020-02-12 14:33:03 +010030#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree_schema.h"
Michal Vasko52927e22020-03-16 17:26:14 +010032#include "xml.h"
Radek Krejci38d85362019-09-05 16:26:38 +020033#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035
Michal Vaskoe444f752020-02-10 12:20:06 +010036struct ly_keys {
37 char *str;
38 struct {
39 const struct lysc_node_leaf *schema;
40 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010041 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010042 } *keys;
43 size_t key_count;
44};
45
Radek Krejci084289f2019-07-09 17:35:30 +020046LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010047lyd_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 +010048 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020049{
Michal Vasko90932a92020-02-12 14:33:03 +010050 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020051 struct ly_err_item *err = NULL;
52 struct ly_ctx *ctx;
53 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020054 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010055 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020056 assert(node);
57
58 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020059
Radek Krejci73dead22019-07-11 16:46:16 +020060 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020061 if (!second) {
62 node->value.realtype = type;
63 }
Michal Vasko90932a92020-02-12 14:33:03 +010064 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vaskof03ed032020-03-04 13:31:44 +010065 tree ? (void *)node : (void *)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +020066 &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010067 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020068 if (err) {
69 ly_err_print(err);
70 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
71 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020072 }
Radek Krejci73dead22019-07-11 16:46:16 +020073 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010074 } else if (dynamic) {
75 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020076 }
77
78error:
79 return ret;
80}
81
Michal Vasko90932a92020-02-12 14:33:03 +010082/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
83static LY_ERR
84lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
85 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
86{
87 LY_ERR ret = LY_SUCCESS;
88 struct ly_err_item *err = NULL;
89 struct ly_ctx *ctx;
90 struct lysc_type *type;
91 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
92
93 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
94
95 ctx = schema->module->ctx;
96 type = ((struct lysc_node_leaf *)schema)->type;
97 val->realtype = type;
98 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
99 val, NULL, &err);
100 if (ret == LY_EINCOMPLETE) {
101 /* this is fine, we do not need it resolved */
102 ret = LY_SUCCESS;
103 } else if (ret && err) {
104 ly_err_print(err);
105 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
106 ly_err_free(err);
107 }
108 if (!ret && dynamic) {
109 *dynamic = 0;
110 }
111
112 return ret;
113}
114
Radek Krejci38d85362019-09-05 16:26:38 +0200115LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100116lyd_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 +0100117 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100118 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200119{
Michal Vasko90932a92020-02-12 14:33:03 +0100120 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200121 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200122 struct lyext_metadata *ant;
123 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100124 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200125
Michal Vasko9f96a052020-03-10 09:41:45 +0100126 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100127
Michal Vasko9f96a052020-03-10 09:41:45 +0100128 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200129
130 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100131 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200132 }
Michal Vasko90932a92020-02-12 14:33:03 +0100133 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100134 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100135 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200136 if (err) {
137 ly_err_print(err);
138 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
139 ly_err_free(err);
140 }
141 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100142 } else if (dynamic) {
143 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200144 }
145
146error:
147 return ret;
148}
149
Radek Krejci084289f2019-07-09 17:35:30 +0200150API LY_ERR
151lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
152 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
153{
154 LY_ERR rc = LY_SUCCESS;
155 struct ly_err_item *err = NULL;
156 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200157
158 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
159
160 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
161 LOGARG(ctx, node);
162 return LY_EINVAL;
163 }
164
165 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200166 /* just validate, no storing of enything */
167 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
168 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
169 if (rc == LY_EINCOMPLETE) {
170 /* actually success since we do not provide the context tree and call validation with
171 * LY_TYPE_OPTS_INCOMPLETE_DATA */
172 rc = LY_SUCCESS;
173 } else if (rc && err) {
174 if (ctx) {
175 /* log only in case the ctx was provided as input parameter */
176 ly_err_print(err);
177 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200178 }
Radek Krejci73dead22019-07-11 16:46:16 +0200179 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200180 }
181
182 return rc;
183}
184
185API LY_ERR
186lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100187 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 +0200188{
189 LY_ERR rc;
190 struct ly_err_item *err = NULL;
191 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100192 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200193
194 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
195
196 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200197 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100198 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200199 NULL, NULL, &err);
200 if (rc == LY_EINCOMPLETE) {
201 return rc;
202 } else if (rc) {
203 if (err) {
204 if (ctx) {
205 ly_err_print(err);
206 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200207 }
Radek Krejci73dead22019-07-11 16:46:16 +0200208 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200209 }
Radek Krejci73dead22019-07-11 16:46:16 +0200210 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200211 }
212
213 return LY_SUCCESS;
214}
215
216API LY_ERR
217lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100218 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 +0200219{
220 LY_ERR ret = LY_SUCCESS, rc;
221 struct ly_err_item *err = NULL;
222 struct ly_ctx *ctx;
223 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200224 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100225 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200226
227 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
228
229 ctx = node->schema->module->ctx;
230 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200231 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 +0100232 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200233 if (rc == LY_EINCOMPLETE) {
234 ret = rc;
235 /* continue with comparing, just remember what to return if storing is ok */
236 } else if (rc) {
237 /* value to compare is invalid */
238 ret = LY_EINVAL;
239 if (err) {
240 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200241 }
Radek Krejci73dead22019-07-11 16:46:16 +0200242 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200243 }
244
245 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200246 if (type->plugin->compare(&node->value, &data)) {
247 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
248 ret = LY_EVALID;
249 }
Radek Krejci084289f2019-07-09 17:35:30 +0200250
251cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200252 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200253
254 return ret;
255}
256
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200257API const char *
258lyd_value2str(const struct lyd_node_term *node, int *dynamic)
259{
260 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
261
262 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
263}
264
265API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100266lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200267{
Michal Vasko9f96a052020-03-10 09:41:45 +0100268 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200269
Michal Vasko9f96a052020-03-10 09:41:45 +0100270 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200271}
272
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200273API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100274lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200275{
Radek Krejcie7b95092019-05-15 11:03:07 +0200276 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200277#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200278 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200279#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200280
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200281 LY_CHECK_ARG_RET(ctx, ctx, NULL);
282
Michal Vasko5b37a352020-03-06 13:38:33 +0100283 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
284 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
285 return NULL;
286 }
287
Michal Vaskoa3881362020-01-21 15:57:35 +0100288#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200289 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200290 /* first item in trees is mandatory - the RPC/action request */
291 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
292 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
293 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
294 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200295 return NULL;
296 }
297 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200298
Radek Krejcie7b95092019-05-15 11:03:07 +0200299 if (options & LYD_OPT_DATA_TEMPLATE) {
300 yang_data_name = va_arg(ap, const char *);
301 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200302#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200303
304 if (!format) {
305 /* TODO try to detect format from the content */
306 }
307
308 switch (format) {
309 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100310 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200311 break;
312#if 0
313 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200314 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200315 break;
316 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200317 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 break;
319#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100320 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 LOGINT(ctx);
322 break;
323 }
324
Radek Krejcie7b95092019-05-15 11:03:07 +0200325 return result;
326}
327
328API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100329lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200330{
331 struct lyd_node *result;
332 size_t length;
333 char *addr;
334
335 LY_CHECK_ARG_RET(ctx, ctx, NULL);
336 if (fd < 0) {
337 LOGARG(ctx, fd);
338 return NULL;
339 }
340
341 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100342 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200343 if (addr) {
344 ly_munmap(addr, length);
345 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200346
347 return result;
348}
349
350API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100351lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200352{
353 int fd;
354 struct lyd_node *result;
355 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200356
357 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
358
359 fd = open(path, O_RDONLY);
360 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
361
362 if (!format) {
363 /* unknown format - try to detect it from filename's suffix */
364 len = strlen(path);
365
366 /* ignore trailing whitespaces */
367 for (; len > 0 && isspace(path[len - 1]); len--);
368
369 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
370 format = LYD_XML;
371#if 0
372 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
373 format = LYD_JSON;
374 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
375 format = LYD_LYB;
376#endif
377 } /* else still unknown, try later to detect it from the content */
378 }
379
Michal Vaskoa3881362020-01-21 15:57:35 +0100380 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200381 close(fd);
382
383 return result;
384}
Radek Krejci084289f2019-07-09 17:35:30 +0200385
Michal Vasko90932a92020-02-12 14:33:03 +0100386LY_ERR
387lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
388 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
389{
390 LY_ERR ret;
391 struct lyd_node_term *term;
392
Michal Vasko9b368d32020-02-14 13:53:31 +0100393 assert(schema->nodetype & LYD_NODE_TERM);
394
Michal Vasko90932a92020-02-12 14:33:03 +0100395 term = calloc(1, sizeof *term);
396 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
397
398 term->schema = schema;
399 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100400 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100401
402 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
403 if (ret && (ret != LY_EINCOMPLETE)) {
404 free(term);
405 return ret;
406 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100407 lyd_hash((struct lyd_node *)term);
408
409 *node = (struct lyd_node *)term;
410 return ret;
411}
412
413LY_ERR
414lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
415{
416 LY_ERR ret;
417 struct lyd_node_term *term;
418 struct lysc_type *type;
419
420 assert(schema->nodetype & LYD_NODE_TERM);
421
422 term = calloc(1, sizeof *term);
423 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
424
425 term->schema = schema;
426 term->prev = (struct lyd_node *)term;
427 term->flags = LYD_NEW;
428
429 type = ((struct lysc_node_leaf *)schema)->type;
430 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
431 if (ret) {
432 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
433 free(term);
434 return ret;
435 }
436 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100437
438 *node = (struct lyd_node *)term;
439 return ret;
440}
441
442LY_ERR
443lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
444{
445 struct lyd_node_inner *in;
446
Michal Vasko9b368d32020-02-14 13:53:31 +0100447 assert(schema->nodetype & LYD_NODE_INNER);
448
Michal Vasko90932a92020-02-12 14:33:03 +0100449 in = calloc(1, sizeof *in);
450 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
451
452 in->schema = schema;
453 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100454 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100455
Michal Vasko9b368d32020-02-14 13:53:31 +0100456 /* do not hash list with keys, we need them for the hash */
457 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
458 lyd_hash((struct lyd_node *)in);
459 }
Michal Vasko90932a92020-02-12 14:33:03 +0100460
461 *node = (struct lyd_node *)in;
462 return LY_SUCCESS;
463}
464
465static void
466ly_keys_clean(struct ly_keys *keys)
467{
468 size_t i;
469
470 for (i = 0; i < keys->key_count; ++i) {
471 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
472 }
473 free(keys->str);
474 free(keys->keys);
475}
476
477static char *
478ly_keys_parse_next(char **next_key, char **key_name)
479{
480 char *ptr, *ptr2, *val, quot;
481
482 ptr = *next_key;
483
484 /* "[" */
485 LY_CHECK_GOTO(ptr[0] != '[', error);
486 ++ptr;
487
488 /* key name */
489 ptr2 = strchr(ptr, '=');
490 LY_CHECK_GOTO(!ptr2, error);
491
492 *key_name = ptr;
493 ptr2[0] = '\0';
494
495 /* \0, was '=' */
496 ptr = ptr2 + 1;
497
498 /* quote */
499 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
500 quot = ptr[0];
501 ++ptr;
502
503 /* value, terminate it */
504 val = ptr;
505 ptr2 = strchr(ptr, quot);
506 LY_CHECK_GOTO(!ptr2, error);
507 ptr2[0] = '\0';
508
509 /* \0, was quote */
510 ptr = ptr2 + 1;
511
512 /* "]" */
513 LY_CHECK_GOTO(ptr[0] != ']', error);
514 ++ptr;
515
516 *next_key = ptr;
517 return val;
518
519error:
520 *next_key = ptr;
521 return NULL;
522}
523
524/* fill keys structure; if store is set, fill also each val */
525static LY_ERR
526ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, struct ly_keys *keys)
527{
528 LY_ERR ret = LY_SUCCESS;
529 char *next_key, *name;
530 const struct lysc_node *key;
531 size_t i;
532
533 assert(list->nodetype == LYS_LIST);
534
535 memset(keys, 0, sizeof *keys);
536
Michal Vaskof03ed032020-03-04 13:31:44 +0100537 if (!keys_str) {
538 /* nothing to parse */
539 return LY_SUCCESS;
540 }
541
542 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100543 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
544
545 next_key = keys->str;
546 while (next_key[0]) {
547 /* new key */
548 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
549 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
550
551 /* fill */
552 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
553 if (!keys->keys[keys->key_count].value) {
554 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
555 ret = LY_EINVAL;
556 goto cleanup;
557 }
558
559 /* find schema node */
560 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
561 if (!key) {
562 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
563 ret = LY_EINVAL;
564 goto cleanup;
565 }
566 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
567
568 /* check that we do not have it already */
569 for (i = 0; i < keys->key_count; ++i) {
570 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
571 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
572 ret = LY_EINVAL;
573 goto cleanup;
574 }
575 }
576
577 if (store) {
578 /* store the value */
579 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
580 lydjson_resolve_prefix, NULL, LYD_JSON);
581 LY_CHECK_GOTO(ret, cleanup);
582 }
583
584 /* another valid key */
585 ++keys->key_count;
586 }
587
588cleanup:
589 ly_keys_clean(keys);
590 return ret;
591}
592
593LY_ERR
594lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node)
595{
596 LY_ERR ret = LY_SUCCESS;
597 const struct lysc_node *key_s;
598 struct lyd_node *list = NULL, *key;
599 struct ly_keys keys = {0};
600 size_t i;
601
602 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
603
604 /* parse keys */
605 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, &keys), cleanup);
606
607 /* create list */
608 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
609
610 /* everything was checked except that all keys are set */
611 i = 0;
612 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
613 ++i;
614 }
615 if (i != keys.key_count) {
616 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
617 ret = LY_EINVAL;
618 goto cleanup;
619 }
620
621 /* create and insert all the keys */
622 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskof03ed032020-03-04 13:31:44 +0100623 LY_CHECK_GOTO(ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value,
624 strlen(keys.keys[i].value), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100625 lyd_insert_node(list, NULL, key);
626 }
627
Michal Vasko9b368d32020-02-14 13:53:31 +0100628 /* hash having all the keys */
629 lyd_hash(list);
630
Michal Vasko90932a92020-02-12 14:33:03 +0100631 /* success */
632 *node = list;
633 list = NULL;
634
635cleanup:
636 lyd_free_tree(list);
637 ly_keys_clean(&keys);
638 return ret;
639}
640
641LY_ERR
642lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
643{
644 struct lyd_node_any *any;
645
Michal Vasko9b368d32020-02-14 13:53:31 +0100646 assert(schema->nodetype & LYD_NODE_ANY);
647
Michal Vasko90932a92020-02-12 14:33:03 +0100648 any = calloc(1, sizeof *any);
649 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
650
651 any->schema = schema;
652 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100653 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100654
655 any->value.xml = value;
656 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100657 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100658
659 *node = (struct lyd_node *)any;
660 return LY_SUCCESS;
661}
662
Michal Vasko52927e22020-03-16 17:26:14 +0100663LY_ERR
664lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
665 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
666 const char *ns, struct lyd_node **node)
667{
668 struct lyd_node_opaq *opaq;
669
670 assert(ctx && name && name_len && ns);
671
672 if (!value_len) {
673 value = "";
674 }
675
676 opaq = calloc(1, sizeof *opaq);
677 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
678
679 opaq->prev = (struct lyd_node *)opaq;
680
681 opaq->name = lydict_insert(ctx, name, name_len);
682 opaq->format = format;
683 if (pref_len) {
684 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
685 }
686 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
687 opaq->val_prefs = val_prefs;
688 if (dynamic && *dynamic) {
689 opaq->value = lydict_insert_zc(ctx, (char *)value);
690 *dynamic = 0;
691 } else {
692 opaq->value = lydict_insert(ctx, value, value_len);
693 }
694 opaq->ctx = ctx;
695
696 *node = (struct lyd_node *)opaq;
697 return LY_SUCCESS;
698}
699
Michal Vasko013a8182020-03-03 10:46:53 +0100700API struct lyd_node *
701lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
702{
703 struct lyd_node *ret = NULL;
704 const struct lysc_node *schema;
705 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
706
707 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
708
Michal Vaskof03ed032020-03-04 13:31:44 +0100709 if (!module) {
710 module = parent->schema->module;
711 }
712
Michal Vasko013a8182020-03-03 10:46:53 +0100713 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_ACTION, 0);
714 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL);
715
716 if (!lyd_create_inner(schema, &ret) && parent) {
717 lyd_insert_node(parent, NULL, ret);
718 }
719 return ret;
720}
721
722API struct lyd_node *
723lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
724{
725 struct lyd_node *ret = NULL, *key;
726 const struct lysc_node *schema, *key_s;
727 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
728 va_list ap;
729 const char *key_val;
730 LY_ERR rc = LY_SUCCESS;
731
732 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
733
Michal Vaskof03ed032020-03-04 13:31:44 +0100734 if (!module) {
735 module = parent->schema->module;
736 }
737
Michal Vasko013a8182020-03-03 10:46:53 +0100738 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
739 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
740
741 /* create list inner node */
742 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
743
744 va_start(ap, name);
745
746 /* create and insert all the keys */
747 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
748 key_val = va_arg(ap, const char *);
749
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
751 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100752 lyd_insert_node(ret, NULL, key);
753 }
754
755 /* hash having all the keys */
756 lyd_hash(ret);
757
758 if (parent) {
759 lyd_insert_node(parent, NULL, ret);
760 }
761
762cleanup:
763 if (rc) {
764 lyd_free_tree(ret);
765 ret = NULL;
766 }
767 va_end(ap);
768 return ret;
769}
770
771API struct lyd_node *
772lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
773{
774 struct lyd_node *ret = NULL;
775 const struct lysc_node *schema;
776 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
777
778 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
779
Michal Vaskof03ed032020-03-04 13:31:44 +0100780 if (!module) {
781 module = parent->schema->module;
782 }
783
Michal Vasko013a8182020-03-03 10:46:53 +0100784 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
785 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
786
Michal Vaskof03ed032020-03-04 13:31:44 +0100787 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100788 lyd_insert_node(parent, NULL, ret);
789 }
790 return ret;
791}
792
793API struct lyd_node *
794lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
795{
796 struct lyd_node *ret = NULL;
797 const struct lysc_node *schema;
798 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
799
800 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
801
Michal Vaskof03ed032020-03-04 13:31:44 +0100802 if (!module) {
803 module = parent->schema->module;
804 }
805
Michal Vasko013a8182020-03-03 10:46:53 +0100806 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
807 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
808
Michal Vaskof03ed032020-03-04 13:31:44 +0100809 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
810 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100811 lyd_insert_node(parent, NULL, ret);
812 }
813 return ret;
814}
815
816API struct lyd_node *
817lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
818 LYD_ANYDATA_VALUETYPE value_type)
819{
820 struct lyd_node *ret = NULL;
821 const struct lysc_node *schema;
822 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
823
824 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
825
Michal Vaskof03ed032020-03-04 13:31:44 +0100826 if (!module) {
827 module = parent->schema->module;
828 }
829
Michal Vasko013a8182020-03-03 10:46:53 +0100830 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
831 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
832
833 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
834 lyd_insert_node(parent, NULL, ret);
835 }
836 return ret;
837}
838
Michal Vasko90932a92020-02-12 14:33:03 +0100839struct lyd_node *
840lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
841{
842 const struct lysc_node *prev_key;
843 struct lyd_node *match = NULL;
844
845 if (!first_sibling) {
846 return NULL;
847 }
848
849 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
850 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
851 }
852
853 return match;
854}
855
856/**
857 * @brief Insert node after a sibling.
858 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100859 * Handles inserting into NP containers and key-less lists.
860 *
Michal Vasko90932a92020-02-12 14:33:03 +0100861 * @param[in] sibling Sibling to insert after.
862 * @param[in] node Node to insert.
863 */
864static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100865lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100866{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100867 struct lyd_node_inner *par;
868
Michal Vasko90932a92020-02-12 14:33:03 +0100869 assert(!node->next && (node->prev == node));
870
871 node->next = sibling->next;
872 node->prev = sibling;
873 sibling->next = node;
874 if (node->next) {
875 /* sibling had a succeeding node */
876 node->next->prev = node;
877 } else {
878 /* sibling was last, find first sibling and change its prev */
879 if (sibling->parent) {
880 sibling = sibling->parent->child;
881 } else {
882 for (; sibling->prev->next != node; sibling = sibling->prev);
883 }
884 sibling->prev = node;
885 }
886 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100887
Michal Vasko9f96a052020-03-10 09:41:45 +0100888 for (par = node->parent; par; par = par->parent) {
889 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
890 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100891 par->flags &= ~LYD_DEFAULT;
892 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100893 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
894 /* rehash key-less list */
895 lyd_hash((struct lyd_node *)par);
896 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100897 }
898
899 /* insert into hash table */
900 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100901}
902
903/**
904 * @brief Insert node before a sibling.
905 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100906 * Handles inserting into NP containers and key-less lists.
907 *
Michal Vasko90932a92020-02-12 14:33:03 +0100908 * @param[in] sibling Sibling to insert before.
909 * @param[in] node Node to insert.
910 */
911static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100912lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100913{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100914 struct lyd_node_inner *par;
915
Michal Vasko90932a92020-02-12 14:33:03 +0100916 assert(!node->next && (node->prev == node));
917
918 node->next = sibling;
919 /* covers situation of sibling being first */
920 node->prev = sibling->prev;
921 sibling->prev = node;
922 if (node->prev->next) {
923 /* sibling had a preceding node */
924 node->prev->next = node;
925 } else if (sibling->parent) {
926 /* sibling was first and we must also change parent child pointer */
927 sibling->parent->child = node;
928 }
929 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100930
Michal Vasko9f96a052020-03-10 09:41:45 +0100931 for (par = node->parent; par; par = par->parent) {
932 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
933 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100934 par->flags &= ~LYD_DEFAULT;
935 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100936 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
937 /* rehash key-less list */
938 lyd_hash((struct lyd_node *)par);
939 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100940 }
941
942 /* insert into hash table */
943 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100944}
945
946/**
947 * @brief Insert node as the last child of a parent.
948 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100949 * Handles inserting into NP containers and key-less lists.
950 *
Michal Vasko90932a92020-02-12 14:33:03 +0100951 * @param[in] parent Parent to insert into.
952 * @param[in] node Node to insert.
953 */
954static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100955lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100956{
957 struct lyd_node_inner *par;
958
Michal Vasko0249f7c2020-03-05 16:36:40 +0100959 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100960 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100961
962 par = (struct lyd_node_inner *)parent;
963
964 if (!par->child) {
965 par->child = node;
966 } else {
967 node->prev = par->child->prev;
968 par->child->prev->next = node;
969 par->child->prev = node;
970 }
971 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100972
Michal Vasko9f96a052020-03-10 09:41:45 +0100973 for (; par; par = par->parent) {
974 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
975 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100976 par->flags &= ~LYD_DEFAULT;
977 }
Michal Vasko52927e22020-03-16 17:26:14 +0100978 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100979 /* rehash key-less list */
980 lyd_hash((struct lyd_node *)par);
981 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100982 }
983
984 /* insert into hash table */
985 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100986}
987
988void
Michal Vasko9b368d32020-02-14 13:53:31 +0100989lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100990{
Michal Vasko9b368d32020-02-14 13:53:31 +0100991 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +0100992 const struct lysc_node *skey = NULL;
993 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +0100994
Michal Vasko52927e22020-03-16 17:26:14 +0100995 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +0100996
997 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
998 parent = (struct lyd_node *)(*first_sibling)->parent;
999 }
Michal Vasko90932a92020-02-12 14:33:03 +01001000
1001 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001002 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001003 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001004 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1005 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001006 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001007 } else if (lyd_node_children(parent)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001008 lyd_insert_before_node((struct lyd_node *)lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001009 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001010 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001011 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001012
1013 /* hash list if all its keys were added */
1014 assert(parent->schema->nodetype == LYS_LIST);
1015 anchor = (struct lyd_node *)lyd_node_children(parent);
1016 has_keys = 1;
1017 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1018 if (!anchor || (anchor->schema != skey)) {
1019 /* key missing */
1020 has_keys = 0;
1021 break;
1022 }
1023
1024 anchor = anchor->next;
1025 }
1026 if (has_keys) {
1027 lyd_hash(parent);
1028 }
1029
Michal Vasko90932a92020-02-12 14:33:03 +01001030 } else {
1031 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001032 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001033 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001034 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001035 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001036 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001037 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001038 anchor = anchor->prev;
1039 }
1040
Michal Vaskoc193ce92020-03-06 11:04:48 +01001041 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001042 /* insert after last sibling from this module */
1043 lyd_insert_after_node(anchor, node);
1044 } else {
1045 /* no data from this module, insert at the last position */
1046 lyd_insert_after_node((*first_sibling)->prev, node);
1047 }
Michal Vasko90932a92020-02-12 14:33:03 +01001048 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001049 /* the only sibling */
1050 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001051 }
Michal Vasko90932a92020-02-12 14:33:03 +01001052}
1053
Michal Vaskof03ed032020-03-04 13:31:44 +01001054static LY_ERR
1055lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1056{
1057 const struct lysc_node *par2;
1058
1059 assert(schema);
1060
1061 /* adjust parent first */
1062 while (parent && (parent->nodetype & (LYS_CASE | LYS_CHOICE))) {
1063 parent = parent->parent;
1064 }
1065
1066 /* find schema parent */
1067 for (par2 = schema->parent; par2 && (par2->nodetype & (LYS_CASE | LYS_CHOICE)); par2 = par2->parent);
1068
1069 if (parent) {
1070 /* inner node */
1071 if (par2 != parent) {
1072 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1073 return LY_EINVAL;
1074 }
1075 } else {
1076 /* top-level node */
1077 if (par2) {
1078 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1079 return LY_EINVAL;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
1086API LY_ERR
1087lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1088{
1089 struct lyd_node *iter;
1090
1091 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1092
1093 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1094
1095 if (node->schema->flags & LYS_KEY) {
1096 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1097 return LY_EINVAL;
1098 }
1099
1100 if (node->parent || node->prev->next) {
1101 lyd_unlink_tree(node);
1102 }
1103
1104 while (node) {
1105 iter = node->next;
1106 lyd_unlink_tree(node);
1107 lyd_insert_node(parent, NULL, node);
1108 node = iter;
1109 }
1110 return LY_SUCCESS;
1111}
1112
1113API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001114lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1115{
1116 struct lyd_node *iter;
1117
1118 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1119
1120 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1121
1122 if (node->schema->flags & LYS_KEY) {
1123 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1124 return LY_EINVAL;
1125 }
1126
1127 if (node->parent || node->prev->next) {
1128 lyd_unlink_tree(node);
1129 }
1130
1131 while (node) {
1132 iter = node->next;
1133 lyd_unlink_tree(node);
1134 lyd_insert_node(NULL, &sibling, node);
1135 node = iter;
1136 }
1137 return LY_SUCCESS;
1138}
1139
Michal Vasko0249f7c2020-03-05 16:36:40 +01001140static LY_ERR
1141lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1142{
1143 if (sibling->parent) {
1144 /* nested, we do not care for the order */
1145 return LY_SUCCESS;
1146 }
1147
1148 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001149 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1150 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001151 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 +01001152 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001153 return LY_EINVAL;
1154 }
1155
Michal Vaskoc193ce92020-03-06 11:04:48 +01001156 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1157 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001158 /* inserting before/after its module data */
1159 return LY_SUCCESS;
1160 }
1161 }
1162
1163 /* find first sibling */
1164 while (sibling->prev->next) {
1165 sibling = sibling->prev;
1166 }
1167
1168 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001169 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001170 /* inserting before its module data */
1171 return LY_SUCCESS;
1172 }
1173 }
1174
1175 /* check there are no data of this module */
1176 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001177 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001178 /* some data of this module found */
1179 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001180 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001181 return LY_EINVAL;
1182 }
1183 }
1184
1185 return LY_SUCCESS;
1186}
1187
Michal Vaskob1b5c262020-03-05 14:29:47 +01001188API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001189lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1190{
1191 struct lyd_node *iter;
1192
1193 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1194
1195 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1196
1197 if (node->schema->flags & LYS_KEY) {
1198 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1199 return LY_EINVAL;
1200 } else if (sibling->schema->flags & LYS_KEY) {
1201 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1202 return LY_EINVAL;
1203 }
1204
Michal Vasko0249f7c2020-03-05 16:36:40 +01001205 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1206
Michal Vaskof03ed032020-03-04 13:31:44 +01001207 if (node->parent || node->prev->next) {
1208 lyd_unlink_tree(node);
1209 }
1210
1211 /* insert in reverse order to get the original order */
1212 node = node->prev;
1213 while (node) {
1214 iter = node->prev;
1215 lyd_unlink_tree(node);
1216
1217 lyd_insert_before_node(sibling, node);
1218 /* move the anchor accordingly */
1219 sibling = node;
1220
1221 node = (iter == node) ? NULL : iter;
1222 }
1223 return LY_SUCCESS;
1224}
1225
1226API LY_ERR
1227lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1228{
1229 struct lyd_node *iter;
1230
1231 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1232
1233 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1234
1235 if (node->schema->flags & LYS_KEY) {
1236 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1237 return LY_EINVAL;
1238 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1239 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1240 return LY_EINVAL;
1241 }
1242
Michal Vasko0249f7c2020-03-05 16:36:40 +01001243 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1244
Michal Vaskof03ed032020-03-04 13:31:44 +01001245 if (node->parent || node->prev->next) {
1246 lyd_unlink_tree(node);
1247 }
1248
1249 while (node) {
1250 iter = node->next;
1251 lyd_unlink_tree(node);
1252
1253 lyd_insert_after_node(sibling, node);
1254 /* move the anchor accordingly */
1255 sibling = node;
1256
1257 node = iter;
1258 }
1259 return LY_SUCCESS;
1260}
1261
1262API void
1263lyd_unlink_tree(struct lyd_node *node)
1264{
1265 struct lyd_node *iter;
1266
1267 if (!node) {
1268 return;
1269 }
1270
1271 /* unlink from siblings */
1272 if (node->prev->next) {
1273 node->prev->next = node->next;
1274 }
1275 if (node->next) {
1276 node->next->prev = node->prev;
1277 } else {
1278 /* unlinking the last node */
1279 if (node->parent) {
1280 iter = node->parent->child;
1281 } else {
1282 iter = node->prev;
1283 while (iter->prev != node) {
1284 iter = iter->prev;
1285 }
1286 }
1287 /* update the "last" pointer from the first node */
1288 iter->prev = node->prev;
1289 }
1290
1291 /* unlink from parent */
1292 if (node->parent) {
1293 if (node->parent->child == node) {
1294 /* the node is the first child */
1295 node->parent->child = node->next;
1296 }
1297
1298 lyd_unlink_hash(node);
1299
1300 /* check for keyless list and update its hash */
1301 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
1302 if (iter->schema->flags & LYS_KEYLESS) {
1303 lyd_hash(iter);
1304 }
1305 }
1306
1307 node->parent = NULL;
1308 }
1309
1310 node->next = NULL;
1311 node->prev = node;
1312}
1313
Michal Vasko90932a92020-02-12 14:33:03 +01001314LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001315lyd_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 +01001316 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 +01001317 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001318{
1319 LY_ERR ret;
1320 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001321 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001322 uint32_t v;
1323
Michal Vasko9f96a052020-03-10 09:41:45 +01001324 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001325
Michal Vasko90932a92020-02-12 14:33:03 +01001326 LY_ARRAY_FOR(mod->compiled->exts, v) {
1327 if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1328 !ly_strncmp(mod->compiled->exts[v].argument, name, name_len)) {
1329 /* we have the annotation definition */
1330 ant = &mod->compiled->exts[v];
1331 break;
1332 }
1333 }
1334 if (!ant) {
1335 /* attribute is not defined as a metadata annotation (RFC 7952) */
1336 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1337 mod->name, name_len, name);
1338 return LY_EINVAL;
1339 }
1340
Michal Vasko9f96a052020-03-10 09:41:45 +01001341 mt = calloc(1, sizeof *mt);
1342 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1343 mt->parent = parent;
1344 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001345 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 +01001346 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001347 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001348 return ret;
1349 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001350 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001351
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001352 /* insert as the last attribute */
1353 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001354 if (parent->meta) {
1355 for (last = parent->meta; last->next; last = last->next);
1356 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001357 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001358 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001359 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001360 } else if (*meta) {
1361 for (last = *meta; last->next; last = last->next);
1362 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001363 }
1364
1365 /* remove default flags from NP containers */
1366 while (parent && (parent->flags & LYD_DEFAULT)) {
1367 parent->flags &= ~LYD_DEFAULT;
1368 parent = (struct lyd_node *)parent->parent;
1369 }
1370
Michal Vasko9f96a052020-03-10 09:41:45 +01001371 if (meta) {
1372 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001373 }
1374 return ret;
1375}
1376
Michal Vasko52927e22020-03-16 17:26:14 +01001377LY_ERR
1378ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1379 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1380 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1381{
1382 struct ly_attr *at, *last;
1383 struct lyd_node_opaq *opaq;
1384
1385 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1386 assert(name && name_len);
1387 assert((prefix_len && ns) || (!prefix_len && !ns));
1388
1389 if (!value_len) {
1390 value = "";
1391 }
1392
1393 at = calloc(1, sizeof *at);
1394 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1395 at->parent = (struct lyd_node_opaq *)parent;
1396 at->name = lydict_insert(ctx, name, name_len);
1397 if (dynamic && *dynamic) {
1398 at->value = lydict_insert_zc(ctx, (char *)value);
1399 *dynamic = 0;
1400 } else {
1401 at->value = lydict_insert(ctx, value, value_len);
1402 }
1403
1404 at->format = format;
1405 at->val_prefs = val_prefs;
1406 if (ns) {
1407 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1408 at->prefix.ns = lydict_insert(ctx, ns, 0);
1409 }
1410
1411 /* insert as the last attribute */
1412 if (parent) {
1413 opaq = (struct lyd_node_opaq *)parent;
1414 if (opaq->attr) {
1415 for (last = opaq->attr; last->next; last = last->next);
1416 last->next = at;
1417 } else {
1418 opaq->attr = at;
1419 }
1420 } else if (*attr) {
1421 for (last = *attr; last->next; last = last->next);
1422 last->next = at;
1423 }
1424
1425 if (attr) {
1426 *attr = at;
1427 }
1428 return LY_SUCCESS;
1429}
1430
Radek Krejci084289f2019-07-09 17:35:30 +02001431API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001432lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001433{
Michal Vaskof03ed032020-03-04 13:31:44 +01001434 unsigned int u, x;
Michal Vaskoe444f752020-02-10 12:20:06 +01001435 const struct lyd_node *parent = NULL, *start_search;
1436 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001437 uint64_t pos = 1;
1438
Michal Vaskof03ed032020-03-04 13:31:44 +01001439 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001440
1441 LY_ARRAY_FOR(path, u) {
1442 if (parent) {
1443 start_search = lyd_node_children(parent);
1444search_inner:
Michal Vaskoe444f752020-02-10 12:20:06 +01001445 lyd_find_sibling_next(start_search, path[u].node->module, path[u].node->name, 0, NULL, 0, &node);
Radek Krejci084289f2019-07-09 17:35:30 +02001446 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001447 start_search = tree;
Radek Krejci084289f2019-07-09 17:35:30 +02001448search_toplevel:
Michal Vaskof03ed032020-03-04 13:31:44 +01001449 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
1450 lyd_find_sibling_next(start_search, path[u].node->module, path[u].node->name, 0, NULL, 0, &node);
Radek Krejci084289f2019-07-09 17:35:30 +02001451 }
1452 if (!node) {
1453 return NULL;
1454 }
1455
1456 /* check predicate if any */
1457 LY_ARRAY_FOR(path[u].predicates, x) {
1458 if (path[u].predicates[x].type == 0) {
1459 /* position predicate */
1460 if (pos != path[u].predicates[x].position) {
1461 pos++;
1462 goto search_repeat;
1463 }
1464 /* done, no more predicates are allowed here */
1465 break;
1466 } else if (path[u].predicates[x].type == 1) {
1467 /* key-predicate */
1468 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001469 struct lyd_node *key;
1470 lyd_find_sibling_next(lyd_node_children(node), path[u].predicates[x].key->module,
1471 path[u].predicates[x].key->name, 0, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001472 if (!key) {
1473 /* probably error and we shouldn't be here due to previous checks when creating path */
1474 goto search_repeat;
1475 }
1476 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
1477 goto search_repeat;
1478 }
1479 } else if (path[u].predicates[x].type == 2) {
1480 /* leaf-list-predicate */
1481 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
1482 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
1483 goto search_repeat;
1484 }
1485 } else {
1486 LOGINT(NULL);
1487 }
1488 }
1489
1490 parent = node;
1491 }
1492
1493 return (const struct lyd_node_term*)node;
1494
1495search_repeat:
1496 start_search = node->next;
1497 if (parent) {
1498 goto search_inner;
1499 } else {
1500 goto search_toplevel;
1501 }
1502}
1503
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001504API LY_ERR
1505lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1506{
1507 const struct lyd_node *iter1, *iter2;
1508 struct lyd_node_term *term1, *term2;
1509 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001510 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001511 struct lysc_type *type;
1512 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001513
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001514 if (!node1 || !node2) {
1515 if (node1 == node2) {
1516 return LY_SUCCESS;
1517 } else {
1518 return LY_ENOT;
1519 }
1520 }
1521
Michal Vasko52927e22020-03-16 17:26:14 +01001522 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001523 return LY_ENOT;
1524 }
1525
1526 if (node1->hash != node2->hash) {
1527 return LY_ENOT;
1528 }
Michal Vasko52927e22020-03-16 17:26:14 +01001529 /* 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 +02001530
Michal Vasko52927e22020-03-16 17:26:14 +01001531 if (!node1->schema) {
1532 opaq1 = (struct lyd_node_opaq *)node1;
1533 opaq2 = (struct lyd_node_opaq *)node2;
1534 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001535 return LY_ENOT;
1536 }
Michal Vasko52927e22020-03-16 17:26:14 +01001537 switch (opaq1->format) {
1538 case LYD_XML:
1539 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1540 return LY_ENOT;
1541 }
1542 break;
1543 case LYD_SCHEMA:
1544 /* not allowed */
1545 LOGINT(LYD_NODE_CTX(node1));
1546 return LY_EINT;
1547 }
1548 if (options & LYD_COMPARE_FULL_RECURSION) {
1549 iter1 = opaq1->child;
1550 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001551 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001552 }
1553 return LY_SUCCESS;
1554 } else {
1555 switch (node1->schema->nodetype) {
1556 case LYS_LEAF:
1557 case LYS_LEAFLIST:
1558 if (options & LYD_COMPARE_DEFAULTS) {
1559 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1560 return LY_ENOT;
1561 }
1562 }
1563
1564 term1 = (struct lyd_node_term*)node1;
1565 term2 = (struct lyd_node_term*)node2;
1566 type = ((struct lysc_node_leaf*)node1->schema)->type;
1567
1568 return type->plugin->compare(&term1->value, &term2->value);
1569 case LYS_CONTAINER:
1570 if (options & LYD_COMPARE_DEFAULTS) {
1571 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1572 return LY_ENOT;
1573 }
1574 }
1575 if (options & LYD_COMPARE_FULL_RECURSION) {
1576 iter1 = ((struct lyd_node_inner*)node1)->child;
1577 iter2 = ((struct lyd_node_inner*)node2)->child;
1578 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001579 }
1580 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001581 case LYS_ACTION:
1582 if (options & LYD_COMPARE_FULL_RECURSION) {
1583 /* TODO action/RPC
1584 goto all_children_compare;
1585 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001586 }
1587 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001588 case LYS_NOTIF:
1589 if (options & LYD_COMPARE_FULL_RECURSION) {
1590 /* TODO Notification
1591 goto all_children_compare;
1592 */
1593 }
1594 return LY_SUCCESS;
1595 case LYS_LIST:
1596 iter1 = ((struct lyd_node_inner*)node1)->child;
1597 iter2 = ((struct lyd_node_inner*)node2)->child;
1598
1599 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1600 /* lists with keys, their equivalence is based on their keys */
1601 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1602 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1603 key = key->next) {
1604 if (lyd_compare(iter1, iter2, options)) {
1605 return LY_ENOT;
1606 }
1607 iter1 = iter1->next;
1608 iter2 = iter2->next;
1609 }
1610 } else {
1611 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1612
1613 all_children_compare:
1614 if (!iter1 && !iter2) {
1615 /* no children, nothing to compare */
1616 return LY_SUCCESS;
1617 }
1618
1619 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1620 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1621 return LY_ENOT;
1622 }
1623 }
1624 if (iter1 || iter2) {
1625 return LY_ENOT;
1626 }
1627 }
1628 return LY_SUCCESS;
1629 case LYS_ANYXML:
1630 case LYS_ANYDATA:
1631 any1 = (struct lyd_node_any*)node1;
1632 any2 = (struct lyd_node_any*)node2;
1633
1634 if (any1->value_type != any2->value_type) {
1635 return LY_ENOT;
1636 }
1637 switch (any1->value_type) {
1638 case LYD_ANYDATA_DATATREE:
1639 iter1 = any1->value.tree;
1640 iter2 = any2->value.tree;
1641 goto all_children_compare;
1642 case LYD_ANYDATA_STRING:
1643 case LYD_ANYDATA_XML:
1644 case LYD_ANYDATA_JSON:
1645 len1 = strlen(any1->value.str);
1646 len2 = strlen(any2->value.str);
1647 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1648 return LY_ENOT;
1649 }
1650 return LY_SUCCESS;
1651 #if 0 /* TODO LYB format */
1652 case LYD_ANYDATA_LYB:
1653 int len1 = lyd_lyb_data_length(any1->value.mem);
1654 int len2 = lyd_lyb_data_length(any2->value.mem);
1655 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1656 return LY_ENOT;
1657 }
1658 return LY_SUCCESS;
1659 #endif
1660 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001661 }
1662 }
1663
Michal Vasko52927e22020-03-16 17:26:14 +01001664 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001665 return LY_EINT;
1666}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001667
1668/**
Michal Vasko52927e22020-03-16 17:26:14 +01001669 * @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 +02001670 *
1671 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
1672 */
Michal Vasko52927e22020-03-16 17:26:14 +01001673static LY_ERR
1674lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1675 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001676{
Michal Vasko52927e22020-03-16 17:26:14 +01001677 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001678 struct lyd_node *dup = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +01001679 uint32_t u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001680
Michal Vasko52927e22020-03-16 17:26:14 +01001681 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001682
Michal Vasko52927e22020-03-16 17:26:14 +01001683 if (!node->schema) {
1684 dup = calloc(1, sizeof(struct lyd_node_opaq));
1685 } else {
1686 switch (node->schema->nodetype) {
1687 case LYS_ACTION:
1688 case LYS_NOTIF:
1689 case LYS_CONTAINER:
1690 case LYS_LIST:
1691 dup = calloc(1, sizeof(struct lyd_node_inner));
1692 break;
1693 case LYS_LEAF:
1694 case LYS_LEAFLIST:
1695 dup = calloc(1, sizeof(struct lyd_node_term));
1696 break;
1697 case LYS_ANYDATA:
1698 case LYS_ANYXML:
1699 dup = calloc(1, sizeof(struct lyd_node_any));
1700 break;
1701 default:
1702 LOGINT(LYD_NODE_CTX(node));
1703 ret = LY_EINT;
1704 goto error;
1705 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001706 }
Michal Vasko52927e22020-03-16 17:26:14 +01001707 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001708
1709 /* TODO implement LYD_DUP_WITH_WHEN */
1710 dup->flags = node->flags;
1711 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001712 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001713
1714 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1715
1716 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001717 if (!dup->schema) {
1718 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1719 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1720 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001721
1722 if (options & LYD_DUP_RECURSIVE) {
1723 /* duplicate all the children */
1724 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001725 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1726 }
1727 }
1728 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1729 opaq->format = orig->format;
1730 if (orig->prefix.pref) {
1731 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1732 }
1733 if (orig->prefix.ns) {
1734 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1735 }
1736 if (orig->val_prefs) {
1737 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1738 LY_ARRAY_FOR(orig->val_prefs, u) {
1739 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1740 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1741 LY_ARRAY_INCREMENT(opaq->val_prefs);
1742 }
1743 }
1744 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1745 opaq->ctx = orig->ctx;
1746 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1747 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1748 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1749
1750 term->hash = orig->hash;
1751 term->value.realtype = orig->value.realtype;
1752 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1753 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1754 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1755 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1756 struct lyd_node *child;
1757
1758 if (options & LYD_DUP_RECURSIVE) {
1759 /* duplicate all the children */
1760 LY_LIST_FOR(orig->child, child) {
1761 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001762 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001763 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001764 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001765 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001766 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001767 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1768 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001769 if (!child) {
1770 /* possibly not keys are present in filtered tree */
1771 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001772 } else if (child->schema != key) {
1773 /* possibly not all keys are present in filtered tree,
1774 * but there can be also some non-key nodes */
1775 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001776 }
Michal Vasko52927e22020-03-16 17:26:14 +01001777 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001778 child = child->next;
1779 }
1780 }
1781 lyd_hash(dup);
1782 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001783 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1784 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001785
1786 any->hash = orig->hash;
1787 any->value_type = orig->value_type;
1788 switch (any->value_type) {
1789 case LYD_ANYDATA_DATATREE:
1790 if (orig->value.tree) {
1791 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
1792 LY_CHECK_GOTO(!any->value.tree, error);
1793 }
1794 break;
1795 case LYD_ANYDATA_STRING:
1796 case LYD_ANYDATA_XML:
1797 case LYD_ANYDATA_JSON:
1798 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001799 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001800 }
1801 break;
1802 }
1803 }
1804
Michal Vasko52927e22020-03-16 17:26:14 +01001805 /* insert */
1806 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001807 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001808
1809 if (dup_p) {
1810 *dup_p = dup;
1811 }
1812 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001813
1814error:
Michal Vasko52927e22020-03-16 17:26:14 +01001815 lyd_free_tree(dup);
1816 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001817}
1818
1819API struct lyd_node *
1820lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1821{
1822 struct ly_ctx *ctx;
1823 const struct lyd_node *orig; /* original node to be duplicated */
1824 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001825 struct lyd_node *top = NULL; /* the most higher created node */
1826 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1827 int keyless_parent_list = 0;
1828
1829 LY_CHECK_ARG_RET(NULL, node, NULL);
1830 ctx = node->schema->module->ctx;
1831
1832 if (options & LYD_DUP_WITH_PARENTS) {
1833 struct lyd_node_inner *orig_parent, *iter;
1834 int repeat = 1;
1835 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1836 if (parent && parent->schema == orig_parent->schema) {
1837 /* stop creating parents, connect what we have into the provided parent */
1838 iter = parent;
1839 repeat = 0;
1840 /* get know if there is a keyless list which we will have to rehash */
1841 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001842 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001843 keyless_parent_list = 1;
1844 break;
1845 }
1846 }
1847 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001848 iter = NULL;
1849 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1850 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001851 }
1852 if (!local_parent) {
1853 local_parent = iter;
1854 }
1855 if (iter->child) {
1856 /* 1) list - add after keys
1857 * 2) provided parent with some children */
1858 iter->child->prev->next = top;
1859 if (top) {
1860 top->prev = iter->child->prev;
1861 iter->child->prev = top;
1862 }
1863 } else {
1864 iter->child = top;
1865 if (iter->schema->nodetype == LYS_LIST) {
1866 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1867 keyless_parent_list = 1;
1868 }
1869 }
1870 if (top) {
1871 top->parent = iter;
1872 }
1873 top = (struct lyd_node*)iter;
1874 }
1875 if (repeat && parent) {
1876 /* given parent and created parents chain actually do not interconnect */
1877 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1878 goto error;
1879 }
1880 } else {
1881 local_parent = parent;
1882 }
1883
Radek Krejci22ebdba2019-07-25 13:59:43 +02001884 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001885 /* if there is no local parent, it will be inserted into first */
1886 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 +02001887 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1888 break;
1889 }
1890 }
1891 if (keyless_parent_list) {
1892 /* rehash */
1893 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001894 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001895 lyd_hash((struct lyd_node*)local_parent);
1896 }
1897 }
1898 }
1899 return first;
1900
1901error:
1902 if (top) {
1903 lyd_free_tree(top);
1904 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001905 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001906 }
1907 return NULL;
1908}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001909
1910static LY_ERR
1911lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
1912{
Michal Vasko14654712020-02-06 08:35:21 +01001913 /* ending \0 */
1914 ++reqlen;
1915
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001916 if (reqlen > *buflen) {
1917 if (is_static) {
1918 return LY_EINCOMPLETE;
1919 }
1920
1921 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
1922 if (!*buffer) {
1923 return LY_EMEM;
1924 }
1925
1926 *buflen = reqlen;
1927 }
1928
1929 return LY_SUCCESS;
1930}
1931
1932/**
1933 * @brief Append all list key predicates to path.
1934 *
1935 * @param[in] node Node with keys to print.
1936 * @param[in,out] buffer Buffer to print to.
1937 * @param[in,out] buflen Current buffer length.
1938 * @param[in,out] bufused Current number of characters used in @p buffer.
1939 * @param[in] is_static Whether buffer is static or can be reallocated.
1940 * @return LY_ERR
1941 */
1942static LY_ERR
1943lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1944{
1945 const struct lyd_node *key;
1946 int dynamic = 0;
1947 size_t len;
1948 const char *val;
1949 char quot;
1950 LY_ERR rc;
1951
Michal Vasko14654712020-02-06 08:35:21 +01001952 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001953 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
1954 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
1955 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1956 if (rc != LY_SUCCESS) {
1957 if (dynamic) {
1958 free((char *)val);
1959 }
1960 return rc;
1961 }
1962
1963 quot = '\'';
1964 if (strchr(val, '\'')) {
1965 quot = '"';
1966 }
1967 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
1968
1969 if (dynamic) {
1970 free((char *)val);
1971 }
1972 }
1973
1974 return LY_SUCCESS;
1975}
1976
1977/**
1978 * @brief Append leaf-list value predicate to path.
1979 *
1980 * @param[in] node Node to print.
1981 * @param[in,out] buffer Buffer to print to.
1982 * @param[in,out] buflen Current buffer length.
1983 * @param[in,out] bufused Current number of characters used in @p buffer.
1984 * @param[in] is_static Whether buffer is static or can be reallocated.
1985 * @return LY_ERR
1986 */
1987static LY_ERR
1988lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1989{
1990 int dynamic = 0;
1991 size_t len;
1992 const char *val;
1993 char quot;
1994 LY_ERR rc;
1995
1996 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
1997 len = 4 + strlen(val) + 2;
1998 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1999 if (rc != LY_SUCCESS) {
2000 goto cleanup;
2001 }
2002
2003 quot = '\'';
2004 if (strchr(val, '\'')) {
2005 quot = '"';
2006 }
2007 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2008
2009cleanup:
2010 if (dynamic) {
2011 free((char *)val);
2012 }
2013 return rc;
2014}
2015
2016/**
2017 * @brief Append node position (relative to its other instances) predicate to path.
2018 *
2019 * @param[in] node Node to print.
2020 * @param[in,out] buffer Buffer to print to.
2021 * @param[in,out] buflen Current buffer length.
2022 * @param[in,out] bufused Current number of characters used in @p buffer.
2023 * @param[in] is_static Whether buffer is static or can be reallocated.
2024 * @return LY_ERR
2025 */
2026static LY_ERR
2027lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2028{
2029 const struct lyd_node *first, *iter;
2030 size_t len;
2031 int pos;
2032 char *val = NULL;
2033 LY_ERR rc;
2034
2035 if (node->parent) {
2036 first = node->parent->child;
2037 } else {
2038 for (first = node; node->prev->next; node = node->prev);
2039 }
2040 pos = 1;
2041 for (iter = first; iter != node; iter = iter->next) {
2042 if (iter->schema == node->schema) {
2043 ++pos;
2044 }
2045 }
2046 if (asprintf(&val, "%d", pos) == -1) {
2047 return LY_EMEM;
2048 }
2049
2050 len = 1 + strlen(val) + 1;
2051 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2052 if (rc != LY_SUCCESS) {
2053 goto cleanup;
2054 }
2055
2056 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2057
2058cleanup:
2059 free(val);
2060 return rc;
2061}
2062
2063API char *
2064lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2065{
Michal Vasko14654712020-02-06 08:35:21 +01002066 int is_static = 0, i, depth;
2067 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002068 const struct lyd_node *iter;
2069 const struct lys_module *mod;
2070 LY_ERR rc;
2071
2072 LY_CHECK_ARG_RET(NULL, node, NULL);
2073 if (buffer) {
2074 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2075 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002076 } else {
2077 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002078 }
2079
2080 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002081 case LYD_PATH_LOG:
2082 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002083 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2084 ++depth;
2085 }
2086
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002087 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002088 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002089 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002090 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002091iter_print:
2092 /* print prefix and name */
2093 mod = NULL;
2094 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2095 mod = iter->schema->module;
2096 }
2097
2098 /* realloc string */
2099 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2100 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2101 if (rc != LY_SUCCESS) {
2102 break;
2103 }
2104
2105 /* print next node */
2106 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2107
2108 switch (iter->schema->nodetype) {
2109 case LYS_LIST:
2110 if (iter->schema->flags & LYS_KEYLESS) {
2111 /* print its position */
2112 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2113 } else {
2114 /* print all list keys in predicates */
2115 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2116 }
2117 break;
2118 case LYS_LEAFLIST:
2119 if (iter->schema->flags & LYS_CONFIG_W) {
2120 /* print leaf-list value */
2121 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2122 } else {
2123 /* print its position */
2124 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2125 }
2126 break;
2127 default:
2128 /* nothing to print more */
2129 rc = LY_SUCCESS;
2130 break;
2131 }
2132 if (rc != LY_SUCCESS) {
2133 break;
2134 }
2135
Michal Vasko14654712020-02-06 08:35:21 +01002136 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002137 }
2138 break;
2139 }
2140
2141 return buffer;
2142}
Michal Vaskoe444f752020-02-10 12:20:06 +01002143
Michal Vasko9b368d32020-02-14 13:53:31 +01002144LY_ERR
2145lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2146 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002147{
2148 LY_ERR rc;
2149 const struct lyd_node *node = NULL;
2150 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002151 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002152 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002153 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002154
Michal Vasko9b368d32020-02-14 13:53:31 +01002155 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002156
2157 if (!first) {
2158 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002159 if (match) {
2160 *match = NULL;
2161 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002162 return LY_ENOTFOUND;
2163 }
2164
Michal Vaskoe444f752020-02-10 12:20:06 +01002165 if (key_or_value && !val_len) {
2166 val_len = strlen(key_or_value);
2167 }
2168
2169 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002170 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002171 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 +01002172 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2173 /* parse keys into canonical values */
2174 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, &keys), cleanup);
2175 }
2176
2177 /* find first matching value */
2178 LY_LIST_FOR(first, node) {
2179 if (node->schema != schema) {
2180 continue;
2181 }
2182
2183 if ((schema->nodetype == LYS_LIST) && keys.str) {
2184 /* compare all set keys */
2185 for (i = 0; i < keys.key_count; ++i) {
2186 /* find key */
2187 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2188 (struct lyd_node **)&term);
2189 if (rc == LY_ENOTFOUND) {
2190 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002191 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002192 }
2193 LY_CHECK_GOTO(rc, cleanup);
2194
2195 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002196 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002197 break;
2198 }
2199 }
2200
2201 if (i < keys.key_count) {
2202 /* not a match */
2203 continue;
2204 }
Michal Vasko90932a92020-02-12 14:33:03 +01002205 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002206 term = (struct lyd_node_term *)node;
2207
2208 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002209 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002210 /* not a match */
2211 continue;
2212 }
2213 }
2214
2215 /* all criteria passed */
2216 break;
2217 }
2218
2219 if (!node) {
2220 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002221 if (match) {
2222 *match = NULL;
2223 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002224 goto cleanup;
2225 }
2226
2227 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002228 if (match) {
2229 *match = (struct lyd_node *)node;
2230 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002231 rc = LY_SUCCESS;
2232
2233cleanup:
2234 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002235 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002236 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002237 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002238 return rc;
2239}
2240
2241API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002242lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2243 const char *key_or_value, size_t val_len, struct lyd_node **match)
2244{
2245 const struct lysc_node *schema;
2246
2247 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2248
2249 if (!first) {
2250 /* no data */
2251 *match = NULL;
2252 return LY_ENOTFOUND;
2253 }
2254
2255 /* find schema */
2256 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2257 if (!schema) {
2258 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2259 return LY_EINVAL;
2260 }
2261
2262 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2263}
2264
2265API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002266lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2267{
2268 struct lyd_node **match_p;
2269 struct lyd_node_inner *parent;
2270
Michal Vaskof03ed032020-03-04 13:31:44 +01002271 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002272
2273 if (!siblings) {
2274 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002275 if (match) {
2276 *match = NULL;
2277 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002278 return LY_ENOTFOUND;
2279 }
2280
2281 /* find first sibling */
2282 if (siblings->parent) {
2283 siblings = siblings->parent->child;
2284 } else {
2285 while (siblings->prev->next) {
2286 siblings = siblings->prev;
2287 }
2288 }
2289
2290 parent = (struct lyd_node_inner *)siblings->parent;
2291 if (parent && parent->children_ht) {
2292 assert(target->hash);
2293
2294 /* find by hash */
2295 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2296 siblings = *match_p;
2297 } else {
2298 /* not found */
2299 siblings = NULL;
2300 }
2301 } else {
2302 /* no children hash table */
2303 for (; siblings; siblings = siblings->next) {
2304 if (!lyd_compare(siblings, target, 0)) {
2305 break;
2306 }
2307 }
2308 }
2309
2310 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002311 if (match) {
2312 *match = NULL;
2313 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002314 return LY_ENOTFOUND;
2315 }
2316
Michal Vasko9b368d32020-02-14 13:53:31 +01002317 if (match) {
2318 *match = (struct lyd_node *)siblings;
2319 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002320 return LY_SUCCESS;
2321}
2322
2323API LY_ERR
2324lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2325{
2326 struct lyd_node_inner *parent;
2327 struct lyd_node *match;
2328 struct lyd_node **match_p;
2329 struct ly_set *ret;
2330
2331 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2332
2333 if (!siblings) {
2334 /* no data */
2335 return LY_ENOTFOUND;
2336 }
2337
2338 ret = ly_set_new();
2339 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2340
2341 /* find first sibling */
2342 if (siblings->parent) {
2343 siblings = siblings->parent->child;
2344 } else {
2345 while (siblings->prev->next) {
2346 siblings = siblings->prev;
2347 }
2348 }
2349
2350 parent = (struct lyd_node_inner *)siblings->parent;
2351 if (parent && parent->children_ht) {
2352 assert(target->hash);
2353
2354 /* find by hash */
2355 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2356 match = *match_p;
2357 } else {
2358 /* not found */
2359 match = NULL;
2360 }
2361 while (match) {
2362 /* add all found nodes into the return set */
2363 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2364 goto error;
2365 }
2366
2367 /* find next instance */
2368 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2369 match = NULL;
2370 } else {
2371 match = *match_p;
2372 }
2373 }
2374 } else {
2375 /* no children hash table */
2376 for (; siblings; siblings = siblings->next) {
2377 if (!lyd_compare(siblings, target, 0)) {
2378 /* a match */
2379 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2380 goto error;
2381 }
2382 }
2383 }
2384 }
2385
2386 if (!ret->count) {
2387 ly_set_free(ret, NULL);
2388 return LY_ENOTFOUND;
2389 }
2390
2391 *set = ret;
2392 return LY_SUCCESS;
2393
2394error:
2395 ly_set_free(ret, NULL);
2396 return LY_EMEM;
2397}
2398
Michal Vasko90932a92020-02-12 14:33:03 +01002399static int
2400lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2401{
2402 struct lysc_node *val1;
2403 struct lyd_node *val2;
2404
2405 val1 = *((struct lysc_node **)val1_p);
2406 val2 = *((struct lyd_node **)val2_p);
2407
2408 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2409
2410 if (val1 == val2->schema) {
2411 /* schema match is enough */
2412 return 1;
2413 } else {
2414 return 0;
2415 }
2416}
2417
2418static LY_ERR
2419lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2420{
2421 struct lyd_node **match_p;
2422 struct lyd_node_inner *parent;
2423 uint32_t hash;
2424 values_equal_cb ht_cb;
2425
2426 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2427
2428 /* find first sibling */
2429 if (siblings->parent) {
2430 siblings = siblings->parent->child;
2431 } else {
2432 while (siblings->prev->next) {
2433 siblings = siblings->prev;
2434 }
2435 }
2436
2437 parent = (struct lyd_node_inner *)siblings->parent;
2438 if (parent && parent->children_ht) {
2439 /* calculate our hash */
2440 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2441 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2442 hash = dict_hash_multi(hash, NULL, 0);
2443
2444 /* use special hash table function */
2445 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2446
2447 /* find by hash */
2448 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2449 siblings = *match_p;
2450 } else {
2451 /* not found */
2452 siblings = NULL;
2453 }
2454
2455 /* set the original hash table compare function back */
2456 lyht_set_cb(parent->children_ht, ht_cb);
2457 } else {
2458 /* no children hash table */
2459 for (; siblings; siblings = siblings->next) {
2460 if (siblings->schema == schema) {
2461 /* schema match is enough */
2462 break;
2463 }
2464 }
2465 }
2466
2467 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002468 if (match) {
2469 *match = NULL;
2470 }
Michal Vasko90932a92020-02-12 14:33:03 +01002471 return LY_ENOTFOUND;
2472 }
2473
Michal Vasko9b368d32020-02-14 13:53:31 +01002474 if (match) {
2475 *match = (struct lyd_node *)siblings;
2476 }
Michal Vasko90932a92020-02-12 14:33:03 +01002477 return LY_SUCCESS;
2478}
2479
Michal Vaskoe444f752020-02-10 12:20:06 +01002480API LY_ERR
2481lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2482 size_t val_len, struct lyd_node **match)
2483{
2484 LY_ERR rc;
2485 struct lyd_node *target = NULL;
2486
2487 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
2488 if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) {
2489 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2490 return LY_EINVAL;
2491 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2492 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2493 return LY_EINVAL;
2494 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2495 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2496 lys_nodetype2str(schema->nodetype), __func__);
2497 return LY_EINVAL;
2498 }
2499
2500 if (!siblings) {
2501 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002502 if (match) {
2503 *match = NULL;
2504 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002505 return LY_ENOTFOUND;
2506 }
2507
Michal Vaskof03ed032020-03-04 13:31:44 +01002508 if (key_or_value && !val_len) {
2509 val_len = strlen(key_or_value);
2510 }
2511
Michal Vasko90932a92020-02-12 14:33:03 +01002512 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002513 switch (schema->nodetype) {
2514 case LYS_CONTAINER:
2515 case LYS_ANYXML:
2516 case LYS_ANYDATA:
2517 case LYS_NOTIF:
Michal Vasko9b368d32020-02-14 13:53:31 +01002518 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002519 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002520 /* find it based on schema only */
2521 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002522 break;
2523 case LYS_LEAFLIST:
2524 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002525 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 +01002526 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002527 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002528 if (schema->nodetype == LYS_LIST) {
2529 /* target used attributes: schema, hash, child (all keys) */
2530 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002531 }
2532
2533 /* find it */
2534 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002535 break;
2536 default:
2537 /* unreachable */
2538 LOGINT(schema->module->ctx);
2539 return LY_EINT;
2540 }
2541
Michal Vaskoe444f752020-02-10 12:20:06 +01002542 lyd_free_tree(target);
2543 return rc;
2544}