blob: d22183a60c30bd58329e8ea38891c930757dab82 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vaskod86997b2020-05-26 15:19:54 +020014#define _POSIX_C_SOURCE 200809L /* strndup */
Radek Krejcie7b95092019-05-15 11:03:07 +020015
16#include "common.h"
17
Radek Krejci084289f2019-07-09 17:35:30 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <stdarg.h>
23#include <stdint.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "log.h"
28#include "tree.h"
29#include "tree_data.h"
30#include "tree_data_internal.h"
Michal Vaskod3678892020-05-21 10:06:58 +020031#include "tree_schema_internal.h"
Michal Vasko90932a92020-02-12 14:33:03 +010032#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "tree_schema.h"
Michal Vaskod3678892020-05-21 10:06:58 +020034#include "xpath.h"
Michal Vasko52927e22020-03-16 17:26:14 +010035#include "xml.h"
Radek Krejci38d85362019-09-05 16:26:38 +020036#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010037#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038
Michal Vaskoe444f752020-02-10 12:20:06 +010039struct ly_keys {
40 char *str;
41 struct {
42 const struct lysc_node_leaf *schema;
43 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010044 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010045 } *keys;
46 size_t key_count;
47};
48
Radek Krejci084289f2019-07-09 17:35:30 +020049LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010050lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010051 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020052{
Michal Vasko90932a92020-02-12 14:33:03 +010053 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020054 struct ly_err_item *err = NULL;
55 struct ly_ctx *ctx;
56 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020057 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010058 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020059 assert(node);
60
61 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020062
Radek Krejci73dead22019-07-11 16:46:16 +020063 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020064 if (!second) {
65 node->value.realtype = type;
66 }
Michal Vasko90932a92020-02-12 14:33:03 +010067 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vaskof03ed032020-03-04 13:31:44 +010068 tree ? (void *)node : (void *)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +020069 &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010070 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020071 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020072 /* node may not be connected yet so use the schema node */
73 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
Radek Krejci73dead22019-07-11 16:46:16 +020074 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020075 }
Radek Krejci73dead22019-07-11 16:46:16 +020076 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010077 } else if (dynamic) {
78 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020079 }
80
81error:
82 return ret;
83}
84
Michal Vasko90932a92020-02-12 14:33:03 +010085/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
86static LY_ERR
87lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
88 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
89{
90 LY_ERR ret = LY_SUCCESS;
91 struct ly_err_item *err = NULL;
92 struct ly_ctx *ctx;
93 struct lysc_type *type;
94 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
95
96 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
97
98 ctx = schema->module->ctx;
99 type = ((struct lysc_node_leaf *)schema)->type;
100 val->realtype = type;
101 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
102 val, NULL, &err);
103 if (ret == LY_EINCOMPLETE) {
104 /* this is fine, we do not need it resolved */
105 ret = LY_SUCCESS;
106 } else if (ret && err) {
107 ly_err_print(err);
108 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
109 ly_err_free(err);
110 }
111 if (!ret && dynamic) {
112 *dynamic = 0;
113 }
114
115 return ret;
116}
117
Radek Krejci38d85362019-09-05 16:26:38 +0200118LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100119lyd_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 +0100120 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100121 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200122{
Michal Vasko90932a92020-02-12 14:33:03 +0100123 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200124 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200125 struct lyext_metadata *ant;
126 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100127 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200128
Michal Vasko9f96a052020-03-10 09:41:45 +0100129 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100130
Michal Vasko9f96a052020-03-10 09:41:45 +0100131 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200132
133 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100134 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200135 }
Michal Vasko90932a92020-02-12 14:33:03 +0100136 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100137 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100138 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200139 if (err) {
140 ly_err_print(err);
141 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
142 ly_err_free(err);
143 }
144 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100145 } else if (dynamic) {
146 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200147 }
148
149error:
150 return ret;
151}
152
Radek Krejci084289f2019-07-09 17:35:30 +0200153API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100154lys_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 +0200155 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
156{
157 LY_ERR rc = LY_SUCCESS;
158 struct ly_err_item *err = NULL;
159 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200160
161 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
162
163 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
164 LOGARG(ctx, node);
165 return LY_EINVAL;
166 }
167
168 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200169 /* just validate, no storing of enything */
170 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
171 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
172 if (rc == LY_EINCOMPLETE) {
173 /* actually success since we do not provide the context tree and call validation with
174 * LY_TYPE_OPTS_INCOMPLETE_DATA */
175 rc = LY_SUCCESS;
176 } else if (rc && err) {
177 if (ctx) {
178 /* log only in case the ctx was provided as input parameter */
179 ly_err_print(err);
180 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200181 }
Radek Krejci73dead22019-07-11 16:46:16 +0200182 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200183 }
184
185 return rc;
186}
187
188API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100189lyd_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 +0100190 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 +0200191{
192 LY_ERR rc;
193 struct ly_err_item *err = NULL;
194 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100195 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200196
197 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
198
199 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200200 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100201 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200202 NULL, NULL, &err);
203 if (rc == LY_EINCOMPLETE) {
204 return rc;
205 } else if (rc) {
206 if (err) {
207 if (ctx) {
208 ly_err_print(err);
209 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200210 }
Radek Krejci73dead22019-07-11 16:46:16 +0200211 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200212 }
Radek Krejci73dead22019-07-11 16:46:16 +0200213 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200214 }
215
216 return LY_SUCCESS;
217}
218
219API LY_ERR
220lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100221 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 +0200222{
223 LY_ERR ret = LY_SUCCESS, rc;
224 struct ly_err_item *err = NULL;
225 struct ly_ctx *ctx;
226 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200227 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100228 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200229
230 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
231
232 ctx = node->schema->module->ctx;
233 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200234 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 +0100235 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200236 if (rc == LY_EINCOMPLETE) {
237 ret = rc;
238 /* continue with comparing, just remember what to return if storing is ok */
239 } else if (rc) {
240 /* value to compare is invalid */
241 ret = LY_EINVAL;
242 if (err) {
243 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200244 }
Radek Krejci73dead22019-07-11 16:46:16 +0200245 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200246 }
247
248 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200249 if (type->plugin->compare(&node->value, &data)) {
250 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
251 ret = LY_EVALID;
252 }
Radek Krejci084289f2019-07-09 17:35:30 +0200253
254cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200255 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200256
257 return ret;
258}
259
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200260API const char *
261lyd_value2str(const struct lyd_node_term *node, int *dynamic)
262{
263 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
264
265 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
266}
267
268API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100269lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200270{
Michal Vasko9f96a052020-03-10 09:41:45 +0100271 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200272
Michal Vasko9f96a052020-03-10 09:41:45 +0100273 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200274}
275
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200276API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100277lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200278{
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200280#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200282#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200283
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200284 LY_CHECK_ARG_RET(ctx, ctx, NULL);
285
Michal Vasko5b37a352020-03-06 13:38:33 +0100286 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
287 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
288 return NULL;
289 }
290
Michal Vaskoa3881362020-01-21 15:57:35 +0100291#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200292 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200293 /* first item in trees is mandatory - the RPC/action request */
294 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
295 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
296 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
297 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200298 return NULL;
299 }
300 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200301
Radek Krejcie7b95092019-05-15 11:03:07 +0200302 if (options & LYD_OPT_DATA_TEMPLATE) {
303 yang_data_name = va_arg(ap, const char *);
304 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200305#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200306
307 if (!format) {
308 /* TODO try to detect format from the content */
309 }
310
311 switch (format) {
312 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100313 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200314 break;
315#if 0
316 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200317 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 break;
319 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200320 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 break;
322#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100323 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200324 LOGINT(ctx);
325 break;
326 }
327
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 return result;
329}
330
331API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100332lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200333{
334 struct lyd_node *result;
335 size_t length;
336 char *addr;
337
338 LY_CHECK_ARG_RET(ctx, ctx, NULL);
339 if (fd < 0) {
340 LOGARG(ctx, fd);
341 return NULL;
342 }
343
344 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100345 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200346 if (addr) {
347 ly_munmap(addr, length);
348 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200349
350 return result;
351}
352
353API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100354lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200355{
356 int fd;
357 struct lyd_node *result;
358 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200359
360 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
361
362 fd = open(path, O_RDONLY);
363 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
364
365 if (!format) {
366 /* unknown format - try to detect it from filename's suffix */
367 len = strlen(path);
368
369 /* ignore trailing whitespaces */
370 for (; len > 0 && isspace(path[len - 1]); len--);
371
372 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
373 format = LYD_XML;
374#if 0
375 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
376 format = LYD_JSON;
377 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
378 format = LYD_LYB;
379#endif
380 } /* else still unknown, try later to detect it from the content */
381 }
382
Michal Vaskoa3881362020-01-21 15:57:35 +0100383 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200384 close(fd);
385
386 return result;
387}
Radek Krejci084289f2019-07-09 17:35:30 +0200388
Michal Vasko90932a92020-02-12 14:33:03 +0100389LY_ERR
390lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
391 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
392{
393 LY_ERR ret;
394 struct lyd_node_term *term;
395
Michal Vasko9b368d32020-02-14 13:53:31 +0100396 assert(schema->nodetype & LYD_NODE_TERM);
397
Michal Vasko90932a92020-02-12 14:33:03 +0100398 term = calloc(1, sizeof *term);
399 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
400
401 term->schema = schema;
402 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100403 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100404
405 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
406 if (ret && (ret != LY_EINCOMPLETE)) {
407 free(term);
408 return ret;
409 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100410 lyd_hash((struct lyd_node *)term);
411
412 *node = (struct lyd_node *)term;
413 return ret;
414}
415
416LY_ERR
417lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
418{
419 LY_ERR ret;
420 struct lyd_node_term *term;
421 struct lysc_type *type;
422
423 assert(schema->nodetype & LYD_NODE_TERM);
424
425 term = calloc(1, sizeof *term);
426 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
427
428 term->schema = schema;
429 term->prev = (struct lyd_node *)term;
430 term->flags = LYD_NEW;
431
432 type = ((struct lysc_node_leaf *)schema)->type;
433 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
434 if (ret) {
435 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
436 free(term);
437 return ret;
438 }
439 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100440
441 *node = (struct lyd_node *)term;
442 return ret;
443}
444
445LY_ERR
446lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
447{
448 struct lyd_node_inner *in;
449
Michal Vasko9b368d32020-02-14 13:53:31 +0100450 assert(schema->nodetype & LYD_NODE_INNER);
451
Michal Vasko90932a92020-02-12 14:33:03 +0100452 in = calloc(1, sizeof *in);
453 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
454
455 in->schema = schema;
456 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100457 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100458
Michal Vasko9b368d32020-02-14 13:53:31 +0100459 /* do not hash list with keys, we need them for the hash */
460 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
461 lyd_hash((struct lyd_node *)in);
462 }
Michal Vasko90932a92020-02-12 14:33:03 +0100463
464 *node = (struct lyd_node *)in;
465 return LY_SUCCESS;
466}
467
468static void
469ly_keys_clean(struct ly_keys *keys)
470{
471 size_t i;
472
473 for (i = 0; i < keys->key_count; ++i) {
474 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
475 }
476 free(keys->str);
477 free(keys->keys);
478}
479
480static char *
481ly_keys_parse_next(char **next_key, char **key_name)
482{
483 char *ptr, *ptr2, *val, quot;
Michal Vaskoc2f11292020-05-22 16:43:47 +0200484 const char *pref;
485 size_t pref_len, key_len;
486 int have_equal = 0;
Michal Vasko90932a92020-02-12 14:33:03 +0100487
488 ptr = *next_key;
489
490 /* "[" */
491 LY_CHECK_GOTO(ptr[0] != '[', error);
492 ++ptr;
493
Michal Vaskoc2f11292020-05-22 16:43:47 +0200494 /* skip WS */
495 while (isspace(ptr[0])) {
496 ++ptr;
497 }
Michal Vasko90932a92020-02-12 14:33:03 +0100498
Michal Vaskoc2f11292020-05-22 16:43:47 +0200499 /* key name without prefix */
500 LY_CHECK_GOTO(ly_parse_nodeid((const char **)&ptr, &pref, &pref_len, (const char **)key_name, &key_len), error);
501 if (pref) {
502 goto error;
503 }
Michal Vasko90932a92020-02-12 14:33:03 +0100504
Michal Vaskoc2f11292020-05-22 16:43:47 +0200505 /* terminate it */
506 LY_CHECK_GOTO((ptr[0] != '=') && !isspace(ptr[0]), error);
507 if (ptr[0] == '=') {
508 have_equal = 1;
509 }
510 ptr[0] = '\0';
511 ++ptr;
512
513 if (!have_equal) {
514 /* skip WS */
515 while (isspace(ptr[0])) {
516 ++ptr;
517 }
518
519 /* '=' */
520 LY_CHECK_GOTO(ptr[0] != '=', error);
521 ++ptr;
522 }
523
524 /* skip WS */
525 while (isspace(ptr[0])) {
526 ++ptr;
527 }
Michal Vasko90932a92020-02-12 14:33:03 +0100528
529 /* quote */
530 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
531 quot = ptr[0];
532 ++ptr;
533
534 /* value, terminate it */
535 val = ptr;
536 ptr2 = strchr(ptr, quot);
537 LY_CHECK_GOTO(!ptr2, error);
538 ptr2[0] = '\0';
539
540 /* \0, was quote */
541 ptr = ptr2 + 1;
542
Michal Vaskoc2f11292020-05-22 16:43:47 +0200543 /* skip WS */
544 while (isspace(ptr[0])) {
545 ++ptr;
546 }
547
Michal Vasko90932a92020-02-12 14:33:03 +0100548 /* "]" */
549 LY_CHECK_GOTO(ptr[0] != ']', error);
550 ++ptr;
551
552 *next_key = ptr;
553 return val;
554
555error:
556 *next_key = ptr;
557 return NULL;
558}
559
Michal Vaskod3678892020-05-21 10:06:58 +0200560/* fill keys structure that is expected to be zeroed and must always be cleaned (even on error);
561 * if store is set, fill also each val */
Michal Vasko90932a92020-02-12 14:33:03 +0100562static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200563ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, int log,
564 struct ly_keys *keys)
Michal Vasko90932a92020-02-12 14:33:03 +0100565{
566 LY_ERR ret = LY_SUCCESS;
567 char *next_key, *name;
568 const struct lysc_node *key;
569 size_t i;
570
571 assert(list->nodetype == LYS_LIST);
572
Michal Vaskof03ed032020-03-04 13:31:44 +0100573 if (!keys_str) {
574 /* nothing to parse */
575 return LY_SUCCESS;
576 }
577
578 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100579 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
580
581 next_key = keys->str;
582 while (next_key[0]) {
583 /* new key */
584 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
585 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
586
587 /* fill */
588 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
589 if (!keys->keys[keys->key_count].value) {
Michal Vaskod3678892020-05-21 10:06:58 +0200590 if (log) {
591 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
592 }
Michal Vasko90932a92020-02-12 14:33:03 +0100593 ret = LY_EINVAL;
594 goto cleanup;
595 }
596
597 /* find schema node */
598 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
599 if (!key) {
Michal Vaskod3678892020-05-21 10:06:58 +0200600 if (log) {
601 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
602 }
Michal Vasko90932a92020-02-12 14:33:03 +0100603 ret = LY_EINVAL;
604 goto cleanup;
605 }
606 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
607
608 /* check that we do not have it already */
609 for (i = 0; i < keys->key_count; ++i) {
610 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
Michal Vaskod3678892020-05-21 10:06:58 +0200611 if (log) {
612 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
613 }
Michal Vasko90932a92020-02-12 14:33:03 +0100614 ret = LY_EINVAL;
615 goto cleanup;
616 }
617 }
618
619 if (store) {
620 /* store the value */
621 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
622 lydjson_resolve_prefix, NULL, LYD_JSON);
623 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae420e92020-05-21 10:00:40 +0200624 } else {
625 memset(&keys->keys[keys->key_count].val, 0, sizeof keys->keys[keys->key_count].val);
Michal Vasko90932a92020-02-12 14:33:03 +0100626 }
627
628 /* another valid key */
629 ++keys->key_count;
630 }
631
632cleanup:
Michal Vasko90932a92020-02-12 14:33:03 +0100633 return ret;
634}
635
636LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200637lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, LYD_FORMAT keys_format, int log,
638 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100639{
640 LY_ERR ret = LY_SUCCESS;
641 const struct lysc_node *key_s;
642 struct lyd_node *list = NULL, *key;
643 struct ly_keys keys = {0};
644 size_t i;
645
Michal Vaskod3678892020-05-21 10:06:58 +0200646 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS) && (keys_format != LYD_XML));
Michal Vasko90932a92020-02-12 14:33:03 +0100647
648 /* parse keys */
Michal Vaskod3678892020-05-21 10:06:58 +0200649 LY_CHECK_GOTO(ret = ly_keys_parse(schema, keys_str, keys_len, 0, log, &keys), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100650
651 /* create list */
652 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
653
654 /* everything was checked except that all keys are set */
655 i = 0;
656 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
657 ++i;
658 }
659 if (i != keys.key_count) {
Michal Vaskod3678892020-05-21 10:06:58 +0200660 if (log) {
661 LOGERR(schema->module->ctx, LY_EINVAL, "List \"%s\" is missing some keys.", schema->name);
662 }
Michal Vasko90932a92020-02-12 14:33:03 +0100663 ret = LY_EINVAL;
664 goto cleanup;
665 }
666
667 /* create and insert all the keys */
668 for (i = 0; i < keys.key_count; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +0200669 if (keys_format == LYD_JSON) {
670 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
671 NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
672 } else {
673 assert(keys_format == LYD_SCHEMA);
674 ret = lyd_create_term((struct lysc_node *)keys.keys[i].schema, keys.keys[i].value, strlen(keys.keys[i].value),
675 NULL, lys_resolve_prefix, NULL, LYD_SCHEMA, &key);
676 }
677 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100678 lyd_insert_node(list, NULL, key);
679 }
680
Michal Vasko9b368d32020-02-14 13:53:31 +0100681 /* hash having all the keys */
682 lyd_hash(list);
683
Michal Vasko90932a92020-02-12 14:33:03 +0100684 /* success */
685 *node = list;
686 list = NULL;
687
688cleanup:
689 lyd_free_tree(list);
690 ly_keys_clean(&keys);
691 return ret;
692}
693
694LY_ERR
695lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
696{
697 struct lyd_node_any *any;
698
Michal Vasko9b368d32020-02-14 13:53:31 +0100699 assert(schema->nodetype & LYD_NODE_ANY);
700
Michal Vasko90932a92020-02-12 14:33:03 +0100701 any = calloc(1, sizeof *any);
702 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
703
704 any->schema = schema;
705 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100706 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100707
708 any->value.xml = value;
709 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100711
712 *node = (struct lyd_node *)any;
713 return LY_SUCCESS;
714}
715
Michal Vasko52927e22020-03-16 17:26:14 +0100716LY_ERR
717lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
718 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
719 const char *ns, struct lyd_node **node)
720{
721 struct lyd_node_opaq *opaq;
722
723 assert(ctx && name && name_len && ns);
724
725 if (!value_len) {
726 value = "";
727 }
728
729 opaq = calloc(1, sizeof *opaq);
730 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
731
732 opaq->prev = (struct lyd_node *)opaq;
733
734 opaq->name = lydict_insert(ctx, name, name_len);
735 opaq->format = format;
736 if (pref_len) {
737 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
738 }
739 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
740 opaq->val_prefs = val_prefs;
741 if (dynamic && *dynamic) {
742 opaq->value = lydict_insert_zc(ctx, (char *)value);
743 *dynamic = 0;
744 } else {
745 opaq->value = lydict_insert(ctx, value, value_len);
746 }
747 opaq->ctx = ctx;
748
749 *node = (struct lyd_node *)opaq;
750 return LY_SUCCESS;
751}
752
Michal Vasko013a8182020-03-03 10:46:53 +0100753API struct lyd_node *
754lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
755{
756 struct lyd_node *ret = NULL;
757 const struct lysc_node *schema;
758 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
759
760 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
761
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 if (!module) {
763 module = parent->schema->module;
764 }
765
Michal Vasko1bf09392020-03-27 12:38:10 +0100766 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vaskob00f3cf2020-05-27 11:20:13 +0200767 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100768
769 if (!lyd_create_inner(schema, &ret) && parent) {
770 lyd_insert_node(parent, NULL, ret);
771 }
772 return ret;
773}
774
775API struct lyd_node *
776lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
777{
778 struct lyd_node *ret = NULL, *key;
779 const struct lysc_node *schema, *key_s;
780 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
781 va_list ap;
782 const char *key_val;
783 LY_ERR rc = LY_SUCCESS;
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, LYS_LIST, 0);
792 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
793
794 /* create list inner node */
795 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
796
797 va_start(ap, name);
798
799 /* create and insert all the keys */
800 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
801 key_val = va_arg(ap, const char *);
802
Michal Vaskof03ed032020-03-04 13:31:44 +0100803 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
804 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100805 lyd_insert_node(ret, NULL, key);
806 }
807
808 /* hash having all the keys */
809 lyd_hash(ret);
810
811 if (parent) {
812 lyd_insert_node(parent, NULL, ret);
813 }
814
815cleanup:
816 if (rc) {
817 lyd_free_tree(ret);
818 ret = NULL;
819 }
820 va_end(ap);
821 return ret;
822}
823
824API struct lyd_node *
825lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
826{
827 struct lyd_node *ret = NULL;
828 const struct lysc_node *schema;
829 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
830
831 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
832
Michal Vaskof03ed032020-03-04 13:31:44 +0100833 if (!module) {
834 module = parent->schema->module;
835 }
836
Michal Vasko013a8182020-03-03 10:46:53 +0100837 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
838 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
839
Michal Vaskod3678892020-05-21 10:06:58 +0200840 if (!lyd_create_list(schema, keys, keys ? strlen(keys) : 0, LYD_JSON, 1, &ret) && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100841 lyd_insert_node(parent, NULL, ret);
842 }
843 return ret;
844}
845
846API struct lyd_node *
847lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
848{
849 struct lyd_node *ret = NULL;
850 const struct lysc_node *schema;
851 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
852
853 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
854
Michal Vaskof03ed032020-03-04 13:31:44 +0100855 if (!module) {
856 module = parent->schema->module;
857 }
858
Michal Vasko013a8182020-03-03 10:46:53 +0100859 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
860 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
861
Michal Vaskof03ed032020-03-04 13:31:44 +0100862 if (!lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret)
863 && parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100864 lyd_insert_node(parent, NULL, ret);
865 }
866 return ret;
867}
868
869API struct lyd_node *
870lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
871 LYD_ANYDATA_VALUETYPE value_type)
872{
873 struct lyd_node *ret = NULL;
874 const struct lysc_node *schema;
875 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
876
877 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
878
Michal Vaskof03ed032020-03-04 13:31:44 +0100879 if (!module) {
880 module = parent->schema->module;
881 }
882
Michal Vasko013a8182020-03-03 10:46:53 +0100883 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
884 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
885
886 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
887 lyd_insert_node(parent, NULL, ret);
888 }
889 return ret;
890}
891
Michal Vaskod86997b2020-05-26 15:19:54 +0200892API struct lyd_meta *
893lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
894{
895 struct lyd_meta *ret = NULL;
896 struct ly_ctx *ctx = parent->schema->module->ctx;
897 const char *prefix, *tmp;
898 char *str;
899 size_t pref_len, name_len;
900
901 LY_CHECK_ARG_RET(ctx, parent, name, module || strchr(name, ':'), NULL);
902
903 /* parse the name */
904 tmp = name;
905 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
906 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
907 return NULL;
908 }
909
910 /* find the module */
911 if (prefix) {
912 str = strndup(name, name_len);
913 module = ly_ctx_get_module_implemented(ctx, str);
914 free(str);
915 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%*.s\" not found.", pref_len, prefix), NULL);
916 }
917
918 /* set value if none */
919 if (!val_str) {
920 val_str = "";
921 }
922
923 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
924 LYD_JSON, parent->schema);
925 return ret;
926}
927
Michal Vasko90932a92020-02-12 14:33:03 +0100928struct lyd_node *
929lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
930{
931 const struct lysc_node *prev_key;
932 struct lyd_node *match = NULL;
933
934 if (!first_sibling) {
935 return NULL;
936 }
937
938 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
939 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
940 }
941
942 return match;
943}
944
945/**
946 * @brief Insert node after a sibling.
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] sibling Sibling to insert after.
951 * @param[in] node Node to insert.
952 */
953static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100954lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100955{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100956 struct lyd_node_inner *par;
957
Michal Vasko90932a92020-02-12 14:33:03 +0100958 assert(!node->next && (node->prev == node));
959
960 node->next = sibling->next;
961 node->prev = sibling;
962 sibling->next = node;
963 if (node->next) {
964 /* sibling had a succeeding node */
965 node->next->prev = node;
966 } else {
967 /* sibling was last, find first sibling and change its prev */
968 if (sibling->parent) {
969 sibling = sibling->parent->child;
970 } else {
971 for (; sibling->prev->next != node; sibling = sibling->prev);
972 }
973 sibling->prev = node;
974 }
975 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100976
Michal Vasko9f96a052020-03-10 09:41:45 +0100977 for (par = node->parent; par; par = par->parent) {
978 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
979 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100980 par->flags &= ~LYD_DEFAULT;
981 }
Michal Vasko9f96a052020-03-10 09:41:45 +0100982 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
983 /* rehash key-less list */
984 lyd_hash((struct lyd_node *)par);
985 }
Michal Vasko0249f7c2020-03-05 16:36:40 +0100986 }
987
988 /* insert into hash table */
989 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +0100990}
991
992/**
993 * @brief Insert node before a sibling.
994 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 * Handles inserting into NP containers and key-less lists.
996 *
Michal Vasko90932a92020-02-12 14:33:03 +0100997 * @param[in] sibling Sibling to insert before.
998 * @param[in] node Node to insert.
999 */
1000static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001001lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001002{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001003 struct lyd_node_inner *par;
1004
Michal Vasko90932a92020-02-12 14:33:03 +01001005 assert(!node->next && (node->prev == node));
1006
1007 node->next = sibling;
1008 /* covers situation of sibling being first */
1009 node->prev = sibling->prev;
1010 sibling->prev = node;
1011 if (node->prev->next) {
1012 /* sibling had a preceding node */
1013 node->prev->next = node;
1014 } else if (sibling->parent) {
1015 /* sibling was first and we must also change parent child pointer */
1016 sibling->parent->child = node;
1017 }
1018 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001019
Michal Vasko9f96a052020-03-10 09:41:45 +01001020 for (par = node->parent; par; par = par->parent) {
1021 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1022 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001023 par->flags &= ~LYD_DEFAULT;
1024 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001025 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1026 /* rehash key-less list */
1027 lyd_hash((struct lyd_node *)par);
1028 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001029 }
1030
1031 /* insert into hash table */
1032 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001033}
1034
1035/**
1036 * @brief Insert node as the last child of a parent.
1037 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001038 * Handles inserting into NP containers and key-less lists.
1039 *
Michal Vasko90932a92020-02-12 14:33:03 +01001040 * @param[in] parent Parent to insert into.
1041 * @param[in] node Node to insert.
1042 */
1043static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001044lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001045{
1046 struct lyd_node_inner *par;
1047
Michal Vasko0249f7c2020-03-05 16:36:40 +01001048 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001049 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001050
1051 par = (struct lyd_node_inner *)parent;
1052
1053 if (!par->child) {
1054 par->child = node;
1055 } else {
1056 node->prev = par->child->prev;
1057 par->child->prev->next = node;
1058 par->child->prev = node;
1059 }
1060 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001061
Michal Vasko9f96a052020-03-10 09:41:45 +01001062 for (; par; par = par->parent) {
1063 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1064 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001065 par->flags &= ~LYD_DEFAULT;
1066 }
Michal Vasko52927e22020-03-16 17:26:14 +01001067 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001068 /* rehash key-less list */
1069 lyd_hash((struct lyd_node *)par);
1070 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001071 }
1072
1073 /* insert into hash table */
1074 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001075}
1076
1077void
Michal Vasko9b368d32020-02-14 13:53:31 +01001078lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001079{
Michal Vasko9b368d32020-02-14 13:53:31 +01001080 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001081 const struct lysc_node *skey = NULL;
1082 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001083
Michal Vasko52927e22020-03-16 17:26:14 +01001084 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001085
1086 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1087 parent = (struct lyd_node *)(*first_sibling)->parent;
1088 }
Michal Vasko90932a92020-02-12 14:33:03 +01001089
1090 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001091 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001092 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001093 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1094 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001095 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001096 } else if (lyd_node_children(parent)) {
Radek Krejcidae0ee82020-05-06 16:53:24 +02001097 lyd_insert_before_node(lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001098 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001099 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001100 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001101
1102 /* hash list if all its keys were added */
1103 assert(parent->schema->nodetype == LYS_LIST);
Radek Krejcidae0ee82020-05-06 16:53:24 +02001104 anchor = lyd_node_children(parent);
Michal Vasko9f96a052020-03-10 09:41:45 +01001105 has_keys = 1;
1106 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1107 if (!anchor || (anchor->schema != skey)) {
1108 /* key missing */
1109 has_keys = 0;
1110 break;
1111 }
1112
1113 anchor = anchor->next;
1114 }
1115 if (has_keys) {
1116 lyd_hash(parent);
1117 }
1118
Michal Vasko90932a92020-02-12 14:33:03 +01001119 } else {
1120 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001121 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001122 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001123 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001124 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001125 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001126 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001127 anchor = anchor->prev;
1128 }
1129
Michal Vaskoc193ce92020-03-06 11:04:48 +01001130 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001131 /* insert after last sibling from this module */
1132 lyd_insert_after_node(anchor, node);
1133 } else {
1134 /* no data from this module, insert at the last position */
1135 lyd_insert_after_node((*first_sibling)->prev, node);
1136 }
Michal Vasko90932a92020-02-12 14:33:03 +01001137 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001138 /* the only sibling */
1139 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001140 }
Michal Vasko90932a92020-02-12 14:33:03 +01001141}
1142
Michal Vaskof03ed032020-03-04 13:31:44 +01001143static LY_ERR
1144lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1145{
1146 const struct lysc_node *par2;
1147
1148 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001149 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001150
1151 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001152 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001153
1154 if (parent) {
1155 /* inner node */
1156 if (par2 != parent) {
1157 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1158 return LY_EINVAL;
1159 }
1160 } else {
1161 /* top-level node */
1162 if (par2) {
1163 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1164 return LY_EINVAL;
1165 }
1166 }
1167
1168 return LY_SUCCESS;
1169}
1170
1171API LY_ERR
1172lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1173{
1174 struct lyd_node *iter;
1175
1176 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1177
1178 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1179
1180 if (node->schema->flags & LYS_KEY) {
1181 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1182 return LY_EINVAL;
1183 }
1184
1185 if (node->parent || node->prev->next) {
1186 lyd_unlink_tree(node);
1187 }
1188
1189 while (node) {
1190 iter = node->next;
1191 lyd_unlink_tree(node);
1192 lyd_insert_node(parent, NULL, node);
1193 node = iter;
1194 }
1195 return LY_SUCCESS;
1196}
1197
1198API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001199lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1200{
1201 struct lyd_node *iter;
1202
1203 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1204
Michal Vasko62ed12d2020-05-21 10:08:25 +02001205 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001206
1207 if (node->schema->flags & LYS_KEY) {
1208 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1209 return LY_EINVAL;
1210 }
1211
1212 if (node->parent || node->prev->next) {
1213 lyd_unlink_tree(node);
1214 }
1215
1216 while (node) {
1217 iter = node->next;
1218 lyd_unlink_tree(node);
1219 lyd_insert_node(NULL, &sibling, node);
1220 node = iter;
1221 }
1222 return LY_SUCCESS;
1223}
1224
Michal Vasko0249f7c2020-03-05 16:36:40 +01001225static LY_ERR
1226lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1227{
1228 if (sibling->parent) {
1229 /* nested, we do not care for the order */
1230 return LY_SUCCESS;
1231 }
1232
1233 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001234 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1235 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001236 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 +01001237 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001238 return LY_EINVAL;
1239 }
1240
Michal Vaskoc193ce92020-03-06 11:04:48 +01001241 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1242 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001243 /* inserting before/after its module data */
1244 return LY_SUCCESS;
1245 }
1246 }
1247
1248 /* find first sibling */
1249 while (sibling->prev->next) {
1250 sibling = sibling->prev;
1251 }
1252
1253 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001254 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001255 /* inserting before its module data */
1256 return LY_SUCCESS;
1257 }
1258 }
1259
1260 /* check there are no data of this module */
1261 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001262 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001263 /* some data of this module found */
1264 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001265 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001266 return LY_EINVAL;
1267 }
1268 }
1269
1270 return LY_SUCCESS;
1271}
1272
Michal Vaskob1b5c262020-03-05 14:29:47 +01001273API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001274lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1275{
1276 struct lyd_node *iter;
1277
1278 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1279
Michal Vasko62ed12d2020-05-21 10:08:25 +02001280 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001281
1282 if (node->schema->flags & LYS_KEY) {
1283 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1284 return LY_EINVAL;
1285 } else if (sibling->schema->flags & LYS_KEY) {
1286 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1287 return LY_EINVAL;
1288 }
1289
Michal Vasko0249f7c2020-03-05 16:36:40 +01001290 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1291
Michal Vaskof03ed032020-03-04 13:31:44 +01001292 if (node->parent || node->prev->next) {
1293 lyd_unlink_tree(node);
1294 }
1295
1296 /* insert in reverse order to get the original order */
1297 node = node->prev;
1298 while (node) {
1299 iter = node->prev;
1300 lyd_unlink_tree(node);
1301
1302 lyd_insert_before_node(sibling, node);
1303 /* move the anchor accordingly */
1304 sibling = node;
1305
1306 node = (iter == node) ? NULL : iter;
1307 }
1308 return LY_SUCCESS;
1309}
1310
1311API LY_ERR
1312lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1313{
1314 struct lyd_node *iter;
1315
1316 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1317
Michal Vasko62ed12d2020-05-21 10:08:25 +02001318 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001319
1320 if (node->schema->flags & LYS_KEY) {
1321 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1322 return LY_EINVAL;
1323 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1324 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1325 return LY_EINVAL;
1326 }
1327
Michal Vasko0249f7c2020-03-05 16:36:40 +01001328 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1329
Michal Vaskof03ed032020-03-04 13:31:44 +01001330 if (node->parent || node->prev->next) {
1331 lyd_unlink_tree(node);
1332 }
1333
1334 while (node) {
1335 iter = node->next;
1336 lyd_unlink_tree(node);
1337
1338 lyd_insert_after_node(sibling, node);
1339 /* move the anchor accordingly */
1340 sibling = node;
1341
1342 node = iter;
1343 }
1344 return LY_SUCCESS;
1345}
1346
1347API void
1348lyd_unlink_tree(struct lyd_node *node)
1349{
1350 struct lyd_node *iter;
1351
1352 if (!node) {
1353 return;
1354 }
1355
1356 /* unlink from siblings */
1357 if (node->prev->next) {
1358 node->prev->next = node->next;
1359 }
1360 if (node->next) {
1361 node->next->prev = node->prev;
1362 } else {
1363 /* unlinking the last node */
1364 if (node->parent) {
1365 iter = node->parent->child;
1366 } else {
1367 iter = node->prev;
1368 while (iter->prev != node) {
1369 iter = iter->prev;
1370 }
1371 }
1372 /* update the "last" pointer from the first node */
1373 iter->prev = node->prev;
1374 }
1375
1376 /* unlink from parent */
1377 if (node->parent) {
1378 if (node->parent->child == node) {
1379 /* the node is the first child */
1380 node->parent->child = node->next;
1381 }
1382
1383 lyd_unlink_hash(node);
1384
1385 /* check for keyless list and update its hash */
1386 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001387 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001388 lyd_hash(iter);
1389 }
1390 }
1391
1392 node->parent = NULL;
1393 }
1394
1395 node->next = NULL;
1396 node->prev = node;
1397}
1398
Michal Vasko90932a92020-02-12 14:33:03 +01001399LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001400lyd_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 +01001401 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 +01001402 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001403{
1404 LY_ERR ret;
1405 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001406 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001407 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001408
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001410
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001411 LY_ARRAY_FOR(mod->compiled->exts, u) {
1412 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1413 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001414 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001415 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001416 break;
1417 }
1418 }
1419 if (!ant) {
1420 /* attribute is not defined as a metadata annotation (RFC 7952) */
1421 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1422 mod->name, name_len, name);
1423 return LY_EINVAL;
1424 }
1425
Michal Vasko9f96a052020-03-10 09:41:45 +01001426 mt = calloc(1, sizeof *mt);
1427 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1428 mt->parent = parent;
1429 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001430 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 +01001431 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001433 return ret;
1434 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001436
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001437 /* insert as the last attribute */
1438 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001439 if (parent->meta) {
1440 for (last = parent->meta; last->next; last = last->next);
1441 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001442 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001443 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001444 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 } else if (*meta) {
1446 for (last = *meta; last->next; last = last->next);
1447 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001448 }
1449
1450 /* remove default flags from NP containers */
1451 while (parent && (parent->flags & LYD_DEFAULT)) {
1452 parent->flags &= ~LYD_DEFAULT;
1453 parent = (struct lyd_node *)parent->parent;
1454 }
1455
Michal Vasko9f96a052020-03-10 09:41:45 +01001456 if (meta) {
1457 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001458 }
1459 return ret;
1460}
1461
Michal Vasko52927e22020-03-16 17:26:14 +01001462LY_ERR
1463ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1464 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1465 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1466{
1467 struct ly_attr *at, *last;
1468 struct lyd_node_opaq *opaq;
1469
1470 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1471 assert(name && name_len);
1472 assert((prefix_len && ns) || (!prefix_len && !ns));
1473
1474 if (!value_len) {
1475 value = "";
1476 }
1477
1478 at = calloc(1, sizeof *at);
1479 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1480 at->parent = (struct lyd_node_opaq *)parent;
1481 at->name = lydict_insert(ctx, name, name_len);
1482 if (dynamic && *dynamic) {
1483 at->value = lydict_insert_zc(ctx, (char *)value);
1484 *dynamic = 0;
1485 } else {
1486 at->value = lydict_insert(ctx, value, value_len);
1487 }
1488
1489 at->format = format;
1490 at->val_prefs = val_prefs;
1491 if (ns) {
1492 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1493 at->prefix.ns = lydict_insert(ctx, ns, 0);
1494 }
1495
1496 /* insert as the last attribute */
1497 if (parent) {
1498 opaq = (struct lyd_node_opaq *)parent;
1499 if (opaq->attr) {
1500 for (last = opaq->attr; last->next; last = last->next);
1501 last->next = at;
1502 } else {
1503 opaq->attr = at;
1504 }
1505 } else if (*attr) {
1506 for (last = *attr; last->next; last = last->next);
1507 last->next = at;
1508 }
1509
1510 if (attr) {
1511 *attr = at;
1512 }
1513 return LY_SUCCESS;
1514}
1515
Radek Krejci084289f2019-07-09 17:35:30 +02001516API const struct lyd_node_term *
Michal Vaskof03ed032020-03-04 13:31:44 +01001517lyd_target(struct lyd_value_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001518{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001519 LY_ARRAY_SIZE_TYPE u, v;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001520 const struct lyd_node *start_sibling;
Michal Vaskoe444f752020-02-10 12:20:06 +01001521 struct lyd_node *node = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001522 uint64_t pos = 1;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001523 int match;
Radek Krejci084289f2019-07-09 17:35:30 +02001524
Michal Vaskof03ed032020-03-04 13:31:44 +01001525 LY_CHECK_ARG_RET(NULL, path, tree, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +02001526
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001527 /* first iteration */
1528 start_sibling = tree;
1529 u = 0;
1530 while (u < LY_ARRAY_SIZE(path)) {
1531 /* find next node instance */
Michal Vasko84c9f1b2020-05-14 12:08:11 +02001532 if (start_sibling && !start_sibling->prev->next && !(path[u].node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
1533 /* starting from the beginning using hashes */
1534 lyd_find_sibling_val(start_sibling, path[u].node, NULL, 0, &node);
1535 } else {
1536 /* next matching sibling */
1537 lyd_find_sibling_next2(start_sibling, path[u].node, NULL, 0, &node);
1538 }
Radek Krejci084289f2019-07-09 17:35:30 +02001539 if (!node) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001540 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001541 }
1542
1543 /* check predicate if any */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001544 match = 1;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001545 LY_ARRAY_FOR(path[u].predicates, v) {
1546 if (path[u].predicates[v].type == 0) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001547 assert(LY_ARRAY_SIZE(path[u].predicates) == 1);
Radek Krejci084289f2019-07-09 17:35:30 +02001548 /* position predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001549 if (pos != path[u].predicates[v].position) {
Radek Krejci084289f2019-07-09 17:35:30 +02001550 pos++;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001551 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001552 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001553 } else if (path[u].predicates[v].type == 1) {
Radek Krejci084289f2019-07-09 17:35:30 +02001554 /* key-predicate */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001555 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].predicates[v].key)->type;
Michal Vaskoe444f752020-02-10 12:20:06 +01001556 struct lyd_node *key;
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001557
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001558 lyd_find_sibling_val(lyd_node_children(node), path[u].predicates[v].key, NULL, 0, &key);
Radek Krejci084289f2019-07-09 17:35:30 +02001559 if (!key) {
1560 /* probably error and we shouldn't be here due to previous checks when creating path */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001561 match = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001562 } else if (type->plugin->compare(&((struct lyd_node_term *)key)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001563 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001564 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001565 } else if (path[u].predicates[v].type == 2) {
Radek Krejci084289f2019-07-09 17:35:30 +02001566 /* leaf-list-predicate */
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001567 struct lysc_type *type = ((struct lysc_node_leaf *)path[u].node)->type;
1568
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001569 if (type->plugin->compare(&((struct lyd_node_term *)node)->value, path[u].predicates[v].value)) {
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001570 match = 0;
Radek Krejci084289f2019-07-09 17:35:30 +02001571 }
1572 } else {
1573 LOGINT(NULL);
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001574 return NULL;
1575 }
1576
1577 if (!match) {
1578 /* useless to check more predicates */
1579 break;
Radek Krejci084289f2019-07-09 17:35:30 +02001580 }
1581 }
1582
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001583 if (!match) {
1584 /* try to match next sibling */
1585 start_sibling = node->next;
1586 } else {
1587 /* matched, move to the next path segment */
1588 ++u;
1589 start_sibling = lyd_node_children(node);
1590 pos = 1;
1591 }
Radek Krejci084289f2019-07-09 17:35:30 +02001592 }
1593
Michal Vasko6a12b4b2020-05-13 14:28:09 +02001594 return (const struct lyd_node_term *)node;
Radek Krejci084289f2019-07-09 17:35:30 +02001595}
1596
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001597API LY_ERR
1598lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1599{
1600 const struct lyd_node *iter1, *iter2;
1601 struct lyd_node_term *term1, *term2;
1602 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001603 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001604 struct lysc_type *type;
1605 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001606
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001607 if (!node1 || !node2) {
1608 if (node1 == node2) {
1609 return LY_SUCCESS;
1610 } else {
1611 return LY_ENOT;
1612 }
1613 }
1614
Michal Vasko52927e22020-03-16 17:26:14 +01001615 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001616 return LY_ENOT;
1617 }
1618
1619 if (node1->hash != node2->hash) {
1620 return LY_ENOT;
1621 }
Michal Vasko52927e22020-03-16 17:26:14 +01001622 /* 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 +02001623
Michal Vasko52927e22020-03-16 17:26:14 +01001624 if (!node1->schema) {
1625 opaq1 = (struct lyd_node_opaq *)node1;
1626 opaq2 = (struct lyd_node_opaq *)node2;
1627 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001628 return LY_ENOT;
1629 }
Michal Vasko52927e22020-03-16 17:26:14 +01001630 switch (opaq1->format) {
1631 case LYD_XML:
1632 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1633 return LY_ENOT;
1634 }
1635 break;
1636 case LYD_SCHEMA:
1637 /* not allowed */
1638 LOGINT(LYD_NODE_CTX(node1));
1639 return LY_EINT;
1640 }
1641 if (options & LYD_COMPARE_FULL_RECURSION) {
1642 iter1 = opaq1->child;
1643 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001644 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001645 }
1646 return LY_SUCCESS;
1647 } else {
1648 switch (node1->schema->nodetype) {
1649 case LYS_LEAF:
1650 case LYS_LEAFLIST:
1651 if (options & LYD_COMPARE_DEFAULTS) {
1652 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1653 return LY_ENOT;
1654 }
1655 }
1656
1657 term1 = (struct lyd_node_term*)node1;
1658 term2 = (struct lyd_node_term*)node2;
1659 type = ((struct lysc_node_leaf*)node1->schema)->type;
1660
1661 return type->plugin->compare(&term1->value, &term2->value);
1662 case LYS_CONTAINER:
1663 if (options & LYD_COMPARE_DEFAULTS) {
1664 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1665 return LY_ENOT;
1666 }
1667 }
1668 if (options & LYD_COMPARE_FULL_RECURSION) {
1669 iter1 = ((struct lyd_node_inner*)node1)->child;
1670 iter2 = ((struct lyd_node_inner*)node2)->child;
1671 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001672 }
1673 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001674 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001675 case LYS_ACTION:
1676 if (options & LYD_COMPARE_FULL_RECURSION) {
1677 /* TODO action/RPC
1678 goto all_children_compare;
1679 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001680 }
1681 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001682 case LYS_NOTIF:
1683 if (options & LYD_COMPARE_FULL_RECURSION) {
1684 /* TODO Notification
1685 goto all_children_compare;
1686 */
1687 }
1688 return LY_SUCCESS;
1689 case LYS_LIST:
1690 iter1 = ((struct lyd_node_inner*)node1)->child;
1691 iter2 = ((struct lyd_node_inner*)node2)->child;
1692
1693 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1694 /* lists with keys, their equivalence is based on their keys */
1695 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1696 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1697 key = key->next) {
1698 if (lyd_compare(iter1, iter2, options)) {
1699 return LY_ENOT;
1700 }
1701 iter1 = iter1->next;
1702 iter2 = iter2->next;
1703 }
1704 } else {
1705 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1706
1707 all_children_compare:
1708 if (!iter1 && !iter2) {
1709 /* no children, nothing to compare */
1710 return LY_SUCCESS;
1711 }
1712
1713 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1714 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1715 return LY_ENOT;
1716 }
1717 }
1718 if (iter1 || iter2) {
1719 return LY_ENOT;
1720 }
1721 }
1722 return LY_SUCCESS;
1723 case LYS_ANYXML:
1724 case LYS_ANYDATA:
1725 any1 = (struct lyd_node_any*)node1;
1726 any2 = (struct lyd_node_any*)node2;
1727
1728 if (any1->value_type != any2->value_type) {
1729 return LY_ENOT;
1730 }
1731 switch (any1->value_type) {
1732 case LYD_ANYDATA_DATATREE:
1733 iter1 = any1->value.tree;
1734 iter2 = any2->value.tree;
1735 goto all_children_compare;
1736 case LYD_ANYDATA_STRING:
1737 case LYD_ANYDATA_XML:
1738 case LYD_ANYDATA_JSON:
1739 len1 = strlen(any1->value.str);
1740 len2 = strlen(any2->value.str);
1741 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1742 return LY_ENOT;
1743 }
1744 return LY_SUCCESS;
1745 #if 0 /* TODO LYB format */
1746 case LYD_ANYDATA_LYB:
1747 int len1 = lyd_lyb_data_length(any1->value.mem);
1748 int len2 = lyd_lyb_data_length(any2->value.mem);
1749 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1750 return LY_ENOT;
1751 }
1752 return LY_SUCCESS;
1753 #endif
1754 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001755 }
1756 }
1757
Michal Vasko52927e22020-03-16 17:26:14 +01001758 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001759 return LY_EINT;
1760}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001761
1762/**
Michal Vasko52927e22020-03-16 17:26:14 +01001763 * @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 +02001764 *
1765 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001766 *
1767 * @param[in] node Original node to duplicate
1768 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1769 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1770 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1771 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1772 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001773 */
Michal Vasko52927e22020-03-16 17:26:14 +01001774static LY_ERR
1775lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1776 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001777{
Michal Vasko52927e22020-03-16 17:26:14 +01001778 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001779 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001780 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001781
Michal Vasko52927e22020-03-16 17:26:14 +01001782 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001783
Michal Vasko52927e22020-03-16 17:26:14 +01001784 if (!node->schema) {
1785 dup = calloc(1, sizeof(struct lyd_node_opaq));
1786 } else {
1787 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001788 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001789 case LYS_ACTION:
1790 case LYS_NOTIF:
1791 case LYS_CONTAINER:
1792 case LYS_LIST:
1793 dup = calloc(1, sizeof(struct lyd_node_inner));
1794 break;
1795 case LYS_LEAF:
1796 case LYS_LEAFLIST:
1797 dup = calloc(1, sizeof(struct lyd_node_term));
1798 break;
1799 case LYS_ANYDATA:
1800 case LYS_ANYXML:
1801 dup = calloc(1, sizeof(struct lyd_node_any));
1802 break;
1803 default:
1804 LOGINT(LYD_NODE_CTX(node));
1805 ret = LY_EINT;
1806 goto error;
1807 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001808 }
Michal Vasko52927e22020-03-16 17:26:14 +01001809 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001810
1811 /* TODO implement LYD_DUP_WITH_WHEN */
1812 dup->flags = node->flags;
1813 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001814 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001815
1816 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1817
1818 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001819 if (!dup->schema) {
1820 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1821 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1822 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001823
1824 if (options & LYD_DUP_RECURSIVE) {
1825 /* duplicate all the children */
1826 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001827 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1828 }
1829 }
1830 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1831 opaq->format = orig->format;
1832 if (orig->prefix.pref) {
1833 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1834 }
1835 if (orig->prefix.ns) {
1836 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1837 }
1838 if (orig->val_prefs) {
1839 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1840 LY_ARRAY_FOR(orig->val_prefs, u) {
1841 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1842 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1843 LY_ARRAY_INCREMENT(opaq->val_prefs);
1844 }
1845 }
1846 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1847 opaq->ctx = orig->ctx;
1848 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1849 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1850 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1851
1852 term->hash = orig->hash;
1853 term->value.realtype = orig->value.realtype;
1854 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1855 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1856 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1857 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1858 struct lyd_node *child;
1859
1860 if (options & LYD_DUP_RECURSIVE) {
1861 /* duplicate all the children */
1862 LY_LIST_FOR(orig->child, child) {
1863 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001864 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001865 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001866 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001867 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001868 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001869 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1870 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001871 if (!child) {
1872 /* possibly not keys are present in filtered tree */
1873 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001874 } else if (child->schema != key) {
1875 /* possibly not all keys are present in filtered tree,
1876 * but there can be also some non-key nodes */
1877 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001878 }
Michal Vasko52927e22020-03-16 17:26:14 +01001879 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001880 child = child->next;
1881 }
1882 }
1883 lyd_hash(dup);
1884 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001885 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1886 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001887
1888 any->hash = orig->hash;
1889 any->value_type = orig->value_type;
1890 switch (any->value_type) {
1891 case LYD_ANYDATA_DATATREE:
1892 if (orig->value.tree) {
1893 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02001894 if (!any->value.tree) {
1895 /* get the last error's error code recorded by lyd_dup */
1896 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
1897 ret = ei ? ei->prev->no : LY_EOTHER;
1898 goto error;
1899 }
1900 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0 ,error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001901 }
1902 break;
1903 case LYD_ANYDATA_STRING:
1904 case LYD_ANYDATA_XML:
1905 case LYD_ANYDATA_JSON:
1906 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001907 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001908 }
1909 break;
1910 }
1911 }
1912
Michal Vasko52927e22020-03-16 17:26:14 +01001913 /* insert */
1914 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001915 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001916
1917 if (dup_p) {
1918 *dup_p = dup;
1919 }
1920 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001921
1922error:
Michal Vasko52927e22020-03-16 17:26:14 +01001923 lyd_free_tree(dup);
1924 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001925}
1926
1927API struct lyd_node *
1928lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1929{
1930 struct ly_ctx *ctx;
1931 const struct lyd_node *orig; /* original node to be duplicated */
1932 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001933 struct lyd_node *top = NULL; /* the most higher created node */
1934 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1935 int keyless_parent_list = 0;
1936
1937 LY_CHECK_ARG_RET(NULL, node, NULL);
1938 ctx = node->schema->module->ctx;
1939
1940 if (options & LYD_DUP_WITH_PARENTS) {
1941 struct lyd_node_inner *orig_parent, *iter;
1942 int repeat = 1;
1943 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1944 if (parent && parent->schema == orig_parent->schema) {
1945 /* stop creating parents, connect what we have into the provided parent */
1946 iter = parent;
1947 repeat = 0;
1948 /* get know if there is a keyless list which we will have to rehash */
1949 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001950 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001951 keyless_parent_list = 1;
1952 break;
1953 }
1954 }
1955 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001956 iter = NULL;
1957 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1958 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001959 }
1960 if (!local_parent) {
1961 local_parent = iter;
1962 }
1963 if (iter->child) {
1964 /* 1) list - add after keys
1965 * 2) provided parent with some children */
1966 iter->child->prev->next = top;
1967 if (top) {
1968 top->prev = iter->child->prev;
1969 iter->child->prev = top;
1970 }
1971 } else {
1972 iter->child = top;
1973 if (iter->schema->nodetype == LYS_LIST) {
1974 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1975 keyless_parent_list = 1;
1976 }
1977 }
1978 if (top) {
1979 top->parent = iter;
1980 }
1981 top = (struct lyd_node*)iter;
1982 }
1983 if (repeat && parent) {
1984 /* given parent and created parents chain actually do not interconnect */
1985 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1986 goto error;
1987 }
1988 } else {
1989 local_parent = parent;
1990 }
1991
Radek Krejci22ebdba2019-07-25 13:59:43 +02001992 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001993 /* if there is no local parent, it will be inserted into first */
1994 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 +02001995 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1996 break;
1997 }
1998 }
1999 if (keyless_parent_list) {
2000 /* rehash */
2001 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002002 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002003 lyd_hash((struct lyd_node*)local_parent);
2004 }
2005 }
2006 }
2007 return first;
2008
2009error:
2010 if (top) {
2011 lyd_free_tree(top);
2012 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002013 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002014 }
2015 return NULL;
2016}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002017
2018static LY_ERR
2019lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2020{
Michal Vasko14654712020-02-06 08:35:21 +01002021 /* ending \0 */
2022 ++reqlen;
2023
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002024 if (reqlen > *buflen) {
2025 if (is_static) {
2026 return LY_EINCOMPLETE;
2027 }
2028
2029 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2030 if (!*buffer) {
2031 return LY_EMEM;
2032 }
2033
2034 *buflen = reqlen;
2035 }
2036
2037 return LY_SUCCESS;
2038}
2039
2040/**
2041 * @brief Append all list key predicates to path.
2042 *
2043 * @param[in] node Node with keys to print.
2044 * @param[in,out] buffer Buffer to print to.
2045 * @param[in,out] buflen Current buffer length.
2046 * @param[in,out] bufused Current number of characters used in @p buffer.
2047 * @param[in] is_static Whether buffer is static or can be reallocated.
2048 * @return LY_ERR
2049 */
2050static LY_ERR
2051lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2052{
2053 const struct lyd_node *key;
2054 int dynamic = 0;
2055 size_t len;
2056 const char *val;
2057 char quot;
2058 LY_ERR rc;
2059
Michal Vasko14654712020-02-06 08:35:21 +01002060 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002061 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2062 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2063 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2064 if (rc != LY_SUCCESS) {
2065 if (dynamic) {
2066 free((char *)val);
2067 }
2068 return rc;
2069 }
2070
2071 quot = '\'';
2072 if (strchr(val, '\'')) {
2073 quot = '"';
2074 }
2075 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2076
2077 if (dynamic) {
2078 free((char *)val);
2079 }
2080 }
2081
2082 return LY_SUCCESS;
2083}
2084
2085/**
2086 * @brief Append leaf-list value predicate to path.
2087 *
2088 * @param[in] node Node to print.
2089 * @param[in,out] buffer Buffer to print to.
2090 * @param[in,out] buflen Current buffer length.
2091 * @param[in,out] bufused Current number of characters used in @p buffer.
2092 * @param[in] is_static Whether buffer is static or can be reallocated.
2093 * @return LY_ERR
2094 */
2095static LY_ERR
2096lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2097{
2098 int dynamic = 0;
2099 size_t len;
2100 const char *val;
2101 char quot;
2102 LY_ERR rc;
2103
2104 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2105 len = 4 + strlen(val) + 2;
2106 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2107 if (rc != LY_SUCCESS) {
2108 goto cleanup;
2109 }
2110
2111 quot = '\'';
2112 if (strchr(val, '\'')) {
2113 quot = '"';
2114 }
2115 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2116
2117cleanup:
2118 if (dynamic) {
2119 free((char *)val);
2120 }
2121 return rc;
2122}
2123
2124/**
2125 * @brief Append node position (relative to its other instances) predicate to path.
2126 *
2127 * @param[in] node Node to print.
2128 * @param[in,out] buffer Buffer to print to.
2129 * @param[in,out] buflen Current buffer length.
2130 * @param[in,out] bufused Current number of characters used in @p buffer.
2131 * @param[in] is_static Whether buffer is static or can be reallocated.
2132 * @return LY_ERR
2133 */
2134static LY_ERR
2135lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2136{
2137 const struct lyd_node *first, *iter;
2138 size_t len;
2139 int pos;
2140 char *val = NULL;
2141 LY_ERR rc;
2142
2143 if (node->parent) {
2144 first = node->parent->child;
2145 } else {
2146 for (first = node; node->prev->next; node = node->prev);
2147 }
2148 pos = 1;
2149 for (iter = first; iter != node; iter = iter->next) {
2150 if (iter->schema == node->schema) {
2151 ++pos;
2152 }
2153 }
2154 if (asprintf(&val, "%d", pos) == -1) {
2155 return LY_EMEM;
2156 }
2157
2158 len = 1 + strlen(val) + 1;
2159 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2160 if (rc != LY_SUCCESS) {
2161 goto cleanup;
2162 }
2163
2164 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2165
2166cleanup:
2167 free(val);
2168 return rc;
2169}
2170
2171API char *
2172lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2173{
Michal Vasko14654712020-02-06 08:35:21 +01002174 int is_static = 0, i, depth;
2175 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002176 const struct lyd_node *iter;
2177 const struct lys_module *mod;
2178 LY_ERR rc;
2179
2180 LY_CHECK_ARG_RET(NULL, node, NULL);
2181 if (buffer) {
2182 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2183 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002184 } else {
2185 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002186 }
2187
2188 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002189 case LYD_PATH_LOG:
2190 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002191 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2192 ++depth;
2193 }
2194
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002195 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002196 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002197 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002198 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002199iter_print:
2200 /* print prefix and name */
2201 mod = NULL;
2202 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2203 mod = iter->schema->module;
2204 }
2205
2206 /* realloc string */
2207 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2208 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2209 if (rc != LY_SUCCESS) {
2210 break;
2211 }
2212
2213 /* print next node */
2214 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2215
2216 switch (iter->schema->nodetype) {
2217 case LYS_LIST:
2218 if (iter->schema->flags & LYS_KEYLESS) {
2219 /* print its position */
2220 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2221 } else {
2222 /* print all list keys in predicates */
2223 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2224 }
2225 break;
2226 case LYS_LEAFLIST:
2227 if (iter->schema->flags & LYS_CONFIG_W) {
2228 /* print leaf-list value */
2229 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2230 } else {
2231 /* print its position */
2232 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2233 }
2234 break;
2235 default:
2236 /* nothing to print more */
2237 rc = LY_SUCCESS;
2238 break;
2239 }
2240 if (rc != LY_SUCCESS) {
2241 break;
2242 }
2243
Michal Vasko14654712020-02-06 08:35:21 +01002244 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002245 }
2246 break;
2247 }
2248
2249 return buffer;
2250}
Michal Vaskoe444f752020-02-10 12:20:06 +01002251
Michal Vasko9b368d32020-02-14 13:53:31 +01002252LY_ERR
2253lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2254 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002255{
2256 LY_ERR rc;
2257 const struct lyd_node *node = NULL;
2258 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002259 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002260 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002261 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002262
Michal Vasko9b368d32020-02-14 13:53:31 +01002263 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002264
2265 if (!first) {
2266 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002267 if (match) {
2268 *match = NULL;
2269 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002270 return LY_ENOTFOUND;
2271 }
2272
Michal Vaskoe444f752020-02-10 12:20:06 +01002273 if (key_or_value && !val_len) {
2274 val_len = strlen(key_or_value);
2275 }
2276
2277 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002278 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002279 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 +01002280 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2281 /* parse keys into canonical values */
Michal Vaskod3678892020-05-21 10:06:58 +02002282 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, 1, &keys), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002283 }
2284
2285 /* find first matching value */
2286 LY_LIST_FOR(first, node) {
2287 if (node->schema != schema) {
2288 continue;
2289 }
2290
2291 if ((schema->nodetype == LYS_LIST) && keys.str) {
2292 /* compare all set keys */
2293 for (i = 0; i < keys.key_count; ++i) {
2294 /* find key */
2295 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2296 (struct lyd_node **)&term);
2297 if (rc == LY_ENOTFOUND) {
2298 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002299 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002300 }
2301 LY_CHECK_GOTO(rc, cleanup);
2302
2303 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002304 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002305 break;
2306 }
2307 }
2308
2309 if (i < keys.key_count) {
2310 /* not a match */
2311 continue;
2312 }
Michal Vasko90932a92020-02-12 14:33:03 +01002313 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002314 term = (struct lyd_node_term *)node;
2315
2316 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002317 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002318 /* not a match */
2319 continue;
2320 }
2321 }
2322
2323 /* all criteria passed */
2324 break;
2325 }
2326
2327 if (!node) {
2328 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002329 if (match) {
2330 *match = NULL;
2331 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002332 goto cleanup;
2333 }
2334
2335 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002336 if (match) {
2337 *match = (struct lyd_node *)node;
2338 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002339 rc = LY_SUCCESS;
2340
2341cleanup:
2342 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002343 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002344 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002345 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002346 return rc;
2347}
2348
2349API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002350lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2351 const char *key_or_value, size_t val_len, struct lyd_node **match)
2352{
2353 const struct lysc_node *schema;
2354
2355 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2356
2357 if (!first) {
2358 /* no data */
2359 *match = NULL;
2360 return LY_ENOTFOUND;
2361 }
2362
2363 /* find schema */
2364 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2365 if (!schema) {
2366 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2367 return LY_EINVAL;
2368 }
2369
2370 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2371}
2372
2373API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002374lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2375{
2376 struct lyd_node **match_p;
2377 struct lyd_node_inner *parent;
2378
Michal Vaskof03ed032020-03-04 13:31:44 +01002379 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002380
Michal Vasko62ed12d2020-05-21 10:08:25 +02002381 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2382 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002383 if (match) {
2384 *match = NULL;
2385 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002386 return LY_ENOTFOUND;
2387 }
2388
2389 /* find first sibling */
2390 if (siblings->parent) {
2391 siblings = siblings->parent->child;
2392 } else {
2393 while (siblings->prev->next) {
2394 siblings = siblings->prev;
2395 }
2396 }
2397
2398 parent = (struct lyd_node_inner *)siblings->parent;
2399 if (parent && parent->children_ht) {
2400 assert(target->hash);
2401
2402 /* find by hash */
2403 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2404 siblings = *match_p;
2405 } else {
2406 /* not found */
2407 siblings = NULL;
2408 }
2409 } else {
2410 /* no children hash table */
2411 for (; siblings; siblings = siblings->next) {
2412 if (!lyd_compare(siblings, target, 0)) {
2413 break;
2414 }
2415 }
2416 }
2417
2418 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002419 if (match) {
2420 *match = NULL;
2421 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002422 return LY_ENOTFOUND;
2423 }
2424
Michal Vasko9b368d32020-02-14 13:53:31 +01002425 if (match) {
2426 *match = (struct lyd_node *)siblings;
2427 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002428 return LY_SUCCESS;
2429}
2430
2431API LY_ERR
2432lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2433{
2434 struct lyd_node_inner *parent;
2435 struct lyd_node *match;
2436 struct lyd_node **match_p;
2437 struct ly_set *ret;
2438
2439 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2440
Michal Vasko62ed12d2020-05-21 10:08:25 +02002441 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2442 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01002443 return LY_ENOTFOUND;
2444 }
2445
2446 ret = ly_set_new();
2447 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2448
2449 /* find first sibling */
2450 if (siblings->parent) {
2451 siblings = siblings->parent->child;
2452 } else {
2453 while (siblings->prev->next) {
2454 siblings = siblings->prev;
2455 }
2456 }
2457
2458 parent = (struct lyd_node_inner *)siblings->parent;
2459 if (parent && parent->children_ht) {
2460 assert(target->hash);
2461
2462 /* find by hash */
2463 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2464 match = *match_p;
2465 } else {
2466 /* not found */
2467 match = NULL;
2468 }
2469 while (match) {
2470 /* add all found nodes into the return set */
2471 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2472 goto error;
2473 }
2474
2475 /* find next instance */
2476 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2477 match = NULL;
2478 } else {
2479 match = *match_p;
2480 }
2481 }
2482 } else {
2483 /* no children hash table */
2484 for (; siblings; siblings = siblings->next) {
2485 if (!lyd_compare(siblings, target, 0)) {
2486 /* a match */
2487 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2488 goto error;
2489 }
2490 }
2491 }
2492 }
2493
2494 if (!ret->count) {
2495 ly_set_free(ret, NULL);
2496 return LY_ENOTFOUND;
2497 }
2498
2499 *set = ret;
2500 return LY_SUCCESS;
2501
2502error:
2503 ly_set_free(ret, NULL);
2504 return LY_EMEM;
2505}
2506
Michal Vasko90932a92020-02-12 14:33:03 +01002507static int
2508lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2509{
2510 struct lysc_node *val1;
2511 struct lyd_node *val2;
2512
2513 val1 = *((struct lysc_node **)val1_p);
2514 val2 = *((struct lyd_node **)val2_p);
2515
2516 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2517
2518 if (val1 == val2->schema) {
2519 /* schema match is enough */
2520 return 1;
2521 } else {
2522 return 0;
2523 }
2524}
2525
2526static LY_ERR
2527lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2528{
2529 struct lyd_node **match_p;
2530 struct lyd_node_inner *parent;
2531 uint32_t hash;
2532 values_equal_cb ht_cb;
2533
2534 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2535
2536 /* find first sibling */
2537 if (siblings->parent) {
2538 siblings = siblings->parent->child;
2539 } else {
2540 while (siblings->prev->next) {
2541 siblings = siblings->prev;
2542 }
2543 }
2544
2545 parent = (struct lyd_node_inner *)siblings->parent;
2546 if (parent && parent->children_ht) {
2547 /* calculate our hash */
2548 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2549 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2550 hash = dict_hash_multi(hash, NULL, 0);
2551
2552 /* use special hash table function */
2553 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2554
2555 /* find by hash */
2556 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2557 siblings = *match_p;
2558 } else {
2559 /* not found */
2560 siblings = NULL;
2561 }
2562
2563 /* set the original hash table compare function back */
2564 lyht_set_cb(parent->children_ht, ht_cb);
2565 } else {
2566 /* no children hash table */
2567 for (; siblings; siblings = siblings->next) {
2568 if (siblings->schema == schema) {
2569 /* schema match is enough */
2570 break;
2571 }
2572 }
2573 }
2574
2575 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002576 if (match) {
2577 *match = NULL;
2578 }
Michal Vasko90932a92020-02-12 14:33:03 +01002579 return LY_ENOTFOUND;
2580 }
2581
Michal Vasko9b368d32020-02-14 13:53:31 +01002582 if (match) {
2583 *match = (struct lyd_node *)siblings;
2584 }
Michal Vasko90932a92020-02-12 14:33:03 +01002585 return LY_SUCCESS;
2586}
2587
Michal Vaskoe444f752020-02-10 12:20:06 +01002588API LY_ERR
2589lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2590 size_t val_len, struct lyd_node **match)
2591{
2592 LY_ERR rc;
2593 struct lyd_node *target = NULL;
2594
2595 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02002596 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002597 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2598 return LY_EINVAL;
2599 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2600 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2601 return LY_EINVAL;
2602 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2603 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2604 lys_nodetype2str(schema->nodetype), __func__);
2605 return LY_EINVAL;
2606 }
2607
Michal Vasko62ed12d2020-05-21 10:08:25 +02002608 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2609 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002610 if (match) {
2611 *match = NULL;
2612 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002613 return LY_ENOTFOUND;
2614 }
2615
Michal Vaskof03ed032020-03-04 13:31:44 +01002616 if (key_or_value && !val_len) {
2617 val_len = strlen(key_or_value);
2618 }
2619
Michal Vasko90932a92020-02-12 14:33:03 +01002620 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002621 switch (schema->nodetype) {
2622 case LYS_CONTAINER:
2623 case LYS_ANYXML:
2624 case LYS_ANYDATA:
2625 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01002626 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01002627 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002628 case LYS_LEAF:
Michal Vasko90932a92020-02-12 14:33:03 +01002629 /* find it based on schema only */
2630 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002631 break;
2632 case LYS_LEAFLIST:
2633 /* target used attributes: schema, hash, value */
Michal Vasko90932a92020-02-12 14:33:03 +01002634 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 +01002635 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002636 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002637 if (schema->nodetype == LYS_LIST) {
2638 /* target used attributes: schema, hash, child (all keys) */
Michal Vaskod3678892020-05-21 10:06:58 +02002639 LY_CHECK_RET(lyd_create_list(schema, key_or_value, val_len, LYD_JSON, 1, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002640 }
2641
2642 /* find it */
2643 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002644 break;
2645 default:
2646 /* unreachable */
2647 LOGINT(schema->module->ctx);
2648 return LY_EINT;
2649 }
2650
Michal Vaskoe444f752020-02-10 12:20:06 +01002651 lyd_free_tree(target);
2652 return rc;
2653}
Michal Vaskoccc02342020-05-21 10:09:21 +02002654
2655API LY_ERR
2656lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2657{
2658 LY_ERR ret = LY_SUCCESS;
2659 struct lyxp_set xp_set;
2660 struct lyxp_expr *exp;
2661 uint32_t i;
2662
2663 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2664
2665 memset(&xp_set, 0, sizeof xp_set);
2666
2667 /* compile expression */
2668 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath);
2669 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
2670
2671 /* evaluate expression */
2672 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
2673 LY_CHECK_GOTO(ret, cleanup);
2674
2675 /* allocate return set */
2676 *set = ly_set_new();
2677 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2678
2679 /* transform into ly_set */
2680 if (xp_set.type == LYXP_SET_NODE_SET) {
2681 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
2682 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2683 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2684 (*set)->size = xp_set.used;
2685
2686 for (i = 0; i < xp_set.used; ++i) {
2687 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2688 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
2689 }
2690 }
2691 }
2692
2693cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002694 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002695 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
2696 return ret;
2697}