blob: affa568db4ee18b0e3623d07fe08f899a13d1dcf [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "context.h"
32#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020033#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.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"
Radek Krejci859a15a2021-03-05 20:56:59 +010048#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020049#include "tree_schema.h"
50#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020051#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020052#include "xml.h"
53#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020054
Michal Vaskof277d362023-04-24 09:08:31 +020055static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
56 ly_bool parental_schemas_checked);
57
Radek Krejci7931b192020-06-25 17:05:03 +020058static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020059lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020060{
Michal Vasko69730152020-10-09 16:30:07 +020061 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020062 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020063 const char *path = in->method.fpath.filepath;
64 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020065
66 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020067 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
70 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020071 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010072 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
73 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020074 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010075 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
76 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020077 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020078 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020079 }
80
Radek Krejci7931b192020-06-25 17:05:03 +020081 return format;
82}
Radek Krejcie7b95092019-05-15 11:03:07 +020083
Michal Vaskoe0665742021-02-11 11:08:44 +010084/**
85 * @brief Parse YANG data into a data tree.
86 *
87 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010088 * @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 +010089 * @param[in] parent Parent to connect the parsed nodes to, if any.
Michal Vaskob36c56f2024-01-31 12:50:07 +010090 * @param[in,out] first_p Pointer to the first parsed node.
Michal Vaskoe0665742021-02-11 11:08:44 +010091 * @param[in] in Input handle to read the input from.
92 * @param[in] format Expected format of the data in @p in.
93 * @param[in] parse_opts Options for parser.
94 * @param[in] val_opts Options for validation.
95 * @param[out] op Optional pointer to the parsed operation, if any.
96 * @return LY_ERR value.
97 */
98static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010099lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
100 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 +0200101{
stewegd8e2fc92023-05-31 09:52:56 +0200102 LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200103 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100104 struct ly_set parsed = {0};
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100105 uint32_t i, int_opts = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100106 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200107
Michal Vaskoe0665742021-02-11 11:08:44 +0100108 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200109
110 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100111 if (first_p) {
112 *first_p = NULL;
113 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200114
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 /* remember input position */
116 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200117
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100118 /* set internal options */
119 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
120 int_opts = LYD_INTOPT_WITH_SIBLINGS;
121 }
122
Michal Vaskoe0665742021-02-11 11:08:44 +0100123 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200124 switch (format) {
125 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100126 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100127 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200128 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200129 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100130 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100131 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200132 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200133 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100134 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100135 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200136 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200137 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100138 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100139 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100140 break;
141 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100142 if (r) {
143 rc = r;
144 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) ||
145 (ly_vecode(ctx) == LYVE_SYNTAX)) {
146 goto cleanup;
147 }
148 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100149
Michal Vaskoa6139e02023-10-03 14:13:22 +0200150 if (parent && parsed.count) {
151 /* use the first parsed node */
Michal Vaskob36c56f2024-01-31 12:50:07 +0100152 if (first_p) {
153 *first_p = parsed.dnodes[0];
154 } else {
155 first_p = &parsed.dnodes[0];
156 }
Radek Krejci7931b192020-06-25 17:05:03 +0200157 }
158
Michal Vaskoe0665742021-02-11 11:08:44 +0100159 if (!(parse_opts & LYD_PARSE_ONLY)) {
160 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200161 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 +0200162 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200163 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100164 }
Radek Krejci7931b192020-06-25 17:05:03 +0200165
Michal Vaskoe0665742021-02-11 11:08:44 +0100166 /* set the operation node */
167 if (op) {
168 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200169 }
170
171cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100172 if (lydctx) {
173 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200174 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100175 if (rc) {
176 if (parent) {
177 /* free all the parsed subtrees */
178 for (i = 0; i < parsed.count; ++i) {
179 lyd_free_tree(parsed.dnodes[i]);
180 }
181 } else {
182 /* free everything */
183 lyd_free_all(*first_p);
184 *first_p = NULL;
185 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100186 } else if (subtree_sibling) {
187 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100188 }
189 ly_set_erase(&parsed, NULL);
190 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200191}
192
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100193LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100194lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
195 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
196{
197 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
198
199 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
200 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
201 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
202
203 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
204}
205
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100206LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100207lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
208 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
209{
210 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
211 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
212 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
213
Radek Krejcif16e2542021-02-17 15:39:23 +0100214 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100215}
216
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100217LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100218lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
219 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200220{
221 LY_ERR ret;
222 struct ly_in *in;
223
224 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100225 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200226
227 ly_in_free(in, 0);
228 return ret;
229}
230
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100231LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200232lyd_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 +0200233 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200234{
235 LY_ERR ret;
236 struct ly_in *in;
237
238 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100239 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200240
241 ly_in_free(in, 0);
242 return ret;
243}
244
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100245LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200246lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
247 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200248{
249 LY_ERR ret;
250 struct ly_in *in;
251
252 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100253 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200254
255 ly_in_free(in, 0);
256 return ret;
257}
258
Radek Krejcif16e2542021-02-17 15:39:23 +0100259/**
260 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
261 * for schema nodes locked inside the extension instance.
262 *
263 * At least one of @p parent, @p tree, or @p op must always be set.
264 *
Michal Vasko820efe82023-05-12 15:47:43 +0200265 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100266 *
267 * @param[in] ctx libyang context.
268 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
269 * @param[in] parent Optional parent to connect the parsed nodes to.
270 * @param[in] in Input handle to read the input from.
271 * @param[in] format Expected format of the data in @p in.
272 * @param[in] data_type Expected operation to parse (@ref datatype).
273 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
274 * @param[out] op Optional parsed operation node.
275 * @return LY_ERR value.
276 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
277 */
278static LY_ERR
279lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
280 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 +0200281{
Michal Vaskoe0665742021-02-11 11:08:44 +0100282 LY_ERR rc = LY_SUCCESS;
283 struct lyd_ctx *lydctx = NULL;
284 struct ly_set parsed = {0};
285 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100286 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200287 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200288
Michal Vasko27fb0262021-02-23 09:42:01 +0100289 if (!ctx) {
290 ctx = LYD_CTX(parent);
291 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200292 if (tree) {
293 *tree = NULL;
294 }
295 if (op) {
296 *op = NULL;
297 }
298
Radek Krejci7931b192020-06-25 17:05:03 +0200299 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200300
Michal Vasko63f3d842020-07-08 10:10:14 +0200301 /* remember input position */
302 in->func_start = in->current;
303
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100304 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200305 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100306 val_opts = 0;
307
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100308 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100309 case LYD_TYPE_RPC_NETCONF:
310 case LYD_TYPE_NOTIF_NETCONF:
311 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200312 proto_msg = 1;
313 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100314 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200315 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
316 tree, !op, LY_EINVAL);
317 proto_msg = 1;
318 break;
319 case LYD_TYPE_RPC_RESTCONF:
320 case LYD_TYPE_REPLY_RESTCONF:
321 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
322 proto_msg = 1;
323 break;
324 case LYD_TYPE_NOTIF_RESTCONF:
325 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
326 proto_msg = 1;
327 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100328
Michal Vasko820efe82023-05-12 15:47:43 +0200329 /* set internal opts */
330 case LYD_TYPE_RPC_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200331 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200332 break;
333 case LYD_TYPE_NOTIF_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200334 int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200335 break;
336 case LYD_TYPE_REPLY_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200337 int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200338 break;
339 case LYD_TYPE_DATA_YANG:
340 LOGINT(ctx);
341 rc = LY_EINT;
342 goto cleanup;
343 }
344
345 /* parse a full protocol message */
346 if (proto_msg) {
347 if (format == LYD_XML) {
348 /* parse the NETCONF (or RESTCONF XML) message */
349 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
350 } else {
351 /* parse the RESTCONF message */
352 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
353 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100354 if (rc) {
355 if (envp) {
356 /* special situation when the envelopes were parsed successfully */
357 *tree = envp;
358 }
Michal Vasko73792a12022-05-10 09:28:45 +0200359 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100360 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100361
362 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100363 if (envp) {
364 /* special out param meaning */
365 *tree = envp;
366 } else {
367 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100368 }
369 if (op) {
370 *op = lydctx->op_node;
371 }
372 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100373 }
374
375 /* parse the data */
376 switch (format) {
377 case LYD_XML:
378 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 +0100379 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200380 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100381 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 +0100382 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200383 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100384 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 +0100385 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200386 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100387 LOGARG(ctx, format);
388 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200389 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200390 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100391 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200392
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 /* set out params correctly */
394 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100395 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100396 }
397 if (op) {
398 *op = lydctx->op_node;
399 }
400
401cleanup:
402 if (lydctx) {
403 lydctx->free(lydctx);
404 }
405 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200406 /* free all the parsed nodes */
407 if (parsed.count) {
408 i = parsed.count;
409 do {
410 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100411 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200412 } while (i);
413 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200414 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200415 *tree = NULL;
416 }
417 if (op) {
418 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100419 }
420 }
421 ly_set_erase(&parsed, NULL);
422 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200423}
Radek Krejci084289f2019-07-09 17:35:30 +0200424
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100425LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100426lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
427 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
428{
429 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
430
431 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
432}
433
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100434LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100435lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
436 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
437{
438 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
439
440 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
441
442 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
443}
444
Michal Vasko90932a92020-02-12 14:33:03 +0100445struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200446lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100447{
Michal Vaskob104f112020-07-17 09:54:54 +0200448 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100449 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200450 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100451 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100452
Michal Vaskob104f112020-07-17 09:54:54 +0200453 assert(new_node);
454
Michal Vasko9beceb82022-04-05 12:14:15 +0200455 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200456 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100457 return NULL;
458 }
459
Michal Vasko93b16062020-12-09 18:14:18 +0100460 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100461 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100462 getnext_opts = LYS_GETNEXT_OUTPUT;
463 }
464
Michal Vaskoa2756f02021-07-09 13:20:28 +0200465 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200466 /* find the anchor using hashes */
467 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100468 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200469 while (schema) {
470 /* keep trying to find the first existing instance of the closest following schema sibling,
471 * otherwise return NULL - inserting at the end */
472 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
473 break;
474 }
475
Michal Vasko93b16062020-12-09 18:14:18 +0100476 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200477 }
478 } else {
479 /* find the anchor without hashes */
480 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200481 sparent = lysc_data_parent(new_node->schema);
482 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200483 /* we are in top-level, skip all the data from preceding modules */
484 LY_LIST_FOR(match, match) {
485 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
486 break;
487 }
488 }
489 }
490
491 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100492 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200493 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200494 /* must be a top-level extension instance data, no anchor */
495 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200496 }
Michal Vaskob104f112020-07-17 09:54:54 +0200497
498 found = 0;
499 LY_LIST_FOR(match, match) {
500 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
501 /* we have found an opaque node, which must be at the end, so use it OR
502 * modules do not match, so we must have traversed all the data from new_node module (if any),
503 * we have found the first node of the next module, that is what we want */
504 break;
505 }
506
507 /* skip schema nodes until we find the instantiated one */
508 while (!found) {
509 if (new_node->schema == schema) {
510 /* we have found the schema of the new node, continue search to find the first
511 * data node with a different schema (after our schema) */
512 found = 1;
513 break;
514 }
515 if (match->schema == schema) {
516 /* current node (match) is a data node still before the new node, continue search in data */
517 break;
518 }
Michal Vasko9a463472023-08-08 09:43:33 +0200519
Michal Vasko93b16062020-12-09 18:14:18 +0100520 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200521 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200522 /* must be a top-level extension instance data, no anchor */
523 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200524 }
Michal Vaskob104f112020-07-17 09:54:54 +0200525 }
526
527 if (found && (match->schema != new_node->schema)) {
528 /* find the next node after we have found our node schema data instance */
529 break;
530 }
531 }
Michal Vasko90932a92020-02-12 14:33:03 +0100532 }
533
534 return match;
535}
536
Michal Vaskoc61dd062022-06-07 11:01:28 +0200537void
Michal Vaskof03ed032020-03-04 13:31:44 +0100538lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100539{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100540 struct lyd_node_inner *par;
541
Michal Vasko90932a92020-02-12 14:33:03 +0100542 assert(!node->next && (node->prev == node));
543
544 node->next = sibling->next;
545 node->prev = sibling;
546 sibling->next = node;
547 if (node->next) {
548 /* sibling had a succeeding node */
549 node->next->prev = node;
550 } else {
551 /* sibling was last, find first sibling and change its prev */
552 if (sibling->parent) {
553 sibling = sibling->parent->child;
554 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200555 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100556 }
557 sibling->prev = node;
558 }
559 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100560
Michal Vasko9f96a052020-03-10 09:41:45 +0100561 for (par = node->parent; par; par = par->parent) {
562 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
563 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100564 par->flags &= ~LYD_DEFAULT;
565 }
566 }
Michal Vasko90932a92020-02-12 14:33:03 +0100567}
568
Michal Vaskoc61dd062022-06-07 11:01:28 +0200569void
Michal Vaskof03ed032020-03-04 13:31:44 +0100570lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100571{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100572 struct lyd_node_inner *par;
573
Michal Vasko90932a92020-02-12 14:33:03 +0100574 assert(!node->next && (node->prev == node));
575
576 node->next = sibling;
577 /* covers situation of sibling being first */
578 node->prev = sibling->prev;
579 sibling->prev = node;
580 if (node->prev->next) {
581 /* sibling had a preceding node */
582 node->prev->next = node;
583 } else if (sibling->parent) {
584 /* sibling was first and we must also change parent child pointer */
585 sibling->parent->child = node;
586 }
587 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100588
Michal Vasko9f96a052020-03-10 09:41:45 +0100589 for (par = node->parent; par; par = par->parent) {
590 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
591 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100592 par->flags &= ~LYD_DEFAULT;
593 }
594 }
Michal Vasko90932a92020-02-12 14:33:03 +0100595}
596
597/**
Michal Vaskob104f112020-07-17 09:54:54 +0200598 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100599 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100600 * Handles inserting into NP containers and key-less lists.
601 *
Michal Vasko90932a92020-02-12 14:33:03 +0100602 * @param[in] parent Parent to insert into.
603 * @param[in] node Node to insert.
604 */
605static void
Michal Vaskob104f112020-07-17 09:54:54 +0200606lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100607{
608 struct lyd_node_inner *par;
609
Radek Krejcia1c1e542020-09-29 16:06:52 +0200610 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100611 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100612
613 par = (struct lyd_node_inner *)parent;
614
Michal Vaskob104f112020-07-17 09:54:54 +0200615 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100616 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100617
Michal Vaskod989ba02020-08-24 10:59:24 +0200618 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100619 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
620 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100621 par->flags &= ~LYD_DEFAULT;
622 }
623 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200624}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100625
Michal Vasko751cb4d2020-07-14 12:25:28 +0200626/**
627 * @brief Learn whether a list instance has all the keys.
628 *
629 * @param[in] list List instance to check.
630 * @return non-zero if all the keys were found,
631 * @return 0 otherwise.
632 */
633static int
634lyd_insert_has_keys(const struct lyd_node *list)
635{
636 const struct lyd_node *key;
637 const struct lysc_node *skey = NULL;
638
639 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200640 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200641 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
642 if (!key || (key->schema != skey)) {
643 /* key missing */
644 return 0;
645 }
646
647 key = key->next;
648 }
649
650 /* all keys found */
651 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100652}
653
654void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200655lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +0100656{
Michal Vaskob104f112020-07-17 09:54:54 +0200657 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100658
Michal Vaskob104f112020-07-17 09:54:54 +0200659 /* inserting list without its keys is not supported */
660 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100661 assert(!parent || !parent->schema ||
662 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100663
Michal Vaskob104f112020-07-17 09:54:54 +0200664 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100665 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100666 }
Michal Vasko90932a92020-02-12 14:33:03 +0100667
Michal Vaskob104f112020-07-17 09:54:54 +0200668 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100669 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100670
Michal Vasko9beceb82022-04-05 12:14:15 +0200671 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200672 /* no next anchor */
673 anchor = NULL;
674 } else {
675 /* find the anchor, our next node, so we can insert before it */
676 anchor = lyd_insert_get_next_anchor(first_sibling, node);
Michal Vasko6a0bb252023-08-21 11:55:11 +0200677
678 /* cannot insert data node after opaque nodes */
Michal Vasko5c1d4c22023-12-14 10:45:58 +0100679 if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) {
Michal Vasko6a0bb252023-08-21 11:55:11 +0200680 anchor = first_sibling->prev;
681 while ((anchor != first_sibling) && !anchor->prev->schema) {
682 anchor = anchor->prev;
683 }
684 }
Michal Vasko6ee6f432021-07-16 09:49:14 +0200685 }
686
Michal Vaskob104f112020-07-17 09:54:54 +0200687 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200688 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200689 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100690 if (!parent && (*first_sibling_p == anchor)) {
691 /* move first sibling */
692 *first_sibling_p = node;
693 }
Michal Vaskob104f112020-07-17 09:54:54 +0200694 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200695 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200696 lyd_insert_after_node(first_sibling->prev, node);
697 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200698 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200699 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100700 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200701 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200702 *first_sibling_p = node;
703 }
704
705 /* insert into parent HT */
706 lyd_insert_hash(node);
707
708 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200709 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200710 lyd_hash(parent);
711
712 /* now we can insert even the list into its parent HT */
713 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100714 }
Michal Vasko90932a92020-02-12 14:33:03 +0100715}
716
Michal Vasko717a4c32020-12-07 09:40:10 +0100717/**
718 * @brief Check schema place of a node to be inserted.
719 *
720 * @param[in] parent Schema node of the parent data node.
721 * @param[in] sibling Schema node of a sibling data node.
722 * @param[in] schema Schema node if the data node to be inserted.
723 * @return LY_SUCCESS on success.
724 * @return LY_EINVAL if the place is invalid.
725 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100726static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100727lyd_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 +0100728{
729 const struct lysc_node *par2;
730
Michal Vasko62ed12d2020-05-21 10:08:25 +0200731 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100732 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
733 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100734
Michal Vasko717a4c32020-12-07 09:40:10 +0100735 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100736 /* opaque nodes can be inserted wherever */
737 return LY_SUCCESS;
738 }
739
Michal Vasko717a4c32020-12-07 09:40:10 +0100740 if (!parent) {
741 parent = lysc_data_parent(sibling);
742 }
743
Michal Vaskof03ed032020-03-04 13:31:44 +0100744 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200745 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100746
747 if (parent) {
748 /* inner node */
749 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200750 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200751 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 return LY_EINVAL;
753 }
754 } else {
755 /* top-level node */
756 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200757 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100758 return LY_EINVAL;
759 }
760 }
761
762 return LY_SUCCESS;
763}
764
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100765LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200766lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100767{
768 struct lyd_node *iter;
769
Michal Vasko0ab974d2021-02-24 13:18:26 +0100770 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100771 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100772
Michal Vasko717a4c32020-12-07 09:40:10 +0100773 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100774
Michal Vasko0ab974d2021-02-24 13:18:26 +0100775 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100776 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100777 return LY_EINVAL;
778 }
779
780 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100781 lyd_unlink(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100782 }
783
784 while (node) {
785 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100786 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200787 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100788 node = iter;
789 }
790 return LY_SUCCESS;
791}
792
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100793LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200794lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100795{
796 struct lyd_node *iter;
797
798 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
799 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
800
801 if (first->schema && (first->schema->flags & LYS_KEY)) {
802 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
803 return LY_EINVAL;
804 }
805
806 while (first) {
807 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100808 lyd_unlink(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100809 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100810 first = iter;
811 }
812 return LY_SUCCESS;
813}
814
815LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200816lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100817{
818 struct lyd_node *iter;
819
Michal Vaskob104f112020-07-17 09:54:54 +0200820 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100821
Michal Vaskob104f112020-07-17 09:54:54 +0200822 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100823 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100824 }
825
Michal Vasko553d0b52020-12-04 16:27:52 +0100826 if (sibling == node) {
827 /* we need to keep the connection to siblings so we can insert into them */
828 sibling = sibling->prev;
829 }
830
Michal Vaskob1b5c262020-03-05 14:29:47 +0100831 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100832 lyd_unlink(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100833 }
834
835 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100836 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200837 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200838 return LY_EINVAL;
839 }
840
Michal Vaskob1b5c262020-03-05 14:29:47 +0100841 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100842 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200843 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100844 node = iter;
845 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100846
Michal Vaskob104f112020-07-17 09:54:54 +0200847 if (first) {
848 /* find the first sibling */
849 *first = sibling;
850 while ((*first)->prev->next) {
851 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100852 }
853 }
854
855 return LY_SUCCESS;
856}
857
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100858LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100859lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
860{
Michal Vaskobce230b2022-04-19 09:55:31 +0200861 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100862 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100863
Michal Vasko717a4c32020-12-07 09:40:10 +0100864 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100865
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200866 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200867 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100868 return LY_EINVAL;
869 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200870 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
871 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100872 return LY_EINVAL;
873 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100874
Michal Vasko2e784f82024-01-11 09:51:22 +0100875 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100876 lyd_insert_before_node(sibling, node);
877 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100878
Michal Vaskof03ed032020-03-04 13:31:44 +0100879 return LY_SUCCESS;
880}
881
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100882LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100883lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
884{
Michal Vaskobce230b2022-04-19 09:55:31 +0200885 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100886 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100887
Michal Vasko717a4c32020-12-07 09:40:10 +0100888 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100889
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200890 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200891 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100892 return LY_EINVAL;
893 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200894 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
895 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100896 return LY_EINVAL;
897 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100898
Michal Vasko2e784f82024-01-11 09:51:22 +0100899 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100900 lyd_insert_after_node(sibling, node);
901 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100902
Michal Vaskof03ed032020-03-04 13:31:44 +0100903 return LY_SUCCESS;
904}
905
Michal Vasko2e784f82024-01-11 09:51:22 +0100906void
907lyd_unlink(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100908{
909 struct lyd_node *iter;
910
911 if (!node) {
912 return;
913 }
914
Michal Vaskob104f112020-07-17 09:54:54 +0200915 /* update hashes while still linked into the tree */
916 lyd_unlink_hash(node);
917
stewegf9041a22024-01-18 13:29:12 +0100918 /* unlink leafref nodes */
919 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
920 lyd_free_leafref_nodes((struct lyd_node_term *)node);
921 }
922
Michal Vaskof03ed032020-03-04 13:31:44 +0100923 /* unlink from siblings */
924 if (node->prev->next) {
925 node->prev->next = node->next;
926 }
927 if (node->next) {
928 node->next->prev = node->prev;
929 } else {
930 /* unlinking the last node */
931 if (node->parent) {
932 iter = node->parent->child;
933 } else {
934 iter = node->prev;
935 while (iter->prev != node) {
936 iter = iter->prev;
937 }
938 }
939 /* update the "last" pointer from the first node */
940 iter->prev = node->prev;
941 }
942
943 /* unlink from parent */
944 if (node->parent) {
945 if (node->parent->child == node) {
946 /* the node is the first child */
947 node->parent->child = node->next;
948 }
949
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200950 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100951 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200952
Michal Vaskof03ed032020-03-04 13:31:44 +0100953 node->parent = NULL;
954 }
955
956 node->next = NULL;
957 node->prev = node;
958}
959
Michal Vasko2e784f82024-01-11 09:51:22 +0100960LIBYANG_API_DEF void
961lyd_unlink_siblings(struct lyd_node *node)
962{
963 struct lyd_node *next, *elem, *first = NULL;
964
965 LY_LIST_FOR_SAFE(node, next, elem) {
966 if (lysc_is_key(elem->schema) && elem->parent) {
967 LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
968 LYD_NAME(elem));
969 return;
970 }
971
972 lyd_unlink(elem);
973 lyd_insert_node(NULL, &first, elem, 1);
974 }
975}
976
977LIBYANG_API_DEF void
978lyd_unlink_tree(struct lyd_node *node)
979{
980 if (node && lysc_is_key(node->schema) && node->parent) {
981 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
982 LYD_NAME(node));
983 return;
984 }
985
986 lyd_unlink(node);
987}
988
Michal Vaskoa5da3292020-08-12 13:10:50 +0200989void
Michal Vasko871a0252020-11-11 18:35:24 +0100990lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200991{
992 struct lyd_meta *last, *iter;
993
994 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200995
996 if (!meta) {
997 return;
998 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200999
1000 for (iter = meta; iter; iter = iter->next) {
1001 iter->parent = parent;
1002 }
1003
1004 /* insert as the last attribute */
1005 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001006 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001007 last->next = meta;
1008 } else {
1009 parent->meta = meta;
1010 }
1011
1012 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001013 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001014 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001015 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001016 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001017}
1018
1019LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001020lyd_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 +02001021 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 +01001022 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 +01001023{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001024 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001025 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001026 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001027 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001028 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001029
Michal Vasko9f96a052020-03-10 09:41:45 +01001030 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001031
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001032 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001033 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001034 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001035 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001036 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001037 break;
1038 }
1039 }
1040 if (!ant) {
1041 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001042 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001043 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001044 ret = LY_EINVAL;
1045 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001046 }
1047
Michal Vasko9f96a052020-03-10 09:41:45 +01001048 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001049 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001050 mt->parent = parent;
1051 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001052 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001053 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 +01001054 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001055 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1056 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1057 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001058
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001059 /* insert as the last attribute */
1060 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001061 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001062 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001063 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001064 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001065 }
1066
Michal Vasko9f96a052020-03-10 09:41:45 +01001067 if (meta) {
1068 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001069 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001070
1071cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001072 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001073}
1074
Michal Vaskoa5da3292020-08-12 13:10:50 +02001075void
1076lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1077{
1078 struct lyd_attr *last, *iter;
1079 struct lyd_node_opaq *opaq;
1080
1081 assert(parent && !parent->schema);
1082
1083 if (!attr) {
1084 return;
1085 }
1086
1087 opaq = (struct lyd_node_opaq *)parent;
1088 for (iter = attr; iter; iter = iter->next) {
1089 iter->parent = opaq;
1090 }
1091
1092 /* insert as the last attribute */
1093 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001094 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001095 last->next = attr;
1096 } else {
1097 opaq->attr = attr;
1098 }
1099}
1100
Michal Vasko52927e22020-03-16 17:26:14 +01001101LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001102lyd_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 +01001103 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 +02001104 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 +01001105{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001106 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001107 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001108
1109 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001110 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001111
Michal Vasko2a3722d2021-06-16 11:52:39 +02001112 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001113 value = "";
1114 }
1115
1116 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001117 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001118
Michal Vaskoad92b672020-11-12 13:11:31 +01001119 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001120 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001121 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001122 }
1123 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001124 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001125 }
1126
Michal Vasko52927e22020-03-16 17:26:14 +01001127 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001128 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1129 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001130 *dynamic = 0;
1131 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001132 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001133 }
Michal Vasko501af032020-11-11 20:27:44 +01001134 at->format = format;
1135 at->val_prefix_data = val_prefix_data;
1136 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001137
1138 /* insert as the last attribute */
1139 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001140 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001141 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001142 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001143 last->next = at;
1144 }
1145
Radek Krejci011e4aa2020-09-04 15:22:31 +02001146finish:
1147 if (ret) {
1148 lyd_free_attr_single(ctx, at);
1149 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001150 *attr = at;
1151 }
1152 return LY_SUCCESS;
1153}
1154
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001155LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001156lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001157{
Michal Vasko90189962023-02-28 12:10:34 +01001158 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001159
Michal Vasko90189962023-02-28 12:10:34 +01001160 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001161
Michal Vasko004d3152020-06-11 19:59:22 +02001162 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001163}
1164
aPiecek2f63f952021-03-30 12:22:18 +02001165/**
1166 * @brief Check the equality of the two schemas from different contexts.
1167 *
1168 * @param schema1 of first node.
1169 * @param schema2 of second node.
1170 * @return 1 if the schemas are equal otherwise 0.
1171 */
1172static ly_bool
1173lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1174{
1175 if (!schema1 && !schema2) {
1176 return 1;
1177 } else if (!schema1 || !schema2) {
1178 return 0;
1179 }
1180
1181 assert(schema1->module->ctx != schema2->module->ctx);
1182
1183 if (schema1->nodetype != schema2->nodetype) {
1184 return 0;
1185 }
1186
1187 if (strcmp(schema1->name, schema2->name)) {
1188 return 0;
1189 }
1190
1191 if (strcmp(schema1->module->name, schema2->module->name)) {
1192 return 0;
1193 }
1194
aPiecek2f63f952021-03-30 12:22:18 +02001195 return 1;
1196}
1197
1198/**
1199 * @brief Check the equality of the schemas for all parent nodes.
1200 *
1201 * Both nodes must be from different contexts.
1202 *
1203 * @param node1 Data of first node.
1204 * @param node2 Data of second node.
1205 * @return 1 if the all related parental schemas are equal otherwise 0.
1206 */
1207static ly_bool
1208lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1209{
1210 const struct lysc_node *parent1, *parent2;
1211
1212 assert(node1 && node2);
1213
1214 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1215 parent1 && parent2;
1216 parent1 = parent1->parent, parent2 = parent2->parent) {
1217 if (!lyd_compare_schema_equal(parent1, parent2)) {
1218 return 0;
1219 }
1220 }
1221
1222 if (parent1 || parent2) {
1223 return 0;
1224 }
1225
1226 return 1;
1227}
1228
1229/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001230 * @brief Compare 2 nodes values including opaque node values.
1231 *
1232 * @param[in] node1 First node to compare.
1233 * @param[in] node2 Second node to compare.
1234 * @return LY_SUCCESS if equal.
1235 * @return LY_ENOT if not equal.
1236 * @return LY_ERR on error.
1237 */
1238static LY_ERR
1239lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1240{
1241 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1242 const char *val1, *val2, *col;
1243 const struct lys_module *mod;
1244 char *val_dyn = NULL;
1245 LY_ERR rc = LY_SUCCESS;
1246
1247 if (!node1->schema) {
1248 opaq1 = (struct lyd_node_opaq *)node1;
1249 }
1250 if (!node2->schema) {
1251 opaq2 = (struct lyd_node_opaq *)node2;
1252 }
1253
1254 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1255 /* opaque XML and opaque XML node */
1256 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1257 opaq2->val_prefix_data)) {
1258 return LY_ENOT;
1259 }
1260 return LY_SUCCESS;
1261 }
1262
1263 /* get their values */
1264 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1265 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1266 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1267 if (!mod) {
1268 /* unable to compare */
1269 return LY_ENOT;
1270 }
1271
1272 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1273 LOGMEM(LYD_CTX(node1));
1274 return LY_EMEM;
1275 }
1276 val1 = val_dyn;
1277 } else {
1278 val1 = lyd_get_value(node1);
1279 }
1280 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1281 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1282 if (!mod) {
1283 return LY_ENOT;
1284 }
1285
1286 assert(!val_dyn);
1287 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1288 LOGMEM(LYD_CTX(node2));
1289 return LY_EMEM;
1290 }
1291 val2 = val_dyn;
1292 } else {
1293 val2 = lyd_get_value(node2);
1294 }
1295
1296 /* compare values */
1297 if (strcmp(val1, val2)) {
1298 rc = LY_ENOT;
1299 }
1300
1301 free(val_dyn);
1302 return rc;
1303}
1304
1305/**
Michal Vaskof277d362023-04-24 09:08:31 +02001306 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1307 *
1308 * Works correctly even if @p node1 and @p node2 have different contexts.
1309 *
1310 * @param[in] node1 The first node to compare.
1311 * @param[in] node2 The second node to compare.
1312 * @param[in] options Various @ref datacompareoptions.
1313 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1314 * @return LY_SUCCESS if the nodes are equivalent.
1315 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001316 */
1317static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001318lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001319 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001320{
aPiecek2f63f952021-03-30 12:22:18 +02001321 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1322 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001323 if (options & LYD_COMPARE_OPAQ) {
1324 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1325 return LY_ENOT;
1326 }
1327 } else {
1328 if (node1->schema != node2->schema) {
1329 return LY_ENOT;
1330 }
aPiecek2f63f952021-03-30 12:22:18 +02001331 }
1332 } else {
1333 /* different contexts */
1334 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1335 return LY_ENOT;
1336 }
1337 if (!parental_schemas_checked) {
1338 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1339 return LY_ENOT;
1340 }
1341 parental_schemas_checked = 1;
1342 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001343 }
1344
Michal Vaskof277d362023-04-24 09:08:31 +02001345 return LY_SUCCESS;
1346}
1347
1348/**
1349 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1350 *
1351 * Works correctly even if @p node1 and @p node2 have different contexts.
1352 *
1353 * @param[in] node1 The first node to compare.
1354 * @param[in] node2 The second node to compare.
1355 * @param[in] options Various @ref datacompareoptions.
1356 * @return LY_SUCCESS if the nodes are equivalent.
1357 * @return LY_ENOT if the nodes are not equivalent.
1358 */
1359static LY_ERR
1360lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1361{
1362 const struct lyd_node *iter1, *iter2;
1363 struct lyd_node_any *any1, *any2;
1364 int len1, len2;
1365 LY_ERR r;
1366
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001367 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001368 return LY_ENOT;
1369 }
aPiecek2f63f952021-03-30 12:22:18 +02001370 /* 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 +02001371
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001372 if (!node1->schema || !node2->schema) {
1373 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001374 return LY_ENOT;
1375 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001376 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1377 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1378 /* compare values only if there are any to compare */
1379 if ((r = lyd_compare_single_value(node1, node2))) {
1380 return r;
1381 }
Michal Vasko52927e22020-03-16 17:26:14 +01001382 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001383
Michal Vasko52927e22020-03-16 17:26:14 +01001384 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001385 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001386 }
1387 return LY_SUCCESS;
1388 } else {
1389 switch (node1->schema->nodetype) {
1390 case LYS_LEAF:
1391 case LYS_LEAFLIST:
1392 if (options & LYD_COMPARE_DEFAULTS) {
1393 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1394 return LY_ENOT;
1395 }
1396 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001397 if ((r = lyd_compare_single_value(node1, node2))) {
1398 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001399 }
1400
aPiecek2f63f952021-03-30 12:22:18 +02001401 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001402 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001403 case LYS_RPC:
1404 case LYS_ACTION:
1405 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001406 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001407 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001408 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001409 }
1410 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001411 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001412 iter1 = lyd_child(node1);
1413 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001414
Michal Vaskoee9b9482023-06-19 13:17:48 +02001415 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001416 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001417 } else if (node1->schema->flags & LYS_KEYLESS) {
1418 /* always equal */
1419 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001420 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001421
1422 /* lists with keys, their equivalence is based on their keys */
1423 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1424 key && (key->flags & LYS_KEY);
1425 key = key->next) {
1426 if (!iter1 || !iter2) {
1427 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1428 }
1429 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1430 LY_CHECK_RET(r);
1431 r = lyd_compare_single_data(iter1, iter2, options);
1432 LY_CHECK_RET(r);
1433
1434 iter1 = iter1->next;
1435 iter2 = iter2->next;
1436 }
1437
1438 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001439 case LYS_ANYXML:
1440 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001441 any1 = (struct lyd_node_any *)node1;
1442 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001443
1444 if (any1->value_type != any2->value_type) {
1445 return LY_ENOT;
1446 }
1447 switch (any1->value_type) {
1448 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001449 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001450 case LYD_ANYDATA_STRING:
1451 case LYD_ANYDATA_XML:
1452 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001453 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1454 return LY_ENOT;
1455 } else if (!any1->value.str && !any2->value.str) {
1456 return LY_SUCCESS;
1457 }
Michal Vasko52927e22020-03-16 17:26:14 +01001458 len1 = strlen(any1->value.str);
1459 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001460 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001461 return LY_ENOT;
1462 }
1463 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001464 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001465 len1 = lyd_lyb_data_length(any1->value.mem);
1466 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001467 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001468 return LY_ENOT;
1469 }
1470 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001471 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001472 }
1473 }
1474
Michal Vaskob7be7a82020-08-20 09:09:04 +02001475 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001476 return LY_EINT;
1477}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001478
Michal Vaskof277d362023-04-24 09:08:31 +02001479/**
1480 * @brief Compare all siblings at a node level.
1481 *
1482 * @param[in] node1 First sibling list.
1483 * @param[in] node2 Second sibling list.
1484 * @param[in] options Various @ref datacompareoptions.
1485 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1486 * @return LY_SUCCESS if equal.
1487 * @return LY_ENOT if not equal.
1488 * @return LY_ERR on error.
1489 */
1490static LY_ERR
1491lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1492 ly_bool parental_schemas_checked)
1493{
1494 LY_ERR r;
1495 const struct lyd_node *iter2;
1496
1497 while (node1 && node2) {
1498 /* schema match */
1499 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1500 LY_CHECK_RET(r);
1501
1502 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1503 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1504 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1505 /* find a matching instance in case they are ordered differently */
1506 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1507 if (r == LY_ENOTFOUND) {
1508 /* no matching instance, data not equal */
1509 r = LY_ENOT;
1510 }
1511 LY_CHECK_RET(r);
1512 } else {
1513 /* compare with the current node */
1514 iter2 = node2;
1515 }
1516
1517 /* data match */
1518 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1519 LY_CHECK_RET(r);
1520
1521 node1 = node1->next;
1522 node2 = node2->next;
1523 }
1524
1525 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1526}
1527
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001528LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001529lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1530{
Michal Vaskof277d362023-04-24 09:08:31 +02001531 LY_ERR r;
1532
1533 if (!node1 || !node2) {
1534 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1535 }
1536
1537 /* schema match */
1538 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1539 return r;
1540 }
1541
1542 /* data match */
1543 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001544}
1545
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001546LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001547lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001548{
Michal Vaskof277d362023-04-24 09:08:31 +02001549 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001550}
1551
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001552LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001553lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1554{
1555 if (!meta1 || !meta2) {
1556 if (meta1 == meta2) {
1557 return LY_SUCCESS;
1558 } else {
1559 return LY_ENOT;
1560 }
1561 }
1562
Michal Vaskoa8083062020-11-06 17:22:10 +01001563 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001564 return LY_ENOT;
1565 }
1566
Michal Vasko21725742020-06-29 11:49:25 +02001567 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1568}
1569
Radek Krejci22ebdba2019-07-25 13:59:43 +02001570/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001571 * @brief Create a copy of the attribute.
1572 *
1573 * @param[in] attr Attribute to copy.
1574 * @param[in] node Opaque where to append the new attribute.
1575 * @param[out] dup Optional created attribute copy.
1576 * @return LY_ERR value.
1577 */
1578static LY_ERR
1579lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1580{
1581 LY_ERR ret = LY_SUCCESS;
1582 struct lyd_attr *a, *last;
1583 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1584
1585 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1586
1587 /* create a copy */
1588 a = calloc(1, sizeof *attr);
1589 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1590
1591 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1592 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1593 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1594 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1595 a->hints = attr->hints;
1596 a->format = attr->format;
1597 if (attr->val_prefix_data) {
1598 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1599 LY_CHECK_GOTO(ret, finish);
1600 }
1601
1602 /* insert as the last attribute */
1603 a->parent = opaq;
1604 if (opaq->attr) {
1605 for (last = opaq->attr; last->next; last = last->next) {}
1606 last->next = a;
1607 } else {
1608 opaq->attr = a;
1609 }
1610
1611finish:
1612 if (ret) {
1613 lyd_free_attr_single(LYD_CTX(node), a);
1614 } else if (dup) {
1615 *dup = a;
1616 }
1617 return LY_SUCCESS;
1618}
1619
1620/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001621 * @brief Find @p schema equivalent in @p trg_ctx.
1622 *
1623 * @param[in] schema Schema node to find.
1624 * @param[in] trg_ctx Target context to search in.
1625 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001626 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001627 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1628 * @return LY_RRR value.
1629 */
1630static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001631lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1632 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001633{
Michal Vasko9beceb82022-04-05 12:14:15 +02001634 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1635 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001636 char *path;
1637
1638 if (!schema) {
1639 /* opaque node */
1640 *trg_schema = NULL;
1641 return LY_SUCCESS;
1642 }
1643
Michal Vasko9beceb82022-04-05 12:14:15 +02001644 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001645 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001646 trg_parent = parent->schema;
1647 src_parent = lysc_data_parent(schema);
1648 }
1649
1650 do {
1651 /* find the next parent */
1652 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001653 while (lysc_data_parent(sp) != src_parent) {
1654 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001655 }
1656 src_parent = sp;
1657
1658 if (!src_parent->parent) {
1659 /* find the module first */
1660 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1661 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001662 if (log) {
1663 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1664 src_parent->module->name);
1665 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001666 return LY_ENOTFOUND;
1667 }
1668 }
1669
1670 /* find the next parent */
1671 assert(trg_parent || trg_mod);
1672 tp = NULL;
1673 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1674 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1675 break;
1676 }
1677 }
1678 if (!tp) {
1679 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001680 if (log) {
1681 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1682 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1683 free(path);
1684 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001685 return LY_ENOTFOUND;
1686 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001687
Michal Vasko9beceb82022-04-05 12:14:15 +02001688 trg_parent = tp;
1689 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001690
Michal Vasko9beceb82022-04-05 12:14:15 +02001691 /* success */
1692 *trg_schema = trg_parent;
1693 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001694}
1695
1696/**
Michal Vasko52927e22020-03-16 17:26:14 +01001697 * @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 +02001698 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001699 * Ignores ::LYD_DUP_WITH_PARENTS and ::LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001700 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001701 * @param[in] node Node to duplicate.
1702 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001703 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001704 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1705 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001706 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1707 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001708 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1709 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001710 */
Michal Vasko52927e22020-03-16 17:26:14 +01001711static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001712lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1713 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001714{
Michal Vasko52927e22020-03-16 17:26:14 +01001715 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001716 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001717 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001718 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001719 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001720 const struct lysc_type *type;
1721 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001722
Michal Vasko52927e22020-03-16 17:26:14 +01001723 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001724
Michal Vasko19175b62022-04-01 09:17:07 +02001725 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001726 if (options & LYD_DUP_NO_EXT) {
1727 /* no not duplicate this subtree */
1728 return LY_SUCCESS;
1729 }
1730
Michal Vasko19175b62022-04-01 09:17:07 +02001731 /* we need to use the same context */
1732 trg_ctx = LYD_CTX(node);
1733 }
1734
Michal Vasko52927e22020-03-16 17:26:14 +01001735 if (!node->schema) {
1736 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001737 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001738 } else {
1739 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001740 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001741 case LYS_ACTION:
1742 case LYS_NOTIF:
1743 case LYS_CONTAINER:
1744 case LYS_LIST:
1745 dup = calloc(1, sizeof(struct lyd_node_inner));
1746 break;
1747 case LYS_LEAF:
1748 case LYS_LEAFLIST:
1749 dup = calloc(1, sizeof(struct lyd_node_term));
1750 break;
1751 case LYS_ANYDATA:
1752 case LYS_ANYXML:
1753 dup = calloc(1, sizeof(struct lyd_node_any));
1754 break;
1755 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001756 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001757 ret = LY_EINT;
1758 goto error;
1759 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001760 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001761 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001762
Michal Vaskof6df0a02020-06-16 13:08:34 +02001763 if (options & LYD_DUP_WITH_FLAGS) {
1764 dup->flags = node->flags;
1765 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001766 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001767 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02001768 if (options & LYD_DUP_WITH_PRIV) {
1769 dup->priv = node->priv;
1770 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001771 if (trg_ctx == LYD_CTX(node)) {
1772 dup->schema = node->schema;
1773 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001774 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001775 if (ret) {
1776 /* has no schema but is not an opaque node */
1777 free(dup);
1778 dup = NULL;
1779 goto error;
1780 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001781 }
Michal Vasko52927e22020-03-16 17:26:14 +01001782 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001783
Michal Vasko9cf62422021-07-01 13:29:32 +02001784 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001785 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001786 if (!node->schema) {
1787 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1788 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1789 }
1790 } else {
1791 LY_LIST_FOR(node->meta, meta) {
1792 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1793 }
Michal Vasko25a32822020-07-09 15:48:22 +02001794 }
1795 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001796
1797 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001798 if (!dup->schema) {
1799 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1800 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1801 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001802
1803 if (options & LYD_DUP_RECURSIVE) {
1804 /* duplicate all the children */
1805 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001806 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001807 }
1808 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001809 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1810 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1811 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1812 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001813 opaq->hints = orig->hints;
1814 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001815 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001816 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001817 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001818 }
Michal Vasko52927e22020-03-16 17:26:14 +01001819 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1820 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1821 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1822
1823 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001824 if (trg_ctx == LYD_CTX(node)) {
1825 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1826 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1827 } else {
1828 /* store canonical value in the target context */
1829 val_can = lyd_get_value(node);
1830 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001831 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 +01001832 LYD_HINT_DATA, term->schema, NULL);
1833 LY_CHECK_GOTO(ret, error);
1834 }
Michal Vasko52927e22020-03-16 17:26:14 +01001835 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1836 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1837 struct lyd_node *child;
1838
1839 if (options & LYD_DUP_RECURSIVE) {
1840 /* duplicate all the children */
1841 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001842 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001843 }
Michal Vasko69730152020-10-09 16:30:07 +02001844 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001845 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001846 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001847 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001848 }
1849 }
1850 lyd_hash(dup);
1851 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001852 dup->hash = node->hash;
1853 any = (struct lyd_node_any *)node;
1854 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001855 }
1856
Michal Vasko52927e22020-03-16 17:26:14 +01001857 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001858 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001859
1860 if (dup_p) {
1861 *dup_p = dup;
1862 }
1863 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001864
1865error:
Michal Vasko52927e22020-03-16 17:26:14 +01001866 lyd_free_tree(dup);
1867 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001868}
1869
Michal Vasko29d674b2021-08-25 11:18:35 +02001870/**
1871 * @brief Get a parent node to connect duplicated subtree to.
1872 *
1873 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001874 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001875 * @param[in] parent Initial parent to connect to.
1876 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1877 * @param[out] dup_parent First duplicated parent node, if any.
1878 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1879 * @return LY_ERR value.
1880 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001881static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001882lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1883 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001884{
Michal Vasko58d89e92023-05-23 09:56:19 +02001885 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001886 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02001887 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001888
1889 *dup_parent = NULL;
1890 *local_parent = NULL;
1891
Michal Vasko83522192022-07-20 08:07:34 +02001892 if (node->flags & LYD_EXT) {
1893 ext_parent = 1;
1894 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001895 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001896 if (ext_parent) {
1897 /* use the standard context */
1898 trg_ctx = LYD_CTX(orig_parent);
1899 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001900 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001901 /* stop creating parents, connect what we have into the provided parent */
1902 iter = parent;
1903 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001904 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1905 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001906 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001907 iter = parent;
1908 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001909 } else {
1910 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001911 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001912
1913 /* insert into the previous duplicated parent */
1914 if (*dup_parent) {
1915 lyd_insert_node(iter, NULL, *dup_parent, 0);
1916 }
1917
1918 /* update the last duplicated parent */
1919 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001920 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001921
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001922 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001923 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001924 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001925 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001926
Michal Vasko83522192022-07-20 08:07:34 +02001927 if (orig_parent->flags & LYD_EXT) {
1928 ext_parent = 1;
1929 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001930 }
1931
1932 if (repeat && parent) {
1933 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001934 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1935 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001936 return LY_EINVAL;
1937 }
1938
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001939 if (*dup_parent && parent) {
1940 /* last insert into a prevously-existing parent */
1941 lyd_insert_node(parent, NULL, *dup_parent, 0);
1942 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001943 return LY_SUCCESS;
1944}
1945
1946static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001947lyd_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 +01001948 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001949{
1950 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001951 const struct lyd_node *orig; /* original node to be duplicated */
1952 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001953 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001954 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001955
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001956 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001957
1958 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001959 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 +02001960 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001961 } else {
1962 local_parent = parent;
1963 }
1964
Radek Krejci22ebdba2019-07-25 13:59:43 +02001965 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001966 if (lysc_is_key(orig->schema)) {
1967 if (local_parent) {
1968 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001969 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001970 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001971 } else {
1972 assert(!(options & LYD_DUP_WITH_PARENTS));
1973 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001974 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001975 LY_CHECK_GOTO(rc, error);
1976 }
1977 } else {
1978 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001979 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001980 LY_CHECK_GOTO(rc, error);
1981 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001982 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001983 break;
1984 }
1985 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001986
Michal Vasko3a41dff2020-07-15 14:30:28 +02001987 if (dup) {
1988 *dup = first;
1989 }
1990 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001991
1992error:
1993 if (top) {
1994 lyd_free_tree(top);
1995 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001996 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001997 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001998 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001999}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002000
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002001/**
2002 * @brief Check the context of node and parent when duplicating nodes.
2003 *
2004 * @param[in] node Node to duplicate.
2005 * @param[in] parent Parent of the duplicated node(s).
2006 * @return LY_ERR value.
2007 */
2008static LY_ERR
2009lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2010{
2011 const struct lyd_node *iter;
2012
2013 if (!node || !parent) {
2014 return LY_SUCCESS;
2015 }
2016
2017 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2018 /* try to find top-level ext data parent */
2019 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2020
2021 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002022 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002023 return LY_EINVAL;
2024 }
2025 }
2026
2027 return LY_SUCCESS;
2028}
2029
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002030LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002031lyd_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 +02002032{
Michal Vaskoddd76592022-01-17 13:34:48 +01002033 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002034 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002035
Michal Vasko58d89e92023-05-23 09:56:19 +02002036 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002037}
2038
2039LIBYANG_API_DEF LY_ERR
2040lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2041 uint32_t options, struct lyd_node **dup)
2042{
2043 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2044
Michal Vasko58d89e92023-05-23 09:56:19 +02002045 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002046}
2047
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002048LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002049lyd_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 +02002050{
Michal Vaskoddd76592022-01-17 13:34:48 +01002051 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002052 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002053
Michal Vasko58d89e92023-05-23 09:56:19 +02002054 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002055}
2056
2057LIBYANG_API_DEF LY_ERR
2058lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2059 uint32_t options, struct lyd_node **dup)
2060{
2061 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2062
Michal Vasko58d89e92023-05-23 09:56:19 +02002063 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002064}
2065
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002066LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002067lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002068{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002069 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002070 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002071 struct lyd_meta *mt, *last;
2072
Michal Vasko3a41dff2020-07-15 14:30:28 +02002073 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002074
Michal Vasko33c48972022-07-20 10:28:07 +02002075 /* log to node context but value must always use the annotation context */
2076 ctx = meta->annotation->module->ctx;
2077
Michal Vasko25a32822020-07-09 15:48:22 +02002078 /* create a copy */
2079 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002080 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002081 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002082 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002083 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002084 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002085
2086 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002087 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002088 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002089 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002090 last->next = mt;
2091 } else {
2092 node->meta = mt;
2093 }
2094
Radek Krejci011e4aa2020-09-04 15:22:31 +02002095finish:
2096 if (ret) {
2097 lyd_free_meta_single(mt);
2098 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002099 *dup = mt;
2100 }
2101 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002102}
2103
Michal Vasko4490d312020-06-16 13:08:55 +02002104/**
2105 * @brief Merge a source sibling into target siblings.
2106 *
2107 * @param[in,out] first_trg First target sibling, is updated if top-level.
2108 * @param[in] parent_trg Target parent.
2109 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002110 * @param[in] merge_cb Optional merge callback.
2111 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002112 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002113 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002114 * @return LY_ERR value.
2115 */
2116static LY_ERR
2117lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Michal Vasko8efac242023-03-30 08:24:56 +02002118 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002119{
Michal Vasko4490d312020-06-16 13:08:55 +02002120 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002121 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002122 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002123 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002124 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002125 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002126 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002127
2128 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002129 if (!sibling_src->schema) {
2130 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002131 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002132 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002133 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002134 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002135 } else {
2136 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002137 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002138 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002139 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002140
Michal Vaskocd3f6172021-05-18 16:14:50 +02002141 if (match_trg) {
2142 /* update match as needed */
2143 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2144 } else {
2145 /* first instance of this node */
2146 first_inst = 1;
2147 }
2148
2149 if (match_trg) {
2150 /* call callback */
2151 if (merge_cb) {
2152 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2153 }
2154
Michal Vasko4490d312020-06-16 13:08:55 +02002155 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002156 if (!match_trg->schema) {
2157 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2158 /* update value */
2159 opaq_trg = (struct lyd_node_opaq *)match_trg;
2160 opaq_src = (struct lyd_node_opaq *)sibling_src;
2161
2162 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2163 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2164 opaq_trg->hints = opaq_src->hints;
2165
2166 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2167 opaq_trg->format = opaq_src->format;
2168 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2169 &opaq_trg->val_prefix_data);
2170 }
2171 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2172 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002173 /* since they are different, they cannot both be default */
2174 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2175
Michal Vasko3a41dff2020-07-15 14:30:28 +02002176 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2177 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002178 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002179 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2180 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002181 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002182
2183 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002184 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002185 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002186 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002187 /* update value */
2188 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002189 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002190
2191 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002192 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002193 }
2194
2195 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002196 r = LY_SUCCESS;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002197 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko42c96e22024-01-18 08:18:06 +01002198 r = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
Michal Vaskocd3f6172021-05-18 16:14:50 +02002199 &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002200 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002201 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002202 }
2203 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002204 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002205 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002206 } else {
2207 /* node not found, merge it */
2208 if (options & LYD_MERGE_DESTRUCT) {
2209 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002210 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002211 /* spend it */
2212 *sibling_src_p = NULL;
2213 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002214 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 +02002215 }
2216
Michal Vasko7e8315b2021-08-03 17:01:06 +02002217 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2218 /* set LYD_NEW for all the new nodes, required for validation */
2219 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2220 elem->flags |= LYD_NEW;
2221 LYD_TREE_DFS_END(dup_src, elem);
2222 }
Michal Vasko4490d312020-06-16 13:08:55 +02002223 }
2224
Michal Vaskocd3f6172021-05-18 16:14:50 +02002225 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002226 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002227
2228 if (first_inst) {
2229 /* remember not to find this instance next time */
2230 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2231 }
2232
2233 /* call callback, no source node */
2234 if (merge_cb) {
2235 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2236 }
Michal Vasko4490d312020-06-16 13:08:55 +02002237 }
2238
2239 return LY_SUCCESS;
2240}
2241
Michal Vasko3a41dff2020-07-15 14:30:28 +02002242static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002243lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2244 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002245{
2246 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002247 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002248 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002249 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002250
2251 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002252 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2253 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002254
2255 if (!source) {
2256 /* nothing to merge */
2257 return LY_SUCCESS;
2258 }
2259
Michal Vasko831422b2020-11-24 11:20:51 +01002260 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002261 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002262 return LY_EINVAL;
2263 }
2264
2265 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002266 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2267 /* skip data nodes from different modules */
2268 continue;
2269 }
2270
Radek Krejci857189e2020-09-01 13:26:36 +02002271 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002272 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2273 if (ret) {
2274 break;
2275 }
Michal Vasko4490d312020-06-16 13:08:55 +02002276 if (first && !sibling_src) {
2277 /* source was spent (unlinked), move to the next node */
2278 source = tmp;
2279 }
2280
Michal Vasko3a41dff2020-07-15 14:30:28 +02002281 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002282 break;
2283 }
2284 }
2285
2286 if (options & LYD_MERGE_DESTRUCT) {
2287 /* free any leftover source data that were not merged */
2288 lyd_free_siblings((struct lyd_node *)source);
2289 }
2290
Michal Vaskocd3f6172021-05-18 16:14:50 +02002291 lyd_dup_inst_free(dup_inst);
2292 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002293}
2294
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002295LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002296lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002297{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002298 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002299}
2300
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002301LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002302lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002303{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002304 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2305}
2306
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002307LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002308lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2309 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2310{
2311 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002312}
2313
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002314static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002315lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002316{
Michal Vasko14654712020-02-06 08:35:21 +01002317 /* ending \0 */
2318 ++reqlen;
2319
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002320 if (reqlen > *buflen) {
2321 if (is_static) {
2322 return LY_EINCOMPLETE;
2323 }
2324
2325 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2326 if (!*buffer) {
2327 return LY_EMEM;
2328 }
2329
2330 *buflen = reqlen;
2331 }
2332
2333 return LY_SUCCESS;
2334}
2335
Michal Vaskod59035b2020-07-08 12:00:06 +02002336LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002337lyd_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 +02002338{
2339 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002340 size_t len;
2341 const char *val;
2342 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002343
Michal Vasko824d0832021-11-04 15:36:51 +01002344 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002345 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002346 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002347 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002348
2349 quot = '\'';
2350 if (strchr(val, '\'')) {
2351 quot = '"';
2352 }
2353 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002354 }
2355
2356 return LY_SUCCESS;
2357}
2358
2359/**
2360 * @brief Append leaf-list value predicate to path.
2361 *
2362 * @param[in] node Node to print.
2363 * @param[in,out] buffer Buffer to print to.
2364 * @param[in,out] buflen Current buffer length.
2365 * @param[in,out] bufused Current number of characters used in @p buffer.
2366 * @param[in] is_static Whether buffer is static or can be reallocated.
2367 * @return LY_ERR
2368 */
2369static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002370lyd_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 +02002371{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002372 size_t len;
2373 const char *val;
2374 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002375
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002376 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002377 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002378 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002379
2380 quot = '\'';
2381 if (strchr(val, '\'')) {
2382 quot = '"';
2383 }
2384 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2385
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002386 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002387}
2388
2389/**
2390 * @brief Append node position (relative to its other instances) predicate to path.
2391 *
2392 * @param[in] node Node to print.
2393 * @param[in,out] buffer Buffer to print to.
2394 * @param[in,out] buflen Current buffer length.
2395 * @param[in,out] bufused Current number of characters used in @p buffer.
2396 * @param[in] is_static Whether buffer is static or can be reallocated.
2397 * @return LY_ERR
2398 */
2399static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002400lyd_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 +02002401{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002402 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002403 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002404 char *val = NULL;
2405 LY_ERR rc;
2406
Michal Vasko50cc0562021-05-18 16:15:43 +02002407 pos = lyd_list_pos(node);
2408 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002409 return LY_EMEM;
2410 }
2411
2412 len = 1 + strlen(val) + 1;
2413 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2414 if (rc != LY_SUCCESS) {
2415 goto cleanup;
2416 }
2417
2418 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2419
2420cleanup:
2421 free(val);
2422 return rc;
2423}
2424
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002425LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002426lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2427{
Radek Krejci857189e2020-09-01 13:26:36 +02002428 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002429 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002430 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002431 const struct lyd_node *iter, *parent;
2432 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002433 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002434
2435 LY_CHECK_ARG_RET(NULL, node, NULL);
2436 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002437 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002438 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002439 } else {
2440 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002441 }
2442
2443 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002444 case LYD_PATH_STD:
2445 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002446 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002447 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002448 ++depth;
2449 }
2450
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002451 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002452 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002453 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002454 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002455iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002456 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002457 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002458 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002459 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002460 if (prev_mod == mod) {
2461 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002462 }
2463
2464 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002465 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2466 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002467 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2468 if (rc != LY_SUCCESS) {
2469 break;
2470 }
2471
2472 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002473 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002474
Michal Vasko790b2bc2020-08-03 13:35:06 +02002475 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002476 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002477 switch (iter->schema->nodetype) {
2478 case LYS_LIST:
2479 if (iter->schema->flags & LYS_KEYLESS) {
2480 /* print its position */
2481 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2482 } else {
2483 /* print all list keys in predicates */
2484 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2485 }
2486 break;
2487 case LYS_LEAFLIST:
2488 if (iter->schema->flags & LYS_CONFIG_W) {
2489 /* print leaf-list value */
2490 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2491 } else {
2492 /* print its position */
2493 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2494 }
2495 break;
2496 default:
2497 /* nothing to print more */
2498 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002499 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002500 }
2501 if (rc != LY_SUCCESS) {
2502 break;
2503 }
2504
Michal Vasko14654712020-02-06 08:35:21 +01002505 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002506 }
2507 break;
2508 }
2509
2510 return buffer;
2511}
Michal Vaskoe444f752020-02-10 12:20:06 +01002512
Michal Vaskodbf3e652022-10-21 08:46:25 +02002513char *
2514lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2515{
2516 uint32_t depth;
2517 size_t bufused = 0, buflen = 0, len;
2518 char *buffer = NULL;
2519 const struct lyd_node *iter, *parent;
2520 const struct lys_module *mod, *prev_mod;
2521 LY_ERR rc = LY_SUCCESS;
2522
2523 switch (pathtype) {
2524 case LYD_PATH_STD:
2525 case LYD_PATH_STD_NO_LAST_PRED:
2526 for (depth = 1; depth <= dnodes->count; ++depth) {
2527 /* current node */
2528 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002529 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002530
2531 /* parent */
2532 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002533 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2534 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002535 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2536 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002537
2538 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002539 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002540 if (prev_mod == mod) {
2541 mod = NULL;
2542 }
2543
2544 /* realloc string */
2545 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2546 strlen(((struct lyd_node_opaq *)iter)->name.name));
2547 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2548 break;
2549 }
2550
2551 /* print next node */
2552 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2553
2554 /* do not always print the last (first) predicate */
2555 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2556 switch (iter->schema->nodetype) {
2557 case LYS_LIST:
2558 if (iter->schema->flags & LYS_KEYLESS) {
2559 /* print its position */
2560 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2561 } else {
2562 /* print all list keys in predicates */
2563 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2564 }
2565 break;
2566 case LYS_LEAFLIST:
2567 if (iter->schema->flags & LYS_CONFIG_W) {
2568 /* print leaf-list value */
2569 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2570 } else {
2571 /* print its position */
2572 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2573 }
2574 break;
2575 default:
2576 /* nothing to print more */
2577 break;
2578 }
2579 }
2580 if (rc) {
2581 break;
2582 }
2583 }
2584 break;
2585 }
2586
2587 return buffer;
2588}
2589
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002590LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002591lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2592{
2593 struct lyd_meta *ret = NULL;
2594 const struct ly_ctx *ctx;
2595 const char *prefix, *tmp;
2596 char *str;
2597 size_t pref_len, name_len;
2598
2599 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002600 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002601
2602 if (!first) {
2603 return NULL;
2604 }
2605
2606 ctx = first->annotation->module->ctx;
2607
2608 /* parse the name */
2609 tmp = name;
2610 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2611 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2612 return NULL;
2613 }
2614
2615 /* find the module */
2616 if (prefix) {
2617 str = strndup(prefix, pref_len);
2618 module = ly_ctx_get_module_latest(ctx, str);
2619 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002620 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 +02002621 }
2622
2623 /* find the metadata */
2624 LY_LIST_FOR(first, first) {
2625 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2626 ret = (struct lyd_meta *)first;
2627 break;
2628 }
2629 }
2630
2631 return ret;
2632}
2633
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002634LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002635lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2636{
Michal Vasko9beceb82022-04-05 12:14:15 +02002637 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002638 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002639 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002640
Michal Vaskof03ed032020-03-04 13:31:44 +01002641 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002642 if (!siblings) {
2643 /* no data */
2644 if (match) {
2645 *match = NULL;
2646 }
2647 return LY_ENOTFOUND;
2648 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002649
Michal Vasko9beceb82022-04-05 12:14:15 +02002650 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2651 /* create a duplicate in this context */
2652 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2653 target = dup;
2654 }
2655
2656 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2657 /* schema mismatch */
2658 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002659 if (match) {
2660 *match = NULL;
2661 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002662 return LY_ENOTFOUND;
2663 }
2664
Michal Vaskoe78faec2021-04-08 17:24:43 +02002665 /* get first sibling */
2666 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002667
Michal Vasko9e685082021-01-29 14:49:09 +01002668 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002669 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002670 assert(target->hash);
2671
Michal Vaskoe78faec2021-04-08 17:24:43 +02002672 if (lysc_is_dup_inst_list(target->schema)) {
2673 /* we must search the instances from beginning to find the first matching one */
2674 found = 0;
2675 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002676 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002677 found = 1;
2678 break;
2679 }
2680 }
2681 if (found) {
2682 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002683 } else {
2684 siblings = NULL;
2685 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002686 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002687 /* find by hash */
2688 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2689 siblings = *match_p;
2690 } else {
2691 /* not found */
2692 siblings = NULL;
2693 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002694 }
2695 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002696 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002697 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002698 if (lysc_is_dup_inst_list(target->schema)) {
2699 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2700 break;
2701 }
2702 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002703 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002704 break;
2705 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002706 }
2707 }
2708 }
2709
Michal Vasko9beceb82022-04-05 12:14:15 +02002710 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002711 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002712 if (match) {
2713 *match = NULL;
2714 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002715 return LY_ENOTFOUND;
2716 }
2717
Michal Vasko9b368d32020-02-14 13:53:31 +01002718 if (match) {
2719 *match = (struct lyd_node *)siblings;
2720 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002721 return LY_SUCCESS;
2722}
2723
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002724LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002725lyd_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 +02002726 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002727{
2728 LY_ERR rc;
2729 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002730 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002731
Michal Vasko4c583e82020-07-17 12:16:14 +02002732 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002733 if (!siblings) {
2734 /* no data */
2735 if (match) {
2736 *match = NULL;
2737 }
2738 return LY_ENOTFOUND;
2739 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002740
Michal Vasko9beceb82022-04-05 12:14:15 +02002741 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2742 /* parent of ext nodes is useless */
2743 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002744 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2745 /* no schema node in siblings so certainly no data node either */
2746 if (match) {
2747 *match = NULL;
2748 }
2749 return LY_ENOTFOUND;
2750 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002751 }
2752
2753 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2754 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002755 if (match) {
2756 *match = NULL;
2757 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002758 return LY_ENOTFOUND;
2759 }
2760
Michal Vaskof03ed032020-03-04 13:31:44 +01002761 if (key_or_value && !val_len) {
2762 val_len = strlen(key_or_value);
2763 }
2764
Michal Vaskob104f112020-07-17 09:54:54 +02002765 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2766 /* create a data node and find the instance */
2767 if (schema->nodetype == LYS_LEAFLIST) {
2768 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002769 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 +02002770 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002771 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002772 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002773 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002774 }
2775
2776 /* find it */
2777 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002778 } else {
2779 /* find the first schema node instance */
2780 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002781 }
2782
Michal Vaskoe444f752020-02-10 12:20:06 +01002783 lyd_free_tree(target);
2784 return rc;
2785}
Michal Vaskoccc02342020-05-21 10:09:21 +02002786
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002787LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002788lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2789{
2790 struct lyd_node **match_p, *first, *iter;
2791 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002792 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002793
Michal Vasko83ae7772022-06-08 10:01:55 +02002794 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002795 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002796
2797 LY_CHECK_RET(ly_set_new(set));
2798
2799 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2800 /* no data or schema mismatch */
2801 return LY_ENOTFOUND;
2802 }
2803
Michal Vaskoee9b9482023-06-19 13:17:48 +02002804 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002805 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002806
Michal Vaskoe78faec2021-04-08 17:24:43 +02002807 /* get first sibling */
2808 siblings = lyd_first_sibling(siblings);
2809
2810 parent = siblings->parent;
2811 if (parent && parent->schema && parent->children_ht) {
2812 assert(target->hash);
2813
2814 /* find the first instance */
2815 lyd_find_sibling_first(siblings, target, &first);
2816 if (first) {
2817 /* add it so that it is the first in the set */
2818 if (ly_set_add(*set, first, 1, NULL)) {
2819 goto error;
2820 }
2821
2822 /* find by hash */
2823 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2824 iter = *match_p;
2825 } else {
2826 /* not found */
2827 iter = NULL;
2828 }
2829 while (iter) {
2830 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002831 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002832 goto error;
2833 }
2834
2835 /* find next instance */
2836 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2837 iter = NULL;
2838 } else {
2839 iter = *match_p;
2840 }
2841 }
2842 }
2843 } else {
2844 /* no children hash table */
2845 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002846 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002847 ly_set_add(*set, (void *)siblings, 1, NULL);
2848 }
2849 }
2850 }
2851
2852 if (!(*set)->count) {
2853 return LY_ENOTFOUND;
2854 }
2855 return LY_SUCCESS;
2856
2857error:
2858 ly_set_free(*set, NULL);
2859 *set = NULL;
2860 return LY_EMEM;
2861}
2862
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002863LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002864lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2865{
2866 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2867
Michal Vaskoe271a312023-08-15 11:46:30 +02002868 if (first && first->schema) {
2869 first = first->prev;
2870 if (first->schema) {
2871 /* no opaque nodes */
2872 first = NULL;
2873 } else {
2874 /* opaque nodes are at the end, find quickly the first */
2875 while (!first->prev->schema) {
2876 first = first->prev;
2877 }
2878 }
2879 }
2880
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002881 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02002882 assert(!first->schema);
2883 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002884 break;
2885 }
2886 }
2887
2888 if (match) {
2889 *match = (struct lyd_node *)first;
2890 }
2891 return first ? LY_SUCCESS : LY_ENOTFOUND;
2892}
2893
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002894LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002895lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002896{
Michal Vaskod96e2372023-02-24 16:07:51 +01002897 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002898
Michal Vaskod96e2372023-02-24 16:07:51 +01002899 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2900}
Michal Vaskoccc02342020-05-21 10:09:21 +02002901
Michal Vaskod96e2372023-02-24 16:07:51 +01002902LIBYANG_API_DEF LY_ERR
2903lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2904{
2905 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002906
Michal Vaskod96e2372023-02-24 16:07:51 +01002907 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002908}
Radek Krejcica989142020-11-05 11:32:22 +01002909
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002910LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002911lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2912 const struct lyxp_var *vars, struct ly_set **set)
2913{
2914 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2915
2916 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2917}
2918
2919LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002920lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2921 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002922{
Michal Vaskod96e2372023-02-24 16:07:51 +01002923 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002924
Michal Vaskod96e2372023-02-24 16:07:51 +01002925 *set = NULL;
2926
2927 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 +01002928}
2929
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002930LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002931lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002932{
Michal Vaskod96e2372023-02-24 16:07:51 +01002933 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002934}
2935
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002936LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002937lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2938{
2939 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2940}
2941
2942LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002943lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2944 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002945{
Michal Vaskod96e2372023-02-24 16:07:51 +01002946 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2947}
2948
2949LIBYANG_API_DEF LY_ERR
2950lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2951 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2952 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2953{
2954 LY_ERR ret = LY_SUCCESS;
2955 struct lyxp_set xp_set = {0};
2956 struct lyxp_expr *exp = NULL;
2957 uint32_t i;
2958
2959 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2960 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2961 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2962
2963 /* parse expression */
2964 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2965 LY_CHECK_GOTO(ret, cleanup);
2966
2967 /* evaluate expression */
2968 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2969 LYXP_IGNORE_WHEN);
2970 LY_CHECK_GOTO(ret, cleanup);
2971
2972 /* return expected result type without or with casting */
2973 if (node_set) {
2974 /* node set */
2975 if (xp_set.type == LYXP_SET_NODE_SET) {
2976 /* transform into a set */
2977 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2978 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2979 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2980 (*node_set)->size = xp_set.used;
2981 for (i = 0; i < xp_set.used; ++i) {
2982 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2983 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2984 LY_CHECK_GOTO(ret, cleanup);
2985 }
2986 }
2987 if (ret_type) {
2988 *ret_type = LY_XPATH_NODE_SET;
2989 }
2990 } else if (!string && !number && !boolean) {
2991 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2992 ret = LY_EINVAL;
2993 goto cleanup;
2994 }
2995 }
2996
2997 if (string) {
2998 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2999 /* cast into string */
3000 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3001 }
3002 if (xp_set.type == LYXP_SET_STRING) {
3003 /* string */
3004 *string = xp_set.val.str;
3005 xp_set.val.str = NULL;
3006 if (ret_type) {
3007 *ret_type = LY_XPATH_STRING;
3008 }
3009 }
3010 }
3011
3012 if (number) {
3013 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3014 /* cast into number */
3015 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3016 }
3017 if (xp_set.type == LYXP_SET_NUMBER) {
3018 /* number */
3019 *number = xp_set.val.num;
3020 if (ret_type) {
3021 *ret_type = LY_XPATH_NUMBER;
3022 }
3023 }
3024 }
3025
3026 if (boolean) {
3027 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3028 /* cast into boolean */
3029 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3030 }
3031 if (xp_set.type == LYXP_SET_BOOLEAN) {
3032 /* boolean */
3033 *boolean = xp_set.val.bln;
3034 if (ret_type) {
3035 *ret_type = LY_XPATH_BOOLEAN;
3036 }
3037 }
3038 }
3039
3040cleanup:
3041 lyxp_set_free_content(&xp_set);
3042 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3043 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003044}
3045
Michal Vasko99a77882024-01-04 14:50:51 +01003046/**
3047 * @brief Hash table node equal callback.
3048 */
3049static ly_bool
3050lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3051{
3052 struct lyd_node *node1, *node2;
3053
3054 node1 = *(struct lyd_node **)val1_p;
3055 node2 = *(struct lyd_node **)val2_p;
3056
3057 return node1 == node2;
3058}
3059
3060LIBYANG_API_DEF LY_ERR
3061lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3062{
3063 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003064 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003065 struct lyxp_set xp_set = {0};
3066 struct lyxp_expr *exp = NULL;
3067 struct lyd_node *node, *parent;
3068 struct lyxp_set_hash_node hnode;
3069 struct ly_ht *parent_ht = NULL;
3070 struct ly_set free_set = {0};
3071 uint32_t i, hash;
3072 ly_bool is_result;
3073
3074 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3075
3076 if (!*tree) {
3077 /* nothing to do */
3078 goto cleanup;
3079 }
3080
3081 *tree = lyd_first_sibling(*tree);
3082 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3083
3084 /* parse expression */
3085 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3086 LY_CHECK_GOTO(ret, cleanup);
3087
3088 /* evaluate expression */
3089 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3090 LY_CHECK_GOTO(ret, cleanup);
3091
3092 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003093 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003094 LY_CHECK_GOTO(!parent_ht, cleanup);
3095
3096 for (i = 0; i < xp_set.used; ++i) {
3097 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3098 /* ignore */
3099 continue;
3100 }
3101
3102 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3103 /* add the parent into parent_ht */
3104 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3105 if (ret == LY_EEXIST) {
3106 /* shared parent, we are done */
3107 break;
3108 }
3109 LY_CHECK_GOTO(ret, cleanup);
3110 }
3111 }
3112
3113 hnode.type = LYXP_NODE_ELEM;
3114 LY_LIST_FOR(*tree, parent) {
3115 LYD_TREE_DFS_BEGIN(parent, node) {
3116 if (lysc_is_key(node->schema)) {
3117 /* ignore */
3118 goto next_iter;
3119 }
3120
3121 /* check the results */
3122 is_result = 0;
3123 if (xp_set.ht) {
3124 hnode.node = node;
3125 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3126 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3127 hash = lyht_hash_multi(hash, NULL, 0);
3128
3129 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3130 is_result = 1;
3131 }
3132 } else {
3133 /* not enough elements for a hash table */
3134 for (i = 0; i < xp_set.used; ++i) {
3135 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3136 /* ignore */
3137 continue;
3138 }
3139
3140 if (xp_set.val.nodes[i].node == node) {
3141 is_result = 1;
3142 break;
3143 }
3144 }
3145 }
3146
3147 if (is_result) {
3148 /* keep the whole subtree if the node is in the results */
3149 LYD_TREE_DFS_continue = 1;
3150 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3151 /* free the whole subtree if the node is not even among the selected parents */
3152 ret = ly_set_add(&free_set, node, 1, NULL);
3153 LY_CHECK_GOTO(ret, cleanup);
3154 LYD_TREE_DFS_continue = 1;
3155 } /* else keep the parent node because a subtree is in the results */
3156
3157next_iter:
3158 LYD_TREE_DFS_END(parent, node);
3159 }
3160 }
3161
3162 /* free */
3163 for (i = 0; i < free_set.count; ++i) {
3164 node = free_set.dnodes[i];
3165 if (*tree == node) {
3166 *tree = (*tree)->next;
3167 }
3168 lyd_free_tree(node);
3169 }
3170
3171cleanup:
3172 lyxp_set_free_content(&xp_set);
3173 lyxp_expr_free(ctx, exp);
3174 lyht_free(parent_ht, NULL);
3175 ly_set_erase(&free_set, NULL);
3176 return ret;
3177}
3178
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003179LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003180lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3181{
3182 LY_ERR ret = LY_SUCCESS;
3183 struct lyxp_expr *expr = NULL;
3184 struct ly_path *lypath = NULL;
3185
3186 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3187
3188 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003189 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 +01003190 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003191 LY_CHECK_GOTO(ret, cleanup);
3192
3193 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003194 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003195 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 +01003196 LY_CHECK_GOTO(ret, cleanup);
3197
3198 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003199 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003200
3201cleanup:
3202 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3203 ly_path_free(LYD_CTX(ctx_node), lypath);
3204 return ret;
3205}
3206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003207LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003208lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3209{
3210 LY_ERR ret;
3211 struct lyd_node *m;
3212
3213 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3214
Michal Vasko90189962023-02-28 12:10:34 +01003215 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003216 if (ret) {
3217 if (match) {
3218 *match = NULL;
3219 }
3220 return LY_ENOTFOUND;
3221 }
3222
3223 if (match) {
3224 *match = m;
3225 }
3226 return LY_SUCCESS;
3227}
Irfand3b351a2023-09-14 14:52:15 +02003228
3229LIBYANG_API_DEF struct lyd_node *
3230lyd_parent(const struct lyd_node *node)
3231{
3232 if (!node || !node->parent) {
3233 return NULL;
3234 }
3235
3236 return &node->parent->node;
3237}
3238
3239LIBYANG_API_DEF struct lyd_node *
3240lyd_child(const struct lyd_node *node)
3241{
3242 if (!node) {
3243 return NULL;
3244 }
3245
3246 if (!node->schema) {
3247 /* opaq node */
3248 return ((const struct lyd_node_opaq *)node)->child;
3249 }
3250
3251 switch (node->schema->nodetype) {
3252 case LYS_CONTAINER:
3253 case LYS_LIST:
3254 case LYS_RPC:
3255 case LYS_ACTION:
3256 case LYS_NOTIF:
3257 return ((const struct lyd_node_inner *)node)->child;
3258 default:
3259 return NULL;
3260 }
3261}
3262
3263LIBYANG_API_DEF const char *
3264lyd_get_value(const struct lyd_node *node)
3265{
3266 if (!node) {
3267 return NULL;
3268 }
3269
3270 if (!node->schema) {
3271 return ((const struct lyd_node_opaq *)node)->value;
3272 } else if (node->schema->nodetype & LYD_NODE_TERM) {
3273 const struct lyd_value *value = &((const struct lyd_node_term *)node)->value;
3274
3275 return value->_canonical ? value->_canonical : lyd_value_get_canonical(LYD_CTX(node), value);
3276 }
3277
3278 return NULL;
3279}
stewegf9041a22024-01-18 13:29:12 +01003280
3281LY_ERR
3282lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3283{
3284 struct ly_ht *ht;
3285 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003286 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003287
3288 assert(node);
3289 assert(record);
3290
Michal Vaskob46061d2024-01-18 13:58:13 +01003291 *record = NULL;
3292
stewegf9041a22024-01-18 13:29:12 +01003293 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003294 return LY_EDENIED;
3295 }
3296
3297 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003298 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003299 hash = lyht_hash((const char *)&node, sizeof node);
3300
stewegf9041a22024-01-18 13:29:12 +01003301 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3302 if (create) {
3303 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3304 } else {
stewegf9041a22024-01-18 13:29:12 +01003305 return LY_ENOTFOUND;
3306 }
3307 }
3308
3309 return LY_SUCCESS;
3310}
3311
3312LIBYANG_API_DEF LY_ERR
3313lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3314{
3315 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3316
3317 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3318}
3319
3320LY_ERR
3321lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3322{
stewegf9041a22024-01-18 13:29:12 +01003323 const struct lyd_node_term **item = NULL;
3324 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003325 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003326
3327 assert(node);
3328 assert(leafref_node);
3329
3330 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3331 return LY_EDENIED;
3332 }
3333
steweg67388952024-01-25 12:14:50 +01003334 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003335 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3336 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3337 if (rec->leafref_nodes[u] == leafref_node) {
3338 return LY_SUCCESS;
3339 }
3340 }
3341
3342 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3343 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003344
3345 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003346 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003347 LY_ARRAY_FOR(rec->target_nodes, u) {
3348 if (rec->target_nodes[u] == node) {
3349 return LY_SUCCESS;
3350 }
3351 }
3352
3353 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3354 *item = node;
3355
stewegf9041a22024-01-18 13:29:12 +01003356 return LY_SUCCESS;
3357}
3358
3359LIBYANG_API_DEF LY_ERR
3360lyd_leafref_link_node_tree(const struct lyd_node *tree)
3361{
3362 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003363 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003364 char *errmsg;
3365 struct lyd_node_term *leafref_node;
3366 struct lysc_node_leaf *leaf_schema;
3367 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003368 LY_ERR ret = LY_SUCCESS;
3369 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003370
3371 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3372
3373 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3374 return LY_EDENIED;
3375 }
3376
3377 LY_LIST_FOR(tree, sibling) {
3378 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003379 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003380 leafref_node = (struct lyd_node_term *)elem;
3381 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003382
stewegf9041a22024-01-18 13:29:12 +01003383 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3384 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003385 ly_set_free(targets, NULL);
3386 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003387 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003388 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003389 } else {
3390 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003391 for (i = 0; i < targets->count; ++i) {
3392 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3393 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3394 LY_CHECK_GOTO(ret, cleanup);
3395 }
3396 }
stewegf9041a22024-01-18 13:29:12 +01003397 }
3398 }
3399 }
3400 LYD_TREE_DFS_END(sibling, elem);
3401 }
3402 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003403
steweg67388952024-01-25 12:14:50 +01003404cleanup:
3405 ly_set_free(targets, NULL);
3406 return ret;
stewegf9041a22024-01-18 13:29:12 +01003407}
3408
3409LY_ERR
3410lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3411{
3412 LY_ERR ret;
3413 struct lyd_leafref_links_rec *rec;
3414
3415 assert(node);
3416 assert(leafref_node);
3417
3418 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3419 return LY_EDENIED;
3420 }
3421
steweg67388952024-01-25 12:14:50 +01003422 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003423 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3424 if (ret == LY_SUCCESS) {
3425 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003426 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003427 lyd_free_leafref_nodes(node);
3428 }
3429 } else if (ret != LY_ENOTFOUND) {
3430 return ret;
3431 }
3432
steweg67388952024-01-25 12:14:50 +01003433 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003434 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3435 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003436 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3437 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003438 lyd_free_leafref_nodes(leafref_node);
3439 }
3440 } else if (ret != LY_ENOTFOUND) {
3441 return ret;
3442 }
3443
3444 return LY_SUCCESS;
3445}