blob: 9a905ac9a5d808a83514557a8a91978f1cdaed7d [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;
326 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200327 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 break;
329#endif
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:
1821 /* not allowed */
1822 LOGINT(LYD_NODE_CTX(node1));
1823 return LY_EINT;
1824 }
1825 if (options & LYD_COMPARE_FULL_RECURSION) {
1826 iter1 = opaq1->child;
1827 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001828 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001829 }
1830 return LY_SUCCESS;
1831 } else {
1832 switch (node1->schema->nodetype) {
1833 case LYS_LEAF:
1834 case LYS_LEAFLIST:
1835 if (options & LYD_COMPARE_DEFAULTS) {
1836 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1837 return LY_ENOT;
1838 }
1839 }
1840
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001841 term1 = (struct lyd_node_term *)node1;
1842 term2 = (struct lyd_node_term *)node2;
1843 if (term1->value.realtype != term2->value.realtype) {
1844 return LY_ENOT;
1845 }
Michal Vasko52927e22020-03-16 17:26:14 +01001846
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001847 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001848 case LYS_CONTAINER:
1849 if (options & LYD_COMPARE_DEFAULTS) {
1850 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1851 return LY_ENOT;
1852 }
1853 }
1854 if (options & LYD_COMPARE_FULL_RECURSION) {
1855 iter1 = ((struct lyd_node_inner*)node1)->child;
1856 iter2 = ((struct lyd_node_inner*)node2)->child;
1857 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001858 }
1859 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001860 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001861 case LYS_ACTION:
1862 if (options & LYD_COMPARE_FULL_RECURSION) {
1863 /* TODO action/RPC
1864 goto all_children_compare;
1865 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001866 }
1867 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001868 case LYS_NOTIF:
1869 if (options & LYD_COMPARE_FULL_RECURSION) {
1870 /* TODO Notification
1871 goto all_children_compare;
1872 */
1873 }
1874 return LY_SUCCESS;
1875 case LYS_LIST:
1876 iter1 = ((struct lyd_node_inner*)node1)->child;
1877 iter2 = ((struct lyd_node_inner*)node2)->child;
1878
1879 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1880 /* lists with keys, their equivalence is based on their keys */
1881 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001882 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001883 key = key->next) {
1884 if (lyd_compare(iter1, iter2, options)) {
1885 return LY_ENOT;
1886 }
1887 iter1 = iter1->next;
1888 iter2 = iter2->next;
1889 }
1890 } else {
1891 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1892
1893 all_children_compare:
1894 if (!iter1 && !iter2) {
1895 /* no children, nothing to compare */
1896 return LY_SUCCESS;
1897 }
1898
1899 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1900 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1901 return LY_ENOT;
1902 }
1903 }
1904 if (iter1 || iter2) {
1905 return LY_ENOT;
1906 }
1907 }
1908 return LY_SUCCESS;
1909 case LYS_ANYXML:
1910 case LYS_ANYDATA:
1911 any1 = (struct lyd_node_any*)node1;
1912 any2 = (struct lyd_node_any*)node2;
1913
1914 if (any1->value_type != any2->value_type) {
1915 return LY_ENOT;
1916 }
1917 switch (any1->value_type) {
1918 case LYD_ANYDATA_DATATREE:
1919 iter1 = any1->value.tree;
1920 iter2 = any2->value.tree;
1921 goto all_children_compare;
1922 case LYD_ANYDATA_STRING:
1923 case LYD_ANYDATA_XML:
1924 case LYD_ANYDATA_JSON:
1925 len1 = strlen(any1->value.str);
1926 len2 = strlen(any2->value.str);
1927 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1928 return LY_ENOT;
1929 }
1930 return LY_SUCCESS;
1931 #if 0 /* TODO LYB format */
1932 case LYD_ANYDATA_LYB:
1933 int len1 = lyd_lyb_data_length(any1->value.mem);
1934 int len2 = lyd_lyb_data_length(any2->value.mem);
1935 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1936 return LY_ENOT;
1937 }
1938 return LY_SUCCESS;
1939 #endif
1940 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001941 }
1942 }
1943
Michal Vasko52927e22020-03-16 17:26:14 +01001944 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001945 return LY_EINT;
1946}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001947
Michal Vasko21725742020-06-29 11:49:25 +02001948API LY_ERR
1949lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1950{
1951 if (!meta1 || !meta2) {
1952 if (meta1 == meta2) {
1953 return LY_SUCCESS;
1954 } else {
1955 return LY_ENOT;
1956 }
1957 }
1958
1959 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
1960 return LY_ENOT;
1961 }
1962
1963 if (meta1->value.realtype != meta2->value.realtype) {
1964 return LY_ENOT;
1965 }
1966
1967 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1968}
1969
Radek Krejci22ebdba2019-07-25 13:59:43 +02001970/**
Michal Vasko52927e22020-03-16 17:26:14 +01001971 * @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 +02001972 *
1973 * 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 +02001974 *
1975 * @param[in] node Original node to duplicate
1976 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1977 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1978 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1979 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1980 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001981 */
Michal Vasko52927e22020-03-16 17:26:14 +01001982static LY_ERR
1983lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1984 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001985{
Michal Vasko52927e22020-03-16 17:26:14 +01001986 LY_ERR ret;
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 }
2111 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;
2121 }
2122 }
2123
Michal Vasko52927e22020-03-16 17:26:14 +01002124 /* insert */
2125 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002126
2127 if (dup_p) {
2128 *dup_p = dup;
2129 }
2130 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002131
2132error:
Michal Vasko52927e22020-03-16 17:26:14 +01002133 lyd_free_tree(dup);
2134 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002135}
2136
2137API struct lyd_node *
2138lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
2139{
2140 struct ly_ctx *ctx;
2141 const struct lyd_node *orig; /* original node to be duplicated */
2142 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002143 struct lyd_node *top = NULL; /* the most higher created node */
2144 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
2145 int keyless_parent_list = 0;
2146
2147 LY_CHECK_ARG_RET(NULL, node, NULL);
2148 ctx = node->schema->module->ctx;
2149
2150 if (options & LYD_DUP_WITH_PARENTS) {
2151 struct lyd_node_inner *orig_parent, *iter;
2152 int repeat = 1;
2153 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2154 if (parent && parent->schema == orig_parent->schema) {
2155 /* stop creating parents, connect what we have into the provided parent */
2156 iter = parent;
2157 repeat = 0;
2158 /* get know if there is a keyless list which we will have to rehash */
2159 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002160 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002161 keyless_parent_list = 1;
2162 break;
2163 }
2164 }
2165 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01002166 iter = NULL;
2167 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2168 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002169 }
2170 if (!local_parent) {
2171 local_parent = iter;
2172 }
2173 if (iter->child) {
2174 /* 1) list - add after keys
2175 * 2) provided parent with some children */
2176 iter->child->prev->next = top;
2177 if (top) {
2178 top->prev = iter->child->prev;
2179 iter->child->prev = top;
2180 }
2181 } else {
2182 iter->child = top;
2183 if (iter->schema->nodetype == LYS_LIST) {
2184 /* keyless list - we will need to rehash it since we are going to add nodes into it */
2185 keyless_parent_list = 1;
2186 }
2187 }
2188 if (top) {
2189 top->parent = iter;
2190 }
2191 top = (struct lyd_node*)iter;
2192 }
2193 if (repeat && parent) {
2194 /* given parent and created parents chain actually do not interconnect */
2195 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2196 goto error;
2197 }
2198 } else {
2199 local_parent = parent;
2200 }
2201
Radek Krejci22ebdba2019-07-25 13:59:43 +02002202 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002203 /* if there is no local parent, it will be inserted into first */
2204 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 +02002205 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
2206 break;
2207 }
2208 }
2209 if (keyless_parent_list) {
2210 /* rehash */
2211 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002212 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002213 lyd_hash((struct lyd_node*)local_parent);
2214 }
2215 }
2216 }
2217 return first;
2218
2219error:
2220 if (top) {
2221 lyd_free_tree(top);
2222 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002223 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002224 }
2225 return NULL;
2226}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002227
Michal Vasko4490d312020-06-16 13:08:55 +02002228/**
2229 * @brief Merge a source sibling into target siblings.
2230 *
2231 * @param[in,out] first_trg First target sibling, is updated if top-level.
2232 * @param[in] parent_trg Target parent.
2233 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2234 * @param[in] options Merge options.
2235 * @return LY_ERR value.
2236 */
2237static LY_ERR
2238lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2239 int options)
2240{
2241 LY_ERR ret;
2242 const struct lyd_node *child_src, *tmp, *sibling_src;
2243 struct lyd_node *match_trg, *dup_src, *next, *elem;
2244 struct lysc_type *type;
2245 LYD_ANYDATA_VALUETYPE tmp_val_type;
2246 union lyd_any_value tmp_val;
2247
2248 sibling_src = *sibling_src_p;
2249 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2250 /* try to find the exact instance */
2251 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2252 } else {
2253 /* try to simply find the node, there cannot be more instances */
2254 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2255 }
2256
2257 if (!ret) {
2258 /* node found, make sure even value matches for all node types */
2259 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2260 /* since they are different, they cannot both be default */
2261 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2262
2263 /* update value (or only LYD_DEFAULT flag) only if no flag set or the source node is not default */
2264 if (!(options & LYD_MERGE_EXPLICIT) || !(sibling_src->flags & LYD_DEFAULT)) {
2265 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2266 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2267 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2268 &((struct lyd_node_term *)match_trg)->value));
2269
2270 /* copy flags and add LYD_NEW */
2271 match_trg->flags = sibling_src->flags | LYD_NEW;
2272 }
2273 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2274 if (options & LYD_MERGE_DESTRUCT) {
2275 dup_src = (struct lyd_node *)sibling_src;
2276 lyd_unlink_tree(dup_src);
2277 /* spend it */
2278 *sibling_src_p = NULL;
2279 } else {
2280 dup_src = lyd_dup(sibling_src, NULL, 0);
2281 LY_CHECK_RET(!dup_src, LY_EMEM);
2282 }
2283 /* just switch values */
2284 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2285 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2286 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2287 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2288 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2289 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2290
2291 /* copy flags and add LYD_NEW */
2292 match_trg->flags = sibling_src->flags | LYD_NEW;
2293
2294 /* dup_src is not needed, actually */
2295 lyd_free_tree(dup_src);
2296 } else {
2297 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002298 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002299 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2300 }
2301 }
2302 } else {
2303 /* node not found, merge it */
2304 if (options & LYD_MERGE_DESTRUCT) {
2305 dup_src = (struct lyd_node *)sibling_src;
2306 lyd_unlink_tree(dup_src);
2307 /* spend it */
2308 *sibling_src_p = NULL;
2309 } else {
2310 dup_src = lyd_dup(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS);
2311 LY_CHECK_RET(!dup_src, LY_EMEM);
2312 }
2313
2314 /* set LYD_NEW for all the new nodes, required for validation */
2315 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2316 elem->flags |= LYD_NEW;
2317 LYD_TREE_DFS_END(dup_src, next, elem);
2318 }
2319
2320 lyd_insert_node(parent_trg, first_trg, dup_src);
2321 }
2322
2323 return LY_SUCCESS;
2324}
2325
2326API LY_ERR
2327lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options)
2328{
2329 const struct lyd_node *sibling_src, *tmp;
2330 int first;
2331
2332 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2333
2334 if (!source) {
2335 /* nothing to merge */
2336 return LY_SUCCESS;
2337 }
2338
2339 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2340 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2341 return LY_EINVAL;
2342 }
2343
2344 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2345 first = sibling_src == source ? 1 : 0;
2346 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2347 if (first && !sibling_src) {
2348 /* source was spent (unlinked), move to the next node */
2349 source = tmp;
2350 }
2351
2352 if (options & LYD_MERGE_NOSIBLINGS) {
2353 break;
2354 }
2355 }
2356
2357 if (options & LYD_MERGE_DESTRUCT) {
2358 /* free any leftover source data that were not merged */
2359 lyd_free_siblings((struct lyd_node *)source);
2360 }
2361
2362 return LY_SUCCESS;
2363}
2364
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002365struct lyd_diff_userord {
2366 const struct lysc_node *schema; /**< User-ordered list/leaf-list schema node. */
2367 uint64_t pos; /**< Current position in the second tree. */
2368 const struct lyd_node **inst; /**< Sized array of current instance order. */
2369};
2370
2371enum lyd_diff_op {
2372 LYD_DIFF_OP_CREATE, /**< Subtree created. */
2373 LYD_DIFF_OP_DELETE, /**< Subtree deleted. */
2374 LYD_DIFF_OP_REPLACE, /**< Node value changed or (leaf-)list instance moved. */
2375 LYD_DIFF_OP_NONE, /**< No change of an existing inner node or default flag change of a term node. */
2376};
2377
2378static const char *
2379lyd_diff_op2str(enum lyd_diff_op op)
2380{
2381 switch (op) {
2382 case LYD_DIFF_OP_CREATE:
2383 return "create";
2384 case LYD_DIFF_OP_DELETE:
2385 return "delete";
2386 case LYD_DIFF_OP_REPLACE:
2387 return "replace";
2388 case LYD_DIFF_OP_NONE:
2389 return "none";
2390 }
2391
2392 LOGINT(NULL);
2393 return NULL;
2394}
2395
2396/**
2397 * @brief Add a new change into diff.
2398 *
2399 * @param[in] node Node (subtree) to add into diff.
2400 * @param[in] op Operation to set.
2401 * @param[in] orig_default Original default metadata to set.
2402 * @param[in] orig_value Original value metadata to set.
2403 * @param[in] key Key metadata to set.
2404 * @param[in] value Value metadata to set.
2405 * @param[in] orig_key Original key metadata to set.
2406 * @param[in,out] diff Diff to append to.
2407 * @return LY_ERR value.
2408 */
2409static LY_ERR
2410lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
2411 const char *key, const char *value, const char *orig_key, struct lyd_node **diff)
2412{
2413 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
2414 const struct lyd_node *parent = NULL;
2415 const struct lys_module *yang_mod;
2416
2417 /* find the first existing parent */
2418 siblings = *diff;
2419 while (1) {
2420 /* find next node parent */
2421 parent = node;
2422 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
2423 parent = (struct lyd_node *)parent->parent;
2424 }
2425 if (parent == node) {
2426 /* no more parents to find */
2427 break;
2428 }
2429
2430 /* check whether it exists in the diff */
2431 if (lyd_find_sibling_first(siblings, parent, &match)) {
2432 break;
2433 }
2434
2435 /* another parent found */
2436 diff_parent = match;
2437
2438 /* move down in the diff */
2439 siblings = LYD_CHILD(match);
2440 }
2441
2442 /* duplicate the subtree (and connect to the diff if possible) */
2443 dup = lyd_dup(node, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS);
2444 LY_CHECK_RET(!dup, LY_EMEM);
2445
2446 /* find the first duplicated parent */
2447 if (!diff_parent) {
2448 diff_parent = (struct lyd_node *)dup->parent;
2449 while (diff_parent && diff_parent->parent) {
2450 diff_parent = (struct lyd_node *)diff_parent->parent;
2451 }
2452 } else {
2453 diff_parent = (struct lyd_node *)dup;
2454 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
2455 diff_parent = (struct lyd_node *)diff_parent->parent;
2456 }
2457 }
2458
2459 /* no parent existed, must be manually connected */
2460 if (!diff_parent) {
2461 /* there actually was no parent to duplicate */
2462 if (*diff) {
2463 lyd_insert_sibling(*diff, dup);
2464 } else {
2465 *diff = dup;
2466 }
2467 } else if (!diff_parent->parent) {
2468 if (*diff) {
2469 lyd_insert_sibling(*diff, diff_parent);
2470 } else {
2471 *diff = diff_parent;
2472 }
2473 }
2474
2475 /* get module with the operation metadata */
2476 yang_mod = LYD_NODE_CTX(node)->list.objs[1];
2477 assert(!strcmp(yang_mod->name, "yang"));
2478
2479 /* add parent operation, if any */
2480 if (diff_parent && (diff_parent != dup) && !lyd_new_meta(diff_parent, yang_mod, "operation", "none")) {
2481 return LY_EMEM;
2482 }
2483
2484 /* add subtree operation */
2485 if (!lyd_new_meta(dup, yang_mod, "operation", lyd_diff_op2str(op))) {
2486 return LY_EMEM;
2487 }
2488
2489 /* orig-default */
2490 if (orig_default && !lyd_new_meta(dup, yang_mod, "orig-default", orig_default)) {
2491 return LY_EMEM;
2492 }
2493
2494 /* orig-value */
2495 if (orig_value && !lyd_new_meta(dup, yang_mod, "orig-value", orig_value)) {
2496 return LY_EMEM;
2497 }
2498
2499 /* key */
2500 if (key && !lyd_new_meta(dup, yang_mod, "key", key)) {
2501 return LY_EMEM;
2502 }
2503
2504 /* value */
2505 if (value && !lyd_new_meta(dup, yang_mod, "value", value)) {
2506 return LY_EMEM;
2507 }
2508
2509 /* orig-key */
2510 if (orig_key && !lyd_new_meta(dup, yang_mod, "orig-key", orig_key)) {
2511 return LY_EMEM;
2512 }
2513
2514 return LY_SUCCESS;
2515}
2516
2517/**
2518 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
2519 *
2520 * @param[in] first
2521 * @param[in] schema Schema node of the list/leaf-list.
2522 * @param[in,out] userord Sized array of userord items.
2523 * @return Userord item for all the user-ordered list/leaf-list instances.
2524 */
2525static struct lyd_diff_userord *
2526lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
2527{
2528 struct lyd_diff_userord *item;
2529 const struct lyd_node *iter, **node;
2530 LY_ARRAY_SIZE_TYPE u;
2531
2532 LY_ARRAY_FOR(*userord, u) {
2533 if ((*userord)[u].schema == schema) {
2534 return &(*userord)[u];
2535 }
2536 }
2537
2538 /* it was not added yet, add it now */
2539 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
2540
2541 item->schema = schema;
2542 item->pos = 0;
2543 item->inst = NULL;
2544
2545 /* store all the instance pointers in the current order */
2546 if (first) {
2547 if (first->parent) {
2548 iter = first->parent->child;
2549 } else {
2550 for (iter = first; iter->prev->next; iter = iter->prev);
2551 }
2552 for (; iter; iter = iter->next) {
2553 if (iter->schema == first->schema) {
2554 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
2555 *node = iter;
2556 }
2557 }
2558 }
2559
2560 return item;
2561}
2562
2563/**
2564 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
2565 * lists/leaf-lists.
2566 *
2567 * @param[in] first Node from the first tree, can be NULL (on create).
2568 * @param[in] second Node from the second tree, can be NULL (on delete).
2569 * @param[in] options Diff options.
2570 * @param[in,out] userord Sized array of userord items for keeping the current node order.
2571 * @param[out] op Operation.
2572 * @param[out] orig_default Original default metadata.
2573 * @param[out] value Value metadata.
2574 * @param[out] orig_value Original value metadata
2575 * @param[out] key Key metadata.
2576 * @param[out] orig_key Original key metadata.
2577 * @return LY_SUCCESS on success,
2578 * @return LY_ENOT if there is no change to be added into diff,
2579 * @return LY_ERR value on other errors.
2580 */
2581static LY_ERR
2582lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, int options,
2583 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
2584 char **orig_value, char **key, char **orig_key)
2585{
2586 const struct lysc_node *schema;
2587 int dynamic;
2588 size_t buflen, bufused, first_pos, second_pos;
2589 struct lyd_diff_userord *userord_item;
2590
2591 assert(first || second);
2592
2593 *orig_default = NULL;
2594 *value = NULL;
2595 *orig_value = NULL;
2596 *key = NULL;
2597 *orig_key = NULL;
2598
2599 schema = first ? first->schema : second->schema;
2600 assert(lysc_is_userordered(schema));
2601
2602 /* get userord entry */
2603 userord_item = lyd_diff_userord_get(first, schema, userord);
2604 LY_CHECK_RET(!userord_item, LY_EMEM);
2605
2606 /* prepare position of the next instance */
2607 second_pos = userord_item->pos++;
2608
2609 /* find user-ordered first position */
2610 if (first) {
2611 for (first_pos = second_pos; first_pos < LY_ARRAY_SIZE(userord_item->inst); ++first_pos) {
2612 if (userord_item->inst[first_pos] == first) {
2613 break;
2614 }
2615 }
2616 assert(first_pos < LY_ARRAY_SIZE(userord_item->inst));
2617 }
2618
2619 /* learn operation first */
2620 if (!second) {
2621 *op = LYD_DIFF_OP_DELETE;
2622 } else if (!first) {
2623 *op = LYD_DIFF_OP_CREATE;
2624 } else {
2625 assert(schema->nodetype & (LYS_LIST | LYS_LEAFLIST));
2626 if (lyd_compare(second, userord_item->inst[second_pos], 0)) {
2627 /* in first, there is a different instance on the second position, we are going to move 'first' node */
2628 *op = LYD_DIFF_OP_REPLACE;
2629 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2630 /* default flag change */
2631 *op = LYD_DIFF_OP_NONE;
2632 } else {
2633 /* no changes */
2634 return LY_ENOT;
2635 }
2636 }
2637
2638 /*
2639 * set each attribute correctly based on the operation and node type
2640 */
2641
2642 /* orig-default */
2643 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype == LYS_LEAFLIST)
2644 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2645 if (first->flags & LYD_DEFAULT) {
2646 *orig_default = "true";
2647 } else {
2648 *orig_default = "false";
2649 }
2650 }
2651
2652 /* value */
2653 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
2654 if (second_pos) {
2655 *value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[second_pos - 1], &dynamic);
2656 if (!dynamic) {
2657 *value = strdup(*value);
2658 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2659 }
2660 } else {
2661 *value = strdup("");
2662 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2663 }
2664 }
2665
2666 /* orig-value */
2667 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2668 if (first_pos) {
2669 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[first_pos - 1], &dynamic);
2670 if (!dynamic) {
2671 *orig_value = strdup(*orig_value);
2672 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2673 }
2674 } else {
2675 *orig_value = strdup("");
2676 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2677 }
2678 }
2679
2680 /* key */
2681 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op ==LYD_DIFF_OP_CREATE))) {
2682 if (second_pos) {
2683 buflen = bufused = 0;
2684 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
2685 } else {
2686 *key = strdup("");
2687 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
2688 }
2689 }
2690
2691 /* orig-key */
2692 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2693 if (first_pos) {
2694 buflen = bufused = 0;
2695 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
2696 } else {
2697 *orig_key = strdup("");
2698 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
2699 }
2700 }
2701
2702 /*
2703 * update our instances - apply the change
2704 */
2705 if (*op == LYD_DIFF_OP_CREATE) {
2706 /* insert the instance */
2707 LY_ARRAY_RESIZE_ERR_RET(schema->module->ctx, userord_item->inst, LY_ARRAY_SIZE(userord_item->inst) + 1,
2708 ;, LY_EMEM);
2709 if (second_pos < LY_ARRAY_SIZE(userord_item->inst)) {
2710 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2711 (LY_ARRAY_SIZE(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
2712 }
2713 LY_ARRAY_INCREMENT(userord_item->inst);
2714 userord_item->inst[second_pos] = second;
2715
2716 } else if (*op == LYD_DIFF_OP_DELETE) {
2717 /* remove the instance */
2718 if (first_pos + 1 < LY_ARRAY_SIZE(userord_item->inst)) {
2719 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
2720 (LY_ARRAY_SIZE(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
2721 }
2722 LY_ARRAY_DECREMENT(userord_item->inst);
2723
2724 } else if (*op == LYD_DIFF_OP_REPLACE) {
2725 /* move the instances */
2726 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2727 (first_pos - second_pos) * sizeof *userord_item->inst);
2728 userord_item->inst[second_pos] = first;
2729 }
2730
2731 return LY_SUCCESS;
2732}
2733
2734/**
2735 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
2736 * lists/leaf-lists.
2737 *
2738 * @param[in] first Node from the first tree, can be NULL (on create).
2739 * @param[in] second Node from the second tree, can be NULL (on delete).
2740 * @param[in] options Diff options.
2741 * @param[out] op Operation.
2742 * @param[out] orig_default Original default metadata.
2743 * @param[out] orig_value Original value metadata.
2744 * @return LY_SUCCESS on success,
2745 * @return LY_ENOT if there is no change to be added into diff,
2746 * @return LY_ERR value on other errors.
2747 */
2748static LY_ERR
2749lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, int options, enum lyd_diff_op *op,
2750 const char **orig_default, char **orig_value)
2751{
2752 const struct lysc_node *schema;
2753 int dynamic;
2754
2755 assert(first || second);
2756
2757 *orig_default = NULL;
2758 *orig_value = NULL;
2759
2760 schema = first ? first->schema : second->schema;
2761 assert(!lysc_is_userordered(schema));
2762
2763 /* learn operation first */
2764 if (!second) {
2765 *op = LYD_DIFF_OP_DELETE;
2766 } else if (!first) {
2767 *op = LYD_DIFF_OP_CREATE;
2768 } else {
2769 switch (schema->nodetype) {
2770 case LYS_CONTAINER:
2771 case LYS_RPC:
2772 case LYS_ACTION:
2773 case LYS_NOTIF:
2774 /* no changes */
2775 return LY_ENOT;
2776 case LYS_LIST:
2777 case LYS_LEAFLIST:
2778 if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2779 /* default flag change */
2780 *op = LYD_DIFF_OP_NONE;
2781 } else {
2782 /* no changes */
2783 return LY_ENOT;
2784 }
2785 break;
2786 case LYS_LEAF:
2787 case LYS_ANYXML:
2788 case LYS_ANYDATA:
2789 if (lyd_compare(first, second, 0)) {
2790 /* different values */
2791 *op = LYD_DIFF_OP_REPLACE;
2792 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2793 /* default flag change */
2794 *op = LYD_DIFF_OP_NONE;
2795 } else {
2796 /* no changes */
2797 return LY_ENOT;
2798 }
2799 break;
2800 default:
2801 LOGINT_RET(schema->module->ctx);
2802 }
2803 }
2804
2805 /*
2806 * set each attribute correctly based on the operation and node type
2807 */
2808
2809 /* orig-default */
2810 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype & LYD_NODE_TERM)
2811 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2812 if (first->flags & LYD_DEFAULT) {
2813 *orig_default = "true";
2814 } else {
2815 *orig_default = "false";
2816 }
2817 }
2818
2819 /* orig-value */
2820 if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
2821 /* leaf */
2822 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)first, &dynamic);
2823 if (!dynamic) {
2824 *orig_value = strdup(*orig_value);
2825 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2826 }
2827 }
2828
2829 return LY_SUCCESS;
2830}
2831
2832/**
2833 * @brief Perform diff for all siblings at certain depth, recursively.
2834 *
2835 * For user-ordered lists/leaf-lists a specific structure is used for storing
2836 * the current order. The idea is to apply all the generated diff changes
2837 * virtually on the first tree so that we can continue to generate correct
2838 * changes after some were already generated.
2839 *
2840 * The algorithm then uses second tree position-based changes with a before
2841 * (preceding) item anchor.
2842 *
2843 * Example:
2844 *
2845 * Virtual first tree leaf-list order:
2846 * 1 2 [3] 4 5
2847 *
2848 * Second tree leaf-list order:
2849 * 1 2 [5] 3 4
2850 *
2851 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
2852 * match - they do not - move nodes so that the 3rd position node is final ->
2853 * -> move node 5 to the 3rd position -> move node 5 after node 2.
2854 *
2855 * Required properties:
2856 * Stored operations (move) should not be affected by later operations -
2857 * - would cause a redundantly long list of operations, possibly inifinite.
2858 *
2859 * Implemenation justification:
2860 * First, all delete operations and only then move/create operations are stored.
2861 * Also, preceding anchor is used and after each iteration another node is
2862 * at its final position. That results in the invariant that all preceding
2863 * nodes are final and will not be changed by the later operations, meaning
2864 * they can safely be used as anchors for the later operations.
2865 *
2866 * @param[in] first First tree first sibling.
2867 * @param[in] second Second tree first sibling.
2868 * @param[in] options Diff options.
2869 * @param[in,out] diff Diff to append to.
2870 * @return LY_ERR value.
2871 */
2872static LY_ERR
2873lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
2874{
2875 LY_ERR ret = LY_SUCCESS;
2876 const struct lyd_node *iter_first, *iter_second;
2877 struct lyd_node *match_second, *match_first;
2878 int nosiblings = 0;
2879 struct lyd_diff_userord *userord = NULL;
2880 LY_ARRAY_SIZE_TYPE u;
2881 enum lyd_diff_op op;
2882 const char *orig_default;
2883 char *orig_value, *key, *value, *orig_key;
2884
2885 if (options & LYD_DIFF_NOSIBLINGS) {
2886 /* remember it for this function call only, should not be passed recursively */
2887 nosiblings = 1;
2888 options &= ~LYD_DIFF_NOSIBLINGS;
2889 }
2890
2891 /* compare first tree to the second tree - delete, replace, none */
2892 LY_LIST_FOR(first, iter_first) {
2893 assert(!(iter_first->schema->flags & LYS_KEY));
2894 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2895 /* skip default nodes */
2896 continue;
2897 }
2898
2899 if (iter_first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2900 /* try to find the exact instance */
2901 lyd_find_sibling_first(second, iter_first, &match_second);
2902 } else {
2903 /* try to simply find the node, there cannot be more instances */
2904 lyd_find_sibling_val(second, iter_first->schema, NULL, 0, &match_second);
2905 }
2906
2907 if (match_second && (match_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2908 /* ignore default nodes */
2909 match_second = NULL;
2910 }
2911
2912 if (lysc_is_userordered(iter_first->schema)) {
2913 if (match_second) {
2914 /* we are handling only user-ordered node delete now */
2915 continue;
2916 }
2917
2918 /* get all the attributes */
2919 LY_CHECK_GOTO(lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
2920 &value, &orig_value, &key, &orig_key), cleanup);
2921
2922 /* there must be changes, it is deleted */
2923 assert(op == LYD_DIFF_OP_DELETE);
2924 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, orig_key, diff);
2925
2926 free(orig_value);
2927 free(key);
2928 free(value);
2929 free(orig_key);
2930 LY_CHECK_GOTO(ret, cleanup);
2931 } else {
2932 /* get all the attributes */
2933 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
2934
2935 /* add into diff if there are any changes */
2936 if (!ret) {
2937 if (op == LYD_DIFF_OP_DELETE) {
2938 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2939 } else {
2940 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2941 }
2942
2943 free(orig_value);
2944 LY_CHECK_GOTO(ret, cleanup);
2945 } else if (ret == LY_ENOT) {
2946 ret = LY_SUCCESS;
2947 } else {
2948 goto cleanup;
2949 }
2950 }
2951
2952 /* check descendants, if any, recursively */
2953 if (match_second) {
2954 LY_CHECK_GOTO(lyd_diff_siblings_r(LYD_CHILD(iter_first), LYD_CHILD(match_second), options, diff), cleanup);
2955 }
2956
2957 if (nosiblings) {
2958 break;
2959 }
2960 }
2961
2962 /* reset all cached positions */
2963 LY_ARRAY_FOR(userord, u) {
2964 userord[u].pos = 0;
2965 }
2966
2967 /* compare second tree to the first tree - create, user-ordered move */
2968 LY_LIST_FOR(second, iter_second) {
2969 assert(!(iter_second->schema->flags & LYS_KEY));
2970 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2971 /* skip default nodes */
2972 continue;
2973 }
2974
2975 if (iter_second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2976 lyd_find_sibling_first(first, iter_second, &match_first);
2977 } else {
2978 lyd_find_sibling_val(first, iter_second->schema, NULL, 0, &match_first);
2979 }
2980
2981 if (match_first && (match_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2982 /* ignore default nodes */
2983 match_first = NULL;
2984 }
2985
2986 if (lysc_is_userordered(iter_second->schema)) {
2987 /* get all the attributes */
2988 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
2989 &value, &orig_value, &key, &orig_key);
2990
2991 /* add into diff if there are any changes */
2992 if (!ret) {
2993 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, orig_key, diff);
2994
2995 free(orig_value);
2996 free(key);
2997 free(value);
2998 free(orig_key);
2999 LY_CHECK_GOTO(ret, cleanup);
3000 } else if (ret == LY_ENOT) {
3001 ret = LY_SUCCESS;
3002 } else {
3003 goto cleanup;
3004 }
3005 } else if (!match_first) {
3006 /* get all the attributes */
3007 LY_CHECK_GOTO(lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
3008
3009 /* there must be changes, it is created */
3010 assert(op == LYD_DIFF_OP_CREATE);
3011 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
3012
3013 free(orig_value);
3014 LY_CHECK_GOTO(ret, cleanup);
3015 } /* else was handled */
3016
3017 if (nosiblings) {
3018 break;
3019 }
3020 }
3021
3022cleanup:
3023 LY_ARRAY_FOR(userord, u) {
3024 LY_ARRAY_FREE(userord[u].inst);
3025 }
3026 LY_ARRAY_FREE(userord);
3027 return ret;
3028}
3029
3030API LY_ERR
3031lyd_diff(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
3032{
3033 const struct ly_ctx *ctx;
3034
3035 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
3036
3037 if (first) {
3038 ctx = LYD_NODE_CTX(first);
3039 } else if (second) {
3040 ctx = LYD_NODE_CTX(second);
3041 } else {
3042 ctx = NULL;
3043 }
3044
3045 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
3046 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
3047 return LY_EINVAL;
3048 }
3049
3050 *diff = NULL;
3051
3052 return lyd_diff_siblings_r(first, second, options, diff);
3053}
3054
3055/**
3056 * @brief Find a matching node in data tree for a diff node.
3057 *
3058 * @param[in] first_node First sibling in the data tree.
3059 * @param[in] diff_node Diff node to match.
3060 * @param[out] match_p Matching node.
3061 * @return LY_ERR value.
3062 */
3063static LY_ERR
3064lyd_diff_find_node(const struct lyd_node *first_node, const struct lyd_node *diff_node, struct lyd_node **match_p)
3065{
3066 if (diff_node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3067 /* try to find the exact instance */
3068 lyd_find_sibling_first(first_node, diff_node, match_p);
3069 } else {
3070 /* try to simply find the node, there cannot be more instances */
3071 lyd_find_sibling_val(first_node, diff_node->schema, NULL, 0, match_p);
3072 }
3073 LY_CHECK_ERR_RET(!*match_p, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3074
3075 return LY_SUCCESS;
3076}
3077
3078/**
3079 * @brief Learn operation of a diff node.
3080 *
3081 * @param[in] diff_node Diff node.
3082 * @param[out] op Operation.
3083 * @param[out] key_or_value Optional list instance keys predicate or leaf-list value for move operation.
3084 * @return LY_ERR value.
3085 */
3086static LY_ERR
3087lyd_diff_get_op(const struct lyd_node *diff_node, const char **op, const char **key_or_value)
3088{
3089 struct lyd_meta *meta = NULL;
3090 const struct lyd_node *diff_parent;
3091 const char *meta_name, *str;
3092 int dynamic;
3093
3094 for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
3095 LY_LIST_FOR(diff_parent->meta, meta) {
3096 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
3097 str = lyd_meta2str(meta, &dynamic);
3098 assert(!dynamic);
3099 if ((str[0] == 'r') && (diff_parent != diff_node)) {
3100 /* we do not care about this operation if it's in our parent */
3101 continue;
3102 }
3103 *op = str;
3104 break;
3105 }
3106 }
3107 if (meta) {
3108 break;
3109 }
3110 }
3111 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3112
3113 *key_or_value = NULL;
3114 if (lysc_is_userordered(diff_node->schema) && (((*op)[0] == 'c') || ((*op)[0] == 'r'))) {
3115 if (diff_node->schema->nodetype == LYS_LIST) {
3116 meta_name = "key";
3117 } else {
3118 meta_name = "value";
3119 }
3120
3121 LY_LIST_FOR(diff_node->meta, meta) {
3122 if (!strcmp(meta->name, meta_name) && !strcmp(meta->annotation->module->name, "yang")) {
3123 str = lyd_meta2str(meta, &dynamic);
3124 assert(!dynamic);
3125 *key_or_value = str;
3126 break;
3127 }
3128 }
3129 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3130 }
3131
3132 return LY_SUCCESS;
3133}
3134
3135/**
3136 * @brief Insert a diff node into a data tree.
3137 *
3138 * @param[in,out] first_node First sibling of the data tree.
3139 * @param[in] parent_node Data tree sibling parent node.
3140 * @param[in] new_node Node to insert.
3141 * @param[in] keys_or_value Optional predicate of relative (leaf-)list instance. If not set, the user-ordered
3142 * instance will be inserted at the first position.
3143 * @return err_info, NULL on success.
3144 */
3145static LY_ERR
3146lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
3147 const char *key_or_value)
3148{
3149 LY_ERR ret;
3150 struct lyd_node *anchor;
3151
3152 assert(new_node);
3153
3154 if (!*first_node) {
3155 if (!parent_node) {
3156 /* no parent or siblings */
3157 *first_node = new_node;
3158 return LY_SUCCESS;
3159 }
3160
3161 /* simply insert into parent, no other children */
3162 if (key_or_value) {
3163 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3164 new_node->schema->name);
3165 return LY_EINVAL;
3166 }
3167 return lyd_insert(parent_node, new_node);
3168 }
3169
3170 assert(!(*first_node)->parent || ((struct lyd_node *)(*first_node)->parent == parent_node));
3171
3172 /* simple insert */
3173 if (!lysc_is_userordered(new_node->schema)) {
3174 /* insert at the end */
3175 return lyd_insert_sibling(*first_node, new_node);
3176 }
3177
3178 if (key_or_value) {
3179 /* find the anchor sibling */
3180 ret = lyd_find_sibling_val(*first_node, new_node->schema, key_or_value, 0, &anchor);
3181 if (ret == LY_ENOTFOUND) {
3182 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3183 new_node->schema->name);
3184 return LY_EINVAL;
3185 } else if (ret) {
3186 return ret;
3187 }
3188
3189 /* insert after */
3190 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3191 assert(new_node->prev == anchor);
3192 if (*first_node == new_node) {
3193 *first_node = anchor;
3194 }
3195 } else {
3196 if ((*first_node)->schema->flags & LYS_KEY) {
3197 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
3198
3199 /* find last key */
3200 anchor = *first_node;
3201 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
3202 anchor = anchor->next;
3203 }
3204 /* insert after the last key */
3205 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3206 } else {
3207 /* insert at the beginning */
3208 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
3209 *first_node = new_node;
3210 }
3211 }
3212
3213 return LY_SUCCESS;
3214}
3215
3216/**
3217 * @brief Apply diff subtree on data tree nodes, recursively.
3218 *
3219 * @param[in,out] first_node First sibling of the data tree.
3220 * @param[in] parent_node Parent of the first sibling.
3221 * @param[in] diff_node Current diff node.
3222 * @return LY_ERR value.
3223 */
3224static LY_ERR
3225lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
3226 lyd_diff_cb diff_cb, void *cb_data)
3227{
3228 LY_ERR ret;
3229 struct lyd_node *match, *diff_child;
3230 const char *op, *key_or_value, *str_val;
3231 int dynamic;
3232 const struct ly_ctx *ctx = LYD_NODE_CTX(diff_node);
3233
3234 /* read all the valid attributes */
3235 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op, &key_or_value));
3236
3237 /* handle user-ordered (leaf-)lists separately */
3238 if (key_or_value) {
3239 assert((op[0] == 'c') || (op[0] == 'r'));
3240 if (op[0] == 'r') {
3241 /* find the node (we must have some siblings because the node was only moved) */
3242 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3243 } else {
3244 /* duplicate the node(s) */
3245 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3246 LY_CHECK_RET(!match, LY_EMEM);
3247 }
3248
3249 /* insert/move the node */
3250 if (key_or_value[0]) {
3251 ret = lyd_diff_insert(first_node, parent_node, match, key_or_value);
3252 } else {
3253 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
3254 }
3255 if (ret) {
3256 if (op[0] == 'c') {
3257 lyd_free_tree(match);
3258 }
3259 return ret;
3260 }
3261
3262 goto next_iter_r;
3263 }
3264
3265 /* apply operation */
3266 switch (op[0]) {
3267 case 'n':
3268 /* find the node */
3269 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3270
3271 if (match->schema->nodetype & LYD_NODE_TERM) {
3272 /* special case of only dflt flag change */
3273 if (diff_node->flags & LYD_DEFAULT) {
3274 match->flags |= LYD_DEFAULT;
3275 } else {
3276 match->flags &= ~LYD_DEFAULT;
3277 }
3278 } else {
3279 /* none operation on nodes without children is redundant and hence forbidden */
3280 if (!LYD_CHILD(diff_node)) {
3281 LOGINT_RET(ctx);
3282 }
3283 }
3284 break;
3285 case 'c':
3286 /* duplicate the node */
3287 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3288 LY_CHECK_RET(!match, LY_EMEM);
3289
3290 /* insert it at the end */
3291 ret = 0;
3292 if (*first_node) {
3293 ret = lyd_insert_after((*first_node)->prev, match);
3294 } else if (parent_node) {
3295 ret = lyd_insert(parent_node, match);
3296 } else {
3297 *first_node = match;
3298 }
3299 if (ret) {
3300 lyd_free_tree(match);
3301 return ret;
3302 }
3303
3304 break;
3305 case 'd':
3306 /* find the node */
3307 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3308
3309 /* remove it */
3310 if ((match == *first_node) && !match->parent) {
3311 assert(!parent_node);
3312 /* we have removed the top-level node */
3313 *first_node = (*first_node)->next;
3314 }
3315 lyd_free_tree(match);
3316
3317 /* we are not going recursively in this case, the whole subtree was already deleted */
3318 return LY_SUCCESS;
3319 case 'r':
3320 LY_CHECK_ERR_RET(diff_node->schema->nodetype != LYS_LEAF, LOGINT(ctx), LY_EINT);
3321
3322 /* find the node */
3323 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3324
3325 /* update its value */
3326 str_val = lyd_value2str((struct lyd_node_term *)diff_node, &dynamic);
3327 ret = lyd_change_term(match, str_val);
3328 if (dynamic) {
3329 free((char *)str_val);
3330 }
3331 if (ret && (ret != LY_EEXIST)) {
3332 LOGINT_RET(ctx);
3333 }
3334
3335 /* with flags */
3336 match->flags = diff_node->flags;
3337 break;
3338 default:
3339 LOGINT_RET(ctx);
3340 }
3341
3342next_iter_r:
3343 if (diff_cb) {
3344 /* call callback */
3345 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
3346 }
3347
3348 /* apply diff recursively */
3349 LY_LIST_FOR(LYD_CHILD(diff_node), diff_child) {
3350 LY_CHECK_RET(lyd_diff_apply_r(lyd_node_children_p(match), match, diff_child, diff_cb, cb_data));
3351 }
3352
3353 return LY_SUCCESS;
3354}
3355
3356API LY_ERR
3357lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
3358 lyd_diff_cb diff_cb, void *cb_data)
3359{
3360 const struct lyd_node *root;
3361
3362 LY_LIST_FOR(diff, root) {
3363 if (mod && (lyd_owner_module(root) != mod)) {
3364 /* skip data nodes from different modules */
3365 continue;
3366 }
3367
3368 /* apply relevant nodes from the diff datatree */
3369 LY_CHECK_RET(lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data));
3370 }
3371
3372 return LY_SUCCESS;
3373}
3374
3375API LY_ERR
3376lyd_diff_apply(struct lyd_node **data, const struct lyd_node *diff)
3377{
3378 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
3379}
3380
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003381static LY_ERR
3382lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
3383{
Michal Vasko14654712020-02-06 08:35:21 +01003384 /* ending \0 */
3385 ++reqlen;
3386
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003387 if (reqlen > *buflen) {
3388 if (is_static) {
3389 return LY_EINCOMPLETE;
3390 }
3391
3392 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3393 if (!*buffer) {
3394 return LY_EMEM;
3395 }
3396
3397 *buflen = reqlen;
3398 }
3399
3400 return LY_SUCCESS;
3401}
3402
3403/**
3404 * @brief Append all list key predicates to path.
3405 *
3406 * @param[in] node Node with keys to print.
3407 * @param[in,out] buffer Buffer to print to.
3408 * @param[in,out] buflen Current buffer length.
3409 * @param[in,out] bufused Current number of characters used in @p buffer.
3410 * @param[in] is_static Whether buffer is static or can be reallocated.
3411 * @return LY_ERR
3412 */
3413static LY_ERR
3414lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3415{
3416 const struct lyd_node *key;
3417 int dynamic = 0;
3418 size_t len;
3419 const char *val;
3420 char quot;
3421 LY_ERR rc;
3422
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003423 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003424 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
3425 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
3426 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3427 if (rc != LY_SUCCESS) {
3428 if (dynamic) {
3429 free((char *)val);
3430 }
3431 return rc;
3432 }
3433
3434 quot = '\'';
3435 if (strchr(val, '\'')) {
3436 quot = '"';
3437 }
3438 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
3439
3440 if (dynamic) {
3441 free((char *)val);
3442 }
3443 }
3444
3445 return LY_SUCCESS;
3446}
3447
3448/**
3449 * @brief Append leaf-list value predicate to path.
3450 *
3451 * @param[in] node Node to print.
3452 * @param[in,out] buffer Buffer to print to.
3453 * @param[in,out] buflen Current buffer length.
3454 * @param[in,out] bufused Current number of characters used in @p buffer.
3455 * @param[in] is_static Whether buffer is static or can be reallocated.
3456 * @return LY_ERR
3457 */
3458static LY_ERR
3459lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3460{
3461 int dynamic = 0;
3462 size_t len;
3463 const char *val;
3464 char quot;
3465 LY_ERR rc;
3466
3467 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
3468 len = 4 + strlen(val) + 2;
3469 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3470 if (rc != LY_SUCCESS) {
3471 goto cleanup;
3472 }
3473
3474 quot = '\'';
3475 if (strchr(val, '\'')) {
3476 quot = '"';
3477 }
3478 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3479
3480cleanup:
3481 if (dynamic) {
3482 free((char *)val);
3483 }
3484 return rc;
3485}
3486
3487/**
3488 * @brief Append node position (relative to its other instances) predicate to path.
3489 *
3490 * @param[in] node Node to print.
3491 * @param[in,out] buffer Buffer to print to.
3492 * @param[in,out] buflen Current buffer length.
3493 * @param[in,out] bufused Current number of characters used in @p buffer.
3494 * @param[in] is_static Whether buffer is static or can be reallocated.
3495 * @return LY_ERR
3496 */
3497static LY_ERR
3498lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3499{
3500 const struct lyd_node *first, *iter;
3501 size_t len;
3502 int pos;
3503 char *val = NULL;
3504 LY_ERR rc;
3505
3506 if (node->parent) {
3507 first = node->parent->child;
3508 } else {
3509 for (first = node; node->prev->next; node = node->prev);
3510 }
3511 pos = 1;
3512 for (iter = first; iter != node; iter = iter->next) {
3513 if (iter->schema == node->schema) {
3514 ++pos;
3515 }
3516 }
3517 if (asprintf(&val, "%d", pos) == -1) {
3518 return LY_EMEM;
3519 }
3520
3521 len = 1 + strlen(val) + 1;
3522 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3523 if (rc != LY_SUCCESS) {
3524 goto cleanup;
3525 }
3526
3527 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3528
3529cleanup:
3530 free(val);
3531 return rc;
3532}
3533
3534API char *
3535lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3536{
Michal Vasko14654712020-02-06 08:35:21 +01003537 int is_static = 0, i, depth;
3538 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003539 const struct lyd_node *iter;
3540 const struct lys_module *mod;
3541 LY_ERR rc;
3542
3543 LY_CHECK_ARG_RET(NULL, node, NULL);
3544 if (buffer) {
3545 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3546 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003547 } else {
3548 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003549 }
3550
3551 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003552 case LYD_PATH_LOG:
3553 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003554 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3555 ++depth;
3556 }
3557
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003558 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003559 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003560 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003561 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003562iter_print:
3563 /* print prefix and name */
3564 mod = NULL;
3565 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
3566 mod = iter->schema->module;
3567 }
3568
3569 /* realloc string */
3570 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
3571 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3572 if (rc != LY_SUCCESS) {
3573 break;
3574 }
3575
3576 /* print next node */
3577 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
3578
3579 switch (iter->schema->nodetype) {
3580 case LYS_LIST:
3581 if (iter->schema->flags & LYS_KEYLESS) {
3582 /* print its position */
3583 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3584 } else {
3585 /* print all list keys in predicates */
3586 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3587 }
3588 break;
3589 case LYS_LEAFLIST:
3590 if (iter->schema->flags & LYS_CONFIG_W) {
3591 /* print leaf-list value */
3592 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3593 } else {
3594 /* print its position */
3595 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3596 }
3597 break;
3598 default:
3599 /* nothing to print more */
3600 rc = LY_SUCCESS;
3601 break;
3602 }
3603 if (rc != LY_SUCCESS) {
3604 break;
3605 }
3606
Michal Vasko14654712020-02-06 08:35:21 +01003607 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003608 }
3609 break;
3610 }
3611
3612 return buffer;
3613}
Michal Vaskoe444f752020-02-10 12:20:06 +01003614
Michal Vasko9b368d32020-02-14 13:53:31 +01003615LY_ERR
3616lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
3617 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003618{
3619 LY_ERR rc;
3620 const struct lyd_node *node = NULL;
3621 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02003622 struct lyxp_expr *expr = NULL;
3623 uint16_t exp_idx = 0;
3624 struct ly_path_predicate *predicates = NULL;
3625 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01003626 struct lyd_value val = {0};
Michal Vasko00cbf532020-06-15 13:58:47 +02003627 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01003628
Michal Vasko9b368d32020-02-14 13:53:31 +01003629 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003630
3631 if (!first) {
3632 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01003633 if (match) {
3634 *match = NULL;
3635 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003636 return LY_ENOTFOUND;
3637 }
3638
Michal Vaskoe444f752020-02-10 12:20:06 +01003639 if (key_or_value && !val_len) {
3640 val_len = strlen(key_or_value);
3641 }
3642
3643 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01003644 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01003645 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 +01003646 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003647 /* parse keys */
3648 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
3649 LY_PATH_PRED_KEYS, &expr), cleanup);
3650
3651 /* compile them */
3652 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
3653 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003654 }
3655
3656 /* find first matching value */
3657 LY_LIST_FOR(first, node) {
3658 if (node->schema != schema) {
3659 continue;
3660 }
3661
Michal Vasko00cbf532020-06-15 13:58:47 +02003662 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003663 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02003664 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003665 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003666 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 +01003667 if (rc == LY_ENOTFOUND) {
3668 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01003669 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01003670 }
3671 LY_CHECK_GOTO(rc, cleanup);
3672
3673 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02003674 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003675 break;
3676 }
3677 }
3678
Michal Vasko00cbf532020-06-15 13:58:47 +02003679 if (u < LY_ARRAY_SIZE(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003680 /* not a match */
3681 continue;
3682 }
Michal Vasko90932a92020-02-12 14:33:03 +01003683 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003684 term = (struct lyd_node_term *)node;
3685
3686 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01003687 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003688 /* not a match */
3689 continue;
3690 }
3691 }
3692
3693 /* all criteria passed */
3694 break;
3695 }
3696
3697 if (!node) {
3698 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01003699 if (match) {
3700 *match = NULL;
3701 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003702 goto cleanup;
3703 }
3704
3705 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01003706 if (match) {
3707 *match = (struct lyd_node *)node;
3708 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003709 rc = LY_SUCCESS;
3710
3711cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02003712 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
3713 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01003714 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003715 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01003716 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003717 return rc;
3718}
3719
3720API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01003721lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
3722 const char *key_or_value, size_t val_len, struct lyd_node **match)
3723{
3724 const struct lysc_node *schema;
3725
3726 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
3727
3728 if (!first) {
3729 /* no data */
3730 *match = NULL;
3731 return LY_ENOTFOUND;
3732 }
3733
3734 /* find schema */
3735 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
3736 if (!schema) {
3737 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
3738 return LY_EINVAL;
3739 }
3740
3741 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
3742}
3743
3744API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003745lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3746{
3747 struct lyd_node **match_p;
3748 struct lyd_node_inner *parent;
3749
Michal Vaskof03ed032020-03-04 13:31:44 +01003750 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003751
Michal Vasko62ed12d2020-05-21 10:08:25 +02003752 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3753 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003754 if (match) {
3755 *match = NULL;
3756 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003757 return LY_ENOTFOUND;
3758 }
3759
3760 /* find first sibling */
3761 if (siblings->parent) {
3762 siblings = siblings->parent->child;
3763 } else {
3764 while (siblings->prev->next) {
3765 siblings = siblings->prev;
3766 }
3767 }
3768
3769 parent = (struct lyd_node_inner *)siblings->parent;
3770 if (parent && parent->children_ht) {
3771 assert(target->hash);
3772
3773 /* find by hash */
3774 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3775 siblings = *match_p;
3776 } else {
3777 /* not found */
3778 siblings = NULL;
3779 }
3780 } else {
3781 /* no children hash table */
3782 for (; siblings; siblings = siblings->next) {
3783 if (!lyd_compare(siblings, target, 0)) {
3784 break;
3785 }
3786 }
3787 }
3788
3789 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003790 if (match) {
3791 *match = NULL;
3792 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003793 return LY_ENOTFOUND;
3794 }
3795
Michal Vasko9b368d32020-02-14 13:53:31 +01003796 if (match) {
3797 *match = (struct lyd_node *)siblings;
3798 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003799 return LY_SUCCESS;
3800}
3801
3802API LY_ERR
3803lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3804{
3805 struct lyd_node_inner *parent;
3806 struct lyd_node *match;
3807 struct lyd_node **match_p;
3808 struct ly_set *ret;
3809
3810 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3811
Michal Vasko62ed12d2020-05-21 10:08:25 +02003812 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3813 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003814 return LY_ENOTFOUND;
3815 }
3816
3817 ret = ly_set_new();
3818 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3819
3820 /* find first sibling */
3821 if (siblings->parent) {
3822 siblings = siblings->parent->child;
3823 } else {
3824 while (siblings->prev->next) {
3825 siblings = siblings->prev;
3826 }
3827 }
3828
3829 parent = (struct lyd_node_inner *)siblings->parent;
3830 if (parent && parent->children_ht) {
3831 assert(target->hash);
3832
3833 /* find by hash */
3834 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3835 match = *match_p;
3836 } else {
3837 /* not found */
3838 match = NULL;
3839 }
3840 while (match) {
3841 /* add all found nodes into the return set */
3842 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3843 goto error;
3844 }
3845
3846 /* find next instance */
3847 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3848 match = NULL;
3849 } else {
3850 match = *match_p;
3851 }
3852 }
3853 } else {
3854 /* no children hash table */
3855 for (; siblings; siblings = siblings->next) {
3856 if (!lyd_compare(siblings, target, 0)) {
3857 /* a match */
3858 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3859 goto error;
3860 }
3861 }
3862 }
3863 }
3864
3865 if (!ret->count) {
3866 ly_set_free(ret, NULL);
3867 return LY_ENOTFOUND;
3868 }
3869
3870 *set = ret;
3871 return LY_SUCCESS;
3872
3873error:
3874 ly_set_free(ret, NULL);
3875 return LY_EMEM;
3876}
3877
Michal Vasko90932a92020-02-12 14:33:03 +01003878static int
3879lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3880{
3881 struct lysc_node *val1;
3882 struct lyd_node *val2;
3883
3884 val1 = *((struct lysc_node **)val1_p);
3885 val2 = *((struct lyd_node **)val2_p);
3886
3887 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
3888
3889 if (val1 == val2->schema) {
3890 /* schema match is enough */
3891 return 1;
3892 } else {
3893 return 0;
3894 }
3895}
3896
3897static LY_ERR
3898lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3899{
3900 struct lyd_node **match_p;
3901 struct lyd_node_inner *parent;
3902 uint32_t hash;
3903 values_equal_cb ht_cb;
3904
3905 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3906
3907 /* find first sibling */
3908 if (siblings->parent) {
3909 siblings = siblings->parent->child;
3910 } else {
3911 while (siblings->prev->next) {
3912 siblings = siblings->prev;
3913 }
3914 }
3915
3916 parent = (struct lyd_node_inner *)siblings->parent;
3917 if (parent && parent->children_ht) {
3918 /* calculate our hash */
3919 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3920 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3921 hash = dict_hash_multi(hash, NULL, 0);
3922
3923 /* use special hash table function */
3924 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3925
3926 /* find by hash */
3927 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3928 siblings = *match_p;
3929 } else {
3930 /* not found */
3931 siblings = NULL;
3932 }
3933
3934 /* set the original hash table compare function back */
3935 lyht_set_cb(parent->children_ht, ht_cb);
3936 } else {
3937 /* no children hash table */
3938 for (; siblings; siblings = siblings->next) {
3939 if (siblings->schema == schema) {
3940 /* schema match is enough */
3941 break;
3942 }
3943 }
3944 }
3945
3946 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003947 if (match) {
3948 *match = NULL;
3949 }
Michal Vasko90932a92020-02-12 14:33:03 +01003950 return LY_ENOTFOUND;
3951 }
3952
Michal Vasko9b368d32020-02-14 13:53:31 +01003953 if (match) {
3954 *match = (struct lyd_node *)siblings;
3955 }
Michal Vasko90932a92020-02-12 14:33:03 +01003956 return LY_SUCCESS;
3957}
3958
Michal Vaskoe444f752020-02-10 12:20:06 +01003959API LY_ERR
3960lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3961 size_t val_len, struct lyd_node **match)
3962{
3963 LY_ERR rc;
3964 struct lyd_node *target = NULL;
3965
3966 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003967 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003968 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3969 return LY_EINVAL;
3970 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
3971 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
3972 return LY_EINVAL;
3973 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3974 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3975 lys_nodetype2str(schema->nodetype), __func__);
3976 return LY_EINVAL;
3977 }
3978
Michal Vasko62ed12d2020-05-21 10:08:25 +02003979 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3980 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003981 if (match) {
3982 *match = NULL;
3983 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003984 return LY_ENOTFOUND;
3985 }
3986
Michal Vaskof03ed032020-03-04 13:31:44 +01003987 if (key_or_value && !val_len) {
3988 val_len = strlen(key_or_value);
3989 }
3990
Michal Vasko90932a92020-02-12 14:33:03 +01003991 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01003992 switch (schema->nodetype) {
3993 case LYS_CONTAINER:
3994 case LYS_ANYXML:
3995 case LYS_ANYDATA:
3996 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01003997 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01003998 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01003999 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02004000 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01004001 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004002 break;
4003 case LYS_LEAFLIST:
4004 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02004005 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
4006 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
4007 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01004008 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01004009 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01004010 if (schema->nodetype == LYS_LIST) {
4011 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004012 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004013 }
4014
4015 /* find it */
4016 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004017 break;
4018 default:
4019 /* unreachable */
4020 LOGINT(schema->module->ctx);
4021 return LY_EINT;
4022 }
4023
Michal Vaskoe444f752020-02-10 12:20:06 +01004024 lyd_free_tree(target);
4025 return rc;
4026}
Michal Vaskoccc02342020-05-21 10:09:21 +02004027
4028API LY_ERR
4029lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4030{
4031 LY_ERR ret = LY_SUCCESS;
4032 struct lyxp_set xp_set;
4033 struct lyxp_expr *exp;
4034 uint32_t i;
4035
4036 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4037
4038 memset(&xp_set, 0, sizeof xp_set);
4039
4040 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02004041 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02004042 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
4043
4044 /* evaluate expression */
4045 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
4046 LY_CHECK_GOTO(ret, cleanup);
4047
4048 /* allocate return set */
4049 *set = ly_set_new();
4050 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4051
4052 /* transform into ly_set */
4053 if (xp_set.type == LYXP_SET_NODE_SET) {
4054 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4055 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
4056 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4057 (*set)->size = xp_set.used;
4058
4059 for (i = 0; i < xp_set.used; ++i) {
4060 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
4061 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
4062 }
4063 }
4064 }
4065
4066cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004067 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02004068 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
4069 return ret;
4070}