blob: bf3402ca80043a297754f24db7da205f93e83278 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_schema.c
3 * @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"
Radek Krejci38d85362019-09-05 16:26:38 +020032#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034
Michal Vaskoe444f752020-02-10 12:20:06 +010035struct ly_keys {
36 char *str;
37 struct {
38 const struct lysc_node_leaf *schema;
39 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010040 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010041 } *keys;
42 size_t key_count;
43};
44
Radek Krejci084289f2019-07-09 17:35:30 +020045LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010046lyd_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 +010047 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020048{
Michal Vasko90932a92020-02-12 14:33:03 +010049 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020050 struct ly_err_item *err = NULL;
51 struct ly_ctx *ctx;
52 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020053 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010054 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020055 assert(node);
56
57 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020058
Radek Krejci73dead22019-07-11 16:46:16 +020059 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020060 if (!second) {
61 node->value.realtype = type;
62 }
Michal Vasko90932a92020-02-12 14:33:03 +010063 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vaskof03ed032020-03-04 13:31:44 +010064 tree ? (void *)node : (void *)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +020065 &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010066 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020067 if (err) {
68 ly_err_print(err);
69 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
70 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 Vasko8d544252020-03-02 10:19:52 +0100115lyd_value_parse_attr(struct ly_ctx *ctx, struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic,
116 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 Vaskof03ed032020-03-04 13:31:44 +0100125 assert(ctx && attr && ((tree && attr->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100126
Radek Krejci38d85362019-09-05 16:26:38 +0200127 ant = attr->annotation->data;
128
129 if (!second) {
130 attr->value.realtype = ant->type;
131 }
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 Vaskof03ed032020-03-04 13:31:44 +0100133 tree ? (void *)attr->parent : (void *)ctx_snode, tree, &attr->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
150lys_value_validate(struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
151 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
185lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +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 *
265lyd_attr2str(const struct lyd_attr *attr, int *dynamic)
266{
267 LY_CHECK_ARG_RET(attr ? attr->parent->schema->module->ctx : NULL, attr, dynamic, NULL);
268
269 return attr->value.realtype->plugin->print(&attr->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
270}
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 Vaskoa3881362020-01-21 15:57:35 +0100282#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200283 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200284 /* first item in trees is mandatory - the RPC/action request */
285 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
286 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
287 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
288 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200289 return NULL;
290 }
291 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200292
Radek Krejcie7b95092019-05-15 11:03:07 +0200293 if (options & LYD_OPT_DATA_TEMPLATE) {
294 yang_data_name = va_arg(ap, const char *);
295 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200296#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200297
298 if (!format) {
299 /* TODO try to detect format from the content */
300 }
301
302 switch (format) {
303 case LYD_XML:
Michal Vaskoa3881362020-01-21 15:57:35 +0100304 lyd_parse_xml(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 break;
306#if 0
307 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200308 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200309 break;
310 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200311 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200312 break;
313#endif
314 case LYD_UNKNOWN:
315 LOGINT(ctx);
316 break;
317 }
318
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 return result;
320}
321
322API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100323lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200324{
325 struct lyd_node *result;
326 size_t length;
327 char *addr;
328
329 LY_CHECK_ARG_RET(ctx, ctx, NULL);
330 if (fd < 0) {
331 LOGARG(ctx, fd);
332 return NULL;
333 }
334
335 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100336 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200337 if (addr) {
338 ly_munmap(addr, length);
339 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200340
341 return result;
342}
343
344API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100345lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200346{
347 int fd;
348 struct lyd_node *result;
349 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200350
351 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
352
353 fd = open(path, O_RDONLY);
354 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
355
356 if (!format) {
357 /* unknown format - try to detect it from filename's suffix */
358 len = strlen(path);
359
360 /* ignore trailing whitespaces */
361 for (; len > 0 && isspace(path[len - 1]); len--);
362
363 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
364 format = LYD_XML;
365#if 0
366 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
367 format = LYD_JSON;
368 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
369 format = LYD_LYB;
370#endif
371 } /* else still unknown, try later to detect it from the content */
372 }
373
Michal Vaskoa3881362020-01-21 15:57:35 +0100374 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200375 close(fd);
376
377 return result;
378}
Radek Krejci084289f2019-07-09 17:35:30 +0200379
Michal Vasko90932a92020-02-12 14:33:03 +0100380LY_ERR
381lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
382 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
383{
384 LY_ERR ret;
385 struct lyd_node_term *term;
386
Michal Vasko9b368d32020-02-14 13:53:31 +0100387 assert(schema->nodetype & LYD_NODE_TERM);
388
Michal Vasko90932a92020-02-12 14:33:03 +0100389 term = calloc(1, sizeof *term);
390 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
391
392 term->schema = schema;
393 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100394 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100395
396 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
397 if (ret && (ret != LY_EINCOMPLETE)) {
398 free(term);
399 return ret;
400 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100401 lyd_hash((struct lyd_node *)term);
402
403 *node = (struct lyd_node *)term;
404 return ret;
405}
406
407LY_ERR
408lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
409{
410 LY_ERR ret;
411 struct lyd_node_term *term;
412 struct lysc_type *type;
413
414 assert(schema->nodetype & LYD_NODE_TERM);
415
416 term = calloc(1, sizeof *term);
417 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
418
419 term->schema = schema;
420 term->prev = (struct lyd_node *)term;
421 term->flags = LYD_NEW;
422
423 type = ((struct lysc_node_leaf *)schema)->type;
424 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
425 if (ret) {
426 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
427 free(term);
428 return ret;
429 }
430 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100431
432 *node = (struct lyd_node *)term;
433 return ret;
434}
435
436LY_ERR
437lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
438{
439 struct lyd_node_inner *in;
440
Michal Vasko9b368d32020-02-14 13:53:31 +0100441 assert(schema->nodetype & LYD_NODE_INNER);
442
Michal Vasko90932a92020-02-12 14:33:03 +0100443 in = calloc(1, sizeof *in);
444 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
445
446 in->schema = schema;
447 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100448 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100449
Michal Vasko9b368d32020-02-14 13:53:31 +0100450 /* do not hash list with keys, we need them for the hash */
451 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
452 lyd_hash((struct lyd_node *)in);
453 }
454 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
455 /* NP cotnainer always a default */
456 in->flags |= LYD_DEFAULT;
Michal Vasko90932a92020-02-12 14:33:03 +0100457 }
458
459 *node = (struct lyd_node *)in;
460 return LY_SUCCESS;
461}
462
463static void
464ly_keys_clean(struct ly_keys *keys)
465{
466 size_t i;
467
468 for (i = 0; i < keys->key_count; ++i) {
469 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
470 }
471 free(keys->str);
472 free(keys->keys);
473}
474
475static char *
476ly_keys_parse_next(char **next_key, char **key_name)
477{
478 char *ptr, *ptr2, *val, quot;
479
480 ptr = *next_key;
481
482 /* "[" */
483 LY_CHECK_GOTO(ptr[0] != '[', error);
484 ++ptr;
485
486 /* key name */
487 ptr2 = strchr(ptr, '=');
488 LY_CHECK_GOTO(!ptr2, error);
489
490 *key_name = ptr;
491 ptr2[0] = '\0';
492
493 /* \0, was '=' */
494 ptr = ptr2 + 1;
495
496 /* quote */
497 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
498 quot = ptr[0];
499 ++ptr;
500
501 /* value, terminate it */
502 val = ptr;
503 ptr2 = strchr(ptr, quot);
504 LY_CHECK_GOTO(!ptr2, error);
505 ptr2[0] = '\0';
506
507 /* \0, was quote */
508 ptr = ptr2 + 1;
509
510 /* "]" */
511 LY_CHECK_GOTO(ptr[0] != ']', error);
512 ++ptr;
513
514 *next_key = ptr;
515 return val;
516
517error:
518 *next_key = ptr;
519 return NULL;
520}
521
522/* fill keys structure; if store is set, fill also each val */
523static LY_ERR
524ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, struct ly_keys *keys)
525{
526 LY_ERR ret = LY_SUCCESS;
527 char *next_key, *name;
528 const struct lysc_node *key;
529 size_t i;
530
531 assert(list->nodetype == LYS_LIST);
532
533 memset(keys, 0, sizeof *keys);
534
Michal Vaskof03ed032020-03-04 13:31:44 +0100535 if (!keys_str) {
536 /* nothing to parse */
537 return LY_SUCCESS;
538 }
539
540 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100541 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
542
543 next_key = keys->str;
544 while (next_key[0]) {
545 /* new key */
546 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
547 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
548
549 /* fill */
550 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
551 if (!keys->keys[keys->key_count].value) {
552 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
553 ret = LY_EINVAL;
554 goto cleanup;
555 }
556
557 /* find schema node */
558 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
559 if (!key) {
560 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
561 ret = LY_EINVAL;
562 goto cleanup;
563 }
564 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
565
566 /* check that we do not have it already */
567 for (i = 0; i < keys->key_count; ++i) {
568 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
569 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
570 ret = LY_EINVAL;
571 goto cleanup;
572 }
573 }
574
575 if (store) {
576 /* store the value */
577 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
578 lydjson_resolve_prefix, NULL, LYD_JSON);
579 LY_CHECK_GOTO(ret, cleanup);
580 }
581
582 /* another valid key */
583 ++keys->key_count;
584 }
585
586cleanup:
587 ly_keys_clean(keys);
588 return ret;
589}
590
591LY_ERR
592lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node)
593{
594 LY_ERR ret = LY_SUCCESS;
595 const struct lysc_node *key_s;
596 struct lyd_node *list = NULL, *key;
597 struct ly_keys keys = {0};
598 size_t i;
599
600 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
601
602 /* parse keys */
603 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, &keys), cleanup);
604
605 /* create list */
606 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
607
608 /* everything was checked except that all keys are set */
609 i = 0;
610 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
611 ++i;
612 }
613 if (i != keys.key_count) {
614 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
615 ret = LY_EINVAL;
616 goto cleanup;
617 }
618
619 /* create and insert all the keys */
620 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskof03ed032020-03-04 13:31:44 +0100621 LY_CHECK_GOTO(ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value,
622 strlen(keys.keys[i].value), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100623 lyd_insert_node(list, NULL, key);
624 }
625
Michal Vasko9b368d32020-02-14 13:53:31 +0100626 /* hash having all the keys */
627 lyd_hash(list);
628
Michal Vasko90932a92020-02-12 14:33:03 +0100629 /* success */
630 *node = list;
631 list = NULL;
632
633cleanup:
634 lyd_free_tree(list);
635 ly_keys_clean(&keys);
636 return ret;
637}
638
639LY_ERR
640lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
641{
642 struct lyd_node_any *any;
643
Michal Vasko9b368d32020-02-14 13:53:31 +0100644 assert(schema->nodetype & LYD_NODE_ANY);
645
Michal Vasko90932a92020-02-12 14:33:03 +0100646 any = calloc(1, sizeof *any);
647 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
648
649 any->schema = schema;
650 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100651 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100652
653 any->value.xml = value;
654 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100656
657 *node = (struct lyd_node *)any;
658 return LY_SUCCESS;
659}
660
Michal Vasko013a8182020-03-03 10:46:53 +0100661API struct lyd_node *
662lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
663{
664 struct lyd_node *ret = NULL;
665 const struct lysc_node *schema;
666 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
667
668 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
669
Michal Vaskof03ed032020-03-04 13:31:44 +0100670 if (!module) {
671 module = parent->schema->module;
672 }
673
Michal Vasko013a8182020-03-03 10:46:53 +0100674 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_ACTION, 0);
675 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node \"%s\" not found.", name), NULL);
676
677 if (!lyd_create_inner(schema, &ret) && parent) {
678 lyd_insert_node(parent, NULL, ret);
679 }
680 return ret;
681}
682
683API struct lyd_node *
684lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
685{
686 struct lyd_node *ret = NULL, *key;
687 const struct lysc_node *schema, *key_s;
688 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
689 va_list ap;
690 const char *key_val;
691 LY_ERR rc = LY_SUCCESS;
692
693 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
694
Michal Vaskof03ed032020-03-04 13:31:44 +0100695 if (!module) {
696 module = parent->schema->module;
697 }
698
Michal Vasko013a8182020-03-03 10:46:53 +0100699 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
700 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
701
702 /* create list inner node */
703 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
704
705 va_start(ap, name);
706
707 /* create and insert all the keys */
708 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
709 key_val = va_arg(ap, const char *);
710
Michal Vaskof03ed032020-03-04 13:31:44 +0100711 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
712 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100713 lyd_insert_node(ret, NULL, key);
714 }
715
716 /* hash having all the keys */
717 lyd_hash(ret);
718
719 if (parent) {
720 lyd_insert_node(parent, NULL, ret);
721 }
722
723cleanup:
724 if (rc) {
725 lyd_free_tree(ret);
726 ret = NULL;
727 }
728 va_end(ap);
729 return ret;
730}
731
732API struct lyd_node *
733lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
734{
735 struct lyd_node *ret = NULL;
736 const struct lysc_node *schema;
737 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
738
739 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
740
Michal Vaskof03ed032020-03-04 13:31:44 +0100741 if (!module) {
742 module = parent->schema->module;
743 }
744
Michal Vasko013a8182020-03-03 10:46:53 +0100745 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
746 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
747
Michal Vaskof03ed032020-03-04 13:31:44 +0100748 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100749 lyd_insert_node(parent, NULL, ret);
750 }
751 return ret;
752}
753
754API struct lyd_node *
755lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
756{
757 struct lyd_node *ret = NULL;
758 const struct lysc_node *schema;
759 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
760
761 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
762
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 if (!module) {
764 module = parent->schema->module;
765 }
766
Michal Vasko013a8182020-03-03 10:46:53 +0100767 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
768 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
769
Michal Vaskof03ed032020-03-04 13:31:44 +0100770 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
771 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100772 lyd_insert_node(parent, NULL, ret);
773 }
774 return ret;
775}
776
777API struct lyd_node *
778lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
779 LYD_ANYDATA_VALUETYPE value_type)
780{
781 struct lyd_node *ret = NULL;
782 const struct lysc_node *schema;
783 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
784
785 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
786
Michal Vaskof03ed032020-03-04 13:31:44 +0100787 if (!module) {
788 module = parent->schema->module;
789 }
790
Michal Vasko013a8182020-03-03 10:46:53 +0100791 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
792 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
793
794 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
795 lyd_insert_node(parent, NULL, ret);
796 }
797 return ret;
798}
799
Michal Vasko90932a92020-02-12 14:33:03 +0100800struct lyd_node *
801lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
802{
803 const struct lysc_node *prev_key;
804 struct lyd_node *match = NULL;
805
806 if (!first_sibling) {
807 return NULL;
808 }
809
810 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
811 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
812 }
813
814 return match;
815}
816
817/**
818 * @brief Insert node after a sibling.
819 *
820 * @param[in] sibling Sibling to insert after.
821 * @param[in] node Node to insert.
822 */
823static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100824lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100825{
826 assert(!node->next && (node->prev == node));
827
828 node->next = sibling->next;
829 node->prev = sibling;
830 sibling->next = node;
831 if (node->next) {
832 /* sibling had a succeeding node */
833 node->next->prev = node;
834 } else {
835 /* sibling was last, find first sibling and change its prev */
836 if (sibling->parent) {
837 sibling = sibling->parent->child;
838 } else {
839 for (; sibling->prev->next != node; sibling = sibling->prev);
840 }
841 sibling->prev = node;
842 }
843 node->parent = sibling->parent;
844}
845
846/**
847 * @brief Insert node before a sibling.
848 *
849 * @param[in] sibling Sibling to insert before.
850 * @param[in] node Node to insert.
851 */
852static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100853lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100854{
855 assert(!node->next && (node->prev == node));
856
857 node->next = sibling;
858 /* covers situation of sibling being first */
859 node->prev = sibling->prev;
860 sibling->prev = node;
861 if (node->prev->next) {
862 /* sibling had a preceding node */
863 node->prev->next = node;
864 } else if (sibling->parent) {
865 /* sibling was first and we must also change parent child pointer */
866 sibling->parent->child = node;
867 }
868 node->parent = sibling->parent;
869}
870
871/**
872 * @brief Insert node as the last child of a parent.
873 *
874 * @param[in] parent Parent to insert into.
875 * @param[in] node Node to insert.
876 */
877static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100878lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100879{
880 struct lyd_node_inner *par;
881
882 assert(!node->next && (node->prev == node));
883 assert(parent->schema->nodetype & LYD_NODE_INNER);
884
885 par = (struct lyd_node_inner *)parent;
886
887 if (!par->child) {
888 par->child = node;
889 } else {
890 node->prev = par->child->prev;
891 par->child->prev->next = node;
892 par->child->prev = node;
893 }
894 node->parent = par;
895}
896
897void
Michal Vasko9b368d32020-02-14 13:53:31 +0100898lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100899{
Michal Vasko9b368d32020-02-14 13:53:31 +0100900 struct lyd_node *anchor;
Michal Vasko90932a92020-02-12 14:33:03 +0100901
Michal Vasko9b368d32020-02-14 13:53:31 +0100902 assert((parent || first_sibling) && node && node->hash);
903
904 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
905 parent = (struct lyd_node *)(*first_sibling)->parent;
906 }
Michal Vasko90932a92020-02-12 14:33:03 +0100907
908 if (parent) {
909 if (node->schema->flags & LYS_KEY) {
910 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +0100911 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
912 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +0100913 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100914 } else if (lyd_node_children(parent)) {
Michal Vaskof03ed032020-03-04 13:31:44 +0100915 lyd_insert_before_node((struct lyd_node *)lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +0100916 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +0100917 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100918 }
919 } else {
920 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +0100921 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100922 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100923 } else if (*first_sibling) {
924 /* top-level siblings, find the last one from this module, or simply the last */
925 anchor = (*first_sibling)->prev;
926 while (anchor->prev->next && (lyd_top_node_module(anchor) != lyd_top_node_module(node))) {
927 anchor = anchor->prev;
928 }
929
930 /* insert */
Michal Vaskof03ed032020-03-04 13:31:44 +0100931 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100932 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +0100933 /* the only sibling */
934 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100935 }
936
Michal Vasko9b368d32020-02-14 13:53:31 +0100937 if (!(node->flags & LYD_DEFAULT)) {
938 /* remove default flags from NP containers */
939 while (parent && (parent->flags & LYD_DEFAULT)) {
940 parent->flags &= ~LYD_DEFAULT;
941 parent = (struct lyd_node *)parent->parent;
942 }
Michal Vasko90932a92020-02-12 14:33:03 +0100943 }
944
945 /* insert into hash table */
946 lyd_insert_hash(node);
947}
948
Michal Vaskof03ed032020-03-04 13:31:44 +0100949static LY_ERR
950lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
951{
952 const struct lysc_node *par2;
953
954 assert(schema);
955
956 /* adjust parent first */
957 while (parent && (parent->nodetype & (LYS_CASE | LYS_CHOICE))) {
958 parent = parent->parent;
959 }
960
961 /* find schema parent */
962 for (par2 = schema->parent; par2 && (par2->nodetype & (LYS_CASE | LYS_CHOICE)); par2 = par2->parent);
963
964 if (parent) {
965 /* inner node */
966 if (par2 != parent) {
967 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
968 return LY_EINVAL;
969 }
970 } else {
971 /* top-level node */
972 if (par2) {
973 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
974 return LY_EINVAL;
975 }
976 }
977
978 return LY_SUCCESS;
979}
980
981API LY_ERR
982lyd_insert(struct lyd_node *parent, struct lyd_node *node)
983{
984 struct lyd_node *iter;
985
986 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
987
988 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
989
990 if (node->schema->flags & LYS_KEY) {
991 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
992 return LY_EINVAL;
993 }
994
995 if (node->parent || node->prev->next) {
996 lyd_unlink_tree(node);
997 }
998
999 while (node) {
1000 iter = node->next;
1001 lyd_unlink_tree(node);
1002 lyd_insert_node(parent, NULL, node);
1003 node = iter;
1004 }
1005 return LY_SUCCESS;
1006}
1007
1008API LY_ERR
1009lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1010{
1011 struct lyd_node *iter;
1012
1013 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1014
1015 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1016
1017 if (node->schema->flags & LYS_KEY) {
1018 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1019 return LY_EINVAL;
1020 } else if (sibling->schema->flags & LYS_KEY) {
1021 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1022 return LY_EINVAL;
1023 }
1024
1025 if (node->parent || node->prev->next) {
1026 lyd_unlink_tree(node);
1027 }
1028
1029 /* insert in reverse order to get the original order */
1030 node = node->prev;
1031 while (node) {
1032 iter = node->prev;
1033 lyd_unlink_tree(node);
1034
1035 lyd_insert_before_node(sibling, node);
1036 /* move the anchor accordingly */
1037 sibling = node;
1038
1039 node = (iter == node) ? NULL : iter;
1040 }
1041 return LY_SUCCESS;
1042}
1043
1044API LY_ERR
1045lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1046{
1047 struct lyd_node *iter;
1048
1049 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1050
1051 LY_CHECK_RET(lyd_insert_check_schema(sibling->schema->parent, node->schema));
1052
1053 if (node->schema->flags & LYS_KEY) {
1054 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1055 return LY_EINVAL;
1056 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1057 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1058 return LY_EINVAL;
1059 }
1060
1061 if (node->parent || node->prev->next) {
1062 lyd_unlink_tree(node);
1063 }
1064
1065 while (node) {
1066 iter = node->next;
1067 lyd_unlink_tree(node);
1068
1069 lyd_insert_after_node(sibling, node);
1070 /* move the anchor accordingly */
1071 sibling = node;
1072
1073 node = iter;
1074 }
1075 return LY_SUCCESS;
1076}
1077
1078API void
1079lyd_unlink_tree(struct lyd_node *node)
1080{
1081 struct lyd_node *iter;
1082
1083 if (!node) {
1084 return;
1085 }
1086
1087 /* unlink from siblings */
1088 if (node->prev->next) {
1089 node->prev->next = node->next;
1090 }
1091 if (node->next) {
1092 node->next->prev = node->prev;
1093 } else {
1094 /* unlinking the last node */
1095 if (node->parent) {
1096 iter = node->parent->child;
1097 } else {
1098 iter = node->prev;
1099 while (iter->prev != node) {
1100 iter = iter->prev;
1101 }
1102 }
1103 /* update the "last" pointer from the first node */
1104 iter->prev = node->prev;
1105 }
1106
1107 /* unlink from parent */
1108 if (node->parent) {
1109 if (node->parent->child == node) {
1110 /* the node is the first child */
1111 node->parent->child = node->next;
1112 }
1113
1114 lyd_unlink_hash(node);
1115
1116 /* check for keyless list and update its hash */
1117 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
1118 if (iter->schema->flags & LYS_KEYLESS) {
1119 lyd_hash(iter);
1120 }
1121 }
1122
1123 node->parent = NULL;
1124 }
1125
1126 node->next = NULL;
1127 node->prev = node;
1128}
1129
Michal Vasko90932a92020-02-12 14:33:03 +01001130LY_ERR
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001131lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct lys_module *mod, const char *name,
1132 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix get_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01001133 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001134{
1135 LY_ERR ret;
1136 struct lysc_ext_instance *ant = NULL;
1137 struct lyd_attr *at, *last;
1138 uint32_t v;
1139
Michal Vasko8d544252020-03-02 10:19:52 +01001140 assert((parent || attr) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001141
Michal Vasko90932a92020-02-12 14:33:03 +01001142 LY_ARRAY_FOR(mod->compiled->exts, v) {
1143 if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1144 !ly_strncmp(mod->compiled->exts[v].argument, name, name_len)) {
1145 /* we have the annotation definition */
1146 ant = &mod->compiled->exts[v];
1147 break;
1148 }
1149 }
1150 if (!ant) {
1151 /* attribute is not defined as a metadata annotation (RFC 7952) */
1152 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1153 mod->name, name_len, name);
1154 return LY_EINVAL;
1155 }
1156
1157 at = calloc(1, sizeof *at);
1158 LY_CHECK_ERR_RET(!at, LOGMEM(mod->ctx), LY_EMEM);
1159 at->parent = parent;
1160 at->annotation = ant;
Michal Vasko8d544252020-03-02 10:19:52 +01001161 ret = lyd_value_parse_attr(mod->ctx, at, value, value_len, dynamic, 0, get_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01001162 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
1163 free(at);
1164 return ret;
1165 }
1166 at->name = lydict_insert(mod->ctx, name, name_len);
1167
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001168 /* insert as the last attribute */
1169 if (parent) {
1170 if (parent->attr) {
1171 for (last = parent->attr; last->next; last = last->next);
1172 last->next = at;
1173 } else {
1174 parent->attr = at;
1175 }
1176 } else if (*attr) {
1177 for (last = *attr; last->next; last = last->next);
Michal Vasko90932a92020-02-12 14:33:03 +01001178 last->next = at;
Michal Vasko90932a92020-02-12 14:33:03 +01001179 }
1180
1181 /* remove default flags from NP containers */
1182 while (parent && (parent->flags & LYD_DEFAULT)) {
1183 parent->flags &= ~LYD_DEFAULT;
1184 parent = (struct lyd_node *)parent->parent;
1185 }
1186
1187 if (attr) {
1188 *attr = at;
1189 }
1190 return ret;
1191}
1192
Radek Krejci084289f2019-07-09 17:35:30 +02001193API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001194lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001195{
Michal Vaskof03ed032020-03-04 13:31:44 +01001196 unsigned int u, x;
Michal Vaskoe444f752020-02-10 12:20:06 +01001197 const struct lyd_node *parent = NULL, *start_search;
1198 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001199 uint64_t pos = 1;
1200
Michal Vaskof03ed032020-03-04 13:31:44 +01001201 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001202
1203 LY_ARRAY_FOR(path, u) {
1204 if (parent) {
1205 start_search = lyd_node_children(parent);
1206search_inner:
Michal Vaskoe444f752020-02-10 12:20:06 +01001207 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 +02001208 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001209 start_search = tree;
Radek Krejci084289f2019-07-09 17:35:30 +02001210search_toplevel:
Michal Vaskof03ed032020-03-04 13:31:44 +01001211 /* WARNING! to use search_toplevel label correctly, variable v must be preserved and not changed! */
1212 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 +02001213 }
1214 if (!node) {
1215 return NULL;
1216 }
1217
1218 /* check predicate if any */
1219 LY_ARRAY_FOR(path[u].predicates, x) {
1220 if (path[u].predicates[x].type == 0) {
1221 /* position predicate */
1222 if (pos != path[u].predicates[x].position) {
1223 pos++;
1224 goto search_repeat;
1225 }
1226 /* done, no more predicates are allowed here */
1227 break;
1228 } else if (path[u].predicates[x].type == 1) {
1229 /* key-predicate */
1230 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].predicates[x].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001231 struct lyd_node *key;
1232 lyd_find_sibling_next(lyd_node_children(node), path[u].predicates[x].key->module,
1233 path[u].predicates[x].key->name, 0, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001234 if (!key) {
1235 /* probably error and we shouldn't be here due to previous checks when creating path */
1236 goto search_repeat;
1237 }
1238 if (type->plugin->compare(&((struct lyd_node_term*)key)->value, path[u].predicates[x].value)) {
1239 goto search_repeat;
1240 }
1241 } else if (path[u].predicates[x].type == 2) {
1242 /* leaf-list-predicate */
1243 struct lysc_type *type = ((struct lysc_node_leaf*)path[u].node)->type;
1244 if (type->plugin->compare(&((struct lyd_node_term*)node)->value, path[u].predicates[x].value)) {
1245 goto search_repeat;
1246 }
1247 } else {
1248 LOGINT(NULL);
1249 }
1250 }
1251
1252 parent = node;
1253 }
1254
1255 return (const struct lyd_node_term*)node;
1256
1257search_repeat:
1258 start_search = node->next;
1259 if (parent) {
1260 goto search_inner;
1261 } else {
1262 goto search_toplevel;
1263 }
1264}
1265
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001266API LY_ERR
1267lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1268{
1269 const struct lyd_node *iter1, *iter2;
1270 struct lyd_node_term *term1, *term2;
1271 struct lyd_node_any *any1, *any2;
1272 struct lysc_type *type;
1273 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001274
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001275 if (!node1 || !node2) {
1276 if (node1 == node2) {
1277 return LY_SUCCESS;
1278 } else {
1279 return LY_ENOT;
1280 }
1281 }
1282
Michal Vasko14654712020-02-06 08:35:21 +01001283 if (node1->schema->module->ctx != node2->schema->module->ctx || node1->schema != node2->schema) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001284 return LY_ENOT;
1285 }
1286
1287 if (node1->hash != node2->hash) {
1288 return LY_ENOT;
1289 }
1290
1291 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
1292
1293 switch (node1->schema->nodetype) {
1294 case LYS_LEAF:
1295 case LYS_LEAFLIST:
1296 if (options & LYD_COMPARE_DEFAULTS) {
1297 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1298 return LY_ENOT;
1299 }
1300 }
1301
1302 term1 = (struct lyd_node_term*)node1;
1303 term2 = (struct lyd_node_term*)node2;
1304 type = ((struct lysc_node_leaf*)node1->schema)->type;
1305
1306 return type->plugin->compare(&term1->value, &term2->value);
1307 case LYS_CONTAINER:
1308 if (options & LYD_COMPARE_DEFAULTS) {
1309 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1310 return LY_ENOT;
1311 }
1312 }
1313 if (options & LYD_COMPARE_FULL_RECURSION) {
1314 iter1 = ((struct lyd_node_inner*)node1)->child;
1315 iter2 = ((struct lyd_node_inner*)node2)->child;
1316 goto all_children_compare;
1317 }
1318 return LY_SUCCESS;
1319 case LYS_ACTION:
1320 if (options & LYD_COMPARE_FULL_RECURSION) {
1321 /* TODO action/RPC
1322 goto all_children_compare;
1323 */
1324 }
1325 return LY_SUCCESS;
1326 case LYS_NOTIF:
1327 if (options & LYD_COMPARE_FULL_RECURSION) {
1328 /* TODO Notification
1329 goto all_children_compare;
1330 */
1331 }
1332 return LY_SUCCESS;
1333 case LYS_LIST:
1334 iter1 = ((struct lyd_node_inner*)node1)->child;
1335 iter2 = ((struct lyd_node_inner*)node2)->child;
1336
Radek Krejci0fe9b512019-07-26 17:51:05 +02001337 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001338 /* lists with keys, their equivalence is based on their keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02001339 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1340 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1341 key = key->next) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001342 if (lyd_compare(iter1, iter2, options)) {
1343 return LY_ENOT;
1344 }
1345 iter1 = iter1->next;
1346 iter2 = iter2->next;
1347 }
1348 } else {
1349 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1350
1351all_children_compare:
1352 if (!iter1 && !iter2) {
1353 /* no children, nothing to compare */
1354 return LY_SUCCESS;
1355 }
1356
1357 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1358 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1359 return LY_ENOT;
1360 }
1361 }
1362 if (iter1 || iter2) {
1363 return LY_ENOT;
1364 }
1365 }
1366 return LY_SUCCESS;
1367 case LYS_ANYXML:
1368 case LYS_ANYDATA:
1369 any1 = (struct lyd_node_any*)node1;
1370 any2 = (struct lyd_node_any*)node2;
1371
1372 if (any1->value_type != any2->value_type) {
1373 return LY_ENOT;
1374 }
1375 switch (any1->value_type) {
1376 case LYD_ANYDATA_DATATREE:
1377 iter1 = any1->value.tree;
1378 iter2 = any2->value.tree;
1379 goto all_children_compare;
1380 case LYD_ANYDATA_STRING:
1381 case LYD_ANYDATA_XML:
1382 case LYD_ANYDATA_JSON:
1383 len1 = strlen(any1->value.str);
1384 len2 = strlen(any2->value.str);
1385 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1386 return LY_ENOT;
1387 }
1388 return LY_SUCCESS;
1389#if 0 /* TODO LYB format */
1390 case LYD_ANYDATA_LYB:
1391 int len1 = lyd_lyb_data_length(any1->value.mem);
1392 int len2 = lyd_lyb_data_length(any2->value.mem);
1393 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1394 return LY_ENOT;
1395 }
1396 return LY_SUCCESS;
1397#endif
1398 }
1399 }
1400
1401 LOGINT(node1->schema->module->ctx);
1402 return LY_EINT;
1403}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001404
1405/**
1406 * @brief Duplicates just a single node and interconnect it into a @p parent (if present) and after the @p prev
1407 * sibling (if present).
1408 *
1409 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
1410 */
1411static struct lyd_node *
1412lyd_dup_recursive(const struct lyd_node *node, struct lyd_node_inner *parent, struct lyd_node *prev, int options)
1413{
1414 struct ly_ctx *ctx;
1415 struct lyd_node *dup = NULL;
1416
1417 LY_CHECK_ARG_RET(NULL, node, NULL);
1418 ctx = node->schema->module->ctx;
1419
1420 switch (node->schema->nodetype) {
1421 case LYS_ACTION:
1422 case LYS_NOTIF:
1423 case LYS_CONTAINER:
1424 case LYS_LIST:
1425 dup = calloc(1, sizeof(struct lyd_node_inner));
1426 break;
1427 case LYS_LEAF:
1428 case LYS_LEAFLIST:
1429 dup = calloc(1, sizeof(struct lyd_node_term));
1430 break;
1431 case LYS_ANYDATA:
1432 case LYS_ANYXML:
1433 dup = calloc(1, sizeof(struct lyd_node_any));
1434 break;
1435 default:
1436 LOGINT(ctx);
1437 goto error;
1438 }
1439
1440 /* TODO implement LYD_DUP_WITH_WHEN */
1441 dup->flags = node->flags;
1442 dup->schema = node->schema;
1443
1444 /* interconnect the node at the end */
1445 dup->parent = parent;
1446 if (prev) {
1447 dup->prev = prev;
1448 prev->next = dup;
1449 } else {
1450 dup->prev = dup;
1451 if (parent) {
1452 parent->child = dup;
1453 }
1454 }
1455 if (parent) {
1456 parent->child->prev = dup;
1457 } else if (prev) {
1458 struct lyd_node *first;
1459 for (first = prev; first->prev != prev; first = first->prev);
1460 first->prev = dup;
1461 }
1462
1463 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1464
1465 /* nodetype-specific work */
1466 if (dup->schema->nodetype & LYD_NODE_TERM) {
1467 struct lyd_node_term *term = (struct lyd_node_term*)dup;
1468 struct lyd_node_term *orig = (struct lyd_node_term*)node;
1469
1470 term->hash = orig->hash;
1471 term->value.realtype = orig->value.realtype;
1472 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(ctx, &orig->value, &term->value),
1473 LOGERR(ctx, LY_EINT, "Value duplication failed."), error);
1474 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1475 struct lyd_node_inner *inner = (struct lyd_node_inner*)dup;
1476 struct lyd_node_inner *orig = (struct lyd_node_inner*)node;
1477 struct lyd_node *child, *last = NULL;
1478
1479 if (options & LYD_DUP_RECURSIVE) {
1480 /* duplicate all the children */
1481 LY_LIST_FOR(orig->child, child) {
1482 last = lyd_dup_recursive(child, inner, last, options);
1483 LY_CHECK_GOTO(!last, error);
1484 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001485 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001486 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001487 child = orig->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001488 for (struct lysc_node *key = ((struct lysc_node_list*)dup->schema)->child;
1489 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1490 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001491 if (!child) {
1492 /* possibly not keys are present in filtered tree */
1493 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001494 } else if (child->schema != key) {
1495 /* possibly not all keys are present in filtered tree,
1496 * but there can be also some non-key nodes */
1497 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001498 }
1499 last = lyd_dup_recursive(child, inner, last, options);
1500 child = child->next;
1501 }
1502 }
1503 lyd_hash(dup);
1504 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
1505 struct lyd_node_any *any = (struct lyd_node_any*)dup;
1506 struct lyd_node_any *orig = (struct lyd_node_any*)node;
1507
1508 any->hash = orig->hash;
1509 any->value_type = orig->value_type;
1510 switch (any->value_type) {
1511 case LYD_ANYDATA_DATATREE:
1512 if (orig->value.tree) {
1513 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
1514 LY_CHECK_GOTO(!any->value.tree, error);
1515 }
1516 break;
1517 case LYD_ANYDATA_STRING:
1518 case LYD_ANYDATA_XML:
1519 case LYD_ANYDATA_JSON:
1520 if (orig->value.str) {
1521 any->value.str = lydict_insert(ctx, orig->value.str, strlen(orig->value.str));
1522 }
1523 break;
1524 }
1525 }
1526
1527 lyd_insert_hash(dup);
1528 return dup;
1529
1530error:
1531 if (!parent && !prev) {
1532 lyd_free_tree(dup);
1533 }
1534 return NULL;
1535}
1536
1537API struct lyd_node *
1538lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1539{
1540 struct ly_ctx *ctx;
1541 const struct lyd_node *orig; /* original node to be duplicated */
1542 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
1543 struct lyd_node *last = NULL; /* the last sibling of the duplicated nodes */
1544 struct lyd_node *top = NULL; /* the most higher created node */
1545 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1546 int keyless_parent_list = 0;
1547
1548 LY_CHECK_ARG_RET(NULL, node, NULL);
1549 ctx = node->schema->module->ctx;
1550
1551 if (options & LYD_DUP_WITH_PARENTS) {
1552 struct lyd_node_inner *orig_parent, *iter;
1553 int repeat = 1;
1554 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1555 if (parent && parent->schema == orig_parent->schema) {
1556 /* stop creating parents, connect what we have into the provided parent */
1557 iter = parent;
1558 repeat = 0;
1559 /* get know if there is a keyless list which we will have to rehash */
1560 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001561 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001562 keyless_parent_list = 1;
1563 break;
1564 }
1565 }
1566 } else {
1567 iter = (struct lyd_node_inner*)lyd_dup_recursive((struct lyd_node*)orig_parent, NULL, NULL, 0);
1568 LY_CHECK_GOTO(!iter, error);
1569 }
1570 if (!local_parent) {
1571 local_parent = iter;
1572 }
1573 if (iter->child) {
1574 /* 1) list - add after keys
1575 * 2) provided parent with some children */
1576 iter->child->prev->next = top;
1577 if (top) {
1578 top->prev = iter->child->prev;
1579 iter->child->prev = top;
1580 }
1581 } else {
1582 iter->child = top;
1583 if (iter->schema->nodetype == LYS_LIST) {
1584 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1585 keyless_parent_list = 1;
1586 }
1587 }
1588 if (top) {
1589 top->parent = iter;
1590 }
1591 top = (struct lyd_node*)iter;
1592 }
1593 if (repeat && parent) {
1594 /* given parent and created parents chain actually do not interconnect */
1595 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1596 goto error;
1597 }
1598 } else {
1599 local_parent = parent;
1600 }
1601
1602 if (local_parent && local_parent->child) {
1603 last = local_parent->child->prev;
1604 }
1605
1606 LY_LIST_FOR(node, orig) {
1607 last = lyd_dup_recursive(orig, local_parent, last, options);
1608 LY_CHECK_GOTO(!last, error);
1609 if (!first) {
1610 first = last;
1611 }
1612
1613 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1614 break;
1615 }
1616 }
1617 if (keyless_parent_list) {
1618 /* rehash */
1619 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001620 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001621 lyd_hash((struct lyd_node*)local_parent);
1622 }
1623 }
1624 }
1625 return first;
1626
1627error:
1628 if (top) {
1629 lyd_free_tree(top);
1630 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001631 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001632 }
1633 return NULL;
1634}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001635
1636static LY_ERR
1637lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
1638{
Michal Vasko14654712020-02-06 08:35:21 +01001639 /* ending \0 */
1640 ++reqlen;
1641
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001642 if (reqlen > *buflen) {
1643 if (is_static) {
1644 return LY_EINCOMPLETE;
1645 }
1646
1647 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
1648 if (!*buffer) {
1649 return LY_EMEM;
1650 }
1651
1652 *buflen = reqlen;
1653 }
1654
1655 return LY_SUCCESS;
1656}
1657
1658/**
1659 * @brief Append all list key predicates to path.
1660 *
1661 * @param[in] node Node with keys to print.
1662 * @param[in,out] buffer Buffer to print to.
1663 * @param[in,out] buflen Current buffer length.
1664 * @param[in,out] bufused Current number of characters used in @p buffer.
1665 * @param[in] is_static Whether buffer is static or can be reallocated.
1666 * @return LY_ERR
1667 */
1668static LY_ERR
1669lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1670{
1671 const struct lyd_node *key;
1672 int dynamic = 0;
1673 size_t len;
1674 const char *val;
1675 char quot;
1676 LY_ERR rc;
1677
Michal Vasko14654712020-02-06 08:35:21 +01001678 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001679 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
1680 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
1681 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1682 if (rc != LY_SUCCESS) {
1683 if (dynamic) {
1684 free((char *)val);
1685 }
1686 return rc;
1687 }
1688
1689 quot = '\'';
1690 if (strchr(val, '\'')) {
1691 quot = '"';
1692 }
1693 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
1694
1695 if (dynamic) {
1696 free((char *)val);
1697 }
1698 }
1699
1700 return LY_SUCCESS;
1701}
1702
1703/**
1704 * @brief Append leaf-list value predicate to path.
1705 *
1706 * @param[in] node Node to print.
1707 * @param[in,out] buffer Buffer to print to.
1708 * @param[in,out] buflen Current buffer length.
1709 * @param[in,out] bufused Current number of characters used in @p buffer.
1710 * @param[in] is_static Whether buffer is static or can be reallocated.
1711 * @return LY_ERR
1712 */
1713static LY_ERR
1714lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1715{
1716 int dynamic = 0;
1717 size_t len;
1718 const char *val;
1719 char quot;
1720 LY_ERR rc;
1721
1722 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
1723 len = 4 + strlen(val) + 2;
1724 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1725 if (rc != LY_SUCCESS) {
1726 goto cleanup;
1727 }
1728
1729 quot = '\'';
1730 if (strchr(val, '\'')) {
1731 quot = '"';
1732 }
1733 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
1734
1735cleanup:
1736 if (dynamic) {
1737 free((char *)val);
1738 }
1739 return rc;
1740}
1741
1742/**
1743 * @brief Append node position (relative to its other instances) predicate to path.
1744 *
1745 * @param[in] node Node to print.
1746 * @param[in,out] buffer Buffer to print to.
1747 * @param[in,out] buflen Current buffer length.
1748 * @param[in,out] bufused Current number of characters used in @p buffer.
1749 * @param[in] is_static Whether buffer is static or can be reallocated.
1750 * @return LY_ERR
1751 */
1752static LY_ERR
1753lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
1754{
1755 const struct lyd_node *first, *iter;
1756 size_t len;
1757 int pos;
1758 char *val = NULL;
1759 LY_ERR rc;
1760
1761 if (node->parent) {
1762 first = node->parent->child;
1763 } else {
1764 for (first = node; node->prev->next; node = node->prev);
1765 }
1766 pos = 1;
1767 for (iter = first; iter != node; iter = iter->next) {
1768 if (iter->schema == node->schema) {
1769 ++pos;
1770 }
1771 }
1772 if (asprintf(&val, "%d", pos) == -1) {
1773 return LY_EMEM;
1774 }
1775
1776 len = 1 + strlen(val) + 1;
1777 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
1778 if (rc != LY_SUCCESS) {
1779 goto cleanup;
1780 }
1781
1782 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
1783
1784cleanup:
1785 free(val);
1786 return rc;
1787}
1788
1789API char *
1790lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
1791{
Michal Vasko14654712020-02-06 08:35:21 +01001792 int is_static = 0, i, depth;
1793 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001794 const struct lyd_node *iter;
1795 const struct lys_module *mod;
1796 LY_ERR rc;
1797
1798 LY_CHECK_ARG_RET(NULL, node, NULL);
1799 if (buffer) {
1800 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
1801 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01001802 } else {
1803 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001804 }
1805
1806 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01001807 case LYD_PATH_LOG:
1808 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001809 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
1810 ++depth;
1811 }
1812
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001813 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01001814 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001815 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01001816 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001817iter_print:
1818 /* print prefix and name */
1819 mod = NULL;
1820 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
1821 mod = iter->schema->module;
1822 }
1823
1824 /* realloc string */
1825 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
1826 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
1827 if (rc != LY_SUCCESS) {
1828 break;
1829 }
1830
1831 /* print next node */
1832 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
1833
1834 switch (iter->schema->nodetype) {
1835 case LYS_LIST:
1836 if (iter->schema->flags & LYS_KEYLESS) {
1837 /* print its position */
1838 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1839 } else {
1840 /* print all list keys in predicates */
1841 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
1842 }
1843 break;
1844 case LYS_LEAFLIST:
1845 if (iter->schema->flags & LYS_CONFIG_W) {
1846 /* print leaf-list value */
1847 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
1848 } else {
1849 /* print its position */
1850 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
1851 }
1852 break;
1853 default:
1854 /* nothing to print more */
1855 rc = LY_SUCCESS;
1856 break;
1857 }
1858 if (rc != LY_SUCCESS) {
1859 break;
1860 }
1861
Michal Vasko14654712020-02-06 08:35:21 +01001862 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001863 }
1864 break;
1865 }
1866
1867 return buffer;
1868}
Michal Vaskoe444f752020-02-10 12:20:06 +01001869
Michal Vasko9b368d32020-02-14 13:53:31 +01001870LY_ERR
1871lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
1872 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01001873{
1874 LY_ERR rc;
1875 const struct lyd_node *node = NULL;
1876 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01001877 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01001878 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01001879 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01001880
Michal Vasko9b368d32020-02-14 13:53:31 +01001881 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01001882
1883 if (!first) {
1884 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01001885 if (match) {
1886 *match = NULL;
1887 }
Michal Vaskoe444f752020-02-10 12:20:06 +01001888 return LY_ENOTFOUND;
1889 }
1890
Michal Vaskoe444f752020-02-10 12:20:06 +01001891 if (key_or_value && !val_len) {
1892 val_len = strlen(key_or_value);
1893 }
1894
1895 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01001896 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01001897 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 +01001898 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
1899 /* parse keys into canonical values */
1900 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, &keys), cleanup);
1901 }
1902
1903 /* find first matching value */
1904 LY_LIST_FOR(first, node) {
1905 if (node->schema != schema) {
1906 continue;
1907 }
1908
1909 if ((schema->nodetype == LYS_LIST) && keys.str) {
1910 /* compare all set keys */
1911 for (i = 0; i < keys.key_count; ++i) {
1912 /* find key */
1913 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
1914 (struct lyd_node **)&term);
1915 if (rc == LY_ENOTFOUND) {
1916 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01001917 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01001918 }
1919 LY_CHECK_GOTO(rc, cleanup);
1920
1921 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01001922 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01001923 break;
1924 }
1925 }
1926
1927 if (i < keys.key_count) {
1928 /* not a match */
1929 continue;
1930 }
Michal Vasko90932a92020-02-12 14:33:03 +01001931 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01001932 term = (struct lyd_node_term *)node;
1933
1934 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01001935 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01001936 /* not a match */
1937 continue;
1938 }
1939 }
1940
1941 /* all criteria passed */
1942 break;
1943 }
1944
1945 if (!node) {
1946 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01001947 if (match) {
1948 *match = NULL;
1949 }
Michal Vaskoe444f752020-02-10 12:20:06 +01001950 goto cleanup;
1951 }
1952
1953 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01001954 if (match) {
1955 *match = (struct lyd_node *)node;
1956 }
Michal Vaskoe444f752020-02-10 12:20:06 +01001957 rc = LY_SUCCESS;
1958
1959cleanup:
1960 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01001961 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001962 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01001963 }
Michal Vaskoe444f752020-02-10 12:20:06 +01001964 return rc;
1965}
1966
1967API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01001968lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
1969 const char *key_or_value, size_t val_len, struct lyd_node **match)
1970{
1971 const struct lysc_node *schema;
1972
1973 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
1974
1975 if (!first) {
1976 /* no data */
1977 *match = NULL;
1978 return LY_ENOTFOUND;
1979 }
1980
1981 /* find schema */
1982 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
1983 if (!schema) {
1984 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
1985 return LY_EINVAL;
1986 }
1987
1988 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
1989}
1990
1991API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01001992lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
1993{
1994 struct lyd_node **match_p;
1995 struct lyd_node_inner *parent;
1996
Michal Vaskof03ed032020-03-04 13:31:44 +01001997 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01001998
1999 if (!siblings) {
2000 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002001 if (match) {
2002 *match = NULL;
2003 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002004 return LY_ENOTFOUND;
2005 }
2006
2007 /* find first sibling */
2008 if (siblings->parent) {
2009 siblings = siblings->parent->child;
2010 } else {
2011 while (siblings->prev->next) {
2012 siblings = siblings->prev;
2013 }
2014 }
2015
2016 parent = (struct lyd_node_inner *)siblings->parent;
2017 if (parent && parent->children_ht) {
2018 assert(target->hash);
2019
2020 /* find by hash */
2021 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2022 siblings = *match_p;
2023 } else {
2024 /* not found */
2025 siblings = NULL;
2026 }
2027 } else {
2028 /* no children hash table */
2029 for (; siblings; siblings = siblings->next) {
2030 if (!lyd_compare(siblings, target, 0)) {
2031 break;
2032 }
2033 }
2034 }
2035
2036 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002037 if (match) {
2038 *match = NULL;
2039 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002040 return LY_ENOTFOUND;
2041 }
2042
Michal Vasko9b368d32020-02-14 13:53:31 +01002043 if (match) {
2044 *match = (struct lyd_node *)siblings;
2045 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002046 return LY_SUCCESS;
2047}
2048
2049API LY_ERR
2050lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2051{
2052 struct lyd_node_inner *parent;
2053 struct lyd_node *match;
2054 struct lyd_node **match_p;
2055 struct ly_set *ret;
2056
2057 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2058
2059 if (!siblings) {
2060 /* no data */
2061 return LY_ENOTFOUND;
2062 }
2063
2064 ret = ly_set_new();
2065 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2066
2067 /* find first sibling */
2068 if (siblings->parent) {
2069 siblings = siblings->parent->child;
2070 } else {
2071 while (siblings->prev->next) {
2072 siblings = siblings->prev;
2073 }
2074 }
2075
2076 parent = (struct lyd_node_inner *)siblings->parent;
2077 if (parent && parent->children_ht) {
2078 assert(target->hash);
2079
2080 /* find by hash */
2081 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2082 match = *match_p;
2083 } else {
2084 /* not found */
2085 match = NULL;
2086 }
2087 while (match) {
2088 /* add all found nodes into the return set */
2089 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2090 goto error;
2091 }
2092
2093 /* find next instance */
2094 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2095 match = NULL;
2096 } else {
2097 match = *match_p;
2098 }
2099 }
2100 } else {
2101 /* no children hash table */
2102 for (; siblings; siblings = siblings->next) {
2103 if (!lyd_compare(siblings, target, 0)) {
2104 /* a match */
2105 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2106 goto error;
2107 }
2108 }
2109 }
2110 }
2111
2112 if (!ret->count) {
2113 ly_set_free(ret, NULL);
2114 return LY_ENOTFOUND;
2115 }
2116
2117 *set = ret;
2118 return LY_SUCCESS;
2119
2120error:
2121 ly_set_free(ret, NULL);
2122 return LY_EMEM;
2123}
2124
Michal Vasko90932a92020-02-12 14:33:03 +01002125static int
2126lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2127{
2128 struct lysc_node *val1;
2129 struct lyd_node *val2;
2130
2131 val1 = *((struct lysc_node **)val1_p);
2132 val2 = *((struct lyd_node **)val2_p);
2133
2134 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2135
2136 if (val1 == val2->schema) {
2137 /* schema match is enough */
2138 return 1;
2139 } else {
2140 return 0;
2141 }
2142}
2143
2144static LY_ERR
2145lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2146{
2147 struct lyd_node **match_p;
2148 struct lyd_node_inner *parent;
2149 uint32_t hash;
2150 values_equal_cb ht_cb;
2151
2152 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2153
2154 /* find first sibling */
2155 if (siblings->parent) {
2156 siblings = siblings->parent->child;
2157 } else {
2158 while (siblings->prev->next) {
2159 siblings = siblings->prev;
2160 }
2161 }
2162
2163 parent = (struct lyd_node_inner *)siblings->parent;
2164 if (parent && parent->children_ht) {
2165 /* calculate our hash */
2166 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2167 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2168 hash = dict_hash_multi(hash, NULL, 0);
2169
2170 /* use special hash table function */
2171 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2172
2173 /* find by hash */
2174 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2175 siblings = *match_p;
2176 } else {
2177 /* not found */
2178 siblings = NULL;
2179 }
2180
2181 /* set the original hash table compare function back */
2182 lyht_set_cb(parent->children_ht, ht_cb);
2183 } else {
2184 /* no children hash table */
2185 for (; siblings; siblings = siblings->next) {
2186 if (siblings->schema == schema) {
2187 /* schema match is enough */
2188 break;
2189 }
2190 }
2191 }
2192
2193 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002194 if (match) {
2195 *match = NULL;
2196 }
Michal Vasko90932a92020-02-12 14:33:03 +01002197 return LY_ENOTFOUND;
2198 }
2199
Michal Vasko9b368d32020-02-14 13:53:31 +01002200 if (match) {
2201 *match = (struct lyd_node *)siblings;
2202 }
Michal Vasko90932a92020-02-12 14:33:03 +01002203 return LY_SUCCESS;
2204}
2205
Michal Vaskoe444f752020-02-10 12:20:06 +01002206API LY_ERR
2207lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2208 size_t val_len, struct lyd_node **match)
2209{
2210 LY_ERR rc;
2211 struct lyd_node *target = NULL;
2212
2213 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
2214 if ((schema->nodetype == LYS_LIST) && schema->flags & LYS_KEYLESS) {
2215 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2216 return LY_EINVAL;
2217 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2218 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2219 return LY_EINVAL;
2220 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2221 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2222 lys_nodetype2str(schema->nodetype), __func__);
2223 return LY_EINVAL;
2224 }
2225
2226 if (!siblings) {
2227 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002228 if (match) {
2229 *match = NULL;
2230 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002231 return LY_ENOTFOUND;
2232 }
2233
Michal Vaskof03ed032020-03-04 13:31:44 +01002234 if (key_or_value && !val_len) {
2235 val_len = strlen(key_or_value);
2236 }
2237
Michal Vasko90932a92020-02-12 14:33:03 +01002238 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002239 switch (schema->nodetype) {
2240 case LYS_CONTAINER:
2241 case LYS_ANYXML:
2242 case LYS_ANYDATA:
2243 case LYS_NOTIF:
Michal Vasko9b368d32020-02-14 13:53:31 +01002244 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002245 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002246 /* find it based on schema only */
2247 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002248 break;
2249 case LYS_LEAFLIST:
2250 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002251 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 +01002252 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002253 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002254 if (schema->nodetype == LYS_LIST) {
2255 /* target used attributes: schema, hash, child (all keys) */
2256 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002257 }
2258
2259 /* find it */
2260 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002261 break;
2262 default:
2263 /* unreachable */
2264 LOGINT(schema->module->ctx);
2265 return LY_EINT;
2266 }
2267
Michal Vaskoe444f752020-02-10 12:20:06 +01002268 lyd_free_tree(target);
2269 return rc;
2270}