blob: 9d9f2865ec24bb10118d52e5c4281012ee161a97 [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;
aPiecek6cf1d162023-11-08 16:07:00 +0100729 } else {
aPiecek61880852024-01-11 14:10:29 +0100730 lyd_insert_node_last(parent, first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100731 }
aPiecek6cf1d162023-11-08 16:07:00 +0100732}
733
734void
aPiecek1462ab12024-02-07 09:13:29 +0100735lyd_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 +0100736{
737 LY_ERR ret = LY_SUCCESS;
aPiecek61880852024-01-11 14:10:29 +0100738 struct lyd_node *first_sibling, *leader;
aPiecek6cf1d162023-11-08 16:07:00 +0100739
740 /* inserting list without its keys is not supported */
741 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
742 assert(!parent || !parent->schema ||
743 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
744
aPiecek61880852024-01-11 14:10:29 +0100745 if (!parent && first_sibling_p && (*first_sibling_p)) {
aPiecek6cf1d162023-11-08 16:07:00 +0100746 parent = lyd_parent(*first_sibling_p);
747 }
aPiecek61880852024-01-11 14:10:29 +0100748 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
aPiecek6cf1d162023-11-08 16:07:00 +0100749
aPiecek1462ab12024-02-07 09:13:29 +0100750 if (order == LYD_INSERT_NODE_LAST) {
aPiecek743184b2024-02-01 13:25:56 +0100751 lyd_insert_node_last(parent, &first_sibling, node);
aPiecek1462ab12024-02-07 09:13:29 +0100752 } else if (order == LYD_INSERT_NODE_LAST_BY_SCHEMA) {
753 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek61880852024-01-11 14:10:29 +0100754 } else if (lyds_is_supported(node) &&
755 (lyd_find_sibling_schema(first_sibling, node->schema, &leader) == LY_SUCCESS)) {
aPiecek743184b2024-02-01 13:25:56 +0100756 ret = lyds_insert(&first_sibling, &leader, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100757 if (ret) {
758 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
759 * but insert the node anyway although the nodes will not be sorted.
760 */
761 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted.", node->schema->name);
aPiecek743184b2024-02-01 13:25:56 +0100762 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100763 }
764 } else {
aPiecek743184b2024-02-01 13:25:56 +0100765 lyd_insert_node_ordby_schema(parent, &first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100766 }
Michal Vaskob104f112020-07-17 09:54:54 +0200767
768 /* insert into parent HT */
769 lyd_insert_hash(node);
770
771 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200772 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200773 lyd_hash(parent);
774
775 /* now we can insert even the list into its parent HT */
776 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100777 }
aPiecek61880852024-01-11 14:10:29 +0100778
779 if (first_sibling_p) {
aPiecek743184b2024-02-01 13:25:56 +0100780 *first_sibling_p = first_sibling;
aPiecek61880852024-01-11 14:10:29 +0100781 }
aPiecek1462ab12024-02-07 09:13:29 +0100782
783#ifndef NDEBUG
784 if ((order == LYD_INSERT_NODE_LAST) && lyds_is_supported(node) &&
785 (node->prev->schema == node->schema) && (lyds_compare_single(node->prev, node) > 0)) {
786 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted, inserted node should not be added to the end.",
787 node->schema->name);
788 }
789#endif
Michal Vasko90932a92020-02-12 14:33:03 +0100790}
791
Michal Vasko717a4c32020-12-07 09:40:10 +0100792/**
aPiecekcada0d72024-01-11 15:04:12 +0100793 * @brief Check that @p node can be unlinked.
794 *
795 * @param[in] node Node to check
796 * @return LY_ERR value.
797 */
798static LY_ERR
799lyd_unlink_check(struct lyd_node *node)
800{
801 if (!node) {
802 return LY_SUCCESS;
803 }
804
805 if (lysc_is_key(node->schema) && node->parent) {
806 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
807 LYD_NAME(node));
808 return LY_EINVAL;
809 }
810
811 return LY_SUCCESS;
812}
813
814/**
815 * @brief Move schema instances before anchor or as the last.
816 *
817 * The nodes will remain sorted according to the schema.
818 *
aPiecek743184b2024-02-01 13:25:56 +0100819 * @param[in] first_dst First sibling, destination.
aPiecekcada0d72024-01-11 15:04:12 +0100820 * @param[in] node Starting node, all following nodes with the same schema will be moved.
821 * @param[out] next_p Next node that has a different schema or NULL.
822 * @return LY_ERR value.
823 */
824static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100825lyd_move_nodes_ordby_schema(struct lyd_node **first_dst, struct lyd_node *node, struct lyd_node **next_p)
aPiecekcada0d72024-01-11 15:04:12 +0100826{
aPiecek743184b2024-02-01 13:25:56 +0100827 struct lyd_node *second, *anchor, *iter, *next, *dst, *src, *first_src = NULL;
aPiecekcada0d72024-01-11 15:04:12 +0100828
aPiecek743184b2024-02-01 13:25:56 +0100829 assert(first_dst && *first_dst && !(*first_dst)->prev->next && node && next_p);
aPiecekcada0d72024-01-11 15:04:12 +0100830
aPiecek743184b2024-02-01 13:25:56 +0100831 if ((anchor = lyd_insert_node_find_anchor(*first_dst, node))) {
aPiecekcada0d72024-01-11 15:04:12 +0100832 /* move the first node to the correct place according to the schema */
833 LY_CHECK_RET(lyd_unlink_check(node));
834 second = node->next;
aPiecek743184b2024-02-01 13:25:56 +0100835 lyd_unlink_ignore_lyds(&first_src, node);
aPiecekcada0d72024-01-11 15:04:12 +0100836 lyd_insert_before_node(anchor, node);
837 lyd_insert_hash(node);
aPiecek743184b2024-02-01 13:25:56 +0100838 *first_dst = *first_dst != anchor ? *first_dst : node;
aPiecekcada0d72024-01-11 15:04:12 +0100839 if (!second || (node->schema != second->schema)) {
840 /* no more nodes to move */
841 *next_p = second;
842 return LY_SUCCESS;
843 }
844 dst = node;
845 src = second;
846 } else {
847 /* just move all instances to the end */
aPiecek743184b2024-02-01 13:25:56 +0100848 dst = (*first_dst)->prev;
aPiecekcada0d72024-01-11 15:04:12 +0100849 src = node;
850 }
851
852 /* move the rest of source instances after @p node */
853 LY_LIST_FOR_SAFE(src, next, iter) {
854 LY_CHECK_RET(lyd_unlink_check(iter));
855 if (iter->schema != src->schema) {
856 break;
857 }
aPiecek743184b2024-02-01 13:25:56 +0100858 lyd_unlink_ignore_lyds(&first_src, iter);
859 lyd_insert_after_node(first_dst, dst, iter);
aPiecekcada0d72024-01-11 15:04:12 +0100860 lyd_insert_hash(iter);
861 dst = iter;
862 }
863 *next_p = iter;
864
865 return LY_SUCCESS;
866}
867
868/**
869 * @brief Move nodes regardless of schema.
870 *
871 * The destination for the move is NULL, or a childless parent.
872 *
873 * @param[in] parent Parent to insert into, NULL for top-level sibling.
874 * @param[in] first_src First sibling, all following nodes will be moved.
875 * @return LY_ERR value.
876 */
877static LY_ERR
878lyd_move_nodes_at_once(struct lyd_node *parent, struct lyd_node *first_src)
879{
aPiecek743184b2024-02-01 13:25:56 +0100880 struct lyd_node *start, *next, *iter, *first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100881
882 assert(!lyd_child(parent) && first_src && !first_src->prev->next && !first_src->parent);
883
884 LY_CHECK_RET(lyd_unlink_check(first_src));
885
886 /* move the first node */
887 start = first_src->next;
aPiecek743184b2024-02-01 13:25:56 +0100888 first_dst = first_src;
aPiecekcada0d72024-01-11 15:04:12 +0100889 if (parent) {
aPiecek743184b2024-02-01 13:25:56 +0100890 lyd_unlink_ignore_lyds(&first_src, first_dst);
891 lyd_insert_only_child(parent, first_dst);
892 lyd_insert_hash(first_dst);
aPiecekcada0d72024-01-11 15:04:12 +0100893 } else {
aPiecek743184b2024-02-01 13:25:56 +0100894 lyd_unlink_ignore_lyds(&first_src, first_dst);
aPiecekcada0d72024-01-11 15:04:12 +0100895 }
896
897 /* move the rest of the nodes */
898 LY_LIST_FOR_SAFE(start, next, iter) {
899 LY_CHECK_RET(lyd_unlink_check(iter));
aPiecek743184b2024-02-01 13:25:56 +0100900 lyd_unlink_ignore_lyds(&first_src, iter);
901 lyd_insert_after_node(&first_dst, first_dst->prev, iter);
aPiecekcada0d72024-01-11 15:04:12 +0100902 lyd_insert_hash(iter);
903 }
904
905 return LY_SUCCESS;
906}
907
908/**
909 * @brief Move the nodes in parts according to the schema.
910 *
aPiecek743184b2024-02-01 13:25:56 +0100911 * @param[in,out] first_dst First sibling, destination.
aPiecekcada0d72024-01-11 15:04:12 +0100912 * @param[in] first_src First sibling, all following nodes will be moved.
913 * @return LY_ERR value.
914 */
915static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100916lyd_move_nodes_by_schema(struct lyd_node **first_dst, struct lyd_node *first_src)
aPiecekcada0d72024-01-11 15:04:12 +0100917{
918 LY_ERR ret;
919 struct lyd_node *next, *iter, *leader;
920
aPiecek743184b2024-02-01 13:25:56 +0100921 assert(first_dst && *first_dst && !(*first_dst)->prev->next && first_src &&
922 !first_src->prev->next && !first_src->parent);
aPiecekcada0d72024-01-11 15:04:12 +0100923
924 for (iter = first_src; iter; iter = next) {
925 if (lyds_is_supported(iter) &&
aPiecek743184b2024-02-01 13:25:56 +0100926 (lyd_find_sibling_schema(*first_dst, iter->schema, &leader) == LY_SUCCESS)) {
927 ret = lyds_merge(first_dst, &leader, &first_src, iter, &next);
aPiecekcada0d72024-01-11 15:04:12 +0100928 if (ret) {
929 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
930 * but insert the node anyway although the nodes will not be sorted.
931 */
932 LOGWRN(LYD_CTX(first_src), "Data in \"%s\" are not sorted.", leader->schema->name);
aPiecek743184b2024-02-01 13:25:56 +0100933 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, next, &next));
aPiecekcada0d72024-01-11 15:04:12 +0100934 }
935 } else {
aPiecek743184b2024-02-01 13:25:56 +0100936 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_dst, iter, &next));
aPiecekcada0d72024-01-11 15:04:12 +0100937 }
aPiecekcada0d72024-01-11 15:04:12 +0100938 }
939
940 return LY_SUCCESS;
941}
942
943/**
944 * @brief Move a nodes into parent/siblings.
945 *
946 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek743184b2024-02-01 13:25:56 +0100947 * @param[in,out] first_dst_p First sibling, NULL if no top-level sibling exist yet.
948 * Can be also NULL if @p parent is set.
aPiecekcada0d72024-01-11 15:04:12 +0100949 * @param[in] first_src First sibling, all following nodes will be moved.
950 * @return LY_ERR value.
951 */
952static LY_ERR
aPiecek743184b2024-02-01 13:25:56 +0100953lyd_move_nodes(struct lyd_node *parent, struct lyd_node **first_dst_p, struct lyd_node *first_src)
aPiecekcada0d72024-01-11 15:04:12 +0100954{
955 LY_ERR ret;
aPiecek743184b2024-02-01 13:25:56 +0100956 struct lyd_node *first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100957
aPiecek743184b2024-02-01 13:25:56 +0100958 assert((parent || first_dst_p) && first_src && !first_src->prev->next);
aPiecekcada0d72024-01-11 15:04:12 +0100959
aPiecek743184b2024-02-01 13:25:56 +0100960 if (!first_dst_p || !*first_dst_p) {
961 first_dst = lyd_child(parent);
aPiecekcada0d72024-01-11 15:04:12 +0100962 } else {
aPiecek743184b2024-02-01 13:25:56 +0100963 first_dst = *first_dst_p;
964 }
965
966 if (first_dst) {
967 ret = lyd_move_nodes_by_schema(&first_dst, first_src);
968 } else {
969 ret = lyd_move_nodes_at_once(parent, first_src);
970 first_dst = first_src;
971 }
972
973 if (first_dst_p) {
974 *first_dst_p = first_dst;
aPiecekcada0d72024-01-11 15:04:12 +0100975 }
976
977 return ret;
978}
979
980/**
Michal Vasko717a4c32020-12-07 09:40:10 +0100981 * @brief Check schema place of a node to be inserted.
982 *
983 * @param[in] parent Schema node of the parent data node.
984 * @param[in] sibling Schema node of a sibling data node.
985 * @param[in] schema Schema node if the data node to be inserted.
986 * @return LY_SUCCESS on success.
987 * @return LY_EINVAL if the place is invalid.
988 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100989static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100990lyd_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 +0100991{
992 const struct lysc_node *par2;
993
Michal Vasko62ed12d2020-05-21 10:08:25 +0200994 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100995 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
996 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100997
Michal Vasko717a4c32020-12-07 09:40:10 +0100998 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100999 /* opaque nodes can be inserted wherever */
1000 return LY_SUCCESS;
1001 }
1002
Michal Vasko717a4c32020-12-07 09:40:10 +01001003 if (!parent) {
1004 parent = lysc_data_parent(sibling);
1005 }
1006
Michal Vaskof03ed032020-03-04 13:31:44 +01001007 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001008 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001009
1010 if (parent) {
1011 /* inner node */
1012 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001013 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02001014 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001015 return LY_EINVAL;
1016 }
1017 } else {
1018 /* top-level node */
1019 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001020 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001021 return LY_EINVAL;
1022 }
1023 }
1024
1025 return LY_SUCCESS;
1026}
1027
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001028LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001029lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001030{
Michal Vasko0ab974d2021-02-24 13:18:26 +01001031 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001032 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001033
Michal Vasko717a4c32020-12-07 09:40:10 +01001034 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001035
aPiecekcada0d72024-01-11 15:04:12 +01001036 if (node->parent || node->prev->next || !node->next) {
aPiecekdfde77d2024-01-15 15:16:01 +01001037 LY_CHECK_RET(lyd_unlink_tree(node));
aPiecek1462ab12024-02-07 09:13:29 +01001038 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_DEFAULT);
aPiecekcada0d72024-01-11 15:04:12 +01001039 } else {
aPiecekdfde77d2024-01-15 15:16:01 +01001040 LY_CHECK_RET(lyd_move_nodes(parent, NULL, node));
Michal Vaskof03ed032020-03-04 13:31:44 +01001041 }
aPiecekcada0d72024-01-11 15:04:12 +01001042
Michal Vaskof03ed032020-03-04 13:31:44 +01001043 return LY_SUCCESS;
1044}
1045
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001046LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001047lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +01001048{
1049 struct lyd_node *iter;
1050
1051 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
1052 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1053
1054 if (first->schema && (first->schema->flags & LYS_KEY)) {
1055 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
1056 return LY_EINVAL;
1057 }
1058
1059 while (first) {
1060 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +01001061 lyd_unlink(first);
aPiecek1462ab12024-02-07 09:13:29 +01001062 lyd_insert_node(parent, NULL, first, LYD_INSERT_NODE_LAST);
Michal Vaskoddd76592022-01-17 13:34:48 +01001063 first = iter;
1064 }
1065 return LY_SUCCESS;
1066}
1067
1068LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001069lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001070{
aPiecekcada0d72024-01-11 15:04:12 +01001071 struct lyd_node *first_sibling;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001072
Michal Vaskob104f112020-07-17 09:54:54 +02001073 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001074
Michal Vaskob104f112020-07-17 09:54:54 +02001075 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01001076 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001077 }
1078
aPiecekcada0d72024-01-11 15:04:12 +01001079 first_sibling = lyd_first_sibling(sibling);
1080 if (node->parent || node->prev->next || !node->next) {
aPiecekdfde77d2024-01-15 15:16:01 +01001081 LY_CHECK_RET(lyd_unlink_tree(node));
aPiecek1462ab12024-02-07 09:13:29 +01001082 lyd_insert_node(NULL, &first_sibling, node, LYD_INSERT_NODE_DEFAULT);
aPiecekcada0d72024-01-11 15:04:12 +01001083 } else {
aPiecek743184b2024-02-01 13:25:56 +01001084 LY_CHECK_RET(lyd_move_nodes(NULL, &first_sibling, node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001085 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001086
Michal Vaskob104f112020-07-17 09:54:54 +02001087 if (first) {
aPiecek743184b2024-02-01 13:25:56 +01001088 *first = first_sibling;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001089 }
1090
1091 return LY_SUCCESS;
1092}
1093
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001094LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001095lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1096{
Michal Vaskobce230b2022-04-19 09:55:31 +02001097 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001098 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001099
Michal Vasko717a4c32020-12-07 09:40:10 +01001100 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001101
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001102 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001103 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001104 return LY_EINVAL;
1105 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001106 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1107 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001108 return LY_EINVAL;
1109 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001110
Michal Vasko2e784f82024-01-11 09:51:22 +01001111 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001112 lyd_insert_before_node(sibling, node);
1113 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001114
Michal Vaskof03ed032020-03-04 13:31:44 +01001115 return LY_SUCCESS;
1116}
1117
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001118LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001119lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1120{
Michal Vaskobce230b2022-04-19 09:55:31 +02001121 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001122 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001123
Michal Vasko717a4c32020-12-07 09:40:10 +01001124 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001125
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001126 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001127 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001128 return LY_EINVAL;
1129 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001130 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1131 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001132 return LY_EINVAL;
1133 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001134
Michal Vasko2e784f82024-01-11 09:51:22 +01001135 lyd_unlink(node);
aPiecek743184b2024-02-01 13:25:56 +01001136 lyd_insert_after_node(NULL, sibling, node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001137 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001138
Michal Vaskof03ed032020-03-04 13:31:44 +01001139 return LY_SUCCESS;
1140}
1141
Michal Vasko2e784f82024-01-11 09:51:22 +01001142void
aPiecek743184b2024-02-01 13:25:56 +01001143lyd_unlink_ignore_lyds(struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001144{
aPiecek743184b2024-02-01 13:25:56 +01001145 struct lyd_node *first_sibling;
aPiecek6cf1d162023-11-08 16:07:00 +01001146
Michal Vaskob104f112020-07-17 09:54:54 +02001147 /* update hashes while still linked into the tree */
1148 lyd_unlink_hash(node);
1149
stewegf9041a22024-01-18 13:29:12 +01001150 /* unlink leafref nodes */
1151 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
1152 lyd_free_leafref_nodes((struct lyd_node_term *)node);
1153 }
1154
Michal Vaskof03ed032020-03-04 13:31:44 +01001155 /* unlink from siblings */
Michal Vaskof03ed032020-03-04 13:31:44 +01001156 if (node->next) {
1157 node->next->prev = node->prev;
aPiecek743184b2024-02-01 13:25:56 +01001158 if (node->prev->next) {
1159 node->prev->next = node->next;
1160 } else if (first_sibling_p) {
1161 /* unlinking the first node */
1162 *first_sibling_p = node->next;
1163 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001164 } else {
1165 /* unlinking the last node */
aPiecek743184b2024-02-01 13:25:56 +01001166 /* update the "last" pointer from the first node */
1167 if (first_sibling_p && *first_sibling_p) {
1168 (*first_sibling_p)->prev = node->prev;
Michal Vaskof03ed032020-03-04 13:31:44 +01001169 } else {
aPiecek743184b2024-02-01 13:25:56 +01001170 first_sibling = lyd_first_sibling(node);
1171 first_sibling->prev = node->prev;
1172 if (first_sibling_p) {
1173 *first_sibling_p = first_sibling;
Michal Vaskof03ed032020-03-04 13:31:44 +01001174 }
1175 }
aPiecek743184b2024-02-01 13:25:56 +01001176 node->prev->next = NULL;
Michal Vaskof03ed032020-03-04 13:31:44 +01001177 }
1178
1179 /* unlink from parent */
1180 if (node->parent) {
1181 if (node->parent->child == node) {
1182 /* the node is the first child */
1183 node->parent->child = node->next;
1184 }
1185
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001186 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +01001187 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001188
Michal Vaskof03ed032020-03-04 13:31:44 +01001189 node->parent = NULL;
1190 }
1191
1192 node->next = NULL;
1193 node->prev = node;
1194}
1195
aPiecekcada0d72024-01-11 15:04:12 +01001196void
1197lyd_unlink(struct lyd_node *node)
1198{
1199 struct lyd_node *leader;
1200
1201 if (!node) {
1202 return;
1203 }
1204
1205 /* unlink from the lyds tree */
1206 if (lyds_is_supported(node)) {
1207 if (!node->prev->next || (node->prev->schema != node->schema)) {
1208 leader = node;
1209 } else {
1210 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
1211 assert(leader);
1212 }
1213 lyds_unlink(&leader, node);
1214 }
1215
1216 /* unlink data tree */
aPiecek743184b2024-02-01 13:25:56 +01001217 lyd_unlink_ignore_lyds(NULL, node);
aPiecekcada0d72024-01-11 15:04:12 +01001218}
1219
aPiecekdfde77d2024-01-15 15:16:01 +01001220LIBYANG_API_DEF LY_ERR
Michal Vasko2e784f82024-01-11 09:51:22 +01001221lyd_unlink_siblings(struct lyd_node *node)
1222{
aPiecek743184b2024-02-01 13:25:56 +01001223 struct lyd_node *next, *iter, *leader, *start, *first_sibling = NULL;
Michal Vasko2e784f82024-01-11 09:51:22 +01001224
aPiecekcada0d72024-01-11 15:04:12 +01001225 if (lyds_is_supported(node) && node->prev->next && (node->prev->schema == node->schema)) {
1226 /* unlink starts at the non-first item in the (leaf-)list */
1227 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
aPiecek743184b2024-02-01 13:25:56 +01001228 lyds_split(&first_sibling, leader, node, &start);
aPiecekcada0d72024-01-11 15:04:12 +01001229 } else {
1230 /* unlink @p node */
aPiecekdfde77d2024-01-15 15:16:01 +01001231 LY_CHECK_RET(lyd_unlink_check(node));
aPiecekcada0d72024-01-11 15:04:12 +01001232 start = node->next;
aPiecek743184b2024-02-01 13:25:56 +01001233 lyd_unlink_ignore_lyds(&first_sibling, node);
aPiecekcada0d72024-01-11 15:04:12 +01001234 }
Michal Vasko2e784f82024-01-11 09:51:22 +01001235
aPiecekcada0d72024-01-11 15:04:12 +01001236 /* continue unlinking the rest */
1237 LY_LIST_FOR_SAFE(start, next, iter) {
aPiecekdfde77d2024-01-15 15:16:01 +01001238 LY_CHECK_RET(lyd_unlink_check(iter));
aPiecek743184b2024-02-01 13:25:56 +01001239 lyd_unlink_ignore_lyds(&first_sibling, iter);
1240 lyd_insert_after_node(&node, node->prev, iter);
aPiecekcada0d72024-01-11 15:04:12 +01001241 lyd_insert_hash(iter);
Michal Vasko2e784f82024-01-11 09:51:22 +01001242 }
aPiecekdfde77d2024-01-15 15:16:01 +01001243
1244 return LY_SUCCESS;
Michal Vasko2e784f82024-01-11 09:51:22 +01001245}
1246
aPiecekdfde77d2024-01-15 15:16:01 +01001247LIBYANG_API_DEF LY_ERR
Michal Vasko2e784f82024-01-11 09:51:22 +01001248lyd_unlink_tree(struct lyd_node *node)
1249{
aPiecekdfde77d2024-01-15 15:16:01 +01001250 LY_CHECK_RET(lyd_unlink_check(node));
Michal Vasko2e784f82024-01-11 09:51:22 +01001251 lyd_unlink(node);
aPiecekdfde77d2024-01-15 15:16:01 +01001252
1253 return LY_SUCCESS;
Michal Vasko2e784f82024-01-11 09:51:22 +01001254}
1255
Michal Vaskoa5da3292020-08-12 13:10:50 +02001256void
Michal Vasko871a0252020-11-11 18:35:24 +01001257lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02001258{
1259 struct lyd_meta *last, *iter;
1260
1261 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001262
1263 if (!meta) {
1264 return;
1265 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001266
1267 for (iter = meta; iter; iter = iter->next) {
1268 iter->parent = parent;
1269 }
1270
1271 /* insert as the last attribute */
1272 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001273 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001274 last->next = meta;
1275 } else {
1276 parent->meta = meta;
1277 }
1278
1279 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001280 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001281 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001282 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001283 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001284}
1285
aPiecek0e92afc2023-11-08 10:48:02 +01001286void
1287lyd_unlink_meta_single(struct lyd_meta *meta)
1288{
1289 struct lyd_meta *iter;
1290
1291 if (!meta) {
1292 return;
1293 }
1294
1295 if (meta->parent && (meta->parent->meta == meta)) {
1296 meta->parent->meta = meta->next;
1297 } else if (meta->parent) {
1298 for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {}
1299 if (iter->next) {
1300 iter->next = meta->next;
1301 }
1302 }
1303
1304 meta->next = NULL;
1305 meta->parent = NULL;
1306}
1307
aPiecek0b1df642023-11-06 16:15:33 +01001308/**
1309 * @brief Get the annotation definition in the module.
1310 *
1311 * @param[in] mod Metadata module (with the annotation definition).
1312 * @param[in] name Attribute name.
1313 * @param[in] name_len Length of @p name, must be set correctly.
1314 * @return compiled YANG extension instance on success.
1315 */
1316static struct lysc_ext_instance *
1317lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len)
1318{
1319 LY_ARRAY_COUNT_TYPE u;
1320 struct lyplg_ext *plugin;
1321
1322 if (!mod) {
1323 return NULL;
1324 }
1325
1326 LY_ARRAY_FOR(mod->compiled->exts, u) {
1327 plugin = mod->compiled->exts[u].def->plugin;
1328 if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) &&
1329 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
1330 return &mod->compiled->exts[u];
1331 }
1332 }
1333
1334 return NULL;
1335}
1336
Radek Krejci1798aae2020-07-14 13:26:06 +02001337LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001338lyd_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 +02001339 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 +01001340 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 +01001341{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001342 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001343 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001344 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001345 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001346
Michal Vasko9f96a052020-03-10 09:41:45 +01001347 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001348
aPiecek0b1df642023-11-06 16:15:33 +01001349 ant = lyd_get_meta_annotation(mod, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001350 if (!ant) {
1351 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001352 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001353 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001354 ret = LY_EINVAL;
1355 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001356 }
1357
Michal Vasko9f96a052020-03-10 09:41:45 +01001358 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001359 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001360 mt->parent = parent;
1361 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001362 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001363 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 +01001364 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001365 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1366 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1367 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001368
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001369 /* insert as the last attribute */
1370 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001371 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001372 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001373 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001374 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001375 }
1376
Michal Vasko9f96a052020-03-10 09:41:45 +01001377 if (meta) {
1378 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001379 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001380
1381cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001382 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001383}
1384
Michal Vaskoa5da3292020-08-12 13:10:50 +02001385void
1386lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1387{
1388 struct lyd_attr *last, *iter;
1389 struct lyd_node_opaq *opaq;
1390
1391 assert(parent && !parent->schema);
1392
1393 if (!attr) {
1394 return;
1395 }
1396
1397 opaq = (struct lyd_node_opaq *)parent;
1398 for (iter = attr; iter; iter = iter->next) {
1399 iter->parent = opaq;
1400 }
1401
1402 /* insert as the last attribute */
1403 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001404 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001405 last->next = attr;
1406 } else {
1407 opaq->attr = attr;
1408 }
1409}
1410
Michal Vasko52927e22020-03-16 17:26:14 +01001411LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001412lyd_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 +01001413 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 +02001414 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 +01001415{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001416 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001417 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001418
1419 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001420 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001421
Michal Vasko2a3722d2021-06-16 11:52:39 +02001422 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001423 value = "";
1424 }
1425
1426 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001427 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001428
Michal Vaskoad92b672020-11-12 13:11:31 +01001429 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001430 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001431 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001432 }
1433 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001434 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001435 }
1436
Michal Vasko52927e22020-03-16 17:26:14 +01001437 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001438 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1439 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001440 *dynamic = 0;
1441 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001442 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001443 }
Michal Vasko501af032020-11-11 20:27:44 +01001444 at->format = format;
1445 at->val_prefix_data = val_prefix_data;
1446 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001447
1448 /* insert as the last attribute */
1449 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001450 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001451 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001452 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001453 last->next = at;
1454 }
1455
Radek Krejci011e4aa2020-09-04 15:22:31 +02001456finish:
1457 if (ret) {
1458 lyd_free_attr_single(ctx, at);
1459 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001460 *attr = at;
1461 }
1462 return LY_SUCCESS;
1463}
1464
aPiecek2f63f952021-03-30 12:22:18 +02001465/**
1466 * @brief Check the equality of the two schemas from different contexts.
1467 *
1468 * @param schema1 of first node.
1469 * @param schema2 of second node.
1470 * @return 1 if the schemas are equal otherwise 0.
1471 */
1472static ly_bool
1473lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1474{
1475 if (!schema1 && !schema2) {
1476 return 1;
1477 } else if (!schema1 || !schema2) {
1478 return 0;
1479 }
1480
1481 assert(schema1->module->ctx != schema2->module->ctx);
1482
1483 if (schema1->nodetype != schema2->nodetype) {
1484 return 0;
1485 }
1486
1487 if (strcmp(schema1->name, schema2->name)) {
1488 return 0;
1489 }
1490
1491 if (strcmp(schema1->module->name, schema2->module->name)) {
1492 return 0;
1493 }
1494
aPiecek2f63f952021-03-30 12:22:18 +02001495 return 1;
1496}
1497
1498/**
1499 * @brief Check the equality of the schemas for all parent nodes.
1500 *
1501 * Both nodes must be from different contexts.
1502 *
1503 * @param node1 Data of first node.
1504 * @param node2 Data of second node.
1505 * @return 1 if the all related parental schemas are equal otherwise 0.
1506 */
1507static ly_bool
1508lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1509{
1510 const struct lysc_node *parent1, *parent2;
1511
1512 assert(node1 && node2);
1513
1514 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1515 parent1 && parent2;
1516 parent1 = parent1->parent, parent2 = parent2->parent) {
1517 if (!lyd_compare_schema_equal(parent1, parent2)) {
1518 return 0;
1519 }
1520 }
1521
1522 if (parent1 || parent2) {
1523 return 0;
1524 }
1525
1526 return 1;
1527}
1528
1529/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001530 * @brief Compare 2 nodes values including opaque node values.
1531 *
1532 * @param[in] node1 First node to compare.
1533 * @param[in] node2 Second node to compare.
1534 * @return LY_SUCCESS if equal.
1535 * @return LY_ENOT if not equal.
1536 * @return LY_ERR on error.
1537 */
1538static LY_ERR
1539lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1540{
1541 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1542 const char *val1, *val2, *col;
1543 const struct lys_module *mod;
1544 char *val_dyn = NULL;
1545 LY_ERR rc = LY_SUCCESS;
1546
1547 if (!node1->schema) {
1548 opaq1 = (struct lyd_node_opaq *)node1;
1549 }
1550 if (!node2->schema) {
1551 opaq2 = (struct lyd_node_opaq *)node2;
1552 }
1553
1554 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1555 /* opaque XML and opaque XML node */
1556 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1557 opaq2->val_prefix_data)) {
1558 return LY_ENOT;
1559 }
1560 return LY_SUCCESS;
1561 }
1562
1563 /* get their values */
1564 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1565 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1566 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1567 if (!mod) {
1568 /* unable to compare */
1569 return LY_ENOT;
1570 }
1571
1572 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1573 LOGMEM(LYD_CTX(node1));
1574 return LY_EMEM;
1575 }
1576 val1 = val_dyn;
1577 } else {
1578 val1 = lyd_get_value(node1);
1579 }
1580 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1581 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1582 if (!mod) {
1583 return LY_ENOT;
1584 }
1585
1586 assert(!val_dyn);
1587 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1588 LOGMEM(LYD_CTX(node2));
1589 return LY_EMEM;
1590 }
1591 val2 = val_dyn;
1592 } else {
1593 val2 = lyd_get_value(node2);
1594 }
1595
1596 /* compare values */
1597 if (strcmp(val1, val2)) {
1598 rc = LY_ENOT;
1599 }
1600
1601 free(val_dyn);
1602 return rc;
1603}
1604
1605/**
Michal Vaskof277d362023-04-24 09:08:31 +02001606 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1607 *
1608 * Works correctly even if @p node1 and @p node2 have different contexts.
1609 *
1610 * @param[in] node1 The first node to compare.
1611 * @param[in] node2 The second node to compare.
1612 * @param[in] options Various @ref datacompareoptions.
1613 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1614 * @return LY_SUCCESS if the nodes are equivalent.
1615 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001616 */
1617static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001618lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001619 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001620{
aPiecek2f63f952021-03-30 12:22:18 +02001621 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1622 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001623 if (options & LYD_COMPARE_OPAQ) {
1624 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1625 return LY_ENOT;
1626 }
1627 } else {
1628 if (node1->schema != node2->schema) {
1629 return LY_ENOT;
1630 }
aPiecek2f63f952021-03-30 12:22:18 +02001631 }
1632 } else {
1633 /* different contexts */
1634 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1635 return LY_ENOT;
1636 }
1637 if (!parental_schemas_checked) {
1638 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1639 return LY_ENOT;
1640 }
1641 parental_schemas_checked = 1;
1642 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001643 }
1644
Michal Vaskof277d362023-04-24 09:08:31 +02001645 return LY_SUCCESS;
1646}
1647
1648/**
1649 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1650 *
1651 * Works correctly even if @p node1 and @p node2 have different contexts.
1652 *
1653 * @param[in] node1 The first node to compare.
1654 * @param[in] node2 The second node to compare.
1655 * @param[in] options Various @ref datacompareoptions.
1656 * @return LY_SUCCESS if the nodes are equivalent.
1657 * @return LY_ENOT if the nodes are not equivalent.
1658 */
1659static LY_ERR
1660lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1661{
1662 const struct lyd_node *iter1, *iter2;
1663 struct lyd_node_any *any1, *any2;
1664 int len1, len2;
1665 LY_ERR r;
1666
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001667 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001668 return LY_ENOT;
1669 }
aPiecek2f63f952021-03-30 12:22:18 +02001670 /* 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 +02001671
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001672 if (!node1->schema || !node2->schema) {
1673 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001674 return LY_ENOT;
1675 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001676 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1677 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1678 /* compare values only if there are any to compare */
1679 if ((r = lyd_compare_single_value(node1, node2))) {
1680 return r;
1681 }
Michal Vasko52927e22020-03-16 17:26:14 +01001682 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001683
Michal Vasko52927e22020-03-16 17:26:14 +01001684 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001685 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001686 }
1687 return LY_SUCCESS;
1688 } else {
1689 switch (node1->schema->nodetype) {
1690 case LYS_LEAF:
1691 case LYS_LEAFLIST:
1692 if (options & LYD_COMPARE_DEFAULTS) {
1693 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1694 return LY_ENOT;
1695 }
1696 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001697 if ((r = lyd_compare_single_value(node1, node2))) {
1698 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001699 }
1700
aPiecek2f63f952021-03-30 12:22:18 +02001701 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001702 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001703 case LYS_RPC:
1704 case LYS_ACTION:
1705 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001706 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001707 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001708 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001709 }
1710 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001711 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001712 iter1 = lyd_child(node1);
1713 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001714
Michal Vaskoee9b9482023-06-19 13:17:48 +02001715 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001716 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001717 } else if (node1->schema->flags & LYS_KEYLESS) {
1718 /* always equal */
1719 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001720 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001721
1722 /* lists with keys, their equivalence is based on their keys */
1723 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1724 key && (key->flags & LYS_KEY);
1725 key = key->next) {
1726 if (!iter1 || !iter2) {
1727 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1728 }
1729 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1730 LY_CHECK_RET(r);
1731 r = lyd_compare_single_data(iter1, iter2, options);
1732 LY_CHECK_RET(r);
1733
1734 iter1 = iter1->next;
1735 iter2 = iter2->next;
1736 }
1737
1738 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001739 case LYS_ANYXML:
1740 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001741 any1 = (struct lyd_node_any *)node1;
1742 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001743
1744 if (any1->value_type != any2->value_type) {
1745 return LY_ENOT;
1746 }
1747 switch (any1->value_type) {
1748 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001749 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001750 case LYD_ANYDATA_STRING:
1751 case LYD_ANYDATA_XML:
1752 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001753 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1754 return LY_ENOT;
1755 } else if (!any1->value.str && !any2->value.str) {
1756 return LY_SUCCESS;
1757 }
Michal Vasko52927e22020-03-16 17:26:14 +01001758 len1 = strlen(any1->value.str);
1759 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001760 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001761 return LY_ENOT;
1762 }
1763 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001764 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001765 len1 = lyd_lyb_data_length(any1->value.mem);
1766 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001767 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001768 return LY_ENOT;
1769 }
1770 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001771 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001772 }
1773 }
1774
Michal Vaskob7be7a82020-08-20 09:09:04 +02001775 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001776 return LY_EINT;
1777}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001778
Michal Vaskof277d362023-04-24 09:08:31 +02001779/**
1780 * @brief Compare all siblings at a node level.
1781 *
1782 * @param[in] node1 First sibling list.
1783 * @param[in] node2 Second sibling list.
1784 * @param[in] options Various @ref datacompareoptions.
1785 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1786 * @return LY_SUCCESS if equal.
1787 * @return LY_ENOT if not equal.
1788 * @return LY_ERR on error.
1789 */
1790static LY_ERR
1791lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1792 ly_bool parental_schemas_checked)
1793{
1794 LY_ERR r;
1795 const struct lyd_node *iter2;
1796
1797 while (node1 && node2) {
1798 /* schema match */
1799 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1800 LY_CHECK_RET(r);
1801
1802 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1803 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1804 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1805 /* find a matching instance in case they are ordered differently */
1806 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1807 if (r == LY_ENOTFOUND) {
1808 /* no matching instance, data not equal */
1809 r = LY_ENOT;
1810 }
1811 LY_CHECK_RET(r);
1812 } else {
1813 /* compare with the current node */
1814 iter2 = node2;
1815 }
1816
1817 /* data match */
1818 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1819 LY_CHECK_RET(r);
1820
1821 node1 = node1->next;
1822 node2 = node2->next;
1823 }
1824
1825 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1826}
1827
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001828LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001829lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1830{
Michal Vaskof277d362023-04-24 09:08:31 +02001831 LY_ERR r;
1832
1833 if (!node1 || !node2) {
1834 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1835 }
1836
1837 /* schema match */
1838 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1839 return r;
1840 }
1841
1842 /* data match */
1843 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001844}
1845
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001846LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001847lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001848{
Michal Vaskof277d362023-04-24 09:08:31 +02001849 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001850}
1851
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001852LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001853lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1854{
aPiecek0a6705b2023-11-14 14:20:58 +01001855 const struct ly_ctx *ctx;
1856
Michal Vasko21725742020-06-29 11:49:25 +02001857 if (!meta1 || !meta2) {
1858 if (meta1 == meta2) {
1859 return LY_SUCCESS;
1860 } else {
1861 return LY_ENOT;
1862 }
1863 }
1864
aPiecek0a6705b2023-11-14 14:20:58 +01001865 ctx = meta1->annotation->module->ctx;
1866 if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001867 return LY_ENOT;
1868 }
1869
aPiecek0a6705b2023-11-14 14:20:58 +01001870 return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value);
Michal Vasko21725742020-06-29 11:49:25 +02001871}
1872
Radek Krejci22ebdba2019-07-25 13:59:43 +02001873/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001874 * @brief Create a copy of the attribute.
1875 *
1876 * @param[in] attr Attribute to copy.
1877 * @param[in] node Opaque where to append the new attribute.
1878 * @param[out] dup Optional created attribute copy.
1879 * @return LY_ERR value.
1880 */
1881static LY_ERR
1882lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1883{
1884 LY_ERR ret = LY_SUCCESS;
1885 struct lyd_attr *a, *last;
1886 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1887
1888 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1889
1890 /* create a copy */
1891 a = calloc(1, sizeof *attr);
1892 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1893
1894 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1895 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1896 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1897 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1898 a->hints = attr->hints;
1899 a->format = attr->format;
1900 if (attr->val_prefix_data) {
1901 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1902 LY_CHECK_GOTO(ret, finish);
1903 }
1904
1905 /* insert as the last attribute */
1906 a->parent = opaq;
1907 if (opaq->attr) {
1908 for (last = opaq->attr; last->next; last = last->next) {}
1909 last->next = a;
1910 } else {
1911 opaq->attr = a;
1912 }
1913
1914finish:
1915 if (ret) {
1916 lyd_free_attr_single(LYD_CTX(node), a);
1917 } else if (dup) {
1918 *dup = a;
1919 }
1920 return LY_SUCCESS;
1921}
1922
1923/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001924 * @brief Find @p schema equivalent in @p trg_ctx.
1925 *
1926 * @param[in] schema Schema node to find.
1927 * @param[in] trg_ctx Target context to search in.
1928 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001929 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001930 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1931 * @return LY_RRR value.
1932 */
1933static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001934lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1935 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001936{
Michal Vasko9beceb82022-04-05 12:14:15 +02001937 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1938 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001939 char *path;
1940
1941 if (!schema) {
1942 /* opaque node */
1943 *trg_schema = NULL;
1944 return LY_SUCCESS;
1945 }
1946
Michal Vasko9beceb82022-04-05 12:14:15 +02001947 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001948 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001949 trg_parent = parent->schema;
1950 src_parent = lysc_data_parent(schema);
1951 }
1952
1953 do {
1954 /* find the next parent */
1955 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001956 while (lysc_data_parent(sp) != src_parent) {
1957 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001958 }
1959 src_parent = sp;
1960
1961 if (!src_parent->parent) {
1962 /* find the module first */
1963 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1964 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001965 if (log) {
1966 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1967 src_parent->module->name);
1968 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001969 return LY_ENOTFOUND;
1970 }
1971 }
1972
1973 /* find the next parent */
1974 assert(trg_parent || trg_mod);
1975 tp = NULL;
1976 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1977 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1978 break;
1979 }
1980 }
1981 if (!tp) {
1982 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001983 if (log) {
1984 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1985 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1986 free(path);
1987 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001988 return LY_ENOTFOUND;
1989 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001990
Michal Vasko9beceb82022-04-05 12:14:15 +02001991 trg_parent = tp;
1992 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001993
Michal Vasko9beceb82022-04-05 12:14:15 +02001994 /* success */
1995 *trg_schema = trg_parent;
1996 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001997}
1998
1999/**
Michal Vasko52927e22020-03-16 17:26:14 +01002000 * @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 +02002001 *
aPiecek55653c92023-11-09 13:43:19 +01002002 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02002003 *
Michal Vaskoddd76592022-01-17 13:34:48 +01002004 * @param[in] node Node to duplicate.
2005 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02002006 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek1462ab12024-02-07 09:13:29 +01002007 * @param[in] insert_order Options for inserting (sorting) duplicated node, @ref insertorder.
Radek Krejcif8b95172020-05-15 14:51:06 +02002008 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2009 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01002010 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
2011 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02002012 */
Michal Vasko52927e22020-03-16 17:26:14 +01002013static LY_ERR
aPiecek1462ab12024-02-07 09:13:29 +01002014lyd_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 +01002015 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002016{
Michal Vasko52927e22020-03-16 17:26:14 +01002017 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002018 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002019 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02002020 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02002021 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01002022 const struct lysc_type *type;
2023 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002024
Michal Vasko52927e22020-03-16 17:26:14 +01002025 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002026
Michal Vasko19175b62022-04-01 09:17:07 +02002027 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02002028 if (options & LYD_DUP_NO_EXT) {
2029 /* no not duplicate this subtree */
2030 return LY_SUCCESS;
2031 }
2032
Michal Vasko19175b62022-04-01 09:17:07 +02002033 /* we need to use the same context */
2034 trg_ctx = LYD_CTX(node);
2035 }
2036
Michal Vasko52927e22020-03-16 17:26:14 +01002037 if (!node->schema) {
2038 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01002039 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01002040 } else {
2041 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002042 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002043 case LYS_ACTION:
2044 case LYS_NOTIF:
2045 case LYS_CONTAINER:
2046 case LYS_LIST:
2047 dup = calloc(1, sizeof(struct lyd_node_inner));
2048 break;
2049 case LYS_LEAF:
2050 case LYS_LEAFLIST:
2051 dup = calloc(1, sizeof(struct lyd_node_term));
2052 break;
2053 case LYS_ANYDATA:
2054 case LYS_ANYXML:
2055 dup = calloc(1, sizeof(struct lyd_node_any));
2056 break;
2057 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01002058 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01002059 ret = LY_EINT;
2060 goto error;
2061 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002062 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002063 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002064
Michal Vaskof6df0a02020-06-16 13:08:34 +02002065 if (options & LYD_DUP_WITH_FLAGS) {
2066 dup->flags = node->flags;
2067 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02002068 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02002069 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02002070 if (options & LYD_DUP_WITH_PRIV) {
2071 dup->priv = node->priv;
2072 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002073 if (trg_ctx == LYD_CTX(node)) {
2074 dup->schema = node->schema;
2075 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01002076 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02002077 if (ret) {
2078 /* has no schema but is not an opaque node */
2079 free(dup);
2080 dup = NULL;
2081 goto error;
2082 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002083 }
Michal Vasko52927e22020-03-16 17:26:14 +01002084 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002085
Michal Vasko9cf62422021-07-01 13:29:32 +02002086 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02002087 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02002088 if (!node->schema) {
2089 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
2090 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
2091 }
2092 } else {
2093 LY_LIST_FOR(node->meta, meta) {
aPiecek41680342023-11-08 10:19:44 +01002094 LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002095 }
Michal Vasko25a32822020-07-09 15:48:22 +02002096 }
2097 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002098
2099 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002100 if (!dup->schema) {
2101 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2102 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2103 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002104
2105 if (options & LYD_DUP_RECURSIVE) {
2106 /* duplicate all the children */
2107 LY_LIST_FOR(orig->child, child) {
aPiecek1462ab12024-02-07 09:13:29 +01002108 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 +01002109 }
2110 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002111 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
2112 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
2113 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
2114 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002115 opaq->hints = orig->hints;
2116 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002117 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002118 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002119 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002120 }
Michal Vasko52927e22020-03-16 17:26:14 +01002121 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2122 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2123 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2124
2125 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01002126 if (trg_ctx == LYD_CTX(node)) {
2127 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
2128 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
2129 } else {
2130 /* store canonical value in the target context */
2131 val_can = lyd_get_value(node);
2132 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02002133 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 +01002134 LYD_HINT_DATA, term->schema, NULL);
2135 LY_CHECK_GOTO(ret, error);
2136 }
Michal Vasko52927e22020-03-16 17:26:14 +01002137 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2138 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2139 struct lyd_node *child;
2140
2141 if (options & LYD_DUP_RECURSIVE) {
2142 /* duplicate all the children */
2143 LY_LIST_FOR(orig->child, child) {
aPiecek1462ab12024-02-07 09:13:29 +01002144 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 +02002145 }
Michal Vasko69730152020-10-09 16:30:07 +02002146 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002147 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02002148 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
aPiecek1462ab12024-02-07 09:13:29 +01002149 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 +02002150 }
2151 }
2152 lyd_hash(dup);
2153 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002154 dup->hash = node->hash;
2155 any = (struct lyd_node_any *)node;
2156 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002157 }
2158
Michal Vasko52927e22020-03-16 17:26:14 +01002159 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01002160 lyd_insert_node(parent, first, dup, insert_order);
Michal Vasko52927e22020-03-16 17:26:14 +01002161
2162 if (dup_p) {
2163 *dup_p = dup;
2164 }
2165 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002166
2167error:
Michal Vasko52927e22020-03-16 17:26:14 +01002168 lyd_free_tree(dup);
2169 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002170}
2171
Michal Vasko29d674b2021-08-25 11:18:35 +02002172/**
2173 * @brief Get a parent node to connect duplicated subtree to.
2174 *
2175 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01002176 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02002177 * @param[in] parent Initial parent to connect to.
2178 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2179 * @param[out] dup_parent First duplicated parent node, if any.
2180 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
2181 * @return LY_ERR value.
2182 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002183static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002184lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
2185 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002186{
Michal Vasko58d89e92023-05-23 09:56:19 +02002187 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002188 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02002189 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002190
2191 *dup_parent = NULL;
2192 *local_parent = NULL;
2193
Michal Vasko83522192022-07-20 08:07:34 +02002194 if (node->flags & LYD_EXT) {
2195 ext_parent = 1;
2196 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002197 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02002198 if (ext_parent) {
2199 /* use the standard context */
2200 trg_ctx = LYD_CTX(orig_parent);
2201 }
Michal Vasko13c5b212023-02-14 10:03:01 +01002202 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002203 /* stop creating parents, connect what we have into the provided parent */
2204 iter = parent;
2205 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01002206 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
2207 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02002208 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01002209 iter = parent;
2210 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002211 } else {
2212 iter = NULL;
aPiecek1462ab12024-02-07 09:13:29 +01002213 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 +02002214
2215 /* insert into the previous duplicated parent */
2216 if (*dup_parent) {
aPiecek1462ab12024-02-07 09:13:29 +01002217 lyd_insert_node(iter, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002218 }
2219
2220 /* update the last duplicated parent */
2221 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002222 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002223
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002224 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002225 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02002226 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002227 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002228
Michal Vasko83522192022-07-20 08:07:34 +02002229 if (orig_parent->flags & LYD_EXT) {
2230 ext_parent = 1;
2231 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002232 }
2233
2234 if (repeat && parent) {
2235 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02002236 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
2237 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002238 return LY_EINVAL;
2239 }
2240
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002241 if (*dup_parent && parent) {
2242 /* last insert into a prevously-existing parent */
aPiecek1462ab12024-02-07 09:13:29 +01002243 lyd_insert_node(parent, NULL, *dup_parent, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002244 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002245 return LY_SUCCESS;
2246}
2247
2248static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002249lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options,
Michal Vaskoddd76592022-01-17 13:34:48 +01002250 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002251{
2252 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002253 const struct lyd_node *orig; /* original node to be duplicated */
2254 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002255 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02002256 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002257
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002258 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002259
2260 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002261 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 +02002262 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002263 } else {
2264 local_parent = parent;
2265 }
2266
Radek Krejci22ebdba2019-07-25 13:59:43 +02002267 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01002268 if (lysc_is_key(orig->schema)) {
2269 if (local_parent) {
2270 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02002271 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01002272 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01002273 } else {
2274 assert(!(options & LYD_DUP_WITH_PARENTS));
2275 /* duplicating a single key, okay, I suppose... */
aPiecek1462ab12024-02-07 09:13:29 +01002276 rc = lyd_dup_r(orig, trg_ctx, NULL, LYD_INSERT_NODE_DEFAULT, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002277 LY_CHECK_GOTO(rc, error);
2278 }
2279 } else {
2280 /* if there is no local parent, it will be inserted into first */
aPiecek1462ab12024-02-07 09:13:29 +01002281 rc = lyd_dup_r(orig, trg_ctx, local_parent,
2282 options & LYD_DUP_NO_LYDS ? LYD_INSERT_NODE_LAST_BY_SCHEMA : LYD_INSERT_NODE_DEFAULT,
2283 &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002284 LY_CHECK_GOTO(rc, error);
2285 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002286 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002287 break;
2288 }
2289 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002290
Michal Vasko3a41dff2020-07-15 14:30:28 +02002291 if (dup) {
2292 *dup = first;
2293 }
2294 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002295
2296error:
2297 if (top) {
2298 lyd_free_tree(top);
2299 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002300 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002301 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002302 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002303}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002304
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002305/**
2306 * @brief Check the context of node and parent when duplicating nodes.
2307 *
2308 * @param[in] node Node to duplicate.
2309 * @param[in] parent Parent of the duplicated node(s).
2310 * @return LY_ERR value.
2311 */
2312static LY_ERR
2313lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2314{
2315 const struct lyd_node *iter;
2316
2317 if (!node || !parent) {
2318 return LY_SUCCESS;
2319 }
2320
2321 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2322 /* try to find top-level ext data parent */
2323 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2324
2325 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002326 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002327 return LY_EINVAL;
2328 }
2329 }
2330
2331 return LY_SUCCESS;
2332}
2333
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002334LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002335lyd_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 +02002336{
Michal Vaskoddd76592022-01-17 13:34:48 +01002337 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002338 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002339
Michal Vasko58d89e92023-05-23 09:56:19 +02002340 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002341}
2342
2343LIBYANG_API_DEF LY_ERR
2344lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2345 uint32_t options, struct lyd_node **dup)
2346{
2347 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2348
Michal Vasko58d89e92023-05-23 09:56:19 +02002349 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002350}
2351
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002352LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002353lyd_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 +02002354{
Michal Vaskoddd76592022-01-17 13:34:48 +01002355 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002356 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002357
Michal Vasko58d89e92023-05-23 09:56:19 +02002358 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002359}
2360
2361LIBYANG_API_DEF LY_ERR
2362lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2363 uint32_t options, struct lyd_node **dup)
2364{
2365 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2366
Michal Vasko58d89e92023-05-23 09:56:19 +02002367 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002368}
2369
aPiecek41680342023-11-08 10:19:44 +01002370LY_ERR
2371lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
2372 struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002373{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002374 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002375 struct lyd_meta *mt, *last;
aPiecek41680342023-11-08 10:19:44 +01002376 const struct lysc_type *ant_type;
2377 struct lys_module *mod;
2378 const char *val_can;
Michal Vasko25a32822020-07-09 15:48:22 +02002379
aPiecek41680342023-11-08 10:19:44 +01002380 LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL);
Michal Vasko33c48972022-07-20 10:28:07 +02002381
Michal Vasko25a32822020-07-09 15:48:22 +02002382 /* create a copy */
2383 mt = calloc(1, sizeof *mt);
aPiecek41680342023-11-08 10:19:44 +01002384 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM);
2385
2386 if (parent_ctx != meta->annotation->module->ctx) {
2387 /* different contexts */
2388 mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision);
2389
2390 /* annotation */
2391 mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name));
2392 lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
2393 LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL,
2394 "Annotation for metadata %s not found, value duplication failed.", meta->name), finish);
2395
2396 /* duplicate callback expect only the same contexts, so use the store callback */
2397 val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value);
2398 ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, NULL,
2399 LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL);
2400 } else {
2401 /* annotation */
2402 mt->annotation = meta->annotation;
2403 /* duplication of value */
2404 ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value);
2405 }
2406 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish);
2407 LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002408
2409 /* insert as the last attribute */
aPiecek41680342023-11-08 10:19:44 +01002410 mt->parent = parent;
2411 if (parent->meta) {
2412 for (last = parent->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002413 last->next = mt;
2414 } else {
aPiecek41680342023-11-08 10:19:44 +01002415 parent->meta = mt;
Michal Vasko25a32822020-07-09 15:48:22 +02002416 }
2417
Radek Krejci011e4aa2020-09-04 15:22:31 +02002418finish:
2419 if (ret) {
2420 lyd_free_meta_single(mt);
2421 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002422 *dup = mt;
2423 }
2424 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002425}
2426
aPiecek41680342023-11-08 10:19:44 +01002427LIBYANG_API_DEF LY_ERR
2428lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
2429{
2430 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
2431
2432 /* log to node context but value must always use the annotation context */
2433 return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup);
2434}
2435
Michal Vasko4490d312020-06-16 13:08:55 +02002436/**
2437 * @brief Merge a source sibling into target siblings.
2438 *
2439 * @param[in,out] first_trg First target sibling, is updated if top-level.
2440 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002441 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002442 * @param[in] merge_cb Optional merge callback.
2443 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002444 * @param[in] options Merge options.
aPieceke6955f42024-01-19 14:01:54 +01002445 * @param[in] lyds Pool of lyds data which can be reused.
2446 * @param[in,out] leader_p Cached first instance of target (leaf-)list.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002447 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002448 * @return LY_ERR value.
2449 */
2450static LY_ERR
aPieceke6955f42024-01-19 14:01:54 +01002451lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg,
2452 const struct lyd_node **sibling_src_p, lyd_merge_cb merge_cb, void *cb_data, uint16_t options,
2453 struct lyds_pool *lyds, struct lyd_node **leader_p, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002454{
Michal Vasko4490d312020-06-16 13:08:55 +02002455 const struct lyd_node *child_src, *tmp, *sibling_src;
aPieceke6955f42024-01-19 14:01:54 +01002456 struct lyd_node *match_trg, *dup_src, *elem, *leader;
Michal Vaskod1afa502022-01-27 16:18:12 +01002457 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002458 struct lysc_type *type;
aPieceke6955f42024-01-19 14:01:54 +01002459 const struct lysc_node *schema;
Michal Vasko8efac242023-03-30 08:24:56 +02002460 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002461 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002462 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002463
2464 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002465 if (!sibling_src->schema) {
2466 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002467 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002468 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002469 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002470 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002471 } else {
2472 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002473 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002474 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002475 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002476
Michal Vaskocd3f6172021-05-18 16:14:50 +02002477 if (match_trg) {
2478 /* update match as needed */
2479 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2480 } else {
2481 /* first instance of this node */
2482 first_inst = 1;
2483 }
2484
2485 if (match_trg) {
2486 /* call callback */
2487 if (merge_cb) {
2488 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2489 }
2490
Michal Vasko4490d312020-06-16 13:08:55 +02002491 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002492 if (!match_trg->schema) {
2493 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2494 /* update value */
2495 opaq_trg = (struct lyd_node_opaq *)match_trg;
2496 opaq_src = (struct lyd_node_opaq *)sibling_src;
2497
2498 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2499 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2500 opaq_trg->hints = opaq_src->hints;
2501
2502 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2503 opaq_trg->format = opaq_src->format;
2504 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2505 &opaq_trg->val_prefix_data);
2506 }
2507 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2508 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002509 /* since they are different, they cannot both be default */
2510 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2511
Michal Vasko3a41dff2020-07-15 14:30:28 +02002512 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2513 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002514 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002515 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2516 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002517 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002518
2519 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002520 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002521 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002522 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002523 /* update value */
2524 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002525 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002526
2527 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002528 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002529 }
2530
2531 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002532 r = LY_SUCCESS;
aPieceke6955f42024-01-19 14:01:54 +01002533 leader = NULL;
2534 schema = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002535 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
aPieceke6955f42024-01-19 14:01:54 +01002536 if ((options & LYD_MERGE_DESTRUCT) && (schema != child_src->schema) && LYDS_NODE_IS_LEADER(child_src)) {
2537 schema = child_src->schema;
2538 /* unlink lyds data and add them to the pool */
2539 lyds_pool_add((struct lyd_node *)child_src, lyds);
2540 }
2541
2542 r = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src,
2543 merge_cb, cb_data, options, lyds, &leader, &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002544 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002545 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002546 }
2547 }
aPieceke6955f42024-01-19 14:01:54 +01002548
Michal Vaskocd3f6172021-05-18 16:14:50 +02002549 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002550 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002551 } else {
2552 /* node not found, merge it */
2553 if (options & LYD_MERGE_DESTRUCT) {
2554 dup_src = (struct lyd_node *)sibling_src;
aPiecek743184b2024-02-01 13:25:56 +01002555 lyd_unlink_ignore_lyds(NULL, dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002556 /* spend it */
2557 *sibling_src_p = NULL;
2558 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002559 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 +02002560 }
2561
Michal Vasko7e8315b2021-08-03 17:01:06 +02002562 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2563 /* set LYD_NEW for all the new nodes, required for validation */
2564 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2565 elem->flags |= LYD_NEW;
2566 LYD_TREE_DFS_END(dup_src, elem);
2567 }
Michal Vasko4490d312020-06-16 13:08:55 +02002568 }
2569
aPieceke6955f42024-01-19 14:01:54 +01002570 if (lyds->rbn) {
2571 /* insert node and try to reuse free lyds data */
2572 lyds_insert2(parent_trg, first_trg, leader_p, dup_src, lyds);
2573 } else {
2574 /* generic insert node */
aPiecek1462ab12024-02-07 09:13:29 +01002575 lyd_insert_node(parent_trg, first_trg, dup_src, LYD_INSERT_NODE_DEFAULT);
aPieceke6955f42024-01-19 14:01:54 +01002576 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002577
2578 if (first_inst) {
2579 /* remember not to find this instance next time */
2580 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2581 }
2582
2583 /* call callback, no source node */
2584 if (merge_cb) {
2585 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2586 }
Michal Vasko4490d312020-06-16 13:08:55 +02002587 }
2588
2589 return LY_SUCCESS;
2590}
2591
Michal Vasko3a41dff2020-07-15 14:30:28 +02002592static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002593lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2594 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002595{
2596 const struct lyd_node *sibling_src, *tmp;
aPieceke6955f42024-01-19 14:01:54 +01002597 const struct lysc_node *schema;
2598 struct lyd_node *leader;
Michal Vasko8efac242023-03-30 08:24:56 +02002599 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002600 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002601 LY_ERR ret = LY_SUCCESS;
aPieceke6955f42024-01-19 14:01:54 +01002602 struct lyds_pool lyds = {0};
Michal Vasko4490d312020-06-16 13:08:55 +02002603
2604 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002605 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2606 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002607
2608 if (!source) {
2609 /* nothing to merge */
2610 return LY_SUCCESS;
2611 }
2612
Michal Vasko831422b2020-11-24 11:20:51 +01002613 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002614 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002615 return LY_EINVAL;
2616 }
2617
aPieceke6955f42024-01-19 14:01:54 +01002618 leader = NULL;
2619 schema = NULL;
Michal Vasko4490d312020-06-16 13:08:55 +02002620 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002621 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2622 /* skip data nodes from different modules */
2623 continue;
2624 }
2625
aPieceke6955f42024-01-19 14:01:54 +01002626 if ((options & LYD_MERGE_DESTRUCT) && (schema != sibling_src->schema) && LYDS_NODE_IS_LEADER(sibling_src)) {
2627 schema = sibling_src->schema;
2628 /* unlink lyds data and add them to the pool */
2629 lyds_pool_add((struct lyd_node *)sibling_src, &lyds);
2630 }
2631
Radek Krejci857189e2020-09-01 13:26:36 +02002632 first = (sibling_src == source) ? 1 : 0;
aPieceke6955f42024-01-19 14:01:54 +01002633 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options,
2634 &lyds, &leader, &dup_inst);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002635 if (ret) {
2636 break;
2637 }
Michal Vasko4490d312020-06-16 13:08:55 +02002638 if (first && !sibling_src) {
2639 /* source was spent (unlinked), move to the next node */
2640 source = tmp;
2641 }
2642
Michal Vasko3a41dff2020-07-15 14:30:28 +02002643 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002644 break;
2645 }
2646 }
aPieceke6955f42024-01-19 14:01:54 +01002647 lyds_pool_clean(&lyds);
Michal Vasko4490d312020-06-16 13:08:55 +02002648
2649 if (options & LYD_MERGE_DESTRUCT) {
2650 /* free any leftover source data that were not merged */
2651 lyd_free_siblings((struct lyd_node *)source);
2652 }
2653
Michal Vaskocd3f6172021-05-18 16:14:50 +02002654 lyd_dup_inst_free(dup_inst);
2655 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002656}
2657
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002658LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002659lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002660{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002661 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002662}
2663
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002664LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002665lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002666{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002667 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2668}
2669
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002670LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002671lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2672 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2673{
2674 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002675}
2676
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002677static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002678lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002679{
Michal Vasko14654712020-02-06 08:35:21 +01002680 /* ending \0 */
2681 ++reqlen;
2682
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002683 if (reqlen > *buflen) {
2684 if (is_static) {
2685 return LY_EINCOMPLETE;
2686 }
2687
2688 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2689 if (!*buffer) {
2690 return LY_EMEM;
2691 }
2692
2693 *buflen = reqlen;
2694 }
2695
2696 return LY_SUCCESS;
2697}
2698
Michal Vaskod59035b2020-07-08 12:00:06 +02002699LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002700lyd_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 +02002701{
2702 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002703 size_t len;
2704 const char *val;
2705 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002706
Michal Vasko824d0832021-11-04 15:36:51 +01002707 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002708 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002709 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002710 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002711
2712 quot = '\'';
2713 if (strchr(val, '\'')) {
2714 quot = '"';
2715 }
2716 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002717 }
2718
2719 return LY_SUCCESS;
2720}
2721
2722/**
2723 * @brief Append leaf-list value predicate to path.
2724 *
2725 * @param[in] node Node to print.
2726 * @param[in,out] buffer Buffer to print to.
2727 * @param[in,out] buflen Current buffer length.
2728 * @param[in,out] bufused Current number of characters used in @p buffer.
2729 * @param[in] is_static Whether buffer is static or can be reallocated.
2730 * @return LY_ERR
2731 */
2732static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002733lyd_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 +02002734{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002735 size_t len;
2736 const char *val;
2737 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002738
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002739 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002740 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002741 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002742
2743 quot = '\'';
2744 if (strchr(val, '\'')) {
2745 quot = '"';
2746 }
2747 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2748
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002749 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002750}
2751
2752/**
2753 * @brief Append node position (relative to its other instances) predicate to path.
2754 *
2755 * @param[in] node Node to print.
2756 * @param[in,out] buffer Buffer to print to.
2757 * @param[in,out] buflen Current buffer length.
2758 * @param[in,out] bufused Current number of characters used in @p buffer.
2759 * @param[in] is_static Whether buffer is static or can be reallocated.
2760 * @return LY_ERR
2761 */
2762static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002763lyd_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 +02002764{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002765 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002766 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002767 char *val = NULL;
2768 LY_ERR rc;
2769
Michal Vasko50cc0562021-05-18 16:15:43 +02002770 pos = lyd_list_pos(node);
2771 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002772 return LY_EMEM;
2773 }
2774
2775 len = 1 + strlen(val) + 1;
2776 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2777 if (rc != LY_SUCCESS) {
2778 goto cleanup;
2779 }
2780
2781 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2782
2783cleanup:
2784 free(val);
2785 return rc;
2786}
2787
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002788LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002789lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2790{
Radek Krejci857189e2020-09-01 13:26:36 +02002791 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002792 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002793 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002794 const struct lyd_node *iter, *parent;
2795 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002796 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002797
2798 LY_CHECK_ARG_RET(NULL, node, NULL);
2799 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002800 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002801 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002802 } else {
2803 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002804 }
2805
2806 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002807 case LYD_PATH_STD:
2808 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002809 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002810 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002811 ++depth;
2812 }
2813
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002814 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002815 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002816 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002817 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002818iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002819 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002820 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002821 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002822 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002823 if (prev_mod == mod) {
2824 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002825 }
2826
2827 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002828 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2829 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002830 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2831 if (rc != LY_SUCCESS) {
2832 break;
2833 }
2834
2835 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002836 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002837
Michal Vasko790b2bc2020-08-03 13:35:06 +02002838 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002839 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002840 switch (iter->schema->nodetype) {
2841 case LYS_LIST:
2842 if (iter->schema->flags & LYS_KEYLESS) {
2843 /* print its position */
2844 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2845 } else {
2846 /* print all list keys in predicates */
2847 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2848 }
2849 break;
2850 case LYS_LEAFLIST:
2851 if (iter->schema->flags & LYS_CONFIG_W) {
2852 /* print leaf-list value */
2853 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2854 } else {
2855 /* print its position */
2856 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2857 }
2858 break;
2859 default:
2860 /* nothing to print more */
2861 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002862 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002863 }
2864 if (rc != LY_SUCCESS) {
2865 break;
2866 }
2867
Michal Vasko14654712020-02-06 08:35:21 +01002868 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002869 }
2870 break;
2871 }
2872
2873 return buffer;
2874}
Michal Vaskoe444f752020-02-10 12:20:06 +01002875
Michal Vaskodbf3e652022-10-21 08:46:25 +02002876char *
2877lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2878{
2879 uint32_t depth;
2880 size_t bufused = 0, buflen = 0, len;
2881 char *buffer = NULL;
2882 const struct lyd_node *iter, *parent;
2883 const struct lys_module *mod, *prev_mod;
2884 LY_ERR rc = LY_SUCCESS;
2885
2886 switch (pathtype) {
2887 case LYD_PATH_STD:
2888 case LYD_PATH_STD_NO_LAST_PRED:
2889 for (depth = 1; depth <= dnodes->count; ++depth) {
2890 /* current node */
2891 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002892 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002893
2894 /* parent */
2895 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002896 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2897 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002898 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2899 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002900
2901 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002902 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002903 if (prev_mod == mod) {
2904 mod = NULL;
2905 }
2906
2907 /* realloc string */
2908 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2909 strlen(((struct lyd_node_opaq *)iter)->name.name));
2910 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2911 break;
2912 }
2913
2914 /* print next node */
2915 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2916
2917 /* do not always print the last (first) predicate */
2918 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2919 switch (iter->schema->nodetype) {
2920 case LYS_LIST:
2921 if (iter->schema->flags & LYS_KEYLESS) {
2922 /* print its position */
2923 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2924 } else {
2925 /* print all list keys in predicates */
2926 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2927 }
2928 break;
2929 case LYS_LEAFLIST:
2930 if (iter->schema->flags & LYS_CONFIG_W) {
2931 /* print leaf-list value */
2932 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2933 } else {
2934 /* print its position */
2935 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2936 }
2937 break;
2938 default:
2939 /* nothing to print more */
2940 break;
2941 }
2942 }
2943 if (rc) {
2944 break;
2945 }
2946 }
2947 break;
2948 }
2949
2950 return buffer;
2951}
2952
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002953LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002954lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2955{
2956 struct lyd_meta *ret = NULL;
2957 const struct ly_ctx *ctx;
2958 const char *prefix, *tmp;
2959 char *str;
2960 size_t pref_len, name_len;
2961
2962 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002963 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002964
2965 if (!first) {
2966 return NULL;
2967 }
2968
2969 ctx = first->annotation->module->ctx;
2970
2971 /* parse the name */
2972 tmp = name;
2973 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2974 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2975 return NULL;
2976 }
2977
2978 /* find the module */
2979 if (prefix) {
2980 str = strndup(prefix, pref_len);
2981 module = ly_ctx_get_module_latest(ctx, str);
2982 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002983 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 +02002984 }
2985
2986 /* find the metadata */
2987 LY_LIST_FOR(first, first) {
2988 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2989 ret = (struct lyd_meta *)first;
2990 break;
2991 }
2992 }
2993
2994 return ret;
2995}
2996
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002997LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002998lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2999{
Michal Vasko9beceb82022-04-05 12:14:15 +02003000 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01003001 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003002 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01003003
Michal Vaskof03ed032020-03-04 13:31:44 +01003004 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02003005 if (!siblings) {
3006 /* no data */
3007 if (match) {
3008 *match = NULL;
3009 }
3010 return LY_ENOTFOUND;
3011 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003012
Michal Vasko9beceb82022-04-05 12:14:15 +02003013 if (LYD_CTX(siblings) != LYD_CTX(target)) {
3014 /* create a duplicate in this context */
3015 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
3016 target = dup;
3017 }
3018
3019 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
3020 /* schema mismatch */
3021 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01003022 if (match) {
3023 *match = NULL;
3024 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003025 return LY_ENOTFOUND;
3026 }
3027
Michal Vaskoe78faec2021-04-08 17:24:43 +02003028 /* get first sibling */
3029 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01003030
Michal Vasko9e685082021-01-29 14:49:09 +01003031 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02003032 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003033 assert(target->hash);
3034
Michal Vaskoe78faec2021-04-08 17:24:43 +02003035 if (lysc_is_dup_inst_list(target->schema)) {
3036 /* we must search the instances from beginning to find the first matching one */
3037 found = 0;
3038 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003039 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003040 found = 1;
3041 break;
3042 }
3043 }
3044 if (found) {
3045 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02003046 } else {
3047 siblings = NULL;
3048 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003049 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003050 /* find by hash */
3051 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3052 siblings = *match_p;
3053 } else {
3054 /* not found */
3055 siblings = NULL;
3056 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003057 }
3058 } else {
Michal Vasko39311152023-08-07 11:03:41 +02003059 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02003060 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003061 if (lysc_is_dup_inst_list(target->schema)) {
3062 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
3063 break;
3064 }
3065 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02003066 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003067 break;
3068 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003069 }
3070 }
3071 }
3072
Michal Vasko9beceb82022-04-05 12:14:15 +02003073 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003074 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003075 if (match) {
3076 *match = NULL;
3077 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003078 return LY_ENOTFOUND;
3079 }
3080
Michal Vasko9b368d32020-02-14 13:53:31 +01003081 if (match) {
3082 *match = (struct lyd_node *)siblings;
3083 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003084 return LY_SUCCESS;
3085}
3086
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003087LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003088lyd_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 +02003089 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003090{
3091 LY_ERR rc;
3092 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02003093 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01003094
Michal Vasko4c583e82020-07-17 12:16:14 +02003095 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02003096 if (!siblings) {
3097 /* no data */
3098 if (match) {
3099 *match = NULL;
3100 }
3101 return LY_ENOTFOUND;
3102 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003103
Michal Vasko9beceb82022-04-05 12:14:15 +02003104 if ((LYD_CTX(siblings) != schema->module->ctx)) {
3105 /* parent of ext nodes is useless */
3106 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01003107 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
3108 /* no schema node in siblings so certainly no data node either */
3109 if (match) {
3110 *match = NULL;
3111 }
3112 return LY_ENOTFOUND;
3113 }
Michal Vasko9beceb82022-04-05 12:14:15 +02003114 }
3115
3116 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3117 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003118 if (match) {
3119 *match = NULL;
3120 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003121 return LY_ENOTFOUND;
3122 }
3123
Michal Vaskof03ed032020-03-04 13:31:44 +01003124 if (key_or_value && !val_len) {
3125 val_len = strlen(key_or_value);
3126 }
3127
Michal Vaskob104f112020-07-17 09:54:54 +02003128 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3129 /* create a data node and find the instance */
3130 if (schema->nodetype == LYS_LEAFLIST) {
3131 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02003132 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 +02003133 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003134 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003135 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003136 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003137 }
3138
3139 /* find it */
3140 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003141 } else {
3142 /* find the first schema node instance */
3143 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003144 }
3145
Michal Vaskoe444f752020-02-10 12:20:06 +01003146 lyd_free_tree(target);
3147 return rc;
3148}
Michal Vaskoccc02342020-05-21 10:09:21 +02003149
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003150LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02003151lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3152{
3153 struct lyd_node **match_p, *first, *iter;
3154 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02003155 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003156
Michal Vasko83ae7772022-06-08 10:01:55 +02003157 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003158 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02003159
3160 LY_CHECK_RET(ly_set_new(set));
3161
3162 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
3163 /* no data or schema mismatch */
3164 return LY_ENOTFOUND;
3165 }
3166
Michal Vaskoee9b9482023-06-19 13:17:48 +02003167 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02003168 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02003169
Michal Vaskoe78faec2021-04-08 17:24:43 +02003170 /* get first sibling */
3171 siblings = lyd_first_sibling(siblings);
3172
3173 parent = siblings->parent;
3174 if (parent && parent->schema && parent->children_ht) {
3175 assert(target->hash);
3176
3177 /* find the first instance */
3178 lyd_find_sibling_first(siblings, target, &first);
3179 if (first) {
3180 /* add it so that it is the first in the set */
3181 if (ly_set_add(*set, first, 1, NULL)) {
3182 goto error;
3183 }
3184
3185 /* find by hash */
3186 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3187 iter = *match_p;
3188 } else {
3189 /* not found */
3190 iter = NULL;
3191 }
3192 while (iter) {
3193 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02003194 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003195 goto error;
3196 }
3197
3198 /* find next instance */
3199 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
3200 iter = NULL;
3201 } else {
3202 iter = *match_p;
3203 }
3204 }
3205 }
3206 } else {
3207 /* no children hash table */
3208 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003209 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003210 ly_set_add(*set, (void *)siblings, 1, NULL);
3211 }
3212 }
3213 }
3214
3215 if (!(*set)->count) {
3216 return LY_ENOTFOUND;
3217 }
3218 return LY_SUCCESS;
3219
3220error:
3221 ly_set_free(*set, NULL);
3222 *set = NULL;
3223 return LY_EMEM;
3224}
3225
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003226LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003227lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3228{
3229 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3230
Michal Vaskoe271a312023-08-15 11:46:30 +02003231 if (first && first->schema) {
3232 first = first->prev;
3233 if (first->schema) {
3234 /* no opaque nodes */
3235 first = NULL;
3236 } else {
3237 /* opaque nodes are at the end, find quickly the first */
3238 while (!first->prev->schema) {
3239 first = first->prev;
3240 }
3241 }
3242 }
3243
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003244 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02003245 assert(!first->schema);
3246 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003247 break;
3248 }
3249 }
3250
3251 if (match) {
3252 *match = (struct lyd_node *)first;
3253 }
3254 return first ? LY_SUCCESS : LY_ENOTFOUND;
3255}
3256
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003257LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003258lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02003259{
Michal Vaskod96e2372023-02-24 16:07:51 +01003260 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003261
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003262 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01003263}
Michal Vaskoccc02342020-05-21 10:09:21 +02003264
Michal Vaskod96e2372023-02-24 16:07:51 +01003265LIBYANG_API_DEF LY_ERR
3266lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
3267{
3268 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003269
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003270 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003271}
Radek Krejcica989142020-11-05 11:32:22 +01003272
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003273LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003274lyd_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 +01003275 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01003276{
Michal Vaskod96e2372023-02-24 16:07:51 +01003277 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01003278
Michal Vaskod96e2372023-02-24 16:07:51 +01003279 *set = NULL;
3280
3281 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 +01003282}
3283
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003284LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003285lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003286{
Michal Vaskod96e2372023-02-24 16:07:51 +01003287 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02003288}
3289
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003290LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02003291lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
3292{
3293 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
3294}
3295
3296LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003297lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
3298 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003299{
Michal Vaskod96e2372023-02-24 16:07:51 +01003300 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
3301}
3302
3303LIBYANG_API_DEF LY_ERR
3304lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
3305 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
3306 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
3307{
3308 LY_ERR ret = LY_SUCCESS;
3309 struct lyxp_set xp_set = {0};
3310 struct lyxp_expr *exp = NULL;
3311 uint32_t i;
3312
3313 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
3314 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
3315 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
3316
3317 /* parse expression */
3318 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
3319 LY_CHECK_GOTO(ret, cleanup);
3320
3321 /* evaluate expression */
3322 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
3323 LYXP_IGNORE_WHEN);
3324 LY_CHECK_GOTO(ret, cleanup);
3325
3326 /* return expected result type without or with casting */
3327 if (node_set) {
3328 /* node set */
3329 if (xp_set.type == LYXP_SET_NODE_SET) {
3330 /* transform into a set */
3331 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
3332 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
3333 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
3334 (*node_set)->size = xp_set.used;
3335 for (i = 0; i < xp_set.used; ++i) {
3336 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3337 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
3338 LY_CHECK_GOTO(ret, cleanup);
3339 }
3340 }
3341 if (ret_type) {
3342 *ret_type = LY_XPATH_NODE_SET;
3343 }
3344 } else if (!string && !number && !boolean) {
3345 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
3346 ret = LY_EINVAL;
3347 goto cleanup;
3348 }
3349 }
3350
3351 if (string) {
3352 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
3353 /* cast into string */
3354 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3355 }
3356 if (xp_set.type == LYXP_SET_STRING) {
3357 /* string */
3358 *string = xp_set.val.str;
3359 xp_set.val.str = NULL;
3360 if (ret_type) {
3361 *ret_type = LY_XPATH_STRING;
3362 }
3363 }
3364 }
3365
3366 if (number) {
3367 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3368 /* cast into number */
3369 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3370 }
3371 if (xp_set.type == LYXP_SET_NUMBER) {
3372 /* number */
3373 *number = xp_set.val.num;
3374 if (ret_type) {
3375 *ret_type = LY_XPATH_NUMBER;
3376 }
3377 }
3378 }
3379
3380 if (boolean) {
3381 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3382 /* cast into boolean */
3383 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3384 }
3385 if (xp_set.type == LYXP_SET_BOOLEAN) {
3386 /* boolean */
3387 *boolean = xp_set.val.bln;
3388 if (ret_type) {
3389 *ret_type = LY_XPATH_BOOLEAN;
3390 }
3391 }
3392 }
3393
3394cleanup:
3395 lyxp_set_free_content(&xp_set);
3396 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3397 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003398}
3399
Michal Vasko99a77882024-01-04 14:50:51 +01003400/**
3401 * @brief Hash table node equal callback.
3402 */
3403static ly_bool
3404lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3405{
3406 struct lyd_node *node1, *node2;
3407
3408 node1 = *(struct lyd_node **)val1_p;
3409 node2 = *(struct lyd_node **)val2_p;
3410
3411 return node1 == node2;
3412}
3413
3414LIBYANG_API_DEF LY_ERR
3415lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3416{
3417 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003418 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003419 struct lyxp_set xp_set = {0};
3420 struct lyxp_expr *exp = NULL;
3421 struct lyd_node *node, *parent;
3422 struct lyxp_set_hash_node hnode;
3423 struct ly_ht *parent_ht = NULL;
3424 struct ly_set free_set = {0};
3425 uint32_t i, hash;
3426 ly_bool is_result;
3427
3428 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3429
3430 if (!*tree) {
3431 /* nothing to do */
3432 goto cleanup;
3433 }
3434
3435 *tree = lyd_first_sibling(*tree);
3436 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3437
3438 /* parse expression */
3439 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3440 LY_CHECK_GOTO(ret, cleanup);
3441
3442 /* evaluate expression */
3443 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3444 LY_CHECK_GOTO(ret, cleanup);
3445
3446 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003447 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003448 LY_CHECK_GOTO(!parent_ht, cleanup);
3449
3450 for (i = 0; i < xp_set.used; ++i) {
3451 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3452 /* ignore */
3453 continue;
3454 }
3455
3456 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3457 /* add the parent into parent_ht */
3458 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3459 if (ret == LY_EEXIST) {
3460 /* shared parent, we are done */
3461 break;
3462 }
3463 LY_CHECK_GOTO(ret, cleanup);
3464 }
3465 }
3466
3467 hnode.type = LYXP_NODE_ELEM;
3468 LY_LIST_FOR(*tree, parent) {
3469 LYD_TREE_DFS_BEGIN(parent, node) {
3470 if (lysc_is_key(node->schema)) {
3471 /* ignore */
3472 goto next_iter;
3473 }
3474
3475 /* check the results */
3476 is_result = 0;
3477 if (xp_set.ht) {
3478 hnode.node = node;
3479 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3480 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3481 hash = lyht_hash_multi(hash, NULL, 0);
3482
3483 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3484 is_result = 1;
3485 }
3486 } else {
3487 /* not enough elements for a hash table */
3488 for (i = 0; i < xp_set.used; ++i) {
3489 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3490 /* ignore */
3491 continue;
3492 }
3493
3494 if (xp_set.val.nodes[i].node == node) {
3495 is_result = 1;
3496 break;
3497 }
3498 }
3499 }
3500
3501 if (is_result) {
3502 /* keep the whole subtree if the node is in the results */
3503 LYD_TREE_DFS_continue = 1;
3504 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3505 /* free the whole subtree if the node is not even among the selected parents */
3506 ret = ly_set_add(&free_set, node, 1, NULL);
3507 LY_CHECK_GOTO(ret, cleanup);
3508 LYD_TREE_DFS_continue = 1;
3509 } /* else keep the parent node because a subtree is in the results */
3510
3511next_iter:
3512 LYD_TREE_DFS_END(parent, node);
3513 }
3514 }
3515
3516 /* free */
3517 for (i = 0; i < free_set.count; ++i) {
3518 node = free_set.dnodes[i];
3519 if (*tree == node) {
3520 *tree = (*tree)->next;
3521 }
3522 lyd_free_tree(node);
3523 }
3524
3525cleanup:
3526 lyxp_set_free_content(&xp_set);
3527 lyxp_expr_free(ctx, exp);
3528 lyht_free(parent_ht, NULL);
3529 ly_set_erase(&free_set, NULL);
3530 return ret;
3531}
3532
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003533LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003534lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3535{
3536 LY_ERR ret = LY_SUCCESS;
3537 struct lyxp_expr *expr = NULL;
3538 struct ly_path *lypath = NULL;
3539
3540 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3541
3542 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003543 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 +01003544 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003545 LY_CHECK_GOTO(ret, cleanup);
3546
3547 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003548 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003549 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 +01003550 LY_CHECK_GOTO(ret, cleanup);
3551
3552 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003553 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003554
3555cleanup:
3556 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3557 ly_path_free(LYD_CTX(ctx_node), lypath);
3558 return ret;
3559}
3560
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003561LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003562lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3563{
3564 LY_ERR ret;
3565 struct lyd_node *m;
3566
3567 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3568
Michal Vasko90189962023-02-28 12:10:34 +01003569 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003570 if (ret) {
3571 if (match) {
3572 *match = NULL;
3573 }
3574 return LY_ENOTFOUND;
3575 }
3576
3577 if (match) {
3578 *match = m;
3579 }
3580 return LY_SUCCESS;
3581}
Irfand3b351a2023-09-14 14:52:15 +02003582
stewegf9041a22024-01-18 13:29:12 +01003583LY_ERR
3584lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3585{
3586 struct ly_ht *ht;
3587 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003588 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003589
3590 assert(node);
3591 assert(record);
3592
Michal Vaskob46061d2024-01-18 13:58:13 +01003593 *record = NULL;
3594
stewegf9041a22024-01-18 13:29:12 +01003595 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003596 return LY_EDENIED;
3597 }
3598
3599 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003600 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003601 hash = lyht_hash((const char *)&node, sizeof node);
3602
stewegf9041a22024-01-18 13:29:12 +01003603 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3604 if (create) {
3605 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3606 } else {
stewegf9041a22024-01-18 13:29:12 +01003607 return LY_ENOTFOUND;
3608 }
3609 }
3610
3611 return LY_SUCCESS;
3612}
3613
3614LIBYANG_API_DEF LY_ERR
3615lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3616{
3617 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3618
3619 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3620}
3621
3622LY_ERR
3623lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3624{
stewegf9041a22024-01-18 13:29:12 +01003625 const struct lyd_node_term **item = NULL;
3626 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003627 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003628
3629 assert(node);
3630 assert(leafref_node);
3631
3632 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3633 return LY_EDENIED;
3634 }
3635
steweg67388952024-01-25 12:14:50 +01003636 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003637 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3638 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3639 if (rec->leafref_nodes[u] == leafref_node) {
3640 return LY_SUCCESS;
3641 }
3642 }
3643
3644 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3645 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003646
3647 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003648 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003649 LY_ARRAY_FOR(rec->target_nodes, u) {
3650 if (rec->target_nodes[u] == node) {
3651 return LY_SUCCESS;
3652 }
3653 }
3654
3655 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3656 *item = node;
3657
stewegf9041a22024-01-18 13:29:12 +01003658 return LY_SUCCESS;
3659}
3660
3661LIBYANG_API_DEF LY_ERR
3662lyd_leafref_link_node_tree(const struct lyd_node *tree)
3663{
3664 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003665 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003666 char *errmsg;
3667 struct lyd_node_term *leafref_node;
3668 struct lysc_node_leaf *leaf_schema;
3669 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003670 LY_ERR ret = LY_SUCCESS;
3671 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003672
3673 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3674
3675 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3676 return LY_EDENIED;
3677 }
3678
3679 LY_LIST_FOR(tree, sibling) {
3680 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003681 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003682 leafref_node = (struct lyd_node_term *)elem;
3683 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003684
stewegf9041a22024-01-18 13:29:12 +01003685 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3686 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003687 ly_set_free(targets, NULL);
3688 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003689 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003690 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003691 } else {
3692 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003693 for (i = 0; i < targets->count; ++i) {
3694 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3695 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3696 LY_CHECK_GOTO(ret, cleanup);
3697 }
3698 }
stewegf9041a22024-01-18 13:29:12 +01003699 }
3700 }
3701 }
3702 LYD_TREE_DFS_END(sibling, elem);
3703 }
3704 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003705
steweg67388952024-01-25 12:14:50 +01003706cleanup:
3707 ly_set_free(targets, NULL);
3708 return ret;
stewegf9041a22024-01-18 13:29:12 +01003709}
3710
3711LY_ERR
3712lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3713{
3714 LY_ERR ret;
3715 struct lyd_leafref_links_rec *rec;
3716
3717 assert(node);
3718 assert(leafref_node);
3719
3720 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3721 return LY_EDENIED;
3722 }
3723
steweg67388952024-01-25 12:14:50 +01003724 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003725 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3726 if (ret == LY_SUCCESS) {
3727 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003728 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003729 lyd_free_leafref_nodes(node);
3730 }
3731 } else if (ret != LY_ENOTFOUND) {
3732 return ret;
3733 }
3734
steweg67388952024-01-25 12:14:50 +01003735 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003736 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3737 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003738 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3739 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003740 lyd_free_leafref_nodes(leafref_node);
3741 }
3742 } else if (ret != LY_ENOTFOUND) {
3743 return ret;
3744 }
3745
3746 return LY_SUCCESS;
3747}