blob: bfe27dd01544d1ee96bdbd0b3828b6d9e97c10be [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/**
692 * @brief Insert a node into parent/siblings.
693 *
694 * @param[in] parent Parent to insert into, NULL for top-level sibling.
695 * @param[in,out] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
696 * @param[in] node Individual node (without siblings) to insert.
697 * @param[in] last If set, do not search for the correct anchor but always insert at the end.
698 */
699static void
700lyd_insert_node_ordby_schema(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node,
701 ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +0100702{
Michal Vaskob104f112020-07-17 09:54:54 +0200703 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100704
Michal Vaskob104f112020-07-17 09:54:54 +0200705 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100706 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
aPiecek6cf1d162023-11-08 16:07:00 +0100707 anchor = !last ? lyd_insert_node_find_anchor(first_sibling, node) : NULL;
Michal Vasko6ee6f432021-07-16 09:49:14 +0200708
Michal Vaskob104f112020-07-17 09:54:54 +0200709 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200710 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200711 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100712 if (!parent && (*first_sibling_p == anchor)) {
713 /* move first sibling */
714 *first_sibling_p = node;
715 }
Michal Vaskob104f112020-07-17 09:54:54 +0200716 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200717 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200718 lyd_insert_after_node(first_sibling->prev, node);
719 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200720 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200721 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100722 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200723 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200724 *first_sibling_p = node;
725 }
aPiecek6cf1d162023-11-08 16:07:00 +0100726}
727
728/**
729 * @brief Insert a node into parent/siblings. It is sorted using sort callbacks in the plugin types and lyds tree.
730 *
731 * The lyds tree is a Binary search tree that allows to sort instances of the same leaf-list or list.
732 * This Binary search tree is always found in the first instance of the (leaf-)list in its metadata.
733 *
734 * @param[in] parent Parent to insert into, NULL for top-level sibling.
735 * @param[in,out] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
736 * @param[in] node Individual node (without siblings) to insert.
737 * @return LY_ERR value.
738 */
739static LY_ERR
740lyd_insert_node_ordby_lyds(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
741{
742 LY_ERR ret = LY_SUCCESS;
743 struct lyd_node *anchor, *first_sibling, *leader;
744
745 /* get first sibling */
746 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
747
748 if (!parent && !first_sibling) {
749 /* insert as the only sibling */
750 *first_sibling_p = node;
751 /* node is alone, no lyds tree is created */
752 } else if (parent && !first_sibling) {
753 /* insert as the only child */
754 ret = lyds_create_metadata(node);
755 LY_CHECK_RET(ret);
756 lyd_insert_only_child(parent, node);
757 } else if (lyd_find_sibling_schema(first_sibling, node->schema, &leader) == LY_SUCCESS) {
758 /* Found the first instance of (leaf-)list, let's mark it as 'leader'. */
759 /* ensure that leader has meta set (use-case for top-level nodes) */
760 ret = lyds_create_metadata(leader);
761 LY_CHECK_RET(ret);
762 /* insert @p node to the BST tree and put him in line */
763 ret = lyds_insert(&leader, node);
764 LY_CHECK_RET(ret);
765 } else if ((anchor = lyd_insert_node_find_anchor(first_sibling, node))) {
766 /* insert before the anchor */
767 ret = lyds_create_metadata(node);
768 LY_CHECK_RET(ret);
769 lyd_insert_before_node(anchor, node);
770 } else {
771 /* insert as the last node, there is probably no other option */
772 ret = lyds_create_metadata(node);
773 LY_CHECK_RET(ret);
774 lyd_insert_after_node(first_sibling->prev, node);
775 }
776
777 if (first_sibling_p && !node->prev->next) {
778 /* move first sibling */
779 *first_sibling_p = node;
780 }
781
782 return LY_SUCCESS;
783}
784
785void
786lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
787{
788 LY_ERR ret = LY_SUCCESS;
789
790 /* inserting list without its keys is not supported */
791 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
792 assert(!parent || !parent->schema ||
793 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
794
795 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
796 parent = lyd_parent(*first_sibling_p);
797 }
798
799 if (lyds_is_supported(node)) {
800 ret = lyd_insert_node_ordby_lyds(parent, first_sibling_p, node);
801 if (ret) {
802 /* The operation on the sorting tree unexpectedly failed due to some internal issue,
803 * but insert the node anyway although the nodes will not be sorted.
804 */
805 LOGWRN(LYD_CTX(node), "Data in \"%s\" are not sorted.", node->schema->name);
806 lyd_insert_node_ordby_schema(parent, first_sibling_p, node, last);
807 }
808 } else {
809 lyd_insert_node_ordby_schema(parent, first_sibling_p, node, last);
810 }
Michal Vaskob104f112020-07-17 09:54:54 +0200811
812 /* insert into parent HT */
813 lyd_insert_hash(node);
814
815 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200816 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200817 lyd_hash(parent);
818
819 /* now we can insert even the list into its parent HT */
820 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100821 }
Michal Vasko90932a92020-02-12 14:33:03 +0100822}
823
Michal Vasko717a4c32020-12-07 09:40:10 +0100824/**
825 * @brief Check schema place of a node to be inserted.
826 *
827 * @param[in] parent Schema node of the parent data node.
828 * @param[in] sibling Schema node of a sibling data node.
829 * @param[in] schema Schema node if the data node to be inserted.
830 * @return LY_SUCCESS on success.
831 * @return LY_EINVAL if the place is invalid.
832 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100833static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100834lyd_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 +0100835{
836 const struct lysc_node *par2;
837
Michal Vasko62ed12d2020-05-21 10:08:25 +0200838 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100839 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
840 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100841
Michal Vasko717a4c32020-12-07 09:40:10 +0100842 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100843 /* opaque nodes can be inserted wherever */
844 return LY_SUCCESS;
845 }
846
Michal Vasko717a4c32020-12-07 09:40:10 +0100847 if (!parent) {
848 parent = lysc_data_parent(sibling);
849 }
850
Michal Vaskof03ed032020-03-04 13:31:44 +0100851 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200852 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100853
854 if (parent) {
855 /* inner node */
856 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200857 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200858 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100859 return LY_EINVAL;
860 }
861 } else {
862 /* top-level node */
863 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200864 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100865 return LY_EINVAL;
866 }
867 }
868
869 return LY_SUCCESS;
870}
871
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100872LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200873lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100874{
875 struct lyd_node *iter;
876
Michal Vasko0ab974d2021-02-24 13:18:26 +0100877 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100878 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100879
Michal Vasko717a4c32020-12-07 09:40:10 +0100880 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100881
Michal Vasko0ab974d2021-02-24 13:18:26 +0100882 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100883 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100884 return LY_EINVAL;
885 }
886
887 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100888 lyd_unlink(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100889 }
890
891 while (node) {
892 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100893 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200894 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100895 node = iter;
896 }
897 return LY_SUCCESS;
898}
899
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100900LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200901lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100902{
903 struct lyd_node *iter;
904
905 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
906 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
907
908 if (first->schema && (first->schema->flags & LYS_KEY)) {
909 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
910 return LY_EINVAL;
911 }
912
913 while (first) {
914 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100915 lyd_unlink(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100916 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100917 first = iter;
918 }
919 return LY_SUCCESS;
920}
921
922LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200923lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100924{
925 struct lyd_node *iter;
926
Michal Vaskob104f112020-07-17 09:54:54 +0200927 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100928
Michal Vaskob104f112020-07-17 09:54:54 +0200929 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100930 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100931 }
932
aPiecek32bd0d22023-11-16 14:47:04 +0100933 sibling = lyd_first_sibling(sibling);
Michal Vasko553d0b52020-12-04 16:27:52 +0100934
Michal Vaskob1b5c262020-03-05 14:29:47 +0100935 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100936 lyd_unlink(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100937 }
938
939 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100940 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200941 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200942 return LY_EINVAL;
943 }
944
Michal Vaskob1b5c262020-03-05 14:29:47 +0100945 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100946 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200947 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100948 node = iter;
949 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100950
Michal Vaskob104f112020-07-17 09:54:54 +0200951 if (first) {
952 /* find the first sibling */
953 *first = sibling;
954 while ((*first)->prev->next) {
955 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100956 }
957 }
958
959 return LY_SUCCESS;
960}
961
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100962LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100963lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
964{
Michal Vaskobce230b2022-04-19 09:55:31 +0200965 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100966 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100967
Michal Vasko717a4c32020-12-07 09:40:10 +0100968 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100969
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200970 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200971 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100972 return LY_EINVAL;
973 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200974 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
975 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100976 return LY_EINVAL;
977 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100978
Michal Vasko2e784f82024-01-11 09:51:22 +0100979 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100980 lyd_insert_before_node(sibling, node);
981 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100982
Michal Vaskof03ed032020-03-04 13:31:44 +0100983 return LY_SUCCESS;
984}
985
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100986LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100987lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
988{
Michal Vaskobce230b2022-04-19 09:55:31 +0200989 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100990 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100991
Michal Vasko717a4c32020-12-07 09:40:10 +0100992 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100993
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200994 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200995 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100996 return LY_EINVAL;
997 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200998 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
999 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +01001000 return LY_EINVAL;
1001 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001002
Michal Vasko2e784f82024-01-11 09:51:22 +01001003 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001004 lyd_insert_after_node(sibling, node);
1005 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001006
Michal Vaskof03ed032020-03-04 13:31:44 +01001007 return LY_SUCCESS;
1008}
1009
Michal Vasko2e784f82024-01-11 09:51:22 +01001010void
1011lyd_unlink(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001012{
aPiecek6cf1d162023-11-08 16:07:00 +01001013 struct lyd_node *iter, *leader;
Michal Vaskof03ed032020-03-04 13:31:44 +01001014
1015 if (!node) {
1016 return;
1017 }
1018
aPiecek6cf1d162023-11-08 16:07:00 +01001019 /* unlink from the lyds tree */
1020 if (lyds_is_supported(node)) {
1021 lyd_find_sibling_val(node, node->schema, NULL, 0, &leader);
1022 lyds_unlink(&leader, node);
1023 }
1024
Michal Vaskob104f112020-07-17 09:54:54 +02001025 /* update hashes while still linked into the tree */
1026 lyd_unlink_hash(node);
1027
stewegf9041a22024-01-18 13:29:12 +01001028 /* unlink leafref nodes */
1029 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
1030 lyd_free_leafref_nodes((struct lyd_node_term *)node);
1031 }
1032
Michal Vaskof03ed032020-03-04 13:31:44 +01001033 /* unlink from siblings */
1034 if (node->prev->next) {
1035 node->prev->next = node->next;
1036 }
1037 if (node->next) {
1038 node->next->prev = node->prev;
1039 } else {
1040 /* unlinking the last node */
1041 if (node->parent) {
1042 iter = node->parent->child;
1043 } else {
1044 iter = node->prev;
1045 while (iter->prev != node) {
1046 iter = iter->prev;
1047 }
1048 }
1049 /* update the "last" pointer from the first node */
1050 iter->prev = node->prev;
1051 }
1052
1053 /* unlink from parent */
1054 if (node->parent) {
1055 if (node->parent->child == node) {
1056 /* the node is the first child */
1057 node->parent->child = node->next;
1058 }
1059
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001060 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +01001061 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001062
Michal Vaskof03ed032020-03-04 13:31:44 +01001063 node->parent = NULL;
1064 }
1065
1066 node->next = NULL;
1067 node->prev = node;
1068}
1069
Michal Vasko2e784f82024-01-11 09:51:22 +01001070LIBYANG_API_DEF void
1071lyd_unlink_siblings(struct lyd_node *node)
1072{
1073 struct lyd_node *next, *elem, *first = NULL;
1074
1075 LY_LIST_FOR_SAFE(node, next, elem) {
1076 if (lysc_is_key(elem->schema) && elem->parent) {
1077 LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
1078 LYD_NAME(elem));
1079 return;
1080 }
1081
1082 lyd_unlink(elem);
1083 lyd_insert_node(NULL, &first, elem, 1);
1084 }
1085}
1086
1087LIBYANG_API_DEF void
1088lyd_unlink_tree(struct lyd_node *node)
1089{
1090 if (node && lysc_is_key(node->schema) && node->parent) {
1091 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
1092 LYD_NAME(node));
1093 return;
1094 }
1095
1096 lyd_unlink(node);
1097}
1098
Michal Vaskoa5da3292020-08-12 13:10:50 +02001099void
Michal Vasko871a0252020-11-11 18:35:24 +01001100lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02001101{
1102 struct lyd_meta *last, *iter;
1103
1104 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001105
1106 if (!meta) {
1107 return;
1108 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001109
1110 for (iter = meta; iter; iter = iter->next) {
1111 iter->parent = parent;
1112 }
1113
1114 /* insert as the last attribute */
1115 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001116 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001117 last->next = meta;
1118 } else {
1119 parent->meta = meta;
1120 }
1121
1122 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001123 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001124 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001125 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001126 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001127}
1128
aPiecek0e92afc2023-11-08 10:48:02 +01001129void
1130lyd_unlink_meta_single(struct lyd_meta *meta)
1131{
1132 struct lyd_meta *iter;
1133
1134 if (!meta) {
1135 return;
1136 }
1137
1138 if (meta->parent && (meta->parent->meta == meta)) {
1139 meta->parent->meta = meta->next;
1140 } else if (meta->parent) {
1141 for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {}
1142 if (iter->next) {
1143 iter->next = meta->next;
1144 }
1145 }
1146
1147 meta->next = NULL;
1148 meta->parent = NULL;
1149}
1150
aPiecek0b1df642023-11-06 16:15:33 +01001151/**
1152 * @brief Get the annotation definition in the module.
1153 *
1154 * @param[in] mod Metadata module (with the annotation definition).
1155 * @param[in] name Attribute name.
1156 * @param[in] name_len Length of @p name, must be set correctly.
1157 * @return compiled YANG extension instance on success.
1158 */
1159static struct lysc_ext_instance *
1160lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len)
1161{
1162 LY_ARRAY_COUNT_TYPE u;
1163 struct lyplg_ext *plugin;
1164
1165 if (!mod) {
1166 return NULL;
1167 }
1168
1169 LY_ARRAY_FOR(mod->compiled->exts, u) {
1170 plugin = mod->compiled->exts[u].def->plugin;
1171 if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) &&
1172 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
1173 return &mod->compiled->exts[u];
1174 }
1175 }
1176
1177 return NULL;
1178}
1179
Radek Krejci1798aae2020-07-14 13:26:06 +02001180LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001181lyd_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 +02001182 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 +01001183 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 +01001184{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001185 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001186 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001187 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001188 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001189
Michal Vasko9f96a052020-03-10 09:41:45 +01001190 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001191
aPiecek0b1df642023-11-06 16:15:33 +01001192 ant = lyd_get_meta_annotation(mod, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001193 if (!ant) {
1194 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001195 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001196 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001197 ret = LY_EINVAL;
1198 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001199 }
1200
Michal Vasko9f96a052020-03-10 09:41:45 +01001201 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001202 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001203 mt->parent = parent;
1204 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001205 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001206 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 +01001207 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001208 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1209 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1210 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001211
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001212 /* insert as the last attribute */
1213 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001214 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001215 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001216 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001217 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001218 }
1219
Michal Vasko9f96a052020-03-10 09:41:45 +01001220 if (meta) {
1221 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001222 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001223
1224cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001225 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001226}
1227
Michal Vaskoa5da3292020-08-12 13:10:50 +02001228void
1229lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1230{
1231 struct lyd_attr *last, *iter;
1232 struct lyd_node_opaq *opaq;
1233
1234 assert(parent && !parent->schema);
1235
1236 if (!attr) {
1237 return;
1238 }
1239
1240 opaq = (struct lyd_node_opaq *)parent;
1241 for (iter = attr; iter; iter = iter->next) {
1242 iter->parent = opaq;
1243 }
1244
1245 /* insert as the last attribute */
1246 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001247 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001248 last->next = attr;
1249 } else {
1250 opaq->attr = attr;
1251 }
1252}
1253
Michal Vasko52927e22020-03-16 17:26:14 +01001254LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001255lyd_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 +01001256 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 +02001257 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 +01001258{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001259 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001260 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001261
1262 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001263 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001264
Michal Vasko2a3722d2021-06-16 11:52:39 +02001265 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001266 value = "";
1267 }
1268
1269 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001270 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001271
Michal Vaskoad92b672020-11-12 13:11:31 +01001272 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001273 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001274 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001275 }
1276 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001277 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001278 }
1279
Michal Vasko52927e22020-03-16 17:26:14 +01001280 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001281 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1282 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001283 *dynamic = 0;
1284 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001285 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001286 }
Michal Vasko501af032020-11-11 20:27:44 +01001287 at->format = format;
1288 at->val_prefix_data = val_prefix_data;
1289 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001290
1291 /* insert as the last attribute */
1292 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001293 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001294 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001295 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001296 last->next = at;
1297 }
1298
Radek Krejci011e4aa2020-09-04 15:22:31 +02001299finish:
1300 if (ret) {
1301 lyd_free_attr_single(ctx, at);
1302 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001303 *attr = at;
1304 }
1305 return LY_SUCCESS;
1306}
1307
aPiecek2f63f952021-03-30 12:22:18 +02001308/**
1309 * @brief Check the equality of the two schemas from different contexts.
1310 *
1311 * @param schema1 of first node.
1312 * @param schema2 of second node.
1313 * @return 1 if the schemas are equal otherwise 0.
1314 */
1315static ly_bool
1316lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1317{
1318 if (!schema1 && !schema2) {
1319 return 1;
1320 } else if (!schema1 || !schema2) {
1321 return 0;
1322 }
1323
1324 assert(schema1->module->ctx != schema2->module->ctx);
1325
1326 if (schema1->nodetype != schema2->nodetype) {
1327 return 0;
1328 }
1329
1330 if (strcmp(schema1->name, schema2->name)) {
1331 return 0;
1332 }
1333
1334 if (strcmp(schema1->module->name, schema2->module->name)) {
1335 return 0;
1336 }
1337
aPiecek2f63f952021-03-30 12:22:18 +02001338 return 1;
1339}
1340
1341/**
1342 * @brief Check the equality of the schemas for all parent nodes.
1343 *
1344 * Both nodes must be from different contexts.
1345 *
1346 * @param node1 Data of first node.
1347 * @param node2 Data of second node.
1348 * @return 1 if the all related parental schemas are equal otherwise 0.
1349 */
1350static ly_bool
1351lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1352{
1353 const struct lysc_node *parent1, *parent2;
1354
1355 assert(node1 && node2);
1356
1357 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1358 parent1 && parent2;
1359 parent1 = parent1->parent, parent2 = parent2->parent) {
1360 if (!lyd_compare_schema_equal(parent1, parent2)) {
1361 return 0;
1362 }
1363 }
1364
1365 if (parent1 || parent2) {
1366 return 0;
1367 }
1368
1369 return 1;
1370}
1371
1372/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001373 * @brief Compare 2 nodes values including opaque node values.
1374 *
1375 * @param[in] node1 First node to compare.
1376 * @param[in] node2 Second node to compare.
1377 * @return LY_SUCCESS if equal.
1378 * @return LY_ENOT if not equal.
1379 * @return LY_ERR on error.
1380 */
1381static LY_ERR
1382lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1383{
1384 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1385 const char *val1, *val2, *col;
1386 const struct lys_module *mod;
1387 char *val_dyn = NULL;
1388 LY_ERR rc = LY_SUCCESS;
1389
1390 if (!node1->schema) {
1391 opaq1 = (struct lyd_node_opaq *)node1;
1392 }
1393 if (!node2->schema) {
1394 opaq2 = (struct lyd_node_opaq *)node2;
1395 }
1396
1397 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1398 /* opaque XML and opaque XML node */
1399 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1400 opaq2->val_prefix_data)) {
1401 return LY_ENOT;
1402 }
1403 return LY_SUCCESS;
1404 }
1405
1406 /* get their values */
1407 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1408 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1409 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1410 if (!mod) {
1411 /* unable to compare */
1412 return LY_ENOT;
1413 }
1414
1415 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1416 LOGMEM(LYD_CTX(node1));
1417 return LY_EMEM;
1418 }
1419 val1 = val_dyn;
1420 } else {
1421 val1 = lyd_get_value(node1);
1422 }
1423 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1424 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1425 if (!mod) {
1426 return LY_ENOT;
1427 }
1428
1429 assert(!val_dyn);
1430 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1431 LOGMEM(LYD_CTX(node2));
1432 return LY_EMEM;
1433 }
1434 val2 = val_dyn;
1435 } else {
1436 val2 = lyd_get_value(node2);
1437 }
1438
1439 /* compare values */
1440 if (strcmp(val1, val2)) {
1441 rc = LY_ENOT;
1442 }
1443
1444 free(val_dyn);
1445 return rc;
1446}
1447
1448/**
Michal Vaskof277d362023-04-24 09:08:31 +02001449 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1450 *
1451 * Works correctly even if @p node1 and @p node2 have different contexts.
1452 *
1453 * @param[in] node1 The first node to compare.
1454 * @param[in] node2 The second node to compare.
1455 * @param[in] options Various @ref datacompareoptions.
1456 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1457 * @return LY_SUCCESS if the nodes are equivalent.
1458 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001459 */
1460static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001461lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001462 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001463{
aPiecek2f63f952021-03-30 12:22:18 +02001464 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1465 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001466 if (options & LYD_COMPARE_OPAQ) {
1467 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1468 return LY_ENOT;
1469 }
1470 } else {
1471 if (node1->schema != node2->schema) {
1472 return LY_ENOT;
1473 }
aPiecek2f63f952021-03-30 12:22:18 +02001474 }
1475 } else {
1476 /* different contexts */
1477 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1478 return LY_ENOT;
1479 }
1480 if (!parental_schemas_checked) {
1481 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1482 return LY_ENOT;
1483 }
1484 parental_schemas_checked = 1;
1485 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001486 }
1487
Michal Vaskof277d362023-04-24 09:08:31 +02001488 return LY_SUCCESS;
1489}
1490
1491/**
1492 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1493 *
1494 * Works correctly even if @p node1 and @p node2 have different contexts.
1495 *
1496 * @param[in] node1 The first node to compare.
1497 * @param[in] node2 The second node to compare.
1498 * @param[in] options Various @ref datacompareoptions.
1499 * @return LY_SUCCESS if the nodes are equivalent.
1500 * @return LY_ENOT if the nodes are not equivalent.
1501 */
1502static LY_ERR
1503lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1504{
1505 const struct lyd_node *iter1, *iter2;
1506 struct lyd_node_any *any1, *any2;
1507 int len1, len2;
1508 LY_ERR r;
1509
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001510 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001511 return LY_ENOT;
1512 }
aPiecek2f63f952021-03-30 12:22:18 +02001513 /* 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 +02001514
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001515 if (!node1->schema || !node2->schema) {
1516 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001517 return LY_ENOT;
1518 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001519 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1520 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1521 /* compare values only if there are any to compare */
1522 if ((r = lyd_compare_single_value(node1, node2))) {
1523 return r;
1524 }
Michal Vasko52927e22020-03-16 17:26:14 +01001525 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001526
Michal Vasko52927e22020-03-16 17:26:14 +01001527 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001528 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001529 }
1530 return LY_SUCCESS;
1531 } else {
1532 switch (node1->schema->nodetype) {
1533 case LYS_LEAF:
1534 case LYS_LEAFLIST:
1535 if (options & LYD_COMPARE_DEFAULTS) {
1536 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1537 return LY_ENOT;
1538 }
1539 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001540 if ((r = lyd_compare_single_value(node1, node2))) {
1541 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001542 }
1543
aPiecek2f63f952021-03-30 12:22:18 +02001544 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001545 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001546 case LYS_RPC:
1547 case LYS_ACTION:
1548 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001549 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001550 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001551 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001552 }
1553 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001554 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001555 iter1 = lyd_child(node1);
1556 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001557
Michal Vaskoee9b9482023-06-19 13:17:48 +02001558 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001559 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001560 } else if (node1->schema->flags & LYS_KEYLESS) {
1561 /* always equal */
1562 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001563 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001564
1565 /* lists with keys, their equivalence is based on their keys */
1566 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1567 key && (key->flags & LYS_KEY);
1568 key = key->next) {
1569 if (!iter1 || !iter2) {
1570 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1571 }
1572 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1573 LY_CHECK_RET(r);
1574 r = lyd_compare_single_data(iter1, iter2, options);
1575 LY_CHECK_RET(r);
1576
1577 iter1 = iter1->next;
1578 iter2 = iter2->next;
1579 }
1580
1581 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001582 case LYS_ANYXML:
1583 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001584 any1 = (struct lyd_node_any *)node1;
1585 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001586
1587 if (any1->value_type != any2->value_type) {
1588 return LY_ENOT;
1589 }
1590 switch (any1->value_type) {
1591 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001592 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001593 case LYD_ANYDATA_STRING:
1594 case LYD_ANYDATA_XML:
1595 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001596 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1597 return LY_ENOT;
1598 } else if (!any1->value.str && !any2->value.str) {
1599 return LY_SUCCESS;
1600 }
Michal Vasko52927e22020-03-16 17:26:14 +01001601 len1 = strlen(any1->value.str);
1602 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001603 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001604 return LY_ENOT;
1605 }
1606 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001607 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001608 len1 = lyd_lyb_data_length(any1->value.mem);
1609 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001610 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001611 return LY_ENOT;
1612 }
1613 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001614 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001615 }
1616 }
1617
Michal Vaskob7be7a82020-08-20 09:09:04 +02001618 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001619 return LY_EINT;
1620}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001621
Michal Vaskof277d362023-04-24 09:08:31 +02001622/**
1623 * @brief Compare all siblings at a node level.
1624 *
1625 * @param[in] node1 First sibling list.
1626 * @param[in] node2 Second sibling list.
1627 * @param[in] options Various @ref datacompareoptions.
1628 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1629 * @return LY_SUCCESS if equal.
1630 * @return LY_ENOT if not equal.
1631 * @return LY_ERR on error.
1632 */
1633static LY_ERR
1634lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1635 ly_bool parental_schemas_checked)
1636{
1637 LY_ERR r;
1638 const struct lyd_node *iter2;
1639
1640 while (node1 && node2) {
1641 /* schema match */
1642 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1643 LY_CHECK_RET(r);
1644
1645 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1646 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1647 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1648 /* find a matching instance in case they are ordered differently */
1649 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1650 if (r == LY_ENOTFOUND) {
1651 /* no matching instance, data not equal */
1652 r = LY_ENOT;
1653 }
1654 LY_CHECK_RET(r);
1655 } else {
1656 /* compare with the current node */
1657 iter2 = node2;
1658 }
1659
1660 /* data match */
1661 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1662 LY_CHECK_RET(r);
1663
1664 node1 = node1->next;
1665 node2 = node2->next;
1666 }
1667
1668 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1669}
1670
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001671LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001672lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1673{
Michal Vaskof277d362023-04-24 09:08:31 +02001674 LY_ERR r;
1675
1676 if (!node1 || !node2) {
1677 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1678 }
1679
1680 /* schema match */
1681 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1682 return r;
1683 }
1684
1685 /* data match */
1686 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001687}
1688
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001689LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001690lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001691{
Michal Vaskof277d362023-04-24 09:08:31 +02001692 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001693}
1694
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001695LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001696lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1697{
aPiecek0a6705b2023-11-14 14:20:58 +01001698 const struct ly_ctx *ctx;
1699
Michal Vasko21725742020-06-29 11:49:25 +02001700 if (!meta1 || !meta2) {
1701 if (meta1 == meta2) {
1702 return LY_SUCCESS;
1703 } else {
1704 return LY_ENOT;
1705 }
1706 }
1707
aPiecek0a6705b2023-11-14 14:20:58 +01001708 ctx = meta1->annotation->module->ctx;
1709 if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001710 return LY_ENOT;
1711 }
1712
aPiecek0a6705b2023-11-14 14:20:58 +01001713 return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value);
Michal Vasko21725742020-06-29 11:49:25 +02001714}
1715
Radek Krejci22ebdba2019-07-25 13:59:43 +02001716/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001717 * @brief Create a copy of the attribute.
1718 *
1719 * @param[in] attr Attribute to copy.
1720 * @param[in] node Opaque where to append the new attribute.
1721 * @param[out] dup Optional created attribute copy.
1722 * @return LY_ERR value.
1723 */
1724static LY_ERR
1725lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1726{
1727 LY_ERR ret = LY_SUCCESS;
1728 struct lyd_attr *a, *last;
1729 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1730
1731 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1732
1733 /* create a copy */
1734 a = calloc(1, sizeof *attr);
1735 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1736
1737 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1738 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1739 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1740 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1741 a->hints = attr->hints;
1742 a->format = attr->format;
1743 if (attr->val_prefix_data) {
1744 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1745 LY_CHECK_GOTO(ret, finish);
1746 }
1747
1748 /* insert as the last attribute */
1749 a->parent = opaq;
1750 if (opaq->attr) {
1751 for (last = opaq->attr; last->next; last = last->next) {}
1752 last->next = a;
1753 } else {
1754 opaq->attr = a;
1755 }
1756
1757finish:
1758 if (ret) {
1759 lyd_free_attr_single(LYD_CTX(node), a);
1760 } else if (dup) {
1761 *dup = a;
1762 }
1763 return LY_SUCCESS;
1764}
1765
1766/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001767 * @brief Find @p schema equivalent in @p trg_ctx.
1768 *
1769 * @param[in] schema Schema node to find.
1770 * @param[in] trg_ctx Target context to search in.
1771 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001772 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001773 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1774 * @return LY_RRR value.
1775 */
1776static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001777lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1778 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001779{
Michal Vasko9beceb82022-04-05 12:14:15 +02001780 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1781 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001782 char *path;
1783
1784 if (!schema) {
1785 /* opaque node */
1786 *trg_schema = NULL;
1787 return LY_SUCCESS;
1788 }
1789
Michal Vasko9beceb82022-04-05 12:14:15 +02001790 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001791 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001792 trg_parent = parent->schema;
1793 src_parent = lysc_data_parent(schema);
1794 }
1795
1796 do {
1797 /* find the next parent */
1798 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001799 while (lysc_data_parent(sp) != src_parent) {
1800 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001801 }
1802 src_parent = sp;
1803
1804 if (!src_parent->parent) {
1805 /* find the module first */
1806 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1807 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001808 if (log) {
1809 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1810 src_parent->module->name);
1811 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001812 return LY_ENOTFOUND;
1813 }
1814 }
1815
1816 /* find the next parent */
1817 assert(trg_parent || trg_mod);
1818 tp = NULL;
1819 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1820 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1821 break;
1822 }
1823 }
1824 if (!tp) {
1825 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001826 if (log) {
1827 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1828 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1829 free(path);
1830 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001831 return LY_ENOTFOUND;
1832 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001833
Michal Vasko9beceb82022-04-05 12:14:15 +02001834 trg_parent = tp;
1835 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001836
Michal Vasko9beceb82022-04-05 12:14:15 +02001837 /* success */
1838 *trg_schema = trg_parent;
1839 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001840}
1841
1842/**
Michal Vasko52927e22020-03-16 17:26:14 +01001843 * @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 +02001844 *
aPiecek55653c92023-11-09 13:43:19 +01001845 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001846 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001847 * @param[in] node Node to duplicate.
1848 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001849 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001850 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1851 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001852 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1853 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001854 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1855 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001856 */
Michal Vasko52927e22020-03-16 17:26:14 +01001857static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001858lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1859 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001860{
Michal Vasko52927e22020-03-16 17:26:14 +01001861 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001862 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001863 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001864 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001865 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001866 const struct lysc_type *type;
1867 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001868
Michal Vasko52927e22020-03-16 17:26:14 +01001869 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001870
Michal Vasko19175b62022-04-01 09:17:07 +02001871 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001872 if (options & LYD_DUP_NO_EXT) {
1873 /* no not duplicate this subtree */
1874 return LY_SUCCESS;
1875 }
1876
Michal Vasko19175b62022-04-01 09:17:07 +02001877 /* we need to use the same context */
1878 trg_ctx = LYD_CTX(node);
1879 }
1880
Michal Vasko52927e22020-03-16 17:26:14 +01001881 if (!node->schema) {
1882 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001883 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001884 } else {
1885 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001886 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001887 case LYS_ACTION:
1888 case LYS_NOTIF:
1889 case LYS_CONTAINER:
1890 case LYS_LIST:
1891 dup = calloc(1, sizeof(struct lyd_node_inner));
1892 break;
1893 case LYS_LEAF:
1894 case LYS_LEAFLIST:
1895 dup = calloc(1, sizeof(struct lyd_node_term));
1896 break;
1897 case LYS_ANYDATA:
1898 case LYS_ANYXML:
1899 dup = calloc(1, sizeof(struct lyd_node_any));
1900 break;
1901 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001902 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001903 ret = LY_EINT;
1904 goto error;
1905 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001906 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001907 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001908
Michal Vaskof6df0a02020-06-16 13:08:34 +02001909 if (options & LYD_DUP_WITH_FLAGS) {
1910 dup->flags = node->flags;
1911 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001912 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001913 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02001914 if (options & LYD_DUP_WITH_PRIV) {
1915 dup->priv = node->priv;
1916 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001917 if (trg_ctx == LYD_CTX(node)) {
1918 dup->schema = node->schema;
1919 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001920 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001921 if (ret) {
1922 /* has no schema but is not an opaque node */
1923 free(dup);
1924 dup = NULL;
1925 goto error;
1926 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001927 }
Michal Vasko52927e22020-03-16 17:26:14 +01001928 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001929
Michal Vasko9cf62422021-07-01 13:29:32 +02001930 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001931 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001932 if (!node->schema) {
1933 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1934 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1935 }
1936 } else {
1937 LY_LIST_FOR(node->meta, meta) {
aPiecek41680342023-11-08 10:19:44 +01001938 LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001939 }
Michal Vasko25a32822020-07-09 15:48:22 +02001940 }
1941 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001942
1943 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001944 if (!dup->schema) {
1945 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1946 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1947 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001948
1949 if (options & LYD_DUP_RECURSIVE) {
1950 /* duplicate all the children */
1951 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001952 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001953 }
1954 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001955 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1956 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1957 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1958 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001959 opaq->hints = orig->hints;
1960 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001961 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001962 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001963 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001964 }
Michal Vasko52927e22020-03-16 17:26:14 +01001965 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1966 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1967 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1968
1969 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001970 if (trg_ctx == LYD_CTX(node)) {
1971 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1972 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1973 } else {
1974 /* store canonical value in the target context */
1975 val_can = lyd_get_value(node);
1976 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001977 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 +01001978 LYD_HINT_DATA, term->schema, NULL);
1979 LY_CHECK_GOTO(ret, error);
1980 }
Michal Vasko52927e22020-03-16 17:26:14 +01001981 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1982 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1983 struct lyd_node *child;
1984
1985 if (options & LYD_DUP_RECURSIVE) {
1986 /* duplicate all the children */
1987 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001988 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001989 }
Michal Vasko69730152020-10-09 16:30:07 +02001990 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001991 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001992 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001993 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001994 }
1995 }
1996 lyd_hash(dup);
1997 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001998 dup->hash = node->hash;
1999 any = (struct lyd_node_any *)node;
2000 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002001 }
2002
Michal Vasko52927e22020-03-16 17:26:14 +01002003 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02002004 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01002005
2006 if (dup_p) {
2007 *dup_p = dup;
2008 }
2009 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002010
2011error:
Michal Vasko52927e22020-03-16 17:26:14 +01002012 lyd_free_tree(dup);
2013 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002014}
2015
Michal Vasko29d674b2021-08-25 11:18:35 +02002016/**
2017 * @brief Get a parent node to connect duplicated subtree to.
2018 *
2019 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01002020 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02002021 * @param[in] parent Initial parent to connect to.
2022 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2023 * @param[out] dup_parent First duplicated parent node, if any.
2024 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
2025 * @return LY_ERR value.
2026 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002027static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002028lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
2029 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002030{
Michal Vasko58d89e92023-05-23 09:56:19 +02002031 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002032 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02002033 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002034
2035 *dup_parent = NULL;
2036 *local_parent = NULL;
2037
Michal Vasko83522192022-07-20 08:07:34 +02002038 if (node->flags & LYD_EXT) {
2039 ext_parent = 1;
2040 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002041 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02002042 if (ext_parent) {
2043 /* use the standard context */
2044 trg_ctx = LYD_CTX(orig_parent);
2045 }
Michal Vasko13c5b212023-02-14 10:03:01 +01002046 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002047 /* stop creating parents, connect what we have into the provided parent */
2048 iter = parent;
2049 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01002050 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
2051 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02002052 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01002053 iter = parent;
2054 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002055 } else {
2056 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02002057 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002058
2059 /* insert into the previous duplicated parent */
2060 if (*dup_parent) {
2061 lyd_insert_node(iter, NULL, *dup_parent, 0);
2062 }
2063
2064 /* update the last duplicated parent */
2065 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002066 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002067
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002068 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002069 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02002070 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002071 }
Michal Vasko58d89e92023-05-23 09:56:19 +02002072
Michal Vasko83522192022-07-20 08:07:34 +02002073 if (orig_parent->flags & LYD_EXT) {
2074 ext_parent = 1;
2075 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002076 }
2077
2078 if (repeat && parent) {
2079 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02002080 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
2081 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002082 return LY_EINVAL;
2083 }
2084
Michal Vaskoeaef7c92023-10-17 10:06:15 +02002085 if (*dup_parent && parent) {
2086 /* last insert into a prevously-existing parent */
2087 lyd_insert_node(parent, NULL, *dup_parent, 0);
2088 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002089 return LY_SUCCESS;
2090}
2091
2092static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02002093lyd_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 +01002094 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002095{
2096 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002097 const struct lyd_node *orig; /* original node to be duplicated */
2098 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002099 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02002100 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002101
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002102 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002103
2104 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002105 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 +02002106 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002107 } else {
2108 local_parent = parent;
2109 }
2110
Radek Krejci22ebdba2019-07-25 13:59:43 +02002111 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01002112 if (lysc_is_key(orig->schema)) {
2113 if (local_parent) {
2114 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02002115 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01002116 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01002117 } else {
2118 assert(!(options & LYD_DUP_WITH_PARENTS));
2119 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01002120 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002121 LY_CHECK_GOTO(rc, error);
2122 }
2123 } else {
2124 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02002125 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002126 LY_CHECK_GOTO(rc, error);
2127 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002128 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002129 break;
2130 }
2131 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002132
Michal Vasko3a41dff2020-07-15 14:30:28 +02002133 if (dup) {
2134 *dup = first;
2135 }
2136 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002137
2138error:
2139 if (top) {
2140 lyd_free_tree(top);
2141 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002142 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002143 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002144 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002145}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002146
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002147/**
2148 * @brief Check the context of node and parent when duplicating nodes.
2149 *
2150 * @param[in] node Node to duplicate.
2151 * @param[in] parent Parent of the duplicated node(s).
2152 * @return LY_ERR value.
2153 */
2154static LY_ERR
2155lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2156{
2157 const struct lyd_node *iter;
2158
2159 if (!node || !parent) {
2160 return LY_SUCCESS;
2161 }
2162
2163 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2164 /* try to find top-level ext data parent */
2165 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2166
2167 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002168 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002169 return LY_EINVAL;
2170 }
2171 }
2172
2173 return LY_SUCCESS;
2174}
2175
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002176LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002177lyd_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 +02002178{
Michal Vaskoddd76592022-01-17 13:34:48 +01002179 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002180 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002181
Michal Vasko58d89e92023-05-23 09:56:19 +02002182 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002183}
2184
2185LIBYANG_API_DEF LY_ERR
2186lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2187 uint32_t options, struct lyd_node **dup)
2188{
2189 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2190
Michal Vasko58d89e92023-05-23 09:56:19 +02002191 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002192}
2193
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002194LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002195lyd_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 +02002196{
Michal Vaskoddd76592022-01-17 13:34:48 +01002197 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002198 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002199
Michal Vasko58d89e92023-05-23 09:56:19 +02002200 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002201}
2202
2203LIBYANG_API_DEF LY_ERR
2204lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2205 uint32_t options, struct lyd_node **dup)
2206{
2207 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2208
Michal Vasko58d89e92023-05-23 09:56:19 +02002209 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002210}
2211
aPiecek41680342023-11-08 10:19:44 +01002212LY_ERR
2213lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
2214 struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002215{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002216 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002217 struct lyd_meta *mt, *last;
aPiecek41680342023-11-08 10:19:44 +01002218 const struct lysc_type *ant_type;
2219 struct lys_module *mod;
2220 const char *val_can;
Michal Vasko25a32822020-07-09 15:48:22 +02002221
aPiecek41680342023-11-08 10:19:44 +01002222 LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL);
Michal Vasko33c48972022-07-20 10:28:07 +02002223
Michal Vasko25a32822020-07-09 15:48:22 +02002224 /* create a copy */
2225 mt = calloc(1, sizeof *mt);
aPiecek41680342023-11-08 10:19:44 +01002226 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM);
2227
2228 if (parent_ctx != meta->annotation->module->ctx) {
2229 /* different contexts */
2230 mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision);
2231
2232 /* annotation */
2233 mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name));
2234 lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
2235 LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL,
2236 "Annotation for metadata %s not found, value duplication failed.", meta->name), finish);
2237
2238 /* duplicate callback expect only the same contexts, so use the store callback */
2239 val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value);
2240 ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, NULL,
2241 LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL);
2242 } else {
2243 /* annotation */
2244 mt->annotation = meta->annotation;
2245 /* duplication of value */
2246 ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value);
2247 }
2248 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish);
2249 LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002250
2251 /* insert as the last attribute */
aPiecek41680342023-11-08 10:19:44 +01002252 mt->parent = parent;
2253 if (parent->meta) {
2254 for (last = parent->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002255 last->next = mt;
2256 } else {
aPiecek41680342023-11-08 10:19:44 +01002257 parent->meta = mt;
Michal Vasko25a32822020-07-09 15:48:22 +02002258 }
2259
Radek Krejci011e4aa2020-09-04 15:22:31 +02002260finish:
2261 if (ret) {
2262 lyd_free_meta_single(mt);
2263 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002264 *dup = mt;
2265 }
2266 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002267}
2268
aPiecek41680342023-11-08 10:19:44 +01002269LIBYANG_API_DEF LY_ERR
2270lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
2271{
2272 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
2273
2274 /* log to node context but value must always use the annotation context */
2275 return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup);
2276}
2277
Michal Vasko4490d312020-06-16 13:08:55 +02002278/**
2279 * @brief Merge a source sibling into target siblings.
2280 *
2281 * @param[in,out] first_trg First target sibling, is updated if top-level.
2282 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002283 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002284 * @param[in] merge_cb Optional merge callback.
2285 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002286 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002287 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002288 * @return LY_ERR value.
2289 */
2290static LY_ERR
2291lyd_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 +02002292 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002293{
Michal Vasko4490d312020-06-16 13:08:55 +02002294 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002295 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002296 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002297 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002298 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002299 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002300 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002301
2302 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002303 if (!sibling_src->schema) {
2304 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002305 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002306 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002307 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002308 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002309 } else {
2310 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002311 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002312 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002313 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002314
Michal Vaskocd3f6172021-05-18 16:14:50 +02002315 if (match_trg) {
2316 /* update match as needed */
2317 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2318 } else {
2319 /* first instance of this node */
2320 first_inst = 1;
2321 }
2322
2323 if (match_trg) {
2324 /* call callback */
2325 if (merge_cb) {
2326 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2327 }
2328
Michal Vasko4490d312020-06-16 13:08:55 +02002329 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002330 if (!match_trg->schema) {
2331 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2332 /* update value */
2333 opaq_trg = (struct lyd_node_opaq *)match_trg;
2334 opaq_src = (struct lyd_node_opaq *)sibling_src;
2335
2336 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2337 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2338 opaq_trg->hints = opaq_src->hints;
2339
2340 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2341 opaq_trg->format = opaq_src->format;
2342 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2343 &opaq_trg->val_prefix_data);
2344 }
2345 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2346 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002347 /* since they are different, they cannot both be default */
2348 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2349
Michal Vasko3a41dff2020-07-15 14:30:28 +02002350 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2351 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002352 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002353 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2354 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002355 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002356
2357 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002358 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002359 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002360 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002361 /* update value */
2362 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002363 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002364
2365 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002366 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002367 }
2368
2369 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002370 r = LY_SUCCESS;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002371 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko42c96e22024-01-18 08:18:06 +01002372 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 +02002373 &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002374 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002375 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002376 }
2377 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002378 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002379 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002380 } else {
2381 /* node not found, merge it */
2382 if (options & LYD_MERGE_DESTRUCT) {
2383 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002384 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002385 /* spend it */
2386 *sibling_src_p = NULL;
2387 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002388 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 +02002389 }
2390
Michal Vasko7e8315b2021-08-03 17:01:06 +02002391 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2392 /* set LYD_NEW for all the new nodes, required for validation */
2393 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2394 elem->flags |= LYD_NEW;
2395 LYD_TREE_DFS_END(dup_src, elem);
2396 }
Michal Vasko4490d312020-06-16 13:08:55 +02002397 }
2398
Michal Vaskocd3f6172021-05-18 16:14:50 +02002399 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002400 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002401
2402 if (first_inst) {
2403 /* remember not to find this instance next time */
2404 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2405 }
2406
2407 /* call callback, no source node */
2408 if (merge_cb) {
2409 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2410 }
Michal Vasko4490d312020-06-16 13:08:55 +02002411 }
2412
2413 return LY_SUCCESS;
2414}
2415
Michal Vasko3a41dff2020-07-15 14:30:28 +02002416static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002417lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2418 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002419{
2420 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002421 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002422 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002423 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002424
2425 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002426 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2427 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002428
2429 if (!source) {
2430 /* nothing to merge */
2431 return LY_SUCCESS;
2432 }
2433
Michal Vasko831422b2020-11-24 11:20:51 +01002434 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002435 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002436 return LY_EINVAL;
2437 }
2438
2439 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002440 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2441 /* skip data nodes from different modules */
2442 continue;
2443 }
2444
Radek Krejci857189e2020-09-01 13:26:36 +02002445 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002446 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2447 if (ret) {
2448 break;
2449 }
Michal Vasko4490d312020-06-16 13:08:55 +02002450 if (first && !sibling_src) {
2451 /* source was spent (unlinked), move to the next node */
2452 source = tmp;
2453 }
2454
Michal Vasko3a41dff2020-07-15 14:30:28 +02002455 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002456 break;
2457 }
2458 }
2459
2460 if (options & LYD_MERGE_DESTRUCT) {
2461 /* free any leftover source data that were not merged */
2462 lyd_free_siblings((struct lyd_node *)source);
2463 }
2464
Michal Vaskocd3f6172021-05-18 16:14:50 +02002465 lyd_dup_inst_free(dup_inst);
2466 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002467}
2468
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002469LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002470lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002471{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002472 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002473}
2474
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002475LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002476lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002477{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002478 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2479}
2480
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002481LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002482lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2483 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2484{
2485 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002486}
2487
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002488static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002489lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002490{
Michal Vasko14654712020-02-06 08:35:21 +01002491 /* ending \0 */
2492 ++reqlen;
2493
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002494 if (reqlen > *buflen) {
2495 if (is_static) {
2496 return LY_EINCOMPLETE;
2497 }
2498
2499 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2500 if (!*buffer) {
2501 return LY_EMEM;
2502 }
2503
2504 *buflen = reqlen;
2505 }
2506
2507 return LY_SUCCESS;
2508}
2509
Michal Vaskod59035b2020-07-08 12:00:06 +02002510LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002511lyd_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 +02002512{
2513 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002514 size_t len;
2515 const char *val;
2516 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002517
Michal Vasko824d0832021-11-04 15:36:51 +01002518 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002519 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002520 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002521 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002522
2523 quot = '\'';
2524 if (strchr(val, '\'')) {
2525 quot = '"';
2526 }
2527 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002528 }
2529
2530 return LY_SUCCESS;
2531}
2532
2533/**
2534 * @brief Append leaf-list value predicate to path.
2535 *
2536 * @param[in] node Node to print.
2537 * @param[in,out] buffer Buffer to print to.
2538 * @param[in,out] buflen Current buffer length.
2539 * @param[in,out] bufused Current number of characters used in @p buffer.
2540 * @param[in] is_static Whether buffer is static or can be reallocated.
2541 * @return LY_ERR
2542 */
2543static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002544lyd_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 +02002545{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002546 size_t len;
2547 const char *val;
2548 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002549
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002550 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002551 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002552 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002553
2554 quot = '\'';
2555 if (strchr(val, '\'')) {
2556 quot = '"';
2557 }
2558 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2559
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002560 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002561}
2562
2563/**
2564 * @brief Append node position (relative to its other instances) predicate to path.
2565 *
2566 * @param[in] node Node to print.
2567 * @param[in,out] buffer Buffer to print to.
2568 * @param[in,out] buflen Current buffer length.
2569 * @param[in,out] bufused Current number of characters used in @p buffer.
2570 * @param[in] is_static Whether buffer is static or can be reallocated.
2571 * @return LY_ERR
2572 */
2573static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002574lyd_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 +02002575{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002576 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002577 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002578 char *val = NULL;
2579 LY_ERR rc;
2580
Michal Vasko50cc0562021-05-18 16:15:43 +02002581 pos = lyd_list_pos(node);
2582 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002583 return LY_EMEM;
2584 }
2585
2586 len = 1 + strlen(val) + 1;
2587 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2588 if (rc != LY_SUCCESS) {
2589 goto cleanup;
2590 }
2591
2592 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2593
2594cleanup:
2595 free(val);
2596 return rc;
2597}
2598
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002599LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002600lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2601{
Radek Krejci857189e2020-09-01 13:26:36 +02002602 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002603 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002604 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002605 const struct lyd_node *iter, *parent;
2606 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002607 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002608
2609 LY_CHECK_ARG_RET(NULL, node, NULL);
2610 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002611 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002612 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002613 } else {
2614 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002615 }
2616
2617 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002618 case LYD_PATH_STD:
2619 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002620 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002621 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002622 ++depth;
2623 }
2624
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002625 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002626 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002627 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002628 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002629iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002630 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002631 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002632 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002633 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002634 if (prev_mod == mod) {
2635 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002636 }
2637
2638 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002639 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2640 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002641 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2642 if (rc != LY_SUCCESS) {
2643 break;
2644 }
2645
2646 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002647 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002648
Michal Vasko790b2bc2020-08-03 13:35:06 +02002649 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002650 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002651 switch (iter->schema->nodetype) {
2652 case LYS_LIST:
2653 if (iter->schema->flags & LYS_KEYLESS) {
2654 /* print its position */
2655 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2656 } else {
2657 /* print all list keys in predicates */
2658 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2659 }
2660 break;
2661 case LYS_LEAFLIST:
2662 if (iter->schema->flags & LYS_CONFIG_W) {
2663 /* print leaf-list value */
2664 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2665 } else {
2666 /* print its position */
2667 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2668 }
2669 break;
2670 default:
2671 /* nothing to print more */
2672 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002673 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002674 }
2675 if (rc != LY_SUCCESS) {
2676 break;
2677 }
2678
Michal Vasko14654712020-02-06 08:35:21 +01002679 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002680 }
2681 break;
2682 }
2683
2684 return buffer;
2685}
Michal Vaskoe444f752020-02-10 12:20:06 +01002686
Michal Vaskodbf3e652022-10-21 08:46:25 +02002687char *
2688lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2689{
2690 uint32_t depth;
2691 size_t bufused = 0, buflen = 0, len;
2692 char *buffer = NULL;
2693 const struct lyd_node *iter, *parent;
2694 const struct lys_module *mod, *prev_mod;
2695 LY_ERR rc = LY_SUCCESS;
2696
2697 switch (pathtype) {
2698 case LYD_PATH_STD:
2699 case LYD_PATH_STD_NO_LAST_PRED:
2700 for (depth = 1; depth <= dnodes->count; ++depth) {
2701 /* current node */
2702 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002703 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002704
2705 /* parent */
2706 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002707 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2708 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002709 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2710 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002711
2712 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002713 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002714 if (prev_mod == mod) {
2715 mod = NULL;
2716 }
2717
2718 /* realloc string */
2719 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2720 strlen(((struct lyd_node_opaq *)iter)->name.name));
2721 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2722 break;
2723 }
2724
2725 /* print next node */
2726 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2727
2728 /* do not always print the last (first) predicate */
2729 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2730 switch (iter->schema->nodetype) {
2731 case LYS_LIST:
2732 if (iter->schema->flags & LYS_KEYLESS) {
2733 /* print its position */
2734 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2735 } else {
2736 /* print all list keys in predicates */
2737 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2738 }
2739 break;
2740 case LYS_LEAFLIST:
2741 if (iter->schema->flags & LYS_CONFIG_W) {
2742 /* print leaf-list value */
2743 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2744 } else {
2745 /* print its position */
2746 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2747 }
2748 break;
2749 default:
2750 /* nothing to print more */
2751 break;
2752 }
2753 }
2754 if (rc) {
2755 break;
2756 }
2757 }
2758 break;
2759 }
2760
2761 return buffer;
2762}
2763
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002764LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002765lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2766{
2767 struct lyd_meta *ret = NULL;
2768 const struct ly_ctx *ctx;
2769 const char *prefix, *tmp;
2770 char *str;
2771 size_t pref_len, name_len;
2772
2773 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002774 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002775
2776 if (!first) {
2777 return NULL;
2778 }
2779
2780 ctx = first->annotation->module->ctx;
2781
2782 /* parse the name */
2783 tmp = name;
2784 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2785 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2786 return NULL;
2787 }
2788
2789 /* find the module */
2790 if (prefix) {
2791 str = strndup(prefix, pref_len);
2792 module = ly_ctx_get_module_latest(ctx, str);
2793 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002794 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 +02002795 }
2796
2797 /* find the metadata */
2798 LY_LIST_FOR(first, first) {
2799 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2800 ret = (struct lyd_meta *)first;
2801 break;
2802 }
2803 }
2804
2805 return ret;
2806}
2807
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002808LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002809lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2810{
Michal Vasko9beceb82022-04-05 12:14:15 +02002811 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002812 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002813 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002814
Michal Vaskof03ed032020-03-04 13:31:44 +01002815 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002816 if (!siblings) {
2817 /* no data */
2818 if (match) {
2819 *match = NULL;
2820 }
2821 return LY_ENOTFOUND;
2822 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002823
Michal Vasko9beceb82022-04-05 12:14:15 +02002824 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2825 /* create a duplicate in this context */
2826 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2827 target = dup;
2828 }
2829
2830 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2831 /* schema mismatch */
2832 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002833 if (match) {
2834 *match = NULL;
2835 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002836 return LY_ENOTFOUND;
2837 }
2838
Michal Vaskoe78faec2021-04-08 17:24:43 +02002839 /* get first sibling */
2840 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002841
Michal Vasko9e685082021-01-29 14:49:09 +01002842 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002843 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002844 assert(target->hash);
2845
Michal Vaskoe78faec2021-04-08 17:24:43 +02002846 if (lysc_is_dup_inst_list(target->schema)) {
2847 /* we must search the instances from beginning to find the first matching one */
2848 found = 0;
2849 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002850 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002851 found = 1;
2852 break;
2853 }
2854 }
2855 if (found) {
2856 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002857 } else {
2858 siblings = NULL;
2859 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002860 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002861 /* find by hash */
2862 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2863 siblings = *match_p;
2864 } else {
2865 /* not found */
2866 siblings = NULL;
2867 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002868 }
2869 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002870 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002871 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002872 if (lysc_is_dup_inst_list(target->schema)) {
2873 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2874 break;
2875 }
2876 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002877 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002878 break;
2879 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002880 }
2881 }
2882 }
2883
Michal Vasko9beceb82022-04-05 12:14:15 +02002884 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002885 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002886 if (match) {
2887 *match = NULL;
2888 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002889 return LY_ENOTFOUND;
2890 }
2891
Michal Vasko9b368d32020-02-14 13:53:31 +01002892 if (match) {
2893 *match = (struct lyd_node *)siblings;
2894 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002895 return LY_SUCCESS;
2896}
2897
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002898LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002899lyd_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 +02002900 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002901{
2902 LY_ERR rc;
2903 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002904 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002905
Michal Vasko4c583e82020-07-17 12:16:14 +02002906 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002907 if (!siblings) {
2908 /* no data */
2909 if (match) {
2910 *match = NULL;
2911 }
2912 return LY_ENOTFOUND;
2913 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002914
Michal Vasko9beceb82022-04-05 12:14:15 +02002915 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2916 /* parent of ext nodes is useless */
2917 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002918 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2919 /* no schema node in siblings so certainly no data node either */
2920 if (match) {
2921 *match = NULL;
2922 }
2923 return LY_ENOTFOUND;
2924 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002925 }
2926
2927 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2928 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002929 if (match) {
2930 *match = NULL;
2931 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002932 return LY_ENOTFOUND;
2933 }
2934
Michal Vaskof03ed032020-03-04 13:31:44 +01002935 if (key_or_value && !val_len) {
2936 val_len = strlen(key_or_value);
2937 }
2938
Michal Vaskob104f112020-07-17 09:54:54 +02002939 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2940 /* create a data node and find the instance */
2941 if (schema->nodetype == LYS_LEAFLIST) {
2942 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002943 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 +02002944 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002945 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002946 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002947 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002948 }
2949
2950 /* find it */
2951 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002952 } else {
2953 /* find the first schema node instance */
2954 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002955 }
2956
Michal Vaskoe444f752020-02-10 12:20:06 +01002957 lyd_free_tree(target);
2958 return rc;
2959}
Michal Vaskoccc02342020-05-21 10:09:21 +02002960
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002961LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002962lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2963{
2964 struct lyd_node **match_p, *first, *iter;
2965 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002966 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002967
Michal Vasko83ae7772022-06-08 10:01:55 +02002968 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002969 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002970
2971 LY_CHECK_RET(ly_set_new(set));
2972
2973 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2974 /* no data or schema mismatch */
2975 return LY_ENOTFOUND;
2976 }
2977
Michal Vaskoee9b9482023-06-19 13:17:48 +02002978 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002979 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002980
Michal Vaskoe78faec2021-04-08 17:24:43 +02002981 /* get first sibling */
2982 siblings = lyd_first_sibling(siblings);
2983
2984 parent = siblings->parent;
2985 if (parent && parent->schema && parent->children_ht) {
2986 assert(target->hash);
2987
2988 /* find the first instance */
2989 lyd_find_sibling_first(siblings, target, &first);
2990 if (first) {
2991 /* add it so that it is the first in the set */
2992 if (ly_set_add(*set, first, 1, NULL)) {
2993 goto error;
2994 }
2995
2996 /* find by hash */
2997 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2998 iter = *match_p;
2999 } else {
3000 /* not found */
3001 iter = NULL;
3002 }
3003 while (iter) {
3004 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02003005 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003006 goto error;
3007 }
3008
3009 /* find next instance */
3010 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
3011 iter = NULL;
3012 } else {
3013 iter = *match_p;
3014 }
3015 }
3016 }
3017 } else {
3018 /* no children hash table */
3019 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02003020 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003021 ly_set_add(*set, (void *)siblings, 1, NULL);
3022 }
3023 }
3024 }
3025
3026 if (!(*set)->count) {
3027 return LY_ENOTFOUND;
3028 }
3029 return LY_SUCCESS;
3030
3031error:
3032 ly_set_free(*set, NULL);
3033 *set = NULL;
3034 return LY_EMEM;
3035}
3036
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003037LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003038lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3039{
3040 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3041
Michal Vaskoe271a312023-08-15 11:46:30 +02003042 if (first && first->schema) {
3043 first = first->prev;
3044 if (first->schema) {
3045 /* no opaque nodes */
3046 first = NULL;
3047 } else {
3048 /* opaque nodes are at the end, find quickly the first */
3049 while (!first->prev->schema) {
3050 first = first->prev;
3051 }
3052 }
3053 }
3054
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003055 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02003056 assert(!first->schema);
3057 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003058 break;
3059 }
3060 }
3061
3062 if (match) {
3063 *match = (struct lyd_node *)first;
3064 }
3065 return first ? LY_SUCCESS : LY_ENOTFOUND;
3066}
3067
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003068LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003069lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02003070{
Michal Vaskod96e2372023-02-24 16:07:51 +01003071 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003072
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003073 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01003074}
Michal Vaskoccc02342020-05-21 10:09:21 +02003075
Michal Vaskod96e2372023-02-24 16:07:51 +01003076LIBYANG_API_DEF LY_ERR
3077lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
3078{
3079 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02003080
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003081 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003082}
Radek Krejcica989142020-11-05 11:32:22 +01003083
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003084LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01003085lyd_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 +01003086 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01003087{
Michal Vaskod96e2372023-02-24 16:07:51 +01003088 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01003089
Michal Vaskod96e2372023-02-24 16:07:51 +01003090 *set = NULL;
3091
3092 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 +01003093}
3094
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003095LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003096lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003097{
Michal Vaskod96e2372023-02-24 16:07:51 +01003098 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02003099}
3100
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003101LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02003102lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
3103{
3104 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
3105}
3106
3107LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003108lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
3109 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003110{
Michal Vaskod96e2372023-02-24 16:07:51 +01003111 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
3112}
3113
3114LIBYANG_API_DEF LY_ERR
3115lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
3116 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
3117 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
3118{
3119 LY_ERR ret = LY_SUCCESS;
3120 struct lyxp_set xp_set = {0};
3121 struct lyxp_expr *exp = NULL;
3122 uint32_t i;
3123
3124 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
3125 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
3126 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
3127
3128 /* parse expression */
3129 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
3130 LY_CHECK_GOTO(ret, cleanup);
3131
3132 /* evaluate expression */
3133 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
3134 LYXP_IGNORE_WHEN);
3135 LY_CHECK_GOTO(ret, cleanup);
3136
3137 /* return expected result type without or with casting */
3138 if (node_set) {
3139 /* node set */
3140 if (xp_set.type == LYXP_SET_NODE_SET) {
3141 /* transform into a set */
3142 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
3143 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
3144 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
3145 (*node_set)->size = xp_set.used;
3146 for (i = 0; i < xp_set.used; ++i) {
3147 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3148 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
3149 LY_CHECK_GOTO(ret, cleanup);
3150 }
3151 }
3152 if (ret_type) {
3153 *ret_type = LY_XPATH_NODE_SET;
3154 }
3155 } else if (!string && !number && !boolean) {
3156 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
3157 ret = LY_EINVAL;
3158 goto cleanup;
3159 }
3160 }
3161
3162 if (string) {
3163 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
3164 /* cast into string */
3165 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3166 }
3167 if (xp_set.type == LYXP_SET_STRING) {
3168 /* string */
3169 *string = xp_set.val.str;
3170 xp_set.val.str = NULL;
3171 if (ret_type) {
3172 *ret_type = LY_XPATH_STRING;
3173 }
3174 }
3175 }
3176
3177 if (number) {
3178 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3179 /* cast into number */
3180 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3181 }
3182 if (xp_set.type == LYXP_SET_NUMBER) {
3183 /* number */
3184 *number = xp_set.val.num;
3185 if (ret_type) {
3186 *ret_type = LY_XPATH_NUMBER;
3187 }
3188 }
3189 }
3190
3191 if (boolean) {
3192 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3193 /* cast into boolean */
3194 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3195 }
3196 if (xp_set.type == LYXP_SET_BOOLEAN) {
3197 /* boolean */
3198 *boolean = xp_set.val.bln;
3199 if (ret_type) {
3200 *ret_type = LY_XPATH_BOOLEAN;
3201 }
3202 }
3203 }
3204
3205cleanup:
3206 lyxp_set_free_content(&xp_set);
3207 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3208 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003209}
3210
Michal Vasko99a77882024-01-04 14:50:51 +01003211/**
3212 * @brief Hash table node equal callback.
3213 */
3214static ly_bool
3215lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3216{
3217 struct lyd_node *node1, *node2;
3218
3219 node1 = *(struct lyd_node **)val1_p;
3220 node2 = *(struct lyd_node **)val2_p;
3221
3222 return node1 == node2;
3223}
3224
3225LIBYANG_API_DEF LY_ERR
3226lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3227{
3228 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003229 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003230 struct lyxp_set xp_set = {0};
3231 struct lyxp_expr *exp = NULL;
3232 struct lyd_node *node, *parent;
3233 struct lyxp_set_hash_node hnode;
3234 struct ly_ht *parent_ht = NULL;
3235 struct ly_set free_set = {0};
3236 uint32_t i, hash;
3237 ly_bool is_result;
3238
3239 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3240
3241 if (!*tree) {
3242 /* nothing to do */
3243 goto cleanup;
3244 }
3245
3246 *tree = lyd_first_sibling(*tree);
3247 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3248
3249 /* parse expression */
3250 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3251 LY_CHECK_GOTO(ret, cleanup);
3252
3253 /* evaluate expression */
3254 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3255 LY_CHECK_GOTO(ret, cleanup);
3256
3257 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003258 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003259 LY_CHECK_GOTO(!parent_ht, cleanup);
3260
3261 for (i = 0; i < xp_set.used; ++i) {
3262 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3263 /* ignore */
3264 continue;
3265 }
3266
3267 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3268 /* add the parent into parent_ht */
3269 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3270 if (ret == LY_EEXIST) {
3271 /* shared parent, we are done */
3272 break;
3273 }
3274 LY_CHECK_GOTO(ret, cleanup);
3275 }
3276 }
3277
3278 hnode.type = LYXP_NODE_ELEM;
3279 LY_LIST_FOR(*tree, parent) {
3280 LYD_TREE_DFS_BEGIN(parent, node) {
3281 if (lysc_is_key(node->schema)) {
3282 /* ignore */
3283 goto next_iter;
3284 }
3285
3286 /* check the results */
3287 is_result = 0;
3288 if (xp_set.ht) {
3289 hnode.node = node;
3290 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3291 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3292 hash = lyht_hash_multi(hash, NULL, 0);
3293
3294 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3295 is_result = 1;
3296 }
3297 } else {
3298 /* not enough elements for a hash table */
3299 for (i = 0; i < xp_set.used; ++i) {
3300 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3301 /* ignore */
3302 continue;
3303 }
3304
3305 if (xp_set.val.nodes[i].node == node) {
3306 is_result = 1;
3307 break;
3308 }
3309 }
3310 }
3311
3312 if (is_result) {
3313 /* keep the whole subtree if the node is in the results */
3314 LYD_TREE_DFS_continue = 1;
3315 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3316 /* free the whole subtree if the node is not even among the selected parents */
3317 ret = ly_set_add(&free_set, node, 1, NULL);
3318 LY_CHECK_GOTO(ret, cleanup);
3319 LYD_TREE_DFS_continue = 1;
3320 } /* else keep the parent node because a subtree is in the results */
3321
3322next_iter:
3323 LYD_TREE_DFS_END(parent, node);
3324 }
3325 }
3326
3327 /* free */
3328 for (i = 0; i < free_set.count; ++i) {
3329 node = free_set.dnodes[i];
3330 if (*tree == node) {
3331 *tree = (*tree)->next;
3332 }
3333 lyd_free_tree(node);
3334 }
3335
3336cleanup:
3337 lyxp_set_free_content(&xp_set);
3338 lyxp_expr_free(ctx, exp);
3339 lyht_free(parent_ht, NULL);
3340 ly_set_erase(&free_set, NULL);
3341 return ret;
3342}
3343
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003344LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003345lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3346{
3347 LY_ERR ret = LY_SUCCESS;
3348 struct lyxp_expr *expr = NULL;
3349 struct ly_path *lypath = NULL;
3350
3351 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3352
3353 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003354 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 +01003355 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003356 LY_CHECK_GOTO(ret, cleanup);
3357
3358 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003359 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003360 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 +01003361 LY_CHECK_GOTO(ret, cleanup);
3362
3363 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003364 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003365
3366cleanup:
3367 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3368 ly_path_free(LYD_CTX(ctx_node), lypath);
3369 return ret;
3370}
3371
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003372LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003373lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3374{
3375 LY_ERR ret;
3376 struct lyd_node *m;
3377
3378 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3379
Michal Vasko90189962023-02-28 12:10:34 +01003380 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003381 if (ret) {
3382 if (match) {
3383 *match = NULL;
3384 }
3385 return LY_ENOTFOUND;
3386 }
3387
3388 if (match) {
3389 *match = m;
3390 }
3391 return LY_SUCCESS;
3392}
Irfand3b351a2023-09-14 14:52:15 +02003393
stewegf9041a22024-01-18 13:29:12 +01003394LY_ERR
3395lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3396{
3397 struct ly_ht *ht;
3398 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003399 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003400
3401 assert(node);
3402 assert(record);
3403
Michal Vaskob46061d2024-01-18 13:58:13 +01003404 *record = NULL;
3405
stewegf9041a22024-01-18 13:29:12 +01003406 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003407 return LY_EDENIED;
3408 }
3409
3410 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003411 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003412 hash = lyht_hash((const char *)&node, sizeof node);
3413
stewegf9041a22024-01-18 13:29:12 +01003414 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3415 if (create) {
3416 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3417 } else {
stewegf9041a22024-01-18 13:29:12 +01003418 return LY_ENOTFOUND;
3419 }
3420 }
3421
3422 return LY_SUCCESS;
3423}
3424
3425LIBYANG_API_DEF LY_ERR
3426lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3427{
3428 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3429
3430 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3431}
3432
3433LY_ERR
3434lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3435{
stewegf9041a22024-01-18 13:29:12 +01003436 const struct lyd_node_term **item = NULL;
3437 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003438 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003439
3440 assert(node);
3441 assert(leafref_node);
3442
3443 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3444 return LY_EDENIED;
3445 }
3446
steweg67388952024-01-25 12:14:50 +01003447 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003448 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3449 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3450 if (rec->leafref_nodes[u] == leafref_node) {
3451 return LY_SUCCESS;
3452 }
3453 }
3454
3455 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3456 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003457
3458 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003459 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003460 LY_ARRAY_FOR(rec->target_nodes, u) {
3461 if (rec->target_nodes[u] == node) {
3462 return LY_SUCCESS;
3463 }
3464 }
3465
3466 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3467 *item = node;
3468
stewegf9041a22024-01-18 13:29:12 +01003469 return LY_SUCCESS;
3470}
3471
3472LIBYANG_API_DEF LY_ERR
3473lyd_leafref_link_node_tree(const struct lyd_node *tree)
3474{
3475 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003476 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003477 char *errmsg;
3478 struct lyd_node_term *leafref_node;
3479 struct lysc_node_leaf *leaf_schema;
3480 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003481 LY_ERR ret = LY_SUCCESS;
3482 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003483
3484 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3485
3486 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3487 return LY_EDENIED;
3488 }
3489
3490 LY_LIST_FOR(tree, sibling) {
3491 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003492 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003493 leafref_node = (struct lyd_node_term *)elem;
3494 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003495
stewegf9041a22024-01-18 13:29:12 +01003496 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3497 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003498 ly_set_free(targets, NULL);
3499 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003500 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003501 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003502 } else {
3503 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003504 for (i = 0; i < targets->count; ++i) {
3505 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3506 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3507 LY_CHECK_GOTO(ret, cleanup);
3508 }
3509 }
stewegf9041a22024-01-18 13:29:12 +01003510 }
3511 }
3512 }
3513 LYD_TREE_DFS_END(sibling, elem);
3514 }
3515 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003516
steweg67388952024-01-25 12:14:50 +01003517cleanup:
3518 ly_set_free(targets, NULL);
3519 return ret;
stewegf9041a22024-01-18 13:29:12 +01003520}
3521
3522LY_ERR
3523lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3524{
3525 LY_ERR ret;
3526 struct lyd_leafref_links_rec *rec;
3527
3528 assert(node);
3529 assert(leafref_node);
3530
3531 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3532 return LY_EDENIED;
3533 }
3534
steweg67388952024-01-25 12:14:50 +01003535 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003536 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3537 if (ret == LY_SUCCESS) {
3538 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003539 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003540 lyd_free_leafref_nodes(node);
3541 }
3542 } else if (ret != LY_ENOTFOUND) {
3543 return ret;
3544 }
3545
steweg67388952024-01-25 12:14:50 +01003546 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003547 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3548 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003549 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3550 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003551 lyd_free_leafref_nodes(leafref_node);
3552 }
3553 } else if (ret != LY_ENOTFOUND) {
3554 return ret;
3555 }
3556
3557 return LY_SUCCESS;
3558}