blob: e90aa7c4948db5e0664cd33eb0f12c0e47e72480 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02005 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci084289f2019-07-09 17:35:30 +020020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "context.h"
32#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020033#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020042#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010043#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010048#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020049#include "tree_schema.h"
50#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020051#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020052#include "xml.h"
53#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020054
Michal Vaskof277d362023-04-24 09:08:31 +020055static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
56 ly_bool parental_schemas_checked);
57
Radek Krejci7931b192020-06-25 17:05:03 +020058static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020059lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020060{
Michal Vasko69730152020-10-09 16:30:07 +020061 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020062 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020063 const char *path = in->method.fpath.filepath;
64 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020065
66 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020067 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
70 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020071 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010072 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
73 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020074 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010075 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
76 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020077 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020078 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020079 }
80
Radek Krejci7931b192020-06-25 17:05:03 +020081 return format;
82}
Radek Krejcie7b95092019-05-15 11:03:07 +020083
Michal Vaskoe0665742021-02-11 11:08:44 +010084/**
85 * @brief Parse YANG data into a data tree.
86 *
87 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010088 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010089 * @param[in] parent Parent to connect the parsed nodes to, if any.
90 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
91 * @param[in] in Input handle to read the input from.
92 * @param[in] format Expected format of the data in @p in.
93 * @param[in] parse_opts Options for parser.
94 * @param[in] val_opts Options for validation.
95 * @param[out] op Optional pointer to the parsed operation, if any.
96 * @return LY_ERR value.
97 */
98static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010099lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
100 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200101{
Michal Vaskod027f382023-02-10 09:13:25 +0100102 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200103 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100104 struct ly_set parsed = {0};
105 struct lyd_node *first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100106 uint32_t i, int_opts = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100107 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200108
Michal Vaskoe0665742021-02-11 11:08:44 +0100109 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200110
111 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100112 if (first_p) {
113 *first_p = NULL;
114 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200115
Michal Vasko63f3d842020-07-08 10:10:14 +0200116 /* remember input position */
117 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200118
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100119 /* set internal options */
120 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
121 int_opts = LYD_INTOPT_WITH_SIBLINGS;
122 }
123
Michal Vaskoe0665742021-02-11 11:08:44 +0100124 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200125 switch (format) {
126 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100127 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100128 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200129 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200130 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100131 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100132 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200133 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200134 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100135 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100136 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200137 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200138 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100139 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100140 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100141 break;
142 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100143 if (r) {
144 rc = r;
145 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) ||
146 (ly_vecode(ctx) == LYVE_SYNTAX)) {
147 goto cleanup;
148 }
149 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100150
151 if (parent) {
152 /* get first top-level sibling */
153 for (first = parent; first->parent; first = lyd_parent(first)) {}
154 first = lyd_first_sibling(first);
155 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200156 }
157
Michal Vaskoe0665742021-02-11 11:08:44 +0100158 if (!(parse_opts & LYD_PARSE_ONLY)) {
159 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200160 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 +0200161 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200162 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100163 }
Radek Krejci7931b192020-06-25 17:05:03 +0200164
Michal Vaskoe0665742021-02-11 11:08:44 +0100165 /* set the operation node */
166 if (op) {
167 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200168 }
169
170cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100171 if (lydctx) {
172 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100174 if (rc) {
175 if (parent) {
176 /* free all the parsed subtrees */
177 for (i = 0; i < parsed.count; ++i) {
178 lyd_free_tree(parsed.dnodes[i]);
179 }
180 } else {
181 /* free everything */
182 lyd_free_all(*first_p);
183 *first_p = NULL;
184 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100185 } else if (subtree_sibling) {
186 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100187 }
188 ly_set_erase(&parsed, NULL);
189 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200190}
191
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100192LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100193lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
194 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
195{
196 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
197
198 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
199 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
200 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
201
202 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
203}
204
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100205LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100206lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
207 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
208{
209 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
210 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
211 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
212
Radek Krejcif16e2542021-02-17 15:39:23 +0100213 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100214}
215
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100216LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100217lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
218 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200219{
220 LY_ERR ret;
221 struct ly_in *in;
222
223 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100224 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200225
226 ly_in_free(in, 0);
227 return ret;
228}
229
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100230LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200231lyd_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 +0200232 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200233{
234 LY_ERR ret;
235 struct ly_in *in;
236
237 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100238 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200239
240 ly_in_free(in, 0);
241 return ret;
242}
243
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100244LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200245lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
246 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200247{
248 LY_ERR ret;
249 struct ly_in *in;
250
251 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100252 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200253
254 ly_in_free(in, 0);
255 return ret;
256}
257
Radek Krejcif16e2542021-02-17 15:39:23 +0100258/**
259 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
260 * for schema nodes locked inside the extension instance.
261 *
262 * At least one of @p parent, @p tree, or @p op must always be set.
263 *
264 * Specific @p data_type values have different parameter meaning as follows:
265 * - ::LYD_TYPE_RPC_NETCONF:
266 * - @p parent - must be NULL, the whole RPC is expected;
267 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
268 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
269 * a separate opaque data tree, even if the function fails, this may be returned;
270 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
271 *
272 * - ::LYD_TYPE_NOTIF_NETCONF:
273 * - @p parent - must be NULL, the whole notification is expected;
274 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
275 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
276 * a separate opaque data tree, even if the function fails, this may be returned;
277 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
278 *
279 * - ::LYD_TYPE_REPLY_NETCONF:
280 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
281 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
282 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
283 * a separate opaque data tree, even if the function fails, this may be returned;
284 * - @p op - must be NULL, the reply is appended to the RPC;
285 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
286 *
287 * @param[in] ctx libyang context.
288 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
289 * @param[in] parent Optional parent to connect the parsed nodes to.
290 * @param[in] in Input handle to read the input from.
291 * @param[in] format Expected format of the data in @p in.
292 * @param[in] data_type Expected operation to parse (@ref datatype).
293 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
294 * @param[out] op Optional parsed operation node.
295 * @return LY_ERR value.
296 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
297 */
298static LY_ERR
299lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
300 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 +0200301{
Michal Vaskoe0665742021-02-11 11:08:44 +0100302 LY_ERR rc = LY_SUCCESS;
303 struct lyd_ctx *lydctx = NULL;
304 struct ly_set parsed = {0};
305 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100306 uint32_t i, parse_opts, val_opts, int_opts = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200307
Michal Vasko27fb0262021-02-23 09:42:01 +0100308 if (!ctx) {
309 ctx = LYD_CTX(parent);
310 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200311 if (tree) {
312 *tree = NULL;
313 }
314 if (op) {
315 *op = NULL;
316 }
317
Radek Krejci7931b192020-06-25 17:05:03 +0200318 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200319
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 /* remember input position */
321 in->func_start = in->current;
322
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100323 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200324 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100325 val_opts = 0;
326
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100327 switch (data_type) {
328
329 /* special XML NETCONF data types */
330 case LYD_TYPE_RPC_NETCONF:
331 case LYD_TYPE_NOTIF_NETCONF:
332 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
333 /* fallthrough */
334 case LYD_TYPE_REPLY_NETCONF:
335 if (data_type == LYD_TYPE_REPLY_NETCONF) {
336 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
337 LY_EINVAL);
338 }
339
340 /* parse the NETCONF message */
341 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko3a163bc2023-01-10 14:23:56 +0100342 if (rc) {
343 if (envp) {
344 /* special situation when the envelopes were parsed successfully */
345 *tree = envp;
346 }
Michal Vasko73792a12022-05-10 09:28:45 +0200347 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100348 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100349
350 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100351 if (envp) {
352 /* special out param meaning */
353 *tree = envp;
354 } else {
355 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100356 }
357 if (op) {
358 *op = lydctx->op_node;
359 }
360 goto cleanup;
361
362 /* set internal opts */
363 case LYD_TYPE_RPC_YANG:
364 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
365 break;
366 case LYD_TYPE_NOTIF_YANG:
367 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
368 break;
369 case LYD_TYPE_REPLY_YANG:
370 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
371 break;
372 case LYD_TYPE_DATA_YANG:
373 LOGINT(ctx);
374 rc = LY_EINT;
375 goto cleanup;
376 }
377
378 /* parse the data */
379 switch (format) {
380 case LYD_XML:
381 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 +0100382 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200383 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100384 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 +0100385 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200386 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100387 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 +0100388 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200389 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100390 LOGARG(ctx, format);
391 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200392 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200393 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100394 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200395
Michal Vaskoe0665742021-02-11 11:08:44 +0100396 /* set out params correctly */
397 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100398 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100399 }
400 if (op) {
401 *op = lydctx->op_node;
402 }
403
404cleanup:
405 if (lydctx) {
406 lydctx->free(lydctx);
407 }
408 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200409 /* free all the parsed nodes */
410 if (parsed.count) {
411 i = parsed.count;
412 do {
413 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100414 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200415 } while (i);
416 }
417 if (tree && ((format != LYD_XML) || !envp)) {
418 *tree = NULL;
419 }
420 if (op) {
421 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100422 }
423 }
424 ly_set_erase(&parsed, NULL);
425 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200426}
Radek Krejci084289f2019-07-09 17:35:30 +0200427
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100428LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100429lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
430 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
431{
432 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
433
434 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
435}
436
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100437LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100438lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
439 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
440{
441 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
442
443 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
444
445 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
446}
447
Michal Vasko90932a92020-02-12 14:33:03 +0100448struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200449lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100450{
Michal Vaskob104f112020-07-17 09:54:54 +0200451 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100452 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200453 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100454 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100455
Michal Vaskob104f112020-07-17 09:54:54 +0200456 assert(new_node);
457
Michal Vasko9beceb82022-04-05 12:14:15 +0200458 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200459 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100460 return NULL;
461 }
462
Michal Vasko93b16062020-12-09 18:14:18 +0100463 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100464 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100465 getnext_opts = LYS_GETNEXT_OUTPUT;
466 }
467
Michal Vaskoa2756f02021-07-09 13:20:28 +0200468 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200469 /* find the anchor using hashes */
470 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100471 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200472 while (schema) {
473 /* keep trying to find the first existing instance of the closest following schema sibling,
474 * otherwise return NULL - inserting at the end */
475 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
476 break;
477 }
478
Michal Vasko93b16062020-12-09 18:14:18 +0100479 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200480 }
481 } else {
482 /* find the anchor without hashes */
483 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200484 sparent = lysc_data_parent(new_node->schema);
485 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200486 /* we are in top-level, skip all the data from preceding modules */
487 LY_LIST_FOR(match, match) {
488 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
489 break;
490 }
491 }
492 }
493
494 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100495 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200496
497 found = 0;
498 LY_LIST_FOR(match, match) {
499 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
500 /* we have found an opaque node, which must be at the end, so use it OR
501 * modules do not match, so we must have traversed all the data from new_node module (if any),
502 * we have found the first node of the next module, that is what we want */
503 break;
504 }
505
506 /* skip schema nodes until we find the instantiated one */
507 while (!found) {
508 if (new_node->schema == schema) {
509 /* we have found the schema of the new node, continue search to find the first
510 * data node with a different schema (after our schema) */
511 found = 1;
512 break;
513 }
514 if (match->schema == schema) {
515 /* current node (match) is a data node still before the new node, continue search in data */
516 break;
517 }
Michal Vasko93b16062020-12-09 18:14:18 +0100518 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200519 assert(schema);
520 }
521
522 if (found && (match->schema != new_node->schema)) {
523 /* find the next node after we have found our node schema data instance */
524 break;
525 }
526 }
Michal Vasko90932a92020-02-12 14:33:03 +0100527 }
528
529 return match;
530}
531
Michal Vaskoc61dd062022-06-07 11:01:28 +0200532void
Michal Vaskof03ed032020-03-04 13:31:44 +0100533lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100534{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100535 struct lyd_node_inner *par;
536
Michal Vasko90932a92020-02-12 14:33:03 +0100537 assert(!node->next && (node->prev == node));
538
539 node->next = sibling->next;
540 node->prev = sibling;
541 sibling->next = node;
542 if (node->next) {
543 /* sibling had a succeeding node */
544 node->next->prev = node;
545 } else {
546 /* sibling was last, find first sibling and change its prev */
547 if (sibling->parent) {
548 sibling = sibling->parent->child;
549 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200550 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100551 }
552 sibling->prev = node;
553 }
554 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100555
Michal Vasko9f96a052020-03-10 09:41:45 +0100556 for (par = node->parent; par; par = par->parent) {
557 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
558 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100559 par->flags &= ~LYD_DEFAULT;
560 }
561 }
Michal Vasko90932a92020-02-12 14:33:03 +0100562}
563
Michal Vaskoc61dd062022-06-07 11:01:28 +0200564void
Michal Vaskof03ed032020-03-04 13:31:44 +0100565lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100566{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100567 struct lyd_node_inner *par;
568
Michal Vasko90932a92020-02-12 14:33:03 +0100569 assert(!node->next && (node->prev == node));
570
571 node->next = sibling;
572 /* covers situation of sibling being first */
573 node->prev = sibling->prev;
574 sibling->prev = node;
575 if (node->prev->next) {
576 /* sibling had a preceding node */
577 node->prev->next = node;
578 } else if (sibling->parent) {
579 /* sibling was first and we must also change parent child pointer */
580 sibling->parent->child = node;
581 }
582 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100583
Michal Vasko9f96a052020-03-10 09:41:45 +0100584 for (par = node->parent; par; par = par->parent) {
585 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
586 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100587 par->flags &= ~LYD_DEFAULT;
588 }
589 }
Michal Vasko90932a92020-02-12 14:33:03 +0100590}
591
592/**
Michal Vaskob104f112020-07-17 09:54:54 +0200593 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100594 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100595 * Handles inserting into NP containers and key-less lists.
596 *
Michal Vasko90932a92020-02-12 14:33:03 +0100597 * @param[in] parent Parent to insert into.
598 * @param[in] node Node to insert.
599 */
600static void
Michal Vaskob104f112020-07-17 09:54:54 +0200601lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100602{
603 struct lyd_node_inner *par;
604
Radek Krejcia1c1e542020-09-29 16:06:52 +0200605 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100606 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100607
608 par = (struct lyd_node_inner *)parent;
609
Michal Vaskob104f112020-07-17 09:54:54 +0200610 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100611 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100612
Michal Vaskod989ba02020-08-24 10:59:24 +0200613 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100614 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
615 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100616 par->flags &= ~LYD_DEFAULT;
617 }
618 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200619}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100620
Michal Vasko751cb4d2020-07-14 12:25:28 +0200621/**
622 * @brief Learn whether a list instance has all the keys.
623 *
624 * @param[in] list List instance to check.
625 * @return non-zero if all the keys were found,
626 * @return 0 otherwise.
627 */
628static int
629lyd_insert_has_keys(const struct lyd_node *list)
630{
631 const struct lyd_node *key;
632 const struct lysc_node *skey = NULL;
633
634 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200635 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200636 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
637 if (!key || (key->schema != skey)) {
638 /* key missing */
639 return 0;
640 }
641
642 key = key->next;
643 }
644
645 /* all keys found */
646 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100647}
648
649void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200650lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +0100651{
Michal Vaskob104f112020-07-17 09:54:54 +0200652 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100653
Michal Vaskob104f112020-07-17 09:54:54 +0200654 /* inserting list without its keys is not supported */
655 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100656 assert(!parent || !parent->schema ||
657 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100658
Michal Vaskob104f112020-07-17 09:54:54 +0200659 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100660 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100661 }
Michal Vasko90932a92020-02-12 14:33:03 +0100662
Michal Vaskob104f112020-07-17 09:54:54 +0200663 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100664 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100665
Michal Vasko9beceb82022-04-05 12:14:15 +0200666 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200667 /* no next anchor */
668 anchor = NULL;
669 } else {
670 /* find the anchor, our next node, so we can insert before it */
671 anchor = lyd_insert_get_next_anchor(first_sibling, node);
672 }
673
Michal Vaskob104f112020-07-17 09:54:54 +0200674 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200675 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200676 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100677 if (!parent && (*first_sibling_p == anchor)) {
678 /* move first sibling */
679 *first_sibling_p = node;
680 }
Michal Vaskob104f112020-07-17 09:54:54 +0200681 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200682 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200683 lyd_insert_after_node(first_sibling->prev, node);
684 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200685 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200686 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100687 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200688 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200689 *first_sibling_p = node;
690 }
691
692 /* insert into parent HT */
693 lyd_insert_hash(node);
694
695 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200696 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200697 lyd_hash(parent);
698
699 /* now we can insert even the list into its parent HT */
700 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100701 }
Michal Vasko90932a92020-02-12 14:33:03 +0100702}
703
Michal Vasko717a4c32020-12-07 09:40:10 +0100704/**
705 * @brief Check schema place of a node to be inserted.
706 *
707 * @param[in] parent Schema node of the parent data node.
708 * @param[in] sibling Schema node of a sibling data node.
709 * @param[in] schema Schema node if the data node to be inserted.
710 * @return LY_SUCCESS on success.
711 * @return LY_EINVAL if the place is invalid.
712 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100713static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100714lyd_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 +0100715{
716 const struct lysc_node *par2;
717
Michal Vasko62ed12d2020-05-21 10:08:25 +0200718 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100719 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
720 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100721
Michal Vasko717a4c32020-12-07 09:40:10 +0100722 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100723 /* opaque nodes can be inserted wherever */
724 return LY_SUCCESS;
725 }
726
Michal Vasko717a4c32020-12-07 09:40:10 +0100727 if (!parent) {
728 parent = lysc_data_parent(sibling);
729 }
730
Michal Vaskof03ed032020-03-04 13:31:44 +0100731 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200732 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100733
734 if (parent) {
735 /* inner node */
736 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200737 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200738 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100739 return LY_EINVAL;
740 }
741 } else {
742 /* top-level node */
743 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200744 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100745 return LY_EINVAL;
746 }
747 }
748
749 return LY_SUCCESS;
750}
751
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100752LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200753lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100754{
755 struct lyd_node *iter;
756
Michal Vasko0ab974d2021-02-24 13:18:26 +0100757 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100758 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100759
Michal Vasko717a4c32020-12-07 09:40:10 +0100760 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100761
Michal Vasko0ab974d2021-02-24 13:18:26 +0100762 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100763 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100764 return LY_EINVAL;
765 }
766
767 if (node->parent || node->prev->next) {
768 lyd_unlink_tree(node);
769 }
770
771 while (node) {
772 iter = node->next;
773 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200774 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100775 node = iter;
776 }
777 return LY_SUCCESS;
778}
779
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100780LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200781lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100782{
783 struct lyd_node *iter;
784
785 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
786 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
787
788 if (first->schema && (first->schema->flags & LYS_KEY)) {
789 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
790 return LY_EINVAL;
791 }
792
793 while (first) {
794 iter = first->next;
795 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100796 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100797 first = iter;
798 }
799 return LY_SUCCESS;
800}
801
802LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200803lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100804{
805 struct lyd_node *iter;
806
Michal Vaskob104f112020-07-17 09:54:54 +0200807 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100808
Michal Vaskob104f112020-07-17 09:54:54 +0200809 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100810 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100811 }
812
Michal Vasko553d0b52020-12-04 16:27:52 +0100813 if (sibling == node) {
814 /* we need to keep the connection to siblings so we can insert into them */
815 sibling = sibling->prev;
816 }
817
Michal Vaskob1b5c262020-03-05 14:29:47 +0100818 if (node->parent || node->prev->next) {
819 lyd_unlink_tree(node);
820 }
821
822 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100823 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200824 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200825 return LY_EINVAL;
826 }
827
Michal Vaskob1b5c262020-03-05 14:29:47 +0100828 iter = node->next;
829 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200830 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100831 node = iter;
832 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100833
Michal Vaskob104f112020-07-17 09:54:54 +0200834 if (first) {
835 /* find the first sibling */
836 *first = sibling;
837 while ((*first)->prev->next) {
838 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100839 }
840 }
841
842 return LY_SUCCESS;
843}
844
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100845LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100846lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
847{
Michal Vaskobce230b2022-04-19 09:55:31 +0200848 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100849 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100850
Michal Vasko717a4c32020-12-07 09:40:10 +0100851 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100852
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200853 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200854 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100855 return LY_EINVAL;
856 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200857 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
858 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100859 return LY_EINVAL;
860 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100861
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100862 lyd_unlink_tree(node);
863 lyd_insert_before_node(sibling, node);
864 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100865
Michal Vaskof03ed032020-03-04 13:31:44 +0100866 return LY_SUCCESS;
867}
868
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100869LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100870lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
871{
Michal Vaskobce230b2022-04-19 09:55:31 +0200872 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100873 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100874
Michal Vasko717a4c32020-12-07 09:40:10 +0100875 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100876
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200877 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200878 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100879 return LY_EINVAL;
880 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200881 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
882 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100883 return LY_EINVAL;
884 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100885
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100886 lyd_unlink_tree(node);
887 lyd_insert_after_node(sibling, node);
888 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100889
Michal Vaskof03ed032020-03-04 13:31:44 +0100890 return LY_SUCCESS;
891}
892
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100893LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200894lyd_unlink_siblings(struct lyd_node *node)
895{
896 struct lyd_node *next, *elem, *first = NULL;
897
898 LY_LIST_FOR_SAFE(node, next, elem) {
899 lyd_unlink_tree(elem);
900 lyd_insert_node(NULL, &first, elem, 1);
901 }
902}
903
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100904LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100905lyd_unlink_tree(struct lyd_node *node)
906{
907 struct lyd_node *iter;
908
909 if (!node) {
910 return;
911 }
912
Michal Vaskob104f112020-07-17 09:54:54 +0200913 /* update hashes while still linked into the tree */
914 lyd_unlink_hash(node);
915
Michal Vaskof03ed032020-03-04 13:31:44 +0100916 /* unlink from siblings */
917 if (node->prev->next) {
918 node->prev->next = node->next;
919 }
920 if (node->next) {
921 node->next->prev = node->prev;
922 } else {
923 /* unlinking the last node */
924 if (node->parent) {
925 iter = node->parent->child;
926 } else {
927 iter = node->prev;
928 while (iter->prev != node) {
929 iter = iter->prev;
930 }
931 }
932 /* update the "last" pointer from the first node */
933 iter->prev = node->prev;
934 }
935
936 /* unlink from parent */
937 if (node->parent) {
938 if (node->parent->child == node) {
939 /* the node is the first child */
940 node->parent->child = node->next;
941 }
942
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200943 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100944 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200945
Michal Vaskof03ed032020-03-04 13:31:44 +0100946 node->parent = NULL;
947 }
948
949 node->next = NULL;
950 node->prev = node;
951}
952
Michal Vaskoa5da3292020-08-12 13:10:50 +0200953void
Michal Vasko871a0252020-11-11 18:35:24 +0100954lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200955{
956 struct lyd_meta *last, *iter;
957
958 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200959
960 if (!meta) {
961 return;
962 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200963
964 for (iter = meta; iter; iter = iter->next) {
965 iter->parent = parent;
966 }
967
968 /* insert as the last attribute */
969 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200970 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200971 last->next = meta;
972 } else {
973 parent->meta = meta;
974 }
975
976 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100977 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200978 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100979 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200980 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200981}
982
983LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100984lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200985 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +0100986 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 +0100987{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100988 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100989 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200990 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100991 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200992 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100993
Michal Vasko9f96a052020-03-10 09:41:45 +0100994 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100995
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200996 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200997 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200998 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100999 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001000 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001001 break;
1002 }
1003 }
1004 if (!ant) {
1005 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001006 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001007 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 ret = LY_EINVAL;
1009 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001010 }
1011
Michal Vasko9f96a052020-03-10 09:41:45 +01001012 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001013 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001014 mt->parent = parent;
1015 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001016 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001017 ret = lyd_value_store(mod->ctx, &mt->value, ant_type, value, value_len, dynamic, format, prefix_data, hints,
Michal Vaskoddd76592022-01-17 13:34:48 +01001018 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001019 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1020 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1021 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001022
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001023 /* insert as the last attribute */
1024 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001025 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001026 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001027 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001028 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001029 }
1030
Michal Vasko9f96a052020-03-10 09:41:45 +01001031 if (meta) {
1032 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001033 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001034
1035cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001036 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001037}
1038
Michal Vaskoa5da3292020-08-12 13:10:50 +02001039void
1040lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1041{
1042 struct lyd_attr *last, *iter;
1043 struct lyd_node_opaq *opaq;
1044
1045 assert(parent && !parent->schema);
1046
1047 if (!attr) {
1048 return;
1049 }
1050
1051 opaq = (struct lyd_node_opaq *)parent;
1052 for (iter = attr; iter; iter = iter->next) {
1053 iter->parent = opaq;
1054 }
1055
1056 /* insert as the last attribute */
1057 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001058 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001059 last->next = attr;
1060 } else {
1061 opaq->attr = attr;
1062 }
1063}
1064
Michal Vasko52927e22020-03-16 17:26:14 +01001065LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001066lyd_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 +01001067 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 +02001068 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 +01001069{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001070 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001071 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001072
1073 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001074 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001075
Michal Vasko2a3722d2021-06-16 11:52:39 +02001076 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001077 value = "";
1078 }
1079
1080 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001081 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001082
Michal Vaskoad92b672020-11-12 13:11:31 +01001083 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001084 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001085 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001086 }
1087 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001088 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001089 }
1090
Michal Vasko52927e22020-03-16 17:26:14 +01001091 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001092 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1093 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001094 *dynamic = 0;
1095 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001096 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001097 }
Michal Vasko501af032020-11-11 20:27:44 +01001098 at->format = format;
1099 at->val_prefix_data = val_prefix_data;
1100 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001101
1102 /* insert as the last attribute */
1103 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001104 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001105 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001106 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001107 last->next = at;
1108 }
1109
Radek Krejci011e4aa2020-09-04 15:22:31 +02001110finish:
1111 if (ret) {
1112 lyd_free_attr_single(ctx, at);
1113 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001114 *attr = at;
1115 }
1116 return LY_SUCCESS;
1117}
1118
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001119LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001120lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001121{
Michal Vasko90189962023-02-28 12:10:34 +01001122 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001123
Michal Vasko90189962023-02-28 12:10:34 +01001124 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001125
Michal Vasko004d3152020-06-11 19:59:22 +02001126 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001127}
1128
aPiecek2f63f952021-03-30 12:22:18 +02001129/**
1130 * @brief Check the equality of the two schemas from different contexts.
1131 *
1132 * @param schema1 of first node.
1133 * @param schema2 of second node.
1134 * @return 1 if the schemas are equal otherwise 0.
1135 */
1136static ly_bool
1137lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1138{
1139 if (!schema1 && !schema2) {
1140 return 1;
1141 } else if (!schema1 || !schema2) {
1142 return 0;
1143 }
1144
1145 assert(schema1->module->ctx != schema2->module->ctx);
1146
1147 if (schema1->nodetype != schema2->nodetype) {
1148 return 0;
1149 }
1150
1151 if (strcmp(schema1->name, schema2->name)) {
1152 return 0;
1153 }
1154
1155 if (strcmp(schema1->module->name, schema2->module->name)) {
1156 return 0;
1157 }
1158
aPiecek2f63f952021-03-30 12:22:18 +02001159 return 1;
1160}
1161
1162/**
1163 * @brief Check the equality of the schemas for all parent nodes.
1164 *
1165 * Both nodes must be from different contexts.
1166 *
1167 * @param node1 Data of first node.
1168 * @param node2 Data of second node.
1169 * @return 1 if the all related parental schemas are equal otherwise 0.
1170 */
1171static ly_bool
1172lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1173{
1174 const struct lysc_node *parent1, *parent2;
1175
1176 assert(node1 && node2);
1177
1178 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1179 parent1 && parent2;
1180 parent1 = parent1->parent, parent2 = parent2->parent) {
1181 if (!lyd_compare_schema_equal(parent1, parent2)) {
1182 return 0;
1183 }
1184 }
1185
1186 if (parent1 || parent2) {
1187 return 0;
1188 }
1189
1190 return 1;
1191}
1192
1193/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001194 * @brief Compare 2 nodes values including opaque node values.
1195 *
1196 * @param[in] node1 First node to compare.
1197 * @param[in] node2 Second node to compare.
1198 * @return LY_SUCCESS if equal.
1199 * @return LY_ENOT if not equal.
1200 * @return LY_ERR on error.
1201 */
1202static LY_ERR
1203lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1204{
1205 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1206 const char *val1, *val2, *col;
1207 const struct lys_module *mod;
1208 char *val_dyn = NULL;
1209 LY_ERR rc = LY_SUCCESS;
1210
1211 if (!node1->schema) {
1212 opaq1 = (struct lyd_node_opaq *)node1;
1213 }
1214 if (!node2->schema) {
1215 opaq2 = (struct lyd_node_opaq *)node2;
1216 }
1217
1218 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1219 /* opaque XML and opaque XML node */
1220 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1221 opaq2->val_prefix_data)) {
1222 return LY_ENOT;
1223 }
1224 return LY_SUCCESS;
1225 }
1226
1227 /* get their values */
1228 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1229 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1230 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1231 if (!mod) {
1232 /* unable to compare */
1233 return LY_ENOT;
1234 }
1235
1236 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1237 LOGMEM(LYD_CTX(node1));
1238 return LY_EMEM;
1239 }
1240 val1 = val_dyn;
1241 } else {
1242 val1 = lyd_get_value(node1);
1243 }
1244 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1245 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1246 if (!mod) {
1247 return LY_ENOT;
1248 }
1249
1250 assert(!val_dyn);
1251 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1252 LOGMEM(LYD_CTX(node2));
1253 return LY_EMEM;
1254 }
1255 val2 = val_dyn;
1256 } else {
1257 val2 = lyd_get_value(node2);
1258 }
1259
1260 /* compare values */
1261 if (strcmp(val1, val2)) {
1262 rc = LY_ENOT;
1263 }
1264
1265 free(val_dyn);
1266 return rc;
1267}
1268
1269/**
Michal Vaskof277d362023-04-24 09:08:31 +02001270 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1271 *
1272 * Works correctly even if @p node1 and @p node2 have different contexts.
1273 *
1274 * @param[in] node1 The first node to compare.
1275 * @param[in] node2 The second node to compare.
1276 * @param[in] options Various @ref datacompareoptions.
1277 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1278 * @return LY_SUCCESS if the nodes are equivalent.
1279 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001280 */
1281static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001282lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001283 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001284{
aPiecek2f63f952021-03-30 12:22:18 +02001285 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1286 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001287 if (options & LYD_COMPARE_OPAQ) {
1288 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1289 return LY_ENOT;
1290 }
1291 } else {
1292 if (node1->schema != node2->schema) {
1293 return LY_ENOT;
1294 }
aPiecek2f63f952021-03-30 12:22:18 +02001295 }
1296 } else {
1297 /* different contexts */
1298 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1299 return LY_ENOT;
1300 }
1301 if (!parental_schemas_checked) {
1302 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1303 return LY_ENOT;
1304 }
1305 parental_schemas_checked = 1;
1306 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001307 }
1308
Michal Vaskof277d362023-04-24 09:08:31 +02001309 return LY_SUCCESS;
1310}
1311
1312/**
1313 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1314 *
1315 * Works correctly even if @p node1 and @p node2 have different contexts.
1316 *
1317 * @param[in] node1 The first node to compare.
1318 * @param[in] node2 The second node to compare.
1319 * @param[in] options Various @ref datacompareoptions.
1320 * @return LY_SUCCESS if the nodes are equivalent.
1321 * @return LY_ENOT if the nodes are not equivalent.
1322 */
1323static LY_ERR
1324lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1325{
1326 const struct lyd_node *iter1, *iter2;
1327 struct lyd_node_any *any1, *any2;
1328 int len1, len2;
1329 LY_ERR r;
1330
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001331 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001332 return LY_ENOT;
1333 }
aPiecek2f63f952021-03-30 12:22:18 +02001334 /* 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 +02001335
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001336 if (!node1->schema || !node2->schema) {
1337 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001338 return LY_ENOT;
1339 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001340 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1341 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1342 /* compare values only if there are any to compare */
1343 if ((r = lyd_compare_single_value(node1, node2))) {
1344 return r;
1345 }
Michal Vasko52927e22020-03-16 17:26:14 +01001346 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001347
Michal Vasko52927e22020-03-16 17:26:14 +01001348 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001349 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001350 }
1351 return LY_SUCCESS;
1352 } else {
1353 switch (node1->schema->nodetype) {
1354 case LYS_LEAF:
1355 case LYS_LEAFLIST:
1356 if (options & LYD_COMPARE_DEFAULTS) {
1357 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1358 return LY_ENOT;
1359 }
1360 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001361 if ((r = lyd_compare_single_value(node1, node2))) {
1362 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001363 }
1364
aPiecek2f63f952021-03-30 12:22:18 +02001365 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001366 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001367 case LYS_RPC:
1368 case LYS_ACTION:
1369 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001370 if (options & LYD_COMPARE_DEFAULTS) {
1371 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1372 return LY_ENOT;
1373 }
1374 }
1375 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001376 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001377 }
1378 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001379 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001380 iter1 = lyd_child(node1);
1381 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001382
1383 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1384 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001385 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001386 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001387 key = key->next) {
Michal Vasko642eb922023-04-24 11:21:40 +02001388 if (!iter1 || !iter2) {
1389 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
Michal Vasko52927e22020-03-16 17:26:14 +01001390 }
Michal Vaskof277d362023-04-24 09:08:31 +02001391 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1392 LY_CHECK_RET(r);
1393 r = lyd_compare_single_data(iter1, iter2, options);
1394 LY_CHECK_RET(r);
1395
Michal Vasko52927e22020-03-16 17:26:14 +01001396 iter1 = iter1->next;
1397 iter2 = iter2->next;
1398 }
Michal Vaskof277d362023-04-24 09:08:31 +02001399
1400 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001401 } else {
1402 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
Michal Vaskof277d362023-04-24 09:08:31 +02001403 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001404 }
Michal Vasko52927e22020-03-16 17:26:14 +01001405 case LYS_ANYXML:
1406 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001407 any1 = (struct lyd_node_any *)node1;
1408 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001409
1410 if (any1->value_type != any2->value_type) {
1411 return LY_ENOT;
1412 }
1413 switch (any1->value_type) {
1414 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001415 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001416 case LYD_ANYDATA_STRING:
1417 case LYD_ANYDATA_XML:
1418 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001419 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1420 return LY_ENOT;
1421 } else if (!any1->value.str && !any2->value.str) {
1422 return LY_SUCCESS;
1423 }
Michal Vasko52927e22020-03-16 17:26:14 +01001424 len1 = strlen(any1->value.str);
1425 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001426 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001427 return LY_ENOT;
1428 }
1429 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001430 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001431 len1 = lyd_lyb_data_length(any1->value.mem);
1432 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001433 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001434 return LY_ENOT;
1435 }
1436 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001437 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001438 }
1439 }
1440
Michal Vaskob7be7a82020-08-20 09:09:04 +02001441 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001442 return LY_EINT;
1443}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001444
Michal Vaskof277d362023-04-24 09:08:31 +02001445/**
1446 * @brief Compare all siblings at a node level.
1447 *
1448 * @param[in] node1 First sibling list.
1449 * @param[in] node2 Second sibling list.
1450 * @param[in] options Various @ref datacompareoptions.
1451 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1452 * @return LY_SUCCESS if equal.
1453 * @return LY_ENOT if not equal.
1454 * @return LY_ERR on error.
1455 */
1456static LY_ERR
1457lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1458 ly_bool parental_schemas_checked)
1459{
1460 LY_ERR r;
1461 const struct lyd_node *iter2;
1462
1463 while (node1 && node2) {
1464 /* schema match */
1465 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1466 LY_CHECK_RET(r);
1467
1468 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1469 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1470 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1471 /* find a matching instance in case they are ordered differently */
1472 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1473 if (r == LY_ENOTFOUND) {
1474 /* no matching instance, data not equal */
1475 r = LY_ENOT;
1476 }
1477 LY_CHECK_RET(r);
1478 } else {
1479 /* compare with the current node */
1480 iter2 = node2;
1481 }
1482
1483 /* data match */
1484 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1485 LY_CHECK_RET(r);
1486
1487 node1 = node1->next;
1488 node2 = node2->next;
1489 }
1490
1491 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1492}
1493
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001494LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001495lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1496{
Michal Vaskof277d362023-04-24 09:08:31 +02001497 LY_ERR r;
1498
1499 if (!node1 || !node2) {
1500 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1501 }
1502
1503 /* schema match */
1504 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1505 return r;
1506 }
1507
1508 /* data match */
1509 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001510}
1511
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001512LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001513lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001514{
Michal Vaskof277d362023-04-24 09:08:31 +02001515 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001516}
1517
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001518LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001519lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1520{
1521 if (!meta1 || !meta2) {
1522 if (meta1 == meta2) {
1523 return LY_SUCCESS;
1524 } else {
1525 return LY_ENOT;
1526 }
1527 }
1528
Michal Vaskoa8083062020-11-06 17:22:10 +01001529 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001530 return LY_ENOT;
1531 }
1532
Michal Vasko21725742020-06-29 11:49:25 +02001533 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1534}
1535
Radek Krejci22ebdba2019-07-25 13:59:43 +02001536/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001537 * @brief Create a copy of the attribute.
1538 *
1539 * @param[in] attr Attribute to copy.
1540 * @param[in] node Opaque where to append the new attribute.
1541 * @param[out] dup Optional created attribute copy.
1542 * @return LY_ERR value.
1543 */
1544static LY_ERR
1545lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1546{
1547 LY_ERR ret = LY_SUCCESS;
1548 struct lyd_attr *a, *last;
1549 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1550
1551 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1552
1553 /* create a copy */
1554 a = calloc(1, sizeof *attr);
1555 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1556
1557 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1558 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1559 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1560 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1561 a->hints = attr->hints;
1562 a->format = attr->format;
1563 if (attr->val_prefix_data) {
1564 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1565 LY_CHECK_GOTO(ret, finish);
1566 }
1567
1568 /* insert as the last attribute */
1569 a->parent = opaq;
1570 if (opaq->attr) {
1571 for (last = opaq->attr; last->next; last = last->next) {}
1572 last->next = a;
1573 } else {
1574 opaq->attr = a;
1575 }
1576
1577finish:
1578 if (ret) {
1579 lyd_free_attr_single(LYD_CTX(node), a);
1580 } else if (dup) {
1581 *dup = a;
1582 }
1583 return LY_SUCCESS;
1584}
1585
1586/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001587 * @brief Find @p schema equivalent in @p trg_ctx.
1588 *
1589 * @param[in] schema Schema node to find.
1590 * @param[in] trg_ctx Target context to search in.
1591 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001592 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001593 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1594 * @return LY_RRR value.
1595 */
1596static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001597lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1598 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001599{
Michal Vasko9beceb82022-04-05 12:14:15 +02001600 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1601 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001602 char *path;
1603
1604 if (!schema) {
1605 /* opaque node */
1606 *trg_schema = NULL;
1607 return LY_SUCCESS;
1608 }
1609
Michal Vasko9beceb82022-04-05 12:14:15 +02001610 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001611 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001612 trg_parent = parent->schema;
1613 src_parent = lysc_data_parent(schema);
1614 }
1615
1616 do {
1617 /* find the next parent */
1618 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001619 while (lysc_data_parent(sp) != src_parent) {
1620 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001621 }
1622 src_parent = sp;
1623
1624 if (!src_parent->parent) {
1625 /* find the module first */
1626 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1627 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001628 if (log) {
1629 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1630 src_parent->module->name);
1631 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001632 return LY_ENOTFOUND;
1633 }
1634 }
1635
1636 /* find the next parent */
1637 assert(trg_parent || trg_mod);
1638 tp = NULL;
1639 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1640 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1641 break;
1642 }
1643 }
1644 if (!tp) {
1645 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001646 if (log) {
1647 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1648 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1649 free(path);
1650 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001651 return LY_ENOTFOUND;
1652 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001653
Michal Vasko9beceb82022-04-05 12:14:15 +02001654 trg_parent = tp;
1655 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001656
Michal Vasko9beceb82022-04-05 12:14:15 +02001657 /* success */
1658 *trg_schema = trg_parent;
1659 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001660}
1661
1662/**
Michal Vasko52927e22020-03-16 17:26:14 +01001663 * @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 +02001664 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001665 * Ignores ::LYD_DUP_WITH_PARENTS and ::LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001666 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001667 * @param[in] node Node to duplicate.
1668 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001669 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001670 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1671 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001672 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1673 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001674 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1675 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001676 */
Michal Vasko52927e22020-03-16 17:26:14 +01001677static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001678lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1679 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001680{
Michal Vasko52927e22020-03-16 17:26:14 +01001681 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001682 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001683 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001684 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001685 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001686 const struct lysc_type *type;
1687 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001688
Michal Vasko52927e22020-03-16 17:26:14 +01001689 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001690
Michal Vasko19175b62022-04-01 09:17:07 +02001691 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001692 if (options & LYD_DUP_NO_EXT) {
1693 /* no not duplicate this subtree */
1694 return LY_SUCCESS;
1695 }
1696
Michal Vasko19175b62022-04-01 09:17:07 +02001697 /* we need to use the same context */
1698 trg_ctx = LYD_CTX(node);
1699 }
1700
Michal Vasko52927e22020-03-16 17:26:14 +01001701 if (!node->schema) {
1702 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001703 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001704 } else {
1705 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001706 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001707 case LYS_ACTION:
1708 case LYS_NOTIF:
1709 case LYS_CONTAINER:
1710 case LYS_LIST:
1711 dup = calloc(1, sizeof(struct lyd_node_inner));
1712 break;
1713 case LYS_LEAF:
1714 case LYS_LEAFLIST:
1715 dup = calloc(1, sizeof(struct lyd_node_term));
1716 break;
1717 case LYS_ANYDATA:
1718 case LYS_ANYXML:
1719 dup = calloc(1, sizeof(struct lyd_node_any));
1720 break;
1721 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001722 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001723 ret = LY_EINT;
1724 goto error;
1725 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001726 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001727 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001728
Michal Vaskof6df0a02020-06-16 13:08:34 +02001729 if (options & LYD_DUP_WITH_FLAGS) {
1730 dup->flags = node->flags;
1731 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001732 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001733 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001734 if (trg_ctx == LYD_CTX(node)) {
1735 dup->schema = node->schema;
1736 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001737 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001738 if (ret) {
1739 /* has no schema but is not an opaque node */
1740 free(dup);
1741 dup = NULL;
1742 goto error;
1743 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001744 }
Michal Vasko52927e22020-03-16 17:26:14 +01001745 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001746
Michal Vasko9cf62422021-07-01 13:29:32 +02001747 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001748 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001749 if (!node->schema) {
1750 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1751 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1752 }
1753 } else {
1754 LY_LIST_FOR(node->meta, meta) {
1755 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1756 }
Michal Vasko25a32822020-07-09 15:48:22 +02001757 }
1758 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001759
1760 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001761 if (!dup->schema) {
1762 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1763 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1764 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001765
1766 if (options & LYD_DUP_RECURSIVE) {
1767 /* duplicate all the children */
1768 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001769 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001770 }
1771 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001772 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1773 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1774 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1775 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001776 opaq->hints = orig->hints;
1777 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001778 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001779 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001780 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001781 }
Michal Vasko52927e22020-03-16 17:26:14 +01001782 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1783 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1784 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1785
1786 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001787 if (trg_ctx == LYD_CTX(node)) {
1788 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1789 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1790 } else {
1791 /* store canonical value in the target context */
1792 val_can = lyd_get_value(node);
1793 type = ((struct lysc_node_leaf *)term->schema)->type;
1794 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1795 LYD_HINT_DATA, term->schema, NULL);
1796 LY_CHECK_GOTO(ret, error);
1797 }
Michal Vasko52927e22020-03-16 17:26:14 +01001798 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1799 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1800 struct lyd_node *child;
1801
1802 if (options & LYD_DUP_RECURSIVE) {
1803 /* duplicate all the children */
1804 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001805 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001806 }
Michal Vasko69730152020-10-09 16:30:07 +02001807 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001808 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001809 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001810 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001811 }
1812 }
1813 lyd_hash(dup);
1814 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001815 dup->hash = node->hash;
1816 any = (struct lyd_node_any *)node;
1817 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001818 }
1819
Michal Vasko52927e22020-03-16 17:26:14 +01001820 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001821 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001822
1823 if (dup_p) {
1824 *dup_p = dup;
1825 }
1826 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001827
1828error:
Michal Vasko52927e22020-03-16 17:26:14 +01001829 lyd_free_tree(dup);
1830 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001831}
1832
Michal Vasko29d674b2021-08-25 11:18:35 +02001833/**
1834 * @brief Get a parent node to connect duplicated subtree to.
1835 *
1836 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001837 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001838 * @param[in] parent Initial parent to connect to.
1839 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1840 * @param[out] dup_parent First duplicated parent node, if any.
1841 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1842 * @return LY_ERR value.
1843 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001844static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001845lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1846 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001847{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001848 const struct lyd_node_inner *orig_parent, *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001849 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001850
1851 *dup_parent = NULL;
1852 *local_parent = NULL;
1853
Michal Vasko83522192022-07-20 08:07:34 +02001854 if (node->flags & LYD_EXT) {
1855 ext_parent = 1;
1856 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001857 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
Michal Vasko83522192022-07-20 08:07:34 +02001858 if (ext_parent) {
1859 /* use the standard context */
1860 trg_ctx = LYD_CTX(orig_parent);
1861 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001862 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001863 /* stop creating parents, connect what we have into the provided parent */
1864 iter = parent;
1865 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001866 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1867 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
1868 lyd_compare_schema_parents_equal(&parent->node, &orig_parent->node)) {
1869 iter = parent;
1870 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001871 } else {
1872 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001873 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options,
Radek Krejci0f969882020-08-21 16:56:47 +02001874 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001875 }
1876 if (!*local_parent) {
1877 *local_parent = (struct lyd_node_inner *)iter;
1878 }
1879 if (iter->child) {
1880 /* 1) list - add after keys
1881 * 2) provided parent with some children */
1882 iter->child->prev->next = *dup_parent;
1883 if (*dup_parent) {
1884 (*dup_parent)->prev = iter->child->prev;
1885 iter->child->prev = *dup_parent;
1886 }
1887 } else {
1888 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1889 }
1890 if (*dup_parent) {
1891 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1892 }
1893 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001894 if (orig_parent->flags & LYD_EXT) {
1895 ext_parent = 1;
1896 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001897 }
1898
1899 if (repeat && parent) {
1900 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001901 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001902 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001903 return LY_EINVAL;
1904 }
1905
1906 return LY_SUCCESS;
1907}
1908
1909static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001910lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1911 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001912{
1913 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001914 const struct lyd_node *orig; /* original node to be duplicated */
1915 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001916 struct lyd_node *top = NULL; /* the most higher created node */
1917 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001918
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001919 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001920
1921 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001922 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 +02001923 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001924 } else {
1925 local_parent = parent;
1926 }
1927
Radek Krejci22ebdba2019-07-25 13:59:43 +02001928 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001929 if (lysc_is_key(orig->schema)) {
1930 if (local_parent) {
1931 /* the key must already exist in the parent */
1932 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001933 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001934 } else {
1935 assert(!(options & LYD_DUP_WITH_PARENTS));
1936 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001937 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001938 LY_CHECK_GOTO(rc, error);
1939 }
1940 } else {
1941 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001942 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1943 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001944 LY_CHECK_GOTO(rc, error);
1945 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001946 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001947 break;
1948 }
1949 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001950
Michal Vasko3a41dff2020-07-15 14:30:28 +02001951 if (dup) {
1952 *dup = first;
1953 }
1954 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001955
1956error:
1957 if (top) {
1958 lyd_free_tree(top);
1959 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001960 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001961 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001962 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001963}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001964
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001965/**
1966 * @brief Check the context of node and parent when duplicating nodes.
1967 *
1968 * @param[in] node Node to duplicate.
1969 * @param[in] parent Parent of the duplicated node(s).
1970 * @return LY_ERR value.
1971 */
1972static LY_ERR
1973lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1974{
1975 const struct lyd_node *iter;
1976
1977 if (!node || !parent) {
1978 return LY_SUCCESS;
1979 }
1980
1981 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1982 /* try to find top-level ext data parent */
1983 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1984
1985 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001986 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001987 return LY_EINVAL;
1988 }
1989 }
1990
1991 return LY_SUCCESS;
1992}
1993
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001994LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001995lyd_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 +02001996{
Michal Vaskoddd76592022-01-17 13:34:48 +01001997 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001998 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001999
2000 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
2001}
2002
2003LIBYANG_API_DEF LY_ERR
2004lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2005 uint32_t options, struct lyd_node **dup)
2006{
2007 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2008
2009 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002010}
2011
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002012LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002013lyd_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 +02002014{
Michal Vaskoddd76592022-01-17 13:34:48 +01002015 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002016 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002017
2018 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
2019}
2020
2021LIBYANG_API_DEF LY_ERR
2022lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2023 uint32_t options, struct lyd_node **dup)
2024{
2025 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2026
2027 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002028}
2029
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002030LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002031lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002032{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002033 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002034 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002035 struct lyd_meta *mt, *last;
2036
Michal Vasko3a41dff2020-07-15 14:30:28 +02002037 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002038
Michal Vasko33c48972022-07-20 10:28:07 +02002039 /* log to node context but value must always use the annotation context */
2040 ctx = meta->annotation->module->ctx;
2041
Michal Vasko25a32822020-07-09 15:48:22 +02002042 /* create a copy */
2043 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002044 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002045 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002046 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002047 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002048 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002049
2050 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002051 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002052 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002053 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002054 last->next = mt;
2055 } else {
2056 node->meta = mt;
2057 }
2058
Radek Krejci011e4aa2020-09-04 15:22:31 +02002059finish:
2060 if (ret) {
2061 lyd_free_meta_single(mt);
2062 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002063 *dup = mt;
2064 }
2065 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002066}
2067
Michal Vasko4490d312020-06-16 13:08:55 +02002068/**
2069 * @brief Merge a source sibling into target siblings.
2070 *
2071 * @param[in,out] first_trg First target sibling, is updated if top-level.
2072 * @param[in] parent_trg Target parent.
2073 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002074 * @param[in] merge_cb Optional merge callback.
2075 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002076 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002077 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002078 * @return LY_ERR value.
2079 */
2080static LY_ERR
2081lyd_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 +02002082 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002083{
Michal Vasko4490d312020-06-16 13:08:55 +02002084 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002085 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002086 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002087 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002088 struct ly_ht *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002089 LY_ERR ret;
2090 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002091
2092 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002093 if (!sibling_src->schema) {
2094 /* try to find the same opaque node */
2095 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2096 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002097 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002098 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002099 } else {
2100 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002101 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002102 }
2103
Michal Vaskocd3f6172021-05-18 16:14:50 +02002104 if (match_trg) {
2105 /* update match as needed */
2106 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2107 } else {
2108 /* first instance of this node */
2109 first_inst = 1;
2110 }
2111
2112 if (match_trg) {
2113 /* call callback */
2114 if (merge_cb) {
2115 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2116 }
2117
Michal Vasko4490d312020-06-16 13:08:55 +02002118 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002119 if (!match_trg->schema) {
2120 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2121 /* update value */
2122 opaq_trg = (struct lyd_node_opaq *)match_trg;
2123 opaq_src = (struct lyd_node_opaq *)sibling_src;
2124
2125 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2126 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2127 opaq_trg->hints = opaq_src->hints;
2128
2129 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2130 opaq_trg->format = opaq_src->format;
2131 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2132 &opaq_trg->val_prefix_data);
2133 }
2134 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2135 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002136 /* since they are different, they cannot both be default */
2137 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2138
Michal Vasko3a41dff2020-07-15 14:30:28 +02002139 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2140 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002141 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002142 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2143 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002144 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002145
2146 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002147 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002148 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002149 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002150 /* update value */
2151 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002152 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002153
2154 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002155 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002156 }
2157
2158 /* check descendants, recursively */
2159 ret = LY_SUCCESS;
2160 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2161 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2162 &child_dup_inst);
2163 if (ret) {
2164 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002165 }
2166 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002167 lyd_dup_inst_free(child_dup_inst);
2168 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002169 } else {
2170 /* node not found, merge it */
2171 if (options & LYD_MERGE_DESTRUCT) {
2172 dup_src = (struct lyd_node *)sibling_src;
2173 lyd_unlink_tree(dup_src);
2174 /* spend it */
2175 *sibling_src_p = NULL;
2176 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002177 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 +02002178 }
2179
Michal Vasko7e8315b2021-08-03 17:01:06 +02002180 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2181 /* set LYD_NEW for all the new nodes, required for validation */
2182 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2183 elem->flags |= LYD_NEW;
2184 LYD_TREE_DFS_END(dup_src, elem);
2185 }
Michal Vasko4490d312020-06-16 13:08:55 +02002186 }
2187
Michal Vaskocd3f6172021-05-18 16:14:50 +02002188 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002189 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002190
2191 if (first_inst) {
2192 /* remember not to find this instance next time */
2193 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2194 }
2195
2196 /* call callback, no source node */
2197 if (merge_cb) {
2198 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2199 }
Michal Vasko4490d312020-06-16 13:08:55 +02002200 }
2201
2202 return LY_SUCCESS;
2203}
2204
Michal Vasko3a41dff2020-07-15 14:30:28 +02002205static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002206lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2207 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002208{
2209 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002210 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002211 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002212 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002213
2214 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002215 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2216 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002217
2218 if (!source) {
2219 /* nothing to merge */
2220 return LY_SUCCESS;
2221 }
2222
Michal Vasko831422b2020-11-24 11:20:51 +01002223 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002224 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002225 return LY_EINVAL;
2226 }
2227
2228 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002229 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2230 /* skip data nodes from different modules */
2231 continue;
2232 }
2233
Radek Krejci857189e2020-09-01 13:26:36 +02002234 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002235 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2236 if (ret) {
2237 break;
2238 }
Michal Vasko4490d312020-06-16 13:08:55 +02002239 if (first && !sibling_src) {
2240 /* source was spent (unlinked), move to the next node */
2241 source = tmp;
2242 }
2243
Michal Vasko3a41dff2020-07-15 14:30:28 +02002244 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002245 break;
2246 }
2247 }
2248
2249 if (options & LYD_MERGE_DESTRUCT) {
2250 /* free any leftover source data that were not merged */
2251 lyd_free_siblings((struct lyd_node *)source);
2252 }
2253
Michal Vaskocd3f6172021-05-18 16:14:50 +02002254 lyd_dup_inst_free(dup_inst);
2255 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002256}
2257
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002258LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002259lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002260{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002261 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002262}
2263
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002264LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002265lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002266{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002267 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2268}
2269
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002270LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002271lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2272 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2273{
2274 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002275}
2276
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002277static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002278lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002279{
Michal Vasko14654712020-02-06 08:35:21 +01002280 /* ending \0 */
2281 ++reqlen;
2282
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002283 if (reqlen > *buflen) {
2284 if (is_static) {
2285 return LY_EINCOMPLETE;
2286 }
2287
2288 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2289 if (!*buffer) {
2290 return LY_EMEM;
2291 }
2292
2293 *buflen = reqlen;
2294 }
2295
2296 return LY_SUCCESS;
2297}
2298
Michal Vaskod59035b2020-07-08 12:00:06 +02002299LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002300lyd_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 +02002301{
2302 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002303 size_t len;
2304 const char *val;
2305 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002306
Michal Vasko824d0832021-11-04 15:36:51 +01002307 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002308 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002309 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002310 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002311
2312 quot = '\'';
2313 if (strchr(val, '\'')) {
2314 quot = '"';
2315 }
2316 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002317 }
2318
2319 return LY_SUCCESS;
2320}
2321
2322/**
2323 * @brief Append leaf-list value predicate to path.
2324 *
2325 * @param[in] node Node to print.
2326 * @param[in,out] buffer Buffer to print to.
2327 * @param[in,out] buflen Current buffer length.
2328 * @param[in,out] bufused Current number of characters used in @p buffer.
2329 * @param[in] is_static Whether buffer is static or can be reallocated.
2330 * @return LY_ERR
2331 */
2332static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002333lyd_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 +02002334{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002335 size_t len;
2336 const char *val;
2337 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002338
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002339 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002340 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002341 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002342
2343 quot = '\'';
2344 if (strchr(val, '\'')) {
2345 quot = '"';
2346 }
2347 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2348
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002349 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002350}
2351
2352/**
2353 * @brief Append node position (relative to its other instances) predicate to path.
2354 *
2355 * @param[in] node Node to print.
2356 * @param[in,out] buffer Buffer to print to.
2357 * @param[in,out] buflen Current buffer length.
2358 * @param[in,out] bufused Current number of characters used in @p buffer.
2359 * @param[in] is_static Whether buffer is static or can be reallocated.
2360 * @return LY_ERR
2361 */
2362static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002363lyd_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 +02002364{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002365 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002366 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002367 char *val = NULL;
2368 LY_ERR rc;
2369
Michal Vasko50cc0562021-05-18 16:15:43 +02002370 pos = lyd_list_pos(node);
2371 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002372 return LY_EMEM;
2373 }
2374
2375 len = 1 + strlen(val) + 1;
2376 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2377 if (rc != LY_SUCCESS) {
2378 goto cleanup;
2379 }
2380
2381 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2382
2383cleanup:
2384 free(val);
2385 return rc;
2386}
2387
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002388LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002389lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2390{
Radek Krejci857189e2020-09-01 13:26:36 +02002391 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002392 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002393 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002394 const struct lyd_node *iter, *parent;
2395 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002396 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002397
2398 LY_CHECK_ARG_RET(NULL, node, NULL);
2399 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002400 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002401 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002402 } else {
2403 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002404 }
2405
2406 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002407 case LYD_PATH_STD:
2408 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002409 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002410 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002411 ++depth;
2412 }
2413
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002414 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002415 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002416 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002417 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002418iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002419 /* get the module */
2420 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2421 parent = lyd_parent(iter);
2422 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2423 if (prev_mod == mod) {
2424 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002425 }
2426
2427 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002428 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2429 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002430 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2431 if (rc != LY_SUCCESS) {
2432 break;
2433 }
2434
2435 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002436 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002437
Michal Vasko790b2bc2020-08-03 13:35:06 +02002438 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002439 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002440 switch (iter->schema->nodetype) {
2441 case LYS_LIST:
2442 if (iter->schema->flags & LYS_KEYLESS) {
2443 /* print its position */
2444 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2445 } else {
2446 /* print all list keys in predicates */
2447 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2448 }
2449 break;
2450 case LYS_LEAFLIST:
2451 if (iter->schema->flags & LYS_CONFIG_W) {
2452 /* print leaf-list value */
2453 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2454 } else {
2455 /* print its position */
2456 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2457 }
2458 break;
2459 default:
2460 /* nothing to print more */
2461 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002462 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002463 }
2464 if (rc != LY_SUCCESS) {
2465 break;
2466 }
2467
Michal Vasko14654712020-02-06 08:35:21 +01002468 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002469 }
2470 break;
2471 }
2472
2473 return buffer;
2474}
Michal Vaskoe444f752020-02-10 12:20:06 +01002475
Michal Vaskodbf3e652022-10-21 08:46:25 +02002476char *
2477lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2478{
2479 uint32_t depth;
2480 size_t bufused = 0, buflen = 0, len;
2481 char *buffer = NULL;
2482 const struct lyd_node *iter, *parent;
2483 const struct lys_module *mod, *prev_mod;
2484 LY_ERR rc = LY_SUCCESS;
2485
2486 switch (pathtype) {
2487 case LYD_PATH_STD:
2488 case LYD_PATH_STD_NO_LAST_PRED:
2489 for (depth = 1; depth <= dnodes->count; ++depth) {
2490 /* current node */
2491 iter = dnodes->dnodes[depth - 1];
2492 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2493
2494 /* parent */
2495 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002496 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2497 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002498
2499 /* get module to print, if any */
2500 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2501 if (prev_mod == mod) {
2502 mod = NULL;
2503 }
2504
2505 /* realloc string */
2506 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2507 strlen(((struct lyd_node_opaq *)iter)->name.name));
2508 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2509 break;
2510 }
2511
2512 /* print next node */
2513 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2514
2515 /* do not always print the last (first) predicate */
2516 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2517 switch (iter->schema->nodetype) {
2518 case LYS_LIST:
2519 if (iter->schema->flags & LYS_KEYLESS) {
2520 /* print its position */
2521 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2522 } else {
2523 /* print all list keys in predicates */
2524 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2525 }
2526 break;
2527 case LYS_LEAFLIST:
2528 if (iter->schema->flags & LYS_CONFIG_W) {
2529 /* print leaf-list value */
2530 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2531 } else {
2532 /* print its position */
2533 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2534 }
2535 break;
2536 default:
2537 /* nothing to print more */
2538 break;
2539 }
2540 }
2541 if (rc) {
2542 break;
2543 }
2544 }
2545 break;
2546 }
2547
2548 return buffer;
2549}
2550
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002551LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002552lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2553{
2554 struct lyd_meta *ret = NULL;
2555 const struct ly_ctx *ctx;
2556 const char *prefix, *tmp;
2557 char *str;
2558 size_t pref_len, name_len;
2559
2560 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002561 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002562
2563 if (!first) {
2564 return NULL;
2565 }
2566
2567 ctx = first->annotation->module->ctx;
2568
2569 /* parse the name */
2570 tmp = name;
2571 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2572 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2573 return NULL;
2574 }
2575
2576 /* find the module */
2577 if (prefix) {
2578 str = strndup(prefix, pref_len);
2579 module = ly_ctx_get_module_latest(ctx, str);
2580 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002581 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 +02002582 }
2583
2584 /* find the metadata */
2585 LY_LIST_FOR(first, first) {
2586 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2587 ret = (struct lyd_meta *)first;
2588 break;
2589 }
2590 }
2591
2592 return ret;
2593}
2594
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002595LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002596lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2597{
Michal Vasko9beceb82022-04-05 12:14:15 +02002598 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002599 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002600 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002601
Michal Vaskof03ed032020-03-04 13:31:44 +01002602 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002603 if (!siblings) {
2604 /* no data */
2605 if (match) {
2606 *match = NULL;
2607 }
2608 return LY_ENOTFOUND;
2609 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002610
Michal Vasko9beceb82022-04-05 12:14:15 +02002611 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2612 /* create a duplicate in this context */
2613 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2614 target = dup;
2615 }
2616
2617 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2618 /* schema mismatch */
2619 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002620 if (match) {
2621 *match = NULL;
2622 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002623 return LY_ENOTFOUND;
2624 }
2625
Michal Vaskoe78faec2021-04-08 17:24:43 +02002626 /* get first sibling */
2627 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002628
Michal Vasko9e685082021-01-29 14:49:09 +01002629 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002630 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002631 assert(target->hash);
2632
Michal Vaskoe78faec2021-04-08 17:24:43 +02002633 if (lysc_is_dup_inst_list(target->schema)) {
2634 /* we must search the instances from beginning to find the first matching one */
2635 found = 0;
2636 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2637 if (!lyd_compare_single(target, iter, 0)) {
2638 found = 1;
2639 break;
2640 }
2641 }
2642 if (found) {
2643 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002644 } else {
2645 siblings = NULL;
2646 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002647 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002648 /* find by hash */
2649 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2650 siblings = *match_p;
2651 } else {
2652 /* not found */
2653 siblings = NULL;
2654 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002655 }
2656 } else {
2657 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002658 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002659 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002660 break;
2661 }
2662 }
2663 }
2664
Michal Vasko9beceb82022-04-05 12:14:15 +02002665 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002666 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002667 if (match) {
2668 *match = NULL;
2669 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002670 return LY_ENOTFOUND;
2671 }
2672
Michal Vasko9b368d32020-02-14 13:53:31 +01002673 if (match) {
2674 *match = (struct lyd_node *)siblings;
2675 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002676 return LY_SUCCESS;
2677}
2678
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002679LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002680lyd_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 +02002681 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002682{
2683 LY_ERR rc;
2684 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002685 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002686
Michal Vasko4c583e82020-07-17 12:16:14 +02002687 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002688 if (!siblings) {
2689 /* no data */
2690 if (match) {
2691 *match = NULL;
2692 }
2693 return LY_ENOTFOUND;
2694 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002695
Michal Vasko9beceb82022-04-05 12:14:15 +02002696 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2697 /* parent of ext nodes is useless */
2698 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002699 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2700 /* no schema node in siblings so certainly no data node either */
2701 if (match) {
2702 *match = NULL;
2703 }
2704 return LY_ENOTFOUND;
2705 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002706 }
2707
2708 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2709 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002710 if (match) {
2711 *match = NULL;
2712 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002713 return LY_ENOTFOUND;
2714 }
2715
Michal Vaskof03ed032020-03-04 13:31:44 +01002716 if (key_or_value && !val_len) {
2717 val_len = strlen(key_or_value);
2718 }
2719
Michal Vaskob104f112020-07-17 09:54:54 +02002720 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2721 /* create a data node and find the instance */
2722 if (schema->nodetype == LYS_LEAFLIST) {
2723 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002724 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002725 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002726 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002727 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002728 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002729 }
2730
2731 /* find it */
2732 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002733 } else {
2734 /* find the first schema node instance */
2735 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002736 }
2737
Michal Vaskoe444f752020-02-10 12:20:06 +01002738 lyd_free_tree(target);
2739 return rc;
2740}
Michal Vaskoccc02342020-05-21 10:09:21 +02002741
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002742LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002743lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2744{
2745 struct lyd_node **match_p, *first, *iter;
2746 struct lyd_node_inner *parent;
2747
Michal Vasko83ae7772022-06-08 10:01:55 +02002748 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002749 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002750
2751 LY_CHECK_RET(ly_set_new(set));
2752
2753 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2754 /* no data or schema mismatch */
2755 return LY_ENOTFOUND;
2756 }
2757
2758 /* get first sibling */
2759 siblings = lyd_first_sibling(siblings);
2760
2761 parent = siblings->parent;
2762 if (parent && parent->schema && parent->children_ht) {
2763 assert(target->hash);
2764
2765 /* find the first instance */
2766 lyd_find_sibling_first(siblings, target, &first);
2767 if (first) {
2768 /* add it so that it is the first in the set */
2769 if (ly_set_add(*set, first, 1, NULL)) {
2770 goto error;
2771 }
2772
2773 /* find by hash */
2774 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2775 iter = *match_p;
2776 } else {
2777 /* not found */
2778 iter = NULL;
2779 }
2780 while (iter) {
2781 /* add all found nodes into the set */
2782 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2783 goto error;
2784 }
2785
2786 /* find next instance */
2787 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2788 iter = NULL;
2789 } else {
2790 iter = *match_p;
2791 }
2792 }
2793 }
2794 } else {
2795 /* no children hash table */
2796 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002797 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002798 ly_set_add(*set, (void *)siblings, 1, NULL);
2799 }
2800 }
2801 }
2802
2803 if (!(*set)->count) {
2804 return LY_ENOTFOUND;
2805 }
2806 return LY_SUCCESS;
2807
2808error:
2809 ly_set_free(*set, NULL);
2810 *set = NULL;
2811 return LY_EMEM;
2812}
2813
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002814LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002815lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2816{
2817 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2818
2819 for ( ; first; first = first->next) {
2820 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2821 break;
2822 }
2823 }
2824
2825 if (match) {
2826 *match = (struct lyd_node *)first;
2827 }
2828 return first ? LY_SUCCESS : LY_ENOTFOUND;
2829}
2830
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002831LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002832lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002833{
Michal Vaskod96e2372023-02-24 16:07:51 +01002834 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002835
Michal Vaskod96e2372023-02-24 16:07:51 +01002836 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2837}
Michal Vaskoccc02342020-05-21 10:09:21 +02002838
Michal Vaskod96e2372023-02-24 16:07:51 +01002839LIBYANG_API_DEF LY_ERR
2840lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2841{
2842 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002843
Michal Vaskod96e2372023-02-24 16:07:51 +01002844 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002845}
Radek Krejcica989142020-11-05 11:32:22 +01002846
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002847LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002848lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2849 const struct lyxp_var *vars, struct ly_set **set)
2850{
2851 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2852
2853 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2854}
2855
2856LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002857lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2858 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002859{
Michal Vaskod96e2372023-02-24 16:07:51 +01002860 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002861
Michal Vaskod96e2372023-02-24 16:07:51 +01002862 *set = NULL;
2863
2864 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 +01002865}
2866
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002867LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002868lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002869{
Michal Vaskod96e2372023-02-24 16:07:51 +01002870 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002871}
2872
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002873LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002874lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2875{
2876 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2877}
2878
2879LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002880lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2881 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002882{
Michal Vaskod96e2372023-02-24 16:07:51 +01002883 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2884}
2885
2886LIBYANG_API_DEF LY_ERR
2887lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2888 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2889 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2890{
2891 LY_ERR ret = LY_SUCCESS;
2892 struct lyxp_set xp_set = {0};
2893 struct lyxp_expr *exp = NULL;
2894 uint32_t i;
2895
2896 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2897 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2898 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2899
2900 /* parse expression */
2901 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2902 LY_CHECK_GOTO(ret, cleanup);
2903
2904 /* evaluate expression */
2905 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2906 LYXP_IGNORE_WHEN);
2907 LY_CHECK_GOTO(ret, cleanup);
2908
2909 /* return expected result type without or with casting */
2910 if (node_set) {
2911 /* node set */
2912 if (xp_set.type == LYXP_SET_NODE_SET) {
2913 /* transform into a set */
2914 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2915 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2916 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2917 (*node_set)->size = xp_set.used;
2918 for (i = 0; i < xp_set.used; ++i) {
2919 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2920 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2921 LY_CHECK_GOTO(ret, cleanup);
2922 }
2923 }
2924 if (ret_type) {
2925 *ret_type = LY_XPATH_NODE_SET;
2926 }
2927 } else if (!string && !number && !boolean) {
2928 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2929 ret = LY_EINVAL;
2930 goto cleanup;
2931 }
2932 }
2933
2934 if (string) {
2935 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2936 /* cast into string */
2937 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2938 }
2939 if (xp_set.type == LYXP_SET_STRING) {
2940 /* string */
2941 *string = xp_set.val.str;
2942 xp_set.val.str = NULL;
2943 if (ret_type) {
2944 *ret_type = LY_XPATH_STRING;
2945 }
2946 }
2947 }
2948
2949 if (number) {
2950 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2951 /* cast into number */
2952 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2953 }
2954 if (xp_set.type == LYXP_SET_NUMBER) {
2955 /* number */
2956 *number = xp_set.val.num;
2957 if (ret_type) {
2958 *ret_type = LY_XPATH_NUMBER;
2959 }
2960 }
2961 }
2962
2963 if (boolean) {
2964 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2965 /* cast into boolean */
2966 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2967 }
2968 if (xp_set.type == LYXP_SET_BOOLEAN) {
2969 /* boolean */
2970 *boolean = xp_set.val.bln;
2971 if (ret_type) {
2972 *ret_type = LY_XPATH_BOOLEAN;
2973 }
2974 }
2975 }
2976
2977cleanup:
2978 lyxp_set_free_content(&xp_set);
2979 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
2980 return ret;
aPiecekfba75362021-10-07 12:39:48 +02002981}
2982
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002983LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002984lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2985{
2986 LY_ERR ret = LY_SUCCESS;
2987 struct lyxp_expr *expr = NULL;
2988 struct ly_path *lypath = NULL;
2989
2990 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2991
2992 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002993 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 +01002994 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002995 LY_CHECK_GOTO(ret, cleanup);
2996
2997 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002998 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002999 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 +01003000 LY_CHECK_GOTO(ret, cleanup);
3001
3002 /* evaluate the path */
Michal Vasko90189962023-02-28 12:10:34 +01003003 ret = ly_path_eval_partial(lypath, ctx_node, NULL, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003004
3005cleanup:
3006 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3007 ly_path_free(LYD_CTX(ctx_node), lypath);
3008 return ret;
3009}
3010
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003011LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003012lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3013{
3014 LY_ERR ret;
3015 struct lyd_node *m;
3016
3017 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3018
Michal Vasko90189962023-02-28 12:10:34 +01003019 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003020 if (ret) {
3021 if (match) {
3022 *match = NULL;
3023 }
3024 return LY_ENOTFOUND;
3025 }
3026
3027 if (match) {
3028 *match = m;
3029 }
3030 return LY_SUCCESS;
3031}