blob: a5f27a3b29f4ec91b4ccfa735054358f3e0f3be1 [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 Vaskoe3ed7dc2022-11-30 11:39:44 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02005 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci084289f2019-07-09 17:35:30 +020020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#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>
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010037#include "ly_common.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020042#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010043#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
aPiecek6cf1d162023-11-08 16:07:00 +010048#include "tree_data_sorted.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010049#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "tree_schema.h"
51#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020052#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020053#include "xml.h"
54#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020055
Michal Vaskof277d362023-04-24 09:08:31 +020056static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
57 ly_bool parental_schemas_checked);
58
Radek Krejci7931b192020-06-25 17:05:03 +020059static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020060lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020061{
Michal Vasko69730152020-10-09 16:30:07 +020062 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020063 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020064 const char *path = in->method.fpath.filepath;
65 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020066
67 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020068 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020069
Radek Krejcif13b87b2020-12-01 22:02:17 +010070 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
71 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020072 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010073 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
74 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020075 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010076 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
77 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020078 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020079 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020080 }
81
Radek Krejci7931b192020-06-25 17:05:03 +020082 return format;
83}
Radek Krejcie7b95092019-05-15 11:03:07 +020084
Michal Vaskoe0665742021-02-11 11:08:44 +010085/**
86 * @brief Parse YANG data into a data tree.
87 *
88 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010089 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010090 * @param[in] parent Parent to connect the parsed nodes to, if any.
Michal Vaskob36c56f2024-01-31 12:50:07 +010091 * @param[in,out] first_p Pointer to the first parsed node.
Michal Vaskoe0665742021-02-11 11:08:44 +010092 * @param[in] in Input handle to read the input from.
93 * @param[in] format Expected format of the data in @p in.
94 * @param[in] parse_opts Options for parser.
95 * @param[in] val_opts Options for validation.
96 * @param[out] op Optional pointer to the parsed operation, if any.
97 * @return LY_ERR value.
98 */
99static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100100lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
101 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200102{
stewegd8e2fc92023-05-31 09:52:56 +0200103 LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200104 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100105 struct ly_set parsed = {0};
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100106 uint32_t i, int_opts = 0;
Michal Vasko7a266772024-01-23 11:02:38 +0100107 const struct ly_err_item *eitem;
Michal Vaskoddd76592022-01-17 13:34:48 +0100108 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200109
Michal Vaskoe0665742021-02-11 11:08:44 +0100110 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200111
112 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100113 if (first_p) {
114 *first_p = NULL;
115 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200116
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 /* remember input position */
118 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200119
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100120 /* set internal options */
121 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
122 int_opts = LYD_INTOPT_WITH_SIBLINGS;
123 }
124
Michal Vaskoe0665742021-02-11 11:08:44 +0100125 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200126 switch (format) {
127 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100128 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100129 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200131 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100132 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100133 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200135 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100136 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100137 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200138 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200139 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100140 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100141 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100142 break;
143 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100144 if (r) {
145 rc = r;
Michal Vasko7a266772024-01-23 11:02:38 +0100146 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
147 goto cleanup;
148 }
149
150 eitem = ly_err_last(ctx);
151 assert(eitem);
152 if (eitem->vecode == LYVE_SYNTAX) {
153 /* cannot get more errors on a syntax error */
Michal Vasko50da8cd2023-03-10 08:38:59 +0100154 goto cleanup;
155 }
156 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100157
Michal Vaskoa6139e02023-10-03 14:13:22 +0200158 if (parent && parsed.count) {
159 /* use the first parsed node */
Michal Vaskob36c56f2024-01-31 12:50:07 +0100160 if (first_p) {
161 *first_p = parsed.dnodes[0];
162 } else {
163 first_p = &parsed.dnodes[0];
164 }
Radek Krejci7931b192020-06-25 17:05:03 +0200165 }
166
Michal Vaskoe0665742021-02-11 11:08:44 +0100167 if (!(parse_opts & LYD_PARSE_ONLY)) {
168 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200169 r = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
Michal Vasko135719f2022-08-25 12:18:17 +0200170 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200171 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100172 }
Radek Krejci7931b192020-06-25 17:05:03 +0200173
Michal Vaskoe0665742021-02-11 11:08:44 +0100174 /* set the operation node */
175 if (op) {
176 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 }
178
179cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100180 if (lydctx) {
181 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200182 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100183 if (rc) {
184 if (parent) {
185 /* free all the parsed subtrees */
186 for (i = 0; i < parsed.count; ++i) {
187 lyd_free_tree(parsed.dnodes[i]);
188 }
189 } else {
190 /* free everything */
191 lyd_free_all(*first_p);
192 *first_p = NULL;
193 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100194 } else if (subtree_sibling) {
195 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100196 }
197 ly_set_erase(&parsed, NULL);
198 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200199}
200
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100201LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100202lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
203 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
204{
205 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
206
207 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
208 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
209 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
210
211 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
212}
213
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100214LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100215lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
216 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
217{
218 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
219 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
220 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
221
Radek Krejcif16e2542021-02-17 15:39:23 +0100222 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100223}
224
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100225LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100226lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
227 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200228{
229 LY_ERR ret;
230 struct ly_in *in;
231
232 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100233 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200234
235 ly_in_free(in, 0);
236 return ret;
237}
238
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100239LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200240lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200241 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200242{
243 LY_ERR ret;
244 struct ly_in *in;
245
246 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100247 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200248
249 ly_in_free(in, 0);
250 return ret;
251}
252
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100253LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200254lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
255 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200256{
257 LY_ERR ret;
258 struct ly_in *in;
259
260 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100261 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200262
263 ly_in_free(in, 0);
264 return ret;
265}
266
Radek Krejcif16e2542021-02-17 15:39:23 +0100267/**
268 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
269 * for schema nodes locked inside the extension instance.
270 *
271 * At least one of @p parent, @p tree, or @p op must always be set.
272 *
Michal Vasko820efe82023-05-12 15:47:43 +0200273 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100274 *
275 * @param[in] ctx libyang context.
276 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
277 * @param[in] parent Optional parent to connect the parsed nodes to.
278 * @param[in] in Input handle to read the input from.
279 * @param[in] format Expected format of the data in @p in.
280 * @param[in] data_type Expected operation to parse (@ref datatype).
281 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
282 * @param[out] op Optional parsed operation node.
283 * @return LY_ERR value.
284 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
285 */
286static LY_ERR
287lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
288 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200289{
Michal Vaskoe0665742021-02-11 11:08:44 +0100290 LY_ERR rc = LY_SUCCESS;
291 struct lyd_ctx *lydctx = NULL;
292 struct ly_set parsed = {0};
293 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100294 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200295 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200296
Michal Vasko27fb0262021-02-23 09:42:01 +0100297 if (!ctx) {
298 ctx = LYD_CTX(parent);
299 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200300 if (tree) {
301 *tree = NULL;
302 }
303 if (op) {
304 *op = NULL;
305 }
306
Radek Krejci7931b192020-06-25 17:05:03 +0200307 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200308
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 /* remember input position */
310 in->func_start = in->current;
311
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100312 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200313 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100314 val_opts = 0;
315
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100316 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100317 case LYD_TYPE_RPC_NETCONF:
318 case LYD_TYPE_NOTIF_NETCONF:
319 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200320 proto_msg = 1;
321 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100322 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200323 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
324 tree, !op, LY_EINVAL);
325 proto_msg = 1;
326 break;
327 case LYD_TYPE_RPC_RESTCONF:
328 case LYD_TYPE_REPLY_RESTCONF:
329 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
330 proto_msg = 1;
331 break;
332 case LYD_TYPE_NOTIF_RESTCONF:
333 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
334 proto_msg = 1;
335 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100336
Michal Vasko820efe82023-05-12 15:47:43 +0200337 /* set internal opts */
338 case LYD_TYPE_RPC_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200339 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200340 break;
341 case LYD_TYPE_NOTIF_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200342 int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200343 break;
344 case LYD_TYPE_REPLY_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200345 int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200346 break;
347 case LYD_TYPE_DATA_YANG:
348 LOGINT(ctx);
349 rc = LY_EINT;
350 goto cleanup;
351 }
352
353 /* parse a full protocol message */
354 if (proto_msg) {
355 if (format == LYD_XML) {
356 /* parse the NETCONF (or RESTCONF XML) message */
357 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
358 } else {
359 /* parse the RESTCONF message */
360 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
361 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100362 if (rc) {
363 if (envp) {
364 /* special situation when the envelopes were parsed successfully */
365 *tree = envp;
366 }
Michal Vasko73792a12022-05-10 09:28:45 +0200367 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100368 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100369
370 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100371 if (envp) {
372 /* special out param meaning */
373 *tree = envp;
374 } else {
375 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100376 }
377 if (op) {
378 *op = lydctx->op_node;
379 }
380 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100381 }
382
383 /* parse the data */
384 switch (format) {
385 case LYD_XML:
386 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200388 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100389 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200391 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100392 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200394 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100395 LOGARG(ctx, format);
396 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200397 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200398 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100399 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200400
Michal Vaskoe0665742021-02-11 11:08:44 +0100401 /* set out params correctly */
402 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100403 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100404 }
405 if (op) {
406 *op = lydctx->op_node;
407 }
408
409cleanup:
410 if (lydctx) {
411 lydctx->free(lydctx);
412 }
413 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200414 /* free all the parsed nodes */
415 if (parsed.count) {
416 i = parsed.count;
417 do {
418 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100419 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200420 } while (i);
421 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200422 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200423 *tree = NULL;
424 }
425 if (op) {
426 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100427 }
428 }
429 ly_set_erase(&parsed, NULL);
430 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200431}
Radek Krejci084289f2019-07-09 17:35:30 +0200432
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100433LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100434lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
435 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
436{
437 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
438
439 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
440}
441
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100442LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100443lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
444 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
445{
446 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
447
448 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
449
450 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
451}
452
Michal Vasko90932a92020-02-12 14:33:03 +0100453struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200454lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100455{
Michal Vaskob104f112020-07-17 09:54:54 +0200456 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100457 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200458 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100459 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100460
Michal Vaskob104f112020-07-17 09:54:54 +0200461 assert(new_node);
462
Michal Vasko9beceb82022-04-05 12:14:15 +0200463 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200464 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100465 return NULL;
466 }
467
Michal Vasko93b16062020-12-09 18:14:18 +0100468 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100469 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100470 getnext_opts = LYS_GETNEXT_OUTPUT;
471 }
472
Michal Vaskoa2756f02021-07-09 13:20:28 +0200473 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200474 /* find the anchor using hashes */
475 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100476 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200477 while (schema) {
478 /* keep trying to find the first existing instance of the closest following schema sibling,
479 * otherwise return NULL - inserting at the end */
480 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
481 break;
482 }
483
Michal Vasko93b16062020-12-09 18:14:18 +0100484 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200485 }
486 } else {
487 /* find the anchor without hashes */
488 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200489 sparent = lysc_data_parent(new_node->schema);
490 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200491 /* we are in top-level, skip all the data from preceding modules */
492 LY_LIST_FOR(match, match) {
493 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
494 break;
495 }
496 }
497 }
498
499 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100500 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200501 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200502 /* must be a top-level extension instance data, no anchor */
503 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200504 }
Michal Vaskob104f112020-07-17 09:54:54 +0200505
506 found = 0;
507 LY_LIST_FOR(match, match) {
508 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
509 /* we have found an opaque node, which must be at the end, so use it OR
510 * modules do not match, so we must have traversed all the data from new_node module (if any),
511 * we have found the first node of the next module, that is what we want */
512 break;
513 }
514
515 /* skip schema nodes until we find the instantiated one */
516 while (!found) {
517 if (new_node->schema == schema) {
518 /* we have found the schema of the new node, continue search to find the first
519 * data node with a different schema (after our schema) */
520 found = 1;
521 break;
522 }
523 if (match->schema == schema) {
524 /* current node (match) is a data node still before the new node, continue search in data */
525 break;
526 }
Michal Vasko9a463472023-08-08 09:43:33 +0200527
Michal Vasko93b16062020-12-09 18:14:18 +0100528 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200529 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200530 /* must be a top-level extension instance data, no anchor */
531 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200532 }
Michal Vaskob104f112020-07-17 09:54:54 +0200533 }
534
535 if (found && (match->schema != new_node->schema)) {
536 /* find the next node after we have found our node schema data instance */
537 break;
538 }
539 }
Michal Vasko90932a92020-02-12 14:33:03 +0100540 }
541
542 return match;
543}
544
Michal Vaskoc61dd062022-06-07 11:01:28 +0200545void
aPiecek743184b2024-02-01 13:25:56 +0100546lyd_insert_after_node(struct lyd_node **first_sibling_p, struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100547{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100548 struct lyd_node_inner *par;
aPiecek743184b2024-02-01 13:25:56 +0100549 struct lyd_node *first_sibling;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100550
Michal Vasko90932a92020-02-12 14:33:03 +0100551 assert(!node->next && (node->prev == node));
552
aPiecek743184b2024-02-01 13:25:56 +0100553 if (sibling->next) {
Michal Vasko90932a92020-02-12 14:33:03 +0100554 /* sibling had a succeeding node */
aPiecek743184b2024-02-01 13:25:56 +0100555 sibling->next->prev = node;
556 node->next = sibling->next;
Michal Vasko90932a92020-02-12 14:33:03 +0100557 } else {
558 /* sibling was last, find first sibling and change its prev */
aPiecek743184b2024-02-01 13:25:56 +0100559 if (first_sibling_p && *first_sibling_p) {
560 assert(!(*first_sibling_p)->prev->next);
561 (*first_sibling_p)->prev = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100562 } else {
aPiecek743184b2024-02-01 13:25:56 +0100563 first_sibling = lyd_first_sibling(sibling);
564 first_sibling->prev = node;
565 if (first_sibling_p) {
566 *first_sibling_p = first_sibling;
567 }
Michal Vasko90932a92020-02-12 14:33:03 +0100568 }
Michal Vasko90932a92020-02-12 14:33:03 +0100569 }
aPiecek743184b2024-02-01 13:25:56 +0100570 node->prev = sibling;
571 sibling->next = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100572 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100573
Michal Vasko9f96a052020-03-10 09:41:45 +0100574 for (par = node->parent; par; par = par->parent) {
575 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
576 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100577 par->flags &= ~LYD_DEFAULT;
578 }
579 }
Michal Vasko90932a92020-02-12 14:33:03 +0100580}
581
Michal Vaskoc61dd062022-06-07 11:01:28 +0200582void
Michal Vaskof03ed032020-03-04 13:31:44 +0100583lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100584{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100585 struct lyd_node_inner *par;
586
Michal Vasko90932a92020-02-12 14:33:03 +0100587 assert(!node->next && (node->prev == node));
588
589 node->next = sibling;
590 /* covers situation of sibling being first */
591 node->prev = sibling->prev;
592 sibling->prev = node;
593 if (node->prev->next) {
594 /* sibling had a preceding node */
595 node->prev->next = node;
596 } else if (sibling->parent) {
597 /* sibling was first and we must also change parent child pointer */
598 sibling->parent->child = node;
599 }
600 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100601
Michal Vasko9f96a052020-03-10 09:41:45 +0100602 for (par = node->parent; par; par = par->parent) {
603 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
604 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100605 par->flags &= ~LYD_DEFAULT;
606 }
607 }
Michal Vasko90932a92020-02-12 14:33:03 +0100608}
609
610/**
Michal Vaskob104f112020-07-17 09:54:54 +0200611 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100612 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100613 * Handles inserting into NP containers and key-less lists.
614 *
Michal Vasko90932a92020-02-12 14:33:03 +0100615 * @param[in] parent Parent to insert into.
616 * @param[in] node Node to insert.
617 */
618static void
Michal Vaskob104f112020-07-17 09:54:54 +0200619lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100620{
621 struct lyd_node_inner *par;
622
Radek Krejcia1c1e542020-09-29 16:06:52 +0200623 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100624 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100625
626 par = (struct lyd_node_inner *)parent;
627
Michal Vaskob104f112020-07-17 09:54:54 +0200628 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100629 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100630
Michal Vaskod989ba02020-08-24 10:59:24 +0200631 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100632 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
633 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100634 par->flags &= ~LYD_DEFAULT;
635 }
636 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200637}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100638
Michal Vasko751cb4d2020-07-14 12:25:28 +0200639/**
640 * @brief Learn whether a list instance has all the keys.
641 *
642 * @param[in] list List instance to check.
643 * @return non-zero if all the keys were found,
644 * @return 0 otherwise.
645 */
646static int
647lyd_insert_has_keys(const struct lyd_node *list)
648{
649 const struct lyd_node *key;
650 const struct lysc_node *skey = NULL;
651
652 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200653 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200654 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
655 if (!key || (key->schema != skey)) {
656 /* key missing */
657 return 0;
658 }
659
660 key = key->next;
661 }
662
663 /* all keys found */
664 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100665}
666
aPiecek6cf1d162023-11-08 16:07:00 +0100667/**
668 * @brief Get the first subsequent data node that contains a different schema definition.
669 *
670 * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet.
671 * @param[in] node Node to be inserted.
672 * @return Subsequent data node with a different schema.
673 */
674static struct lyd_node *
675lyd_insert_node_find_anchor(struct lyd_node *first_sibling, struct lyd_node *node)
676{
677 struct lyd_node *anchor;
678
679 if (first_sibling && (first_sibling->flags & LYD_EXT)) {
680 return NULL;
681 }
682
683 /* find the anchor, so we can insert somewhere before it */
684 anchor = lyd_insert_get_next_anchor(first_sibling, node);
685 /* cannot insert data node after opaque nodes */
686 if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) {
687 anchor = first_sibling->prev;
688 while ((anchor != first_sibling) && !anchor->prev->schema) {
689 anchor = anchor->prev;
690 }
691 }
692
693 return anchor;
694}
695
696/**
aPiecek61880852024-01-11 14:10:29 +0100697 * @brief Insert @p node as the last node.
aPiecek6cf1d162023-11-08 16:07:00 +0100698 *
699 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek743184b2024-02-01 13:25:56 +0100700 * @param[in,out] first_sibling First sibling, NULL if no top-level sibling exist yet.
701 * Can be also NULL if @p parent is set.
aPiecek6cf1d162023-11-08 16:07:00 +0100702 * @param[in] node Individual node (without siblings) to insert.
aPiecek6cf1d162023-11-08 16:07:00 +0100703 */
704static void
aPiecek743184b2024-02-01 13:25:56 +0100705lyd_insert_node_last(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100706{
aPiecek743184b2024-02-01 13:25:56 +0100707 assert(first_sibling && node);
708
709 if (*first_sibling) {
aPiecek743184b2024-02-01 13:25:56 +0100710 lyd_insert_after_node(first_sibling, (*first_sibling)->prev, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200711 } else if (parent) {
712 lyd_insert_only_child(parent, node);
aPiecek743184b2024-02-01 13:25:56 +0100713 *first_sibling = node;
714 } else {
715 *first_sibling = node;
Michal Vaskob104f112020-07-17 09:54:54 +0200716 }
aPiecek6cf1d162023-11-08 16:07:00 +0100717}
718
aPieceke6955f42024-01-19 14:01:54 +0100719void
aPiecek743184b2024-02-01 13:25:56 +0100720lyd_insert_node_ordby_schema(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
aPiecek6cf1d162023-11-08 16:07:00 +0100721{
aPiecek61880852024-01-11 14:10:29 +0100722 struct lyd_node *anchor;
aPiecek6cf1d162023-11-08 16:07:00 +0100723
aPiecek743184b2024-02-01 13:25:56 +0100724 assert(first_sibling && node);
725
726 if ((anchor = lyd_insert_node_find_anchor(*first_sibling, node))) {
aPiecek6cf1d162023-11-08 16:07:00 +0100727 lyd_insert_before_node(anchor, node);
aPiecek743184b2024-02-01 13:25:56 +0100728 *first_sibling = *first_sibling != anchor ? *first_sibling : node;
aPiecek6f9253c2024-02-13 11:05:12 +0100729 } else if (*first_sibling && node->schema && !(*first_sibling)->prev->schema) {
730 /* cannot insert data node after opaque nodes */
731 anchor = (*first_sibling)->prev;
732 while ((anchor != *first_sibling) && !anchor->prev->schema) {
733 anchor = anchor->prev;
734 }
735 lyd_insert_before_node(anchor, node);
736 *first_sibling = *first_sibling != anchor ? *first_sibling : node;
aPiecek6cf1d162023-11-08 16:07:00 +0100737 } else {
aPiecek61880852024-01-11 14:10:29 +0100738 lyd_insert_node_last(parent, first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100739 }
aPiecek6cf1d162023-11-08 16:07:00 +0100740}
741
742void
aPiecek1462ab12024-02-07 09:13:29 +0100743lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, uint32_t order)
aPiecek6cf1d162023-11-08 16:07:00 +0100744{
745 LY_ERR ret = LY_SUCCESS;
aPiecek61880852024-01-11 14:10:29 +0100746 struct lyd_node *first_sibling, *leader;
aPiecek6cf1d162023-11-08 16:07:00 +0100747
748 /* inserting list without its keys is not supported */
749 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
750 assert(!parent || !parent->schema ||
751 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
752
aPiecek61880852024-01-11 14:10:29 +0100753 if (!parent && first_sibling_p && (*first_sibling_p)) {
aPiecek6cf1d162023-11-08 16:07:00 +0100754 parent = lyd_parent(*first_sibling_p);
755 }
aPiecek61880852024-01-11 14:10:29 +0100756 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
aPiecek6cf1d162023-11-08 16:07:00 +0100757
aPiecek6f9253c2024-02-13 11:05:12 +0100758 if ((order == LYD_INSERT_NODE_LAST) || !node->schema || (first_sibling && (first_sibling->flags & LYD_EXT))) {
aPiecek743184b2024-02-01 13:25:56 +0100759 lyd_insert_node_last(parent, &first_sibling, node);
aPiecek1462ab12024-02-07 09:13:29 +0100760 } else if (order == LYD_INSERT_NODE_LAST_BY_SCHEMA) {
761 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek61880852024-01-11 14:10:29 +0100762 } else if (lyds_is_supported(node) &&
763 (lyd_find_sibling_schema(first_sibling, node->schema, &leader) == LY_SUCCESS)) {
aPiecek743184b2024-02-01 13:25:56 +0100764 ret = lyds_insert(&first_sibling, &leader, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100765 if (ret) {
766 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
767 * but insert the node anyway although the nodes will not be sorted.
768 */
769 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted.", node->schema->name);
aPiecek743184b2024-02-01 13:25:56 +0100770 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100771 }
772 } else {
aPiecek743184b2024-02-01 13:25:56 +0100773 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100774 }
Michal Vaskob104f112020-07-17 09:54:54 +0200775
776 /* insert into parent HT */
777 lyd_insert_hash(node);
778
779 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200780 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200781 lyd_hash(parent);
782
783 /* now we can insert even the list into its parent HT */
784 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100785 }
aPiecek61880852024-01-11 14:10:29 +0100786
787 if (first_sibling_p) {
aPiecek743184b2024-02-01 13:25:56 +0100788 *first_sibling_p = first_sibling;
aPiecek61880852024-01-11 14:10:29 +0100789 }
aPiecek1462ab12024-02-07 09:13:29 +0100790
791#ifndef NDEBUG
792 if ((order == LYD_INSERT_NODE_LAST) && lyds_is_supported(node) &&
793 (node->prev->schema == node->schema) && (lyds_compare_single(node->prev, node) > 0)) {
794 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted, inserted node should not be added to the end.",
795 node->schema->name);
796 }
797#endif
Michal Vasko90932a92020-02-12 14:33:03 +0100798}
799
Michal Vasko717a4c32020-12-07 09:40:10 +0100800/**
aPiecekcada0d72024-01-11 15:04:12 +0100801 * @brief Check that @p node can be unlinked.
802 *
803 * @param[in] node Node to check
804 * @return LY_ERR value.
805 */
806static LY_ERR
807lyd_unlink_check(struct lyd_node *node)
808{
809 if (!node) {
810 return LY_SUCCESS;
811 }
812
813 if (lysc_is_key(node->schema) && node->parent) {
814 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
815 LYD_NAME(node));
816 return LY_EINVAL;
817 }
818
819 return LY_SUCCESS;
820}
821
822/**
823 * @brief Move schema instances before anchor or as the last.
824 *
825 * The nodes will remain sorted according to the schema.
826 *
aPiecek743184b2024-02-01 13:25:56 +0100827 * @param[in] first_dst First sibling, destination.
aPiecekcada0d72024-01-11 15:04:12 +0100828 * @param[in] node Starting node, all following nodes with the same schema will be moved.
829 * @param[out] next_p Next node that has a different schema or NULL.
830 * @return LY_ERR value.
831 */
832static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100833lyd_move_nodes_ordby_schema(struct lyd_node **first_dst, struct lyd_node *node, struct lyd_node **next_p)
aPiecekcada0d72024-01-11 15:04:12 +0100834{
aPiecek743184b2024-02-01 13:25:56 +0100835 struct lyd_node *second, *anchor, *iter, *next, *dst, *src, *first_src = NULL;
aPiecekcada0d72024-01-11 15:04:12 +0100836
aPiecek743184b2024-02-01 13:25:56 +0100837 assert(first_dst && *first_dst && !(*first_dst)->prev->next && node && next_p);
aPiecekcada0d72024-01-11 15:04:12 +0100838
aPiecek743184b2024-02-01 13:25:56 +0100839 if ((anchor = lyd_insert_node_find_anchor(*first_dst, node))) {
aPiecekcada0d72024-01-11 15:04:12 +0100840 /* move the first node to the correct place according to the schema */
841 LY_CHECK_RET(lyd_unlink_check(node));
842 second = node->next;
aPiecek743184b2024-02-01 13:25:56 +0100843 lyd_unlink_ignore_lyds(&first_src, node);
aPiecekcada0d72024-01-11 15:04:12 +0100844 lyd_insert_before_node(anchor, node);
845 lyd_insert_hash(node);
aPiecek743184b2024-02-01 13:25:56 +0100846 *first_dst = *first_dst != anchor ? *first_dst : node;
aPiecekcada0d72024-01-11 15:04:12 +0100847 if (!second || (node->schema != second->schema)) {
848 /* no more nodes to move */
849 *next_p = second;
850 return LY_SUCCESS;
851 }
852 dst = node;
853 src = second;
854 } else {
855 /* just move all instances to the end */
aPiecek743184b2024-02-01 13:25:56 +0100856 dst = (*first_dst)->prev;
aPiecekcada0d72024-01-11 15:04:12 +0100857 src = node;
858 }
859
860 /* move the rest of source instances after @p node */
861 LY_LIST_FOR_SAFE(src, next, iter) {
862 LY_CHECK_RET(lyd_unlink_check(iter));
863 if (iter->schema != src->schema) {
864 break;
865 }
aPiecek743184b2024-02-01 13:25:56 +0100866 lyd_unlink_ignore_lyds(&first_src, iter);
867 lyd_insert_after_node(first_dst, dst, iter);
aPiecekcada0d72024-01-11 15:04:12 +0100868 lyd_insert_hash(iter);
869 dst = iter;
870 }
871 *next_p = iter;
872
873 return LY_SUCCESS;
874}
875
876/**
877 * @brief Move nodes regardless of schema.
878 *
879 * The destination for the move is NULL, or a childless parent.
880 *
881 * @param[in] parent Parent to insert into, NULL for top-level sibling.
882 * @param[in] first_src First sibling, all following nodes will be moved.
883 * @return LY_ERR value.
884 */
885static LY_ERR
886lyd_move_nodes_at_once(struct lyd_node *parent, struct lyd_node *first_src)
887{
aPiecek743184b2024-02-01 13:25:56 +0100888 struct lyd_node *start, *next, *iter, *first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100889
890 assert(!lyd_child(parent) && first_src && !first_src->prev->next && !first_src->parent);
891
892 LY_CHECK_RET(lyd_unlink_check(first_src));
893
894 /* move the first node */
895 start = first_src->next;
aPiecek743184b2024-02-01 13:25:56 +0100896 first_dst = first_src;
aPiecekcada0d72024-01-11 15:04:12 +0100897 if (parent) {
aPiecek743184b2024-02-01 13:25:56 +0100898 lyd_unlink_ignore_lyds(&first_src, first_dst);
899 lyd_insert_only_child(parent, first_dst);
900 lyd_insert_hash(first_dst);
aPiecekcada0d72024-01-11 15:04:12 +0100901 } else {
aPiecek743184b2024-02-01 13:25:56 +0100902 lyd_unlink_ignore_lyds(&first_src, first_dst);
aPiecekcada0d72024-01-11 15:04:12 +0100903 }
904
905 /* move the rest of the nodes */
906 LY_LIST_FOR_SAFE(start, next, iter) {
907 LY_CHECK_RET(lyd_unlink_check(iter));
aPiecek743184b2024-02-01 13:25:56 +0100908 lyd_unlink_ignore_lyds(&first_src, iter);
909 lyd_insert_after_node(&first_dst, first_dst->prev, iter);
aPiecekcada0d72024-01-11 15:04:12 +0100910 lyd_insert_hash(iter);
911 }
912
913 return LY_SUCCESS;
914}
915
916/**
917 * @brief Move the nodes in parts according to the schema.
918 *
aPiecek743184b2024-02-01 13:25:56 +0100919 * @param[in,out] first_dst First sibling, destination.
aPiecekcada0d72024-01-11 15:04:12 +0100920 * @param[in] first_src First sibling, all following nodes will be moved.
921 * @return LY_ERR value.
922 */
923static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100924lyd_move_nodes_by_schema(struct lyd_node **first_dst, struct lyd_node *first_src)
aPiecekcada0d72024-01-11 15:04:12 +0100925{
926 LY_ERR ret;
927 struct lyd_node *next, *iter, *leader;
928
aPiecek743184b2024-02-01 13:25:56 +0100929 assert(first_dst && *first_dst && !(*first_dst)->prev->next && first_src &&
930 !first_src->prev->next && !first_src->parent);
aPiecekcada0d72024-01-11 15:04:12 +0100931
932 for (iter = first_src; iter; iter = next) {
933 if (lyds_is_supported(iter) &&
aPiecek743184b2024-02-01 13:25:56 +0100934 (lyd_find_sibling_schema(*first_dst, iter->schema, &leader) == LY_SUCCESS)) {
935 ret = lyds_merge(first_dst, &leader, &first_src, iter, &next);
aPiecekcada0d72024-01-11 15:04:12 +0100936 if (ret) {
937 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
938 * but insert the node anyway although the nodes will not be sorted.
939 */
940 LOGWRN(LYD_CTX(first_src), "Data in \"%s\" are not sorted.", leader->schema->name);
aPiecek743184b2024-02-01 13:25:56 +0100941 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, next, &next));
aPiecekcada0d72024-01-11 15:04:12 +0100942 }
943 } else {
aPiecek743184b2024-02-01 13:25:56 +0100944 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, iter, &next));
aPiecekcada0d72024-01-11 15:04:12 +0100945 }
aPiecekcada0d72024-01-11 15:04:12 +0100946 }
947
948 return LY_SUCCESS;
949}
950
951/**
952 * @brief Move a nodes into parent/siblings.
953 *
954 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek743184b2024-02-01 13:25:56 +0100955 * @param[in,out] first_dst_p First sibling, NULL if no top-level sibling exist yet.
956 * Can be also NULL if @p parent is set.
aPiecekcada0d72024-01-11 15:04:12 +0100957 * @param[in] first_src First sibling, all following nodes will be moved.
958 * @return LY_ERR value.
959 */
960static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100961lyd_move_nodes(struct lyd_node *parent, struct lyd_node **first_dst_p, struct lyd_node *first_src)
aPiecekcada0d72024-01-11 15:04:12 +0100962{
963 LY_ERR ret;
aPiecek743184b2024-02-01 13:25:56 +0100964 struct lyd_node *first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100965
aPiecek743184b2024-02-01 13:25:56 +0100966 assert((parent || first_dst_p) && first_src && !first_src->prev->next);
aPiecekcada0d72024-01-11 15:04:12 +0100967
aPiecek743184b2024-02-01 13:25:56 +0100968 if (!first_dst_p || !*first_dst_p) {
969 first_dst = lyd_child(parent);
aPiecekcada0d72024-01-11 15:04:12 +0100970 } else {
aPiecek743184b2024-02-01 13:25:56 +0100971 first_dst = *first_dst_p;
972 }
973
974 if (first_dst) {
975 ret = lyd_move_nodes_by_schema(&first_dst, first_src);
976 } else {
977 ret = lyd_move_nodes_at_once(parent, first_src);
978 first_dst = first_src;
979 }
980
981 if (first_dst_p) {
982 *first_dst_p = first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100983 }
984
985 return ret;
986}
987
988/**
Michal Vasko717a4c32020-12-07 09:40:10 +0100989 * @brief Check schema place of a node to be inserted.
990 *
991 * @param[in] parent Schema node of the parent data node.
992 * @param[in] sibling Schema node of a sibling data node.
993 * @param[in] schema Schema node if the data node to be inserted.
994 * @return LY_SUCCESS on success.
995 * @return LY_EINVAL if the place is invalid.
996 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100997static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100998lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema)
Michal Vaskof03ed032020-03-04 13:31:44 +0100999{
1000 const struct lysc_node *par2;
1001
Michal Vasko62ed12d2020-05-21 10:08:25 +02001002 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01001003 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
1004 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001005
Michal Vasko717a4c32020-12-07 09:40:10 +01001006 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01001007 /* opaque nodes can be inserted wherever */
1008 return LY_SUCCESS;
1009 }
1010
Michal Vasko717a4c32020-12-07 09:40:10 +01001011 if (!parent) {
1012 parent = lysc_data_parent(sibling);
1013 }
1014
Michal Vaskof03ed032020-03-04 13:31:44 +01001015 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001016 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001017
1018 if (parent) {
1019 /* inner node */
1020 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001021 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02001022 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001023 return LY_EINVAL;
1024 }
1025 } else {
1026 /* top-level node */
1027 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001028 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001029 return LY_EINVAL;
1030 }
1031 }
1032
1033 return LY_SUCCESS;
1034}
1035
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001036LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001037lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001038{
Michal Vasko0ab974d2021-02-24 13:18:26 +01001039 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001040 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001041
Michal Vasko717a4c32020-12-07 09:40:10 +01001042 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001043
aPiecekcada0d72024-01-11 15:04:12 +01001044 if (node->parent || node->prev->next || !node->next) {
aPiecekdfde77d2024-01-15 15:16:01 +01001045 LY_CHECK_RET(lyd_unlink_tree(node));
aPiecek1462ab12024-02-07 09:13:29 +01001046 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_DEFAULT);
aPiecekcada0d72024-01-11 15:04:12 +01001047 } else {
aPiecekdfde77d2024-01-15 15:16:01 +01001048 LY_CHECK_RET(lyd_move_nodes(parent, NULL, node));
Michal Vaskof03ed032020-03-04 13:31:44 +01001049 }
aPiecekcada0d72024-01-11 15:04:12 +01001050
Michal Vaskof03ed032020-03-04 13:31:44 +01001051 return LY_SUCCESS;
1052}
1053
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001054LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001055lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +01001056{
1057 struct lyd_node *iter;
1058
1059 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
1060 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1061
1062 if (first->schema && (first->schema->flags & LYS_KEY)) {
1063 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
1064 return LY_EINVAL;
1065 }
1066
1067 while (first) {
1068 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +01001069 lyd_unlink(first);
aPiecek1462ab12024-02-07 09:13:29 +01001070 lyd_insert_node(parent, NULL, first, LYD_INSERT_NODE_LAST);
Michal Vaskoddd76592022-01-17 13:34:48 +01001071 first = iter;
1072 }
1073 return LY_SUCCESS;
1074}
1075
1076LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001077lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001078{
aPiecekcada0d72024-01-11 15:04:12 +01001079 struct lyd_node *first_sibling;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001080
Michal Vaskob104f112020-07-17 09:54:54 +02001081 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001082
Michal Vaskob104f112020-07-17 09:54:54 +02001083 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01001084 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001085 }
1086
aPiecekcada0d72024-01-11 15:04:12 +01001087 first_sibling = lyd_first_sibling(sibling);
1088 if (node->parent || node->prev->next || !node->next) {
aPiecekdfde77d2024-01-15 15:16:01 +01001089 LY_CHECK_RET(lyd_unlink_tree(node));
aPiecek1462ab12024-02-07 09:13:29 +01001090 lyd_insert_node(NULL, &first_sibling, node, LYD_INSERT_NODE_DEFAULT);
aPiecekcada0d72024-01-11 15:04:12 +01001091 } else {
aPiecek743184b2024-02-01 13:25:56 +01001092 LY_CHECK_RET(lyd_move_nodes(NULL, &first_sibling, node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001093 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001094
Michal Vaskob104f112020-07-17 09:54:54 +02001095 if (first) {
aPiecek743184b2024-02-01 13:25:56 +01001096 *first = first_sibling;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001097 }
1098
1099 return LY_SUCCESS;
1100}
1101
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001102LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001103lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1104{
Michal Vaskobce230b2022-04-19 09:55:31 +02001105 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001106 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001107
Michal Vasko717a4c32020-12-07 09:40:10 +01001108 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001109
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001110 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001111 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001112 return LY_EINVAL;
1113 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001114 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1115 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001116 return LY_EINVAL;
1117 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001118
Michal Vasko2e784f82024-01-11 09:51:22 +01001119 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001120 lyd_insert_before_node(sibling, node);
1121 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001122
Michal Vaskof03ed032020-03-04 13:31:44 +01001123 return LY_SUCCESS;
1124}
1125
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001126LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001127lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1128{
Michal Vaskobce230b2022-04-19 09:55:31 +02001129 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001130 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001131
Michal Vasko717a4c32020-12-07 09:40:10 +01001132 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001133
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001134 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001135 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001136 return LY_EINVAL;
1137 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001138 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1139 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001140 return LY_EINVAL;
1141 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001142
Michal Vasko2e784f82024-01-11 09:51:22 +01001143 lyd_unlink(node);
aPiecek743184b2024-02-01 13:25:56 +01001144 lyd_insert_after_node(NULL, sibling, node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001145 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001146
Michal Vaskof03ed032020-03-04 13:31:44 +01001147 return LY_SUCCESS;
1148}
1149
Michal Vasko2e784f82024-01-11 09:51:22 +01001150void
aPiecek743184b2024-02-01 13:25:56 +01001151lyd_unlink_ignore_lyds(struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001152{
aPiecek743184b2024-02-01 13:25:56 +01001153 struct lyd_node *first_sibling;
aPiecek6cf1d162023-11-08 16:07:00 +01001154
Michal Vaskob104f112020-07-17 09:54:54 +02001155 /* update hashes while still linked into the tree */
1156 lyd_unlink_hash(node);
1157
stewegf9041a22024-01-18 13:29:12 +01001158 /* unlink leafref nodes */
1159 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
1160 lyd_free_leafref_nodes((struct lyd_node_term *)node);
1161 }
1162
Michal Vaskof03ed032020-03-04 13:31:44 +01001163 /* unlink from siblings */
Michal Vaskof03ed032020-03-04 13:31:44 +01001164 if (node->next) {
1165 node->next->prev = node->prev;
aPiecek743184b2024-02-01 13:25:56 +01001166 if (node->prev->next) {
1167 node->prev->next = node->next;
1168 } else if (first_sibling_p) {
1169 /* unlinking the first node */
1170 *first_sibling_p = node->next;
1171 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001172 } else {
1173 /* unlinking the last node */
aPiecek743184b2024-02-01 13:25:56 +01001174 /* update the "last" pointer from the first node */
1175 if (first_sibling_p && *first_sibling_p) {
1176 (*first_sibling_p)->prev = node->prev;
Michal Vaskof03ed032020-03-04 13:31:44 +01001177 } else {
aPiecek743184b2024-02-01 13:25:56 +01001178 first_sibling = lyd_first_sibling(node);
1179 first_sibling->prev = node->prev;
1180 if (first_sibling_p) {
1181 *first_sibling_p = first_sibling;
Michal Vaskof03ed032020-03-04 13:31:44 +01001182 }
1183 }
aPiecek743184b2024-02-01 13:25:56 +01001184 node->prev->next = NULL;
Michal Vaskof03ed032020-03-04 13:31:44 +01001185 }
1186
1187 /* unlink from parent */
1188 if (node->parent) {
1189 if (node->parent->child == node) {
1190 /* the node is the first child */
1191 node->parent->child = node->next;
1192 }
1193
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001194 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +01001195 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001196
Michal Vaskof03ed032020-03-04 13:31:44 +01001197 node->parent = NULL;
1198 }
1199
1200 node->next = NULL;
1201 node->prev = node;
1202}
1203
aPiecekcada0d72024-01-11 15:04:12 +01001204void
1205lyd_unlink(struct lyd_node *node)
1206{
1207 struct lyd_node *leader;
1208
1209 if (!node) {
1210 return;
1211 }
1212
1213 /* unlink from the lyds tree */
1214 if (lyds_is_supported(node)) {
1215 if (!node->prev->next || (node->prev->schema != node->schema)) {
1216 leader = node;
1217 } else {
1218 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
1219 assert(leader);
1220 }
1221 lyds_unlink(&leader, node);
1222 }
1223
1224 /* unlink data tree */
aPiecek743184b2024-02-01 13:25:56 +01001225 lyd_unlink_ignore_lyds(NULL, node);
aPiecekcada0d72024-01-11 15:04:12 +01001226}
1227
aPiecekdfde77d2024-01-15 15:16:01 +01001228LIBYANG_API_DEF LY_ERR
Michal Vasko2e784f82024-01-11 09:51:22 +01001229lyd_unlink_siblings(struct lyd_node *node)
1230{
aPiecek743184b2024-02-01 13:25:56 +01001231 struct lyd_node *next, *iter, *leader, *start, *first_sibling = NULL;
Michal Vasko2e784f82024-01-11 09:51:22 +01001232
aPiecekcada0d72024-01-11 15:04:12 +01001233 if (lyds_is_supported(node) && node->prev->next && (node->prev->schema == node->schema)) {
1234 /* unlink starts at the non-first item in the (leaf-)list */
1235 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
aPiecek743184b2024-02-01 13:25:56 +01001236 lyds_split(&first_sibling, leader, node, &start);
aPiecekcada0d72024-01-11 15:04:12 +01001237 } else {
1238 /* unlink @p node */
aPiecekdfde77d2024-01-15 15:16:01 +01001239 LY_CHECK_RET(lyd_unlink_check(node));
aPiecekcada0d72024-01-11 15:04:12 +01001240 start = node->next;
aPiecek743184b2024-02-01 13:25:56 +01001241 lyd_unlink_ignore_lyds(&first_sibling, node);
aPiecekcada0d72024-01-11 15:04:12 +01001242 }
Michal Vasko2e784f82024-01-11 09:51:22 +01001243
aPiecekcada0d72024-01-11 15:04:12 +01001244 /* continue unlinking the rest */
1245 LY_LIST_FOR_SAFE(start, next, iter) {
aPiecekdfde77d2024-01-15 15:16:01 +01001246 LY_CHECK_RET(lyd_unlink_check(iter));
aPiecek743184b2024-02-01 13:25:56 +01001247 lyd_unlink_ignore_lyds(&first_sibling, iter);
1248 lyd_insert_after_node(&node, node->prev, iter);
aPiecekcada0d72024-01-11 15:04:12 +01001249 lyd_insert_hash(iter);
Michal Vasko2e784f82024-01-11 09:51:22 +01001250 }
aPiecekdfde77d2024-01-15 15:16:01 +01001251
1252 return LY_SUCCESS;
Michal Vasko2e784f82024-01-11 09:51:22 +01001253}
1254
aPiecekdfde77d2024-01-15 15:16:01 +01001255LIBYANG_API_DEF LY_ERR
Michal Vasko2e784f82024-01-11 09:51:22 +01001256lyd_unlink_tree(struct lyd_node *node)
1257{
aPiecekdfde77d2024-01-15 15:16:01 +01001258 LY_CHECK_RET(lyd_unlink_check(node));
Michal Vasko2e784f82024-01-11 09:51:22 +01001259 lyd_unlink(node);
aPiecekdfde77d2024-01-15 15:16:01 +01001260
1261 return LY_SUCCESS;
Michal Vasko2e784f82024-01-11 09:51:22 +01001262}
1263
Michal Vaskoa5da3292020-08-12 13:10:50 +02001264void
Michal Vasko871a0252020-11-11 18:35:24 +01001265lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02001266{
1267 struct lyd_meta *last, *iter;
1268
1269 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001270
1271 if (!meta) {
1272 return;
1273 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001274
1275 for (iter = meta; iter; iter = iter->next) {
1276 iter->parent = parent;
1277 }
1278
1279 /* insert as the last attribute */
1280 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001281 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001282 last->next = meta;
1283 } else {
1284 parent->meta = meta;
1285 }
1286
1287 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001288 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001289 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001290 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001291 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001292}
1293
aPiecek0e92afc2023-11-08 10:48:02 +01001294void
1295lyd_unlink_meta_single(struct lyd_meta *meta)
1296{
1297 struct lyd_meta *iter;
1298
1299 if (!meta) {
1300 return;
1301 }
1302
1303 if (meta->parent && (meta->parent->meta == meta)) {
1304 meta->parent->meta = meta->next;
1305 } else if (meta->parent) {
1306 for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {}
1307 if (iter->next) {
1308 iter->next = meta->next;
1309 }
1310 }
1311
1312 meta->next = NULL;
1313 meta->parent = NULL;
1314}
1315
aPiecek0b1df642023-11-06 16:15:33 +01001316/**
1317 * @brief Get the annotation definition in the module.
1318 *
1319 * @param[in] mod Metadata module (with the annotation definition).
1320 * @param[in] name Attribute name.
1321 * @param[in] name_len Length of @p name, must be set correctly.
1322 * @return compiled YANG extension instance on success.
1323 */
1324static struct lysc_ext_instance *
1325lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len)
1326{
1327 LY_ARRAY_COUNT_TYPE u;
1328 struct lyplg_ext *plugin;
1329
1330 if (!mod) {
1331 return NULL;
1332 }
1333
1334 LY_ARRAY_FOR(mod->compiled->exts, u) {
1335 plugin = mod->compiled->exts[u].def->plugin;
1336 if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) &&
1337 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
1338 return &mod->compiled->exts[u];
1339 }
1340 }
1341
1342 return NULL;
1343}
1344
Radek Krejci1798aae2020-07-14 13:26:06 +02001345LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001346lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko989cdb42023-10-06 15:32:37 +02001347 size_t name_len, const char *value, size_t value_len, ly_bool is_utf8, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +01001348 void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01001349{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001350 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001351 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001352 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001353 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001354
Michal Vasko9f96a052020-03-10 09:41:45 +01001355 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001356
aPiecek0b1df642023-11-06 16:15:33 +01001357 ant = lyd_get_meta_annotation(mod, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001358 if (!ant) {
1359 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001360 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001361 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001362 ret = LY_EINVAL;
1363 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001364 }
1365
Michal Vasko9f96a052020-03-10 09:41:45 +01001366 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001367 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001368 mt->parent = parent;
1369 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001370 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001371 ret = lyd_value_store(mod->ctx, &mt->value, ant_type, value, value_len, is_utf8, dynamic, format, prefix_data, hints,
Michal Vaskoddd76592022-01-17 13:34:48 +01001372 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001373 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1374 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1375 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001376
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001377 /* insert as the last attribute */
1378 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001379 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001380 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001381 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001382 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001383 }
1384
Michal Vasko9f96a052020-03-10 09:41:45 +01001385 if (meta) {
1386 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001387 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001388
1389cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001390 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001391}
1392
Michal Vaskoa5da3292020-08-12 13:10:50 +02001393void
1394lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1395{
1396 struct lyd_attr *last, *iter;
1397 struct lyd_node_opaq *opaq;
1398
1399 assert(parent && !parent->schema);
1400
1401 if (!attr) {
1402 return;
1403 }
1404
1405 opaq = (struct lyd_node_opaq *)parent;
1406 for (iter = attr; iter; iter = iter->next) {
1407 iter->parent = opaq;
1408 }
1409
1410 /* insert as the last attribute */
1411 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001412 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001413 last->next = attr;
1414 } else {
1415 opaq->attr = attr;
1416 }
1417}
1418
Michal Vasko52927e22020-03-16 17:26:14 +01001419LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001420lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len,
Michal Vasko501af032020-11-11 20:27:44 +01001421 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001422 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints)
Michal Vasko52927e22020-03-16 17:26:14 +01001423{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001424 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001425 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001426
1427 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001428 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001429
Michal Vasko2a3722d2021-06-16 11:52:39 +02001430 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001431 value = "";
1432 }
1433
1434 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001435 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001436
Michal Vaskoad92b672020-11-12 13:11:31 +01001437 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001438 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001439 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001440 }
1441 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001442 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001443 }
1444
Michal Vasko52927e22020-03-16 17:26:14 +01001445 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001446 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1447 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001448 *dynamic = 0;
1449 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001450 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001451 }
Michal Vasko501af032020-11-11 20:27:44 +01001452 at->format = format;
1453 at->val_prefix_data = val_prefix_data;
1454 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001455
1456 /* insert as the last attribute */
1457 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001458 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001459 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001460 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001461 last->next = at;
1462 }
1463
Radek Krejci011e4aa2020-09-04 15:22:31 +02001464finish:
1465 if (ret) {
1466 lyd_free_attr_single(ctx, at);
1467 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001468 *attr = at;
1469 }
1470 return LY_SUCCESS;
1471}
1472
aPiecek2f63f952021-03-30 12:22:18 +02001473/**
1474 * @brief Check the equality of the two schemas from different contexts.
1475 *
1476 * @param schema1 of first node.
1477 * @param schema2 of second node.
1478 * @return 1 if the schemas are equal otherwise 0.
1479 */
1480static ly_bool
1481lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1482{
1483 if (!schema1 && !schema2) {
1484 return 1;
1485 } else if (!schema1 || !schema2) {
1486 return 0;
1487 }
1488
1489 assert(schema1->module->ctx != schema2->module->ctx);
1490
1491 if (schema1->nodetype != schema2->nodetype) {
1492 return 0;
1493 }
1494
1495 if (strcmp(schema1->name, schema2->name)) {
1496 return 0;
1497 }
1498
1499 if (strcmp(schema1->module->name, schema2->module->name)) {
1500 return 0;
1501 }
1502
aPiecek2f63f952021-03-30 12:22:18 +02001503 return 1;
1504}
1505
1506/**
1507 * @brief Check the equality of the schemas for all parent nodes.
1508 *
1509 * Both nodes must be from different contexts.
1510 *
1511 * @param node1 Data of first node.
1512 * @param node2 Data of second node.
1513 * @return 1 if the all related parental schemas are equal otherwise 0.
1514 */
1515static ly_bool
1516lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1517{
1518 const struct lysc_node *parent1, *parent2;
1519
1520 assert(node1 && node2);
1521
1522 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1523 parent1 && parent2;
1524 parent1 = parent1->parent, parent2 = parent2->parent) {
1525 if (!lyd_compare_schema_equal(parent1, parent2)) {
1526 return 0;
1527 }
1528 }
1529
1530 if (parent1 || parent2) {
1531 return 0;
1532 }
1533
1534 return 1;
1535}
1536
1537/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001538 * @brief Compare 2 nodes values including opaque node values.
1539 *
1540 * @param[in] node1 First node to compare.
1541 * @param[in] node2 Second node to compare.
1542 * @return LY_SUCCESS if equal.
1543 * @return LY_ENOT if not equal.
1544 * @return LY_ERR on error.
1545 */
1546static LY_ERR
1547lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1548{
1549 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1550 const char *val1, *val2, *col;
1551 const struct lys_module *mod;
1552 char *val_dyn = NULL;
1553 LY_ERR rc = LY_SUCCESS;
1554
1555 if (!node1->schema) {
1556 opaq1 = (struct lyd_node_opaq *)node1;
1557 }
1558 if (!node2->schema) {
1559 opaq2 = (struct lyd_node_opaq *)node2;
1560 }
1561
1562 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1563 /* opaque XML and opaque XML node */
1564 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1565 opaq2->val_prefix_data)) {
1566 return LY_ENOT;
1567 }
1568 return LY_SUCCESS;
1569 }
1570
1571 /* get their values */
1572 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1573 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1574 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1575 if (!mod) {
1576 /* unable to compare */
1577 return LY_ENOT;
1578 }
1579
1580 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1581 LOGMEM(LYD_CTX(node1));
1582 return LY_EMEM;
1583 }
1584 val1 = val_dyn;
1585 } else {
1586 val1 = lyd_get_value(node1);
1587 }
1588 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1589 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1590 if (!mod) {
1591 return LY_ENOT;
1592 }
1593
1594 assert(!val_dyn);
1595 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1596 LOGMEM(LYD_CTX(node2));
1597 return LY_EMEM;
1598 }
1599 val2 = val_dyn;
1600 } else {
1601 val2 = lyd_get_value(node2);
1602 }
1603
1604 /* compare values */
1605 if (strcmp(val1, val2)) {
1606 rc = LY_ENOT;
1607 }
1608
1609 free(val_dyn);
1610 return rc;
1611}
1612
1613/**
Michal Vaskof277d362023-04-24 09:08:31 +02001614 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1615 *
1616 * Works correctly even if @p node1 and @p node2 have different contexts.
1617 *
1618 * @param[in] node1 The first node to compare.
1619 * @param[in] node2 The second node to compare.
1620 * @param[in] options Various @ref datacompareoptions.
1621 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1622 * @return LY_SUCCESS if the nodes are equivalent.
1623 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001624 */
1625static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001626lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001627 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001628{
aPiecek2f63f952021-03-30 12:22:18 +02001629 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1630 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001631 if (options & LYD_COMPARE_OPAQ) {
1632 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1633 return LY_ENOT;
1634 }
1635 } else {
1636 if (node1->schema != node2->schema) {
1637 return LY_ENOT;
1638 }
aPiecek2f63f952021-03-30 12:22:18 +02001639 }
1640 } else {
1641 /* different contexts */
1642 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1643 return LY_ENOT;
1644 }
1645 if (!parental_schemas_checked) {
1646 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1647 return LY_ENOT;
1648 }
1649 parental_schemas_checked = 1;
1650 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001651 }
1652
Michal Vaskof277d362023-04-24 09:08:31 +02001653 return LY_SUCCESS;
1654}
1655
1656/**
1657 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1658 *
1659 * Works correctly even if @p node1 and @p node2 have different contexts.
1660 *
1661 * @param[in] node1 The first node to compare.
1662 * @param[in] node2 The second node to compare.
1663 * @param[in] options Various @ref datacompareoptions.
1664 * @return LY_SUCCESS if the nodes are equivalent.
1665 * @return LY_ENOT if the nodes are not equivalent.
1666 */
1667static LY_ERR
1668lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1669{
1670 const struct lyd_node *iter1, *iter2;
1671 struct lyd_node_any *any1, *any2;
1672 int len1, len2;
1673 LY_ERR r;
1674
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001675 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001676 return LY_ENOT;
1677 }
aPiecek2f63f952021-03-30 12:22:18 +02001678 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001679
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001680 if (!node1->schema || !node2->schema) {
1681 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001682 return LY_ENOT;
1683 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001684 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1685 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1686 /* compare values only if there are any to compare */
1687 if ((r = lyd_compare_single_value(node1, node2))) {
1688 return r;
1689 }
Michal Vasko52927e22020-03-16 17:26:14 +01001690 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001691
Michal Vasko52927e22020-03-16 17:26:14 +01001692 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001693 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001694 }
1695 return LY_SUCCESS;
1696 } else {
1697 switch (node1->schema->nodetype) {
1698 case LYS_LEAF:
1699 case LYS_LEAFLIST:
1700 if (options & LYD_COMPARE_DEFAULTS) {
1701 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1702 return LY_ENOT;
1703 }
1704 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001705 if ((r = lyd_compare_single_value(node1, node2))) {
1706 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001707 }
1708
aPiecek2f63f952021-03-30 12:22:18 +02001709 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001710 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001711 case LYS_RPC:
1712 case LYS_ACTION:
1713 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001714 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001715 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001716 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001717 }
1718 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001719 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001720 iter1 = lyd_child(node1);
1721 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001722
Michal Vaskoee9b9482023-06-19 13:17:48 +02001723 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001724 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001725 } else if (node1->schema->flags & LYS_KEYLESS) {
1726 /* always equal */
1727 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001728 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001729
1730 /* lists with keys, their equivalence is based on their keys */
1731 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1732 key && (key->flags & LYS_KEY);
1733 key = key->next) {
1734 if (!iter1 || !iter2) {
1735 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1736 }
1737 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1738 LY_CHECK_RET(r);
1739 r = lyd_compare_single_data(iter1, iter2, options);
1740 LY_CHECK_RET(r);
1741
1742 iter1 = iter1->next;
1743 iter2 = iter2->next;
1744 }
1745
1746 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001747 case LYS_ANYXML:
1748 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001749 any1 = (struct lyd_node_any *)node1;
1750 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001751
1752 if (any1->value_type != any2->value_type) {
1753 return LY_ENOT;
1754 }
1755 switch (any1->value_type) {
1756 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001757 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001758 case LYD_ANYDATA_STRING:
1759 case LYD_ANYDATA_XML:
1760 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001761 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1762 return LY_ENOT;
1763 } else if (!any1->value.str && !any2->value.str) {
1764 return LY_SUCCESS;
1765 }
Michal Vasko52927e22020-03-16 17:26:14 +01001766 len1 = strlen(any1->value.str);
1767 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001768 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001769 return LY_ENOT;
1770 }
1771 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001772 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001773 len1 = lyd_lyb_data_length(any1->value.mem);
1774 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001775 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001776 return LY_ENOT;
1777 }
1778 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001779 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001780 }
1781 }
1782
Michal Vaskob7be7a82020-08-20 09:09:04 +02001783 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001784 return LY_EINT;
1785}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001786
Michal Vaskof277d362023-04-24 09:08:31 +02001787/**
1788 * @brief Compare all siblings at a node level.
1789 *
1790 * @param[in] node1 First sibling list.
1791 * @param[in] node2 Second sibling list.
1792 * @param[in] options Various @ref datacompareoptions.
1793 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1794 * @return LY_SUCCESS if equal.
1795 * @return LY_ENOT if not equal.
1796 * @return LY_ERR on error.
1797 */
1798static LY_ERR
1799lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1800 ly_bool parental_schemas_checked)
1801{
1802 LY_ERR r;
1803 const struct lyd_node *iter2;
1804
1805 while (node1 && node2) {
1806 /* schema match */
1807 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1808 LY_CHECK_RET(r);
1809
1810 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1811 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1812 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1813 /* find a matching instance in case they are ordered differently */
1814 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1815 if (r == LY_ENOTFOUND) {
1816 /* no matching instance, data not equal */
1817 r = LY_ENOT;
1818 }
1819 LY_CHECK_RET(r);
1820 } else {
1821 /* compare with the current node */
1822 iter2 = node2;
1823 }
1824
1825 /* data match */
1826 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1827 LY_CHECK_RET(r);
1828
1829 node1 = node1->next;
1830 node2 = node2->next;
1831 }
1832
1833 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1834}
1835
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001836LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001837lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1838{
Michal Vaskof277d362023-04-24 09:08:31 +02001839 LY_ERR r;
1840
1841 if (!node1 || !node2) {
1842 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1843 }
1844
1845 /* schema match */
1846 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1847 return r;
1848 }
1849
1850 /* data match */
1851 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001852}
1853
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001854LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001855lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001856{
Michal Vaskof277d362023-04-24 09:08:31 +02001857 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001858}
1859
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001860LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001861lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1862{
aPiecek0a6705b2023-11-14 14:20:58 +01001863 const struct ly_ctx *ctx;
1864
Michal Vasko21725742020-06-29 11:49:25 +02001865 if (!meta1 || !meta2) {
1866 if (meta1 == meta2) {
1867 return LY_SUCCESS;
1868 } else {
1869 return LY_ENOT;
1870 }
1871 }
1872
aPiecek0a6705b2023-11-14 14:20:58 +01001873 ctx = meta1->annotation->module->ctx;
1874 if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001875 return LY_ENOT;
1876 }
1877
aPiecek0a6705b2023-11-14 14:20:58 +01001878 return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value);
Michal Vasko21725742020-06-29 11:49:25 +02001879}
1880
Radek Krejci22ebdba2019-07-25 13:59:43 +02001881/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001882 * @brief Create a copy of the attribute.
1883 *
1884 * @param[in] attr Attribute to copy.
1885 * @param[in] node Opaque where to append the new attribute.
1886 * @param[out] dup Optional created attribute copy.
1887 * @return LY_ERR value.
1888 */
1889static LY_ERR
1890lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1891{
1892 LY_ERR ret = LY_SUCCESS;
1893 struct lyd_attr *a, *last;
1894 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1895
1896 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1897
1898 /* create a copy */
1899 a = calloc(1, sizeof *attr);
1900 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1901
1902 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1903 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1904 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1905 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1906 a->hints = attr->hints;
1907 a->format = attr->format;
1908 if (attr->val_prefix_data) {
1909 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1910 LY_CHECK_GOTO(ret, finish);
1911 }
1912
1913 /* insert as the last attribute */
1914 a->parent = opaq;
1915 if (opaq->attr) {
1916 for (last = opaq->attr; last->next; last = last->next) {}
1917 last->next = a;
1918 } else {
1919 opaq->attr = a;
1920 }
1921
1922finish:
1923 if (ret) {
1924 lyd_free_attr_single(LYD_CTX(node), a);
1925 } else if (dup) {
1926 *dup = a;
1927 }
1928 return LY_SUCCESS;
1929}
1930
1931/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001932 * @brief Find @p schema equivalent in @p trg_ctx.
1933 *
1934 * @param[in] schema Schema node to find.
1935 * @param[in] trg_ctx Target context to search in.
1936 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001937 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001938 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1939 * @return LY_RRR value.
1940 */
1941static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001942lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1943 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001944{
Michal Vasko9beceb82022-04-05 12:14:15 +02001945 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1946 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001947 char *path;
1948
1949 if (!schema) {
1950 /* opaque node */
1951 *trg_schema = NULL;
1952 return LY_SUCCESS;
1953 }
1954
Michal Vasko9beceb82022-04-05 12:14:15 +02001955 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001956 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001957 trg_parent = parent->schema;
1958 src_parent = lysc_data_parent(schema);
1959 }
1960
1961 do {
1962 /* find the next parent */
1963 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001964 while (lysc_data_parent(sp) != src_parent) {
1965 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001966 }
1967 src_parent = sp;
1968
1969 if (!src_parent->parent) {
1970 /* find the module first */
1971 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1972 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001973 if (log) {
1974 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1975 src_parent->module->name);
1976 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001977 return LY_ENOTFOUND;
1978 }
1979 }
1980
1981 /* find the next parent */
1982 assert(trg_parent || trg_mod);
1983 tp = NULL;
1984 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1985 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1986 break;
1987 }
1988 }
1989 if (!tp) {
1990 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001991 if (log) {
1992 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1993 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1994 free(path);
1995 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001996 return LY_ENOTFOUND;
1997 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001998
Michal Vasko9beceb82022-04-05 12:14:15 +02001999 trg_parent = tp;
2000 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01002001
Michal Vasko9beceb82022-04-05 12:14:15 +02002002 /* success */
2003 *trg_schema = trg_parent;
2004 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01002005}
2006
2007/**
Michal Vasko52927e22020-03-16 17:26:14 +01002008 * @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 +02002009 *
aPiecek55653c92023-11-09 13:43:19 +01002010 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02002011 *
Michal Vaskoddd76592022-01-17 13:34:48 +01002012 * @param[in] node Node to duplicate.
2013 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02002014 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek1462ab12024-02-07 09:13:29 +01002015 * @param[in] insert_order Options for inserting (sorting) duplicated node, @ref insertorder.
Radek Krejcif8b95172020-05-15 14:51:06 +02002016 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2017 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01002018 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
2019 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02002020 */
Michal Vasko52927e22020-03-16 17:26:14 +01002021static LY_ERR
aPiecek1462ab12024-02-07 09:13:29 +01002022lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t insert_order,
Michal Vaskoddd76592022-01-17 13:34:48 +01002023 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002024{
Michal Vasko52927e22020-03-16 17:26:14 +01002025 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002026 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002027 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02002028 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02002029 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01002030 const struct lysc_type *type;
2031 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002032
Michal Vasko52927e22020-03-16 17:26:14 +01002033 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002034
Michal Vasko19175b62022-04-01 09:17:07 +02002035 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02002036 if (options & LYD_DUP_NO_EXT) {
2037 /* no not duplicate this subtree */
2038 return LY_SUCCESS;
2039 }
2040
Michal Vasko19175b62022-04-01 09:17:07 +02002041 /* we need to use the same context */
2042 trg_ctx = LYD_CTX(node);
2043 }
2044
Michal Vasko52927e22020-03-16 17:26:14 +01002045 if (!node->schema) {
2046 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01002047 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01002048 } else {
2049 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002050 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002051 case LYS_ACTION:
2052 case LYS_NOTIF:
2053 case LYS_CONTAINER:
2054 case LYS_LIST:
2055 dup = calloc(1, sizeof(struct lyd_node_inner));
2056 break;
2057 case LYS_LEAF:
2058 case LYS_LEAFLIST:
2059 dup = calloc(1, sizeof(struct lyd_node_term));
2060 break;
2061 case LYS_ANYDATA:
2062 case LYS_ANYXML:
2063 dup = calloc(1, sizeof(struct lyd_node_any));
2064 break;
2065 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01002066 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01002067 ret = LY_EINT;
2068 goto error;
2069 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002070 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002071 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002072
Michal Vaskof6df0a02020-06-16 13:08:34 +02002073 if (options & LYD_DUP_WITH_FLAGS) {
2074 dup->flags = node->flags;
2075 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02002076 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02002077 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02002078 if (options & LYD_DUP_WITH_PRIV) {
2079 dup->priv = node->priv;
2080 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002081 if (trg_ctx == LYD_CTX(node)) {
2082 dup->schema = node->schema;
2083 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01002084 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02002085 if (ret) {
2086 /* has no schema but is not an opaque node */
2087 free(dup);
2088 dup = NULL;
2089 goto error;
2090 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002091 }
Michal Vasko52927e22020-03-16 17:26:14 +01002092 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002093
Michal Vasko9cf62422021-07-01 13:29:32 +02002094 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02002095 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02002096 if (!node->schema) {
2097 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
2098 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
2099 }
2100 } else {
2101 LY_LIST_FOR(node->meta, meta) {
aPiecek41680342023-11-08 10:19:44 +01002102 LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002103 }
Michal Vasko25a32822020-07-09 15:48:22 +02002104 }
2105 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002106
2107 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002108 if (!dup->schema) {
2109 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2110 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2111 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002112
2113 if (options & LYD_DUP_RECURSIVE) {
2114 /* duplicate all the children */
2115 LY_LIST_FOR(orig->child, child) {
aPiecek1462ab12024-02-07 09:13:29 +01002116 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002117 }
2118 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002119 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
2120 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
2121 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
2122 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002123 opaq->hints = orig->hints;
2124 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002125 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002126 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002127 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002128 }
Michal Vasko52927e22020-03-16 17:26:14 +01002129 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2130 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2131 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2132
2133 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01002134 if (trg_ctx == LYD_CTX(node)) {
2135 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
2136 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
2137 } else {
2138 /* store canonical value in the target context */
2139 val_can = lyd_get_value(node);
2140 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02002141 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), 1, NULL, LY_VALUE_CANON, NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +01002142 LYD_HINT_DATA, term->schema, NULL);
2143 LY_CHECK_GOTO(ret, error);
2144 }
Michal Vasko52927e22020-03-16 17:26:14 +01002145 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2146 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2147 struct lyd_node *child;
2148
2149 if (options & LYD_DUP_RECURSIVE) {
2150 /* duplicate all the children */
2151 LY_LIST_FOR(orig->child, child) {
aPiecek1462ab12024-02-07 09:13:29 +01002152 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002153 }
Michal Vasko69730152020-10-09 16:30:07 +02002154 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002155 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02002156 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
aPiecek1462ab12024-02-07 09:13:29 +01002157 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, LYD_INSERT_NODE_LAST, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002158 }
2159 }
2160 lyd_hash(dup);
2161 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002162 dup->hash = node->hash;
2163 any = (struct lyd_node_any *)node;
2164 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002165 }
2166
Michal Vasko52927e22020-03-16 17:26:14 +01002167 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01002168 lyd_insert_node(parent, first, dup, insert_order);
Michal Vasko52927e22020-03-16 17:26:14 +01002169
2170 if (dup_p) {
2171 *dup_p = dup;
2172 }
2173 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002174
2175error:
Michal Vasko52927e22020-03-16 17:26:14 +01002176 lyd_free_tree(dup);
2177 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002178}
2179
Michal Vasko29d674b2021-08-25 11:18:35 +02002180/**
aPieceka7a1de92024-02-12 14:07:12 +01002181 * @brief Duplicate a (leaf-)list and connect it into @p parent (if present) or last of @p first siblings.
2182 *
2183 * @param[in] orig Node to duplicate.
2184 * @param[in] trg_ctx Target context for duplicated nodes.
2185 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2186 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2187 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2188 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
2189 * @return LY_ERR value.
2190 */
2191static LY_ERR
2192lyd_dup_list(const struct lyd_node **orig, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
2193 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
2194{
2195 LY_ERR rc;
2196 struct lyd_node *start, *leader, *dup;
2197 const struct lysc_node *schema;
2198 uint32_t insert_order;
2199
2200 /* duplicate leader */
2201 start = (*orig)->next;
2202 schema = (*orig)->schema;
2203 rc = lyd_dup_r(*orig, trg_ctx, parent, LYD_INSERT_NODE_DEFAULT, first, options, &leader);
2204 LY_CHECK_RET(rc);
2205
2206 if (!start || !start->schema || !LYD_NODE_IS_ALONE(leader)) {
2207 /* no other instances */
2208 if (dup_p) {
2209 *dup_p = leader;
2210 }
2211 return LY_SUCCESS;
2212 }
2213
2214 /* duplicate the rest of the nodes in the (leaf-)list */
2215 insert_order = leader->next ? LYD_INSERT_NODE_LAST_BY_SCHEMA : LYD_INSERT_NODE_LAST;
2216 LY_LIST_FOR(start, *orig) {
2217 if (schema != (*orig)->schema) {
2218 break;
2219 }
2220 rc = lyd_dup_r(*orig, trg_ctx, parent, insert_order, first, options, &dup);
2221 LY_CHECK_GOTO(rc, cleanup);
2222 }
2223
2224cleanup:
2225 if (dup_p) {
2226 *dup_p = leader;
2227 }
2228
2229 return rc;
2230}
2231
2232/**
Michal Vasko29d674b2021-08-25 11:18:35 +02002233 * @brief Get a parent node to connect duplicated subtree to.
2234 *
2235 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01002236 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02002237 * @param[in] parent Initial parent to connect to.
2238 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2239 * @param[out] dup_parent First duplicated parent node, if any.
2240 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
2241 * @return LY_ERR value.
2242 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002243static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002244lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
2245 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002246{
Michal Vasko58d89e92023-05-23 09:56:19 +02002247 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002248 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02002249 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002250
2251 *dup_parent = NULL;
2252 *local_parent = NULL;
2253
Michal Vasko83522192022-07-20 08:07:34 +02002254 if (node->flags & LYD_EXT) {
2255 ext_parent = 1;
2256 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002257 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02002258 if (ext_parent) {
2259 /* use the standard context */
2260 trg_ctx = LYD_CTX(orig_parent);
2261 }
Michal Vasko13c5b212023-02-14 10:03:01 +01002262 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002263 /* stop creating parents, connect what we have into the provided parent */
2264 iter = parent;
2265 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01002266 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
2267 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02002268 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01002269 iter = parent;
2270 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002271 } else {
2272 iter = NULL;
aPiecek1462ab12024-02-07 09:13:29 +01002273 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, LYD_INSERT_NODE_DEFAULT, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002274
2275 /* insert into the previous duplicated parent */
2276 if (*dup_parent) {
aPiecek1462ab12024-02-07 09:13:29 +01002277 lyd_insert_node(iter, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002278 }
2279
2280 /* update the last duplicated parent */
2281 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002282 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002283
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002284 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002285 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02002286 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002287 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002288
Michal Vasko83522192022-07-20 08:07:34 +02002289 if (orig_parent->flags & LYD_EXT) {
2290 ext_parent = 1;
2291 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002292 }
2293
2294 if (repeat && parent) {
2295 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02002296 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
2297 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002298 return LY_EINVAL;
2299 }
2300
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002301 if (*dup_parent && parent) {
2302 /* last insert into a prevously-existing parent */
aPiecek1462ab12024-02-07 09:13:29 +01002303 lyd_insert_node(parent, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002304 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002305 return LY_SUCCESS;
2306}
2307
2308static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002309lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options,
aPieceka7a1de92024-02-12 14:07:12 +01002310 ly_bool nosiblings, struct lyd_node **dup_p)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002311{
2312 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002313 const struct lyd_node *orig; /* original node to be duplicated */
aPieceka7a1de92024-02-12 14:07:12 +01002314 struct lyd_node *first_dup = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002315 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02002316 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
aPieceka7a1de92024-02-12 14:07:12 +01002317 struct lyd_node *dup; /* duplicate node */
2318 struct lyd_node *first_sibling = NULL; /* first sibling node */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002319
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002320 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002321
2322 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002323 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
Michal Vasko20909752021-05-18 16:13:38 +02002324 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002325 } else {
2326 local_parent = parent;
2327 }
2328
Radek Krejci22ebdba2019-07-25 13:59:43 +02002329 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01002330 if (lysc_is_key(orig->schema)) {
2331 if (local_parent) {
2332 /* the key must already exist in the parent */
aPieceka7a1de92024-02-12 14:07:12 +01002333 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, &dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002334 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01002335 } else {
2336 assert(!(options & LYD_DUP_WITH_PARENTS));
2337 /* duplicating a single key, okay, I suppose... */
aPieceka7a1de92024-02-12 14:07:12 +01002338 rc = lyd_dup_r(orig, trg_ctx, NULL, LYD_INSERT_NODE_DEFAULT, &first_sibling, options, &dup);
Michal Vasko35f4d772021-01-12 12:08:57 +01002339 LY_CHECK_GOTO(rc, error);
2340 }
aPieceka7a1de92024-02-12 14:07:12 +01002341 } else if (!nosiblings && orig->schema && (orig->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
2342 /* duplicate the whole (leaf-)list */
2343 rc = lyd_dup_list(&orig, trg_ctx, local_parent, &first_sibling, options, &dup);
2344 LY_CHECK_GOTO(rc, error);
2345 if (!orig) {
2346 break;
2347 }
Michal Vasko35f4d772021-01-12 12:08:57 +01002348 } else {
aPiecek1462ab12024-02-07 09:13:29 +01002349 rc = lyd_dup_r(orig, trg_ctx, local_parent,
2350 options & LYD_DUP_NO_LYDS ? LYD_INSERT_NODE_LAST_BY_SCHEMA : LYD_INSERT_NODE_DEFAULT,
aPieceka7a1de92024-02-12 14:07:12 +01002351 &first_sibling, options, &dup);
Michal Vasko35f4d772021-01-12 12:08:57 +01002352 LY_CHECK_GOTO(rc, error);
2353 }
aPieceka7a1de92024-02-12 14:07:12 +01002354 first_dup = first_dup ? first_dup : dup;
2355
Michal Vasko3a41dff2020-07-15 14:30:28 +02002356 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002357 break;
2358 }
2359 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002360
aPieceka7a1de92024-02-12 14:07:12 +01002361 if (dup_p) {
2362 *dup_p = first_dup;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002363 }
2364 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002365
2366error:
2367 if (top) {
2368 lyd_free_tree(top);
aPieceka7a1de92024-02-12 14:07:12 +01002369 } else if (first_dup) {
2370 lyd_free_siblings(first_dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002371 } else {
aPieceka7a1de92024-02-12 14:07:12 +01002372 lyd_free_siblings(dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002373 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002374 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002375}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002376
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002377/**
2378 * @brief Check the context of node and parent when duplicating nodes.
2379 *
2380 * @param[in] node Node to duplicate.
2381 * @param[in] parent Parent of the duplicated node(s).
2382 * @return LY_ERR value.
2383 */
2384static LY_ERR
2385lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2386{
2387 const struct lyd_node *iter;
2388
2389 if (!node || !parent) {
2390 return LY_SUCCESS;
2391 }
2392
2393 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2394 /* try to find top-level ext data parent */
2395 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2396
2397 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002398 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002399 return LY_EINVAL;
2400 }
2401 }
2402
2403 return LY_SUCCESS;
2404}
2405
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002406LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002407lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002408{
Michal Vaskoddd76592022-01-17 13:34:48 +01002409 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002410 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002411
Michal Vasko58d89e92023-05-23 09:56:19 +02002412 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002413}
2414
2415LIBYANG_API_DEF LY_ERR
2416lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2417 uint32_t options, struct lyd_node **dup)
2418{
2419 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2420
Michal Vasko58d89e92023-05-23 09:56:19 +02002421 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002422}
2423
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002424LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002425lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002426{
Michal Vaskoddd76592022-01-17 13:34:48 +01002427 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002428 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002429
Michal Vasko58d89e92023-05-23 09:56:19 +02002430 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002431}
2432
2433LIBYANG_API_DEF LY_ERR
2434lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2435 uint32_t options, struct lyd_node **dup)
2436{
2437 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2438
Michal Vasko58d89e92023-05-23 09:56:19 +02002439 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002440}
2441
aPiecek41680342023-11-08 10:19:44 +01002442LY_ERR
2443lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
2444 struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002445{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002446 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002447 struct lyd_meta *mt, *last;
aPiecek41680342023-11-08 10:19:44 +01002448 const struct lysc_type *ant_type;
2449 struct lys_module *mod;
2450 const char *val_can;
Michal Vasko25a32822020-07-09 15:48:22 +02002451
aPiecek41680342023-11-08 10:19:44 +01002452 LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL);
Michal Vasko33c48972022-07-20 10:28:07 +02002453
Michal Vasko25a32822020-07-09 15:48:22 +02002454 /* create a copy */
2455 mt = calloc(1, sizeof *mt);
aPiecek41680342023-11-08 10:19:44 +01002456 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM);
2457
2458 if (parent_ctx != meta->annotation->module->ctx) {
2459 /* different contexts */
2460 mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision);
2461
2462 /* annotation */
2463 mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name));
2464 lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
2465 LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL,
2466 "Annotation for metadata %s not found, value duplication failed.", meta->name), finish);
2467
2468 /* duplicate callback expect only the same contexts, so use the store callback */
2469 val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value);
2470 ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, NULL,
2471 LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL);
2472 } else {
2473 /* annotation */
2474 mt->annotation = meta->annotation;
2475 /* duplication of value */
2476 ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value);
2477 }
2478 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish);
2479 LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002480
2481 /* insert as the last attribute */
aPiecek41680342023-11-08 10:19:44 +01002482 mt->parent = parent;
2483 if (parent->meta) {
2484 for (last = parent->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002485 last->next = mt;
2486 } else {
aPiecek41680342023-11-08 10:19:44 +01002487 parent->meta = mt;
Michal Vasko25a32822020-07-09 15:48:22 +02002488 }
2489
Radek Krejci011e4aa2020-09-04 15:22:31 +02002490finish:
2491 if (ret) {
2492 lyd_free_meta_single(mt);
2493 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002494 *dup = mt;
2495 }
2496 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002497}
2498
aPiecek41680342023-11-08 10:19:44 +01002499LIBYANG_API_DEF LY_ERR
2500lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
2501{
2502 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
2503
2504 /* log to node context but value must always use the annotation context */
2505 return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup);
2506}
2507
Michal Vasko4490d312020-06-16 13:08:55 +02002508/**
2509 * @brief Merge a source sibling into target siblings.
2510 *
2511 * @param[in,out] first_trg First target sibling, is updated if top-level.
2512 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002513 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002514 * @param[in] merge_cb Optional merge callback.
2515 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002516 * @param[in] options Merge options.
aPieceke6955f42024-01-19 14:01:54 +01002517 * @param[in] lyds Pool of lyds data which can be reused.
2518 * @param[in,out] leader_p Cached first instance of target (leaf-)list.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002519 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002520 * @return LY_ERR value.
2521 */
2522static LY_ERR
aPieceke6955f42024-01-19 14:01:54 +01002523lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg,
2524 const struct lyd_node **sibling_src_p, lyd_merge_cb merge_cb, void *cb_data, uint16_t options,
2525 struct lyds_pool *lyds, struct lyd_node **leader_p, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002526{
Michal Vasko4490d312020-06-16 13:08:55 +02002527 const struct lyd_node *child_src, *tmp, *sibling_src;
aPieceke6955f42024-01-19 14:01:54 +01002528 struct lyd_node *match_trg, *dup_src, *elem, *leader;
Michal Vaskod1afa502022-01-27 16:18:12 +01002529 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002530 struct lysc_type *type;
aPieceke6955f42024-01-19 14:01:54 +01002531 const struct lysc_node *schema;
Michal Vasko8efac242023-03-30 08:24:56 +02002532 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002533 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002534 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002535
2536 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002537 if (!sibling_src->schema) {
2538 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002539 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002540 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002541 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002542 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002543 } else {
2544 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002545 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002546 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002547 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002548
Michal Vaskocd3f6172021-05-18 16:14:50 +02002549 if (match_trg) {
2550 /* update match as needed */
2551 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2552 } else {
2553 /* first instance of this node */
2554 first_inst = 1;
2555 }
2556
2557 if (match_trg) {
2558 /* call callback */
2559 if (merge_cb) {
2560 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2561 }
2562
Michal Vasko4490d312020-06-16 13:08:55 +02002563 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002564 if (!match_trg->schema) {
2565 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2566 /* update value */
2567 opaq_trg = (struct lyd_node_opaq *)match_trg;
2568 opaq_src = (struct lyd_node_opaq *)sibling_src;
2569
2570 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2571 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2572 opaq_trg->hints = opaq_src->hints;
2573
2574 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2575 opaq_trg->format = opaq_src->format;
2576 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2577 &opaq_trg->val_prefix_data);
2578 }
2579 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2580 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002581 /* since they are different, they cannot both be default */
2582 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2583
Michal Vasko3a41dff2020-07-15 14:30:28 +02002584 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2585 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002586 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002587 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2588 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002589 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002590
2591 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002592 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002593 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002594 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002595 /* update value */
2596 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002597 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002598
2599 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002600 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002601 }
2602
2603 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002604 r = LY_SUCCESS;
aPieceke6955f42024-01-19 14:01:54 +01002605 leader = NULL;
2606 schema = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002607 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
aPieceke6955f42024-01-19 14:01:54 +01002608 if ((options & LYD_MERGE_DESTRUCT) && (schema != child_src->schema) && LYDS_NODE_IS_LEADER(child_src)) {
2609 schema = child_src->schema;
2610 /* unlink lyds data and add them to the pool */
2611 lyds_pool_add((struct lyd_node *)child_src, lyds);
2612 }
2613
2614 r = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src,
2615 merge_cb, cb_data, options, lyds, &leader, &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002616 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002617 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002618 }
2619 }
aPieceke6955f42024-01-19 14:01:54 +01002620
Michal Vaskocd3f6172021-05-18 16:14:50 +02002621 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002622 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002623 } else {
2624 /* node not found, merge it */
2625 if (options & LYD_MERGE_DESTRUCT) {
2626 dup_src = (struct lyd_node *)sibling_src;
aPiecek743184b2024-02-01 13:25:56 +01002627 lyd_unlink_ignore_lyds(NULL, dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002628 /* spend it */
2629 *sibling_src_p = NULL;
2630 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002631 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002632 }
2633
Michal Vasko7e8315b2021-08-03 17:01:06 +02002634 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2635 /* set LYD_NEW for all the new nodes, required for validation */
2636 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2637 elem->flags |= LYD_NEW;
2638 LYD_TREE_DFS_END(dup_src, elem);
2639 }
Michal Vasko4490d312020-06-16 13:08:55 +02002640 }
2641
aPieceke6955f42024-01-19 14:01:54 +01002642 if (lyds->rbn) {
2643 /* insert node and try to reuse free lyds data */
2644 lyds_insert2(parent_trg, first_trg, leader_p, dup_src, lyds);
2645 } else {
2646 /* generic insert node */
aPiecek1462ab12024-02-07 09:13:29 +01002647 lyd_insert_node(parent_trg, first_trg, dup_src, LYD_INSERT_NODE_DEFAULT);
aPieceke6955f42024-01-19 14:01:54 +01002648 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002649
2650 if (first_inst) {
2651 /* remember not to find this instance next time */
2652 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2653 }
2654
2655 /* call callback, no source node */
2656 if (merge_cb) {
2657 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2658 }
Michal Vasko4490d312020-06-16 13:08:55 +02002659 }
2660
2661 return LY_SUCCESS;
2662}
2663
Michal Vasko3a41dff2020-07-15 14:30:28 +02002664static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002665lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2666 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002667{
2668 const struct lyd_node *sibling_src, *tmp;
aPieceke6955f42024-01-19 14:01:54 +01002669 const struct lysc_node *schema;
2670 struct lyd_node *leader;
Michal Vasko8efac242023-03-30 08:24:56 +02002671 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002672 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002673 LY_ERR ret = LY_SUCCESS;
aPieceke6955f42024-01-19 14:01:54 +01002674 struct lyds_pool lyds = {0};
Michal Vasko4490d312020-06-16 13:08:55 +02002675
2676 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002677 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2678 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002679
2680 if (!source) {
2681 /* nothing to merge */
2682 return LY_SUCCESS;
2683 }
2684
Michal Vasko831422b2020-11-24 11:20:51 +01002685 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002686 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002687 return LY_EINVAL;
2688 }
2689
aPieceke6955f42024-01-19 14:01:54 +01002690 leader = NULL;
2691 schema = NULL;
Michal Vasko4490d312020-06-16 13:08:55 +02002692 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002693 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2694 /* skip data nodes from different modules */
2695 continue;
2696 }
2697
aPieceke6955f42024-01-19 14:01:54 +01002698 if ((options & LYD_MERGE_DESTRUCT) && (schema != sibling_src->schema) && LYDS_NODE_IS_LEADER(sibling_src)) {
2699 schema = sibling_src->schema;
2700 /* unlink lyds data and add them to the pool */
2701 lyds_pool_add((struct lyd_node *)sibling_src, &lyds);
2702 }
2703
Radek Krejci857189e2020-09-01 13:26:36 +02002704 first = (sibling_src == source) ? 1 : 0;
aPieceke6955f42024-01-19 14:01:54 +01002705 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options,
2706 &lyds, &leader, &dup_inst);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002707 if (ret) {
2708 break;
2709 }
Michal Vasko4490d312020-06-16 13:08:55 +02002710 if (first && !sibling_src) {
2711 /* source was spent (unlinked), move to the next node */
2712 source = tmp;
2713 }
2714
Michal Vasko3a41dff2020-07-15 14:30:28 +02002715 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002716 break;
2717 }
2718 }
aPieceke6955f42024-01-19 14:01:54 +01002719 lyds_pool_clean(&lyds);
Michal Vasko4490d312020-06-16 13:08:55 +02002720
2721 if (options & LYD_MERGE_DESTRUCT) {
2722 /* free any leftover source data that were not merged */
2723 lyd_free_siblings((struct lyd_node *)source);
2724 }
2725
Michal Vaskocd3f6172021-05-18 16:14:50 +02002726 lyd_dup_inst_free(dup_inst);
2727 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002728}
2729
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002730LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002731lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002732{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002733 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002734}
2735
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002736LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002737lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002738{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002739 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2740}
2741
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002742LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002743lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2744 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2745{
2746 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002747}
2748
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002749static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002750lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002751{
Michal Vasko14654712020-02-06 08:35:21 +01002752 /* ending \0 */
2753 ++reqlen;
2754
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002755 if (reqlen > *buflen) {
2756 if (is_static) {
2757 return LY_EINCOMPLETE;
2758 }
2759
2760 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2761 if (!*buffer) {
2762 return LY_EMEM;
2763 }
2764
2765 *buflen = reqlen;
2766 }
2767
2768 return LY_SUCCESS;
2769}
2770
Michal Vaskod59035b2020-07-08 12:00:06 +02002771LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002772lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002773{
2774 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002775 size_t len;
2776 const char *val;
2777 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002778
Michal Vasko824d0832021-11-04 15:36:51 +01002779 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002780 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002781 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002782 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002783
2784 quot = '\'';
2785 if (strchr(val, '\'')) {
2786 quot = '"';
2787 }
2788 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002789 }
2790
2791 return LY_SUCCESS;
2792}
2793
2794/**
2795 * @brief Append leaf-list value predicate to path.
2796 *
2797 * @param[in] node Node to print.
2798 * @param[in,out] buffer Buffer to print to.
2799 * @param[in,out] buflen Current buffer length.
2800 * @param[in,out] bufused Current number of characters used in @p buffer.
2801 * @param[in] is_static Whether buffer is static or can be reallocated.
2802 * @return LY_ERR
2803 */
2804static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002805lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002806{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002807 size_t len;
2808 const char *val;
2809 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002810
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002811 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002812 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002813 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002814
2815 quot = '\'';
2816 if (strchr(val, '\'')) {
2817 quot = '"';
2818 }
2819 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2820
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002821 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002822}
2823
2824/**
2825 * @brief Append node position (relative to its other instances) predicate to path.
2826 *
2827 * @param[in] node Node to print.
2828 * @param[in,out] buffer Buffer to print to.
2829 * @param[in,out] buflen Current buffer length.
2830 * @param[in,out] bufused Current number of characters used in @p buffer.
2831 * @param[in] is_static Whether buffer is static or can be reallocated.
2832 * @return LY_ERR
2833 */
2834static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002835lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002836{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002837 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002838 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002839 char *val = NULL;
2840 LY_ERR rc;
2841
Michal Vasko50cc0562021-05-18 16:15:43 +02002842 pos = lyd_list_pos(node);
2843 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002844 return LY_EMEM;
2845 }
2846
2847 len = 1 + strlen(val) + 1;
2848 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2849 if (rc != LY_SUCCESS) {
2850 goto cleanup;
2851 }
2852
2853 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2854
2855cleanup:
2856 free(val);
2857 return rc;
2858}
2859
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002860LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002861lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2862{
Radek Krejci857189e2020-09-01 13:26:36 +02002863 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002864 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002865 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002866 const struct lyd_node *iter, *parent;
2867 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002868 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002869
2870 LY_CHECK_ARG_RET(NULL, node, NULL);
2871 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002872 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002873 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002874 } else {
2875 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002876 }
2877
2878 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002879 case LYD_PATH_STD:
2880 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002881 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002882 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002883 ++depth;
2884 }
2885
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002886 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002887 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002888 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002889 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002890iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002891 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002892 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002893 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002894 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002895 if (prev_mod == mod) {
2896 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002897 }
2898
2899 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002900 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2901 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002902 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2903 if (rc != LY_SUCCESS) {
2904 break;
2905 }
2906
2907 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002908 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002909
Michal Vasko790b2bc2020-08-03 13:35:06 +02002910 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002911 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002912 switch (iter->schema->nodetype) {
2913 case LYS_LIST:
2914 if (iter->schema->flags & LYS_KEYLESS) {
2915 /* print its position */
2916 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2917 } else {
2918 /* print all list keys in predicates */
2919 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2920 }
2921 break;
2922 case LYS_LEAFLIST:
2923 if (iter->schema->flags & LYS_CONFIG_W) {
2924 /* print leaf-list value */
2925 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2926 } else {
2927 /* print its position */
2928 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2929 }
2930 break;
2931 default:
2932 /* nothing to print more */
2933 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002934 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002935 }
2936 if (rc != LY_SUCCESS) {
2937 break;
2938 }
2939
Michal Vasko14654712020-02-06 08:35:21 +01002940 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002941 }
2942 break;
2943 }
2944
2945 return buffer;
2946}
Michal Vaskoe444f752020-02-10 12:20:06 +01002947
Michal Vaskodbf3e652022-10-21 08:46:25 +02002948char *
2949lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2950{
2951 uint32_t depth;
2952 size_t bufused = 0, buflen = 0, len;
2953 char *buffer = NULL;
2954 const struct lyd_node *iter, *parent;
2955 const struct lys_module *mod, *prev_mod;
2956 LY_ERR rc = LY_SUCCESS;
2957
2958 switch (pathtype) {
2959 case LYD_PATH_STD:
2960 case LYD_PATH_STD_NO_LAST_PRED:
2961 for (depth = 1; depth <= dnodes->count; ++depth) {
2962 /* current node */
2963 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002964 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002965
2966 /* parent */
2967 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002968 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2969 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002970 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2971 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002972
2973 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002974 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002975 if (prev_mod == mod) {
2976 mod = NULL;
2977 }
2978
2979 /* realloc string */
2980 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2981 strlen(((struct lyd_node_opaq *)iter)->name.name));
2982 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2983 break;
2984 }
2985
2986 /* print next node */
2987 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2988
2989 /* do not always print the last (first) predicate */
2990 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2991 switch (iter->schema->nodetype) {
2992 case LYS_LIST:
2993 if (iter->schema->flags & LYS_KEYLESS) {
2994 /* print its position */
2995 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2996 } else {
2997 /* print all list keys in predicates */
2998 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2999 }
3000 break;
3001 case LYS_LEAFLIST:
3002 if (iter->schema->flags & LYS_CONFIG_W) {
3003 /* print leaf-list value */
3004 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
3005 } else {
3006 /* print its position */
3007 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
3008 }
3009 break;
3010 default:
3011 /* nothing to print more */
3012 break;
3013 }
3014 }
3015 if (rc) {
3016 break;
3017 }
3018 }
3019 break;
3020 }
3021
3022 return buffer;
3023}
3024
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003025LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02003026lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3027{
3028 struct lyd_meta *ret = NULL;
3029 const struct ly_ctx *ctx;
3030 const char *prefix, *tmp;
3031 char *str;
3032 size_t pref_len, name_len;
3033
3034 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003035 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02003036
3037 if (!first) {
3038 return NULL;
3039 }
3040
3041 ctx = first->annotation->module->ctx;
3042
3043 /* parse the name */
3044 tmp = name;
3045 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3046 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3047 return NULL;
3048 }
3049
3050 /* find the module */
3051 if (prefix) {
3052 str = strndup(prefix, pref_len);
3053 module = ly_ctx_get_module_latest(ctx, str);
3054 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003055 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02003056 }
3057
3058 /* find the metadata */
3059 LY_LIST_FOR(first, first) {
3060 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3061 ret = (struct lyd_meta *)first;
3062 break;
3063 }
3064 }
3065
3066 return ret;
3067}
3068
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003069LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003070lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3071{
Michal Vasko9beceb82022-04-05 12:14:15 +02003072 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01003073 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003074 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01003075
Michal Vaskof03ed032020-03-04 13:31:44 +01003076 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02003077 if (!siblings) {
3078 /* no data */
3079 if (match) {
3080 *match = NULL;
3081 }
3082 return LY_ENOTFOUND;
3083 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003084
Michal Vasko9beceb82022-04-05 12:14:15 +02003085 if (LYD_CTX(siblings) != LYD_CTX(target)) {
3086 /* create a duplicate in this context */
3087 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
3088 target = dup;
3089 }
3090
3091 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
3092 /* schema mismatch */
3093 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01003094 if (match) {
3095 *match = NULL;
3096 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003097 return LY_ENOTFOUND;
3098 }
3099
Michal Vaskoe78faec2021-04-08 17:24:43 +02003100 /* get first sibling */
3101 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01003102
Michal Vasko9e685082021-01-29 14:49:09 +01003103 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02003104 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003105 assert(target->hash);
3106
Michal Vaskoe78faec2021-04-08 17:24:43 +02003107 if (lysc_is_dup_inst_list(target->schema)) {
3108 /* we must search the instances from beginning to find the first matching one */
3109 found = 0;
3110 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003111 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003112 found = 1;
3113 break;
3114 }
3115 }
3116 if (found) {
3117 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02003118 } else {
3119 siblings = NULL;
3120 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003121 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003122 /* find by hash */
3123 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3124 siblings = *match_p;
3125 } else {
3126 /* not found */
3127 siblings = NULL;
3128 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003129 }
3130 } else {
Michal Vasko39311152023-08-07 11:03:41 +02003131 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02003132 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003133 if (lysc_is_dup_inst_list(target->schema)) {
3134 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
3135 break;
3136 }
3137 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02003138 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003139 break;
3140 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003141 }
3142 }
3143 }
3144
Michal Vasko9beceb82022-04-05 12:14:15 +02003145 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003146 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003147 if (match) {
3148 *match = NULL;
3149 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003150 return LY_ENOTFOUND;
3151 }
3152
Michal Vasko9b368d32020-02-14 13:53:31 +01003153 if (match) {
3154 *match = (struct lyd_node *)siblings;
3155 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003156 return LY_SUCCESS;
3157}
3158
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003159LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003160lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
Radek Krejci0f969882020-08-21 16:56:47 +02003161 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003162{
3163 LY_ERR rc;
3164 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02003165 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01003166
Michal Vasko4c583e82020-07-17 12:16:14 +02003167 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02003168 if (!siblings) {
3169 /* no data */
3170 if (match) {
3171 *match = NULL;
3172 }
3173 return LY_ENOTFOUND;
3174 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003175
Michal Vasko9beceb82022-04-05 12:14:15 +02003176 if ((LYD_CTX(siblings) != schema->module->ctx)) {
3177 /* parent of ext nodes is useless */
3178 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01003179 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
3180 /* no schema node in siblings so certainly no data node either */
3181 if (match) {
3182 *match = NULL;
3183 }
3184 return LY_ENOTFOUND;
3185 }
Michal Vasko9beceb82022-04-05 12:14:15 +02003186 }
3187
3188 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3189 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003190 if (match) {
3191 *match = NULL;
3192 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003193 return LY_ENOTFOUND;
3194 }
3195
Michal Vaskof03ed032020-03-04 13:31:44 +01003196 if (key_or_value && !val_len) {
3197 val_len = strlen(key_or_value);
3198 }
3199
Michal Vaskob104f112020-07-17 09:54:54 +02003200 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3201 /* create a data node and find the instance */
3202 if (schema->nodetype == LYS_LEAFLIST) {
3203 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02003204 rc = lyd_create_term(schema, key_or_value, val_len, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003205 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003206 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003207 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003208 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003209 }
3210
3211 /* find it */
3212 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003213 } else {
3214 /* find the first schema node instance */
3215 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003216 }
3217
Michal Vaskoe444f752020-02-10 12:20:06 +01003218 lyd_free_tree(target);
3219 return rc;
3220}
Michal Vaskoccc02342020-05-21 10:09:21 +02003221
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003222LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02003223lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3224{
3225 struct lyd_node **match_p, *first, *iter;
3226 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02003227 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003228
Michal Vasko83ae7772022-06-08 10:01:55 +02003229 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003230 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02003231
3232 LY_CHECK_RET(ly_set_new(set));
3233
3234 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
3235 /* no data or schema mismatch */
3236 return LY_ENOTFOUND;
3237 }
3238
Michal Vaskoee9b9482023-06-19 13:17:48 +02003239 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02003240 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02003241
Michal Vaskoe78faec2021-04-08 17:24:43 +02003242 /* get first sibling */
3243 siblings = lyd_first_sibling(siblings);
3244
3245 parent = siblings->parent;
3246 if (parent && parent->schema && parent->children_ht) {
3247 assert(target->hash);
3248
3249 /* find the first instance */
3250 lyd_find_sibling_first(siblings, target, &first);
3251 if (first) {
3252 /* add it so that it is the first in the set */
3253 if (ly_set_add(*set, first, 1, NULL)) {
3254 goto error;
3255 }
3256
3257 /* find by hash */
3258 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3259 iter = *match_p;
3260 } else {
3261 /* not found */
3262 iter = NULL;
3263 }
3264 while (iter) {
3265 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02003266 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003267 goto error;
3268 }
3269
3270 /* find next instance */
3271 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
3272 iter = NULL;
3273 } else {
3274 iter = *match_p;
3275 }
3276 }
3277 }
3278 } else {
3279 /* no children hash table */
3280 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003281 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003282 ly_set_add(*set, (void *)siblings, 1, NULL);
3283 }
3284 }
3285 }
3286
3287 if (!(*set)->count) {
3288 return LY_ENOTFOUND;
3289 }
3290 return LY_SUCCESS;
3291
3292error:
3293 ly_set_free(*set, NULL);
3294 *set = NULL;
3295 return LY_EMEM;
3296}
3297
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003298LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003299lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3300{
3301 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3302
Michal Vaskoe271a312023-08-15 11:46:30 +02003303 if (first && first->schema) {
3304 first = first->prev;
3305 if (first->schema) {
3306 /* no opaque nodes */
3307 first = NULL;
3308 } else {
3309 /* opaque nodes are at the end, find quickly the first */
3310 while (!first->prev->schema) {
3311 first = first->prev;
3312 }
3313 }
3314 }
3315
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003316 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02003317 assert(!first->schema);
3318 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003319 break;
3320 }
3321 }
3322
3323 if (match) {
3324 *match = (struct lyd_node *)first;
3325 }
3326 return first ? LY_SUCCESS : LY_ENOTFOUND;
3327}
3328
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003329LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003330lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02003331{
Michal Vaskod96e2372023-02-24 16:07:51 +01003332 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003333
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003334 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01003335}
Michal Vaskoccc02342020-05-21 10:09:21 +02003336
Michal Vaskod96e2372023-02-24 16:07:51 +01003337LIBYANG_API_DEF LY_ERR
3338lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
3339{
3340 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003341
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003342 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003343}
Radek Krejcica989142020-11-05 11:32:22 +01003344
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003345LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003346lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
Michal Vaskod96e2372023-02-24 16:07:51 +01003347 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01003348{
Michal Vaskod96e2372023-02-24 16:07:51 +01003349 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01003350
Michal Vaskod96e2372023-02-24 16:07:51 +01003351 *set = NULL;
3352
3353 return lyd_eval_xpath4(ctx_node, tree, NULL, xpath, format, prefix_data, vars, NULL, set, NULL, NULL, NULL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01003354}
3355
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003356LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003357lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003358{
Michal Vaskod96e2372023-02-24 16:07:51 +01003359 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02003360}
3361
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003362LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02003363lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
3364{
3365 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
3366}
3367
3368LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003369lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
3370 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003371{
Michal Vaskod96e2372023-02-24 16:07:51 +01003372 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
3373}
3374
3375LIBYANG_API_DEF LY_ERR
3376lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
3377 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
3378 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
3379{
3380 LY_ERR ret = LY_SUCCESS;
3381 struct lyxp_set xp_set = {0};
3382 struct lyxp_expr *exp = NULL;
3383 uint32_t i;
3384
3385 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
3386 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
3387 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
3388
3389 /* parse expression */
3390 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
3391 LY_CHECK_GOTO(ret, cleanup);
3392
3393 /* evaluate expression */
3394 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
3395 LYXP_IGNORE_WHEN);
3396 LY_CHECK_GOTO(ret, cleanup);
3397
3398 /* return expected result type without or with casting */
3399 if (node_set) {
3400 /* node set */
3401 if (xp_set.type == LYXP_SET_NODE_SET) {
3402 /* transform into a set */
3403 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
3404 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
3405 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
3406 (*node_set)->size = xp_set.used;
3407 for (i = 0; i < xp_set.used; ++i) {
3408 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3409 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
3410 LY_CHECK_GOTO(ret, cleanup);
3411 }
3412 }
3413 if (ret_type) {
3414 *ret_type = LY_XPATH_NODE_SET;
3415 }
3416 } else if (!string && !number && !boolean) {
3417 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
3418 ret = LY_EINVAL;
3419 goto cleanup;
3420 }
3421 }
3422
3423 if (string) {
3424 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
3425 /* cast into string */
3426 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3427 }
3428 if (xp_set.type == LYXP_SET_STRING) {
3429 /* string */
3430 *string = xp_set.val.str;
3431 xp_set.val.str = NULL;
3432 if (ret_type) {
3433 *ret_type = LY_XPATH_STRING;
3434 }
3435 }
3436 }
3437
3438 if (number) {
3439 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3440 /* cast into number */
3441 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3442 }
3443 if (xp_set.type == LYXP_SET_NUMBER) {
3444 /* number */
3445 *number = xp_set.val.num;
3446 if (ret_type) {
3447 *ret_type = LY_XPATH_NUMBER;
3448 }
3449 }
3450 }
3451
3452 if (boolean) {
3453 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3454 /* cast into boolean */
3455 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3456 }
3457 if (xp_set.type == LYXP_SET_BOOLEAN) {
3458 /* boolean */
3459 *boolean = xp_set.val.bln;
3460 if (ret_type) {
3461 *ret_type = LY_XPATH_BOOLEAN;
3462 }
3463 }
3464 }
3465
3466cleanup:
3467 lyxp_set_free_content(&xp_set);
3468 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3469 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003470}
3471
Michal Vasko99a77882024-01-04 14:50:51 +01003472/**
3473 * @brief Hash table node equal callback.
3474 */
3475static ly_bool
3476lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3477{
3478 struct lyd_node *node1, *node2;
3479
3480 node1 = *(struct lyd_node **)val1_p;
3481 node2 = *(struct lyd_node **)val2_p;
3482
3483 return node1 == node2;
3484}
3485
3486LIBYANG_API_DEF LY_ERR
3487lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3488{
3489 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003490 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003491 struct lyxp_set xp_set = {0};
3492 struct lyxp_expr *exp = NULL;
3493 struct lyd_node *node, *parent;
3494 struct lyxp_set_hash_node hnode;
3495 struct ly_ht *parent_ht = NULL;
3496 struct ly_set free_set = {0};
3497 uint32_t i, hash;
3498 ly_bool is_result;
3499
3500 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3501
3502 if (!*tree) {
3503 /* nothing to do */
3504 goto cleanup;
3505 }
3506
3507 *tree = lyd_first_sibling(*tree);
3508 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3509
3510 /* parse expression */
3511 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3512 LY_CHECK_GOTO(ret, cleanup);
3513
3514 /* evaluate expression */
3515 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3516 LY_CHECK_GOTO(ret, cleanup);
3517
3518 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003519 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003520 LY_CHECK_GOTO(!parent_ht, cleanup);
3521
3522 for (i = 0; i < xp_set.used; ++i) {
3523 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3524 /* ignore */
3525 continue;
3526 }
3527
3528 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3529 /* add the parent into parent_ht */
3530 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3531 if (ret == LY_EEXIST) {
3532 /* shared parent, we are done */
3533 break;
3534 }
3535 LY_CHECK_GOTO(ret, cleanup);
3536 }
3537 }
3538
3539 hnode.type = LYXP_NODE_ELEM;
3540 LY_LIST_FOR(*tree, parent) {
3541 LYD_TREE_DFS_BEGIN(parent, node) {
3542 if (lysc_is_key(node->schema)) {
3543 /* ignore */
3544 goto next_iter;
3545 }
3546
3547 /* check the results */
3548 is_result = 0;
3549 if (xp_set.ht) {
3550 hnode.node = node;
3551 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3552 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3553 hash = lyht_hash_multi(hash, NULL, 0);
3554
3555 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3556 is_result = 1;
3557 }
3558 } else {
3559 /* not enough elements for a hash table */
3560 for (i = 0; i < xp_set.used; ++i) {
3561 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3562 /* ignore */
3563 continue;
3564 }
3565
3566 if (xp_set.val.nodes[i].node == node) {
3567 is_result = 1;
3568 break;
3569 }
3570 }
3571 }
3572
3573 if (is_result) {
3574 /* keep the whole subtree if the node is in the results */
3575 LYD_TREE_DFS_continue = 1;
3576 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3577 /* free the whole subtree if the node is not even among the selected parents */
3578 ret = ly_set_add(&free_set, node, 1, NULL);
3579 LY_CHECK_GOTO(ret, cleanup);
3580 LYD_TREE_DFS_continue = 1;
3581 } /* else keep the parent node because a subtree is in the results */
3582
3583next_iter:
3584 LYD_TREE_DFS_END(parent, node);
3585 }
3586 }
3587
3588 /* free */
3589 for (i = 0; i < free_set.count; ++i) {
3590 node = free_set.dnodes[i];
3591 if (*tree == node) {
3592 *tree = (*tree)->next;
3593 }
3594 lyd_free_tree(node);
3595 }
3596
3597cleanup:
3598 lyxp_set_free_content(&xp_set);
3599 lyxp_expr_free(ctx, exp);
3600 lyht_free(parent_ht, NULL);
3601 ly_set_erase(&free_set, NULL);
3602 return ret;
3603}
3604
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003605LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003606lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3607{
3608 LY_ERR ret = LY_SUCCESS;
3609 struct lyxp_expr *expr = NULL;
3610 struct ly_path *lypath = NULL;
3611
3612 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3613
3614 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003615 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER,
Michal Vasko32ca49b2023-02-17 15:11:35 +01003616 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003617 LY_CHECK_GOTO(ret, cleanup);
3618
3619 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003620 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003621 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003622 LY_CHECK_GOTO(ret, cleanup);
3623
3624 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003625 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003626
3627cleanup:
3628 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3629 ly_path_free(LYD_CTX(ctx_node), lypath);
3630 return ret;
3631}
3632
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003633LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003634lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3635{
3636 LY_ERR ret;
3637 struct lyd_node *m;
3638
3639 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3640
Michal Vasko90189962023-02-28 12:10:34 +01003641 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003642 if (ret) {
3643 if (match) {
3644 *match = NULL;
3645 }
3646 return LY_ENOTFOUND;
3647 }
3648
3649 if (match) {
3650 *match = m;
3651 }
3652 return LY_SUCCESS;
3653}
Irfand3b351a2023-09-14 14:52:15 +02003654
stewegf9041a22024-01-18 13:29:12 +01003655LY_ERR
3656lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3657{
3658 struct ly_ht *ht;
3659 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003660 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003661
3662 assert(node);
3663 assert(record);
3664
Michal Vaskob46061d2024-01-18 13:58:13 +01003665 *record = NULL;
3666
stewegf9041a22024-01-18 13:29:12 +01003667 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003668 return LY_EDENIED;
3669 }
3670
3671 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003672 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003673 hash = lyht_hash((const char *)&node, sizeof node);
3674
stewegf9041a22024-01-18 13:29:12 +01003675 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3676 if (create) {
3677 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3678 } else {
stewegf9041a22024-01-18 13:29:12 +01003679 return LY_ENOTFOUND;
3680 }
3681 }
3682
3683 return LY_SUCCESS;
3684}
3685
3686LIBYANG_API_DEF LY_ERR
3687lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3688{
3689 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3690
3691 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3692}
3693
3694LY_ERR
3695lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3696{
stewegf9041a22024-01-18 13:29:12 +01003697 const struct lyd_node_term **item = NULL;
3698 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003699 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003700
3701 assert(node);
3702 assert(leafref_node);
3703
3704 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3705 return LY_EDENIED;
3706 }
3707
steweg67388952024-01-25 12:14:50 +01003708 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003709 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3710 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3711 if (rec->leafref_nodes[u] == leafref_node) {
3712 return LY_SUCCESS;
3713 }
3714 }
3715
3716 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3717 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003718
3719 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003720 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003721 LY_ARRAY_FOR(rec->target_nodes, u) {
3722 if (rec->target_nodes[u] == node) {
3723 return LY_SUCCESS;
3724 }
3725 }
3726
3727 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3728 *item = node;
3729
stewegf9041a22024-01-18 13:29:12 +01003730 return LY_SUCCESS;
3731}
3732
3733LIBYANG_API_DEF LY_ERR
3734lyd_leafref_link_node_tree(const struct lyd_node *tree)
3735{
3736 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003737 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003738 char *errmsg;
3739 struct lyd_node_term *leafref_node;
3740 struct lysc_node_leaf *leaf_schema;
3741 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003742 LY_ERR ret = LY_SUCCESS;
3743 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003744
3745 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3746
3747 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3748 return LY_EDENIED;
3749 }
3750
3751 LY_LIST_FOR(tree, sibling) {
3752 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003753 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003754 leafref_node = (struct lyd_node_term *)elem;
3755 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003756
stewegf9041a22024-01-18 13:29:12 +01003757 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3758 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003759 ly_set_free(targets, NULL);
3760 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003761 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003762 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003763 } else {
3764 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003765 for (i = 0; i < targets->count; ++i) {
3766 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3767 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3768 LY_CHECK_GOTO(ret, cleanup);
3769 }
3770 }
stewegf9041a22024-01-18 13:29:12 +01003771 }
3772 }
3773 }
3774 LYD_TREE_DFS_END(sibling, elem);
3775 }
3776 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003777
steweg67388952024-01-25 12:14:50 +01003778cleanup:
3779 ly_set_free(targets, NULL);
3780 return ret;
stewegf9041a22024-01-18 13:29:12 +01003781}
3782
3783LY_ERR
3784lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3785{
3786 LY_ERR ret;
3787 struct lyd_leafref_links_rec *rec;
3788
3789 assert(node);
3790 assert(leafref_node);
3791
3792 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3793 return LY_EDENIED;
3794 }
3795
steweg67388952024-01-25 12:14:50 +01003796 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003797 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3798 if (ret == LY_SUCCESS) {
3799 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003800 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003801 lyd_free_leafref_nodes(node);
3802 }
3803 } else if (ret != LY_ENOTFOUND) {
3804 return ret;
3805 }
3806
steweg67388952024-01-25 12:14:50 +01003807 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003808 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3809 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003810 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3811 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003812 lyd_free_leafref_nodes(leafref_node);
3813 }
3814 } else if (ret != LY_ENOTFOUND) {
3815 return ret;
3816 }
3817
3818 return LY_SUCCESS;
3819}