blob: eceaebf22a6f1bf16f8c4213559053da9572dcd6 [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>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020039#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010040#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "plugins_types.h"
42#include "set.h"
43#include "tree.h"
44#include "tree_data_internal.h"
45#include "tree_schema.h"
46#include "tree_schema_internal.h"
47#include "xml.h"
48#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020049
Michal Vaskoe893ddd2020-06-23 13:35:20 +020050static LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused,
51 int is_static);
52
Radek Krejci084289f2019-07-09 17:35:30 +020053LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010054lyd_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 +010055 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020056{
Michal Vasko90932a92020-02-12 14:33:03 +010057 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020058 struct ly_err_item *err = NULL;
59 struct ly_ctx *ctx;
60 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020061 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010062 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020063 assert(node);
64
65 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020066
Radek Krejci73dead22019-07-11 16:46:16 +020067 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020068 if (!second) {
69 node->value.realtype = type;
70 }
Michal Vasko90932a92020-02-12 14:33:03 +010071 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020072 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010073 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020074 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020075 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020076 if (!node->parent && lysc_data_parent(node->schema)) {
77 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
78 } else {
79 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
80 }
Radek Krejci73dead22019-07-11 16:46:16 +020081 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020082 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010084 } else if (dynamic) {
85 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020086 }
87
88error:
89 return ret;
90}
91
Michal Vasko00cbf532020-06-15 13:58:47 +020092/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020093LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010094lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
95 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
96{
97 LY_ERR ret = LY_SUCCESS;
98 struct ly_err_item *err = NULL;
99 struct ly_ctx *ctx;
100 struct lysc_type *type;
101 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
102
103 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
104
105 ctx = schema->module->ctx;
106 type = ((struct lysc_node_leaf *)schema)->type;
107 val->realtype = type;
108 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
109 val, NULL, &err);
110 if (ret == LY_EINCOMPLETE) {
111 /* this is fine, we do not need it resolved */
112 ret = LY_SUCCESS;
113 } else if (ret && err) {
114 ly_err_print(err);
115 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
116 ly_err_free(err);
117 }
118 if (!ret && dynamic) {
119 *dynamic = 0;
120 }
121
122 return ret;
123}
124
Radek Krejci38d85362019-09-05 16:26:38 +0200125LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100126lyd_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 +0100127 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100128 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200129{
Michal Vasko90932a92020-02-12 14:33:03 +0100130 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200132 struct lyext_metadata *ant;
133 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100134 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200135
Michal Vasko9f96a052020-03-10 09:41:45 +0100136 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100137
Michal Vasko9f96a052020-03-10 09:41:45 +0100138 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200139
140 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100141 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200142 }
Michal Vasko90932a92020-02-12 14:33:03 +0100143 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100144 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100145 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200146 if (err) {
147 ly_err_print(err);
148 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
149 ly_err_free(err);
150 }
151 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100152 } else if (dynamic) {
153 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200154 }
155
156error:
157 return ret;
158}
159
Radek Krejci084289f2019-07-09 17:35:30 +0200160API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100161lys_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 +0200162 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
163{
164 LY_ERR rc = LY_SUCCESS;
165 struct ly_err_item *err = NULL;
166 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200167
168 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
169
170 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
171 LOGARG(ctx, node);
172 return LY_EINVAL;
173 }
174
175 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200176 /* just validate, no storing of enything */
177 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
178 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
179 if (rc == LY_EINCOMPLETE) {
180 /* actually success since we do not provide the context tree and call validation with
181 * LY_TYPE_OPTS_INCOMPLETE_DATA */
182 rc = LY_SUCCESS;
183 } else if (rc && err) {
184 if (ctx) {
185 /* log only in case the ctx was provided as input parameter */
186 ly_err_print(err);
187 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200188 }
Radek Krejci73dead22019-07-11 16:46:16 +0200189 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200190 }
191
192 return rc;
193}
194
195API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100196lyd_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 +0100197 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 +0200198{
199 LY_ERR rc;
200 struct ly_err_item *err = NULL;
201 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100202 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200203
204 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
205
206 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200207 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100208 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200209 NULL, NULL, &err);
210 if (rc == LY_EINCOMPLETE) {
211 return rc;
212 } else if (rc) {
213 if (err) {
214 if (ctx) {
215 ly_err_print(err);
216 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200217 }
Radek Krejci73dead22019-07-11 16:46:16 +0200218 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200219 }
Radek Krejci73dead22019-07-11 16:46:16 +0200220 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200221 }
222
223 return LY_SUCCESS;
224}
225
226API LY_ERR
227lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100228 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 +0200229{
230 LY_ERR ret = LY_SUCCESS, rc;
231 struct ly_err_item *err = NULL;
232 struct ly_ctx *ctx;
233 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200234 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100235 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200236
237 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
238
239 ctx = node->schema->module->ctx;
240 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200241 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 +0100242 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200243 if (rc == LY_EINCOMPLETE) {
244 ret = rc;
245 /* continue with comparing, just remember what to return if storing is ok */
246 } else if (rc) {
247 /* value to compare is invalid */
248 ret = LY_EINVAL;
249 if (err) {
250 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200251 }
Radek Krejci73dead22019-07-11 16:46:16 +0200252 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200253 }
254
255 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200256 if (type->plugin->compare(&node->value, &data)) {
257 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
258 ret = LY_EVALID;
259 }
Radek Krejci084289f2019-07-09 17:35:30 +0200260
261cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200262 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200263
264 return ret;
265}
266
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200267API const char *
268lyd_value2str(const struct lyd_node_term *node, int *dynamic)
269{
270 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
271
272 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
273}
274
275API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100276lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200277{
Michal Vasko9f96a052020-03-10 09:41:45 +0100278 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200279
Michal Vasko9f96a052020-03-10 09:41:45 +0100280 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200281}
282
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200283API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100284lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200285{
Radek Krejcie7b95092019-05-15 11:03:07 +0200286 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200287#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200288 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200289#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200290
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200291 LY_CHECK_ARG_RET(ctx, ctx, NULL);
292
Michal Vasko5b37a352020-03-06 13:38:33 +0100293 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
294 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
295 return NULL;
296 }
297
Michal Vaskoa3881362020-01-21 15:57:35 +0100298#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200299 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200300 /* first item in trees is mandatory - the RPC/action request */
301 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
302 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
303 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
304 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 return NULL;
306 }
307 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200308
Radek Krejcie7b95092019-05-15 11:03:07 +0200309 if (options & LYD_OPT_DATA_TEMPLATE) {
310 yang_data_name = va_arg(ap, const char *);
311 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200312#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200313
314 if (!format) {
315 /* TODO try to detect format from the content */
316 }
317
318 switch (format) {
319 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100320 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 break;
322#if 0
323 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200324 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200325 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200326#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 case LYD_LYB:
328 lyd_parse_lyb_data(ctx, data, options, &result, NULL);
329 break;
Michal Vasko52927e22020-03-16 17:26:14 +0100330 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200331 LOGINT(ctx);
332 break;
333 }
334
Radek Krejcie7b95092019-05-15 11:03:07 +0200335 return result;
336}
337
338API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100339lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200340{
341 struct lyd_node *result;
342 size_t length;
343 char *addr;
344
345 LY_CHECK_ARG_RET(ctx, ctx, NULL);
346 if (fd < 0) {
347 LOGARG(ctx, fd);
348 return NULL;
349 }
350
351 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100352 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200353 if (addr) {
354 ly_munmap(addr, length);
355 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200356
357 return result;
358}
359
360API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100361lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200362{
363 int fd;
364 struct lyd_node *result;
365 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200366
367 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
368
369 fd = open(path, O_RDONLY);
370 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
371
372 if (!format) {
373 /* unknown format - try to detect it from filename's suffix */
374 len = strlen(path);
375
376 /* ignore trailing whitespaces */
377 for (; len > 0 && isspace(path[len - 1]); len--);
378
379 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
380 format = LYD_XML;
381#if 0
382 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
383 format = LYD_JSON;
384 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
385 format = LYD_LYB;
386#endif
387 } /* else still unknown, try later to detect it from the content */
388 }
389
Michal Vaskoa3881362020-01-21 15:57:35 +0100390 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200391 close(fd);
392
393 return result;
394}
Radek Krejci084289f2019-07-09 17:35:30 +0200395
Michal Vasko90932a92020-02-12 14:33:03 +0100396LY_ERR
397lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
398 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
399{
400 LY_ERR ret;
401 struct lyd_node_term *term;
402
Michal Vasko9b368d32020-02-14 13:53:31 +0100403 assert(schema->nodetype & LYD_NODE_TERM);
404
Michal Vasko90932a92020-02-12 14:33:03 +0100405 term = calloc(1, sizeof *term);
406 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
407
408 term->schema = schema;
409 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100410 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100411
412 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
413 if (ret && (ret != LY_EINCOMPLETE)) {
414 free(term);
415 return ret;
416 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100417 lyd_hash((struct lyd_node *)term);
418
419 *node = (struct lyd_node *)term;
420 return ret;
421}
422
423LY_ERR
424lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
425{
426 LY_ERR ret;
427 struct lyd_node_term *term;
428 struct lysc_type *type;
429
430 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200431 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100432
433 term = calloc(1, sizeof *term);
434 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
435
436 term->schema = schema;
437 term->prev = (struct lyd_node *)term;
438 term->flags = LYD_NEW;
439
440 type = ((struct lysc_node_leaf *)schema)->type;
441 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
442 if (ret) {
443 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
444 free(term);
445 return ret;
446 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200447 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100448 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100449
450 *node = (struct lyd_node *)term;
451 return ret;
452}
453
454LY_ERR
455lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
456{
457 struct lyd_node_inner *in;
458
Michal Vasko9b368d32020-02-14 13:53:31 +0100459 assert(schema->nodetype & LYD_NODE_INNER);
460
Michal Vasko90932a92020-02-12 14:33:03 +0100461 in = calloc(1, sizeof *in);
462 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
463
464 in->schema = schema;
465 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100466 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100467
Michal Vasko9b368d32020-02-14 13:53:31 +0100468 /* do not hash list with keys, we need them for the hash */
469 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
470 lyd_hash((struct lyd_node *)in);
471 }
Michal Vasko90932a92020-02-12 14:33:03 +0100472
473 *node = (struct lyd_node *)in;
474 return LY_SUCCESS;
475}
476
Michal Vasko90932a92020-02-12 14:33:03 +0100477LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200478lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100479{
480 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100481 struct lyd_node *list = NULL, *key;
Michal Vasko004d3152020-06-11 19:59:22 +0200482 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100483
Michal Vasko004d3152020-06-11 19:59:22 +0200484 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100485
486 /* create list */
487 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
488
Michal Vasko90932a92020-02-12 14:33:03 +0100489 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200490 LY_ARRAY_FOR(predicates, u) {
491 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100492 lyd_insert_node(list, NULL, key);
493 }
494
Michal Vasko9b368d32020-02-14 13:53:31 +0100495 /* hash having all the keys */
496 lyd_hash(list);
497
Michal Vasko90932a92020-02-12 14:33:03 +0100498 /* success */
499 *node = list;
500 list = NULL;
501
502cleanup:
503 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200504 return ret;
505}
506
507static LY_ERR
508lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
509{
510 LY_ERR ret = LY_SUCCESS;
511 struct lyxp_expr *expr = NULL;
512 uint16_t exp_idx = 0;
513 enum ly_path_pred_type pred_type = 0;
514 struct ly_path_predicate *predicates = NULL;
515
516 /* parse keys */
517 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
518 LY_PATH_PRED_KEYS, &expr), cleanup);
519
520 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200521 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
522 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200523
524 /* create the list node */
525 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
526
527cleanup:
528 lyxp_expr_free(schema->module->ctx, expr);
529 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100530 return ret;
531}
532
533LY_ERR
534lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
535{
536 struct lyd_node_any *any;
537
Michal Vasko9b368d32020-02-14 13:53:31 +0100538 assert(schema->nodetype & LYD_NODE_ANY);
539
Michal Vasko90932a92020-02-12 14:33:03 +0100540 any = calloc(1, sizeof *any);
541 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
542
543 any->schema = schema;
544 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100545 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100546
547 any->value.xml = value;
548 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100549 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100550
551 *node = (struct lyd_node *)any;
552 return LY_SUCCESS;
553}
554
Michal Vasko52927e22020-03-16 17:26:14 +0100555LY_ERR
556lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
557 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
558 const char *ns, struct lyd_node **node)
559{
560 struct lyd_node_opaq *opaq;
561
562 assert(ctx && name && name_len && ns);
563
564 if (!value_len) {
565 value = "";
566 }
567
568 opaq = calloc(1, sizeof *opaq);
569 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
570
571 opaq->prev = (struct lyd_node *)opaq;
572
573 opaq->name = lydict_insert(ctx, name, name_len);
574 opaq->format = format;
575 if (pref_len) {
576 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
577 }
578 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
579 opaq->val_prefs = val_prefs;
580 if (dynamic && *dynamic) {
581 opaq->value = lydict_insert_zc(ctx, (char *)value);
582 *dynamic = 0;
583 } else {
584 opaq->value = lydict_insert(ctx, value, value_len);
585 }
586 opaq->ctx = ctx;
587
588 *node = (struct lyd_node *)opaq;
589 return LY_SUCCESS;
590}
591
Michal Vasko013a8182020-03-03 10:46:53 +0100592API struct lyd_node *
593lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
594{
595 struct lyd_node *ret = NULL;
596 const struct lysc_node *schema;
597 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
598
599 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
600
Michal Vaskof03ed032020-03-04 13:31:44 +0100601 if (!module) {
602 module = parent->schema->module;
603 }
604
Michal Vasko1bf09392020-03-27 12:38:10 +0100605 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 +0200606 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 +0100607
608 if (!lyd_create_inner(schema, &ret) && parent) {
609 lyd_insert_node(parent, NULL, ret);
610 }
611 return ret;
612}
613
614API struct lyd_node *
615lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
616{
617 struct lyd_node *ret = NULL, *key;
618 const struct lysc_node *schema, *key_s;
619 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
620 va_list ap;
621 const char *key_val;
622 LY_ERR rc = LY_SUCCESS;
623
624 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
625
Michal Vaskof03ed032020-03-04 13:31:44 +0100626 if (!module) {
627 module = parent->schema->module;
628 }
629
Michal Vasko013a8182020-03-03 10:46:53 +0100630 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
631 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
632
633 /* create list inner node */
634 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
635
636 va_start(ap, name);
637
638 /* create and insert all the keys */
639 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
640 key_val = va_arg(ap, const char *);
641
Michal Vaskof03ed032020-03-04 13:31:44 +0100642 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200643 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
644 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100645 lyd_insert_node(ret, NULL, key);
646 }
647
648 /* hash having all the keys */
649 lyd_hash(ret);
650
651 if (parent) {
652 lyd_insert_node(parent, NULL, ret);
653 }
654
655cleanup:
656 if (rc) {
657 lyd_free_tree(ret);
658 ret = NULL;
659 }
660 va_end(ap);
661 return ret;
662}
663
664API struct lyd_node *
665lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
666{
667 struct lyd_node *ret = NULL;
668 const struct lysc_node *schema;
669 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
670
671 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
672
Michal Vaskof03ed032020-03-04 13:31:44 +0100673 if (!module) {
674 module = parent->schema->module;
675 }
Michal Vasko004d3152020-06-11 19:59:22 +0200676 if (!keys) {
677 keys = "";
678 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100679
Michal Vasko004d3152020-06-11 19:59:22 +0200680 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100681 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
682 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
683
Michal Vasko004d3152020-06-11 19:59:22 +0200684 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
685 /* key-less list */
686 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
687 } else {
688 /* create the list node */
689 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret), NULL);
690 }
691
692 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100693 lyd_insert_node(parent, NULL, ret);
694 }
695 return ret;
696}
697
698API struct lyd_node *
699lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
700{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200701 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100702 struct lyd_node *ret = NULL;
703 const struct lysc_node *schema;
704 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
705
706 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
707
Michal Vaskof03ed032020-03-04 13:31:44 +0100708 if (!module) {
709 module = parent->schema->module;
710 }
711
Michal Vasko013a8182020-03-03 10:46:53 +0100712 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
713 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
714
Michal Vaskocbff3e92020-05-27 12:56:41 +0200715 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
716 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), NULL);
717
718 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100719 lyd_insert_node(parent, NULL, ret);
720 }
721 return ret;
722}
723
724API struct lyd_node *
725lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
726 LYD_ANYDATA_VALUETYPE value_type)
727{
728 struct lyd_node *ret = NULL;
729 const struct lysc_node *schema;
730 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
731
732 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
733
Michal Vaskof03ed032020-03-04 13:31:44 +0100734 if (!module) {
735 module = parent->schema->module;
736 }
737
Michal Vasko013a8182020-03-03 10:46:53 +0100738 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
739 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
740
741 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
742 lyd_insert_node(parent, NULL, ret);
743 }
744 return ret;
745}
746
Michal Vasko4490d312020-06-16 13:08:55 +0200747/**
748 * @brief Update node value.
749 *
750 * @param[in] node Node to update.
751 * @param[in] value New value to set.
752 * @param[in] value_type Type of @p value for any node.
753 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
754 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
755 * @return LY_ERR value.
756 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200757static LY_ERR
758lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
759 struct lyd_node **new_parent, struct lyd_node **new_node)
760{
761 LY_ERR ret = LY_SUCCESS;
762 struct lyd_node *new_any;
763
764 switch (node->schema->nodetype) {
765 case LYS_CONTAINER:
766 case LYS_NOTIF:
767 case LYS_RPC:
768 case LYS_ACTION:
769 case LYS_LIST:
770 case LYS_LEAFLIST:
771 /* if it exists, there is nothing to update */
772 *new_parent = NULL;
773 *new_node = NULL;
774 break;
775 case LYS_LEAF:
776 ret = lyd_change_term(node, value);
777 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
778 /* there was an actual change (at least of the default flag) */
779 *new_parent = node;
780 *new_node = node;
781 ret = LY_SUCCESS;
782 } else if (ret == LY_ENOT) {
783 /* no change */
784 *new_parent = NULL;
785 *new_node = NULL;
786 ret = LY_SUCCESS;
787 } /* else error */
788 break;
789 case LYS_ANYDATA:
790 case LYS_ANYXML:
791 /* create a new any node */
792 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
793
794 /* compare with the existing one */
795 if (lyd_compare(node, new_any, 0)) {
796 /* not equal, switch values (so that we can use generic node free) */
797 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
798 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
799 ((struct lyd_node_any *)node)->value.str = value;
800 ((struct lyd_node_any *)node)->value_type = value_type;
801
802 *new_parent = node;
803 *new_node = node;
804 } else {
805 /* they are equal */
806 *new_parent = NULL;
807 *new_node = NULL;
808 }
809 lyd_free_tree(new_any);
810 break;
811 default:
812 LOGINT(LYD_NODE_CTX(node));
813 ret = LY_EINT;
814 break;
815 }
816
817 return ret;
818}
819
Michal Vaskod86997b2020-05-26 15:19:54 +0200820API struct lyd_meta *
821lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
822{
823 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200824 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200825 const char *prefix, *tmp;
826 char *str;
827 size_t pref_len, name_len;
828
Michal Vasko00cbf532020-06-15 13:58:47 +0200829 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), NULL);
830
831 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200832
833 /* parse the name */
834 tmp = name;
835 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
836 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
837 return NULL;
838 }
839
840 /* find the module */
841 if (prefix) {
842 str = strndup(name, name_len);
843 module = ly_ctx_get_module_implemented(ctx, str);
844 free(str);
Michal Vasko004d3152020-06-11 19:59:22 +0200845 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +0200846 }
847
848 /* set value if none */
849 if (!val_str) {
850 val_str = "";
851 }
852
853 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
854 LYD_JSON, parent->schema);
855 return ret;
856}
857
Michal Vasko00cbf532020-06-15 13:58:47 +0200858API struct lyd_node *
859lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
860 const char *module_name)
861{
862 struct lyd_node *ret = NULL;
863
864 LY_CHECK_ARG_RET(ctx, parent || ctx, name, module_name, NULL);
865
866 if (!ctx) {
867 ctx = LYD_NODE_CTX(parent);
868 }
869 if (!value) {
870 value = "";
871 }
872
873 if (!lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0, module_name, &ret)
874 && parent) {
875 lyd_insert_node(parent, NULL, ret);
876 }
877 return ret;
878}
879
880API struct ly_attr *
881lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str)
882{
883 struct ly_attr *ret = NULL;
884 const struct ly_ctx *ctx;
885 const char *prefix, *tmp;
886 size_t pref_len, name_len;
887
888 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, NULL);
889
890 ctx = LYD_NODE_CTX(parent);
891
892 /* parse the name */
893 tmp = name;
894 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
895 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
896 return NULL;
897 }
898
899 /* set value if none */
900 if (!val_str) {
901 val_str = "";
902 }
903
904 ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL, prefix,
905 pref_len, module_name);
906 return ret;
907}
908
909API LY_ERR
910lyd_change_term(struct lyd_node *term, const char *val_str)
911{
912 LY_ERR ret = LY_SUCCESS;
913 struct lysc_type *type;
914 struct lyd_node_term *t;
915 struct lyd_node *parent;
916 struct lyd_value val = {0};
917 int dflt_change, val_change;
918
919 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
920
921 if (!val_str) {
922 val_str = "";
923 }
924 t = (struct lyd_node_term *)term;
925 type = ((struct lysc_node_leaf *)term->schema)->type;
926
927 /* parse the new value */
928 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
929 LYD_JSON), cleanup);
930
931 /* compare original and new value */
932 if (type->plugin->compare(&t->value, &val)) {
933 /* values differ, switch them */
934 type->plugin->free(LYD_NODE_CTX(term), &t->value);
935 t->value = val;
936 memset(&val, 0, sizeof val);
937 val_change = 1;
938 } else {
939 val_change = 0;
940 }
941
942 /* always clear the default flag */
943 if (term->flags & LYD_DEFAULT) {
944 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
945 parent->flags &= ~LYD_DEFAULT;
946 }
947 dflt_change = 1;
948 } else {
949 dflt_change = 0;
950 }
951
952 if (val_change || dflt_change) {
953 /* make the node non-validated */
954 term->flags &= LYD_NEW;
955 }
956
957 if (val_change) {
958 if (term->schema->nodetype == LYS_LEAFLIST) {
959 /* leaf-list needs to be hashed again and re-inserted into parent */
960 lyd_unlink_hash(term);
961 lyd_hash(term);
962 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
963 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
964 /* list needs to be updated if its key was changed */
965 assert(term->parent->schema->nodetype == LYS_LIST);
966 lyd_unlink_hash((struct lyd_node *)term->parent);
967 lyd_hash((struct lyd_node *)term->parent);
968 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
969 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
970 }
971
972 /* retrun value */
973 if (!val_change) {
974 if (dflt_change) {
975 /* only default flag change */
976 ret = LY_EEXIST;
977 } else {
978 /* no change */
979 ret = LY_ENOT;
980 }
981 } /* else value changed, LY_SUCCESS */
982
983cleanup:
984 type->plugin->free(LYD_NODE_CTX(term), &val);
985 return ret;
986}
987
988API struct lyd_node *
989lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options)
990{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200991 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200992
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200993 lyd_new_path2(parent, ctx, path, value, 0, options, &new_parent, NULL);
994 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200995}
996
997API struct lyd_node *
998lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
999 LYD_ANYDATA_VALUETYPE value_type, int options)
1000{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001001 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001002
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001003 lyd_new_path2(parent, ctx, path, value, value_type, options, &new_parent, NULL);
1004 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001005}
1006
1007API LY_ERR
1008lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1009 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1010{
1011 LY_ERR ret = LY_SUCCESS, r;
1012 struct lyxp_expr *exp = NULL;
1013 struct ly_path *p = NULL;
1014 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1015 const struct lysc_node *schema;
1016 LY_ARRAY_SIZE_TYPE path_idx = 0;
1017 struct ly_path_predicate *pred;
1018
1019 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1020
1021 if (!ctx) {
1022 ctx = LYD_NODE_CTX(parent);
1023 }
1024
1025 /* parse path */
1026 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1027 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1028
1029 /* compile path */
1030 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1031 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1032 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1033
1034 schema = p[LY_ARRAY_SIZE(p) - 1].node;
1035 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_SIZE(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
1036 && !(options & LYD_NEWOPT_OPAQ)) {
1037 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1038 lys_nodetype2str(schema->nodetype), schema->name);
1039 ret = LY_EINVAL;
1040 goto cleanup;
1041 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_SIZE(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
1042 /* parse leafref value into a predicate, if not defined in the path */
1043 p[LY_ARRAY_SIZE(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1044 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_SIZE(p) - 1].predicates, pred, ret, cleanup);
1045
1046 if (!value) {
1047 value = "";
1048 }
1049
1050 r = LY_SUCCESS;
1051 if (options & LYD_NEWOPT_OPAQ) {
1052 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1053 }
1054 if (!r) {
1055 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1056 NULL, LYD_JSON), cleanup);
1057 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1058 }
1059
1060 /* try to find any existing nodes in the path */
1061 if (parent) {
1062 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1063 if (ret == LY_SUCCESS) {
1064 /* the node exists, are we supposed to update it or is it just a default? */
1065 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1066 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1067 ret = LY_EEXIST;
1068 goto cleanup;
1069 }
1070
1071 /* update the existing node */
1072 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1073 goto cleanup;
1074 } else if (ret == LY_EINCOMPLETE) {
1075 /* some nodes were found, adjust the iterator to the next segment */
1076 ++path_idx;
1077 } else if (ret == LY_ENOTFOUND) {
1078 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1079 if (lysc_data_parent(p[LY_ARRAY_SIZE(p) - 1].node)) {
1080 node = parent;
1081 }
1082 } else {
1083 /* error */
1084 goto cleanup;
1085 }
1086 }
1087
1088 /* create all the non-existing nodes in a loop */
1089 for (; path_idx < LY_ARRAY_SIZE(p); ++path_idx) {
1090 cur_parent = node;
1091 schema = p[path_idx].node;
1092
1093 switch (schema->nodetype) {
1094 case LYS_LIST:
1095 if (!(schema->flags & LYS_KEYLESS)) {
1096 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1097 /* creating opaque list without keys */
1098 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1099 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1100 } else {
1101 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1102 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1103 }
1104 break;
1105 }
1106 /* fallthrough */
1107 case LYS_CONTAINER:
1108 case LYS_NOTIF:
1109 case LYS_RPC:
1110 case LYS_ACTION:
1111 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1112 break;
1113 case LYS_LEAFLIST:
1114 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1115 /* creating opaque leaf-list without value */
1116 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1117 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1118 } else {
1119 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1120 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1121 }
1122 break;
1123 case LYS_LEAF:
1124 /* make there is some value */
1125 if (!value) {
1126 value = "";
1127 }
1128
1129 r = LY_SUCCESS;
1130 if (options & LYD_NEWOPT_OPAQ) {
1131 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1132 }
1133 if (!r) {
1134 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1135 LYD_JSON, &node), cleanup);
1136 } else {
1137 /* creating opaque leaf without value */
1138 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1139 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1140 }
1141 break;
1142 case LYS_ANYDATA:
1143 case LYS_ANYXML:
1144 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1145 break;
1146 default:
1147 LOGINT(ctx);
1148 ret = LY_EINT;
1149 goto cleanup;
1150 }
1151
1152 if (cur_parent) {
1153 /* connect to the parent */
1154 lyd_insert_node(cur_parent, NULL, node);
1155 } else if (parent) {
1156 /* connect to top-level siblings */
1157 lyd_insert_node(NULL, &parent, node);
1158 }
1159
1160 /* update remembered nodes */
1161 if (!nparent) {
1162 nparent = node;
1163 }
1164 nnode = node;
1165 }
1166
1167cleanup:
1168 lyxp_expr_free(ctx, exp);
1169 ly_path_free(ctx, p);
1170 if (!ret) {
1171 /* set out params only on success */
1172 if (new_parent) {
1173 *new_parent = nparent;
1174 }
1175 if (new_node) {
1176 *new_node = nnode;
1177 }
1178 }
1179 return ret;
1180}
1181
Michal Vasko90932a92020-02-12 14:33:03 +01001182struct lyd_node *
1183lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
1184{
1185 const struct lysc_node *prev_key;
1186 struct lyd_node *match = NULL;
1187
1188 if (!first_sibling) {
1189 return NULL;
1190 }
1191
1192 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
1193 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
1194 }
1195
1196 return match;
1197}
1198
1199/**
1200 * @brief Insert node after a sibling.
1201 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001202 * Handles inserting into NP containers and key-less lists.
1203 *
Michal Vasko90932a92020-02-12 14:33:03 +01001204 * @param[in] sibling Sibling to insert after.
1205 * @param[in] node Node to insert.
1206 */
1207static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001208lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001209{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001210 struct lyd_node_inner *par;
1211
Michal Vasko90932a92020-02-12 14:33:03 +01001212 assert(!node->next && (node->prev == node));
1213
1214 node->next = sibling->next;
1215 node->prev = sibling;
1216 sibling->next = node;
1217 if (node->next) {
1218 /* sibling had a succeeding node */
1219 node->next->prev = node;
1220 } else {
1221 /* sibling was last, find first sibling and change its prev */
1222 if (sibling->parent) {
1223 sibling = sibling->parent->child;
1224 } else {
1225 for (; sibling->prev->next != node; sibling = sibling->prev);
1226 }
1227 sibling->prev = node;
1228 }
1229 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001230
Michal Vasko9f96a052020-03-10 09:41:45 +01001231 for (par = node->parent; par; par = par->parent) {
1232 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1233 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001234 par->flags &= ~LYD_DEFAULT;
1235 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001236 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1237 /* rehash key-less list */
1238 lyd_hash((struct lyd_node *)par);
1239 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001240 }
1241
1242 /* insert into hash table */
1243 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001244}
1245
1246/**
1247 * @brief Insert node before a sibling.
1248 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001249 * Handles inserting into NP containers and key-less lists.
1250 *
Michal Vasko90932a92020-02-12 14:33:03 +01001251 * @param[in] sibling Sibling to insert before.
1252 * @param[in] node Node to insert.
1253 */
1254static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001255lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001256{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001257 struct lyd_node_inner *par;
1258
Michal Vasko90932a92020-02-12 14:33:03 +01001259 assert(!node->next && (node->prev == node));
1260
1261 node->next = sibling;
1262 /* covers situation of sibling being first */
1263 node->prev = sibling->prev;
1264 sibling->prev = node;
1265 if (node->prev->next) {
1266 /* sibling had a preceding node */
1267 node->prev->next = node;
1268 } else if (sibling->parent) {
1269 /* sibling was first and we must also change parent child pointer */
1270 sibling->parent->child = node;
1271 }
1272 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001273
Michal Vasko9f96a052020-03-10 09:41:45 +01001274 for (par = node->parent; par; par = par->parent) {
1275 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1276 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001277 par->flags &= ~LYD_DEFAULT;
1278 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001279 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1280 /* rehash key-less list */
1281 lyd_hash((struct lyd_node *)par);
1282 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001283 }
1284
1285 /* insert into hash table */
1286 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001287}
1288
1289/**
1290 * @brief Insert node as the last child of a parent.
1291 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001292 * Handles inserting into NP containers and key-less lists.
1293 *
Michal Vasko90932a92020-02-12 14:33:03 +01001294 * @param[in] parent Parent to insert into.
1295 * @param[in] node Node to insert.
1296 */
1297static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001298lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001299{
1300 struct lyd_node_inner *par;
1301
Michal Vasko0249f7c2020-03-05 16:36:40 +01001302 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001303 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001304
1305 par = (struct lyd_node_inner *)parent;
1306
1307 if (!par->child) {
1308 par->child = node;
1309 } else {
1310 node->prev = par->child->prev;
1311 par->child->prev->next = node;
1312 par->child->prev = node;
1313 }
1314 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001315
Michal Vasko9f96a052020-03-10 09:41:45 +01001316 for (; par; par = par->parent) {
1317 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1318 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001319 par->flags &= ~LYD_DEFAULT;
1320 }
Michal Vasko52927e22020-03-16 17:26:14 +01001321 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001322 /* rehash key-less list */
1323 lyd_hash((struct lyd_node *)par);
1324 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001325 }
1326
1327 /* insert into hash table */
1328 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001329}
1330
1331void
Michal Vasko9b368d32020-02-14 13:53:31 +01001332lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001333{
Michal Vasko9b368d32020-02-14 13:53:31 +01001334 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001335 const struct lysc_node *skey = NULL;
1336 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001337
Michal Vasko52927e22020-03-16 17:26:14 +01001338 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001339
1340 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1341 parent = (struct lyd_node *)(*first_sibling)->parent;
1342 }
Michal Vasko90932a92020-02-12 14:33:03 +01001343
1344 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001345 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001346 /* it is key and we need to insert it at the correct place */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001347 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent, 0), node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +01001348 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001349 lyd_insert_after_node(anchor, node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001350 } else if (lyd_node_children(parent, 0)) {
1351 lyd_insert_before_node(lyd_node_children(parent, 0), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001352 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001353 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001354 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001355
1356 /* hash list if all its keys were added */
1357 assert(parent->schema->nodetype == LYS_LIST);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001358 anchor = lyd_node_children(parent, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +01001359 has_keys = 1;
1360 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1361 if (!anchor || (anchor->schema != skey)) {
1362 /* key missing */
1363 has_keys = 0;
1364 break;
1365 }
1366
1367 anchor = anchor->next;
1368 }
1369 if (has_keys) {
1370 lyd_hash(parent);
1371 }
1372
Michal Vasko90932a92020-02-12 14:33:03 +01001373 } else {
1374 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001375 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001376 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001377 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001378 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001379 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001380 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001381 anchor = anchor->prev;
1382 }
1383
Michal Vaskoc193ce92020-03-06 11:04:48 +01001384 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001385 /* insert after last sibling from this module */
1386 lyd_insert_after_node(anchor, node);
1387 } else {
1388 /* no data from this module, insert at the last position */
1389 lyd_insert_after_node((*first_sibling)->prev, node);
1390 }
Michal Vasko90932a92020-02-12 14:33:03 +01001391 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001392 /* the only sibling */
1393 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001394 }
Michal Vasko90932a92020-02-12 14:33:03 +01001395}
1396
Michal Vaskof03ed032020-03-04 13:31:44 +01001397static LY_ERR
1398lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1399{
1400 const struct lysc_node *par2;
1401
1402 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001403 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001404
1405 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001406 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001407
1408 if (parent) {
1409 /* inner node */
1410 if (par2 != parent) {
1411 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1412 return LY_EINVAL;
1413 }
1414 } else {
1415 /* top-level node */
1416 if (par2) {
1417 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1418 return LY_EINVAL;
1419 }
1420 }
1421
1422 return LY_SUCCESS;
1423}
1424
1425API LY_ERR
1426lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1427{
1428 struct lyd_node *iter;
1429
Michal Vasko654bc852020-06-23 13:28:06 +02001430 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001431
1432 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1433
1434 if (node->schema->flags & LYS_KEY) {
1435 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1436 return LY_EINVAL;
1437 }
1438
1439 if (node->parent || node->prev->next) {
1440 lyd_unlink_tree(node);
1441 }
1442
1443 while (node) {
1444 iter = node->next;
1445 lyd_unlink_tree(node);
1446 lyd_insert_node(parent, NULL, node);
1447 node = iter;
1448 }
1449 return LY_SUCCESS;
1450}
1451
1452API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001453lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1454{
1455 struct lyd_node *iter;
1456
1457 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1458
Michal Vasko62ed12d2020-05-21 10:08:25 +02001459 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001460
1461 if (node->schema->flags & LYS_KEY) {
1462 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1463 return LY_EINVAL;
1464 }
1465
1466 if (node->parent || node->prev->next) {
1467 lyd_unlink_tree(node);
1468 }
1469
1470 while (node) {
1471 iter = node->next;
1472 lyd_unlink_tree(node);
1473 lyd_insert_node(NULL, &sibling, node);
1474 node = iter;
1475 }
1476 return LY_SUCCESS;
1477}
1478
Michal Vasko0249f7c2020-03-05 16:36:40 +01001479static LY_ERR
1480lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1481{
1482 if (sibling->parent) {
1483 /* nested, we do not care for the order */
1484 return LY_SUCCESS;
1485 }
1486
1487 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001488 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1489 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001490 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 +01001491 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001492 return LY_EINVAL;
1493 }
1494
Michal Vaskoc193ce92020-03-06 11:04:48 +01001495 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1496 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001497 /* inserting before/after its module data */
1498 return LY_SUCCESS;
1499 }
1500 }
1501
1502 /* find first sibling */
1503 while (sibling->prev->next) {
1504 sibling = sibling->prev;
1505 }
1506
1507 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001508 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001509 /* inserting before its module data */
1510 return LY_SUCCESS;
1511 }
1512 }
1513
1514 /* check there are no data of this module */
1515 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001516 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001517 /* some data of this module found */
1518 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001519 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001520 return LY_EINVAL;
1521 }
1522 }
1523
1524 return LY_SUCCESS;
1525}
1526
Michal Vaskob1b5c262020-03-05 14:29:47 +01001527API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001528lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1529{
1530 struct lyd_node *iter;
1531
1532 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1533
Michal Vasko62ed12d2020-05-21 10:08:25 +02001534 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001535
1536 if (node->schema->flags & LYS_KEY) {
1537 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1538 return LY_EINVAL;
1539 } else if (sibling->schema->flags & LYS_KEY) {
1540 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1541 return LY_EINVAL;
1542 }
1543
Michal Vasko0249f7c2020-03-05 16:36:40 +01001544 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1545
Michal Vaskof03ed032020-03-04 13:31:44 +01001546 if (node->parent || node->prev->next) {
1547 lyd_unlink_tree(node);
1548 }
1549
1550 /* insert in reverse order to get the original order */
1551 node = node->prev;
1552 while (node) {
1553 iter = node->prev;
1554 lyd_unlink_tree(node);
1555
1556 lyd_insert_before_node(sibling, node);
1557 /* move the anchor accordingly */
1558 sibling = node;
1559
1560 node = (iter == node) ? NULL : iter;
1561 }
1562 return LY_SUCCESS;
1563}
1564
1565API LY_ERR
1566lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1567{
1568 struct lyd_node *iter;
1569
1570 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1571
Michal Vasko62ed12d2020-05-21 10:08:25 +02001572 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001573
1574 if (node->schema->flags & LYS_KEY) {
1575 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1576 return LY_EINVAL;
1577 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1578 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1579 return LY_EINVAL;
1580 }
1581
Michal Vasko0249f7c2020-03-05 16:36:40 +01001582 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1583
Michal Vaskof03ed032020-03-04 13:31:44 +01001584 if (node->parent || node->prev->next) {
1585 lyd_unlink_tree(node);
1586 }
1587
1588 while (node) {
1589 iter = node->next;
1590 lyd_unlink_tree(node);
1591
1592 lyd_insert_after_node(sibling, node);
1593 /* move the anchor accordingly */
1594 sibling = node;
1595
1596 node = iter;
1597 }
1598 return LY_SUCCESS;
1599}
1600
1601API void
1602lyd_unlink_tree(struct lyd_node *node)
1603{
1604 struct lyd_node *iter;
1605
1606 if (!node) {
1607 return;
1608 }
1609
1610 /* unlink from siblings */
1611 if (node->prev->next) {
1612 node->prev->next = node->next;
1613 }
1614 if (node->next) {
1615 node->next->prev = node->prev;
1616 } else {
1617 /* unlinking the last node */
1618 if (node->parent) {
1619 iter = node->parent->child;
1620 } else {
1621 iter = node->prev;
1622 while (iter->prev != node) {
1623 iter = iter->prev;
1624 }
1625 }
1626 /* update the "last" pointer from the first node */
1627 iter->prev = node->prev;
1628 }
1629
1630 /* unlink from parent */
1631 if (node->parent) {
1632 if (node->parent->child == node) {
1633 /* the node is the first child */
1634 node->parent->child = node->next;
1635 }
1636
1637 lyd_unlink_hash(node);
1638
1639 /* check for keyless list and update its hash */
1640 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001641 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001642 lyd_hash(iter);
1643 }
1644 }
1645
1646 node->parent = NULL;
1647 }
1648
1649 node->next = NULL;
1650 node->prev = node;
1651}
1652
Michal Vasko90932a92020-02-12 14:33:03 +01001653LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001654lyd_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 +01001655 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 +01001656 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001657{
1658 LY_ERR ret;
1659 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001660 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001661 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001662
Michal Vasko9f96a052020-03-10 09:41:45 +01001663 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001664
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001665 LY_ARRAY_FOR(mod->compiled->exts, u) {
1666 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1667 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001668 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001669 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001670 break;
1671 }
1672 }
1673 if (!ant) {
1674 /* attribute is not defined as a metadata annotation (RFC 7952) */
1675 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1676 mod->name, name_len, name);
1677 return LY_EINVAL;
1678 }
1679
Michal Vasko9f96a052020-03-10 09:41:45 +01001680 mt = calloc(1, sizeof *mt);
1681 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1682 mt->parent = parent;
1683 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001684 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 +01001685 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001686 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001687 return ret;
1688 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001689 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001690
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001691 /* insert as the last attribute */
1692 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001693 if (parent->meta) {
1694 for (last = parent->meta; last->next; last = last->next);
1695 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001696 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001697 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001698 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001699 } else if (*meta) {
1700 for (last = *meta; last->next; last = last->next);
1701 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001702 }
1703
1704 /* remove default flags from NP containers */
1705 while (parent && (parent->flags & LYD_DEFAULT)) {
1706 parent->flags &= ~LYD_DEFAULT;
1707 parent = (struct lyd_node *)parent->parent;
1708 }
1709
Michal Vasko9f96a052020-03-10 09:41:45 +01001710 if (meta) {
1711 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001712 }
1713 return ret;
1714}
1715
Michal Vasko52927e22020-03-16 17:26:14 +01001716LY_ERR
1717ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1718 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1719 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1720{
1721 struct ly_attr *at, *last;
1722 struct lyd_node_opaq *opaq;
1723
1724 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1725 assert(name && name_len);
1726 assert((prefix_len && ns) || (!prefix_len && !ns));
1727
1728 if (!value_len) {
1729 value = "";
1730 }
1731
1732 at = calloc(1, sizeof *at);
1733 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1734 at->parent = (struct lyd_node_opaq *)parent;
1735 at->name = lydict_insert(ctx, name, name_len);
1736 if (dynamic && *dynamic) {
1737 at->value = lydict_insert_zc(ctx, (char *)value);
1738 *dynamic = 0;
1739 } else {
1740 at->value = lydict_insert(ctx, value, value_len);
1741 }
1742
1743 at->format = format;
1744 at->val_prefs = val_prefs;
1745 if (ns) {
1746 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1747 at->prefix.ns = lydict_insert(ctx, ns, 0);
1748 }
1749
1750 /* insert as the last attribute */
1751 if (parent) {
1752 opaq = (struct lyd_node_opaq *)parent;
1753 if (opaq->attr) {
1754 for (last = opaq->attr; last->next; last = last->next);
1755 last->next = at;
1756 } else {
1757 opaq->attr = at;
1758 }
1759 } else if (*attr) {
1760 for (last = *attr; last->next; last = last->next);
1761 last->next = at;
1762 }
1763
1764 if (attr) {
1765 *attr = at;
1766 }
1767 return LY_SUCCESS;
1768}
1769
Radek Krejci084289f2019-07-09 17:35:30 +02001770API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001771lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001772{
Michal Vasko004d3152020-06-11 19:59:22 +02001773 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001774
Michal Vasko004d3152020-06-11 19:59:22 +02001775 if (ly_path_eval(path, tree, &target)) {
1776 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001777 }
1778
Michal Vasko004d3152020-06-11 19:59:22 +02001779 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001780}
1781
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001782API LY_ERR
1783lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1784{
1785 const struct lyd_node *iter1, *iter2;
1786 struct lyd_node_term *term1, *term2;
1787 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001788 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001789 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001790
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001791 if (!node1 || !node2) {
1792 if (node1 == node2) {
1793 return LY_SUCCESS;
1794 } else {
1795 return LY_ENOT;
1796 }
1797 }
1798
Michal Vasko52927e22020-03-16 17:26:14 +01001799 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001800 return LY_ENOT;
1801 }
1802
1803 if (node1->hash != node2->hash) {
1804 return LY_ENOT;
1805 }
Michal Vasko52927e22020-03-16 17:26:14 +01001806 /* 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 +02001807
Michal Vasko52927e22020-03-16 17:26:14 +01001808 if (!node1->schema) {
1809 opaq1 = (struct lyd_node_opaq *)node1;
1810 opaq2 = (struct lyd_node_opaq *)node2;
1811 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001812 return LY_ENOT;
1813 }
Michal Vasko52927e22020-03-16 17:26:14 +01001814 switch (opaq1->format) {
1815 case LYD_XML:
1816 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1817 return LY_ENOT;
1818 }
1819 break;
1820 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001821 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001822 /* not allowed */
1823 LOGINT(LYD_NODE_CTX(node1));
1824 return LY_EINT;
1825 }
1826 if (options & LYD_COMPARE_FULL_RECURSION) {
1827 iter1 = opaq1->child;
1828 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001829 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001830 }
1831 return LY_SUCCESS;
1832 } else {
1833 switch (node1->schema->nodetype) {
1834 case LYS_LEAF:
1835 case LYS_LEAFLIST:
1836 if (options & LYD_COMPARE_DEFAULTS) {
1837 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1838 return LY_ENOT;
1839 }
1840 }
1841
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001842 term1 = (struct lyd_node_term *)node1;
1843 term2 = (struct lyd_node_term *)node2;
1844 if (term1->value.realtype != term2->value.realtype) {
1845 return LY_ENOT;
1846 }
Michal Vasko52927e22020-03-16 17:26:14 +01001847
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001848 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001849 case LYS_CONTAINER:
1850 if (options & LYD_COMPARE_DEFAULTS) {
1851 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1852 return LY_ENOT;
1853 }
1854 }
1855 if (options & LYD_COMPARE_FULL_RECURSION) {
1856 iter1 = ((struct lyd_node_inner*)node1)->child;
1857 iter2 = ((struct lyd_node_inner*)node2)->child;
1858 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001859 }
1860 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001861 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001862 case LYS_ACTION:
1863 if (options & LYD_COMPARE_FULL_RECURSION) {
1864 /* TODO action/RPC
1865 goto all_children_compare;
1866 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001867 }
1868 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001869 case LYS_NOTIF:
1870 if (options & LYD_COMPARE_FULL_RECURSION) {
1871 /* TODO Notification
1872 goto all_children_compare;
1873 */
1874 }
1875 return LY_SUCCESS;
1876 case LYS_LIST:
1877 iter1 = ((struct lyd_node_inner*)node1)->child;
1878 iter2 = ((struct lyd_node_inner*)node2)->child;
1879
1880 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1881 /* lists with keys, their equivalence is based on their keys */
1882 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001883 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001884 key = key->next) {
1885 if (lyd_compare(iter1, iter2, options)) {
1886 return LY_ENOT;
1887 }
1888 iter1 = iter1->next;
1889 iter2 = iter2->next;
1890 }
1891 } else {
1892 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1893
1894 all_children_compare:
1895 if (!iter1 && !iter2) {
1896 /* no children, nothing to compare */
1897 return LY_SUCCESS;
1898 }
1899
1900 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1901 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1902 return LY_ENOT;
1903 }
1904 }
1905 if (iter1 || iter2) {
1906 return LY_ENOT;
1907 }
1908 }
1909 return LY_SUCCESS;
1910 case LYS_ANYXML:
1911 case LYS_ANYDATA:
1912 any1 = (struct lyd_node_any*)node1;
1913 any2 = (struct lyd_node_any*)node2;
1914
1915 if (any1->value_type != any2->value_type) {
1916 return LY_ENOT;
1917 }
1918 switch (any1->value_type) {
1919 case LYD_ANYDATA_DATATREE:
1920 iter1 = any1->value.tree;
1921 iter2 = any2->value.tree;
1922 goto all_children_compare;
1923 case LYD_ANYDATA_STRING:
1924 case LYD_ANYDATA_XML:
1925 case LYD_ANYDATA_JSON:
1926 len1 = strlen(any1->value.str);
1927 len2 = strlen(any2->value.str);
1928 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1929 return LY_ENOT;
1930 }
1931 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001932 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001933 len1 = lyd_lyb_data_length(any1->value.mem);
1934 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01001935 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1936 return LY_ENOT;
1937 }
1938 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001939 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001940 }
1941 }
1942
Michal Vasko52927e22020-03-16 17:26:14 +01001943 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001944 return LY_EINT;
1945}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001946
Michal Vasko21725742020-06-29 11:49:25 +02001947API LY_ERR
1948lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1949{
1950 if (!meta1 || !meta2) {
1951 if (meta1 == meta2) {
1952 return LY_SUCCESS;
1953 } else {
1954 return LY_ENOT;
1955 }
1956 }
1957
1958 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
1959 return LY_ENOT;
1960 }
1961
1962 if (meta1->value.realtype != meta2->value.realtype) {
1963 return LY_ENOT;
1964 }
1965
1966 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1967}
1968
Radek Krejci22ebdba2019-07-25 13:59:43 +02001969/**
Michal Vasko52927e22020-03-16 17:26:14 +01001970 * @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 +02001971 *
1972 * 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 +02001973 *
1974 * @param[in] node Original node to duplicate
1975 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1976 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1977 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1978 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1979 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001980 */
Michal Vasko52927e22020-03-16 17:26:14 +01001981static LY_ERR
1982lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1983 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001984{
Michal Vasko52927e22020-03-16 17:26:14 +01001985 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +02001986 int len;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001987 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001988 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001989
Michal Vasko52927e22020-03-16 17:26:14 +01001990 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001991
Michal Vasko52927e22020-03-16 17:26:14 +01001992 if (!node->schema) {
1993 dup = calloc(1, sizeof(struct lyd_node_opaq));
1994 } else {
1995 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001996 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001997 case LYS_ACTION:
1998 case LYS_NOTIF:
1999 case LYS_CONTAINER:
2000 case LYS_LIST:
2001 dup = calloc(1, sizeof(struct lyd_node_inner));
2002 break;
2003 case LYS_LEAF:
2004 case LYS_LEAFLIST:
2005 dup = calloc(1, sizeof(struct lyd_node_term));
2006 break;
2007 case LYS_ANYDATA:
2008 case LYS_ANYXML:
2009 dup = calloc(1, sizeof(struct lyd_node_any));
2010 break;
2011 default:
2012 LOGINT(LYD_NODE_CTX(node));
2013 ret = LY_EINT;
2014 goto error;
2015 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002016 }
Michal Vasko52927e22020-03-16 17:26:14 +01002017 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002018
Michal Vaskof6df0a02020-06-16 13:08:34 +02002019 if (options & LYD_DUP_WITH_FLAGS) {
2020 dup->flags = node->flags;
2021 } else {
2022 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2023 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002024 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002025 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002026
2027 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
2028
2029 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002030 if (!dup->schema) {
2031 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2032 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2033 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002034
2035 if (options & LYD_DUP_RECURSIVE) {
2036 /* duplicate all the children */
2037 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01002038 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
2039 }
2040 }
2041 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2042 opaq->format = orig->format;
2043 if (orig->prefix.pref) {
2044 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2045 }
2046 if (orig->prefix.ns) {
2047 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2048 }
2049 if (orig->val_prefs) {
2050 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
2051 LY_ARRAY_FOR(orig->val_prefs, u) {
2052 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2053 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2054 LY_ARRAY_INCREMENT(opaq->val_prefs);
2055 }
2056 }
2057 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2058 opaq->ctx = orig->ctx;
2059 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2060 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2061 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2062
2063 term->hash = orig->hash;
2064 term->value.realtype = orig->value.realtype;
2065 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2066 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2067 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2068 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2069 struct lyd_node *child;
2070
2071 if (options & LYD_DUP_RECURSIVE) {
2072 /* duplicate all the children */
2073 LY_LIST_FOR(orig->child, child) {
2074 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002075 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002076 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002077 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002078 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002079 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002080 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002081 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002082 if (!child) {
2083 /* possibly not keys are present in filtered tree */
2084 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002085 } else if (child->schema != key) {
2086 /* possibly not all keys are present in filtered tree,
2087 * but there can be also some non-key nodes */
2088 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002089 }
Michal Vasko52927e22020-03-16 17:26:14 +01002090 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002091 child = child->next;
2092 }
2093 }
2094 lyd_hash(dup);
2095 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01002096 struct lyd_node_any *any = (struct lyd_node_any *)dup;
2097 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002098
2099 any->hash = orig->hash;
2100 any->value_type = orig->value_type;
2101 switch (any->value_type) {
2102 case LYD_ANYDATA_DATATREE:
2103 if (orig->value.tree) {
2104 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02002105 if (!any->value.tree) {
2106 /* get the last error's error code recorded by lyd_dup */
2107 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
2108 ret = ei ? ei->prev->no : LY_EOTHER;
2109 goto error;
2110 }
Michal Vasko60ea6352020-06-29 13:39:39 +02002111 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002112 }
2113 break;
2114 case LYD_ANYDATA_STRING:
2115 case LYD_ANYDATA_XML:
2116 case LYD_ANYDATA_JSON:
2117 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01002118 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02002119 }
2120 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002121 case LYD_ANYDATA_LYB:
2122 if (orig->value.mem) {
2123 len = lyd_lyb_data_length(orig->value.mem);
2124 any->value.mem = malloc(len);
2125 LY_CHECK_ERR_GOTO(!any->value.mem, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
2126 memcpy(any->value.mem, orig->value.mem, len);
2127 }
2128 break;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002129 }
2130 }
2131
Michal Vasko52927e22020-03-16 17:26:14 +01002132 /* insert */
2133 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002134
2135 if (dup_p) {
2136 *dup_p = dup;
2137 }
2138 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002139
2140error:
Michal Vasko52927e22020-03-16 17:26:14 +01002141 lyd_free_tree(dup);
2142 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002143}
2144
2145API struct lyd_node *
2146lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
2147{
2148 struct ly_ctx *ctx;
2149 const struct lyd_node *orig; /* original node to be duplicated */
2150 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002151 struct lyd_node *top = NULL; /* the most higher created node */
2152 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
2153 int keyless_parent_list = 0;
2154
2155 LY_CHECK_ARG_RET(NULL, node, NULL);
2156 ctx = node->schema->module->ctx;
2157
2158 if (options & LYD_DUP_WITH_PARENTS) {
2159 struct lyd_node_inner *orig_parent, *iter;
2160 int repeat = 1;
2161 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2162 if (parent && parent->schema == orig_parent->schema) {
2163 /* stop creating parents, connect what we have into the provided parent */
2164 iter = parent;
2165 repeat = 0;
2166 /* get know if there is a keyless list which we will have to rehash */
2167 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002168 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002169 keyless_parent_list = 1;
2170 break;
2171 }
2172 }
2173 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01002174 iter = NULL;
2175 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2176 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002177 }
2178 if (!local_parent) {
2179 local_parent = iter;
2180 }
2181 if (iter->child) {
2182 /* 1) list - add after keys
2183 * 2) provided parent with some children */
2184 iter->child->prev->next = top;
2185 if (top) {
2186 top->prev = iter->child->prev;
2187 iter->child->prev = top;
2188 }
2189 } else {
2190 iter->child = top;
2191 if (iter->schema->nodetype == LYS_LIST) {
2192 /* keyless list - we will need to rehash it since we are going to add nodes into it */
2193 keyless_parent_list = 1;
2194 }
2195 }
2196 if (top) {
2197 top->parent = iter;
2198 }
2199 top = (struct lyd_node*)iter;
2200 }
2201 if (repeat && parent) {
2202 /* given parent and created parents chain actually do not interconnect */
2203 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2204 goto error;
2205 }
2206 } else {
2207 local_parent = parent;
2208 }
2209
Radek Krejci22ebdba2019-07-25 13:59:43 +02002210 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002211 /* if there is no local parent, it will be inserted into first */
2212 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 +02002213 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
2214 break;
2215 }
2216 }
2217 if (keyless_parent_list) {
2218 /* rehash */
2219 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002220 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002221 lyd_hash((struct lyd_node*)local_parent);
2222 }
2223 }
2224 }
2225 return first;
2226
2227error:
2228 if (top) {
2229 lyd_free_tree(top);
2230 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002231 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002232 }
2233 return NULL;
2234}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002235
Michal Vasko4490d312020-06-16 13:08:55 +02002236/**
2237 * @brief Merge a source sibling into target siblings.
2238 *
2239 * @param[in,out] first_trg First target sibling, is updated if top-level.
2240 * @param[in] parent_trg Target parent.
2241 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2242 * @param[in] options Merge options.
2243 * @return LY_ERR value.
2244 */
2245static LY_ERR
2246lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2247 int options)
2248{
2249 LY_ERR ret;
2250 const struct lyd_node *child_src, *tmp, *sibling_src;
2251 struct lyd_node *match_trg, *dup_src, *next, *elem;
2252 struct lysc_type *type;
2253 LYD_ANYDATA_VALUETYPE tmp_val_type;
2254 union lyd_any_value tmp_val;
2255
2256 sibling_src = *sibling_src_p;
2257 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2258 /* try to find the exact instance */
2259 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2260 } else {
2261 /* try to simply find the node, there cannot be more instances */
2262 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2263 }
2264
2265 if (!ret) {
2266 /* node found, make sure even value matches for all node types */
2267 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2268 /* since they are different, they cannot both be default */
2269 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2270
2271 /* update value (or only LYD_DEFAULT flag) only if no flag set or the source node is not default */
2272 if (!(options & LYD_MERGE_EXPLICIT) || !(sibling_src->flags & LYD_DEFAULT)) {
2273 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2274 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2275 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2276 &((struct lyd_node_term *)match_trg)->value));
2277
2278 /* copy flags and add LYD_NEW */
2279 match_trg->flags = sibling_src->flags | LYD_NEW;
2280 }
2281 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2282 if (options & LYD_MERGE_DESTRUCT) {
2283 dup_src = (struct lyd_node *)sibling_src;
2284 lyd_unlink_tree(dup_src);
2285 /* spend it */
2286 *sibling_src_p = NULL;
2287 } else {
2288 dup_src = lyd_dup(sibling_src, NULL, 0);
2289 LY_CHECK_RET(!dup_src, LY_EMEM);
2290 }
2291 /* just switch values */
2292 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2293 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2294 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2295 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2296 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2297 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2298
2299 /* copy flags and add LYD_NEW */
2300 match_trg->flags = sibling_src->flags | LYD_NEW;
2301
2302 /* dup_src is not needed, actually */
2303 lyd_free_tree(dup_src);
2304 } else {
2305 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002306 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002307 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2308 }
2309 }
2310 } else {
2311 /* node not found, merge it */
2312 if (options & LYD_MERGE_DESTRUCT) {
2313 dup_src = (struct lyd_node *)sibling_src;
2314 lyd_unlink_tree(dup_src);
2315 /* spend it */
2316 *sibling_src_p = NULL;
2317 } else {
2318 dup_src = lyd_dup(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS);
2319 LY_CHECK_RET(!dup_src, LY_EMEM);
2320 }
2321
2322 /* set LYD_NEW for all the new nodes, required for validation */
2323 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2324 elem->flags |= LYD_NEW;
2325 LYD_TREE_DFS_END(dup_src, next, elem);
2326 }
2327
2328 lyd_insert_node(parent_trg, first_trg, dup_src);
2329 }
2330
2331 return LY_SUCCESS;
2332}
2333
2334API LY_ERR
2335lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options)
2336{
2337 const struct lyd_node *sibling_src, *tmp;
2338 int first;
2339
2340 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2341
2342 if (!source) {
2343 /* nothing to merge */
2344 return LY_SUCCESS;
2345 }
2346
2347 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2348 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2349 return LY_EINVAL;
2350 }
2351
2352 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2353 first = sibling_src == source ? 1 : 0;
2354 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2355 if (first && !sibling_src) {
2356 /* source was spent (unlinked), move to the next node */
2357 source = tmp;
2358 }
2359
2360 if (options & LYD_MERGE_NOSIBLINGS) {
2361 break;
2362 }
2363 }
2364
2365 if (options & LYD_MERGE_DESTRUCT) {
2366 /* free any leftover source data that were not merged */
2367 lyd_free_siblings((struct lyd_node *)source);
2368 }
2369
2370 return LY_SUCCESS;
2371}
2372
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002373struct lyd_diff_userord {
2374 const struct lysc_node *schema; /**< User-ordered list/leaf-list schema node. */
2375 uint64_t pos; /**< Current position in the second tree. */
2376 const struct lyd_node **inst; /**< Sized array of current instance order. */
2377};
2378
2379enum lyd_diff_op {
2380 LYD_DIFF_OP_CREATE, /**< Subtree created. */
2381 LYD_DIFF_OP_DELETE, /**< Subtree deleted. */
2382 LYD_DIFF_OP_REPLACE, /**< Node value changed or (leaf-)list instance moved. */
2383 LYD_DIFF_OP_NONE, /**< No change of an existing inner node or default flag change of a term node. */
2384};
2385
2386static const char *
2387lyd_diff_op2str(enum lyd_diff_op op)
2388{
2389 switch (op) {
2390 case LYD_DIFF_OP_CREATE:
2391 return "create";
2392 case LYD_DIFF_OP_DELETE:
2393 return "delete";
2394 case LYD_DIFF_OP_REPLACE:
2395 return "replace";
2396 case LYD_DIFF_OP_NONE:
2397 return "none";
2398 }
2399
2400 LOGINT(NULL);
2401 return NULL;
2402}
2403
2404/**
2405 * @brief Add a new change into diff.
2406 *
2407 * @param[in] node Node (subtree) to add into diff.
2408 * @param[in] op Operation to set.
2409 * @param[in] orig_default Original default metadata to set.
2410 * @param[in] orig_value Original value metadata to set.
2411 * @param[in] key Key metadata to set.
2412 * @param[in] value Value metadata to set.
2413 * @param[in] orig_key Original key metadata to set.
2414 * @param[in,out] diff Diff to append to.
2415 * @return LY_ERR value.
2416 */
2417static LY_ERR
2418lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
2419 const char *key, const char *value, const char *orig_key, struct lyd_node **diff)
2420{
2421 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
2422 const struct lyd_node *parent = NULL;
2423 const struct lys_module *yang_mod;
2424
2425 /* find the first existing parent */
2426 siblings = *diff;
2427 while (1) {
2428 /* find next node parent */
2429 parent = node;
2430 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
2431 parent = (struct lyd_node *)parent->parent;
2432 }
2433 if (parent == node) {
2434 /* no more parents to find */
2435 break;
2436 }
2437
2438 /* check whether it exists in the diff */
2439 if (lyd_find_sibling_first(siblings, parent, &match)) {
2440 break;
2441 }
2442
2443 /* another parent found */
2444 diff_parent = match;
2445
2446 /* move down in the diff */
2447 siblings = LYD_CHILD(match);
2448 }
2449
2450 /* duplicate the subtree (and connect to the diff if possible) */
2451 dup = lyd_dup(node, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS);
2452 LY_CHECK_RET(!dup, LY_EMEM);
2453
2454 /* find the first duplicated parent */
2455 if (!diff_parent) {
2456 diff_parent = (struct lyd_node *)dup->parent;
2457 while (diff_parent && diff_parent->parent) {
2458 diff_parent = (struct lyd_node *)diff_parent->parent;
2459 }
2460 } else {
2461 diff_parent = (struct lyd_node *)dup;
2462 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
2463 diff_parent = (struct lyd_node *)diff_parent->parent;
2464 }
2465 }
2466
2467 /* no parent existed, must be manually connected */
2468 if (!diff_parent) {
2469 /* there actually was no parent to duplicate */
2470 if (*diff) {
2471 lyd_insert_sibling(*diff, dup);
2472 } else {
2473 *diff = dup;
2474 }
2475 } else if (!diff_parent->parent) {
2476 if (*diff) {
2477 lyd_insert_sibling(*diff, diff_parent);
2478 } else {
2479 *diff = diff_parent;
2480 }
2481 }
2482
2483 /* get module with the operation metadata */
2484 yang_mod = LYD_NODE_CTX(node)->list.objs[1];
2485 assert(!strcmp(yang_mod->name, "yang"));
2486
2487 /* add parent operation, if any */
2488 if (diff_parent && (diff_parent != dup) && !lyd_new_meta(diff_parent, yang_mod, "operation", "none")) {
2489 return LY_EMEM;
2490 }
2491
2492 /* add subtree operation */
2493 if (!lyd_new_meta(dup, yang_mod, "operation", lyd_diff_op2str(op))) {
2494 return LY_EMEM;
2495 }
2496
2497 /* orig-default */
2498 if (orig_default && !lyd_new_meta(dup, yang_mod, "orig-default", orig_default)) {
2499 return LY_EMEM;
2500 }
2501
2502 /* orig-value */
2503 if (orig_value && !lyd_new_meta(dup, yang_mod, "orig-value", orig_value)) {
2504 return LY_EMEM;
2505 }
2506
2507 /* key */
2508 if (key && !lyd_new_meta(dup, yang_mod, "key", key)) {
2509 return LY_EMEM;
2510 }
2511
2512 /* value */
2513 if (value && !lyd_new_meta(dup, yang_mod, "value", value)) {
2514 return LY_EMEM;
2515 }
2516
2517 /* orig-key */
2518 if (orig_key && !lyd_new_meta(dup, yang_mod, "orig-key", orig_key)) {
2519 return LY_EMEM;
2520 }
2521
2522 return LY_SUCCESS;
2523}
2524
2525/**
2526 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
2527 *
2528 * @param[in] first
2529 * @param[in] schema Schema node of the list/leaf-list.
2530 * @param[in,out] userord Sized array of userord items.
2531 * @return Userord item for all the user-ordered list/leaf-list instances.
2532 */
2533static struct lyd_diff_userord *
2534lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
2535{
2536 struct lyd_diff_userord *item;
2537 const struct lyd_node *iter, **node;
2538 LY_ARRAY_SIZE_TYPE u;
2539
2540 LY_ARRAY_FOR(*userord, u) {
2541 if ((*userord)[u].schema == schema) {
2542 return &(*userord)[u];
2543 }
2544 }
2545
2546 /* it was not added yet, add it now */
2547 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
2548
2549 item->schema = schema;
2550 item->pos = 0;
2551 item->inst = NULL;
2552
2553 /* store all the instance pointers in the current order */
2554 if (first) {
2555 if (first->parent) {
2556 iter = first->parent->child;
2557 } else {
2558 for (iter = first; iter->prev->next; iter = iter->prev);
2559 }
2560 for (; iter; iter = iter->next) {
2561 if (iter->schema == first->schema) {
2562 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
2563 *node = iter;
2564 }
2565 }
2566 }
2567
2568 return item;
2569}
2570
2571/**
2572 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
2573 * lists/leaf-lists.
2574 *
2575 * @param[in] first Node from the first tree, can be NULL (on create).
2576 * @param[in] second Node from the second tree, can be NULL (on delete).
2577 * @param[in] options Diff options.
2578 * @param[in,out] userord Sized array of userord items for keeping the current node order.
2579 * @param[out] op Operation.
2580 * @param[out] orig_default Original default metadata.
2581 * @param[out] value Value metadata.
2582 * @param[out] orig_value Original value metadata
2583 * @param[out] key Key metadata.
2584 * @param[out] orig_key Original key metadata.
2585 * @return LY_SUCCESS on success,
2586 * @return LY_ENOT if there is no change to be added into diff,
2587 * @return LY_ERR value on other errors.
2588 */
2589static LY_ERR
2590lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, int options,
2591 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
2592 char **orig_value, char **key, char **orig_key)
2593{
2594 const struct lysc_node *schema;
2595 int dynamic;
2596 size_t buflen, bufused, first_pos, second_pos;
2597 struct lyd_diff_userord *userord_item;
2598
2599 assert(first || second);
2600
2601 *orig_default = NULL;
2602 *value = NULL;
2603 *orig_value = NULL;
2604 *key = NULL;
2605 *orig_key = NULL;
2606
2607 schema = first ? first->schema : second->schema;
2608 assert(lysc_is_userordered(schema));
2609
2610 /* get userord entry */
2611 userord_item = lyd_diff_userord_get(first, schema, userord);
2612 LY_CHECK_RET(!userord_item, LY_EMEM);
2613
2614 /* prepare position of the next instance */
2615 second_pos = userord_item->pos++;
2616
2617 /* find user-ordered first position */
2618 if (first) {
2619 for (first_pos = second_pos; first_pos < LY_ARRAY_SIZE(userord_item->inst); ++first_pos) {
2620 if (userord_item->inst[first_pos] == first) {
2621 break;
2622 }
2623 }
2624 assert(first_pos < LY_ARRAY_SIZE(userord_item->inst));
2625 }
2626
2627 /* learn operation first */
2628 if (!second) {
2629 *op = LYD_DIFF_OP_DELETE;
2630 } else if (!first) {
2631 *op = LYD_DIFF_OP_CREATE;
2632 } else {
2633 assert(schema->nodetype & (LYS_LIST | LYS_LEAFLIST));
2634 if (lyd_compare(second, userord_item->inst[second_pos], 0)) {
2635 /* in first, there is a different instance on the second position, we are going to move 'first' node */
2636 *op = LYD_DIFF_OP_REPLACE;
2637 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2638 /* default flag change */
2639 *op = LYD_DIFF_OP_NONE;
2640 } else {
2641 /* no changes */
2642 return LY_ENOT;
2643 }
2644 }
2645
2646 /*
2647 * set each attribute correctly based on the operation and node type
2648 */
2649
2650 /* orig-default */
2651 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype == LYS_LEAFLIST)
2652 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2653 if (first->flags & LYD_DEFAULT) {
2654 *orig_default = "true";
2655 } else {
2656 *orig_default = "false";
2657 }
2658 }
2659
2660 /* value */
2661 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
2662 if (second_pos) {
2663 *value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[second_pos - 1], &dynamic);
2664 if (!dynamic) {
2665 *value = strdup(*value);
2666 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2667 }
2668 } else {
2669 *value = strdup("");
2670 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2671 }
2672 }
2673
2674 /* orig-value */
2675 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2676 if (first_pos) {
2677 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[first_pos - 1], &dynamic);
2678 if (!dynamic) {
2679 *orig_value = strdup(*orig_value);
2680 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2681 }
2682 } else {
2683 *orig_value = strdup("");
2684 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2685 }
2686 }
2687
2688 /* key */
2689 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op ==LYD_DIFF_OP_CREATE))) {
2690 if (second_pos) {
2691 buflen = bufused = 0;
2692 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
2693 } else {
2694 *key = strdup("");
2695 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
2696 }
2697 }
2698
2699 /* orig-key */
2700 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2701 if (first_pos) {
2702 buflen = bufused = 0;
2703 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
2704 } else {
2705 *orig_key = strdup("");
2706 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
2707 }
2708 }
2709
2710 /*
2711 * update our instances - apply the change
2712 */
2713 if (*op == LYD_DIFF_OP_CREATE) {
2714 /* insert the instance */
2715 LY_ARRAY_RESIZE_ERR_RET(schema->module->ctx, userord_item->inst, LY_ARRAY_SIZE(userord_item->inst) + 1,
2716 ;, LY_EMEM);
2717 if (second_pos < LY_ARRAY_SIZE(userord_item->inst)) {
2718 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2719 (LY_ARRAY_SIZE(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
2720 }
2721 LY_ARRAY_INCREMENT(userord_item->inst);
2722 userord_item->inst[second_pos] = second;
2723
2724 } else if (*op == LYD_DIFF_OP_DELETE) {
2725 /* remove the instance */
2726 if (first_pos + 1 < LY_ARRAY_SIZE(userord_item->inst)) {
2727 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
2728 (LY_ARRAY_SIZE(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
2729 }
2730 LY_ARRAY_DECREMENT(userord_item->inst);
2731
2732 } else if (*op == LYD_DIFF_OP_REPLACE) {
2733 /* move the instances */
2734 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2735 (first_pos - second_pos) * sizeof *userord_item->inst);
2736 userord_item->inst[second_pos] = first;
2737 }
2738
2739 return LY_SUCCESS;
2740}
2741
2742/**
2743 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
2744 * lists/leaf-lists.
2745 *
2746 * @param[in] first Node from the first tree, can be NULL (on create).
2747 * @param[in] second Node from the second tree, can be NULL (on delete).
2748 * @param[in] options Diff options.
2749 * @param[out] op Operation.
2750 * @param[out] orig_default Original default metadata.
2751 * @param[out] orig_value Original value metadata.
2752 * @return LY_SUCCESS on success,
2753 * @return LY_ENOT if there is no change to be added into diff,
2754 * @return LY_ERR value on other errors.
2755 */
2756static LY_ERR
2757lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, int options, enum lyd_diff_op *op,
2758 const char **orig_default, char **orig_value)
2759{
2760 const struct lysc_node *schema;
2761 int dynamic;
2762
2763 assert(first || second);
2764
2765 *orig_default = NULL;
2766 *orig_value = NULL;
2767
2768 schema = first ? first->schema : second->schema;
2769 assert(!lysc_is_userordered(schema));
2770
2771 /* learn operation first */
2772 if (!second) {
2773 *op = LYD_DIFF_OP_DELETE;
2774 } else if (!first) {
2775 *op = LYD_DIFF_OP_CREATE;
2776 } else {
2777 switch (schema->nodetype) {
2778 case LYS_CONTAINER:
2779 case LYS_RPC:
2780 case LYS_ACTION:
2781 case LYS_NOTIF:
2782 /* no changes */
2783 return LY_ENOT;
2784 case LYS_LIST:
2785 case LYS_LEAFLIST:
2786 if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2787 /* default flag change */
2788 *op = LYD_DIFF_OP_NONE;
2789 } else {
2790 /* no changes */
2791 return LY_ENOT;
2792 }
2793 break;
2794 case LYS_LEAF:
2795 case LYS_ANYXML:
2796 case LYS_ANYDATA:
2797 if (lyd_compare(first, second, 0)) {
2798 /* different values */
2799 *op = LYD_DIFF_OP_REPLACE;
2800 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2801 /* default flag change */
2802 *op = LYD_DIFF_OP_NONE;
2803 } else {
2804 /* no changes */
2805 return LY_ENOT;
2806 }
2807 break;
2808 default:
2809 LOGINT_RET(schema->module->ctx);
2810 }
2811 }
2812
2813 /*
2814 * set each attribute correctly based on the operation and node type
2815 */
2816
2817 /* orig-default */
2818 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype & LYD_NODE_TERM)
2819 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2820 if (first->flags & LYD_DEFAULT) {
2821 *orig_default = "true";
2822 } else {
2823 *orig_default = "false";
2824 }
2825 }
2826
2827 /* orig-value */
2828 if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
2829 /* leaf */
2830 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)first, &dynamic);
2831 if (!dynamic) {
2832 *orig_value = strdup(*orig_value);
2833 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2834 }
2835 }
2836
2837 return LY_SUCCESS;
2838}
2839
2840/**
2841 * @brief Perform diff for all siblings at certain depth, recursively.
2842 *
2843 * For user-ordered lists/leaf-lists a specific structure is used for storing
2844 * the current order. The idea is to apply all the generated diff changes
2845 * virtually on the first tree so that we can continue to generate correct
2846 * changes after some were already generated.
2847 *
2848 * The algorithm then uses second tree position-based changes with a before
2849 * (preceding) item anchor.
2850 *
2851 * Example:
2852 *
2853 * Virtual first tree leaf-list order:
2854 * 1 2 [3] 4 5
2855 *
2856 * Second tree leaf-list order:
2857 * 1 2 [5] 3 4
2858 *
2859 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
2860 * match - they do not - move nodes so that the 3rd position node is final ->
2861 * -> move node 5 to the 3rd position -> move node 5 after node 2.
2862 *
2863 * Required properties:
2864 * Stored operations (move) should not be affected by later operations -
2865 * - would cause a redundantly long list of operations, possibly inifinite.
2866 *
2867 * Implemenation justification:
2868 * First, all delete operations and only then move/create operations are stored.
2869 * Also, preceding anchor is used and after each iteration another node is
2870 * at its final position. That results in the invariant that all preceding
2871 * nodes are final and will not be changed by the later operations, meaning
2872 * they can safely be used as anchors for the later operations.
2873 *
2874 * @param[in] first First tree first sibling.
2875 * @param[in] second Second tree first sibling.
2876 * @param[in] options Diff options.
2877 * @param[in,out] diff Diff to append to.
2878 * @return LY_ERR value.
2879 */
2880static LY_ERR
2881lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
2882{
2883 LY_ERR ret = LY_SUCCESS;
2884 const struct lyd_node *iter_first, *iter_second;
2885 struct lyd_node *match_second, *match_first;
2886 int nosiblings = 0;
2887 struct lyd_diff_userord *userord = NULL;
2888 LY_ARRAY_SIZE_TYPE u;
2889 enum lyd_diff_op op;
2890 const char *orig_default;
2891 char *orig_value, *key, *value, *orig_key;
2892
2893 if (options & LYD_DIFF_NOSIBLINGS) {
2894 /* remember it for this function call only, should not be passed recursively */
2895 nosiblings = 1;
2896 options &= ~LYD_DIFF_NOSIBLINGS;
2897 }
2898
2899 /* compare first tree to the second tree - delete, replace, none */
2900 LY_LIST_FOR(first, iter_first) {
2901 assert(!(iter_first->schema->flags & LYS_KEY));
2902 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2903 /* skip default nodes */
2904 continue;
2905 }
2906
2907 if (iter_first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2908 /* try to find the exact instance */
2909 lyd_find_sibling_first(second, iter_first, &match_second);
2910 } else {
2911 /* try to simply find the node, there cannot be more instances */
2912 lyd_find_sibling_val(second, iter_first->schema, NULL, 0, &match_second);
2913 }
2914
2915 if (match_second && (match_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2916 /* ignore default nodes */
2917 match_second = NULL;
2918 }
2919
2920 if (lysc_is_userordered(iter_first->schema)) {
2921 if (match_second) {
2922 /* we are handling only user-ordered node delete now */
2923 continue;
2924 }
2925
2926 /* get all the attributes */
2927 LY_CHECK_GOTO(lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
2928 &value, &orig_value, &key, &orig_key), cleanup);
2929
2930 /* there must be changes, it is deleted */
2931 assert(op == LYD_DIFF_OP_DELETE);
2932 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, orig_key, diff);
2933
2934 free(orig_value);
2935 free(key);
2936 free(value);
2937 free(orig_key);
2938 LY_CHECK_GOTO(ret, cleanup);
2939 } else {
2940 /* get all the attributes */
2941 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
2942
2943 /* add into diff if there are any changes */
2944 if (!ret) {
2945 if (op == LYD_DIFF_OP_DELETE) {
2946 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2947 } else {
2948 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2949 }
2950
2951 free(orig_value);
2952 LY_CHECK_GOTO(ret, cleanup);
2953 } else if (ret == LY_ENOT) {
2954 ret = LY_SUCCESS;
2955 } else {
2956 goto cleanup;
2957 }
2958 }
2959
2960 /* check descendants, if any, recursively */
2961 if (match_second) {
2962 LY_CHECK_GOTO(lyd_diff_siblings_r(LYD_CHILD(iter_first), LYD_CHILD(match_second), options, diff), cleanup);
2963 }
2964
2965 if (nosiblings) {
2966 break;
2967 }
2968 }
2969
2970 /* reset all cached positions */
2971 LY_ARRAY_FOR(userord, u) {
2972 userord[u].pos = 0;
2973 }
2974
2975 /* compare second tree to the first tree - create, user-ordered move */
2976 LY_LIST_FOR(second, iter_second) {
2977 assert(!(iter_second->schema->flags & LYS_KEY));
2978 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2979 /* skip default nodes */
2980 continue;
2981 }
2982
2983 if (iter_second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2984 lyd_find_sibling_first(first, iter_second, &match_first);
2985 } else {
2986 lyd_find_sibling_val(first, iter_second->schema, NULL, 0, &match_first);
2987 }
2988
2989 if (match_first && (match_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2990 /* ignore default nodes */
2991 match_first = NULL;
2992 }
2993
2994 if (lysc_is_userordered(iter_second->schema)) {
2995 /* get all the attributes */
2996 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
2997 &value, &orig_value, &key, &orig_key);
2998
2999 /* add into diff if there are any changes */
3000 if (!ret) {
3001 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, orig_key, diff);
3002
3003 free(orig_value);
3004 free(key);
3005 free(value);
3006 free(orig_key);
3007 LY_CHECK_GOTO(ret, cleanup);
3008 } else if (ret == LY_ENOT) {
3009 ret = LY_SUCCESS;
3010 } else {
3011 goto cleanup;
3012 }
3013 } else if (!match_first) {
3014 /* get all the attributes */
3015 LY_CHECK_GOTO(lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
3016
3017 /* there must be changes, it is created */
3018 assert(op == LYD_DIFF_OP_CREATE);
3019 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
3020
3021 free(orig_value);
3022 LY_CHECK_GOTO(ret, cleanup);
3023 } /* else was handled */
3024
3025 if (nosiblings) {
3026 break;
3027 }
3028 }
3029
3030cleanup:
3031 LY_ARRAY_FOR(userord, u) {
3032 LY_ARRAY_FREE(userord[u].inst);
3033 }
3034 LY_ARRAY_FREE(userord);
3035 return ret;
3036}
3037
3038API LY_ERR
3039lyd_diff(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
3040{
3041 const struct ly_ctx *ctx;
3042
3043 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
3044
3045 if (first) {
3046 ctx = LYD_NODE_CTX(first);
3047 } else if (second) {
3048 ctx = LYD_NODE_CTX(second);
3049 } else {
3050 ctx = NULL;
3051 }
3052
3053 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
3054 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
3055 return LY_EINVAL;
3056 }
3057
3058 *diff = NULL;
3059
3060 return lyd_diff_siblings_r(first, second, options, diff);
3061}
3062
3063/**
3064 * @brief Find a matching node in data tree for a diff node.
3065 *
3066 * @param[in] first_node First sibling in the data tree.
3067 * @param[in] diff_node Diff node to match.
3068 * @param[out] match_p Matching node.
3069 * @return LY_ERR value.
3070 */
3071static LY_ERR
3072lyd_diff_find_node(const struct lyd_node *first_node, const struct lyd_node *diff_node, struct lyd_node **match_p)
3073{
3074 if (diff_node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3075 /* try to find the exact instance */
3076 lyd_find_sibling_first(first_node, diff_node, match_p);
3077 } else {
3078 /* try to simply find the node, there cannot be more instances */
3079 lyd_find_sibling_val(first_node, diff_node->schema, NULL, 0, match_p);
3080 }
3081 LY_CHECK_ERR_RET(!*match_p, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3082
3083 return LY_SUCCESS;
3084}
3085
3086/**
3087 * @brief Learn operation of a diff node.
3088 *
3089 * @param[in] diff_node Diff node.
3090 * @param[out] op Operation.
3091 * @param[out] key_or_value Optional list instance keys predicate or leaf-list value for move operation.
3092 * @return LY_ERR value.
3093 */
3094static LY_ERR
3095lyd_diff_get_op(const struct lyd_node *diff_node, const char **op, const char **key_or_value)
3096{
3097 struct lyd_meta *meta = NULL;
3098 const struct lyd_node *diff_parent;
3099 const char *meta_name, *str;
3100 int dynamic;
3101
3102 for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
3103 LY_LIST_FOR(diff_parent->meta, meta) {
3104 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
3105 str = lyd_meta2str(meta, &dynamic);
3106 assert(!dynamic);
3107 if ((str[0] == 'r') && (diff_parent != diff_node)) {
3108 /* we do not care about this operation if it's in our parent */
3109 continue;
3110 }
3111 *op = str;
3112 break;
3113 }
3114 }
3115 if (meta) {
3116 break;
3117 }
3118 }
3119 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3120
3121 *key_or_value = NULL;
3122 if (lysc_is_userordered(diff_node->schema) && (((*op)[0] == 'c') || ((*op)[0] == 'r'))) {
3123 if (diff_node->schema->nodetype == LYS_LIST) {
3124 meta_name = "key";
3125 } else {
3126 meta_name = "value";
3127 }
3128
3129 LY_LIST_FOR(diff_node->meta, meta) {
3130 if (!strcmp(meta->name, meta_name) && !strcmp(meta->annotation->module->name, "yang")) {
3131 str = lyd_meta2str(meta, &dynamic);
3132 assert(!dynamic);
3133 *key_or_value = str;
3134 break;
3135 }
3136 }
3137 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3138 }
3139
3140 return LY_SUCCESS;
3141}
3142
3143/**
3144 * @brief Insert a diff node into a data tree.
3145 *
3146 * @param[in,out] first_node First sibling of the data tree.
3147 * @param[in] parent_node Data tree sibling parent node.
3148 * @param[in] new_node Node to insert.
3149 * @param[in] keys_or_value Optional predicate of relative (leaf-)list instance. If not set, the user-ordered
3150 * instance will be inserted at the first position.
3151 * @return err_info, NULL on success.
3152 */
3153static LY_ERR
3154lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
3155 const char *key_or_value)
3156{
3157 LY_ERR ret;
3158 struct lyd_node *anchor;
3159
3160 assert(new_node);
3161
3162 if (!*first_node) {
3163 if (!parent_node) {
3164 /* no parent or siblings */
3165 *first_node = new_node;
3166 return LY_SUCCESS;
3167 }
3168
3169 /* simply insert into parent, no other children */
3170 if (key_or_value) {
3171 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3172 new_node->schema->name);
3173 return LY_EINVAL;
3174 }
3175 return lyd_insert(parent_node, new_node);
3176 }
3177
3178 assert(!(*first_node)->parent || ((struct lyd_node *)(*first_node)->parent == parent_node));
3179
3180 /* simple insert */
3181 if (!lysc_is_userordered(new_node->schema)) {
3182 /* insert at the end */
3183 return lyd_insert_sibling(*first_node, new_node);
3184 }
3185
3186 if (key_or_value) {
3187 /* find the anchor sibling */
3188 ret = lyd_find_sibling_val(*first_node, new_node->schema, key_or_value, 0, &anchor);
3189 if (ret == LY_ENOTFOUND) {
3190 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3191 new_node->schema->name);
3192 return LY_EINVAL;
3193 } else if (ret) {
3194 return ret;
3195 }
3196
3197 /* insert after */
3198 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3199 assert(new_node->prev == anchor);
3200 if (*first_node == new_node) {
3201 *first_node = anchor;
3202 }
3203 } else {
3204 if ((*first_node)->schema->flags & LYS_KEY) {
3205 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
3206
3207 /* find last key */
3208 anchor = *first_node;
3209 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
3210 anchor = anchor->next;
3211 }
3212 /* insert after the last key */
3213 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3214 } else {
3215 /* insert at the beginning */
3216 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
3217 *first_node = new_node;
3218 }
3219 }
3220
3221 return LY_SUCCESS;
3222}
3223
3224/**
3225 * @brief Apply diff subtree on data tree nodes, recursively.
3226 *
3227 * @param[in,out] first_node First sibling of the data tree.
3228 * @param[in] parent_node Parent of the first sibling.
3229 * @param[in] diff_node Current diff node.
3230 * @return LY_ERR value.
3231 */
3232static LY_ERR
3233lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
3234 lyd_diff_cb diff_cb, void *cb_data)
3235{
3236 LY_ERR ret;
3237 struct lyd_node *match, *diff_child;
3238 const char *op, *key_or_value, *str_val;
3239 int dynamic;
3240 const struct ly_ctx *ctx = LYD_NODE_CTX(diff_node);
3241
3242 /* read all the valid attributes */
3243 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op, &key_or_value));
3244
3245 /* handle user-ordered (leaf-)lists separately */
3246 if (key_or_value) {
3247 assert((op[0] == 'c') || (op[0] == 'r'));
3248 if (op[0] == 'r') {
3249 /* find the node (we must have some siblings because the node was only moved) */
3250 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3251 } else {
3252 /* duplicate the node(s) */
3253 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3254 LY_CHECK_RET(!match, LY_EMEM);
3255 }
3256
3257 /* insert/move the node */
3258 if (key_or_value[0]) {
3259 ret = lyd_diff_insert(first_node, parent_node, match, key_or_value);
3260 } else {
3261 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
3262 }
3263 if (ret) {
3264 if (op[0] == 'c') {
3265 lyd_free_tree(match);
3266 }
3267 return ret;
3268 }
3269
3270 goto next_iter_r;
3271 }
3272
3273 /* apply operation */
3274 switch (op[0]) {
3275 case 'n':
3276 /* find the node */
3277 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3278
3279 if (match->schema->nodetype & LYD_NODE_TERM) {
3280 /* special case of only dflt flag change */
3281 if (diff_node->flags & LYD_DEFAULT) {
3282 match->flags |= LYD_DEFAULT;
3283 } else {
3284 match->flags &= ~LYD_DEFAULT;
3285 }
3286 } else {
3287 /* none operation on nodes without children is redundant and hence forbidden */
3288 if (!LYD_CHILD(diff_node)) {
3289 LOGINT_RET(ctx);
3290 }
3291 }
3292 break;
3293 case 'c':
3294 /* duplicate the node */
3295 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3296 LY_CHECK_RET(!match, LY_EMEM);
3297
3298 /* insert it at the end */
3299 ret = 0;
3300 if (*first_node) {
3301 ret = lyd_insert_after((*first_node)->prev, match);
3302 } else if (parent_node) {
3303 ret = lyd_insert(parent_node, match);
3304 } else {
3305 *first_node = match;
3306 }
3307 if (ret) {
3308 lyd_free_tree(match);
3309 return ret;
3310 }
3311
3312 break;
3313 case 'd':
3314 /* find the node */
3315 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3316
3317 /* remove it */
3318 if ((match == *first_node) && !match->parent) {
3319 assert(!parent_node);
3320 /* we have removed the top-level node */
3321 *first_node = (*first_node)->next;
3322 }
3323 lyd_free_tree(match);
3324
3325 /* we are not going recursively in this case, the whole subtree was already deleted */
3326 return LY_SUCCESS;
3327 case 'r':
3328 LY_CHECK_ERR_RET(diff_node->schema->nodetype != LYS_LEAF, LOGINT(ctx), LY_EINT);
3329
3330 /* find the node */
3331 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3332
3333 /* update its value */
3334 str_val = lyd_value2str((struct lyd_node_term *)diff_node, &dynamic);
3335 ret = lyd_change_term(match, str_val);
3336 if (dynamic) {
3337 free((char *)str_val);
3338 }
3339 if (ret && (ret != LY_EEXIST)) {
3340 LOGINT_RET(ctx);
3341 }
3342
3343 /* with flags */
3344 match->flags = diff_node->flags;
3345 break;
3346 default:
3347 LOGINT_RET(ctx);
3348 }
3349
3350next_iter_r:
3351 if (diff_cb) {
3352 /* call callback */
3353 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
3354 }
3355
3356 /* apply diff recursively */
3357 LY_LIST_FOR(LYD_CHILD(diff_node), diff_child) {
3358 LY_CHECK_RET(lyd_diff_apply_r(lyd_node_children_p(match), match, diff_child, diff_cb, cb_data));
3359 }
3360
3361 return LY_SUCCESS;
3362}
3363
3364API LY_ERR
3365lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
3366 lyd_diff_cb diff_cb, void *cb_data)
3367{
3368 const struct lyd_node *root;
3369
3370 LY_LIST_FOR(diff, root) {
3371 if (mod && (lyd_owner_module(root) != mod)) {
3372 /* skip data nodes from different modules */
3373 continue;
3374 }
3375
3376 /* apply relevant nodes from the diff datatree */
3377 LY_CHECK_RET(lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data));
3378 }
3379
3380 return LY_SUCCESS;
3381}
3382
3383API LY_ERR
3384lyd_diff_apply(struct lyd_node **data, const struct lyd_node *diff)
3385{
3386 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
3387}
3388
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003389static LY_ERR
3390lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
3391{
Michal Vasko14654712020-02-06 08:35:21 +01003392 /* ending \0 */
3393 ++reqlen;
3394
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003395 if (reqlen > *buflen) {
3396 if (is_static) {
3397 return LY_EINCOMPLETE;
3398 }
3399
3400 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3401 if (!*buffer) {
3402 return LY_EMEM;
3403 }
3404
3405 *buflen = reqlen;
3406 }
3407
3408 return LY_SUCCESS;
3409}
3410
3411/**
3412 * @brief Append all list key predicates to path.
3413 *
3414 * @param[in] node Node with keys to print.
3415 * @param[in,out] buffer Buffer to print to.
3416 * @param[in,out] buflen Current buffer length.
3417 * @param[in,out] bufused Current number of characters used in @p buffer.
3418 * @param[in] is_static Whether buffer is static or can be reallocated.
3419 * @return LY_ERR
3420 */
3421static LY_ERR
3422lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3423{
3424 const struct lyd_node *key;
3425 int dynamic = 0;
3426 size_t len;
3427 const char *val;
3428 char quot;
3429 LY_ERR rc;
3430
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003431 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003432 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
3433 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
3434 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3435 if (rc != LY_SUCCESS) {
3436 if (dynamic) {
3437 free((char *)val);
3438 }
3439 return rc;
3440 }
3441
3442 quot = '\'';
3443 if (strchr(val, '\'')) {
3444 quot = '"';
3445 }
3446 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
3447
3448 if (dynamic) {
3449 free((char *)val);
3450 }
3451 }
3452
3453 return LY_SUCCESS;
3454}
3455
3456/**
3457 * @brief Append leaf-list value predicate to path.
3458 *
3459 * @param[in] node Node to print.
3460 * @param[in,out] buffer Buffer to print to.
3461 * @param[in,out] buflen Current buffer length.
3462 * @param[in,out] bufused Current number of characters used in @p buffer.
3463 * @param[in] is_static Whether buffer is static or can be reallocated.
3464 * @return LY_ERR
3465 */
3466static LY_ERR
3467lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3468{
3469 int dynamic = 0;
3470 size_t len;
3471 const char *val;
3472 char quot;
3473 LY_ERR rc;
3474
3475 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
3476 len = 4 + strlen(val) + 2;
3477 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3478 if (rc != LY_SUCCESS) {
3479 goto cleanup;
3480 }
3481
3482 quot = '\'';
3483 if (strchr(val, '\'')) {
3484 quot = '"';
3485 }
3486 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3487
3488cleanup:
3489 if (dynamic) {
3490 free((char *)val);
3491 }
3492 return rc;
3493}
3494
3495/**
3496 * @brief Append node position (relative to its other instances) predicate to path.
3497 *
3498 * @param[in] node Node to print.
3499 * @param[in,out] buffer Buffer to print to.
3500 * @param[in,out] buflen Current buffer length.
3501 * @param[in,out] bufused Current number of characters used in @p buffer.
3502 * @param[in] is_static Whether buffer is static or can be reallocated.
3503 * @return LY_ERR
3504 */
3505static LY_ERR
3506lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3507{
3508 const struct lyd_node *first, *iter;
3509 size_t len;
3510 int pos;
3511 char *val = NULL;
3512 LY_ERR rc;
3513
3514 if (node->parent) {
3515 first = node->parent->child;
3516 } else {
3517 for (first = node; node->prev->next; node = node->prev);
3518 }
3519 pos = 1;
3520 for (iter = first; iter != node; iter = iter->next) {
3521 if (iter->schema == node->schema) {
3522 ++pos;
3523 }
3524 }
3525 if (asprintf(&val, "%d", pos) == -1) {
3526 return LY_EMEM;
3527 }
3528
3529 len = 1 + strlen(val) + 1;
3530 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3531 if (rc != LY_SUCCESS) {
3532 goto cleanup;
3533 }
3534
3535 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3536
3537cleanup:
3538 free(val);
3539 return rc;
3540}
3541
3542API char *
3543lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3544{
Michal Vasko14654712020-02-06 08:35:21 +01003545 int is_static = 0, i, depth;
3546 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003547 const struct lyd_node *iter;
3548 const struct lys_module *mod;
3549 LY_ERR rc;
3550
3551 LY_CHECK_ARG_RET(NULL, node, NULL);
3552 if (buffer) {
3553 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3554 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003555 } else {
3556 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003557 }
3558
3559 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003560 case LYD_PATH_LOG:
3561 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003562 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3563 ++depth;
3564 }
3565
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003566 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003567 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003568 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003569 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003570iter_print:
3571 /* print prefix and name */
3572 mod = NULL;
3573 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
3574 mod = iter->schema->module;
3575 }
3576
3577 /* realloc string */
3578 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
3579 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3580 if (rc != LY_SUCCESS) {
3581 break;
3582 }
3583
3584 /* print next node */
3585 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
3586
3587 switch (iter->schema->nodetype) {
3588 case LYS_LIST:
3589 if (iter->schema->flags & LYS_KEYLESS) {
3590 /* print its position */
3591 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3592 } else {
3593 /* print all list keys in predicates */
3594 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3595 }
3596 break;
3597 case LYS_LEAFLIST:
3598 if (iter->schema->flags & LYS_CONFIG_W) {
3599 /* print leaf-list value */
3600 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3601 } else {
3602 /* print its position */
3603 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3604 }
3605 break;
3606 default:
3607 /* nothing to print more */
3608 rc = LY_SUCCESS;
3609 break;
3610 }
3611 if (rc != LY_SUCCESS) {
3612 break;
3613 }
3614
Michal Vasko14654712020-02-06 08:35:21 +01003615 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003616 }
3617 break;
3618 }
3619
3620 return buffer;
3621}
Michal Vaskoe444f752020-02-10 12:20:06 +01003622
Michal Vasko9b368d32020-02-14 13:53:31 +01003623LY_ERR
3624lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
3625 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003626{
3627 LY_ERR rc;
3628 const struct lyd_node *node = NULL;
3629 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02003630 struct lyxp_expr *expr = NULL;
3631 uint16_t exp_idx = 0;
3632 struct ly_path_predicate *predicates = NULL;
3633 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01003634 struct lyd_value val = {0};
Michal Vasko00cbf532020-06-15 13:58:47 +02003635 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01003636
Michal Vasko9b368d32020-02-14 13:53:31 +01003637 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003638
3639 if (!first) {
3640 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01003641 if (match) {
3642 *match = NULL;
3643 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003644 return LY_ENOTFOUND;
3645 }
3646
Michal Vaskoe444f752020-02-10 12:20:06 +01003647 if (key_or_value && !val_len) {
3648 val_len = strlen(key_or_value);
3649 }
3650
3651 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01003652 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01003653 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 +01003654 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003655 /* parse keys */
3656 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
3657 LY_PATH_PRED_KEYS, &expr), cleanup);
3658
3659 /* compile them */
3660 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
3661 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003662 }
3663
3664 /* find first matching value */
3665 LY_LIST_FOR(first, node) {
3666 if (node->schema != schema) {
3667 continue;
3668 }
3669
Michal Vasko00cbf532020-06-15 13:58:47 +02003670 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003671 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02003672 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003673 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003674 rc = lyd_find_sibling_val(lyd_node_children(node, 0), predicates[u].key, NULL, 0, (struct lyd_node **)&term);
Michal Vaskoe444f752020-02-10 12:20:06 +01003675 if (rc == LY_ENOTFOUND) {
3676 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01003677 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01003678 }
3679 LY_CHECK_GOTO(rc, cleanup);
3680
3681 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02003682 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003683 break;
3684 }
3685 }
3686
Michal Vasko00cbf532020-06-15 13:58:47 +02003687 if (u < LY_ARRAY_SIZE(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003688 /* not a match */
3689 continue;
3690 }
Michal Vasko90932a92020-02-12 14:33:03 +01003691 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003692 term = (struct lyd_node_term *)node;
3693
3694 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01003695 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003696 /* not a match */
3697 continue;
3698 }
3699 }
3700
3701 /* all criteria passed */
3702 break;
3703 }
3704
3705 if (!node) {
3706 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01003707 if (match) {
3708 *match = NULL;
3709 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003710 goto cleanup;
3711 }
3712
3713 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01003714 if (match) {
3715 *match = (struct lyd_node *)node;
3716 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003717 rc = LY_SUCCESS;
3718
3719cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02003720 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
3721 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01003722 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003723 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01003724 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003725 return rc;
3726}
3727
3728API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01003729lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
3730 const char *key_or_value, size_t val_len, struct lyd_node **match)
3731{
3732 const struct lysc_node *schema;
3733
3734 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
3735
3736 if (!first) {
3737 /* no data */
3738 *match = NULL;
3739 return LY_ENOTFOUND;
3740 }
3741
3742 /* find schema */
3743 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
3744 if (!schema) {
3745 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
3746 return LY_EINVAL;
3747 }
3748
3749 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
3750}
3751
3752API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003753lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3754{
3755 struct lyd_node **match_p;
3756 struct lyd_node_inner *parent;
3757
Michal Vaskof03ed032020-03-04 13:31:44 +01003758 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003759
Michal Vasko62ed12d2020-05-21 10:08:25 +02003760 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3761 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003762 if (match) {
3763 *match = NULL;
3764 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003765 return LY_ENOTFOUND;
3766 }
3767
3768 /* find first sibling */
3769 if (siblings->parent) {
3770 siblings = siblings->parent->child;
3771 } else {
3772 while (siblings->prev->next) {
3773 siblings = siblings->prev;
3774 }
3775 }
3776
3777 parent = (struct lyd_node_inner *)siblings->parent;
3778 if (parent && parent->children_ht) {
3779 assert(target->hash);
3780
3781 /* find by hash */
3782 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3783 siblings = *match_p;
3784 } else {
3785 /* not found */
3786 siblings = NULL;
3787 }
3788 } else {
3789 /* no children hash table */
3790 for (; siblings; siblings = siblings->next) {
3791 if (!lyd_compare(siblings, target, 0)) {
3792 break;
3793 }
3794 }
3795 }
3796
3797 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003798 if (match) {
3799 *match = NULL;
3800 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003801 return LY_ENOTFOUND;
3802 }
3803
Michal Vasko9b368d32020-02-14 13:53:31 +01003804 if (match) {
3805 *match = (struct lyd_node *)siblings;
3806 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003807 return LY_SUCCESS;
3808}
3809
3810API LY_ERR
3811lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3812{
3813 struct lyd_node_inner *parent;
3814 struct lyd_node *match;
3815 struct lyd_node **match_p;
3816 struct ly_set *ret;
3817
3818 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3819
Michal Vasko62ed12d2020-05-21 10:08:25 +02003820 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3821 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003822 return LY_ENOTFOUND;
3823 }
3824
3825 ret = ly_set_new();
3826 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3827
3828 /* find first sibling */
3829 if (siblings->parent) {
3830 siblings = siblings->parent->child;
3831 } else {
3832 while (siblings->prev->next) {
3833 siblings = siblings->prev;
3834 }
3835 }
3836
3837 parent = (struct lyd_node_inner *)siblings->parent;
3838 if (parent && parent->children_ht) {
3839 assert(target->hash);
3840
3841 /* find by hash */
3842 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3843 match = *match_p;
3844 } else {
3845 /* not found */
3846 match = NULL;
3847 }
3848 while (match) {
3849 /* add all found nodes into the return set */
3850 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3851 goto error;
3852 }
3853
3854 /* find next instance */
3855 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3856 match = NULL;
3857 } else {
3858 match = *match_p;
3859 }
3860 }
3861 } else {
3862 /* no children hash table */
3863 for (; siblings; siblings = siblings->next) {
3864 if (!lyd_compare(siblings, target, 0)) {
3865 /* a match */
3866 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3867 goto error;
3868 }
3869 }
3870 }
3871 }
3872
3873 if (!ret->count) {
3874 ly_set_free(ret, NULL);
3875 return LY_ENOTFOUND;
3876 }
3877
3878 *set = ret;
3879 return LY_SUCCESS;
3880
3881error:
3882 ly_set_free(ret, NULL);
3883 return LY_EMEM;
3884}
3885
Michal Vasko90932a92020-02-12 14:33:03 +01003886static int
3887lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3888{
3889 struct lysc_node *val1;
3890 struct lyd_node *val2;
3891
3892 val1 = *((struct lysc_node **)val1_p);
3893 val2 = *((struct lyd_node **)val2_p);
3894
3895 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
3896
3897 if (val1 == val2->schema) {
3898 /* schema match is enough */
3899 return 1;
3900 } else {
3901 return 0;
3902 }
3903}
3904
3905static LY_ERR
3906lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3907{
3908 struct lyd_node **match_p;
3909 struct lyd_node_inner *parent;
3910 uint32_t hash;
3911 values_equal_cb ht_cb;
3912
3913 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3914
3915 /* find first sibling */
3916 if (siblings->parent) {
3917 siblings = siblings->parent->child;
3918 } else {
3919 while (siblings->prev->next) {
3920 siblings = siblings->prev;
3921 }
3922 }
3923
3924 parent = (struct lyd_node_inner *)siblings->parent;
3925 if (parent && parent->children_ht) {
3926 /* calculate our hash */
3927 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3928 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3929 hash = dict_hash_multi(hash, NULL, 0);
3930
3931 /* use special hash table function */
3932 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3933
3934 /* find by hash */
3935 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3936 siblings = *match_p;
3937 } else {
3938 /* not found */
3939 siblings = NULL;
3940 }
3941
3942 /* set the original hash table compare function back */
3943 lyht_set_cb(parent->children_ht, ht_cb);
3944 } else {
3945 /* no children hash table */
3946 for (; siblings; siblings = siblings->next) {
3947 if (siblings->schema == schema) {
3948 /* schema match is enough */
3949 break;
3950 }
3951 }
3952 }
3953
3954 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003955 if (match) {
3956 *match = NULL;
3957 }
Michal Vasko90932a92020-02-12 14:33:03 +01003958 return LY_ENOTFOUND;
3959 }
3960
Michal Vasko9b368d32020-02-14 13:53:31 +01003961 if (match) {
3962 *match = (struct lyd_node *)siblings;
3963 }
Michal Vasko90932a92020-02-12 14:33:03 +01003964 return LY_SUCCESS;
3965}
3966
Michal Vaskoe444f752020-02-10 12:20:06 +01003967API LY_ERR
3968lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3969 size_t val_len, struct lyd_node **match)
3970{
3971 LY_ERR rc;
3972 struct lyd_node *target = NULL;
3973
3974 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003975 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003976 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3977 return LY_EINVAL;
3978 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
3979 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
3980 return LY_EINVAL;
3981 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3982 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3983 lys_nodetype2str(schema->nodetype), __func__);
3984 return LY_EINVAL;
3985 }
3986
Michal Vasko62ed12d2020-05-21 10:08:25 +02003987 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3988 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003989 if (match) {
3990 *match = NULL;
3991 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003992 return LY_ENOTFOUND;
3993 }
3994
Michal Vaskof03ed032020-03-04 13:31:44 +01003995 if (key_or_value && !val_len) {
3996 val_len = strlen(key_or_value);
3997 }
3998
Michal Vasko90932a92020-02-12 14:33:03 +01003999 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01004000 switch (schema->nodetype) {
4001 case LYS_CONTAINER:
4002 case LYS_ANYXML:
4003 case LYS_ANYDATA:
4004 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01004005 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01004006 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01004007 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02004008 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01004009 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004010 break;
4011 case LYS_LEAFLIST:
4012 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02004013 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
4014 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
4015 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01004016 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01004017 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01004018 if (schema->nodetype == LYS_LIST) {
4019 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004020 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004021 }
4022
4023 /* find it */
4024 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004025 break;
4026 default:
4027 /* unreachable */
4028 LOGINT(schema->module->ctx);
4029 return LY_EINT;
4030 }
4031
Michal Vaskoe444f752020-02-10 12:20:06 +01004032 lyd_free_tree(target);
4033 return rc;
4034}
Michal Vaskoccc02342020-05-21 10:09:21 +02004035
4036API LY_ERR
4037lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4038{
4039 LY_ERR ret = LY_SUCCESS;
4040 struct lyxp_set xp_set;
4041 struct lyxp_expr *exp;
4042 uint32_t i;
4043
4044 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4045
4046 memset(&xp_set, 0, sizeof xp_set);
4047
4048 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02004049 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02004050 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
4051
4052 /* evaluate expression */
4053 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
4054 LY_CHECK_GOTO(ret, cleanup);
4055
4056 /* allocate return set */
4057 *set = ly_set_new();
4058 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4059
4060 /* transform into ly_set */
4061 if (xp_set.type == LYXP_SET_NODE_SET) {
4062 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4063 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
4064 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4065 (*set)->size = xp_set.used;
4066
4067 for (i = 0; i < xp_set.used; ++i) {
4068 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
4069 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
4070 }
4071 }
4072 }
4073
4074cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004075 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02004076 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
4077 return ret;
4078}