blob: 7cd608e0eab42cc4b7b81d89e14dc4d7c7d26049 [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{
stewegd8e2fc92023-05-31 09:52:56 +0200102 LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200103 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100104 struct ly_set parsed = {0};
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 *
Michal Vasko820efe82023-05-12 15:47:43 +0200264 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100265 *
266 * @param[in] ctx libyang context.
267 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
268 * @param[in] parent Optional parent to connect the parsed nodes to.
269 * @param[in] in Input handle to read the input from.
270 * @param[in] format Expected format of the data in @p in.
271 * @param[in] data_type Expected operation to parse (@ref datatype).
272 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
273 * @param[out] op Optional parsed operation node.
274 * @return LY_ERR value.
275 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
276 */
277static LY_ERR
278lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
279 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 +0200280{
Michal Vaskoe0665742021-02-11 11:08:44 +0100281 LY_ERR rc = LY_SUCCESS;
282 struct lyd_ctx *lydctx = NULL;
283 struct ly_set parsed = {0};
284 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100285 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200286 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200287
Michal Vasko27fb0262021-02-23 09:42:01 +0100288 if (!ctx) {
289 ctx = LYD_CTX(parent);
290 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200291 if (tree) {
292 *tree = NULL;
293 }
294 if (op) {
295 *op = NULL;
296 }
297
Radek Krejci7931b192020-06-25 17:05:03 +0200298 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200299
Michal Vasko63f3d842020-07-08 10:10:14 +0200300 /* remember input position */
301 in->func_start = in->current;
302
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100303 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200304 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100305 val_opts = 0;
306
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100307 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100308 case LYD_TYPE_RPC_NETCONF:
309 case LYD_TYPE_NOTIF_NETCONF:
310 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200311 proto_msg = 1;
312 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100313 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200314 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
315 tree, !op, LY_EINVAL);
316 proto_msg = 1;
317 break;
318 case LYD_TYPE_RPC_RESTCONF:
319 case LYD_TYPE_REPLY_RESTCONF:
320 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
321 proto_msg = 1;
322 break;
323 case LYD_TYPE_NOTIF_RESTCONF:
324 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
325 proto_msg = 1;
326 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100327
Michal Vasko820efe82023-05-12 15:47:43 +0200328 /* set internal opts */
329 case LYD_TYPE_RPC_YANG:
330 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
331 break;
332 case LYD_TYPE_NOTIF_YANG:
333 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
334 break;
335 case LYD_TYPE_REPLY_YANG:
336 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
337 break;
338 case LYD_TYPE_DATA_YANG:
339 LOGINT(ctx);
340 rc = LY_EINT;
341 goto cleanup;
342 }
343
344 /* parse a full protocol message */
345 if (proto_msg) {
346 if (format == LYD_XML) {
347 /* parse the NETCONF (or RESTCONF XML) message */
348 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
349 } else {
350 /* parse the RESTCONF message */
351 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
352 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100353 if (rc) {
354 if (envp) {
355 /* special situation when the envelopes were parsed successfully */
356 *tree = envp;
357 }
Michal Vasko73792a12022-05-10 09:28:45 +0200358 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100359 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100360
361 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100362 if (envp) {
363 /* special out param meaning */
364 *tree = envp;
365 } else {
366 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100367 }
368 if (op) {
369 *op = lydctx->op_node;
370 }
371 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100372 }
373
374 /* parse the data */
375 switch (format) {
376 case LYD_XML:
377 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 +0100378 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200379 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100380 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 +0100381 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200382 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100383 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 +0100384 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200385 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100386 LOGARG(ctx, format);
387 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200388 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200389 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200391
Michal Vaskoe0665742021-02-11 11:08:44 +0100392 /* set out params correctly */
393 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100394 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100395 }
396 if (op) {
397 *op = lydctx->op_node;
398 }
399
400cleanup:
401 if (lydctx) {
402 lydctx->free(lydctx);
403 }
404 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200405 /* free all the parsed nodes */
406 if (parsed.count) {
407 i = parsed.count;
408 do {
409 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100410 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200411 } while (i);
412 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200413 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200414 *tree = NULL;
415 }
416 if (op) {
417 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100418 }
419 }
420 ly_set_erase(&parsed, NULL);
421 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200422}
Radek Krejci084289f2019-07-09 17:35:30 +0200423
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100424LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100425lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
426 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
427{
428 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
429
430 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
431}
432
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100433LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100434lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
435 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
436{
437 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
438
439 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
440
441 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
442}
443
Michal Vasko90932a92020-02-12 14:33:03 +0100444struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200445lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100446{
Michal Vaskob104f112020-07-17 09:54:54 +0200447 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100448 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200449 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100450 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100451
Michal Vaskob104f112020-07-17 09:54:54 +0200452 assert(new_node);
453
Michal Vasko9beceb82022-04-05 12:14:15 +0200454 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200455 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100456 return NULL;
457 }
458
Michal Vasko93b16062020-12-09 18:14:18 +0100459 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100460 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100461 getnext_opts = LYS_GETNEXT_OUTPUT;
462 }
463
Michal Vaskoa2756f02021-07-09 13:20:28 +0200464 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200465 /* find the anchor using hashes */
466 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100467 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200468 while (schema) {
469 /* keep trying to find the first existing instance of the closest following schema sibling,
470 * otherwise return NULL - inserting at the end */
471 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
472 break;
473 }
474
Michal Vasko93b16062020-12-09 18:14:18 +0100475 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200476 }
477 } else {
478 /* find the anchor without hashes */
479 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200480 sparent = lysc_data_parent(new_node->schema);
481 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200482 /* we are in top-level, skip all the data from preceding modules */
483 LY_LIST_FOR(match, match) {
484 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
485 break;
486 }
487 }
488 }
489
490 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100491 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200492 if (!schema) {
493 /* must be a top-level extension instance data, insert at the end */
494 return first_sibling->prev;
495 }
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 Vasko9a463472023-08-08 09:43:33 +0200518
Michal Vasko93b16062020-12-09 18:14:18 +0100519 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200520 if (!schema) {
521 /* must be a top-level extension instance data, insert at the end */
522 return first_sibling->prev;
523 }
Michal Vaskob104f112020-07-17 09:54:54 +0200524 }
525
526 if (found && (match->schema != new_node->schema)) {
527 /* find the next node after we have found our node schema data instance */
528 break;
529 }
530 }
Michal Vasko90932a92020-02-12 14:33:03 +0100531 }
532
533 return match;
534}
535
Michal Vaskoc61dd062022-06-07 11:01:28 +0200536void
Michal Vaskof03ed032020-03-04 13:31:44 +0100537lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100538{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100539 struct lyd_node_inner *par;
540
Michal Vasko90932a92020-02-12 14:33:03 +0100541 assert(!node->next && (node->prev == node));
542
543 node->next = sibling->next;
544 node->prev = sibling;
545 sibling->next = node;
546 if (node->next) {
547 /* sibling had a succeeding node */
548 node->next->prev = node;
549 } else {
550 /* sibling was last, find first sibling and change its prev */
551 if (sibling->parent) {
552 sibling = sibling->parent->child;
553 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200554 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100555 }
556 sibling->prev = node;
557 }
558 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100559
Michal Vasko9f96a052020-03-10 09:41:45 +0100560 for (par = node->parent; par; par = par->parent) {
561 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
562 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100563 par->flags &= ~LYD_DEFAULT;
564 }
565 }
Michal Vasko90932a92020-02-12 14:33:03 +0100566}
567
Michal Vaskoc61dd062022-06-07 11:01:28 +0200568void
Michal Vaskof03ed032020-03-04 13:31:44 +0100569lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100570{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100571 struct lyd_node_inner *par;
572
Michal Vasko90932a92020-02-12 14:33:03 +0100573 assert(!node->next && (node->prev == node));
574
575 node->next = sibling;
576 /* covers situation of sibling being first */
577 node->prev = sibling->prev;
578 sibling->prev = node;
579 if (node->prev->next) {
580 /* sibling had a preceding node */
581 node->prev->next = node;
582 } else if (sibling->parent) {
583 /* sibling was first and we must also change parent child pointer */
584 sibling->parent->child = node;
585 }
586 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100587
Michal Vasko9f96a052020-03-10 09:41:45 +0100588 for (par = node->parent; par; par = par->parent) {
589 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
590 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100591 par->flags &= ~LYD_DEFAULT;
592 }
593 }
Michal Vasko90932a92020-02-12 14:33:03 +0100594}
595
596/**
Michal Vaskob104f112020-07-17 09:54:54 +0200597 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100598 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100599 * Handles inserting into NP containers and key-less lists.
600 *
Michal Vasko90932a92020-02-12 14:33:03 +0100601 * @param[in] parent Parent to insert into.
602 * @param[in] node Node to insert.
603 */
604static void
Michal Vaskob104f112020-07-17 09:54:54 +0200605lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100606{
607 struct lyd_node_inner *par;
608
Radek Krejcia1c1e542020-09-29 16:06:52 +0200609 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100610 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100611
612 par = (struct lyd_node_inner *)parent;
613
Michal Vaskob104f112020-07-17 09:54:54 +0200614 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100615 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100616
Michal Vaskod989ba02020-08-24 10:59:24 +0200617 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100618 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
619 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100620 par->flags &= ~LYD_DEFAULT;
621 }
622 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200623}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100624
Michal Vasko751cb4d2020-07-14 12:25:28 +0200625/**
626 * @brief Learn whether a list instance has all the keys.
627 *
628 * @param[in] list List instance to check.
629 * @return non-zero if all the keys were found,
630 * @return 0 otherwise.
631 */
632static int
633lyd_insert_has_keys(const struct lyd_node *list)
634{
635 const struct lyd_node *key;
636 const struct lysc_node *skey = NULL;
637
638 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200639 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200640 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
641 if (!key || (key->schema != skey)) {
642 /* key missing */
643 return 0;
644 }
645
646 key = key->next;
647 }
648
649 /* all keys found */
650 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100651}
652
653void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200654lyd_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 +0100655{
Michal Vaskob104f112020-07-17 09:54:54 +0200656 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100657
Michal Vaskob104f112020-07-17 09:54:54 +0200658 /* inserting list without its keys is not supported */
659 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100660 assert(!parent || !parent->schema ||
661 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100662
Michal Vaskob104f112020-07-17 09:54:54 +0200663 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100664 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100665 }
Michal Vasko90932a92020-02-12 14:33:03 +0100666
Michal Vaskob104f112020-07-17 09:54:54 +0200667 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100668 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100669
Michal Vasko9beceb82022-04-05 12:14:15 +0200670 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200671 /* no next anchor */
672 anchor = NULL;
673 } else {
674 /* find the anchor, our next node, so we can insert before it */
675 anchor = lyd_insert_get_next_anchor(first_sibling, node);
676 }
677
Michal Vaskob104f112020-07-17 09:54:54 +0200678 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200679 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200680 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100681 if (!parent && (*first_sibling_p == anchor)) {
682 /* move first sibling */
683 *first_sibling_p = node;
684 }
Michal Vaskob104f112020-07-17 09:54:54 +0200685 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200686 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200687 lyd_insert_after_node(first_sibling->prev, node);
688 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200689 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200690 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100691 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200692 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200693 *first_sibling_p = node;
694 }
695
696 /* insert into parent HT */
697 lyd_insert_hash(node);
698
699 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200700 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200701 lyd_hash(parent);
702
703 /* now we can insert even the list into its parent HT */
704 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100705 }
Michal Vasko90932a92020-02-12 14:33:03 +0100706}
707
Michal Vasko717a4c32020-12-07 09:40:10 +0100708/**
709 * @brief Check schema place of a node to be inserted.
710 *
711 * @param[in] parent Schema node of the parent data node.
712 * @param[in] sibling Schema node of a sibling data node.
713 * @param[in] schema Schema node if the data node to be inserted.
714 * @return LY_SUCCESS on success.
715 * @return LY_EINVAL if the place is invalid.
716 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100717static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100718lyd_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 +0100719{
720 const struct lysc_node *par2;
721
Michal Vasko62ed12d2020-05-21 10:08:25 +0200722 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100723 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
724 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100725
Michal Vasko717a4c32020-12-07 09:40:10 +0100726 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100727 /* opaque nodes can be inserted wherever */
728 return LY_SUCCESS;
729 }
730
Michal Vasko717a4c32020-12-07 09:40:10 +0100731 if (!parent) {
732 parent = lysc_data_parent(sibling);
733 }
734
Michal Vaskof03ed032020-03-04 13:31:44 +0100735 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200736 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100737
738 if (parent) {
739 /* inner node */
740 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200741 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200742 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100743 return LY_EINVAL;
744 }
745 } else {
746 /* top-level node */
747 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200748 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100749 return LY_EINVAL;
750 }
751 }
752
753 return LY_SUCCESS;
754}
755
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100756LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200757lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100758{
759 struct lyd_node *iter;
760
Michal Vasko0ab974d2021-02-24 13:18:26 +0100761 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100762 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100763
Michal Vasko717a4c32020-12-07 09:40:10 +0100764 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100765
Michal Vasko0ab974d2021-02-24 13:18:26 +0100766 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100767 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100768 return LY_EINVAL;
769 }
770
771 if (node->parent || node->prev->next) {
772 lyd_unlink_tree(node);
773 }
774
775 while (node) {
776 iter = node->next;
777 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200778 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100779 node = iter;
780 }
781 return LY_SUCCESS;
782}
783
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100784LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200785lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100786{
787 struct lyd_node *iter;
788
789 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
790 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
791
792 if (first->schema && (first->schema->flags & LYS_KEY)) {
793 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
794 return LY_EINVAL;
795 }
796
797 while (first) {
798 iter = first->next;
799 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100800 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100801 first = iter;
802 }
803 return LY_SUCCESS;
804}
805
806LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200807lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100808{
809 struct lyd_node *iter;
810
Michal Vaskob104f112020-07-17 09:54:54 +0200811 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100812
Michal Vaskob104f112020-07-17 09:54:54 +0200813 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100814 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100815 }
816
Michal Vasko553d0b52020-12-04 16:27:52 +0100817 if (sibling == node) {
818 /* we need to keep the connection to siblings so we can insert into them */
819 sibling = sibling->prev;
820 }
821
Michal Vaskob1b5c262020-03-05 14:29:47 +0100822 if (node->parent || node->prev->next) {
823 lyd_unlink_tree(node);
824 }
825
826 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100827 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200828 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200829 return LY_EINVAL;
830 }
831
Michal Vaskob1b5c262020-03-05 14:29:47 +0100832 iter = node->next;
833 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200834 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100835 node = iter;
836 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100837
Michal Vaskob104f112020-07-17 09:54:54 +0200838 if (first) {
839 /* find the first sibling */
840 *first = sibling;
841 while ((*first)->prev->next) {
842 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100843 }
844 }
845
846 return LY_SUCCESS;
847}
848
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100849LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100850lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
851{
Michal Vaskobce230b2022-04-19 09:55:31 +0200852 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100853 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100854
Michal Vasko717a4c32020-12-07 09:40:10 +0100855 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100856
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200857 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200858 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100859 return LY_EINVAL;
860 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200861 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
862 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100863 return LY_EINVAL;
864 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100865
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100866 lyd_unlink_tree(node);
867 lyd_insert_before_node(sibling, node);
868 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100869
Michal Vaskof03ed032020-03-04 13:31:44 +0100870 return LY_SUCCESS;
871}
872
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100873LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100874lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
875{
Michal Vaskobce230b2022-04-19 09:55:31 +0200876 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100877 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100878
Michal Vasko717a4c32020-12-07 09:40:10 +0100879 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100880
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200881 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200882 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100883 return LY_EINVAL;
884 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200885 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
886 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100887 return LY_EINVAL;
888 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100889
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100890 lyd_unlink_tree(node);
891 lyd_insert_after_node(sibling, node);
892 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100893
Michal Vaskof03ed032020-03-04 13:31:44 +0100894 return LY_SUCCESS;
895}
896
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100897LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200898lyd_unlink_siblings(struct lyd_node *node)
899{
900 struct lyd_node *next, *elem, *first = NULL;
901
902 LY_LIST_FOR_SAFE(node, next, elem) {
903 lyd_unlink_tree(elem);
904 lyd_insert_node(NULL, &first, elem, 1);
905 }
906}
907
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100908LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100909lyd_unlink_tree(struct lyd_node *node)
910{
911 struct lyd_node *iter;
912
913 if (!node) {
914 return;
915 }
916
Michal Vaskob104f112020-07-17 09:54:54 +0200917 /* update hashes while still linked into the tree */
918 lyd_unlink_hash(node);
919
Michal Vaskof03ed032020-03-04 13:31:44 +0100920 /* unlink from siblings */
921 if (node->prev->next) {
922 node->prev->next = node->next;
923 }
924 if (node->next) {
925 node->next->prev = node->prev;
926 } else {
927 /* unlinking the last node */
928 if (node->parent) {
929 iter = node->parent->child;
930 } else {
931 iter = node->prev;
932 while (iter->prev != node) {
933 iter = iter->prev;
934 }
935 }
936 /* update the "last" pointer from the first node */
937 iter->prev = node->prev;
938 }
939
940 /* unlink from parent */
941 if (node->parent) {
942 if (node->parent->child == node) {
943 /* the node is the first child */
944 node->parent->child = node->next;
945 }
946
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200947 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100948 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200949
Michal Vaskof03ed032020-03-04 13:31:44 +0100950 node->parent = NULL;
951 }
952
953 node->next = NULL;
954 node->prev = node;
955}
956
Michal Vaskoa5da3292020-08-12 13:10:50 +0200957void
Michal Vasko871a0252020-11-11 18:35:24 +0100958lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200959{
960 struct lyd_meta *last, *iter;
961
962 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200963
964 if (!meta) {
965 return;
966 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200967
968 for (iter = meta; iter; iter = iter->next) {
969 iter->parent = parent;
970 }
971
972 /* insert as the last attribute */
973 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200974 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200975 last->next = meta;
976 } else {
977 parent->meta = meta;
978 }
979
980 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100981 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200982 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100983 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200984 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200985}
986
987LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100988lyd_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 +0200989 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 +0100990 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 +0100991{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100992 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100993 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200994 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200996 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100997
Michal Vasko9f96a052020-03-10 09:41:45 +0100998 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100999
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001000 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001001 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001002 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001003 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001004 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001005 break;
1006 }
1007 }
1008 if (!ant) {
1009 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001010 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001011 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001012 ret = LY_EINVAL;
1013 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001014 }
1015
Michal Vasko9f96a052020-03-10 09:41:45 +01001016 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001017 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001018 mt->parent = parent;
1019 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001020 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001021 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 +01001022 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001023 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1024 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1025 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001026
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001027 /* insert as the last attribute */
1028 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001029 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001030 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001031 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001032 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001033 }
1034
Michal Vasko9f96a052020-03-10 09:41:45 +01001035 if (meta) {
1036 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001037 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001038
1039cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001040 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001041}
1042
Michal Vaskoa5da3292020-08-12 13:10:50 +02001043void
1044lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1045{
1046 struct lyd_attr *last, *iter;
1047 struct lyd_node_opaq *opaq;
1048
1049 assert(parent && !parent->schema);
1050
1051 if (!attr) {
1052 return;
1053 }
1054
1055 opaq = (struct lyd_node_opaq *)parent;
1056 for (iter = attr; iter; iter = iter->next) {
1057 iter->parent = opaq;
1058 }
1059
1060 /* insert as the last attribute */
1061 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001062 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001063 last->next = attr;
1064 } else {
1065 opaq->attr = attr;
1066 }
1067}
1068
Michal Vasko52927e22020-03-16 17:26:14 +01001069LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001070lyd_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 +01001071 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 +02001072 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 +01001073{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001074 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001075 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001076
1077 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001078 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001079
Michal Vasko2a3722d2021-06-16 11:52:39 +02001080 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001081 value = "";
1082 }
1083
1084 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001085 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001086
Michal Vaskoad92b672020-11-12 13:11:31 +01001087 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001088 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001089 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001090 }
1091 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001092 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001093 }
1094
Michal Vasko52927e22020-03-16 17:26:14 +01001095 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001096 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1097 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001098 *dynamic = 0;
1099 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001100 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001101 }
Michal Vasko501af032020-11-11 20:27:44 +01001102 at->format = format;
1103 at->val_prefix_data = val_prefix_data;
1104 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001105
1106 /* insert as the last attribute */
1107 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001108 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001109 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001110 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001111 last->next = at;
1112 }
1113
Radek Krejci011e4aa2020-09-04 15:22:31 +02001114finish:
1115 if (ret) {
1116 lyd_free_attr_single(ctx, at);
1117 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001118 *attr = at;
1119 }
1120 return LY_SUCCESS;
1121}
1122
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001123LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001124lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001125{
Michal Vasko90189962023-02-28 12:10:34 +01001126 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001127
Michal Vasko90189962023-02-28 12:10:34 +01001128 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001129
Michal Vasko004d3152020-06-11 19:59:22 +02001130 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001131}
1132
aPiecek2f63f952021-03-30 12:22:18 +02001133/**
1134 * @brief Check the equality of the two schemas from different contexts.
1135 *
1136 * @param schema1 of first node.
1137 * @param schema2 of second node.
1138 * @return 1 if the schemas are equal otherwise 0.
1139 */
1140static ly_bool
1141lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1142{
1143 if (!schema1 && !schema2) {
1144 return 1;
1145 } else if (!schema1 || !schema2) {
1146 return 0;
1147 }
1148
1149 assert(schema1->module->ctx != schema2->module->ctx);
1150
1151 if (schema1->nodetype != schema2->nodetype) {
1152 return 0;
1153 }
1154
1155 if (strcmp(schema1->name, schema2->name)) {
1156 return 0;
1157 }
1158
1159 if (strcmp(schema1->module->name, schema2->module->name)) {
1160 return 0;
1161 }
1162
aPiecek2f63f952021-03-30 12:22:18 +02001163 return 1;
1164}
1165
1166/**
1167 * @brief Check the equality of the schemas for all parent nodes.
1168 *
1169 * Both nodes must be from different contexts.
1170 *
1171 * @param node1 Data of first node.
1172 * @param node2 Data of second node.
1173 * @return 1 if the all related parental schemas are equal otherwise 0.
1174 */
1175static ly_bool
1176lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1177{
1178 const struct lysc_node *parent1, *parent2;
1179
1180 assert(node1 && node2);
1181
1182 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1183 parent1 && parent2;
1184 parent1 = parent1->parent, parent2 = parent2->parent) {
1185 if (!lyd_compare_schema_equal(parent1, parent2)) {
1186 return 0;
1187 }
1188 }
1189
1190 if (parent1 || parent2) {
1191 return 0;
1192 }
1193
1194 return 1;
1195}
1196
1197/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001198 * @brief Compare 2 nodes values including opaque node values.
1199 *
1200 * @param[in] node1 First node to compare.
1201 * @param[in] node2 Second node to compare.
1202 * @return LY_SUCCESS if equal.
1203 * @return LY_ENOT if not equal.
1204 * @return LY_ERR on error.
1205 */
1206static LY_ERR
1207lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1208{
1209 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1210 const char *val1, *val2, *col;
1211 const struct lys_module *mod;
1212 char *val_dyn = NULL;
1213 LY_ERR rc = LY_SUCCESS;
1214
1215 if (!node1->schema) {
1216 opaq1 = (struct lyd_node_opaq *)node1;
1217 }
1218 if (!node2->schema) {
1219 opaq2 = (struct lyd_node_opaq *)node2;
1220 }
1221
1222 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1223 /* opaque XML and opaque XML node */
1224 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1225 opaq2->val_prefix_data)) {
1226 return LY_ENOT;
1227 }
1228 return LY_SUCCESS;
1229 }
1230
1231 /* get their values */
1232 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1233 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1234 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1235 if (!mod) {
1236 /* unable to compare */
1237 return LY_ENOT;
1238 }
1239
1240 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1241 LOGMEM(LYD_CTX(node1));
1242 return LY_EMEM;
1243 }
1244 val1 = val_dyn;
1245 } else {
1246 val1 = lyd_get_value(node1);
1247 }
1248 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1249 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1250 if (!mod) {
1251 return LY_ENOT;
1252 }
1253
1254 assert(!val_dyn);
1255 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1256 LOGMEM(LYD_CTX(node2));
1257 return LY_EMEM;
1258 }
1259 val2 = val_dyn;
1260 } else {
1261 val2 = lyd_get_value(node2);
1262 }
1263
1264 /* compare values */
1265 if (strcmp(val1, val2)) {
1266 rc = LY_ENOT;
1267 }
1268
1269 free(val_dyn);
1270 return rc;
1271}
1272
1273/**
Michal Vaskof277d362023-04-24 09:08:31 +02001274 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1275 *
1276 * Works correctly even if @p node1 and @p node2 have different contexts.
1277 *
1278 * @param[in] node1 The first node to compare.
1279 * @param[in] node2 The second node to compare.
1280 * @param[in] options Various @ref datacompareoptions.
1281 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1282 * @return LY_SUCCESS if the nodes are equivalent.
1283 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001284 */
1285static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001286lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001287 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001288{
aPiecek2f63f952021-03-30 12:22:18 +02001289 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1290 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001291 if (options & LYD_COMPARE_OPAQ) {
1292 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1293 return LY_ENOT;
1294 }
1295 } else {
1296 if (node1->schema != node2->schema) {
1297 return LY_ENOT;
1298 }
aPiecek2f63f952021-03-30 12:22:18 +02001299 }
1300 } else {
1301 /* different contexts */
1302 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1303 return LY_ENOT;
1304 }
1305 if (!parental_schemas_checked) {
1306 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1307 return LY_ENOT;
1308 }
1309 parental_schemas_checked = 1;
1310 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001311 }
1312
Michal Vaskof277d362023-04-24 09:08:31 +02001313 return LY_SUCCESS;
1314}
1315
1316/**
1317 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1318 *
1319 * Works correctly even if @p node1 and @p node2 have different contexts.
1320 *
1321 * @param[in] node1 The first node to compare.
1322 * @param[in] node2 The second node to compare.
1323 * @param[in] options Various @ref datacompareoptions.
1324 * @return LY_SUCCESS if the nodes are equivalent.
1325 * @return LY_ENOT if the nodes are not equivalent.
1326 */
1327static LY_ERR
1328lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1329{
1330 const struct lyd_node *iter1, *iter2;
1331 struct lyd_node_any *any1, *any2;
1332 int len1, len2;
1333 LY_ERR r;
1334
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001335 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001336 return LY_ENOT;
1337 }
aPiecek2f63f952021-03-30 12:22:18 +02001338 /* 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 +02001339
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001340 if (!node1->schema || !node2->schema) {
1341 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001342 return LY_ENOT;
1343 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001344 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1345 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1346 /* compare values only if there are any to compare */
1347 if ((r = lyd_compare_single_value(node1, node2))) {
1348 return r;
1349 }
Michal Vasko52927e22020-03-16 17:26:14 +01001350 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001351
Michal Vasko52927e22020-03-16 17:26:14 +01001352 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001353 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001354 }
1355 return LY_SUCCESS;
1356 } else {
1357 switch (node1->schema->nodetype) {
1358 case LYS_LEAF:
1359 case LYS_LEAFLIST:
1360 if (options & LYD_COMPARE_DEFAULTS) {
1361 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1362 return LY_ENOT;
1363 }
1364 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001365 if ((r = lyd_compare_single_value(node1, node2))) {
1366 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001367 }
1368
aPiecek2f63f952021-03-30 12:22:18 +02001369 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001370 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001371 case LYS_RPC:
1372 case LYS_ACTION:
1373 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001374 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001375 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
Michal Vaskoee9b9482023-06-19 13:17:48 +02001383 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001384 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001385 } else if (node1->schema->flags & LYS_KEYLESS) {
1386 /* always equal */
1387 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001388 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001389
1390 /* lists with keys, their equivalence is based on their keys */
1391 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1392 key && (key->flags & LYS_KEY);
1393 key = key->next) {
1394 if (!iter1 || !iter2) {
1395 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1396 }
1397 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1398 LY_CHECK_RET(r);
1399 r = lyd_compare_single_data(iter1, iter2, options);
1400 LY_CHECK_RET(r);
1401
1402 iter1 = iter1->next;
1403 iter2 = iter2->next;
1404 }
1405
1406 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001407 case LYS_ANYXML:
1408 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001409 any1 = (struct lyd_node_any *)node1;
1410 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001411
1412 if (any1->value_type != any2->value_type) {
1413 return LY_ENOT;
1414 }
1415 switch (any1->value_type) {
1416 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001417 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001418 case LYD_ANYDATA_STRING:
1419 case LYD_ANYDATA_XML:
1420 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001421 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1422 return LY_ENOT;
1423 } else if (!any1->value.str && !any2->value.str) {
1424 return LY_SUCCESS;
1425 }
Michal Vasko52927e22020-03-16 17:26:14 +01001426 len1 = strlen(any1->value.str);
1427 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001428 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001429 return LY_ENOT;
1430 }
1431 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001432 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001433 len1 = lyd_lyb_data_length(any1->value.mem);
1434 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001435 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001436 return LY_ENOT;
1437 }
1438 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001439 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001440 }
1441 }
1442
Michal Vaskob7be7a82020-08-20 09:09:04 +02001443 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001444 return LY_EINT;
1445}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001446
Michal Vaskof277d362023-04-24 09:08:31 +02001447/**
1448 * @brief Compare all siblings at a node level.
1449 *
1450 * @param[in] node1 First sibling list.
1451 * @param[in] node2 Second sibling list.
1452 * @param[in] options Various @ref datacompareoptions.
1453 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1454 * @return LY_SUCCESS if equal.
1455 * @return LY_ENOT if not equal.
1456 * @return LY_ERR on error.
1457 */
1458static LY_ERR
1459lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1460 ly_bool parental_schemas_checked)
1461{
1462 LY_ERR r;
1463 const struct lyd_node *iter2;
1464
1465 while (node1 && node2) {
1466 /* schema match */
1467 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1468 LY_CHECK_RET(r);
1469
1470 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1471 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1472 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1473 /* find a matching instance in case they are ordered differently */
1474 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1475 if (r == LY_ENOTFOUND) {
1476 /* no matching instance, data not equal */
1477 r = LY_ENOT;
1478 }
1479 LY_CHECK_RET(r);
1480 } else {
1481 /* compare with the current node */
1482 iter2 = node2;
1483 }
1484
1485 /* data match */
1486 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1487 LY_CHECK_RET(r);
1488
1489 node1 = node1->next;
1490 node2 = node2->next;
1491 }
1492
1493 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1494}
1495
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001496LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001497lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1498{
Michal Vaskof277d362023-04-24 09:08:31 +02001499 LY_ERR r;
1500
1501 if (!node1 || !node2) {
1502 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1503 }
1504
1505 /* schema match */
1506 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1507 return r;
1508 }
1509
1510 /* data match */
1511 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001512}
1513
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001514LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001515lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001516{
Michal Vaskof277d362023-04-24 09:08:31 +02001517 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001518}
1519
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001520LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001521lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1522{
1523 if (!meta1 || !meta2) {
1524 if (meta1 == meta2) {
1525 return LY_SUCCESS;
1526 } else {
1527 return LY_ENOT;
1528 }
1529 }
1530
Michal Vaskoa8083062020-11-06 17:22:10 +01001531 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001532 return LY_ENOT;
1533 }
1534
Michal Vasko21725742020-06-29 11:49:25 +02001535 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1536}
1537
Radek Krejci22ebdba2019-07-25 13:59:43 +02001538/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001539 * @brief Create a copy of the attribute.
1540 *
1541 * @param[in] attr Attribute to copy.
1542 * @param[in] node Opaque where to append the new attribute.
1543 * @param[out] dup Optional created attribute copy.
1544 * @return LY_ERR value.
1545 */
1546static LY_ERR
1547lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1548{
1549 LY_ERR ret = LY_SUCCESS;
1550 struct lyd_attr *a, *last;
1551 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1552
1553 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1554
1555 /* create a copy */
1556 a = calloc(1, sizeof *attr);
1557 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1558
1559 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1560 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1561 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1562 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1563 a->hints = attr->hints;
1564 a->format = attr->format;
1565 if (attr->val_prefix_data) {
1566 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1567 LY_CHECK_GOTO(ret, finish);
1568 }
1569
1570 /* insert as the last attribute */
1571 a->parent = opaq;
1572 if (opaq->attr) {
1573 for (last = opaq->attr; last->next; last = last->next) {}
1574 last->next = a;
1575 } else {
1576 opaq->attr = a;
1577 }
1578
1579finish:
1580 if (ret) {
1581 lyd_free_attr_single(LYD_CTX(node), a);
1582 } else if (dup) {
1583 *dup = a;
1584 }
1585 return LY_SUCCESS;
1586}
1587
1588/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001589 * @brief Find @p schema equivalent in @p trg_ctx.
1590 *
1591 * @param[in] schema Schema node to find.
1592 * @param[in] trg_ctx Target context to search in.
1593 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001594 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001595 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1596 * @return LY_RRR value.
1597 */
1598static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001599lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1600 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001601{
Michal Vasko9beceb82022-04-05 12:14:15 +02001602 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1603 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001604 char *path;
1605
1606 if (!schema) {
1607 /* opaque node */
1608 *trg_schema = NULL;
1609 return LY_SUCCESS;
1610 }
1611
Michal Vasko9beceb82022-04-05 12:14:15 +02001612 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001613 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001614 trg_parent = parent->schema;
1615 src_parent = lysc_data_parent(schema);
1616 }
1617
1618 do {
1619 /* find the next parent */
1620 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001621 while (lysc_data_parent(sp) != src_parent) {
1622 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001623 }
1624 src_parent = sp;
1625
1626 if (!src_parent->parent) {
1627 /* find the module first */
1628 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1629 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001630 if (log) {
1631 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1632 src_parent->module->name);
1633 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001634 return LY_ENOTFOUND;
1635 }
1636 }
1637
1638 /* find the next parent */
1639 assert(trg_parent || trg_mod);
1640 tp = NULL;
1641 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1642 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1643 break;
1644 }
1645 }
1646 if (!tp) {
1647 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001648 if (log) {
1649 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1650 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1651 free(path);
1652 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001653 return LY_ENOTFOUND;
1654 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001655
Michal Vasko9beceb82022-04-05 12:14:15 +02001656 trg_parent = tp;
1657 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001658
Michal Vasko9beceb82022-04-05 12:14:15 +02001659 /* success */
1660 *trg_schema = trg_parent;
1661 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001662}
1663
1664/**
Michal Vasko52927e22020-03-16 17:26:14 +01001665 * @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 +02001666 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001667 * 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 +02001668 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001669 * @param[in] node Node to duplicate.
1670 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001671 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001672 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1673 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001674 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1675 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001676 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1677 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001678 */
Michal Vasko52927e22020-03-16 17:26:14 +01001679static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001680lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1681 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001682{
Michal Vasko52927e22020-03-16 17:26:14 +01001683 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001684 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001685 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001686 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001687 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001688 const struct lysc_type *type;
1689 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001690
Michal Vasko52927e22020-03-16 17:26:14 +01001691 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001692
Michal Vasko19175b62022-04-01 09:17:07 +02001693 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001694 if (options & LYD_DUP_NO_EXT) {
1695 /* no not duplicate this subtree */
1696 return LY_SUCCESS;
1697 }
1698
Michal Vasko19175b62022-04-01 09:17:07 +02001699 /* we need to use the same context */
1700 trg_ctx = LYD_CTX(node);
1701 }
1702
Michal Vasko52927e22020-03-16 17:26:14 +01001703 if (!node->schema) {
1704 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001705 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001706 } else {
1707 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001708 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001709 case LYS_ACTION:
1710 case LYS_NOTIF:
1711 case LYS_CONTAINER:
1712 case LYS_LIST:
1713 dup = calloc(1, sizeof(struct lyd_node_inner));
1714 break;
1715 case LYS_LEAF:
1716 case LYS_LEAFLIST:
1717 dup = calloc(1, sizeof(struct lyd_node_term));
1718 break;
1719 case LYS_ANYDATA:
1720 case LYS_ANYXML:
1721 dup = calloc(1, sizeof(struct lyd_node_any));
1722 break;
1723 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001724 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001725 ret = LY_EINT;
1726 goto error;
1727 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001728 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001729 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001730
Michal Vaskof6df0a02020-06-16 13:08:34 +02001731 if (options & LYD_DUP_WITH_FLAGS) {
1732 dup->flags = node->flags;
1733 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001734 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001735 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001736 if (trg_ctx == LYD_CTX(node)) {
1737 dup->schema = node->schema;
1738 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001739 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001740 if (ret) {
1741 /* has no schema but is not an opaque node */
1742 free(dup);
1743 dup = NULL;
1744 goto error;
1745 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001746 }
Michal Vasko52927e22020-03-16 17:26:14 +01001747 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001748
Michal Vasko9cf62422021-07-01 13:29:32 +02001749 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001750 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001751 if (!node->schema) {
1752 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1753 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1754 }
1755 } else {
1756 LY_LIST_FOR(node->meta, meta) {
1757 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1758 }
Michal Vasko25a32822020-07-09 15:48:22 +02001759 }
1760 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001761
1762 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001763 if (!dup->schema) {
1764 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1765 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1766 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001767
1768 if (options & LYD_DUP_RECURSIVE) {
1769 /* duplicate all the children */
1770 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001771 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001772 }
1773 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001774 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1775 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1776 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1777 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001778 opaq->hints = orig->hints;
1779 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001780 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001781 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001782 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001783 }
Michal Vasko52927e22020-03-16 17:26:14 +01001784 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1785 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1786 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1787
1788 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001789 if (trg_ctx == LYD_CTX(node)) {
1790 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1791 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1792 } else {
1793 /* store canonical value in the target context */
1794 val_can = lyd_get_value(node);
1795 type = ((struct lysc_node_leaf *)term->schema)->type;
1796 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1797 LYD_HINT_DATA, term->schema, NULL);
1798 LY_CHECK_GOTO(ret, error);
1799 }
Michal Vasko52927e22020-03-16 17:26:14 +01001800 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1801 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1802 struct lyd_node *child;
1803
1804 if (options & LYD_DUP_RECURSIVE) {
1805 /* duplicate all the children */
1806 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001807 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001808 }
Michal Vasko69730152020-10-09 16:30:07 +02001809 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001810 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001811 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001812 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001813 }
1814 }
1815 lyd_hash(dup);
1816 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001817 dup->hash = node->hash;
1818 any = (struct lyd_node_any *)node;
1819 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001820 }
1821
Michal Vasko52927e22020-03-16 17:26:14 +01001822 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001823 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001824
1825 if (dup_p) {
1826 *dup_p = dup;
1827 }
1828 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001829
1830error:
Michal Vasko52927e22020-03-16 17:26:14 +01001831 lyd_free_tree(dup);
1832 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001833}
1834
Michal Vasko29d674b2021-08-25 11:18:35 +02001835/**
1836 * @brief Get a parent node to connect duplicated subtree to.
1837 *
1838 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001839 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001840 * @param[in] parent Initial parent to connect to.
1841 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1842 * @param[out] dup_parent First duplicated parent node, if any.
1843 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1844 * @return LY_ERR value.
1845 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001846static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001847lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1848 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001849{
Michal Vasko58d89e92023-05-23 09:56:19 +02001850 const struct lyd_node *orig_parent;
1851 struct lyd_node *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001852 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001853
1854 *dup_parent = NULL;
1855 *local_parent = NULL;
1856
Michal Vasko83522192022-07-20 08:07:34 +02001857 if (node->flags & LYD_EXT) {
1858 ext_parent = 1;
1859 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001860 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001861 if (ext_parent) {
1862 /* use the standard context */
1863 trg_ctx = LYD_CTX(orig_parent);
1864 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001865 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001866 /* stop creating parents, connect what we have into the provided parent */
1867 iter = parent;
1868 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001869 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1870 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001871 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001872 iter = parent;
1873 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001874 } else {
1875 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001876 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001877 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001878
Michal Vasko3a41dff2020-07-15 14:30:28 +02001879 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001880 /* update local parent (created parent) */
1881 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001882 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001883
Michal Vasko3a41dff2020-07-15 14:30:28 +02001884 if (*dup_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001885 lyd_insert_node(iter, NULL, *dup_parent, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001886 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001887 *dup_parent = iter;
Michal Vasko83522192022-07-20 08:07:34 +02001888 if (orig_parent->flags & LYD_EXT) {
1889 ext_parent = 1;
1890 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001891 }
1892
1893 if (repeat && parent) {
1894 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001895 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1896 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001897 return LY_EINVAL;
1898 }
1899
1900 return LY_SUCCESS;
1901}
1902
1903static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001904lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options,
Michal Vaskoddd76592022-01-17 13:34:48 +01001905 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001906{
1907 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001908 const struct lyd_node *orig; /* original node to be duplicated */
1909 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001910 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001911 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001912
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001913 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001914
1915 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001916 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 +02001917 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001918 } else {
1919 local_parent = parent;
1920 }
1921
Radek Krejci22ebdba2019-07-25 13:59:43 +02001922 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001923 if (lysc_is_key(orig->schema)) {
1924 if (local_parent) {
1925 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001926 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001927 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001928 } else {
1929 assert(!(options & LYD_DUP_WITH_PARENTS));
1930 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001931 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001932 LY_CHECK_GOTO(rc, error);
1933 }
1934 } else {
1935 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001936 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001937 LY_CHECK_GOTO(rc, error);
1938 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001939 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001940 break;
1941 }
1942 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001943
Michal Vasko3a41dff2020-07-15 14:30:28 +02001944 if (dup) {
1945 *dup = first;
1946 }
1947 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001948
1949error:
1950 if (top) {
1951 lyd_free_tree(top);
1952 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001953 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001954 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001955 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001956}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001957
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001958/**
1959 * @brief Check the context of node and parent when duplicating nodes.
1960 *
1961 * @param[in] node Node to duplicate.
1962 * @param[in] parent Parent of the duplicated node(s).
1963 * @return LY_ERR value.
1964 */
1965static LY_ERR
1966lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1967{
1968 const struct lyd_node *iter;
1969
1970 if (!node || !parent) {
1971 return LY_SUCCESS;
1972 }
1973
1974 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1975 /* try to find top-level ext data parent */
1976 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1977
1978 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001979 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001980 return LY_EINVAL;
1981 }
1982 }
1983
1984 return LY_SUCCESS;
1985}
1986
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001987LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001988lyd_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 +02001989{
Michal Vaskoddd76592022-01-17 13:34:48 +01001990 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001991 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001992
Michal Vasko58d89e92023-05-23 09:56:19 +02001993 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001994}
1995
1996LIBYANG_API_DEF LY_ERR
1997lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1998 uint32_t options, struct lyd_node **dup)
1999{
2000 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2001
Michal Vasko58d89e92023-05-23 09:56:19 +02002002 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002003}
2004
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002005LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002006lyd_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 +02002007{
Michal Vaskoddd76592022-01-17 13:34:48 +01002008 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002009 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002010
Michal Vasko58d89e92023-05-23 09:56:19 +02002011 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002012}
2013
2014LIBYANG_API_DEF LY_ERR
2015lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2016 uint32_t options, struct lyd_node **dup)
2017{
2018 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2019
Michal Vasko58d89e92023-05-23 09:56:19 +02002020 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002021}
2022
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002023LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002024lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002025{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002026 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002027 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002028 struct lyd_meta *mt, *last;
2029
Michal Vasko3a41dff2020-07-15 14:30:28 +02002030 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002031
Michal Vasko33c48972022-07-20 10:28:07 +02002032 /* log to node context but value must always use the annotation context */
2033 ctx = meta->annotation->module->ctx;
2034
Michal Vasko25a32822020-07-09 15:48:22 +02002035 /* create a copy */
2036 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002037 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002038 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002039 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002040 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002041 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002042
2043 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002044 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002045 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002046 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002047 last->next = mt;
2048 } else {
2049 node->meta = mt;
2050 }
2051
Radek Krejci011e4aa2020-09-04 15:22:31 +02002052finish:
2053 if (ret) {
2054 lyd_free_meta_single(mt);
2055 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002056 *dup = mt;
2057 }
2058 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002059}
2060
Michal Vasko4490d312020-06-16 13:08:55 +02002061/**
2062 * @brief Merge a source sibling into target siblings.
2063 *
2064 * @param[in,out] first_trg First target sibling, is updated if top-level.
2065 * @param[in] parent_trg Target parent.
2066 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002067 * @param[in] merge_cb Optional merge callback.
2068 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002069 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002070 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002071 * @return LY_ERR value.
2072 */
2073static LY_ERR
2074lyd_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 +02002075 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002076{
Michal Vasko4490d312020-06-16 13:08:55 +02002077 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002078 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002079 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002080 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002081 struct ly_ht *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002082 LY_ERR ret;
2083 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002084
2085 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002086 if (!sibling_src->schema) {
2087 /* try to find the same opaque node */
2088 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2089 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002090 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002091 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002092 } else {
2093 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002094 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002095 }
2096
Michal Vaskocd3f6172021-05-18 16:14:50 +02002097 if (match_trg) {
2098 /* update match as needed */
2099 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2100 } else {
2101 /* first instance of this node */
2102 first_inst = 1;
2103 }
2104
2105 if (match_trg) {
2106 /* call callback */
2107 if (merge_cb) {
2108 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2109 }
2110
Michal Vasko4490d312020-06-16 13:08:55 +02002111 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002112 if (!match_trg->schema) {
2113 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2114 /* update value */
2115 opaq_trg = (struct lyd_node_opaq *)match_trg;
2116 opaq_src = (struct lyd_node_opaq *)sibling_src;
2117
2118 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2119 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2120 opaq_trg->hints = opaq_src->hints;
2121
2122 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2123 opaq_trg->format = opaq_src->format;
2124 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2125 &opaq_trg->val_prefix_data);
2126 }
2127 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2128 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002129 /* since they are different, they cannot both be default */
2130 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2131
Michal Vasko3a41dff2020-07-15 14:30:28 +02002132 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2133 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002134 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002135 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2136 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002137 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002138
2139 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002140 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002141 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002142 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002143 /* update value */
2144 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002145 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002146
2147 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002148 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002149 }
2150
2151 /* check descendants, recursively */
2152 ret = LY_SUCCESS;
2153 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2154 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2155 &child_dup_inst);
2156 if (ret) {
2157 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002158 }
2159 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002160 lyd_dup_inst_free(child_dup_inst);
2161 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002162 } else {
2163 /* node not found, merge it */
2164 if (options & LYD_MERGE_DESTRUCT) {
2165 dup_src = (struct lyd_node *)sibling_src;
2166 lyd_unlink_tree(dup_src);
2167 /* spend it */
2168 *sibling_src_p = NULL;
2169 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002170 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 +02002171 }
2172
Michal Vasko7e8315b2021-08-03 17:01:06 +02002173 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2174 /* set LYD_NEW for all the new nodes, required for validation */
2175 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2176 elem->flags |= LYD_NEW;
2177 LYD_TREE_DFS_END(dup_src, elem);
2178 }
Michal Vasko4490d312020-06-16 13:08:55 +02002179 }
2180
Michal Vaskocd3f6172021-05-18 16:14:50 +02002181 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002182 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002183
2184 if (first_inst) {
2185 /* remember not to find this instance next time */
2186 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2187 }
2188
2189 /* call callback, no source node */
2190 if (merge_cb) {
2191 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2192 }
Michal Vasko4490d312020-06-16 13:08:55 +02002193 }
2194
2195 return LY_SUCCESS;
2196}
2197
Michal Vasko3a41dff2020-07-15 14:30:28 +02002198static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002199lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2200 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002201{
2202 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002203 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002204 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002205 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002206
2207 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002208 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2209 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002210
2211 if (!source) {
2212 /* nothing to merge */
2213 return LY_SUCCESS;
2214 }
2215
Michal Vasko831422b2020-11-24 11:20:51 +01002216 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002217 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002218 return LY_EINVAL;
2219 }
2220
2221 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002222 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2223 /* skip data nodes from different modules */
2224 continue;
2225 }
2226
Radek Krejci857189e2020-09-01 13:26:36 +02002227 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002228 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2229 if (ret) {
2230 break;
2231 }
Michal Vasko4490d312020-06-16 13:08:55 +02002232 if (first && !sibling_src) {
2233 /* source was spent (unlinked), move to the next node */
2234 source = tmp;
2235 }
2236
Michal Vasko3a41dff2020-07-15 14:30:28 +02002237 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002238 break;
2239 }
2240 }
2241
2242 if (options & LYD_MERGE_DESTRUCT) {
2243 /* free any leftover source data that were not merged */
2244 lyd_free_siblings((struct lyd_node *)source);
2245 }
2246
Michal Vaskocd3f6172021-05-18 16:14:50 +02002247 lyd_dup_inst_free(dup_inst);
2248 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002249}
2250
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002251LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002252lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002253{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002254 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002255}
2256
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002257LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002258lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002259{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002260 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2261}
2262
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002263LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002264lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2265 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2266{
2267 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002268}
2269
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002270static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002271lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002272{
Michal Vasko14654712020-02-06 08:35:21 +01002273 /* ending \0 */
2274 ++reqlen;
2275
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002276 if (reqlen > *buflen) {
2277 if (is_static) {
2278 return LY_EINCOMPLETE;
2279 }
2280
2281 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2282 if (!*buffer) {
2283 return LY_EMEM;
2284 }
2285
2286 *buflen = reqlen;
2287 }
2288
2289 return LY_SUCCESS;
2290}
2291
Michal Vaskod59035b2020-07-08 12:00:06 +02002292LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002293lyd_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 +02002294{
2295 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002296 size_t len;
2297 const char *val;
2298 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002299
Michal Vasko824d0832021-11-04 15:36:51 +01002300 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002301 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002302 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002303 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002304
2305 quot = '\'';
2306 if (strchr(val, '\'')) {
2307 quot = '"';
2308 }
2309 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002310 }
2311
2312 return LY_SUCCESS;
2313}
2314
2315/**
2316 * @brief Append leaf-list value predicate to path.
2317 *
2318 * @param[in] node Node to print.
2319 * @param[in,out] buffer Buffer to print to.
2320 * @param[in,out] buflen Current buffer length.
2321 * @param[in,out] bufused Current number of characters used in @p buffer.
2322 * @param[in] is_static Whether buffer is static or can be reallocated.
2323 * @return LY_ERR
2324 */
2325static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002326lyd_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 +02002327{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002328 size_t len;
2329 const char *val;
2330 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002331
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002332 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002333 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002334 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002335
2336 quot = '\'';
2337 if (strchr(val, '\'')) {
2338 quot = '"';
2339 }
2340 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2341
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002342 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002343}
2344
2345/**
2346 * @brief Append node position (relative to its other instances) predicate to path.
2347 *
2348 * @param[in] node Node to print.
2349 * @param[in,out] buffer Buffer to print to.
2350 * @param[in,out] buflen Current buffer length.
2351 * @param[in,out] bufused Current number of characters used in @p buffer.
2352 * @param[in] is_static Whether buffer is static or can be reallocated.
2353 * @return LY_ERR
2354 */
2355static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002356lyd_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 +02002357{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002358 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002359 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002360 char *val = NULL;
2361 LY_ERR rc;
2362
Michal Vasko50cc0562021-05-18 16:15:43 +02002363 pos = lyd_list_pos(node);
2364 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002365 return LY_EMEM;
2366 }
2367
2368 len = 1 + strlen(val) + 1;
2369 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2370 if (rc != LY_SUCCESS) {
2371 goto cleanup;
2372 }
2373
2374 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2375
2376cleanup:
2377 free(val);
2378 return rc;
2379}
2380
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002381LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002382lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2383{
Radek Krejci857189e2020-09-01 13:26:36 +02002384 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002385 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002386 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002387 const struct lyd_node *iter, *parent;
2388 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002389 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002390
2391 LY_CHECK_ARG_RET(NULL, node, NULL);
2392 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002393 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002394 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002395 } else {
2396 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002397 }
2398
2399 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002400 case LYD_PATH_STD:
2401 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002402 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002403 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002404 ++depth;
2405 }
2406
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002407 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002408 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002409 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002410 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002411iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002412 /* get the module */
2413 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2414 parent = lyd_parent(iter);
2415 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2416 if (prev_mod == mod) {
2417 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002418 }
2419
2420 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002421 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2422 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002423 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2424 if (rc != LY_SUCCESS) {
2425 break;
2426 }
2427
2428 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002429 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002430
Michal Vasko790b2bc2020-08-03 13:35:06 +02002431 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002432 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002433 switch (iter->schema->nodetype) {
2434 case LYS_LIST:
2435 if (iter->schema->flags & LYS_KEYLESS) {
2436 /* print its position */
2437 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2438 } else {
2439 /* print all list keys in predicates */
2440 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2441 }
2442 break;
2443 case LYS_LEAFLIST:
2444 if (iter->schema->flags & LYS_CONFIG_W) {
2445 /* print leaf-list value */
2446 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2447 } else {
2448 /* print its position */
2449 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2450 }
2451 break;
2452 default:
2453 /* nothing to print more */
2454 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002455 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002456 }
2457 if (rc != LY_SUCCESS) {
2458 break;
2459 }
2460
Michal Vasko14654712020-02-06 08:35:21 +01002461 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002462 }
2463 break;
2464 }
2465
2466 return buffer;
2467}
Michal Vaskoe444f752020-02-10 12:20:06 +01002468
Michal Vaskodbf3e652022-10-21 08:46:25 +02002469char *
2470lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2471{
2472 uint32_t depth;
2473 size_t bufused = 0, buflen = 0, len;
2474 char *buffer = NULL;
2475 const struct lyd_node *iter, *parent;
2476 const struct lys_module *mod, *prev_mod;
2477 LY_ERR rc = LY_SUCCESS;
2478
2479 switch (pathtype) {
2480 case LYD_PATH_STD:
2481 case LYD_PATH_STD_NO_LAST_PRED:
2482 for (depth = 1; depth <= dnodes->count; ++depth) {
2483 /* current node */
2484 iter = dnodes->dnodes[depth - 1];
2485 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2486
2487 /* parent */
2488 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002489 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2490 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko8c320b82022-11-21 15:51:57 +01002491 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002492
2493 /* get module to print, if any */
2494 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2495 if (prev_mod == mod) {
2496 mod = NULL;
2497 }
2498
2499 /* realloc string */
2500 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2501 strlen(((struct lyd_node_opaq *)iter)->name.name));
2502 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2503 break;
2504 }
2505
2506 /* print next node */
2507 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2508
2509 /* do not always print the last (first) predicate */
2510 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2511 switch (iter->schema->nodetype) {
2512 case LYS_LIST:
2513 if (iter->schema->flags & LYS_KEYLESS) {
2514 /* print its position */
2515 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2516 } else {
2517 /* print all list keys in predicates */
2518 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2519 }
2520 break;
2521 case LYS_LEAFLIST:
2522 if (iter->schema->flags & LYS_CONFIG_W) {
2523 /* print leaf-list value */
2524 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2525 } else {
2526 /* print its position */
2527 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2528 }
2529 break;
2530 default:
2531 /* nothing to print more */
2532 break;
2533 }
2534 }
2535 if (rc) {
2536 break;
2537 }
2538 }
2539 break;
2540 }
2541
2542 return buffer;
2543}
2544
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002545LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002546lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2547{
2548 struct lyd_meta *ret = NULL;
2549 const struct ly_ctx *ctx;
2550 const char *prefix, *tmp;
2551 char *str;
2552 size_t pref_len, name_len;
2553
2554 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002555 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002556
2557 if (!first) {
2558 return NULL;
2559 }
2560
2561 ctx = first->annotation->module->ctx;
2562
2563 /* parse the name */
2564 tmp = name;
2565 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2566 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2567 return NULL;
2568 }
2569
2570 /* find the module */
2571 if (prefix) {
2572 str = strndup(prefix, pref_len);
2573 module = ly_ctx_get_module_latest(ctx, str);
2574 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002575 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 +02002576 }
2577
2578 /* find the metadata */
2579 LY_LIST_FOR(first, first) {
2580 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2581 ret = (struct lyd_meta *)first;
2582 break;
2583 }
2584 }
2585
2586 return ret;
2587}
2588
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002589LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002590lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2591{
Michal Vasko9beceb82022-04-05 12:14:15 +02002592 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002593 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002594 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002595
Michal Vaskof03ed032020-03-04 13:31:44 +01002596 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002597 if (!siblings) {
2598 /* no data */
2599 if (match) {
2600 *match = NULL;
2601 }
2602 return LY_ENOTFOUND;
2603 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002604
Michal Vasko9beceb82022-04-05 12:14:15 +02002605 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2606 /* create a duplicate in this context */
2607 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2608 target = dup;
2609 }
2610
2611 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2612 /* schema mismatch */
2613 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002614 if (match) {
2615 *match = NULL;
2616 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002617 return LY_ENOTFOUND;
2618 }
2619
Michal Vaskoe78faec2021-04-08 17:24:43 +02002620 /* get first sibling */
2621 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002622
Michal Vasko9e685082021-01-29 14:49:09 +01002623 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002624 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002625 assert(target->hash);
2626
Michal Vaskoe78faec2021-04-08 17:24:43 +02002627 if (lysc_is_dup_inst_list(target->schema)) {
2628 /* we must search the instances from beginning to find the first matching one */
2629 found = 0;
2630 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002631 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002632 found = 1;
2633 break;
2634 }
2635 }
2636 if (found) {
2637 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002638 } else {
2639 siblings = NULL;
2640 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002641 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002642 /* find by hash */
2643 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2644 siblings = *match_p;
2645 } else {
2646 /* not found */
2647 siblings = NULL;
2648 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002649 }
2650 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002651 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002652 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002653 if (lysc_is_dup_inst_list(target->schema)) {
2654 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2655 break;
2656 }
2657 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002658 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002659 break;
2660 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002661 }
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;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002747 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002748
Michal Vasko83ae7772022-06-08 10:01:55 +02002749 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002750 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002751
2752 LY_CHECK_RET(ly_set_new(set));
2753
2754 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2755 /* no data or schema mismatch */
2756 return LY_ENOTFOUND;
2757 }
2758
Michal Vaskoee9b9482023-06-19 13:17:48 +02002759 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002760 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002761
Michal Vaskoe78faec2021-04-08 17:24:43 +02002762 /* get first sibling */
2763 siblings = lyd_first_sibling(siblings);
2764
2765 parent = siblings->parent;
2766 if (parent && parent->schema && parent->children_ht) {
2767 assert(target->hash);
2768
2769 /* find the first instance */
2770 lyd_find_sibling_first(siblings, target, &first);
2771 if (first) {
2772 /* add it so that it is the first in the set */
2773 if (ly_set_add(*set, first, 1, NULL)) {
2774 goto error;
2775 }
2776
2777 /* find by hash */
2778 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2779 iter = *match_p;
2780 } else {
2781 /* not found */
2782 iter = NULL;
2783 }
2784 while (iter) {
2785 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002786 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002787 goto error;
2788 }
2789
2790 /* find next instance */
2791 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2792 iter = NULL;
2793 } else {
2794 iter = *match_p;
2795 }
2796 }
2797 }
2798 } else {
2799 /* no children hash table */
2800 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002801 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002802 ly_set_add(*set, (void *)siblings, 1, NULL);
2803 }
2804 }
2805 }
2806
2807 if (!(*set)->count) {
2808 return LY_ENOTFOUND;
2809 }
2810 return LY_SUCCESS;
2811
2812error:
2813 ly_set_free(*set, NULL);
2814 *set = NULL;
2815 return LY_EMEM;
2816}
2817
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002818LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002819lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2820{
2821 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2822
2823 for ( ; first; first = first->next) {
2824 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2825 break;
2826 }
2827 }
2828
2829 if (match) {
2830 *match = (struct lyd_node *)first;
2831 }
2832 return first ? LY_SUCCESS : LY_ENOTFOUND;
2833}
2834
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002835LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002836lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002837{
Michal Vaskod96e2372023-02-24 16:07:51 +01002838 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002839
Michal Vaskod96e2372023-02-24 16:07:51 +01002840 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2841}
Michal Vaskoccc02342020-05-21 10:09:21 +02002842
Michal Vaskod96e2372023-02-24 16:07:51 +01002843LIBYANG_API_DEF LY_ERR
2844lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2845{
2846 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002847
Michal Vaskod96e2372023-02-24 16:07:51 +01002848 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002849}
Radek Krejcica989142020-11-05 11:32:22 +01002850
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002851LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002852lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2853 const struct lyxp_var *vars, struct ly_set **set)
2854{
2855 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2856
2857 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2858}
2859
2860LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002861lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2862 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002863{
Michal Vaskod96e2372023-02-24 16:07:51 +01002864 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002865
Michal Vaskod96e2372023-02-24 16:07:51 +01002866 *set = NULL;
2867
2868 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 +01002869}
2870
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002871LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002872lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002873{
Michal Vaskod96e2372023-02-24 16:07:51 +01002874 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002875}
2876
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002877LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002878lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2879{
2880 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2881}
2882
2883LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002884lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2885 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002886{
Michal Vaskod96e2372023-02-24 16:07:51 +01002887 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2888}
2889
2890LIBYANG_API_DEF LY_ERR
2891lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2892 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2893 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2894{
2895 LY_ERR ret = LY_SUCCESS;
2896 struct lyxp_set xp_set = {0};
2897 struct lyxp_expr *exp = NULL;
2898 uint32_t i;
2899
2900 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2901 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2902 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2903
2904 /* parse expression */
2905 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2906 LY_CHECK_GOTO(ret, cleanup);
2907
2908 /* evaluate expression */
2909 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2910 LYXP_IGNORE_WHEN);
2911 LY_CHECK_GOTO(ret, cleanup);
2912
2913 /* return expected result type without or with casting */
2914 if (node_set) {
2915 /* node set */
2916 if (xp_set.type == LYXP_SET_NODE_SET) {
2917 /* transform into a set */
2918 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2919 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2920 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2921 (*node_set)->size = xp_set.used;
2922 for (i = 0; i < xp_set.used; ++i) {
2923 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2924 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2925 LY_CHECK_GOTO(ret, cleanup);
2926 }
2927 }
2928 if (ret_type) {
2929 *ret_type = LY_XPATH_NODE_SET;
2930 }
2931 } else if (!string && !number && !boolean) {
2932 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2933 ret = LY_EINVAL;
2934 goto cleanup;
2935 }
2936 }
2937
2938 if (string) {
2939 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2940 /* cast into string */
2941 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2942 }
2943 if (xp_set.type == LYXP_SET_STRING) {
2944 /* string */
2945 *string = xp_set.val.str;
2946 xp_set.val.str = NULL;
2947 if (ret_type) {
2948 *ret_type = LY_XPATH_STRING;
2949 }
2950 }
2951 }
2952
2953 if (number) {
2954 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2955 /* cast into number */
2956 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2957 }
2958 if (xp_set.type == LYXP_SET_NUMBER) {
2959 /* number */
2960 *number = xp_set.val.num;
2961 if (ret_type) {
2962 *ret_type = LY_XPATH_NUMBER;
2963 }
2964 }
2965 }
2966
2967 if (boolean) {
2968 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2969 /* cast into boolean */
2970 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2971 }
2972 if (xp_set.type == LYXP_SET_BOOLEAN) {
2973 /* boolean */
2974 *boolean = xp_set.val.bln;
2975 if (ret_type) {
2976 *ret_type = LY_XPATH_BOOLEAN;
2977 }
2978 }
2979 }
2980
2981cleanup:
2982 lyxp_set_free_content(&xp_set);
2983 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
2984 return ret;
aPiecekfba75362021-10-07 12:39:48 +02002985}
2986
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002987LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002988lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2989{
2990 LY_ERR ret = LY_SUCCESS;
2991 struct lyxp_expr *expr = NULL;
2992 struct ly_path *lypath = NULL;
2993
2994 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2995
2996 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002997 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 +01002998 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002999 LY_CHECK_GOTO(ret, cleanup);
3000
3001 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003002 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003003 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 +01003004 LY_CHECK_GOTO(ret, cleanup);
3005
3006 /* evaluate the path */
Michal Vasko90189962023-02-28 12:10:34 +01003007 ret = ly_path_eval_partial(lypath, ctx_node, NULL, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003008
3009cleanup:
3010 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3011 ly_path_free(LYD_CTX(ctx_node), lypath);
3012 return ret;
3013}
3014
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003015LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003016lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3017{
3018 LY_ERR ret;
3019 struct lyd_node *m;
3020
3021 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3022
Michal Vasko90189962023-02-28 12:10:34 +01003023 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003024 if (ret) {
3025 if (match) {
3026 *match = NULL;
3027 }
3028 return LY_ENOTFOUND;
3029 }
3030
3031 if (match) {
3032 *match = m;
3033 }
3034 return LY_SUCCESS;
3035}