blob: 61b3ef1f96c0b62db3640ac15fbe0e0473b0eeb7 [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) {
Michal Vaskofea12c62020-03-30 11:00:15 +020069 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
Radek Krejci73dead22019-07-11 16:46:16 +020070 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020071 }
Radek Krejci73dead22019-07-11 16:46:16 +020072 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010073 } else if (dynamic) {
74 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020075 }
76
77error:
78 return ret;
79}
80
Michal Vasko90932a92020-02-12 14:33:03 +010081/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
82static LY_ERR
83lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
84 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
85{
86 LY_ERR ret = LY_SUCCESS;
87 struct ly_err_item *err = NULL;
88 struct ly_ctx *ctx;
89 struct lysc_type *type;
90 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
91
92 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
93
94 ctx = schema->module->ctx;
95 type = ((struct lysc_node_leaf *)schema)->type;
96 val->realtype = type;
97 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
98 val, NULL, &err);
99 if (ret == LY_EINCOMPLETE) {
100 /* this is fine, we do not need it resolved */
101 ret = LY_SUCCESS;
102 } else if (ret && err) {
103 ly_err_print(err);
104 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
105 ly_err_free(err);
106 }
107 if (!ret && dynamic) {
108 *dynamic = 0;
109 }
110
111 return ret;
112}
113
Radek Krejci38d85362019-09-05 16:26:38 +0200114LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100115lyd_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 +0100116 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100117 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200118{
Michal Vasko90932a92020-02-12 14:33:03 +0100119 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200120 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200121 struct lyext_metadata *ant;
122 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100123 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200124
Michal Vasko9f96a052020-03-10 09:41:45 +0100125 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100126
Michal Vasko9f96a052020-03-10 09:41:45 +0100127 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200128
129 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100130 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 }
Michal Vasko90932a92020-02-12 14:33:03 +0100132 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100133 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100134 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200135 if (err) {
136 ly_err_print(err);
137 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
138 ly_err_free(err);
139 }
140 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100141 } else if (dynamic) {
142 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200143 }
144
145error:
146 return ret;
147}
148
Radek Krejci084289f2019-07-09 17:35:30 +0200149API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100150lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci084289f2019-07-09 17:35:30 +0200151 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
152{
153 LY_ERR rc = LY_SUCCESS;
154 struct ly_err_item *err = NULL;
155 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200156
157 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
158
159 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
160 LOGARG(ctx, node);
161 return LY_EINVAL;
162 }
163
164 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200165 /* just validate, no storing of enything */
166 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
167 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
168 if (rc == LY_EINCOMPLETE) {
169 /* actually success since we do not provide the context tree and call validation with
170 * LY_TYPE_OPTS_INCOMPLETE_DATA */
171 rc = LY_SUCCESS;
172 } else if (rc && err) {
173 if (ctx) {
174 /* log only in case the ctx was provided as input parameter */
175 ly_err_print(err);
176 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200177 }
Radek Krejci73dead22019-07-11 16:46:16 +0200178 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200179 }
180
181 return rc;
182}
183
184API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100185lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100186 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 +0200187{
188 LY_ERR rc;
189 struct ly_err_item *err = NULL;
190 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100191 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200192
193 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
194
195 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200196 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100197 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200198 NULL, NULL, &err);
199 if (rc == LY_EINCOMPLETE) {
200 return rc;
201 } else if (rc) {
202 if (err) {
203 if (ctx) {
204 ly_err_print(err);
205 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200206 }
Radek Krejci73dead22019-07-11 16:46:16 +0200207 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200208 }
Radek Krejci73dead22019-07-11 16:46:16 +0200209 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200210 }
211
212 return LY_SUCCESS;
213}
214
215API LY_ERR
216lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100217 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 +0200218{
219 LY_ERR ret = LY_SUCCESS, rc;
220 struct ly_err_item *err = NULL;
221 struct ly_ctx *ctx;
222 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200223 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100224 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200225
226 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
227
228 ctx = node->schema->module->ctx;
229 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200230 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 +0100231 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200232 if (rc == LY_EINCOMPLETE) {
233 ret = rc;
234 /* continue with comparing, just remember what to return if storing is ok */
235 } else if (rc) {
236 /* value to compare is invalid */
237 ret = LY_EINVAL;
238 if (err) {
239 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200240 }
Radek Krejci73dead22019-07-11 16:46:16 +0200241 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200242 }
243
244 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200245 if (type->plugin->compare(&node->value, &data)) {
246 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
247 ret = LY_EVALID;
248 }
Radek Krejci084289f2019-07-09 17:35:30 +0200249
250cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200251 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200252
253 return ret;
254}
255
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200256API const char *
257lyd_value2str(const struct lyd_node_term *node, int *dynamic)
258{
259 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
260
261 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
262}
263
264API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100265lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200266{
Michal Vasko9f96a052020-03-10 09:41:45 +0100267 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200268
Michal Vasko9f96a052020-03-10 09:41:45 +0100269 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200270}
271
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200272API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100273lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200274{
Radek Krejcie7b95092019-05-15 11:03:07 +0200275 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200276#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200277 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200278#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200279
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200280 LY_CHECK_ARG_RET(ctx, ctx, NULL);
281
Michal Vasko5b37a352020-03-06 13:38:33 +0100282 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
283 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
284 return NULL;
285 }
286
Michal Vaskoa3881362020-01-21 15:57:35 +0100287#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200288 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200289 /* first item in trees is mandatory - the RPC/action request */
290 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
291 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
292 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
293 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200294 return NULL;
295 }
296 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200297
Radek Krejcie7b95092019-05-15 11:03:07 +0200298 if (options & LYD_OPT_DATA_TEMPLATE) {
299 yang_data_name = va_arg(ap, const char *);
300 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200301#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200302
303 if (!format) {
304 /* TODO try to detect format from the content */
305 }
306
307 switch (format) {
308 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100309 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 break;
311#if 0
312 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200313 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200314 break;
315 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200316 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 break;
318#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100319 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 LOGINT(ctx);
321 break;
322 }
323
Radek Krejcie7b95092019-05-15 11:03:07 +0200324 return result;
325}
326
327API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100328lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200329{
330 struct lyd_node *result;
331 size_t length;
332 char *addr;
333
334 LY_CHECK_ARG_RET(ctx, ctx, NULL);
335 if (fd < 0) {
336 LOGARG(ctx, fd);
337 return NULL;
338 }
339
340 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100341 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200342 if (addr) {
343 ly_munmap(addr, length);
344 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200345
346 return result;
347}
348
349API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100350lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200351{
352 int fd;
353 struct lyd_node *result;
354 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200355
356 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
357
358 fd = open(path, O_RDONLY);
359 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
360
361 if (!format) {
362 /* unknown format - try to detect it from filename's suffix */
363 len = strlen(path);
364
365 /* ignore trailing whitespaces */
366 for (; len > 0 && isspace(path[len - 1]); len--);
367
368 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
369 format = LYD_XML;
370#if 0
371 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
372 format = LYD_JSON;
373 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
374 format = LYD_LYB;
375#endif
376 } /* else still unknown, try later to detect it from the content */
377 }
378
Michal Vaskoa3881362020-01-21 15:57:35 +0100379 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200380 close(fd);
381
382 return result;
383}
Radek Krejci084289f2019-07-09 17:35:30 +0200384
Michal Vasko90932a92020-02-12 14:33:03 +0100385LY_ERR
386lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
387 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
388{
389 LY_ERR ret;
390 struct lyd_node_term *term;
391
Michal Vasko9b368d32020-02-14 13:53:31 +0100392 assert(schema->nodetype & LYD_NODE_TERM);
393
Michal Vasko90932a92020-02-12 14:33:03 +0100394 term = calloc(1, sizeof *term);
395 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
396
397 term->schema = schema;
398 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100399 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100400
401 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
402 if (ret && (ret != LY_EINCOMPLETE)) {
403 free(term);
404 return ret;
405 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100406 lyd_hash((struct lyd_node *)term);
407
408 *node = (struct lyd_node *)term;
409 return ret;
410}
411
412LY_ERR
413lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
414{
415 LY_ERR ret;
416 struct lyd_node_term *term;
417 struct lysc_type *type;
418
419 assert(schema->nodetype & LYD_NODE_TERM);
420
421 term = calloc(1, sizeof *term);
422 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
423
424 term->schema = schema;
425 term->prev = (struct lyd_node *)term;
426 term->flags = LYD_NEW;
427
428 type = ((struct lysc_node_leaf *)schema)->type;
429 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
430 if (ret) {
431 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
432 free(term);
433 return ret;
434 }
435 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100436
437 *node = (struct lyd_node *)term;
438 return ret;
439}
440
441LY_ERR
442lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
443{
444 struct lyd_node_inner *in;
445
Michal Vasko9b368d32020-02-14 13:53:31 +0100446 assert(schema->nodetype & LYD_NODE_INNER);
447
Michal Vasko90932a92020-02-12 14:33:03 +0100448 in = calloc(1, sizeof *in);
449 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
450
451 in->schema = schema;
452 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100453 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100454
Michal Vasko9b368d32020-02-14 13:53:31 +0100455 /* do not hash list with keys, we need them for the hash */
456 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
457 lyd_hash((struct lyd_node *)in);
458 }
Michal Vasko90932a92020-02-12 14:33:03 +0100459
460 *node = (struct lyd_node *)in;
461 return LY_SUCCESS;
462}
463
464static void
465ly_keys_clean(struct ly_keys *keys)
466{
467 size_t i;
468
469 for (i = 0; i < keys->key_count; ++i) {
470 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
471 }
472 free(keys->str);
473 free(keys->keys);
474}
475
476static char *
477ly_keys_parse_next(char **next_key, char **key_name)
478{
479 char *ptr, *ptr2, *val, quot;
480
481 ptr = *next_key;
482
483 /* "[" */
484 LY_CHECK_GOTO(ptr[0] != '[', error);
485 ++ptr;
486
487 /* key name */
488 ptr2 = strchr(ptr, '=');
489 LY_CHECK_GOTO(!ptr2, error);
490
491 *key_name = ptr;
492 ptr2[0] = '\0';
493
494 /* \0, was '=' */
495 ptr = ptr2 + 1;
496
497 /* quote */
498 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
499 quot = ptr[0];
500 ++ptr;
501
502 /* value, terminate it */
503 val = ptr;
504 ptr2 = strchr(ptr, quot);
505 LY_CHECK_GOTO(!ptr2, error);
506 ptr2[0] = '\0';
507
508 /* \0, was quote */
509 ptr = ptr2 + 1;
510
511 /* "]" */
512 LY_CHECK_GOTO(ptr[0] != ']', error);
513 ++ptr;
514
515 *next_key = ptr;
516 return val;
517
518error:
519 *next_key = ptr;
520 return NULL;
521}
522
523/* fill keys structure; if store is set, fill also each val */
524static LY_ERR
525ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, struct ly_keys *keys)
526{
527 LY_ERR ret = LY_SUCCESS;
528 char *next_key, *name;
529 const struct lysc_node *key;
530 size_t i;
531
532 assert(list->nodetype == LYS_LIST);
533
534 memset(keys, 0, sizeof *keys);
535
Michal Vaskof03ed032020-03-04 13:31:44 +0100536 if (!keys_str) {
537 /* nothing to parse */
538 return LY_SUCCESS;
539 }
540
541 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100542 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
543
544 next_key = keys->str;
545 while (next_key[0]) {
546 /* new key */
547 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
548 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
549
550 /* fill */
551 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
552 if (!keys->keys[keys->key_count].value) {
553 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
554 ret = LY_EINVAL;
555 goto cleanup;
556 }
557
558 /* find schema node */
559 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
560 if (!key) {
561 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
562 ret = LY_EINVAL;
563 goto cleanup;
564 }
565 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
566
567 /* check that we do not have it already */
568 for (i = 0; i < keys->key_count; ++i) {
569 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
570 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
571 ret = LY_EINVAL;
572 goto cleanup;
573 }
574 }
575
576 if (store) {
577 /* store the value */
578 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
579 lydjson_resolve_prefix, NULL, LYD_JSON);
580 LY_CHECK_GOTO(ret, cleanup);
581 }
582
583 /* another valid key */
584 ++keys->key_count;
585 }
586
587cleanup:
588 ly_keys_clean(keys);
589 return ret;
590}
591
592LY_ERR
593lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node)
594{
595 LY_ERR ret = LY_SUCCESS;
596 const struct lysc_node *key_s;
597 struct lyd_node *list = NULL, *key;
598 struct ly_keys keys = {0};
599 size_t i;
600
601 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
602
603 /* parse keys */
604 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, &keys), cleanup);
605
606 /* create list */
607 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
608
609 /* everything was checked except that all keys are set */
610 i = 0;
611 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
612 ++i;
613 }
614 if (i != keys.key_count) {
615 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
616 ret = LY_EINVAL;
617 goto cleanup;
618 }
619
620 /* create and insert all the keys */
621 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskof03ed032020-03-04 13:31:44 +0100622 LY_CHECK_GOTO(ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value,
623 strlen(keys.keys[i].value), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100624 lyd_insert_node(list, NULL, key);
625 }
626
Michal Vasko9b368d32020-02-14 13:53:31 +0100627 /* hash having all the keys */
628 lyd_hash(list);
629
Michal Vasko90932a92020-02-12 14:33:03 +0100630 /* success */
631 *node = list;
632 list = NULL;
633
634cleanup:
635 lyd_free_tree(list);
636 ly_keys_clean(&keys);
637 return ret;
638}
639
640LY_ERR
641lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
642{
643 struct lyd_node_any *any;
644
Michal Vasko9b368d32020-02-14 13:53:31 +0100645 assert(schema->nodetype & LYD_NODE_ANY);
646
Michal Vasko90932a92020-02-12 14:33:03 +0100647 any = calloc(1, sizeof *any);
648 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
649
650 any->schema = schema;
651 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100652 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100653
654 any->value.xml = value;
655 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100656 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100657
658 *node = (struct lyd_node *)any;
659 return LY_SUCCESS;
660}
661
Michal Vasko52927e22020-03-16 17:26:14 +0100662LY_ERR
663lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
664 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
665 const char *ns, struct lyd_node **node)
666{
667 struct lyd_node_opaq *opaq;
668
669 assert(ctx && name && name_len && ns);
670
671 if (!value_len) {
672 value = "";
673 }
674
675 opaq = calloc(1, sizeof *opaq);
676 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
677
678 opaq->prev = (struct lyd_node *)opaq;
679
680 opaq->name = lydict_insert(ctx, name, name_len);
681 opaq->format = format;
682 if (pref_len) {
683 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
684 }
685 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
686 opaq->val_prefs = val_prefs;
687 if (dynamic && *dynamic) {
688 opaq->value = lydict_insert_zc(ctx, (char *)value);
689 *dynamic = 0;
690 } else {
691 opaq->value = lydict_insert(ctx, value, value_len);
692 }
693 opaq->ctx = ctx;
694
695 *node = (struct lyd_node *)opaq;
696 return LY_SUCCESS;
697}
698
Michal Vasko013a8182020-03-03 10:46:53 +0100699API struct lyd_node *
700lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
701{
702 struct lyd_node *ret = NULL;
703 const struct lysc_node *schema;
704 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
705
706 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
707
Michal Vaskof03ed032020-03-04 13:31:44 +0100708 if (!module) {
709 module = parent->schema->module;
710 }
711
Michal Vasko1bf09392020-03-27 12:38:10 +0100712 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100713 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL);
714
715 if (!lyd_create_inner(schema, &ret) && parent) {
716 lyd_insert_node(parent, NULL, ret);
717 }
718 return ret;
719}
720
721API struct lyd_node *
722lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
723{
724 struct lyd_node *ret = NULL, *key;
725 const struct lysc_node *schema, *key_s;
726 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
727 va_list ap;
728 const char *key_val;
729 LY_ERR rc = LY_SUCCESS;
730
731 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
732
Michal Vaskof03ed032020-03-04 13:31:44 +0100733 if (!module) {
734 module = parent->schema->module;
735 }
736
Michal Vasko013a8182020-03-03 10:46:53 +0100737 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
738 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
739
740 /* create list inner node */
741 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
742
743 va_start(ap, name);
744
745 /* create and insert all the keys */
746 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
747 key_val = va_arg(ap, const char *);
748
Michal Vaskof03ed032020-03-04 13:31:44 +0100749 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
750 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100751 lyd_insert_node(ret, NULL, key);
752 }
753
754 /* hash having all the keys */
755 lyd_hash(ret);
756
757 if (parent) {
758 lyd_insert_node(parent, NULL, ret);
759 }
760
761cleanup:
762 if (rc) {
763 lyd_free_tree(ret);
764 ret = NULL;
765 }
766 va_end(ap);
767 return ret;
768}
769
770API struct lyd_node *
771lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
772{
773 struct lyd_node *ret = NULL;
774 const struct lysc_node *schema;
775 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
776
777 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
778
Michal Vaskof03ed032020-03-04 13:31:44 +0100779 if (!module) {
780 module = parent->schema->module;
781 }
782
Michal Vasko013a8182020-03-03 10:46:53 +0100783 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
784 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
785
Michal Vaskof03ed032020-03-04 13:31:44 +0100786 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100787 lyd_insert_node(parent, NULL, ret);
788 }
789 return ret;
790}
791
792API struct lyd_node *
793lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
794{
795 struct lyd_node *ret = NULL;
796 const struct lysc_node *schema;
797 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
798
799 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
800
Michal Vaskof03ed032020-03-04 13:31:44 +0100801 if (!module) {
802 module = parent->schema->module;
803 }
804
Michal Vasko013a8182020-03-03 10:46:53 +0100805 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
806 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
807
Michal Vaskof03ed032020-03-04 13:31:44 +0100808 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
809 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100810 lyd_insert_node(parent, NULL, ret);
811 }
812 return ret;
813}
814
815API struct lyd_node *
816lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
817 LYD_ANYDATA_VALUETYPE value_type)
818{
819 struct lyd_node *ret = NULL;
820 const struct lysc_node *schema;
821 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
822
823 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
824
Michal Vaskof03ed032020-03-04 13:31:44 +0100825 if (!module) {
826 module = parent->schema->module;
827 }
828
Michal Vasko013a8182020-03-03 10:46:53 +0100829 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
830 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
831
832 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
833 lyd_insert_node(parent, NULL, ret);
834 }
835 return ret;
836}
837
Michal Vasko90932a92020-02-12 14:33:03 +0100838struct lyd_node *
839lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
840{
841 const struct lysc_node *prev_key;
842 struct lyd_node *match = NULL;
843
844 if (!first_sibling) {
845 return NULL;
846 }
847
848 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
849 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
850 }
851
852 return match;
853}
854
855/**
856 * @brief Insert node after a sibling.
857 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100858 * Handles inserting into NP containers and key-less lists.
859 *
Michal Vasko90932a92020-02-12 14:33:03 +0100860 * @param[in] sibling Sibling to insert after.
861 * @param[in] node Node to insert.
862 */
863static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100864lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100865{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100866 struct lyd_node_inner *par;
867
Michal Vasko90932a92020-02-12 14:33:03 +0100868 assert(!node->next && (node->prev == node));
869
870 node->next = sibling->next;
871 node->prev = sibling;
872 sibling->next = node;
873 if (node->next) {
874 /* sibling had a succeeding node */
875 node->next->prev = node;
876 } else {
877 /* sibling was last, find first sibling and change its prev */
878 if (sibling->parent) {
879 sibling = sibling->parent->child;
880 } else {
881 for (; sibling->prev->next != node; sibling = sibling->prev);
882 }
883 sibling->prev = node;
884 }
885 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100886
Michal Vasko9f96a052020-03-10 09:41:45 +0100887 for (par = node->parent; par; par = par->parent) {
888 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
889 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100890 par->flags &= ~LYD_DEFAULT;
891 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100892 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
893 /* rehash key-less list */
894 lyd_hash((struct lyd_node *)par);
895 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100896 }
897
898 /* insert into hash table */
899 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100900}
901
902/**
903 * @brief Insert node before a sibling.
904 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100905 * Handles inserting into NP containers and key-less lists.
906 *
Michal Vasko90932a92020-02-12 14:33:03 +0100907 * @param[in] sibling Sibling to insert before.
908 * @param[in] node Node to insert.
909 */
910static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100911lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100912{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100913 struct lyd_node_inner *par;
914
Michal Vasko90932a92020-02-12 14:33:03 +0100915 assert(!node->next && (node->prev == node));
916
917 node->next = sibling;
918 /* covers situation of sibling being first */
919 node->prev = sibling->prev;
920 sibling->prev = node;
921 if (node->prev->next) {
922 /* sibling had a preceding node */
923 node->prev->next = node;
924 } else if (sibling->parent) {
925 /* sibling was first and we must also change parent child pointer */
926 sibling->parent->child = node;
927 }
928 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100929
Michal Vasko9f96a052020-03-10 09:41:45 +0100930 for (par = node->parent; par; par = par->parent) {
931 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
932 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100933 par->flags &= ~LYD_DEFAULT;
934 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100935 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
936 /* rehash key-less list */
937 lyd_hash((struct lyd_node *)par);
938 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100939 }
940
941 /* insert into hash table */
942 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100943}
944
945/**
946 * @brief Insert node as the last child of a parent.
947 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100948 * Handles inserting into NP containers and key-less lists.
949 *
Michal Vasko90932a92020-02-12 14:33:03 +0100950 * @param[in] parent Parent to insert into.
951 * @param[in] node Node to insert.
952 */
953static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100954lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100955{
956 struct lyd_node_inner *par;
957
Michal Vasko0249f7c2020-03-05 16:36:40 +0100958 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100959 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100960
961 par = (struct lyd_node_inner *)parent;
962
963 if (!par->child) {
964 par->child = node;
965 } else {
966 node->prev = par->child->prev;
967 par->child->prev->next = node;
968 par->child->prev = node;
969 }
970 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100971
Michal Vasko9f96a052020-03-10 09:41:45 +0100972 for (; par; par = par->parent) {
973 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
974 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100975 par->flags &= ~LYD_DEFAULT;
976 }
Michal Vasko52927e22020-03-16 17:26:14 +0100977 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100978 /* rehash key-less list */
979 lyd_hash((struct lyd_node *)par);
980 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100981 }
982
983 /* insert into hash table */
984 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100985}
986
987void
Michal Vasko9b368d32020-02-14 13:53:31 +0100988lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100989{
Michal Vasko9b368d32020-02-14 13:53:31 +0100990 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +0100991 const struct lysc_node *skey = NULL;
992 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +0100993
Michal Vasko52927e22020-03-16 17:26:14 +0100994 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +0100995
996 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
997 parent = (struct lyd_node *)(*first_sibling)->parent;
998 }
Michal Vasko90932a92020-02-12 14:33:03 +0100999
1000 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001001 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001002 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001003 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1004 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001005 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001006 } else if (lyd_node_children(parent)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001007 lyd_insert_before_node((struct lyd_node *)lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001008 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001009 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001010 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001011
1012 /* hash list if all its keys were added */
1013 assert(parent->schema->nodetype == LYS_LIST);
1014 anchor = (struct lyd_node *)lyd_node_children(parent);
1015 has_keys = 1;
1016 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1017 if (!anchor || (anchor->schema != skey)) {
1018 /* key missing */
1019 has_keys = 0;
1020 break;
1021 }
1022
1023 anchor = anchor->next;
1024 }
1025 if (has_keys) {
1026 lyd_hash(parent);
1027 }
1028
Michal Vasko90932a92020-02-12 14:33:03 +01001029 } else {
1030 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001031 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001032 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001033 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001034 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001035 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001036 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001037 anchor = anchor->prev;
1038 }
1039
Michal Vaskoc193ce92020-03-06 11:04:48 +01001040 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001041 /* insert after last sibling from this module */
1042 lyd_insert_after_node(anchor, node);
1043 } else {
1044 /* no data from this module, insert at the last position */
1045 lyd_insert_after_node((*first_sibling)->prev, node);
1046 }
Michal Vasko90932a92020-02-12 14:33:03 +01001047 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001048 /* the only sibling */
1049 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001050 }
Michal Vasko90932a92020-02-12 14:33:03 +01001051}
1052
Michal Vaskof03ed032020-03-04 13:31:44 +01001053static LY_ERR
1054lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1055{
1056 const struct lysc_node *par2;
1057
1058 assert(schema);
1059
1060 /* adjust parent first */
1061 while (parent && (parent->nodetype & (LYS_CASE | LYS_CHOICE))) {
1062 parent = parent->parent;
1063 }
1064
1065 /* find schema parent */
1066 for (par2 = schema->parent; par2 && (par2->nodetype & (LYS_CASE | LYS_CHOICE)); par2 = par2->parent);
1067
1068 if (parent) {
1069 /* inner node */
1070 if (par2 != parent) {
1071 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1072 return LY_EINVAL;
1073 }
1074 } else {
1075 /* top-level node */
1076 if (par2) {
1077 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1078 return LY_EINVAL;
1079 }
1080 }
1081
1082 return LY_SUCCESS;
1083}
1084
1085API LY_ERR
1086lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1087{
1088 struct lyd_node *iter;
1089
1090 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1091
1092 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1093
1094 if (node->schema->flags & LYS_KEY) {
1095 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1096 return LY_EINVAL;
1097 }
1098
1099 if (node->parent || node->prev->next) {
1100 lyd_unlink_tree(node);
1101 }
1102
1103 while (node) {
1104 iter = node->next;
1105 lyd_unlink_tree(node);
1106 lyd_insert_node(parent, NULL, node);
1107 node = iter;
1108 }
1109 return LY_SUCCESS;
1110}
1111
1112API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001113lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1114{
1115 struct lyd_node *iter;
1116
1117 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1118
1119 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1120
1121 if (node->schema->flags & LYS_KEY) {
1122 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1123 return LY_EINVAL;
1124 }
1125
1126 if (node->parent || node->prev->next) {
1127 lyd_unlink_tree(node);
1128 }
1129
1130 while (node) {
1131 iter = node->next;
1132 lyd_unlink_tree(node);
1133 lyd_insert_node(NULL, &sibling, node);
1134 node = iter;
1135 }
1136 return LY_SUCCESS;
1137}
1138
Michal Vasko0249f7c2020-03-05 16:36:40 +01001139static LY_ERR
1140lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1141{
1142 if (sibling->parent) {
1143 /* nested, we do not care for the order */
1144 return LY_SUCCESS;
1145 }
1146
1147 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001148 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1149 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001150 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 +01001151 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001152 return LY_EINVAL;
1153 }
1154
Michal Vaskoc193ce92020-03-06 11:04:48 +01001155 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1156 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001157 /* inserting before/after its module data */
1158 return LY_SUCCESS;
1159 }
1160 }
1161
1162 /* find first sibling */
1163 while (sibling->prev->next) {
1164 sibling = sibling->prev;
1165 }
1166
1167 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001168 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001169 /* inserting before its module data */
1170 return LY_SUCCESS;
1171 }
1172 }
1173
1174 /* check there are no data of this module */
1175 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001176 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001177 /* some data of this module found */
1178 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001179 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001180 return LY_EINVAL;
1181 }
1182 }
1183
1184 return LY_SUCCESS;
1185}
1186
Michal Vaskob1b5c262020-03-05 14:29:47 +01001187API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001188lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1189{
1190 struct lyd_node *iter;
1191
1192 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1193
1194 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1195
1196 if (node->schema->flags & LYS_KEY) {
1197 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1198 return LY_EINVAL;
1199 } else if (sibling->schema->flags & LYS_KEY) {
1200 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1201 return LY_EINVAL;
1202 }
1203
Michal Vasko0249f7c2020-03-05 16:36:40 +01001204 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1205
Michal Vaskof03ed032020-03-04 13:31:44 +01001206 if (node->parent || node->prev->next) {
1207 lyd_unlink_tree(node);
1208 }
1209
1210 /* insert in reverse order to get the original order */
1211 node = node->prev;
1212 while (node) {
1213 iter = node->prev;
1214 lyd_unlink_tree(node);
1215
1216 lyd_insert_before_node(sibling, node);
1217 /* move the anchor accordingly */
1218 sibling = node;
1219
1220 node = (iter == node) ? NULL : iter;
1221 }
1222 return LY_SUCCESS;
1223}
1224
1225API LY_ERR
1226lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1227{
1228 struct lyd_node *iter;
1229
1230 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1231
1232 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1233
1234 if (node->schema->flags & LYS_KEY) {
1235 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1236 return LY_EINVAL;
1237 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1238 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1239 return LY_EINVAL;
1240 }
1241
Michal Vasko0249f7c2020-03-05 16:36:40 +01001242 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1243
Michal Vaskof03ed032020-03-04 13:31:44 +01001244 if (node->parent || node->prev->next) {
1245 lyd_unlink_tree(node);
1246 }
1247
1248 while (node) {
1249 iter = node->next;
1250 lyd_unlink_tree(node);
1251
1252 lyd_insert_after_node(sibling, node);
1253 /* move the anchor accordingly */
1254 sibling = node;
1255
1256 node = iter;
1257 }
1258 return LY_SUCCESS;
1259}
1260
1261API void
1262lyd_unlink_tree(struct lyd_node *node)
1263{
1264 struct lyd_node *iter;
1265
1266 if (!node) {
1267 return;
1268 }
1269
1270 /* unlink from siblings */
1271 if (node->prev->next) {
1272 node->prev->next = node->next;
1273 }
1274 if (node->next) {
1275 node->next->prev = node->prev;
1276 } else {
1277 /* unlinking the last node */
1278 if (node->parent) {
1279 iter = node->parent->child;
1280 } else {
1281 iter = node->prev;
1282 while (iter->prev != node) {
1283 iter = iter->prev;
1284 }
1285 }
1286 /* update the "last" pointer from the first node */
1287 iter->prev = node->prev;
1288 }
1289
1290 /* unlink from parent */
1291 if (node->parent) {
1292 if (node->parent->child == node) {
1293 /* the node is the first child */
1294 node->parent->child = node->next;
1295 }
1296
1297 lyd_unlink_hash(node);
1298
1299 /* check for keyless list and update its hash */
1300 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001301 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001302 lyd_hash(iter);
1303 }
1304 }
1305
1306 node->parent = NULL;
1307 }
1308
1309 node->next = NULL;
1310 node->prev = node;
1311}
1312
Michal Vasko90932a92020-02-12 14:33:03 +01001313LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001314lyd_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 +01001315 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 +01001316 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001317{
1318 LY_ERR ret;
1319 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001320 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001321 uint32_t v;
1322
Michal Vasko9f96a052020-03-10 09:41:45 +01001323 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001324
Michal Vasko90932a92020-02-12 14:33:03 +01001325 LY_ARRAY_FOR(mod->compiled->exts, v) {
1326 if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1327 !ly_strncmp(mod->compiled->exts[v].argument, name, name_len)) {
1328 /* we have the annotation definition */
1329 ant = &mod->compiled->exts[v];
1330 break;
1331 }
1332 }
1333 if (!ant) {
1334 /* attribute is not defined as a metadata annotation (RFC 7952) */
1335 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1336 mod->name, name_len, name);
1337 return LY_EINVAL;
1338 }
1339
Michal Vasko9f96a052020-03-10 09:41:45 +01001340 mt = calloc(1, sizeof *mt);
1341 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1342 mt->parent = parent;
1343 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001344 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 +01001345 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001346 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001347 return ret;
1348 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001349 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001350
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001351 /* insert as the last attribute */
1352 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001353 if (parent->meta) {
1354 for (last = parent->meta; last->next; last = last->next);
1355 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001356 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001357 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001358 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001359 } else if (*meta) {
1360 for (last = *meta; last->next; last = last->next);
1361 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001362 }
1363
1364 /* remove default flags from NP containers */
1365 while (parent && (parent->flags & LYD_DEFAULT)) {
1366 parent->flags &= ~LYD_DEFAULT;
1367 parent = (struct lyd_node *)parent->parent;
1368 }
1369
Michal Vasko9f96a052020-03-10 09:41:45 +01001370 if (meta) {
1371 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001372 }
1373 return ret;
1374}
1375
Michal Vasko52927e22020-03-16 17:26:14 +01001376LY_ERR
1377ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1378 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1379 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1380{
1381 struct ly_attr *at, *last;
1382 struct lyd_node_opaq *opaq;
1383
1384 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1385 assert(name && name_len);
1386 assert((prefix_len && ns) || (!prefix_len && !ns));
1387
1388 if (!value_len) {
1389 value = "";
1390 }
1391
1392 at = calloc(1, sizeof *at);
1393 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1394 at->parent = (struct lyd_node_opaq *)parent;
1395 at->name = lydict_insert(ctx, name, name_len);
1396 if (dynamic && *dynamic) {
1397 at->value = lydict_insert_zc(ctx, (char *)value);
1398 *dynamic = 0;
1399 } else {
1400 at->value = lydict_insert(ctx, value, value_len);
1401 }
1402
1403 at->format = format;
1404 at->val_prefs = val_prefs;
1405 if (ns) {
1406 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1407 at->prefix.ns = lydict_insert(ctx, ns, 0);
1408 }
1409
1410 /* insert as the last attribute */
1411 if (parent) {
1412 opaq = (struct lyd_node_opaq *)parent;
1413 if (opaq->attr) {
1414 for (last = opaq->attr; last->next; last = last->next);
1415 last->next = at;
1416 } else {
1417 opaq->attr = at;
1418 }
1419 } else if (*attr) {
1420 for (last = *attr; last->next; last = last->next);
1421 last->next = at;
1422 }
1423
1424 if (attr) {
1425 *attr = at;
1426 }
1427 return LY_SUCCESS;
1428}
1429
Radek Krejci084289f2019-07-09 17:35:30 +02001430API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001431lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001432{
Michal Vaskof03ed032020-03-04 13:31:44 +01001433 unsigned int u, x;
Michal Vaskoe444f752020-02-10 12:20:06 +01001434 const struct lyd_node *parent = NULL, *start_search;
1435 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001436 uint64_t pos = 1;
1437
Michal Vaskof03ed032020-03-04 13:31:44 +01001438 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001439
1440 LY_ARRAY_FOR(path, u) {
1441 if (parent) {
1442 start_search = lyd_node_children(parent);
1443search_inner:
Michal Vaskoe444f752020-02-10 12:20:06 +01001444 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 +02001445 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001446 start_search = tree;
Radek Krejci084289f2019-07-09 17:35:30 +02001447search_toplevel:
Michal Vaskof03ed032020-03-04 13:31:44 +01001448 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
1449 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 +02001450 }
1451 if (!node) {
1452 return NULL;
1453 }
1454
1455 /* check predicate if any */
1456 LY_ARRAY_FOR(path[u].predicates, x) {
1457 if (path[u].predicates[x].type == 0) {
1458 /* position predicate */
1459 if (pos != path[u].predicates[x].position) {
1460 pos++;
1461 goto search_repeat;
1462 }
1463 /* done, no more predicates are allowed here */
1464 break;
1465 } else if (path[u].predicates[x].type == 1) {
1466 /* key-predicate */
1467 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001468 struct lyd_node *key;
1469 lyd_find_sibling_next(lyd_node_children(node), path[u].predicates[x].key->module,
1470 path[u].predicates[x].key->name, 0, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001471 if (!key) {
1472 /* probably error and we shouldn't be here due to previous checks when creating path */
1473 goto search_repeat;
1474 }
1475 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
1476 goto search_repeat;
1477 }
1478 } else if (path[u].predicates[x].type == 2) {
1479 /* leaf-list-predicate */
1480 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
1481 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
1482 goto search_repeat;
1483 }
1484 } else {
1485 LOGINT(NULL);
1486 }
1487 }
1488
1489 parent = node;
1490 }
1491
1492 return (const struct lyd_node_term*)node;
1493
1494search_repeat:
1495 start_search = node->next;
1496 if (parent) {
1497 goto search_inner;
1498 } else {
1499 goto search_toplevel;
1500 }
1501}
1502
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001503API LY_ERR
1504lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1505{
1506 const struct lyd_node *iter1, *iter2;
1507 struct lyd_node_term *term1, *term2;
1508 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001509 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001510 struct lysc_type *type;
1511 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001512
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001513 if (!node1 || !node2) {
1514 if (node1 == node2) {
1515 return LY_SUCCESS;
1516 } else {
1517 return LY_ENOT;
1518 }
1519 }
1520
Michal Vasko52927e22020-03-16 17:26:14 +01001521 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001522 return LY_ENOT;
1523 }
1524
1525 if (node1->hash != node2->hash) {
1526 return LY_ENOT;
1527 }
Michal Vasko52927e22020-03-16 17:26:14 +01001528 /* 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 +02001529
Michal Vasko52927e22020-03-16 17:26:14 +01001530 if (!node1->schema) {
1531 opaq1 = (struct lyd_node_opaq *)node1;
1532 opaq2 = (struct lyd_node_opaq *)node2;
1533 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001534 return LY_ENOT;
1535 }
Michal Vasko52927e22020-03-16 17:26:14 +01001536 switch (opaq1->format) {
1537 case LYD_XML:
1538 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1539 return LY_ENOT;
1540 }
1541 break;
1542 case LYD_SCHEMA:
1543 /* not allowed */
1544 LOGINT(LYD_NODE_CTX(node1));
1545 return LY_EINT;
1546 }
1547 if (options & LYD_COMPARE_FULL_RECURSION) {
1548 iter1 = opaq1->child;
1549 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001550 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001551 }
1552 return LY_SUCCESS;
1553 } else {
1554 switch (node1->schema->nodetype) {
1555 case LYS_LEAF:
1556 case LYS_LEAFLIST:
1557 if (options & LYD_COMPARE_DEFAULTS) {
1558 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1559 return LY_ENOT;
1560 }
1561 }
1562
1563 term1 = (struct lyd_node_term*)node1;
1564 term2 = (struct lyd_node_term*)node2;
1565 type = ((struct lysc_node_leaf*)node1->schema)->type;
1566
1567 return type->plugin->compare(&term1->value, &term2->value);
1568 case LYS_CONTAINER:
1569 if (options & LYD_COMPARE_DEFAULTS) {
1570 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1571 return LY_ENOT;
1572 }
1573 }
1574 if (options & LYD_COMPARE_FULL_RECURSION) {
1575 iter1 = ((struct lyd_node_inner*)node1)->child;
1576 iter2 = ((struct lyd_node_inner*)node2)->child;
1577 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001578 }
1579 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001580 case LYS_RPC:
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) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001687 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001688 case LYS_ACTION:
1689 case LYS_NOTIF:
1690 case LYS_CONTAINER:
1691 case LYS_LIST:
1692 dup = calloc(1, sizeof(struct lyd_node_inner));
1693 break;
1694 case LYS_LEAF:
1695 case LYS_LEAFLIST:
1696 dup = calloc(1, sizeof(struct lyd_node_term));
1697 break;
1698 case LYS_ANYDATA:
1699 case LYS_ANYXML:
1700 dup = calloc(1, sizeof(struct lyd_node_any));
1701 break;
1702 default:
1703 LOGINT(LYD_NODE_CTX(node));
1704 ret = LY_EINT;
1705 goto error;
1706 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001707 }
Michal Vasko52927e22020-03-16 17:26:14 +01001708 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001709
1710 /* TODO implement LYD_DUP_WITH_WHEN */
1711 dup->flags = node->flags;
1712 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001713 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001714
1715 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1716
1717 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001718 if (!dup->schema) {
1719 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1720 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1721 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001722
1723 if (options & LYD_DUP_RECURSIVE) {
1724 /* duplicate all the children */
1725 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001726 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1727 }
1728 }
1729 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1730 opaq->format = orig->format;
1731 if (orig->prefix.pref) {
1732 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1733 }
1734 if (orig->prefix.ns) {
1735 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1736 }
1737 if (orig->val_prefs) {
1738 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1739 LY_ARRAY_FOR(orig->val_prefs, u) {
1740 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1741 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1742 LY_ARRAY_INCREMENT(opaq->val_prefs);
1743 }
1744 }
1745 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1746 opaq->ctx = orig->ctx;
1747 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1748 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1749 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1750
1751 term->hash = orig->hash;
1752 term->value.realtype = orig->value.realtype;
1753 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1754 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1755 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1756 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1757 struct lyd_node *child;
1758
1759 if (options & LYD_DUP_RECURSIVE) {
1760 /* duplicate all the children */
1761 LY_LIST_FOR(orig->child, child) {
1762 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001763 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001764 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001765 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001766 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001767 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001768 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1769 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001770 if (!child) {
1771 /* possibly not keys are present in filtered tree */
1772 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001773 } else if (child->schema != key) {
1774 /* possibly not all keys are present in filtered tree,
1775 * but there can be also some non-key nodes */
1776 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001777 }
Michal Vasko52927e22020-03-16 17:26:14 +01001778 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001779 child = child->next;
1780 }
1781 }
1782 lyd_hash(dup);
1783 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001784 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1785 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001786
1787 any->hash = orig->hash;
1788 any->value_type = orig->value_type;
1789 switch (any->value_type) {
1790 case LYD_ANYDATA_DATATREE:
1791 if (orig->value.tree) {
1792 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
1793 LY_CHECK_GOTO(!any->value.tree, error);
1794 }
1795 break;
1796 case LYD_ANYDATA_STRING:
1797 case LYD_ANYDATA_XML:
1798 case LYD_ANYDATA_JSON:
1799 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001800 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001801 }
1802 break;
1803 }
1804 }
1805
Michal Vasko52927e22020-03-16 17:26:14 +01001806 /* insert */
1807 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001808 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001809
1810 if (dup_p) {
1811 *dup_p = dup;
1812 }
1813 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001814
1815error:
Michal Vasko52927e22020-03-16 17:26:14 +01001816 lyd_free_tree(dup);
1817 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001818}
1819
1820API struct lyd_node *
1821lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1822{
1823 struct ly_ctx *ctx;
1824 const struct lyd_node *orig; /* original node to be duplicated */
1825 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001826 struct lyd_node *top = NULL; /* the most higher created node */
1827 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1828 int keyless_parent_list = 0;
1829
1830 LY_CHECK_ARG_RET(NULL, node, NULL);
1831 ctx = node->schema->module->ctx;
1832
1833 if (options & LYD_DUP_WITH_PARENTS) {
1834 struct lyd_node_inner *orig_parent, *iter;
1835 int repeat = 1;
1836 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1837 if (parent && parent->schema == orig_parent->schema) {
1838 /* stop creating parents, connect what we have into the provided parent */
1839 iter = parent;
1840 repeat = 0;
1841 /* get know if there is a keyless list which we will have to rehash */
1842 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001843 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001844 keyless_parent_list = 1;
1845 break;
1846 }
1847 }
1848 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001849 iter = NULL;
1850 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1851 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001852 }
1853 if (!local_parent) {
1854 local_parent = iter;
1855 }
1856 if (iter->child) {
1857 /* 1) list - add after keys
1858 * 2) provided parent with some children */
1859 iter->child->prev->next = top;
1860 if (top) {
1861 top->prev = iter->child->prev;
1862 iter->child->prev = top;
1863 }
1864 } else {
1865 iter->child = top;
1866 if (iter->schema->nodetype == LYS_LIST) {
1867 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1868 keyless_parent_list = 1;
1869 }
1870 }
1871 if (top) {
1872 top->parent = iter;
1873 }
1874 top = (struct lyd_node*)iter;
1875 }
1876 if (repeat && parent) {
1877 /* given parent and created parents chain actually do not interconnect */
1878 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1879 goto error;
1880 }
1881 } else {
1882 local_parent = parent;
1883 }
1884
Radek Krejci22ebdba2019-07-25 13:59:43 +02001885 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001886 /* if there is no local parent, it will be inserted into first */
1887 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 +02001888 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1889 break;
1890 }
1891 }
1892 if (keyless_parent_list) {
1893 /* rehash */
1894 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001895 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001896 lyd_hash((struct lyd_node*)local_parent);
1897 }
1898 }
1899 }
1900 return first;
1901
1902error:
1903 if (top) {
1904 lyd_free_tree(top);
1905 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001906 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001907 }
1908 return NULL;
1909}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001910
1911static LY_ERR
1912lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
1913{
Michal Vasko14654712020-02-06 08:35:21 +01001914 /* ending \0 */
1915 ++reqlen;
1916
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001917 if (reqlen > *buflen) {
1918 if (is_static) {
1919 return LY_EINCOMPLETE;
1920 }
1921
1922 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
1923 if (!*buffer) {
1924 return LY_EMEM;
1925 }
1926
1927 *buflen = reqlen;
1928 }
1929
1930 return LY_SUCCESS;
1931}
1932
1933/**
1934 * @brief Append all list key predicates to path.
1935 *
1936 * @param[in] node Node with keys to print.
1937 * @param[in,out] buffer Buffer to print to.
1938 * @param[in,out] buflen Current buffer length.
1939 * @param[in,out] bufused Current number of characters used in @p buffer.
1940 * @param[in] is_static Whether buffer is static or can be reallocated.
1941 * @return LY_ERR
1942 */
1943static LY_ERR
1944lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1945{
1946 const struct lyd_node *key;
1947 int dynamic = 0;
1948 size_t len;
1949 const char *val;
1950 char quot;
1951 LY_ERR rc;
1952
Michal Vasko14654712020-02-06 08:35:21 +01001953 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001954 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
1955 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
1956 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1957 if (rc != LY_SUCCESS) {
1958 if (dynamic) {
1959 free((char *)val);
1960 }
1961 return rc;
1962 }
1963
1964 quot = '\'';
1965 if (strchr(val, '\'')) {
1966 quot = '"';
1967 }
1968 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
1969
1970 if (dynamic) {
1971 free((char *)val);
1972 }
1973 }
1974
1975 return LY_SUCCESS;
1976}
1977
1978/**
1979 * @brief Append leaf-list value predicate to path.
1980 *
1981 * @param[in] node Node to print.
1982 * @param[in,out] buffer Buffer to print to.
1983 * @param[in,out] buflen Current buffer length.
1984 * @param[in,out] bufused Current number of characters used in @p buffer.
1985 * @param[in] is_static Whether buffer is static or can be reallocated.
1986 * @return LY_ERR
1987 */
1988static LY_ERR
1989lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1990{
1991 int dynamic = 0;
1992 size_t len;
1993 const char *val;
1994 char quot;
1995 LY_ERR rc;
1996
1997 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
1998 len = 4 + strlen(val) + 2;
1999 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2000 if (rc != LY_SUCCESS) {
2001 goto cleanup;
2002 }
2003
2004 quot = '\'';
2005 if (strchr(val, '\'')) {
2006 quot = '"';
2007 }
2008 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2009
2010cleanup:
2011 if (dynamic) {
2012 free((char *)val);
2013 }
2014 return rc;
2015}
2016
2017/**
2018 * @brief Append node position (relative to its other instances) predicate to path.
2019 *
2020 * @param[in] node Node to print.
2021 * @param[in,out] buffer Buffer to print to.
2022 * @param[in,out] buflen Current buffer length.
2023 * @param[in,out] bufused Current number of characters used in @p buffer.
2024 * @param[in] is_static Whether buffer is static or can be reallocated.
2025 * @return LY_ERR
2026 */
2027static LY_ERR
2028lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2029{
2030 const struct lyd_node *first, *iter;
2031 size_t len;
2032 int pos;
2033 char *val = NULL;
2034 LY_ERR rc;
2035
2036 if (node->parent) {
2037 first = node->parent->child;
2038 } else {
2039 for (first = node; node->prev->next; node = node->prev);
2040 }
2041 pos = 1;
2042 for (iter = first; iter != node; iter = iter->next) {
2043 if (iter->schema == node->schema) {
2044 ++pos;
2045 }
2046 }
2047 if (asprintf(&val, "%d", pos) == -1) {
2048 return LY_EMEM;
2049 }
2050
2051 len = 1 + strlen(val) + 1;
2052 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2053 if (rc != LY_SUCCESS) {
2054 goto cleanup;
2055 }
2056
2057 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2058
2059cleanup:
2060 free(val);
2061 return rc;
2062}
2063
2064API char *
2065lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2066{
Michal Vasko14654712020-02-06 08:35:21 +01002067 int is_static = 0, i, depth;
2068 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002069 const struct lyd_node *iter;
2070 const struct lys_module *mod;
2071 LY_ERR rc;
2072
2073 LY_CHECK_ARG_RET(NULL, node, NULL);
2074 if (buffer) {
2075 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2076 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002077 } else {
2078 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002079 }
2080
2081 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002082 case LYD_PATH_LOG:
2083 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002084 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2085 ++depth;
2086 }
2087
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002088 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002089 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002090 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002091 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002092iter_print:
2093 /* print prefix and name */
2094 mod = NULL;
2095 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2096 mod = iter->schema->module;
2097 }
2098
2099 /* realloc string */
2100 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2101 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2102 if (rc != LY_SUCCESS) {
2103 break;
2104 }
2105
2106 /* print next node */
2107 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2108
2109 switch (iter->schema->nodetype) {
2110 case LYS_LIST:
2111 if (iter->schema->flags & LYS_KEYLESS) {
2112 /* print its position */
2113 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2114 } else {
2115 /* print all list keys in predicates */
2116 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2117 }
2118 break;
2119 case LYS_LEAFLIST:
2120 if (iter->schema->flags & LYS_CONFIG_W) {
2121 /* print leaf-list value */
2122 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2123 } else {
2124 /* print its position */
2125 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2126 }
2127 break;
2128 default:
2129 /* nothing to print more */
2130 rc = LY_SUCCESS;
2131 break;
2132 }
2133 if (rc != LY_SUCCESS) {
2134 break;
2135 }
2136
Michal Vasko14654712020-02-06 08:35:21 +01002137 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002138 }
2139 break;
2140 }
2141
2142 return buffer;
2143}
Michal Vaskoe444f752020-02-10 12:20:06 +01002144
Michal Vasko9b368d32020-02-14 13:53:31 +01002145LY_ERR
2146lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2147 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002148{
2149 LY_ERR rc;
2150 const struct lyd_node *node = NULL;
2151 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002152 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002153 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002154 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002155
Michal Vasko9b368d32020-02-14 13:53:31 +01002156 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002157
2158 if (!first) {
2159 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002160 if (match) {
2161 *match = NULL;
2162 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002163 return LY_ENOTFOUND;
2164 }
2165
Michal Vaskoe444f752020-02-10 12:20:06 +01002166 if (key_or_value && !val_len) {
2167 val_len = strlen(key_or_value);
2168 }
2169
2170 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002171 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002172 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 +01002173 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2174 /* parse keys into canonical values */
2175 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, &keys), cleanup);
2176 }
2177
2178 /* find first matching value */
2179 LY_LIST_FOR(first, node) {
2180 if (node->schema != schema) {
2181 continue;
2182 }
2183
2184 if ((schema->nodetype == LYS_LIST) && keys.str) {
2185 /* compare all set keys */
2186 for (i = 0; i < keys.key_count; ++i) {
2187 /* find key */
2188 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2189 (struct lyd_node **)&term);
2190 if (rc == LY_ENOTFOUND) {
2191 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002192 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002193 }
2194 LY_CHECK_GOTO(rc, cleanup);
2195
2196 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002197 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002198 break;
2199 }
2200 }
2201
2202 if (i < keys.key_count) {
2203 /* not a match */
2204 continue;
2205 }
Michal Vasko90932a92020-02-12 14:33:03 +01002206 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002207 term = (struct lyd_node_term *)node;
2208
2209 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002210 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002211 /* not a match */
2212 continue;
2213 }
2214 }
2215
2216 /* all criteria passed */
2217 break;
2218 }
2219
2220 if (!node) {
2221 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002222 if (match) {
2223 *match = NULL;
2224 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002225 goto cleanup;
2226 }
2227
2228 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002229 if (match) {
2230 *match = (struct lyd_node *)node;
2231 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002232 rc = LY_SUCCESS;
2233
2234cleanup:
2235 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002236 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002237 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002238 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002239 return rc;
2240}
2241
2242API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002243lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2244 const char *key_or_value, size_t val_len, struct lyd_node **match)
2245{
2246 const struct lysc_node *schema;
2247
2248 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2249
2250 if (!first) {
2251 /* no data */
2252 *match = NULL;
2253 return LY_ENOTFOUND;
2254 }
2255
2256 /* find schema */
2257 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2258 if (!schema) {
2259 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2260 return LY_EINVAL;
2261 }
2262
2263 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2264}
2265
2266API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002267lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2268{
2269 struct lyd_node **match_p;
2270 struct lyd_node_inner *parent;
2271
Michal Vaskof03ed032020-03-04 13:31:44 +01002272 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002273
2274 if (!siblings) {
2275 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002276 if (match) {
2277 *match = NULL;
2278 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002279 return LY_ENOTFOUND;
2280 }
2281
2282 /* find first sibling */
2283 if (siblings->parent) {
2284 siblings = siblings->parent->child;
2285 } else {
2286 while (siblings->prev->next) {
2287 siblings = siblings->prev;
2288 }
2289 }
2290
2291 parent = (struct lyd_node_inner *)siblings->parent;
2292 if (parent && parent->children_ht) {
2293 assert(target->hash);
2294
2295 /* find by hash */
2296 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2297 siblings = *match_p;
2298 } else {
2299 /* not found */
2300 siblings = NULL;
2301 }
2302 } else {
2303 /* no children hash table */
2304 for (; siblings; siblings = siblings->next) {
2305 if (!lyd_compare(siblings, target, 0)) {
2306 break;
2307 }
2308 }
2309 }
2310
2311 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002312 if (match) {
2313 *match = NULL;
2314 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002315 return LY_ENOTFOUND;
2316 }
2317
Michal Vasko9b368d32020-02-14 13:53:31 +01002318 if (match) {
2319 *match = (struct lyd_node *)siblings;
2320 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002321 return LY_SUCCESS;
2322}
2323
2324API LY_ERR
2325lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2326{
2327 struct lyd_node_inner *parent;
2328 struct lyd_node *match;
2329 struct lyd_node **match_p;
2330 struct ly_set *ret;
2331
2332 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2333
2334 if (!siblings) {
2335 /* no data */
2336 return LY_ENOTFOUND;
2337 }
2338
2339 ret = ly_set_new();
2340 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2341
2342 /* find first sibling */
2343 if (siblings->parent) {
2344 siblings = siblings->parent->child;
2345 } else {
2346 while (siblings->prev->next) {
2347 siblings = siblings->prev;
2348 }
2349 }
2350
2351 parent = (struct lyd_node_inner *)siblings->parent;
2352 if (parent && parent->children_ht) {
2353 assert(target->hash);
2354
2355 /* find by hash */
2356 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2357 match = *match_p;
2358 } else {
2359 /* not found */
2360 match = NULL;
2361 }
2362 while (match) {
2363 /* add all found nodes into the return set */
2364 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2365 goto error;
2366 }
2367
2368 /* find next instance */
2369 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2370 match = NULL;
2371 } else {
2372 match = *match_p;
2373 }
2374 }
2375 } else {
2376 /* no children hash table */
2377 for (; siblings; siblings = siblings->next) {
2378 if (!lyd_compare(siblings, target, 0)) {
2379 /* a match */
2380 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2381 goto error;
2382 }
2383 }
2384 }
2385 }
2386
2387 if (!ret->count) {
2388 ly_set_free(ret, NULL);
2389 return LY_ENOTFOUND;
2390 }
2391
2392 *set = ret;
2393 return LY_SUCCESS;
2394
2395error:
2396 ly_set_free(ret, NULL);
2397 return LY_EMEM;
2398}
2399
Michal Vasko90932a92020-02-12 14:33:03 +01002400static int
2401lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2402{
2403 struct lysc_node *val1;
2404 struct lyd_node *val2;
2405
2406 val1 = *((struct lysc_node **)val1_p);
2407 val2 = *((struct lyd_node **)val2_p);
2408
2409 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2410
2411 if (val1 == val2->schema) {
2412 /* schema match is enough */
2413 return 1;
2414 } else {
2415 return 0;
2416 }
2417}
2418
2419static LY_ERR
2420lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2421{
2422 struct lyd_node **match_p;
2423 struct lyd_node_inner *parent;
2424 uint32_t hash;
2425 values_equal_cb ht_cb;
2426
2427 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2428
2429 /* find first sibling */
2430 if (siblings->parent) {
2431 siblings = siblings->parent->child;
2432 } else {
2433 while (siblings->prev->next) {
2434 siblings = siblings->prev;
2435 }
2436 }
2437
2438 parent = (struct lyd_node_inner *)siblings->parent;
2439 if (parent && parent->children_ht) {
2440 /* calculate our hash */
2441 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2442 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2443 hash = dict_hash_multi(hash, NULL, 0);
2444
2445 /* use special hash table function */
2446 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2447
2448 /* find by hash */
2449 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2450 siblings = *match_p;
2451 } else {
2452 /* not found */
2453 siblings = NULL;
2454 }
2455
2456 /* set the original hash table compare function back */
2457 lyht_set_cb(parent->children_ht, ht_cb);
2458 } else {
2459 /* no children hash table */
2460 for (; siblings; siblings = siblings->next) {
2461 if (siblings->schema == schema) {
2462 /* schema match is enough */
2463 break;
2464 }
2465 }
2466 }
2467
2468 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002469 if (match) {
2470 *match = NULL;
2471 }
Michal Vasko90932a92020-02-12 14:33:03 +01002472 return LY_ENOTFOUND;
2473 }
2474
Michal Vasko9b368d32020-02-14 13:53:31 +01002475 if (match) {
2476 *match = (struct lyd_node *)siblings;
2477 }
Michal Vasko90932a92020-02-12 14:33:03 +01002478 return LY_SUCCESS;
2479}
2480
Michal Vaskoe444f752020-02-10 12:20:06 +01002481API LY_ERR
2482lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2483 size_t val_len, struct lyd_node **match)
2484{
2485 LY_ERR rc;
2486 struct lyd_node *target = NULL;
2487
2488 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
2489 if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) {
2490 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2491 return LY_EINVAL;
2492 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2493 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2494 return LY_EINVAL;
2495 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2496 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2497 lys_nodetype2str(schema->nodetype), __func__);
2498 return LY_EINVAL;
2499 }
2500
2501 if (!siblings) {
2502 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002503 if (match) {
2504 *match = NULL;
2505 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002506 return LY_ENOTFOUND;
2507 }
2508
Michal Vaskof03ed032020-03-04 13:31:44 +01002509 if (key_or_value && !val_len) {
2510 val_len = strlen(key_or_value);
2511 }
2512
Michal Vasko90932a92020-02-12 14:33:03 +01002513 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002514 switch (schema->nodetype) {
2515 case LYS_CONTAINER:
2516 case LYS_ANYXML:
2517 case LYS_ANYDATA:
2518 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01002519 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01002520 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002521 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002522 /* find it based on schema only */
2523 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002524 break;
2525 case LYS_LEAFLIST:
2526 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002527 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 +01002528 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002529 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002530 if (schema->nodetype == LYS_LIST) {
2531 /* target used attributes: schema, hash, child (all keys) */
2532 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002533 }
2534
2535 /* find it */
2536 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002537 break;
2538 default:
2539 /* unreachable */
2540 LOGINT(schema->module->ctx);
2541 return LY_EINT;
2542 }
2543
Michal Vaskoe444f752020-02-10 12:20:06 +01002544 lyd_free_tree(target);
2545 return rc;
2546}