blob: faeae00ccd7b68ad4fdde9daadc32a5c162ac536 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02005 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci084289f2019-07-09 17:35:30 +020020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010037#include "ly_common.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020042#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010043#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
aPiecek6cf1d162023-11-08 16:07:00 +010048#include "tree_data_sorted.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010049#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "tree_schema.h"
51#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020052#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020053#include "xml.h"
54#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020055
Michal Vaskof277d362023-04-24 09:08:31 +020056static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
57 ly_bool parental_schemas_checked);
58
Radek Krejci7931b192020-06-25 17:05:03 +020059static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020060lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020061{
Michal Vasko69730152020-10-09 16:30:07 +020062 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020063 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020064 const char *path = in->method.fpath.filepath;
65 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020066
67 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020068 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020069
Radek Krejcif13b87b2020-12-01 22:02:17 +010070 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
71 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020072 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010073 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
74 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020075 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010076 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
77 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020078 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020079 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020080 }
81
Radek Krejci7931b192020-06-25 17:05:03 +020082 return format;
83}
Radek Krejcie7b95092019-05-15 11:03:07 +020084
Michal Vaskoe0665742021-02-11 11:08:44 +010085/**
86 * @brief Parse YANG data into a data tree.
87 *
88 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010089 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010090 * @param[in] parent Parent to connect the parsed nodes to, if any.
Michal Vaskob36c56f2024-01-31 12:50:07 +010091 * @param[in,out] first_p Pointer to the first parsed node.
Michal Vaskoe0665742021-02-11 11:08:44 +010092 * @param[in] in Input handle to read the input from.
93 * @param[in] format Expected format of the data in @p in.
94 * @param[in] parse_opts Options for parser.
95 * @param[in] val_opts Options for validation.
96 * @param[out] op Optional pointer to the parsed operation, if any.
97 * @return LY_ERR value.
98 */
99static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100100lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
101 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200102{
stewegd8e2fc92023-05-31 09:52:56 +0200103 LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200104 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100105 struct ly_set parsed = {0};
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100106 uint32_t i, int_opts = 0;
Michal Vasko7a266772024-01-23 11:02:38 +0100107 const struct ly_err_item *eitem;
Michal Vaskoddd76592022-01-17 13:34:48 +0100108 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200109
Michal Vaskoe0665742021-02-11 11:08:44 +0100110 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200111
112 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100113 if (first_p) {
114 *first_p = NULL;
115 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200116
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 /* remember input position */
118 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200119
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100120 /* set internal options */
121 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
122 int_opts = LYD_INTOPT_WITH_SIBLINGS;
123 }
124
Michal Vaskoe0665742021-02-11 11:08:44 +0100125 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200126 switch (format) {
127 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100128 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100129 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200131 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100132 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100133 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200135 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100136 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100137 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200138 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200139 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100140 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100141 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100142 break;
143 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100144 if (r) {
145 rc = r;
Michal Vasko7a266772024-01-23 11:02:38 +0100146 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
147 goto cleanup;
148 }
149
150 eitem = ly_err_last(ctx);
151 assert(eitem);
152 if (eitem->vecode == LYVE_SYNTAX) {
153 /* cannot get more errors on a syntax error */
Michal Vasko50da8cd2023-03-10 08:38:59 +0100154 goto cleanup;
155 }
156 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100157
Michal Vaskoa6139e02023-10-03 14:13:22 +0200158 if (parent && parsed.count) {
159 /* use the first parsed node */
Michal Vaskob36c56f2024-01-31 12:50:07 +0100160 if (first_p) {
161 *first_p = parsed.dnodes[0];
162 } else {
163 first_p = &parsed.dnodes[0];
164 }
Radek Krejci7931b192020-06-25 17:05:03 +0200165 }
166
Michal Vaskoe0665742021-02-11 11:08:44 +0100167 if (!(parse_opts & LYD_PARSE_ONLY)) {
168 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200169 r = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
Michal Vasko135719f2022-08-25 12:18:17 +0200170 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200171 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100172 }
Radek Krejci7931b192020-06-25 17:05:03 +0200173
Michal Vaskoe0665742021-02-11 11:08:44 +0100174 /* set the operation node */
175 if (op) {
176 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 }
178
179cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100180 if (lydctx) {
181 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200182 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100183 if (rc) {
184 if (parent) {
185 /* free all the parsed subtrees */
186 for (i = 0; i < parsed.count; ++i) {
187 lyd_free_tree(parsed.dnodes[i]);
188 }
189 } else {
190 /* free everything */
191 lyd_free_all(*first_p);
192 *first_p = NULL;
193 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100194 } else if (subtree_sibling) {
195 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100196 }
197 ly_set_erase(&parsed, NULL);
198 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200199}
200
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100201LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100202lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
203 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
204{
205 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
206
207 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
208 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
209 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
210
211 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
212}
213
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100214LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100215lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
216 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
217{
218 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
219 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
220 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
221
Radek Krejcif16e2542021-02-17 15:39:23 +0100222 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100223}
224
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100225LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100226lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
227 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200228{
229 LY_ERR ret;
230 struct ly_in *in;
231
232 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100233 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200234
235 ly_in_free(in, 0);
236 return ret;
237}
238
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100239LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200240lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200241 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200242{
243 LY_ERR ret;
244 struct ly_in *in;
245
246 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100247 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200248
249 ly_in_free(in, 0);
250 return ret;
251}
252
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100253LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200254lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
255 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200256{
257 LY_ERR ret;
258 struct ly_in *in;
259
260 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100261 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200262
263 ly_in_free(in, 0);
264 return ret;
265}
266
Radek Krejcif16e2542021-02-17 15:39:23 +0100267/**
268 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
269 * for schema nodes locked inside the extension instance.
270 *
271 * At least one of @p parent, @p tree, or @p op must always be set.
272 *
Michal Vasko820efe82023-05-12 15:47:43 +0200273 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100274 *
275 * @param[in] ctx libyang context.
276 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
277 * @param[in] parent Optional parent to connect the parsed nodes to.
278 * @param[in] in Input handle to read the input from.
279 * @param[in] format Expected format of the data in @p in.
280 * @param[in] data_type Expected operation to parse (@ref datatype).
281 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
282 * @param[out] op Optional parsed operation node.
283 * @return LY_ERR value.
284 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
285 */
286static LY_ERR
287lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
288 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200289{
Michal Vaskoe0665742021-02-11 11:08:44 +0100290 LY_ERR rc = LY_SUCCESS;
291 struct lyd_ctx *lydctx = NULL;
292 struct ly_set parsed = {0};
293 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100294 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200295 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200296
Michal Vasko27fb0262021-02-23 09:42:01 +0100297 if (!ctx) {
298 ctx = LYD_CTX(parent);
299 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200300 if (tree) {
301 *tree = NULL;
302 }
303 if (op) {
304 *op = NULL;
305 }
306
Radek Krejci7931b192020-06-25 17:05:03 +0200307 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200308
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 /* remember input position */
310 in->func_start = in->current;
311
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100312 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200313 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100314 val_opts = 0;
315
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100316 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100317 case LYD_TYPE_RPC_NETCONF:
318 case LYD_TYPE_NOTIF_NETCONF:
319 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200320 proto_msg = 1;
321 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100322 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200323 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
324 tree, !op, LY_EINVAL);
325 proto_msg = 1;
326 break;
327 case LYD_TYPE_RPC_RESTCONF:
328 case LYD_TYPE_REPLY_RESTCONF:
329 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
330 proto_msg = 1;
331 break;
332 case LYD_TYPE_NOTIF_RESTCONF:
333 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
334 proto_msg = 1;
335 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100336
Michal Vasko820efe82023-05-12 15:47:43 +0200337 /* set internal opts */
338 case LYD_TYPE_RPC_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200339 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200340 break;
341 case LYD_TYPE_NOTIF_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200342 int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200343 break;
344 case LYD_TYPE_REPLY_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200345 int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200346 break;
347 case LYD_TYPE_DATA_YANG:
348 LOGINT(ctx);
349 rc = LY_EINT;
350 goto cleanup;
351 }
352
353 /* parse a full protocol message */
354 if (proto_msg) {
355 if (format == LYD_XML) {
356 /* parse the NETCONF (or RESTCONF XML) message */
357 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
358 } else {
359 /* parse the RESTCONF message */
360 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
361 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100362 if (rc) {
363 if (envp) {
364 /* special situation when the envelopes were parsed successfully */
365 *tree = envp;
366 }
Michal Vasko73792a12022-05-10 09:28:45 +0200367 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100368 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100369
370 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100371 if (envp) {
372 /* special out param meaning */
373 *tree = envp;
374 } else {
375 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100376 }
377 if (op) {
378 *op = lydctx->op_node;
379 }
380 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100381 }
382
383 /* parse the data */
384 switch (format) {
385 case LYD_XML:
386 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200388 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100389 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200391 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100392 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200394 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100395 LOGARG(ctx, format);
396 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200397 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200398 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100399 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200400
Michal Vaskoe0665742021-02-11 11:08:44 +0100401 /* set out params correctly */
402 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100403 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100404 }
405 if (op) {
406 *op = lydctx->op_node;
407 }
408
409cleanup:
410 if (lydctx) {
411 lydctx->free(lydctx);
412 }
413 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200414 /* free all the parsed nodes */
415 if (parsed.count) {
416 i = parsed.count;
417 do {
418 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100419 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200420 } while (i);
421 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200422 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200423 *tree = NULL;
424 }
425 if (op) {
426 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100427 }
428 }
429 ly_set_erase(&parsed, NULL);
430 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200431}
Radek Krejci084289f2019-07-09 17:35:30 +0200432
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100433LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100434lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
435 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
436{
437 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
438
439 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
440}
441
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100442LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100443lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
444 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
445{
446 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
447
448 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
449
450 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
451}
452
Michal Vasko90932a92020-02-12 14:33:03 +0100453struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200454lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100455{
Michal Vaskob104f112020-07-17 09:54:54 +0200456 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100457 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200458 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100459 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100460
Michal Vaskob104f112020-07-17 09:54:54 +0200461 assert(new_node);
462
Michal Vasko9beceb82022-04-05 12:14:15 +0200463 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200464 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100465 return NULL;
466 }
467
Michal Vasko93b16062020-12-09 18:14:18 +0100468 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100469 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100470 getnext_opts = LYS_GETNEXT_OUTPUT;
471 }
472
Michal Vaskoa2756f02021-07-09 13:20:28 +0200473 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200474 /* find the anchor using hashes */
475 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100476 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200477 while (schema) {
478 /* keep trying to find the first existing instance of the closest following schema sibling,
479 * otherwise return NULL - inserting at the end */
480 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
481 break;
482 }
483
Michal Vasko93b16062020-12-09 18:14:18 +0100484 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200485 }
486 } else {
487 /* find the anchor without hashes */
488 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200489 sparent = lysc_data_parent(new_node->schema);
490 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200491 /* we are in top-level, skip all the data from preceding modules */
492 LY_LIST_FOR(match, match) {
493 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
494 break;
495 }
496 }
497 }
498
499 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100500 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200501 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200502 /* must be a top-level extension instance data, no anchor */
503 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200504 }
Michal Vaskob104f112020-07-17 09:54:54 +0200505
506 found = 0;
507 LY_LIST_FOR(match, match) {
508 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
509 /* we have found an opaque node, which must be at the end, so use it OR
510 * modules do not match, so we must have traversed all the data from new_node module (if any),
511 * we have found the first node of the next module, that is what we want */
512 break;
513 }
514
515 /* skip schema nodes until we find the instantiated one */
516 while (!found) {
517 if (new_node->schema == schema) {
518 /* we have found the schema of the new node, continue search to find the first
519 * data node with a different schema (after our schema) */
520 found = 1;
521 break;
522 }
523 if (match->schema == schema) {
524 /* current node (match) is a data node still before the new node, continue search in data */
525 break;
526 }
Michal Vasko9a463472023-08-08 09:43:33 +0200527
Michal Vasko93b16062020-12-09 18:14:18 +0100528 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200529 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200530 /* must be a top-level extension instance data, no anchor */
531 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200532 }
Michal Vaskob104f112020-07-17 09:54:54 +0200533 }
534
535 if (found && (match->schema != new_node->schema)) {
536 /* find the next node after we have found our node schema data instance */
537 break;
538 }
539 }
Michal Vasko90932a92020-02-12 14:33:03 +0100540 }
541
542 return match;
543}
544
Michal Vaskoc61dd062022-06-07 11:01:28 +0200545void
Michal Vaskof03ed032020-03-04 13:31:44 +0100546lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100547{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100548 struct lyd_node_inner *par;
549
Michal Vasko90932a92020-02-12 14:33:03 +0100550 assert(!node->next && (node->prev == node));
551
552 node->next = sibling->next;
553 node->prev = sibling;
554 sibling->next = node;
555 if (node->next) {
556 /* sibling had a succeeding node */
557 node->next->prev = node;
558 } else {
559 /* sibling was last, find first sibling and change its prev */
560 if (sibling->parent) {
561 sibling = sibling->parent->child;
562 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200563 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100564 }
565 sibling->prev = node;
566 }
567 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100568
Michal Vasko9f96a052020-03-10 09:41:45 +0100569 for (par = node->parent; par; par = par->parent) {
570 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
571 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100572 par->flags &= ~LYD_DEFAULT;
573 }
574 }
Michal Vasko90932a92020-02-12 14:33:03 +0100575}
576
Michal Vaskoc61dd062022-06-07 11:01:28 +0200577void
Michal Vaskof03ed032020-03-04 13:31:44 +0100578lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100579{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100580 struct lyd_node_inner *par;
581
Michal Vasko90932a92020-02-12 14:33:03 +0100582 assert(!node->next && (node->prev == node));
583
584 node->next = sibling;
585 /* covers situation of sibling being first */
586 node->prev = sibling->prev;
587 sibling->prev = node;
588 if (node->prev->next) {
589 /* sibling had a preceding node */
590 node->prev->next = node;
591 } else if (sibling->parent) {
592 /* sibling was first and we must also change parent child pointer */
593 sibling->parent->child = node;
594 }
595 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100596
Michal Vasko9f96a052020-03-10 09:41:45 +0100597 for (par = node->parent; par; par = par->parent) {
598 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
599 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100600 par->flags &= ~LYD_DEFAULT;
601 }
602 }
Michal Vasko90932a92020-02-12 14:33:03 +0100603}
604
605/**
Michal Vaskob104f112020-07-17 09:54:54 +0200606 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100607 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100608 * Handles inserting into NP containers and key-less lists.
609 *
Michal Vasko90932a92020-02-12 14:33:03 +0100610 * @param[in] parent Parent to insert into.
611 * @param[in] node Node to insert.
612 */
613static void
Michal Vaskob104f112020-07-17 09:54:54 +0200614lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100615{
616 struct lyd_node_inner *par;
617
Radek Krejcia1c1e542020-09-29 16:06:52 +0200618 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100619 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100620
621 par = (struct lyd_node_inner *)parent;
622
Michal Vaskob104f112020-07-17 09:54:54 +0200623 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100624 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100625
Michal Vaskod989ba02020-08-24 10:59:24 +0200626 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100627 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
628 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100629 par->flags &= ~LYD_DEFAULT;
630 }
631 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200632}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100633
Michal Vasko751cb4d2020-07-14 12:25:28 +0200634/**
635 * @brief Learn whether a list instance has all the keys.
636 *
637 * @param[in] list List instance to check.
638 * @return non-zero if all the keys were found,
639 * @return 0 otherwise.
640 */
641static int
642lyd_insert_has_keys(const struct lyd_node *list)
643{
644 const struct lyd_node *key;
645 const struct lysc_node *skey = NULL;
646
647 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200648 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200649 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
650 if (!key || (key->schema != skey)) {
651 /* key missing */
652 return 0;
653 }
654
655 key = key->next;
656 }
657
658 /* all keys found */
659 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100660}
661
aPiecek6cf1d162023-11-08 16:07:00 +0100662/**
663 * @brief Get the first subsequent data node that contains a different schema definition.
664 *
665 * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet.
666 * @param[in] node Node to be inserted.
667 * @return Subsequent data node with a different schema.
668 */
669static struct lyd_node *
670lyd_insert_node_find_anchor(struct lyd_node *first_sibling, struct lyd_node *node)
671{
672 struct lyd_node *anchor;
673
674 if (first_sibling && (first_sibling->flags & LYD_EXT)) {
675 return NULL;
676 }
677
678 /* find the anchor, so we can insert somewhere before it */
679 anchor = lyd_insert_get_next_anchor(first_sibling, node);
680 /* cannot insert data node after opaque nodes */
681 if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) {
682 anchor = first_sibling->prev;
683 while ((anchor != first_sibling) && !anchor->prev->schema) {
684 anchor = anchor->prev;
685 }
686 }
687
688 return anchor;
689}
690
691/**
aPiecek61880852024-01-11 14:10:29 +0100692 * @brief Insert @p node as the last node.
aPiecek6cf1d162023-11-08 16:07:00 +0100693 *
694 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek61880852024-01-11 14:10:29 +0100695 * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
aPiecek6cf1d162023-11-08 16:07:00 +0100696 * @param[in] node Individual node (without siblings) to insert.
aPiecek6cf1d162023-11-08 16:07:00 +0100697 */
698static void
aPiecek61880852024-01-11 14:10:29 +0100699lyd_insert_node_last(struct lyd_node *parent, struct lyd_node *first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100700{
aPiecek61880852024-01-11 14:10:29 +0100701 if (first_sibling) {
aPiecek2d8ce0b2024-01-15 15:47:23 +0100702#ifndef NDEBUG
703 if (lyds_is_supported(node) && (first_sibling->prev->schema == node->schema) &&
704 (lyds_compare_single(first_sibling->prev, node) > 0)) {
705 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted, inserted node should not be added to the end.",
706 node->schema->name);
707 }
708#endif
Michal Vaskob104f112020-07-17 09:54:54 +0200709 lyd_insert_after_node(first_sibling->prev, node);
710 } else if (parent) {
711 lyd_insert_only_child(parent, node);
Michal Vaskob104f112020-07-17 09:54:54 +0200712 }
aPiecek6cf1d162023-11-08 16:07:00 +0100713}
714
715/**
aPiecek61880852024-01-11 14:10:29 +0100716 * @brief Insert a node into parent/siblings.
aPiecek6cf1d162023-11-08 16:07:00 +0100717 *
718 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek61880852024-01-11 14:10:29 +0100719 * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
aPiecek6cf1d162023-11-08 16:07:00 +0100720 * @param[in] node Individual node (without siblings) to insert.
aPiecek6cf1d162023-11-08 16:07:00 +0100721 */
aPiecek61880852024-01-11 14:10:29 +0100722static void
723lyd_insert_node_ordby_schema(struct lyd_node *parent, struct lyd_node *first_sibling, struct lyd_node *node)
aPiecek6cf1d162023-11-08 16:07:00 +0100724{
aPiecek61880852024-01-11 14:10:29 +0100725 struct lyd_node *anchor;
aPiecek6cf1d162023-11-08 16:07:00 +0100726
aPiecek61880852024-01-11 14:10:29 +0100727 if ((anchor = lyd_insert_node_find_anchor(first_sibling, node))) {
aPiecek6cf1d162023-11-08 16:07:00 +0100728 lyd_insert_before_node(anchor, node);
729 } else {
aPiecek61880852024-01-11 14:10:29 +0100730 lyd_insert_node_last(parent, first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100731 }
aPiecek6cf1d162023-11-08 16:07:00 +0100732}
733
734void
735lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
736{
737 LY_ERR ret = LY_SUCCESS;
aPiecek61880852024-01-11 14:10:29 +0100738 struct lyd_node *first_sibling, *leader;
aPiecek6cf1d162023-11-08 16:07:00 +0100739
740 /* inserting list without its keys is not supported */
741 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
742 assert(!parent || !parent->schema ||
743 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
744
aPiecek61880852024-01-11 14:10:29 +0100745 if (!parent && first_sibling_p && (*first_sibling_p)) {
aPiecek6cf1d162023-11-08 16:07:00 +0100746 parent = lyd_parent(*first_sibling_p);
747 }
aPiecek61880852024-01-11 14:10:29 +0100748 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
aPiecek6cf1d162023-11-08 16:07:00 +0100749
aPiecek61880852024-01-11 14:10:29 +0100750 if (last) {
751 lyd_insert_node_last(parent, first_sibling, node);
752 } else if (lyds_is_supported(node) &&
753 (lyd_find_sibling_schema(first_sibling, node->schema, &leader) == LY_SUCCESS)) {
754 ret = lyds_insert(&leader, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100755 if (ret) {
756 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
757 * but insert the node anyway although the nodes will not be sorted.
758 */
759 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted.", node->schema->name);
aPiecek61880852024-01-11 14:10:29 +0100760 lyd_insert_node_ordby_schema(parent, first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100761 }
762 } else {
aPiecek61880852024-01-11 14:10:29 +0100763 lyd_insert_node_ordby_schema(parent, first_sibling, node);
aPiecek6cf1d162023-11-08 16:07:00 +0100764 }
Michal Vaskob104f112020-07-17 09:54:54 +0200765
766 /* insert into parent HT */
767 lyd_insert_hash(node);
768
769 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200770 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200771 lyd_hash(parent);
772
773 /* now we can insert even the list into its parent HT */
774 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100775 }
aPiecek61880852024-01-11 14:10:29 +0100776
777 if (first_sibling_p) {
778 *first_sibling_p = node->prev->next ? first_sibling : node;
779 }
Michal Vasko90932a92020-02-12 14:33:03 +0100780}
781
Michal Vasko717a4c32020-12-07 09:40:10 +0100782/**
aPiecekcada0d72024-01-11 15:04:12 +0100783 * @brief Check that @p node can be unlinked.
784 *
785 * @param[in] node Node to check
786 * @return LY_ERR value.
787 */
788static LY_ERR
789lyd_unlink_check(struct lyd_node *node)
790{
791 if (!node) {
792 return LY_SUCCESS;
793 }
794
795 if (lysc_is_key(node->schema) && node->parent) {
796 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
797 LYD_NAME(node));
798 return LY_EINVAL;
799 }
800
801 return LY_SUCCESS;
802}
803
804/**
805 * @brief Move schema instances before anchor or as the last.
806 *
807 * The nodes will remain sorted according to the schema.
808 *
809 * @param[in] first_sibling First sibling, destination.
810 * @param[in] node Starting node, all following nodes with the same schema will be moved.
811 * @param[out] next_p Next node that has a different schema or NULL.
812 * @return LY_ERR value.
813 */
814static LY_ERR
815lyd_move_nodes_ordby_schema(struct lyd_node *first_sibling, struct lyd_node *node, struct lyd_node **next_p)
816{
817 struct lyd_node *second, *anchor, *iter, *next, *dst, *src;
818
819 assert(first_sibling && !first_sibling->prev->next && node && next_p);
820
821 if ((anchor = lyd_insert_node_find_anchor(first_sibling, node))) {
822 /* move the first node to the correct place according to the schema */
823 LY_CHECK_RET(lyd_unlink_check(node));
824 second = node->next;
825 lyd_unlink_ignore_lyds(node);
826 lyd_insert_before_node(anchor, node);
827 lyd_insert_hash(node);
828 if (!second || (node->schema != second->schema)) {
829 /* no more nodes to move */
830 *next_p = second;
831 return LY_SUCCESS;
832 }
833 dst = node;
834 src = second;
835 } else {
836 /* just move all instances to the end */
837 dst = first_sibling->prev;
838 src = node;
839 }
840
841 /* move the rest of source instances after @p node */
842 LY_LIST_FOR_SAFE(src, next, iter) {
843 LY_CHECK_RET(lyd_unlink_check(iter));
844 if (iter->schema != src->schema) {
845 break;
846 }
847 lyd_unlink_ignore_lyds(iter);
848 lyd_insert_after_node(dst, iter);
849 lyd_insert_hash(iter);
850 dst = iter;
851 }
852 *next_p = iter;
853
854 return LY_SUCCESS;
855}
856
857/**
858 * @brief Move nodes regardless of schema.
859 *
860 * The destination for the move is NULL, or a childless parent.
861 *
862 * @param[in] parent Parent to insert into, NULL for top-level sibling.
863 * @param[in] first_src First sibling, all following nodes will be moved.
864 * @return LY_ERR value.
865 */
866static LY_ERR
867lyd_move_nodes_at_once(struct lyd_node *parent, struct lyd_node *first_src)
868{
869 struct lyd_node *start, *next, *iter;
870
871 assert(!lyd_child(parent) && first_src && !first_src->prev->next && !first_src->parent);
872
873 LY_CHECK_RET(lyd_unlink_check(first_src));
874
875 /* move the first node */
876 start = first_src->next;
877 if (parent) {
878 lyd_unlink_ignore_lyds(first_src);
879 lyd_insert_only_child(parent, first_src);
880 lyd_insert_hash(first_src);
881 } else {
882 lyd_unlink_ignore_lyds(first_src);
883 }
884
885 /* move the rest of the nodes */
886 LY_LIST_FOR_SAFE(start, next, iter) {
887 LY_CHECK_RET(lyd_unlink_check(iter));
888 lyd_unlink_ignore_lyds(iter);
889 lyd_insert_after_node(first_src->prev, iter);
890 lyd_insert_hash(iter);
891 }
892
893 return LY_SUCCESS;
894}
895
896/**
897 * @brief Move the nodes in parts according to the schema.
898 *
899 * @param[in] first_sibling First sibling, destination.
900 * @param[in] first_src First sibling, all following nodes will be moved.
901 * @return LY_ERR value.
902 */
903static LY_ERR
904lyd_move_nodes_by_schema(struct lyd_node *first_sibling, struct lyd_node *first_src)
905{
906 LY_ERR ret;
907 struct lyd_node *next, *iter, *leader;
908
909 assert(first_sibling && !first_sibling->prev->next && first_src && !first_src->prev->next && !first_src->parent);
910
911 for (iter = first_src; iter; iter = next) {
912 if (lyds_is_supported(iter) &&
913 (lyd_find_sibling_schema(first_sibling, iter->schema, &leader) == LY_SUCCESS)) {
914 ret = lyds_merge(&leader, iter, &next);
915 if (ret) {
916 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
917 * but insert the node anyway although the nodes will not be sorted.
918 */
919 LOGWRN(LYD_CTX(first_src), "Data in \"%s\" are not sorted.", leader->schema->name);
920 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_sibling, next, &next));
921 }
922 } else {
923 LY_CHECK_RET(lyd_move_nodes_ordby_schema(first_sibling, iter, &next));
924 }
925 first_sibling = iter->prev->next ? first_sibling : iter;
926 }
927
928 return LY_SUCCESS;
929}
930
931/**
932 * @brief Move a nodes into parent/siblings.
933 *
934 * @param[in] parent Parent to insert into, NULL for top-level sibling.
935 * @param[in] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
936 * @param[in] first_src First sibling, all following nodes will be moved.
937 * @return LY_ERR value.
938 */
939static LY_ERR
940lyd_move_nodes(struct lyd_node *parent, struct lyd_node *first_sibling, struct lyd_node *first_src)
941{
942 LY_ERR ret;
943
944 assert(first_src && (!first_sibling || !first_sibling->prev->next) && !first_src->prev->next);
945
946 first_sibling = first_sibling ? first_sibling : lyd_child(parent);
947 if (!first_sibling) {
948 ret = lyd_move_nodes_at_once(parent, first_src);
949 } else {
950 ret = lyd_move_nodes_by_schema(first_sibling, first_src);
951 }
952
953 return ret;
954}
955
956/**
Michal Vasko717a4c32020-12-07 09:40:10 +0100957 * @brief Check schema place of a node to be inserted.
958 *
959 * @param[in] parent Schema node of the parent data node.
960 * @param[in] sibling Schema node of a sibling data node.
961 * @param[in] schema Schema node if the data node to be inserted.
962 * @return LY_SUCCESS on success.
963 * @return LY_EINVAL if the place is invalid.
964 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100965static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100966lyd_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 +0100967{
968 const struct lysc_node *par2;
969
Michal Vasko62ed12d2020-05-21 10:08:25 +0200970 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100971 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
972 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100973
Michal Vasko717a4c32020-12-07 09:40:10 +0100974 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100975 /* opaque nodes can be inserted wherever */
976 return LY_SUCCESS;
977 }
978
Michal Vasko717a4c32020-12-07 09:40:10 +0100979 if (!parent) {
980 parent = lysc_data_parent(sibling);
981 }
982
Michal Vaskof03ed032020-03-04 13:31:44 +0100983 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200984 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100985
986 if (parent) {
987 /* inner node */
988 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200989 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200990 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100991 return LY_EINVAL;
992 }
993 } else {
994 /* top-level node */
995 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200996 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100997 return LY_EINVAL;
998 }
999 }
1000
1001 return LY_SUCCESS;
1002}
1003
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001004LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001005lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001006{
Michal Vasko0ab974d2021-02-24 13:18:26 +01001007 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001008 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001009
Michal Vasko717a4c32020-12-07 09:40:10 +01001010 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001011
aPiecekcada0d72024-01-11 15:04:12 +01001012 if (node->parent || node->prev->next || !node->next) {
1013 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001014 lyd_insert_node(parent, NULL, node, 0);
aPiecekcada0d72024-01-11 15:04:12 +01001015 } else {
1016 lyd_move_nodes(parent, NULL, node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001017 }
aPiecekcada0d72024-01-11 15:04:12 +01001018
Michal Vaskof03ed032020-03-04 13:31:44 +01001019 return LY_SUCCESS;
1020}
1021
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001022LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001023lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +01001024{
1025 struct lyd_node *iter;
1026
1027 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
1028 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1029
1030 if (first->schema && (first->schema->flags & LYS_KEY)) {
1031 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
1032 return LY_EINVAL;
1033 }
1034
1035 while (first) {
1036 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +01001037 lyd_unlink(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001038 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +01001039 first = iter;
1040 }
1041 return LY_SUCCESS;
1042}
1043
1044LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001045lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001046{
aPiecekcada0d72024-01-11 15:04:12 +01001047 struct lyd_node *first_sibling;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001048
Michal Vaskob104f112020-07-17 09:54:54 +02001049 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001050
Michal Vaskob104f112020-07-17 09:54:54 +02001051 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01001052 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001053 }
1054
aPiecekcada0d72024-01-11 15:04:12 +01001055 first_sibling = lyd_first_sibling(sibling);
1056 if (node->parent || node->prev->next || !node->next) {
1057 lyd_unlink_tree(node);
1058 lyd_insert_node(NULL, &first_sibling, node, 0);
1059 } else {
1060 lyd_move_nodes(NULL, first_sibling, node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001061 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001062
Michal Vaskob104f112020-07-17 09:54:54 +02001063 if (first) {
aPiecekcada0d72024-01-11 15:04:12 +01001064 *first = node->prev->next ? first_sibling : node;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001065 }
1066
1067 return LY_SUCCESS;
1068}
1069
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001070LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001071lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1072{
Michal Vaskobce230b2022-04-19 09:55:31 +02001073 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001074 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001075
Michal Vasko717a4c32020-12-07 09:40:10 +01001076 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001077
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001078 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001079 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001080 return LY_EINVAL;
1081 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001082 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1083 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001084 return LY_EINVAL;
1085 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001086
Michal Vasko2e784f82024-01-11 09:51:22 +01001087 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001088 lyd_insert_before_node(sibling, node);
1089 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001090
Michal Vaskof03ed032020-03-04 13:31:44 +01001091 return LY_SUCCESS;
1092}
1093
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001094LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001095lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1096{
Michal Vaskobce230b2022-04-19 09:55:31 +02001097 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001098 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001099
Michal Vasko717a4c32020-12-07 09:40:10 +01001100 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001101
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02001102 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001103 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001104 return LY_EINVAL;
1105 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +02001106 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
1107 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001108 return LY_EINVAL;
1109 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001110
Michal Vasko2e784f82024-01-11 09:51:22 +01001111 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001112 lyd_insert_after_node(sibling, node);
1113 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001114
Michal Vaskof03ed032020-03-04 13:31:44 +01001115 return LY_SUCCESS;
1116}
1117
Michal Vasko2e784f82024-01-11 09:51:22 +01001118void
aPiecekcada0d72024-01-11 15:04:12 +01001119lyd_unlink_ignore_lyds(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001120{
aPiecekcada0d72024-01-11 15:04:12 +01001121 struct lyd_node *iter;
aPiecek6cf1d162023-11-08 16:07:00 +01001122
Michal Vaskob104f112020-07-17 09:54:54 +02001123 /* update hashes while still linked into the tree */
1124 lyd_unlink_hash(node);
1125
stewegf9041a22024-01-18 13:29:12 +01001126 /* unlink leafref nodes */
1127 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
1128 lyd_free_leafref_nodes((struct lyd_node_term *)node);
1129 }
1130
Michal Vaskof03ed032020-03-04 13:31:44 +01001131 /* unlink from siblings */
1132 if (node->prev->next) {
1133 node->prev->next = node->next;
1134 }
1135 if (node->next) {
1136 node->next->prev = node->prev;
1137 } else {
1138 /* unlinking the last node */
1139 if (node->parent) {
1140 iter = node->parent->child;
1141 } else {
1142 iter = node->prev;
1143 while (iter->prev != node) {
1144 iter = iter->prev;
1145 }
1146 }
1147 /* update the "last" pointer from the first node */
1148 iter->prev = node->prev;
1149 }
1150
1151 /* unlink from parent */
1152 if (node->parent) {
1153 if (node->parent->child == node) {
1154 /* the node is the first child */
1155 node->parent->child = node->next;
1156 }
1157
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001158 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +01001159 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001160
Michal Vaskof03ed032020-03-04 13:31:44 +01001161 node->parent = NULL;
1162 }
1163
1164 node->next = NULL;
1165 node->prev = node;
1166}
1167
aPiecekcada0d72024-01-11 15:04:12 +01001168void
1169lyd_unlink(struct lyd_node *node)
1170{
1171 struct lyd_node *leader;
1172
1173 if (!node) {
1174 return;
1175 }
1176
1177 /* unlink from the lyds tree */
1178 if (lyds_is_supported(node)) {
1179 if (!node->prev->next || (node->prev->schema != node->schema)) {
1180 leader = node;
1181 } else {
1182 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
1183 assert(leader);
1184 }
1185 lyds_unlink(&leader, node);
1186 }
1187
1188 /* unlink data tree */
1189 lyd_unlink_ignore_lyds(node);
1190}
1191
Michal Vasko2e784f82024-01-11 09:51:22 +01001192LIBYANG_API_DEF void
1193lyd_unlink_siblings(struct lyd_node *node)
1194{
aPiecekcada0d72024-01-11 15:04:12 +01001195 struct lyd_node *next, *iter, *leader, *start;
Michal Vasko2e784f82024-01-11 09:51:22 +01001196
aPiecekcada0d72024-01-11 15:04:12 +01001197 if (lyds_is_supported(node) && node->prev->next && (node->prev->schema == node->schema)) {
1198 /* unlink starts at the non-first item in the (leaf-)list */
1199 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
1200 lyds_split(leader, node, &start);
1201 } else {
1202 /* unlink @p node */
1203 LY_CHECK_RET(lyd_unlink_check(node), );
1204 start = node->next;
1205 lyd_unlink_ignore_lyds(node);
1206 }
Michal Vasko2e784f82024-01-11 09:51:22 +01001207
aPiecekcada0d72024-01-11 15:04:12 +01001208 /* continue unlinking the rest */
1209 LY_LIST_FOR_SAFE(start, next, iter) {
1210 LY_CHECK_RET(lyd_unlink_check(iter), );
1211 lyd_unlink_ignore_lyds(iter);
1212 lyd_insert_after_node(node->prev, iter);
1213 lyd_insert_hash(iter);
Michal Vasko2e784f82024-01-11 09:51:22 +01001214 }
1215}
1216
1217LIBYANG_API_DEF void
1218lyd_unlink_tree(struct lyd_node *node)
1219{
aPiecekcada0d72024-01-11 15:04:12 +01001220 LY_CHECK_RET(lyd_unlink_check(node), );
Michal Vasko2e784f82024-01-11 09:51:22 +01001221 lyd_unlink(node);
1222}
1223
Michal Vaskoa5da3292020-08-12 13:10:50 +02001224void
Michal Vasko871a0252020-11-11 18:35:24 +01001225lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02001226{
1227 struct lyd_meta *last, *iter;
1228
1229 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001230
1231 if (!meta) {
1232 return;
1233 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001234
1235 for (iter = meta; iter; iter = iter->next) {
1236 iter->parent = parent;
1237 }
1238
1239 /* insert as the last attribute */
1240 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001241 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001242 last->next = meta;
1243 } else {
1244 parent->meta = meta;
1245 }
1246
1247 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001248 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001249 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001250 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001251 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001252}
1253
aPiecek0e92afc2023-11-08 10:48:02 +01001254void
1255lyd_unlink_meta_single(struct lyd_meta *meta)
1256{
1257 struct lyd_meta *iter;
1258
1259 if (!meta) {
1260 return;
1261 }
1262
1263 if (meta->parent && (meta->parent->meta == meta)) {
1264 meta->parent->meta = meta->next;
1265 } else if (meta->parent) {
1266 for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {}
1267 if (iter->next) {
1268 iter->next = meta->next;
1269 }
1270 }
1271
1272 meta->next = NULL;
1273 meta->parent = NULL;
1274}
1275
aPiecek0b1df642023-11-06 16:15:33 +01001276/**
1277 * @brief Get the annotation definition in the module.
1278 *
1279 * @param[in] mod Metadata module (with the annotation definition).
1280 * @param[in] name Attribute name.
1281 * @param[in] name_len Length of @p name, must be set correctly.
1282 * @return compiled YANG extension instance on success.
1283 */
1284static struct lysc_ext_instance *
1285lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len)
1286{
1287 LY_ARRAY_COUNT_TYPE u;
1288 struct lyplg_ext *plugin;
1289
1290 if (!mod) {
1291 return NULL;
1292 }
1293
1294 LY_ARRAY_FOR(mod->compiled->exts, u) {
1295 plugin = mod->compiled->exts[u].def->plugin;
1296 if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) &&
1297 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
1298 return &mod->compiled->exts[u];
1299 }
1300 }
1301
1302 return NULL;
1303}
1304
Radek Krejci1798aae2020-07-14 13:26:06 +02001305LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001306lyd_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 +02001307 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 +01001308 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 +01001309{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001310 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001311 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001312 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001313 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001314
Michal Vasko9f96a052020-03-10 09:41:45 +01001315 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001316
aPiecek0b1df642023-11-06 16:15:33 +01001317 ant = lyd_get_meta_annotation(mod, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001318 if (!ant) {
1319 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001320 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001321 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001322 ret = LY_EINVAL;
1323 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001324 }
1325
Michal Vasko9f96a052020-03-10 09:41:45 +01001326 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001327 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001328 mt->parent = parent;
1329 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001330 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001331 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 +01001332 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001333 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1334 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1335 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001336
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001337 /* insert as the last attribute */
1338 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001339 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001340 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001341 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001342 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001343 }
1344
Michal Vasko9f96a052020-03-10 09:41:45 +01001345 if (meta) {
1346 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001347 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001348
1349cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001350 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001351}
1352
Michal Vaskoa5da3292020-08-12 13:10:50 +02001353void
1354lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1355{
1356 struct lyd_attr *last, *iter;
1357 struct lyd_node_opaq *opaq;
1358
1359 assert(parent && !parent->schema);
1360
1361 if (!attr) {
1362 return;
1363 }
1364
1365 opaq = (struct lyd_node_opaq *)parent;
1366 for (iter = attr; iter; iter = iter->next) {
1367 iter->parent = opaq;
1368 }
1369
1370 /* insert as the last attribute */
1371 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001372 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001373 last->next = attr;
1374 } else {
1375 opaq->attr = attr;
1376 }
1377}
1378
Michal Vasko52927e22020-03-16 17:26:14 +01001379LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001380lyd_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 +01001381 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 +02001382 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 +01001383{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001384 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001385 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001386
1387 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001388 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001389
Michal Vasko2a3722d2021-06-16 11:52:39 +02001390 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001391 value = "";
1392 }
1393
1394 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001395 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001396
Michal Vaskoad92b672020-11-12 13:11:31 +01001397 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001398 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001399 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001400 }
1401 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001402 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001403 }
1404
Michal Vasko52927e22020-03-16 17:26:14 +01001405 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001406 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1407 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001408 *dynamic = 0;
1409 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001410 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001411 }
Michal Vasko501af032020-11-11 20:27:44 +01001412 at->format = format;
1413 at->val_prefix_data = val_prefix_data;
1414 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001415
1416 /* insert as the last attribute */
1417 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001418 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001419 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001420 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001421 last->next = at;
1422 }
1423
Radek Krejci011e4aa2020-09-04 15:22:31 +02001424finish:
1425 if (ret) {
1426 lyd_free_attr_single(ctx, at);
1427 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001428 *attr = at;
1429 }
1430 return LY_SUCCESS;
1431}
1432
aPiecek2f63f952021-03-30 12:22:18 +02001433/**
1434 * @brief Check the equality of the two schemas from different contexts.
1435 *
1436 * @param schema1 of first node.
1437 * @param schema2 of second node.
1438 * @return 1 if the schemas are equal otherwise 0.
1439 */
1440static ly_bool
1441lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1442{
1443 if (!schema1 && !schema2) {
1444 return 1;
1445 } else if (!schema1 || !schema2) {
1446 return 0;
1447 }
1448
1449 assert(schema1->module->ctx != schema2->module->ctx);
1450
1451 if (schema1->nodetype != schema2->nodetype) {
1452 return 0;
1453 }
1454
1455 if (strcmp(schema1->name, schema2->name)) {
1456 return 0;
1457 }
1458
1459 if (strcmp(schema1->module->name, schema2->module->name)) {
1460 return 0;
1461 }
1462
aPiecek2f63f952021-03-30 12:22:18 +02001463 return 1;
1464}
1465
1466/**
1467 * @brief Check the equality of the schemas for all parent nodes.
1468 *
1469 * Both nodes must be from different contexts.
1470 *
1471 * @param node1 Data of first node.
1472 * @param node2 Data of second node.
1473 * @return 1 if the all related parental schemas are equal otherwise 0.
1474 */
1475static ly_bool
1476lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1477{
1478 const struct lysc_node *parent1, *parent2;
1479
1480 assert(node1 && node2);
1481
1482 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1483 parent1 && parent2;
1484 parent1 = parent1->parent, parent2 = parent2->parent) {
1485 if (!lyd_compare_schema_equal(parent1, parent2)) {
1486 return 0;
1487 }
1488 }
1489
1490 if (parent1 || parent2) {
1491 return 0;
1492 }
1493
1494 return 1;
1495}
1496
1497/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001498 * @brief Compare 2 nodes values including opaque node values.
1499 *
1500 * @param[in] node1 First node to compare.
1501 * @param[in] node2 Second node to compare.
1502 * @return LY_SUCCESS if equal.
1503 * @return LY_ENOT if not equal.
1504 * @return LY_ERR on error.
1505 */
1506static LY_ERR
1507lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1508{
1509 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1510 const char *val1, *val2, *col;
1511 const struct lys_module *mod;
1512 char *val_dyn = NULL;
1513 LY_ERR rc = LY_SUCCESS;
1514
1515 if (!node1->schema) {
1516 opaq1 = (struct lyd_node_opaq *)node1;
1517 }
1518 if (!node2->schema) {
1519 opaq2 = (struct lyd_node_opaq *)node2;
1520 }
1521
1522 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1523 /* opaque XML and opaque XML node */
1524 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1525 opaq2->val_prefix_data)) {
1526 return LY_ENOT;
1527 }
1528 return LY_SUCCESS;
1529 }
1530
1531 /* get their values */
1532 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1533 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1534 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1535 if (!mod) {
1536 /* unable to compare */
1537 return LY_ENOT;
1538 }
1539
1540 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1541 LOGMEM(LYD_CTX(node1));
1542 return LY_EMEM;
1543 }
1544 val1 = val_dyn;
1545 } else {
1546 val1 = lyd_get_value(node1);
1547 }
1548 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1549 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1550 if (!mod) {
1551 return LY_ENOT;
1552 }
1553
1554 assert(!val_dyn);
1555 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1556 LOGMEM(LYD_CTX(node2));
1557 return LY_EMEM;
1558 }
1559 val2 = val_dyn;
1560 } else {
1561 val2 = lyd_get_value(node2);
1562 }
1563
1564 /* compare values */
1565 if (strcmp(val1, val2)) {
1566 rc = LY_ENOT;
1567 }
1568
1569 free(val_dyn);
1570 return rc;
1571}
1572
1573/**
Michal Vaskof277d362023-04-24 09:08:31 +02001574 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1575 *
1576 * Works correctly even if @p node1 and @p node2 have different contexts.
1577 *
1578 * @param[in] node1 The first node to compare.
1579 * @param[in] node2 The second node to compare.
1580 * @param[in] options Various @ref datacompareoptions.
1581 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1582 * @return LY_SUCCESS if the nodes are equivalent.
1583 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001584 */
1585static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001586lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001587 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001588{
aPiecek2f63f952021-03-30 12:22:18 +02001589 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1590 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001591 if (options & LYD_COMPARE_OPAQ) {
1592 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1593 return LY_ENOT;
1594 }
1595 } else {
1596 if (node1->schema != node2->schema) {
1597 return LY_ENOT;
1598 }
aPiecek2f63f952021-03-30 12:22:18 +02001599 }
1600 } else {
1601 /* different contexts */
1602 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1603 return LY_ENOT;
1604 }
1605 if (!parental_schemas_checked) {
1606 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1607 return LY_ENOT;
1608 }
1609 parental_schemas_checked = 1;
1610 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001611 }
1612
Michal Vaskof277d362023-04-24 09:08:31 +02001613 return LY_SUCCESS;
1614}
1615
1616/**
1617 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1618 *
1619 * Works correctly even if @p node1 and @p node2 have different contexts.
1620 *
1621 * @param[in] node1 The first node to compare.
1622 * @param[in] node2 The second node to compare.
1623 * @param[in] options Various @ref datacompareoptions.
1624 * @return LY_SUCCESS if the nodes are equivalent.
1625 * @return LY_ENOT if the nodes are not equivalent.
1626 */
1627static LY_ERR
1628lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1629{
1630 const struct lyd_node *iter1, *iter2;
1631 struct lyd_node_any *any1, *any2;
1632 int len1, len2;
1633 LY_ERR r;
1634
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001635 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001636 return LY_ENOT;
1637 }
aPiecek2f63f952021-03-30 12:22:18 +02001638 /* 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 +02001639
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001640 if (!node1->schema || !node2->schema) {
1641 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001642 return LY_ENOT;
1643 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001644 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1645 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1646 /* compare values only if there are any to compare */
1647 if ((r = lyd_compare_single_value(node1, node2))) {
1648 return r;
1649 }
Michal Vasko52927e22020-03-16 17:26:14 +01001650 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001651
Michal Vasko52927e22020-03-16 17:26:14 +01001652 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001653 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001654 }
1655 return LY_SUCCESS;
1656 } else {
1657 switch (node1->schema->nodetype) {
1658 case LYS_LEAF:
1659 case LYS_LEAFLIST:
1660 if (options & LYD_COMPARE_DEFAULTS) {
1661 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1662 return LY_ENOT;
1663 }
1664 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001665 if ((r = lyd_compare_single_value(node1, node2))) {
1666 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001667 }
1668
aPiecek2f63f952021-03-30 12:22:18 +02001669 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001670 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001671 case LYS_RPC:
1672 case LYS_ACTION:
1673 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001674 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001675 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001676 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001677 }
1678 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001679 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001680 iter1 = lyd_child(node1);
1681 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001682
Michal Vaskoee9b9482023-06-19 13:17:48 +02001683 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001684 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001685 } else if (node1->schema->flags & LYS_KEYLESS) {
1686 /* always equal */
1687 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001688 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001689
1690 /* lists with keys, their equivalence is based on their keys */
1691 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1692 key && (key->flags & LYS_KEY);
1693 key = key->next) {
1694 if (!iter1 || !iter2) {
1695 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1696 }
1697 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1698 LY_CHECK_RET(r);
1699 r = lyd_compare_single_data(iter1, iter2, options);
1700 LY_CHECK_RET(r);
1701
1702 iter1 = iter1->next;
1703 iter2 = iter2->next;
1704 }
1705
1706 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001707 case LYS_ANYXML:
1708 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001709 any1 = (struct lyd_node_any *)node1;
1710 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001711
1712 if (any1->value_type != any2->value_type) {
1713 return LY_ENOT;
1714 }
1715 switch (any1->value_type) {
1716 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001717 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001718 case LYD_ANYDATA_STRING:
1719 case LYD_ANYDATA_XML:
1720 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001721 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1722 return LY_ENOT;
1723 } else if (!any1->value.str && !any2->value.str) {
1724 return LY_SUCCESS;
1725 }
Michal Vasko52927e22020-03-16 17:26:14 +01001726 len1 = strlen(any1->value.str);
1727 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001728 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001729 return LY_ENOT;
1730 }
1731 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001732 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001733 len1 = lyd_lyb_data_length(any1->value.mem);
1734 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001735 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001736 return LY_ENOT;
1737 }
1738 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001739 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001740 }
1741 }
1742
Michal Vaskob7be7a82020-08-20 09:09:04 +02001743 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001744 return LY_EINT;
1745}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001746
Michal Vaskof277d362023-04-24 09:08:31 +02001747/**
1748 * @brief Compare all siblings at a node level.
1749 *
1750 * @param[in] node1 First sibling list.
1751 * @param[in] node2 Second sibling list.
1752 * @param[in] options Various @ref datacompareoptions.
1753 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1754 * @return LY_SUCCESS if equal.
1755 * @return LY_ENOT if not equal.
1756 * @return LY_ERR on error.
1757 */
1758static LY_ERR
1759lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1760 ly_bool parental_schemas_checked)
1761{
1762 LY_ERR r;
1763 const struct lyd_node *iter2;
1764
1765 while (node1 && node2) {
1766 /* schema match */
1767 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1768 LY_CHECK_RET(r);
1769
1770 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1771 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1772 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1773 /* find a matching instance in case they are ordered differently */
1774 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1775 if (r == LY_ENOTFOUND) {
1776 /* no matching instance, data not equal */
1777 r = LY_ENOT;
1778 }
1779 LY_CHECK_RET(r);
1780 } else {
1781 /* compare with the current node */
1782 iter2 = node2;
1783 }
1784
1785 /* data match */
1786 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1787 LY_CHECK_RET(r);
1788
1789 node1 = node1->next;
1790 node2 = node2->next;
1791 }
1792
1793 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1794}
1795
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001796LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001797lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1798{
Michal Vaskof277d362023-04-24 09:08:31 +02001799 LY_ERR r;
1800
1801 if (!node1 || !node2) {
1802 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1803 }
1804
1805 /* schema match */
1806 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1807 return r;
1808 }
1809
1810 /* data match */
1811 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001812}
1813
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001814LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001815lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001816{
Michal Vaskof277d362023-04-24 09:08:31 +02001817 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001818}
1819
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001820LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001821lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1822{
aPiecek0a6705b2023-11-14 14:20:58 +01001823 const struct ly_ctx *ctx;
1824
Michal Vasko21725742020-06-29 11:49:25 +02001825 if (!meta1 || !meta2) {
1826 if (meta1 == meta2) {
1827 return LY_SUCCESS;
1828 } else {
1829 return LY_ENOT;
1830 }
1831 }
1832
aPiecek0a6705b2023-11-14 14:20:58 +01001833 ctx = meta1->annotation->module->ctx;
1834 if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001835 return LY_ENOT;
1836 }
1837
aPiecek0a6705b2023-11-14 14:20:58 +01001838 return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value);
Michal Vasko21725742020-06-29 11:49:25 +02001839}
1840
Radek Krejci22ebdba2019-07-25 13:59:43 +02001841/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001842 * @brief Create a copy of the attribute.
1843 *
1844 * @param[in] attr Attribute to copy.
1845 * @param[in] node Opaque where to append the new attribute.
1846 * @param[out] dup Optional created attribute copy.
1847 * @return LY_ERR value.
1848 */
1849static LY_ERR
1850lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1851{
1852 LY_ERR ret = LY_SUCCESS;
1853 struct lyd_attr *a, *last;
1854 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1855
1856 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1857
1858 /* create a copy */
1859 a = calloc(1, sizeof *attr);
1860 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1861
1862 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1863 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1864 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1865 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1866 a->hints = attr->hints;
1867 a->format = attr->format;
1868 if (attr->val_prefix_data) {
1869 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1870 LY_CHECK_GOTO(ret, finish);
1871 }
1872
1873 /* insert as the last attribute */
1874 a->parent = opaq;
1875 if (opaq->attr) {
1876 for (last = opaq->attr; last->next; last = last->next) {}
1877 last->next = a;
1878 } else {
1879 opaq->attr = a;
1880 }
1881
1882finish:
1883 if (ret) {
1884 lyd_free_attr_single(LYD_CTX(node), a);
1885 } else if (dup) {
1886 *dup = a;
1887 }
1888 return LY_SUCCESS;
1889}
1890
1891/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001892 * @brief Find @p schema equivalent in @p trg_ctx.
1893 *
1894 * @param[in] schema Schema node to find.
1895 * @param[in] trg_ctx Target context to search in.
1896 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001897 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001898 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1899 * @return LY_RRR value.
1900 */
1901static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001902lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1903 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001904{
Michal Vasko9beceb82022-04-05 12:14:15 +02001905 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1906 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001907 char *path;
1908
1909 if (!schema) {
1910 /* opaque node */
1911 *trg_schema = NULL;
1912 return LY_SUCCESS;
1913 }
1914
Michal Vasko9beceb82022-04-05 12:14:15 +02001915 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001916 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001917 trg_parent = parent->schema;
1918 src_parent = lysc_data_parent(schema);
1919 }
1920
1921 do {
1922 /* find the next parent */
1923 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001924 while (lysc_data_parent(sp) != src_parent) {
1925 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001926 }
1927 src_parent = sp;
1928
1929 if (!src_parent->parent) {
1930 /* find the module first */
1931 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1932 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001933 if (log) {
1934 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1935 src_parent->module->name);
1936 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001937 return LY_ENOTFOUND;
1938 }
1939 }
1940
1941 /* find the next parent */
1942 assert(trg_parent || trg_mod);
1943 tp = NULL;
1944 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1945 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1946 break;
1947 }
1948 }
1949 if (!tp) {
1950 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001951 if (log) {
1952 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1953 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1954 free(path);
1955 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001956 return LY_ENOTFOUND;
1957 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001958
Michal Vasko9beceb82022-04-05 12:14:15 +02001959 trg_parent = tp;
1960 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001961
Michal Vasko9beceb82022-04-05 12:14:15 +02001962 /* success */
1963 *trg_schema = trg_parent;
1964 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001965}
1966
1967/**
Michal Vasko52927e22020-03-16 17:26:14 +01001968 * @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 +02001969 *
aPiecek55653c92023-11-09 13:43:19 +01001970 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001971 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001972 * @param[in] node Node to duplicate.
1973 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001974 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001975 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1976 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001977 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1978 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001979 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1980 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001981 */
Michal Vasko52927e22020-03-16 17:26:14 +01001982static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001983lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1984 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001985{
Michal Vasko52927e22020-03-16 17:26:14 +01001986 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001987 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001988 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001989 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001990 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001991 const struct lysc_type *type;
1992 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001993
Michal Vasko52927e22020-03-16 17:26:14 +01001994 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001995
Michal Vasko19175b62022-04-01 09:17:07 +02001996 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001997 if (options & LYD_DUP_NO_EXT) {
1998 /* no not duplicate this subtree */
1999 return LY_SUCCESS;
2000 }
2001
Michal Vasko19175b62022-04-01 09:17:07 +02002002 /* we need to use the same context */
2003 trg_ctx = LYD_CTX(node);
2004 }
2005
Michal Vasko52927e22020-03-16 17:26:14 +01002006 if (!node->schema) {
2007 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01002008 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01002009 } else {
2010 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002011 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002012 case LYS_ACTION:
2013 case LYS_NOTIF:
2014 case LYS_CONTAINER:
2015 case LYS_LIST:
2016 dup = calloc(1, sizeof(struct lyd_node_inner));
2017 break;
2018 case LYS_LEAF:
2019 case LYS_LEAFLIST:
2020 dup = calloc(1, sizeof(struct lyd_node_term));
2021 break;
2022 case LYS_ANYDATA:
2023 case LYS_ANYXML:
2024 dup = calloc(1, sizeof(struct lyd_node_any));
2025 break;
2026 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01002027 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01002028 ret = LY_EINT;
2029 goto error;
2030 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002031 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002032 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002033
Michal Vaskof6df0a02020-06-16 13:08:34 +02002034 if (options & LYD_DUP_WITH_FLAGS) {
2035 dup->flags = node->flags;
2036 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02002037 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02002038 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02002039 if (options & LYD_DUP_WITH_PRIV) {
2040 dup->priv = node->priv;
2041 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002042 if (trg_ctx == LYD_CTX(node)) {
2043 dup->schema = node->schema;
2044 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01002045 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02002046 if (ret) {
2047 /* has no schema but is not an opaque node */
2048 free(dup);
2049 dup = NULL;
2050 goto error;
2051 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002052 }
Michal Vasko52927e22020-03-16 17:26:14 +01002053 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002054
Michal Vasko9cf62422021-07-01 13:29:32 +02002055 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02002056 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02002057 if (!node->schema) {
2058 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
2059 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
2060 }
2061 } else {
2062 LY_LIST_FOR(node->meta, meta) {
aPiecek41680342023-11-08 10:19:44 +01002063 LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002064 }
Michal Vasko25a32822020-07-09 15:48:22 +02002065 }
2066 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002067
2068 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002069 if (!dup->schema) {
2070 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2071 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2072 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002073
2074 if (options & LYD_DUP_RECURSIVE) {
2075 /* duplicate all the children */
2076 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002077 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002078 }
2079 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002080 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
2081 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
2082 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
2083 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02002084 opaq->hints = orig->hints;
2085 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002086 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002087 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002088 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002089 }
Michal Vasko52927e22020-03-16 17:26:14 +01002090 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2091 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2092 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2093
2094 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01002095 if (trg_ctx == LYD_CTX(node)) {
2096 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
2097 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
2098 } else {
2099 /* store canonical value in the target context */
2100 val_can = lyd_get_value(node);
2101 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02002102 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 +01002103 LYD_HINT_DATA, term->schema, NULL);
2104 LY_CHECK_GOTO(ret, error);
2105 }
Michal Vasko52927e22020-03-16 17:26:14 +01002106 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2107 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2108 struct lyd_node *child;
2109
2110 if (options & LYD_DUP_RECURSIVE) {
2111 /* duplicate all the children */
2112 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002113 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002114 }
Michal Vasko69730152020-10-09 16:30:07 +02002115 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002116 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02002117 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002118 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002119 }
2120 }
2121 lyd_hash(dup);
2122 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002123 dup->hash = node->hash;
2124 any = (struct lyd_node_any *)node;
2125 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002126 }
2127
Michal Vasko52927e22020-03-16 17:26:14 +01002128 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02002129 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01002130
2131 if (dup_p) {
2132 *dup_p = dup;
2133 }
2134 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002135
2136error:
Michal Vasko52927e22020-03-16 17:26:14 +01002137 lyd_free_tree(dup);
2138 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002139}
2140
Michal Vasko29d674b2021-08-25 11:18:35 +02002141/**
2142 * @brief Get a parent node to connect duplicated subtree to.
2143 *
2144 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01002145 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02002146 * @param[in] parent Initial parent to connect to.
2147 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2148 * @param[out] dup_parent First duplicated parent node, if any.
2149 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
2150 * @return LY_ERR value.
2151 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002152static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002153lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
2154 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002155{
Michal Vasko58d89e92023-05-23 09:56:19 +02002156 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002157 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02002158 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002159
2160 *dup_parent = NULL;
2161 *local_parent = NULL;
2162
Michal Vasko83522192022-07-20 08:07:34 +02002163 if (node->flags & LYD_EXT) {
2164 ext_parent = 1;
2165 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002166 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02002167 if (ext_parent) {
2168 /* use the standard context */
2169 trg_ctx = LYD_CTX(orig_parent);
2170 }
Michal Vasko13c5b212023-02-14 10:03:01 +01002171 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002172 /* stop creating parents, connect what we have into the provided parent */
2173 iter = parent;
2174 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01002175 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
2176 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02002177 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01002178 iter = parent;
2179 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002180 } else {
2181 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02002182 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002183
2184 /* insert into the previous duplicated parent */
2185 if (*dup_parent) {
2186 lyd_insert_node(iter, NULL, *dup_parent, 0);
2187 }
2188
2189 /* update the last duplicated parent */
2190 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002191 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002192
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002193 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002194 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02002195 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002196 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002197
Michal Vasko83522192022-07-20 08:07:34 +02002198 if (orig_parent->flags & LYD_EXT) {
2199 ext_parent = 1;
2200 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002201 }
2202
2203 if (repeat && parent) {
2204 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02002205 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
2206 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002207 return LY_EINVAL;
2208 }
2209
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002210 if (*dup_parent && parent) {
2211 /* last insert into a prevously-existing parent */
2212 lyd_insert_node(parent, NULL, *dup_parent, 0);
2213 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002214 return LY_SUCCESS;
2215}
2216
2217static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002218lyd_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 +01002219 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002220{
2221 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002222 const struct lyd_node *orig; /* original node to be duplicated */
2223 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002224 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02002225 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002226
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002227 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002228
2229 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002230 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 +02002231 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002232 } else {
2233 local_parent = parent;
2234 }
2235
Radek Krejci22ebdba2019-07-25 13:59:43 +02002236 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01002237 if (lysc_is_key(orig->schema)) {
2238 if (local_parent) {
2239 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02002240 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01002241 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01002242 } else {
2243 assert(!(options & LYD_DUP_WITH_PARENTS));
2244 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01002245 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002246 LY_CHECK_GOTO(rc, error);
2247 }
2248 } else {
2249 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02002250 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002251 LY_CHECK_GOTO(rc, error);
2252 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002253 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002254 break;
2255 }
2256 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002257
Michal Vasko3a41dff2020-07-15 14:30:28 +02002258 if (dup) {
2259 *dup = first;
2260 }
2261 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002262
2263error:
2264 if (top) {
2265 lyd_free_tree(top);
2266 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002267 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002268 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002269 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002270}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002271
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002272/**
2273 * @brief Check the context of node and parent when duplicating nodes.
2274 *
2275 * @param[in] node Node to duplicate.
2276 * @param[in] parent Parent of the duplicated node(s).
2277 * @return LY_ERR value.
2278 */
2279static LY_ERR
2280lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2281{
2282 const struct lyd_node *iter;
2283
2284 if (!node || !parent) {
2285 return LY_SUCCESS;
2286 }
2287
2288 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2289 /* try to find top-level ext data parent */
2290 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2291
2292 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002293 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002294 return LY_EINVAL;
2295 }
2296 }
2297
2298 return LY_SUCCESS;
2299}
2300
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002301LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002302lyd_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 +02002303{
Michal Vaskoddd76592022-01-17 13:34:48 +01002304 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002305 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002306
Michal Vasko58d89e92023-05-23 09:56:19 +02002307 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002308}
2309
2310LIBYANG_API_DEF LY_ERR
2311lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2312 uint32_t options, struct lyd_node **dup)
2313{
2314 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2315
Michal Vasko58d89e92023-05-23 09:56:19 +02002316 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002317}
2318
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002319LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002320lyd_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 +02002321{
Michal Vaskoddd76592022-01-17 13:34:48 +01002322 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002323 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002324
Michal Vasko58d89e92023-05-23 09:56:19 +02002325 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002326}
2327
2328LIBYANG_API_DEF LY_ERR
2329lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2330 uint32_t options, struct lyd_node **dup)
2331{
2332 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2333
Michal Vasko58d89e92023-05-23 09:56:19 +02002334 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002335}
2336
aPiecek41680342023-11-08 10:19:44 +01002337LY_ERR
2338lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
2339 struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002340{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002341 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002342 struct lyd_meta *mt, *last;
aPiecek41680342023-11-08 10:19:44 +01002343 const struct lysc_type *ant_type;
2344 struct lys_module *mod;
2345 const char *val_can;
Michal Vasko25a32822020-07-09 15:48:22 +02002346
aPiecek41680342023-11-08 10:19:44 +01002347 LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL);
Michal Vasko33c48972022-07-20 10:28:07 +02002348
Michal Vasko25a32822020-07-09 15:48:22 +02002349 /* create a copy */
2350 mt = calloc(1, sizeof *mt);
aPiecek41680342023-11-08 10:19:44 +01002351 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM);
2352
2353 if (parent_ctx != meta->annotation->module->ctx) {
2354 /* different contexts */
2355 mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision);
2356
2357 /* annotation */
2358 mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name));
2359 lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
2360 LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL,
2361 "Annotation for metadata %s not found, value duplication failed.", meta->name), finish);
2362
2363 /* duplicate callback expect only the same contexts, so use the store callback */
2364 val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value);
2365 ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, NULL,
2366 LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL);
2367 } else {
2368 /* annotation */
2369 mt->annotation = meta->annotation;
2370 /* duplication of value */
2371 ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value);
2372 }
2373 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish);
2374 LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002375
2376 /* insert as the last attribute */
aPiecek41680342023-11-08 10:19:44 +01002377 mt->parent = parent;
2378 if (parent->meta) {
2379 for (last = parent->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002380 last->next = mt;
2381 } else {
aPiecek41680342023-11-08 10:19:44 +01002382 parent->meta = mt;
Michal Vasko25a32822020-07-09 15:48:22 +02002383 }
2384
Radek Krejci011e4aa2020-09-04 15:22:31 +02002385finish:
2386 if (ret) {
2387 lyd_free_meta_single(mt);
2388 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002389 *dup = mt;
2390 }
2391 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002392}
2393
aPiecek41680342023-11-08 10:19:44 +01002394LIBYANG_API_DEF LY_ERR
2395lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
2396{
2397 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
2398
2399 /* log to node context but value must always use the annotation context */
2400 return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup);
2401}
2402
Michal Vasko4490d312020-06-16 13:08:55 +02002403/**
2404 * @brief Merge a source sibling into target siblings.
2405 *
2406 * @param[in,out] first_trg First target sibling, is updated if top-level.
2407 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002408 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002409 * @param[in] merge_cb Optional merge callback.
2410 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002411 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002412 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002413 * @return LY_ERR value.
2414 */
2415static LY_ERR
2416lyd_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 +02002417 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002418{
Michal Vasko4490d312020-06-16 13:08:55 +02002419 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002420 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002421 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002422 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002423 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002424 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002425 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002426
2427 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002428 if (!sibling_src->schema) {
2429 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002430 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002431 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002432 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002433 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002434 } else {
2435 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002436 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002437 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002438 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002439
Michal Vaskocd3f6172021-05-18 16:14:50 +02002440 if (match_trg) {
2441 /* update match as needed */
2442 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2443 } else {
2444 /* first instance of this node */
2445 first_inst = 1;
2446 }
2447
2448 if (match_trg) {
2449 /* call callback */
2450 if (merge_cb) {
2451 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2452 }
2453
Michal Vasko4490d312020-06-16 13:08:55 +02002454 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002455 if (!match_trg->schema) {
2456 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2457 /* update value */
2458 opaq_trg = (struct lyd_node_opaq *)match_trg;
2459 opaq_src = (struct lyd_node_opaq *)sibling_src;
2460
2461 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2462 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2463 opaq_trg->hints = opaq_src->hints;
2464
2465 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2466 opaq_trg->format = opaq_src->format;
2467 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2468 &opaq_trg->val_prefix_data);
2469 }
2470 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2471 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002472 /* since they are different, they cannot both be default */
2473 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2474
Michal Vasko3a41dff2020-07-15 14:30:28 +02002475 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2476 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002477 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002478 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2479 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002480 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002481
2482 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002483 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002484 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002485 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002486 /* update value */
2487 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002488 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002489
2490 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002491 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002492 }
2493
2494 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002495 r = LY_SUCCESS;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002496 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko42c96e22024-01-18 08:18:06 +01002497 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 +02002498 &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002499 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002500 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002501 }
2502 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002503 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002504 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002505 } else {
2506 /* node not found, merge it */
2507 if (options & LYD_MERGE_DESTRUCT) {
2508 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002509 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002510 /* spend it */
2511 *sibling_src_p = NULL;
2512 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002513 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 +02002514 }
2515
Michal Vasko7e8315b2021-08-03 17:01:06 +02002516 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2517 /* set LYD_NEW for all the new nodes, required for validation */
2518 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2519 elem->flags |= LYD_NEW;
2520 LYD_TREE_DFS_END(dup_src, elem);
2521 }
Michal Vasko4490d312020-06-16 13:08:55 +02002522 }
2523
Michal Vaskocd3f6172021-05-18 16:14:50 +02002524 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002525 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002526
2527 if (first_inst) {
2528 /* remember not to find this instance next time */
2529 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2530 }
2531
2532 /* call callback, no source node */
2533 if (merge_cb) {
2534 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2535 }
Michal Vasko4490d312020-06-16 13:08:55 +02002536 }
2537
2538 return LY_SUCCESS;
2539}
2540
Michal Vasko3a41dff2020-07-15 14:30:28 +02002541static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002542lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2543 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002544{
2545 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002546 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002547 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002548 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002549
2550 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002551 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2552 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002553
2554 if (!source) {
2555 /* nothing to merge */
2556 return LY_SUCCESS;
2557 }
2558
Michal Vasko831422b2020-11-24 11:20:51 +01002559 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002560 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002561 return LY_EINVAL;
2562 }
2563
2564 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002565 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2566 /* skip data nodes from different modules */
2567 continue;
2568 }
2569
Radek Krejci857189e2020-09-01 13:26:36 +02002570 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002571 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2572 if (ret) {
2573 break;
2574 }
Michal Vasko4490d312020-06-16 13:08:55 +02002575 if (first && !sibling_src) {
2576 /* source was spent (unlinked), move to the next node */
2577 source = tmp;
2578 }
2579
Michal Vasko3a41dff2020-07-15 14:30:28 +02002580 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002581 break;
2582 }
2583 }
2584
2585 if (options & LYD_MERGE_DESTRUCT) {
2586 /* free any leftover source data that were not merged */
2587 lyd_free_siblings((struct lyd_node *)source);
2588 }
2589
Michal Vaskocd3f6172021-05-18 16:14:50 +02002590 lyd_dup_inst_free(dup_inst);
2591 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002592}
2593
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002594LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002595lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002596{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002597 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002598}
2599
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002600LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002601lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002602{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002603 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2604}
2605
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002606LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002607lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2608 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2609{
2610 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002611}
2612
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002613static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002614lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002615{
Michal Vasko14654712020-02-06 08:35:21 +01002616 /* ending \0 */
2617 ++reqlen;
2618
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002619 if (reqlen > *buflen) {
2620 if (is_static) {
2621 return LY_EINCOMPLETE;
2622 }
2623
2624 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2625 if (!*buffer) {
2626 return LY_EMEM;
2627 }
2628
2629 *buflen = reqlen;
2630 }
2631
2632 return LY_SUCCESS;
2633}
2634
Michal Vaskod59035b2020-07-08 12:00:06 +02002635LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002636lyd_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 +02002637{
2638 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002639 size_t len;
2640 const char *val;
2641 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002642
Michal Vasko824d0832021-11-04 15:36:51 +01002643 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002644 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002645 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002646 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002647
2648 quot = '\'';
2649 if (strchr(val, '\'')) {
2650 quot = '"';
2651 }
2652 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002653 }
2654
2655 return LY_SUCCESS;
2656}
2657
2658/**
2659 * @brief Append leaf-list value predicate to path.
2660 *
2661 * @param[in] node Node to print.
2662 * @param[in,out] buffer Buffer to print to.
2663 * @param[in,out] buflen Current buffer length.
2664 * @param[in,out] bufused Current number of characters used in @p buffer.
2665 * @param[in] is_static Whether buffer is static or can be reallocated.
2666 * @return LY_ERR
2667 */
2668static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002669lyd_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 +02002670{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002671 size_t len;
2672 const char *val;
2673 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002674
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002675 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002676 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002677 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002678
2679 quot = '\'';
2680 if (strchr(val, '\'')) {
2681 quot = '"';
2682 }
2683 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2684
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002685 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002686}
2687
2688/**
2689 * @brief Append node position (relative to its other instances) predicate to path.
2690 *
2691 * @param[in] node Node to print.
2692 * @param[in,out] buffer Buffer to print to.
2693 * @param[in,out] buflen Current buffer length.
2694 * @param[in,out] bufused Current number of characters used in @p buffer.
2695 * @param[in] is_static Whether buffer is static or can be reallocated.
2696 * @return LY_ERR
2697 */
2698static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002699lyd_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 +02002700{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002701 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002702 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002703 char *val = NULL;
2704 LY_ERR rc;
2705
Michal Vasko50cc0562021-05-18 16:15:43 +02002706 pos = lyd_list_pos(node);
2707 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002708 return LY_EMEM;
2709 }
2710
2711 len = 1 + strlen(val) + 1;
2712 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2713 if (rc != LY_SUCCESS) {
2714 goto cleanup;
2715 }
2716
2717 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2718
2719cleanup:
2720 free(val);
2721 return rc;
2722}
2723
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002724LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002725lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2726{
Radek Krejci857189e2020-09-01 13:26:36 +02002727 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002728 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002729 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002730 const struct lyd_node *iter, *parent;
2731 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002732 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002733
2734 LY_CHECK_ARG_RET(NULL, node, NULL);
2735 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002736 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002737 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002738 } else {
2739 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002740 }
2741
2742 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002743 case LYD_PATH_STD:
2744 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002745 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002746 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002747 ++depth;
2748 }
2749
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002750 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002751 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002752 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002753 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002754iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002755 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002756 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002757 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002758 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002759 if (prev_mod == mod) {
2760 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002761 }
2762
2763 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002764 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2765 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002766 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2767 if (rc != LY_SUCCESS) {
2768 break;
2769 }
2770
2771 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002772 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002773
Michal Vasko790b2bc2020-08-03 13:35:06 +02002774 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002775 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002776 switch (iter->schema->nodetype) {
2777 case LYS_LIST:
2778 if (iter->schema->flags & LYS_KEYLESS) {
2779 /* print its position */
2780 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2781 } else {
2782 /* print all list keys in predicates */
2783 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2784 }
2785 break;
2786 case LYS_LEAFLIST:
2787 if (iter->schema->flags & LYS_CONFIG_W) {
2788 /* print leaf-list value */
2789 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2790 } else {
2791 /* print its position */
2792 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2793 }
2794 break;
2795 default:
2796 /* nothing to print more */
2797 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002798 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002799 }
2800 if (rc != LY_SUCCESS) {
2801 break;
2802 }
2803
Michal Vasko14654712020-02-06 08:35:21 +01002804 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002805 }
2806 break;
2807 }
2808
2809 return buffer;
2810}
Michal Vaskoe444f752020-02-10 12:20:06 +01002811
Michal Vaskodbf3e652022-10-21 08:46:25 +02002812char *
2813lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2814{
2815 uint32_t depth;
2816 size_t bufused = 0, buflen = 0, len;
2817 char *buffer = NULL;
2818 const struct lyd_node *iter, *parent;
2819 const struct lys_module *mod, *prev_mod;
2820 LY_ERR rc = LY_SUCCESS;
2821
2822 switch (pathtype) {
2823 case LYD_PATH_STD:
2824 case LYD_PATH_STD_NO_LAST_PRED:
2825 for (depth = 1; depth <= dnodes->count; ++depth) {
2826 /* current node */
2827 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002828 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002829
2830 /* parent */
2831 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002832 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2833 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002834 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2835 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002836
2837 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002838 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002839 if (prev_mod == mod) {
2840 mod = NULL;
2841 }
2842
2843 /* realloc string */
2844 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2845 strlen(((struct lyd_node_opaq *)iter)->name.name));
2846 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2847 break;
2848 }
2849
2850 /* print next node */
2851 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2852
2853 /* do not always print the last (first) predicate */
2854 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2855 switch (iter->schema->nodetype) {
2856 case LYS_LIST:
2857 if (iter->schema->flags & LYS_KEYLESS) {
2858 /* print its position */
2859 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2860 } else {
2861 /* print all list keys in predicates */
2862 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2863 }
2864 break;
2865 case LYS_LEAFLIST:
2866 if (iter->schema->flags & LYS_CONFIG_W) {
2867 /* print leaf-list value */
2868 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2869 } else {
2870 /* print its position */
2871 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2872 }
2873 break;
2874 default:
2875 /* nothing to print more */
2876 break;
2877 }
2878 }
2879 if (rc) {
2880 break;
2881 }
2882 }
2883 break;
2884 }
2885
2886 return buffer;
2887}
2888
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002889LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002890lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2891{
2892 struct lyd_meta *ret = NULL;
2893 const struct ly_ctx *ctx;
2894 const char *prefix, *tmp;
2895 char *str;
2896 size_t pref_len, name_len;
2897
2898 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002899 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002900
2901 if (!first) {
2902 return NULL;
2903 }
2904
2905 ctx = first->annotation->module->ctx;
2906
2907 /* parse the name */
2908 tmp = name;
2909 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2910 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2911 return NULL;
2912 }
2913
2914 /* find the module */
2915 if (prefix) {
2916 str = strndup(prefix, pref_len);
2917 module = ly_ctx_get_module_latest(ctx, str);
2918 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002919 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 +02002920 }
2921
2922 /* find the metadata */
2923 LY_LIST_FOR(first, first) {
2924 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2925 ret = (struct lyd_meta *)first;
2926 break;
2927 }
2928 }
2929
2930 return ret;
2931}
2932
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002933LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002934lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2935{
Michal Vasko9beceb82022-04-05 12:14:15 +02002936 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002937 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002938 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002939
Michal Vaskof03ed032020-03-04 13:31:44 +01002940 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002941 if (!siblings) {
2942 /* no data */
2943 if (match) {
2944 *match = NULL;
2945 }
2946 return LY_ENOTFOUND;
2947 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002948
Michal Vasko9beceb82022-04-05 12:14:15 +02002949 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2950 /* create a duplicate in this context */
2951 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2952 target = dup;
2953 }
2954
2955 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2956 /* schema mismatch */
2957 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002958 if (match) {
2959 *match = NULL;
2960 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002961 return LY_ENOTFOUND;
2962 }
2963
Michal Vaskoe78faec2021-04-08 17:24:43 +02002964 /* get first sibling */
2965 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002966
Michal Vasko9e685082021-01-29 14:49:09 +01002967 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002968 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002969 assert(target->hash);
2970
Michal Vaskoe78faec2021-04-08 17:24:43 +02002971 if (lysc_is_dup_inst_list(target->schema)) {
2972 /* we must search the instances from beginning to find the first matching one */
2973 found = 0;
2974 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002975 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002976 found = 1;
2977 break;
2978 }
2979 }
2980 if (found) {
2981 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002982 } else {
2983 siblings = NULL;
2984 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002985 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002986 /* find by hash */
2987 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2988 siblings = *match_p;
2989 } else {
2990 /* not found */
2991 siblings = NULL;
2992 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002993 }
2994 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002995 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002996 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002997 if (lysc_is_dup_inst_list(target->schema)) {
2998 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2999 break;
3000 }
3001 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02003002 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003003 break;
3004 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003005 }
3006 }
3007 }
3008
Michal Vasko9beceb82022-04-05 12:14:15 +02003009 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003010 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003011 if (match) {
3012 *match = NULL;
3013 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003014 return LY_ENOTFOUND;
3015 }
3016
Michal Vasko9b368d32020-02-14 13:53:31 +01003017 if (match) {
3018 *match = (struct lyd_node *)siblings;
3019 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003020 return LY_SUCCESS;
3021}
3022
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003023LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003024lyd_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 +02003025 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003026{
3027 LY_ERR rc;
3028 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02003029 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01003030
Michal Vasko4c583e82020-07-17 12:16:14 +02003031 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02003032 if (!siblings) {
3033 /* no data */
3034 if (match) {
3035 *match = NULL;
3036 }
3037 return LY_ENOTFOUND;
3038 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003039
Michal Vasko9beceb82022-04-05 12:14:15 +02003040 if ((LYD_CTX(siblings) != schema->module->ctx)) {
3041 /* parent of ext nodes is useless */
3042 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01003043 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
3044 /* no schema node in siblings so certainly no data node either */
3045 if (match) {
3046 *match = NULL;
3047 }
3048 return LY_ENOTFOUND;
3049 }
Michal Vasko9beceb82022-04-05 12:14:15 +02003050 }
3051
3052 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3053 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003054 if (match) {
3055 *match = NULL;
3056 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003057 return LY_ENOTFOUND;
3058 }
3059
Michal Vaskof03ed032020-03-04 13:31:44 +01003060 if (key_or_value && !val_len) {
3061 val_len = strlen(key_or_value);
3062 }
3063
Michal Vaskob104f112020-07-17 09:54:54 +02003064 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3065 /* create a data node and find the instance */
3066 if (schema->nodetype == LYS_LEAFLIST) {
3067 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02003068 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 +02003069 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003070 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003071 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003072 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003073 }
3074
3075 /* find it */
3076 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003077 } else {
3078 /* find the first schema node instance */
3079 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003080 }
3081
Michal Vaskoe444f752020-02-10 12:20:06 +01003082 lyd_free_tree(target);
3083 return rc;
3084}
Michal Vaskoccc02342020-05-21 10:09:21 +02003085
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003086LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02003087lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3088{
3089 struct lyd_node **match_p, *first, *iter;
3090 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02003091 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003092
Michal Vasko83ae7772022-06-08 10:01:55 +02003093 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003094 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02003095
3096 LY_CHECK_RET(ly_set_new(set));
3097
3098 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
3099 /* no data or schema mismatch */
3100 return LY_ENOTFOUND;
3101 }
3102
Michal Vaskoee9b9482023-06-19 13:17:48 +02003103 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02003104 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02003105
Michal Vaskoe78faec2021-04-08 17:24:43 +02003106 /* get first sibling */
3107 siblings = lyd_first_sibling(siblings);
3108
3109 parent = siblings->parent;
3110 if (parent && parent->schema && parent->children_ht) {
3111 assert(target->hash);
3112
3113 /* find the first instance */
3114 lyd_find_sibling_first(siblings, target, &first);
3115 if (first) {
3116 /* add it so that it is the first in the set */
3117 if (ly_set_add(*set, first, 1, NULL)) {
3118 goto error;
3119 }
3120
3121 /* find by hash */
3122 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3123 iter = *match_p;
3124 } else {
3125 /* not found */
3126 iter = NULL;
3127 }
3128 while (iter) {
3129 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02003130 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003131 goto error;
3132 }
3133
3134 /* find next instance */
3135 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
3136 iter = NULL;
3137 } else {
3138 iter = *match_p;
3139 }
3140 }
3141 }
3142 } else {
3143 /* no children hash table */
3144 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003145 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003146 ly_set_add(*set, (void *)siblings, 1, NULL);
3147 }
3148 }
3149 }
3150
3151 if (!(*set)->count) {
3152 return LY_ENOTFOUND;
3153 }
3154 return LY_SUCCESS;
3155
3156error:
3157 ly_set_free(*set, NULL);
3158 *set = NULL;
3159 return LY_EMEM;
3160}
3161
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003162LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003163lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3164{
3165 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3166
Michal Vaskoe271a312023-08-15 11:46:30 +02003167 if (first && first->schema) {
3168 first = first->prev;
3169 if (first->schema) {
3170 /* no opaque nodes */
3171 first = NULL;
3172 } else {
3173 /* opaque nodes are at the end, find quickly the first */
3174 while (!first->prev->schema) {
3175 first = first->prev;
3176 }
3177 }
3178 }
3179
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003180 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02003181 assert(!first->schema);
3182 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003183 break;
3184 }
3185 }
3186
3187 if (match) {
3188 *match = (struct lyd_node *)first;
3189 }
3190 return first ? LY_SUCCESS : LY_ENOTFOUND;
3191}
3192
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003193LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003194lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02003195{
Michal Vaskod96e2372023-02-24 16:07:51 +01003196 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003197
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003198 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01003199}
Michal Vaskoccc02342020-05-21 10:09:21 +02003200
Michal Vaskod96e2372023-02-24 16:07:51 +01003201LIBYANG_API_DEF LY_ERR
3202lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
3203{
3204 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003205
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003206 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003207}
Radek Krejcica989142020-11-05 11:32:22 +01003208
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003209LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003210lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
Michal Vaskod96e2372023-02-24 16:07:51 +01003211 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01003212{
Michal Vaskod96e2372023-02-24 16:07:51 +01003213 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01003214
Michal Vaskod96e2372023-02-24 16:07:51 +01003215 *set = NULL;
3216
3217 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 +01003218}
3219
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003220LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003221lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003222{
Michal Vaskod96e2372023-02-24 16:07:51 +01003223 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02003224}
3225
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003226LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02003227lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
3228{
3229 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
3230}
3231
3232LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003233lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
3234 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003235{
Michal Vaskod96e2372023-02-24 16:07:51 +01003236 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
3237}
3238
3239LIBYANG_API_DEF LY_ERR
3240lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
3241 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
3242 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
3243{
3244 LY_ERR ret = LY_SUCCESS;
3245 struct lyxp_set xp_set = {0};
3246 struct lyxp_expr *exp = NULL;
3247 uint32_t i;
3248
3249 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
3250 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
3251 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
3252
3253 /* parse expression */
3254 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
3255 LY_CHECK_GOTO(ret, cleanup);
3256
3257 /* evaluate expression */
3258 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
3259 LYXP_IGNORE_WHEN);
3260 LY_CHECK_GOTO(ret, cleanup);
3261
3262 /* return expected result type without or with casting */
3263 if (node_set) {
3264 /* node set */
3265 if (xp_set.type == LYXP_SET_NODE_SET) {
3266 /* transform into a set */
3267 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
3268 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
3269 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
3270 (*node_set)->size = xp_set.used;
3271 for (i = 0; i < xp_set.used; ++i) {
3272 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3273 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
3274 LY_CHECK_GOTO(ret, cleanup);
3275 }
3276 }
3277 if (ret_type) {
3278 *ret_type = LY_XPATH_NODE_SET;
3279 }
3280 } else if (!string && !number && !boolean) {
3281 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
3282 ret = LY_EINVAL;
3283 goto cleanup;
3284 }
3285 }
3286
3287 if (string) {
3288 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
3289 /* cast into string */
3290 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3291 }
3292 if (xp_set.type == LYXP_SET_STRING) {
3293 /* string */
3294 *string = xp_set.val.str;
3295 xp_set.val.str = NULL;
3296 if (ret_type) {
3297 *ret_type = LY_XPATH_STRING;
3298 }
3299 }
3300 }
3301
3302 if (number) {
3303 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3304 /* cast into number */
3305 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3306 }
3307 if (xp_set.type == LYXP_SET_NUMBER) {
3308 /* number */
3309 *number = xp_set.val.num;
3310 if (ret_type) {
3311 *ret_type = LY_XPATH_NUMBER;
3312 }
3313 }
3314 }
3315
3316 if (boolean) {
3317 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3318 /* cast into boolean */
3319 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3320 }
3321 if (xp_set.type == LYXP_SET_BOOLEAN) {
3322 /* boolean */
3323 *boolean = xp_set.val.bln;
3324 if (ret_type) {
3325 *ret_type = LY_XPATH_BOOLEAN;
3326 }
3327 }
3328 }
3329
3330cleanup:
3331 lyxp_set_free_content(&xp_set);
3332 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3333 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003334}
3335
Michal Vasko99a77882024-01-04 14:50:51 +01003336/**
3337 * @brief Hash table node equal callback.
3338 */
3339static ly_bool
3340lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3341{
3342 struct lyd_node *node1, *node2;
3343
3344 node1 = *(struct lyd_node **)val1_p;
3345 node2 = *(struct lyd_node **)val2_p;
3346
3347 return node1 == node2;
3348}
3349
3350LIBYANG_API_DEF LY_ERR
3351lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3352{
3353 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003354 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003355 struct lyxp_set xp_set = {0};
3356 struct lyxp_expr *exp = NULL;
3357 struct lyd_node *node, *parent;
3358 struct lyxp_set_hash_node hnode;
3359 struct ly_ht *parent_ht = NULL;
3360 struct ly_set free_set = {0};
3361 uint32_t i, hash;
3362 ly_bool is_result;
3363
3364 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3365
3366 if (!*tree) {
3367 /* nothing to do */
3368 goto cleanup;
3369 }
3370
3371 *tree = lyd_first_sibling(*tree);
3372 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3373
3374 /* parse expression */
3375 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3376 LY_CHECK_GOTO(ret, cleanup);
3377
3378 /* evaluate expression */
3379 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3380 LY_CHECK_GOTO(ret, cleanup);
3381
3382 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003383 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003384 LY_CHECK_GOTO(!parent_ht, cleanup);
3385
3386 for (i = 0; i < xp_set.used; ++i) {
3387 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3388 /* ignore */
3389 continue;
3390 }
3391
3392 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3393 /* add the parent into parent_ht */
3394 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3395 if (ret == LY_EEXIST) {
3396 /* shared parent, we are done */
3397 break;
3398 }
3399 LY_CHECK_GOTO(ret, cleanup);
3400 }
3401 }
3402
3403 hnode.type = LYXP_NODE_ELEM;
3404 LY_LIST_FOR(*tree, parent) {
3405 LYD_TREE_DFS_BEGIN(parent, node) {
3406 if (lysc_is_key(node->schema)) {
3407 /* ignore */
3408 goto next_iter;
3409 }
3410
3411 /* check the results */
3412 is_result = 0;
3413 if (xp_set.ht) {
3414 hnode.node = node;
3415 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3416 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3417 hash = lyht_hash_multi(hash, NULL, 0);
3418
3419 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3420 is_result = 1;
3421 }
3422 } else {
3423 /* not enough elements for a hash table */
3424 for (i = 0; i < xp_set.used; ++i) {
3425 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3426 /* ignore */
3427 continue;
3428 }
3429
3430 if (xp_set.val.nodes[i].node == node) {
3431 is_result = 1;
3432 break;
3433 }
3434 }
3435 }
3436
3437 if (is_result) {
3438 /* keep the whole subtree if the node is in the results */
3439 LYD_TREE_DFS_continue = 1;
3440 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3441 /* free the whole subtree if the node is not even among the selected parents */
3442 ret = ly_set_add(&free_set, node, 1, NULL);
3443 LY_CHECK_GOTO(ret, cleanup);
3444 LYD_TREE_DFS_continue = 1;
3445 } /* else keep the parent node because a subtree is in the results */
3446
3447next_iter:
3448 LYD_TREE_DFS_END(parent, node);
3449 }
3450 }
3451
3452 /* free */
3453 for (i = 0; i < free_set.count; ++i) {
3454 node = free_set.dnodes[i];
3455 if (*tree == node) {
3456 *tree = (*tree)->next;
3457 }
3458 lyd_free_tree(node);
3459 }
3460
3461cleanup:
3462 lyxp_set_free_content(&xp_set);
3463 lyxp_expr_free(ctx, exp);
3464 lyht_free(parent_ht, NULL);
3465 ly_set_erase(&free_set, NULL);
3466 return ret;
3467}
3468
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003469LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003470lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3471{
3472 LY_ERR ret = LY_SUCCESS;
3473 struct lyxp_expr *expr = NULL;
3474 struct ly_path *lypath = NULL;
3475
3476 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3477
3478 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003479 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 +01003480 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003481 LY_CHECK_GOTO(ret, cleanup);
3482
3483 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003484 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003485 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 +01003486 LY_CHECK_GOTO(ret, cleanup);
3487
3488 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003489 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003490
3491cleanup:
3492 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3493 ly_path_free(LYD_CTX(ctx_node), lypath);
3494 return ret;
3495}
3496
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003497LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003498lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3499{
3500 LY_ERR ret;
3501 struct lyd_node *m;
3502
3503 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3504
Michal Vasko90189962023-02-28 12:10:34 +01003505 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003506 if (ret) {
3507 if (match) {
3508 *match = NULL;
3509 }
3510 return LY_ENOTFOUND;
3511 }
3512
3513 if (match) {
3514 *match = m;
3515 }
3516 return LY_SUCCESS;
3517}
Irfand3b351a2023-09-14 14:52:15 +02003518
stewegf9041a22024-01-18 13:29:12 +01003519LY_ERR
3520lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3521{
3522 struct ly_ht *ht;
3523 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003524 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003525
3526 assert(node);
3527 assert(record);
3528
Michal Vaskob46061d2024-01-18 13:58:13 +01003529 *record = NULL;
3530
stewegf9041a22024-01-18 13:29:12 +01003531 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003532 return LY_EDENIED;
3533 }
3534
3535 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003536 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003537 hash = lyht_hash((const char *)&node, sizeof node);
3538
stewegf9041a22024-01-18 13:29:12 +01003539 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3540 if (create) {
3541 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3542 } else {
stewegf9041a22024-01-18 13:29:12 +01003543 return LY_ENOTFOUND;
3544 }
3545 }
3546
3547 return LY_SUCCESS;
3548}
3549
3550LIBYANG_API_DEF LY_ERR
3551lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3552{
3553 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3554
3555 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3556}
3557
3558LY_ERR
3559lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3560{
stewegf9041a22024-01-18 13:29:12 +01003561 const struct lyd_node_term **item = NULL;
3562 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003563 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003564
3565 assert(node);
3566 assert(leafref_node);
3567
3568 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3569 return LY_EDENIED;
3570 }
3571
steweg67388952024-01-25 12:14:50 +01003572 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003573 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3574 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3575 if (rec->leafref_nodes[u] == leafref_node) {
3576 return LY_SUCCESS;
3577 }
3578 }
3579
3580 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3581 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003582
3583 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003584 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003585 LY_ARRAY_FOR(rec->target_nodes, u) {
3586 if (rec->target_nodes[u] == node) {
3587 return LY_SUCCESS;
3588 }
3589 }
3590
3591 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3592 *item = node;
3593
stewegf9041a22024-01-18 13:29:12 +01003594 return LY_SUCCESS;
3595}
3596
3597LIBYANG_API_DEF LY_ERR
3598lyd_leafref_link_node_tree(const struct lyd_node *tree)
3599{
3600 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003601 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003602 char *errmsg;
3603 struct lyd_node_term *leafref_node;
3604 struct lysc_node_leaf *leaf_schema;
3605 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003606 LY_ERR ret = LY_SUCCESS;
3607 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003608
3609 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3610
3611 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3612 return LY_EDENIED;
3613 }
3614
3615 LY_LIST_FOR(tree, sibling) {
3616 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003617 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003618 leafref_node = (struct lyd_node_term *)elem;
3619 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003620
stewegf9041a22024-01-18 13:29:12 +01003621 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3622 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003623 ly_set_free(targets, NULL);
3624 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003625 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003626 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003627 } else {
3628 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003629 for (i = 0; i < targets->count; ++i) {
3630 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3631 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3632 LY_CHECK_GOTO(ret, cleanup);
3633 }
3634 }
stewegf9041a22024-01-18 13:29:12 +01003635 }
3636 }
3637 }
3638 LYD_TREE_DFS_END(sibling, elem);
3639 }
3640 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003641
steweg67388952024-01-25 12:14:50 +01003642cleanup:
3643 ly_set_free(targets, NULL);
3644 return ret;
stewegf9041a22024-01-18 13:29:12 +01003645}
3646
3647LY_ERR
3648lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3649{
3650 LY_ERR ret;
3651 struct lyd_leafref_links_rec *rec;
3652
3653 assert(node);
3654 assert(leafref_node);
3655
3656 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3657 return LY_EDENIED;
3658 }
3659
steweg67388952024-01-25 12:14:50 +01003660 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003661 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3662 if (ret == LY_SUCCESS) {
3663 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003664 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003665 lyd_free_leafref_nodes(node);
3666 }
3667 } else if (ret != LY_ENOTFOUND) {
3668 return ret;
3669 }
3670
steweg67388952024-01-25 12:14:50 +01003671 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003672 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3673 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003674 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3675 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003676 lyd_free_leafref_nodes(leafref_node);
3677 }
3678 } else if (ret != LY_ENOTFOUND) {
3679 return ret;
3680 }
3681
3682 return LY_SUCCESS;
3683}