blob: de5a120bf54eab74ab5c1f779092170d17879d59 [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 }
413 if (tree && ((format != LYD_XML) || !envp)) {
414 *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 Vaskob104f112020-07-17 09:54:54 +0200492
493 found = 0;
494 LY_LIST_FOR(match, match) {
495 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
496 /* we have found an opaque node, which must be at the end, so use it OR
497 * modules do not match, so we must have traversed all the data from new_node module (if any),
498 * we have found the first node of the next module, that is what we want */
499 break;
500 }
501
502 /* skip schema nodes until we find the instantiated one */
503 while (!found) {
504 if (new_node->schema == schema) {
505 /* we have found the schema of the new node, continue search to find the first
506 * data node with a different schema (after our schema) */
507 found = 1;
508 break;
509 }
510 if (match->schema == schema) {
511 /* current node (match) is a data node still before the new node, continue search in data */
512 break;
513 }
Michal Vasko93b16062020-12-09 18:14:18 +0100514 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200515 assert(schema);
516 }
517
518 if (found && (match->schema != new_node->schema)) {
519 /* find the next node after we have found our node schema data instance */
520 break;
521 }
522 }
Michal Vasko90932a92020-02-12 14:33:03 +0100523 }
524
525 return match;
526}
527
Michal Vaskoc61dd062022-06-07 11:01:28 +0200528void
Michal Vaskof03ed032020-03-04 13:31:44 +0100529lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100530{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100531 struct lyd_node_inner *par;
532
Michal Vasko90932a92020-02-12 14:33:03 +0100533 assert(!node->next && (node->prev == node));
534
535 node->next = sibling->next;
536 node->prev = sibling;
537 sibling->next = node;
538 if (node->next) {
539 /* sibling had a succeeding node */
540 node->next->prev = node;
541 } else {
542 /* sibling was last, find first sibling and change its prev */
543 if (sibling->parent) {
544 sibling = sibling->parent->child;
545 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200546 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100547 }
548 sibling->prev = node;
549 }
550 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100551
Michal Vasko9f96a052020-03-10 09:41:45 +0100552 for (par = node->parent; par; par = par->parent) {
553 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
554 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100555 par->flags &= ~LYD_DEFAULT;
556 }
557 }
Michal Vasko90932a92020-02-12 14:33:03 +0100558}
559
Michal Vaskoc61dd062022-06-07 11:01:28 +0200560void
Michal Vaskof03ed032020-03-04 13:31:44 +0100561lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100562{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100563 struct lyd_node_inner *par;
564
Michal Vasko90932a92020-02-12 14:33:03 +0100565 assert(!node->next && (node->prev == node));
566
567 node->next = sibling;
568 /* covers situation of sibling being first */
569 node->prev = sibling->prev;
570 sibling->prev = node;
571 if (node->prev->next) {
572 /* sibling had a preceding node */
573 node->prev->next = node;
574 } else if (sibling->parent) {
575 /* sibling was first and we must also change parent child pointer */
576 sibling->parent->child = node;
577 }
578 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100579
Michal Vasko9f96a052020-03-10 09:41:45 +0100580 for (par = node->parent; par; par = par->parent) {
581 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
582 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100583 par->flags &= ~LYD_DEFAULT;
584 }
585 }
Michal Vasko90932a92020-02-12 14:33:03 +0100586}
587
588/**
Michal Vaskob104f112020-07-17 09:54:54 +0200589 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100590 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100591 * Handles inserting into NP containers and key-less lists.
592 *
Michal Vasko90932a92020-02-12 14:33:03 +0100593 * @param[in] parent Parent to insert into.
594 * @param[in] node Node to insert.
595 */
596static void
Michal Vaskob104f112020-07-17 09:54:54 +0200597lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100598{
599 struct lyd_node_inner *par;
600
Radek Krejcia1c1e542020-09-29 16:06:52 +0200601 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100602 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100603
604 par = (struct lyd_node_inner *)parent;
605
Michal Vaskob104f112020-07-17 09:54:54 +0200606 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100607 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100608
Michal Vaskod989ba02020-08-24 10:59:24 +0200609 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100610 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
611 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100612 par->flags &= ~LYD_DEFAULT;
613 }
614 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200615}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100616
Michal Vasko751cb4d2020-07-14 12:25:28 +0200617/**
618 * @brief Learn whether a list instance has all the keys.
619 *
620 * @param[in] list List instance to check.
621 * @return non-zero if all the keys were found,
622 * @return 0 otherwise.
623 */
624static int
625lyd_insert_has_keys(const struct lyd_node *list)
626{
627 const struct lyd_node *key;
628 const struct lysc_node *skey = NULL;
629
630 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200631 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200632 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
633 if (!key || (key->schema != skey)) {
634 /* key missing */
635 return 0;
636 }
637
638 key = key->next;
639 }
640
641 /* all keys found */
642 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100643}
644
645void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200646lyd_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 +0100647{
Michal Vaskob104f112020-07-17 09:54:54 +0200648 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100649
Michal Vaskob104f112020-07-17 09:54:54 +0200650 /* inserting list without its keys is not supported */
651 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100652 assert(!parent || !parent->schema ||
653 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100654
Michal Vaskob104f112020-07-17 09:54:54 +0200655 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100656 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100657 }
Michal Vasko90932a92020-02-12 14:33:03 +0100658
Michal Vaskob104f112020-07-17 09:54:54 +0200659 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100660 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100661
Michal Vasko9beceb82022-04-05 12:14:15 +0200662 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200663 /* no next anchor */
664 anchor = NULL;
665 } else {
666 /* find the anchor, our next node, so we can insert before it */
667 anchor = lyd_insert_get_next_anchor(first_sibling, node);
668 }
669
Michal Vaskob104f112020-07-17 09:54:54 +0200670 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200671 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200672 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100673 if (!parent && (*first_sibling_p == anchor)) {
674 /* move first sibling */
675 *first_sibling_p = node;
676 }
Michal Vaskob104f112020-07-17 09:54:54 +0200677 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200678 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200679 lyd_insert_after_node(first_sibling->prev, node);
680 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200681 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200682 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100683 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200684 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200685 *first_sibling_p = node;
686 }
687
688 /* insert into parent HT */
689 lyd_insert_hash(node);
690
691 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200692 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200693 lyd_hash(parent);
694
695 /* now we can insert even the list into its parent HT */
696 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100697 }
Michal Vasko90932a92020-02-12 14:33:03 +0100698}
699
Michal Vasko717a4c32020-12-07 09:40:10 +0100700/**
701 * @brief Check schema place of a node to be inserted.
702 *
703 * @param[in] parent Schema node of the parent data node.
704 * @param[in] sibling Schema node of a sibling data node.
705 * @param[in] schema Schema node if the data node to be inserted.
706 * @return LY_SUCCESS on success.
707 * @return LY_EINVAL if the place is invalid.
708 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100709static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100710lyd_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 +0100711{
712 const struct lysc_node *par2;
713
Michal Vasko62ed12d2020-05-21 10:08:25 +0200714 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100715 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
716 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100717
Michal Vasko717a4c32020-12-07 09:40:10 +0100718 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100719 /* opaque nodes can be inserted wherever */
720 return LY_SUCCESS;
721 }
722
Michal Vasko717a4c32020-12-07 09:40:10 +0100723 if (!parent) {
724 parent = lysc_data_parent(sibling);
725 }
726
Michal Vaskof03ed032020-03-04 13:31:44 +0100727 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200728 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100729
730 if (parent) {
731 /* inner node */
732 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200733 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200734 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100735 return LY_EINVAL;
736 }
737 } else {
738 /* top-level node */
739 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200740 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100741 return LY_EINVAL;
742 }
743 }
744
745 return LY_SUCCESS;
746}
747
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100748LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200749lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100750{
751 struct lyd_node *iter;
752
Michal Vasko0ab974d2021-02-24 13:18:26 +0100753 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100754 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100755
Michal Vasko717a4c32020-12-07 09:40:10 +0100756 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100757
Michal Vasko0ab974d2021-02-24 13:18:26 +0100758 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100759 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100760 return LY_EINVAL;
761 }
762
763 if (node->parent || node->prev->next) {
764 lyd_unlink_tree(node);
765 }
766
767 while (node) {
768 iter = node->next;
769 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200770 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100771 node = iter;
772 }
773 return LY_SUCCESS;
774}
775
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100776LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200777lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100778{
779 struct lyd_node *iter;
780
781 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
782 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
783
784 if (first->schema && (first->schema->flags & LYS_KEY)) {
785 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
786 return LY_EINVAL;
787 }
788
789 while (first) {
790 iter = first->next;
791 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100792 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100793 first = iter;
794 }
795 return LY_SUCCESS;
796}
797
798LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200799lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100800{
801 struct lyd_node *iter;
802
Michal Vaskob104f112020-07-17 09:54:54 +0200803 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100804
Michal Vaskob104f112020-07-17 09:54:54 +0200805 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100806 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100807 }
808
Michal Vasko553d0b52020-12-04 16:27:52 +0100809 if (sibling == node) {
810 /* we need to keep the connection to siblings so we can insert into them */
811 sibling = sibling->prev;
812 }
813
Michal Vaskob1b5c262020-03-05 14:29:47 +0100814 if (node->parent || node->prev->next) {
815 lyd_unlink_tree(node);
816 }
817
818 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100819 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200820 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200821 return LY_EINVAL;
822 }
823
Michal Vaskob1b5c262020-03-05 14:29:47 +0100824 iter = node->next;
825 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200826 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100827 node = iter;
828 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100829
Michal Vaskob104f112020-07-17 09:54:54 +0200830 if (first) {
831 /* find the first sibling */
832 *first = sibling;
833 while ((*first)->prev->next) {
834 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100835 }
836 }
837
838 return LY_SUCCESS;
839}
840
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100841LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100842lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
843{
Michal Vaskobce230b2022-04-19 09:55:31 +0200844 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100845 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100846
Michal Vasko717a4c32020-12-07 09:40:10 +0100847 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100848
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200849 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200850 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100851 return LY_EINVAL;
852 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200853 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
854 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100855 return LY_EINVAL;
856 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100857
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100858 lyd_unlink_tree(node);
859 lyd_insert_before_node(sibling, node);
860 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100861
Michal Vaskof03ed032020-03-04 13:31:44 +0100862 return LY_SUCCESS;
863}
864
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100865LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100866lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
867{
Michal Vaskobce230b2022-04-19 09:55:31 +0200868 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100869 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100870
Michal Vasko717a4c32020-12-07 09:40:10 +0100871 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100872
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200873 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200874 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100875 return LY_EINVAL;
876 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200877 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
878 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100879 return LY_EINVAL;
880 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100881
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100882 lyd_unlink_tree(node);
883 lyd_insert_after_node(sibling, node);
884 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100885
Michal Vaskof03ed032020-03-04 13:31:44 +0100886 return LY_SUCCESS;
887}
888
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100889LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200890lyd_unlink_siblings(struct lyd_node *node)
891{
892 struct lyd_node *next, *elem, *first = NULL;
893
894 LY_LIST_FOR_SAFE(node, next, elem) {
895 lyd_unlink_tree(elem);
896 lyd_insert_node(NULL, &first, elem, 1);
897 }
898}
899
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100900LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100901lyd_unlink_tree(struct lyd_node *node)
902{
903 struct lyd_node *iter;
904
905 if (!node) {
906 return;
907 }
908
Michal Vaskob104f112020-07-17 09:54:54 +0200909 /* update hashes while still linked into the tree */
910 lyd_unlink_hash(node);
911
Michal Vaskof03ed032020-03-04 13:31:44 +0100912 /* unlink from siblings */
913 if (node->prev->next) {
914 node->prev->next = node->next;
915 }
916 if (node->next) {
917 node->next->prev = node->prev;
918 } else {
919 /* unlinking the last node */
920 if (node->parent) {
921 iter = node->parent->child;
922 } else {
923 iter = node->prev;
924 while (iter->prev != node) {
925 iter = iter->prev;
926 }
927 }
928 /* update the "last" pointer from the first node */
929 iter->prev = node->prev;
930 }
931
932 /* unlink from parent */
933 if (node->parent) {
934 if (node->parent->child == node) {
935 /* the node is the first child */
936 node->parent->child = node->next;
937 }
938
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200939 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100940 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200941
Michal Vaskof03ed032020-03-04 13:31:44 +0100942 node->parent = NULL;
943 }
944
945 node->next = NULL;
946 node->prev = node;
947}
948
Michal Vaskoa5da3292020-08-12 13:10:50 +0200949void
Michal Vasko871a0252020-11-11 18:35:24 +0100950lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200951{
952 struct lyd_meta *last, *iter;
953
954 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200955
956 if (!meta) {
957 return;
958 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200959
960 for (iter = meta; iter; iter = iter->next) {
961 iter->parent = parent;
962 }
963
964 /* insert as the last attribute */
965 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200966 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200967 last->next = meta;
968 } else {
969 parent->meta = meta;
970 }
971
972 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100973 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100975 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200976 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200977}
978
979LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100980lyd_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 +0200981 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 +0100982 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 +0100983{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100984 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100985 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200986 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100987 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200988 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100989
Michal Vasko9f96a052020-03-10 09:41:45 +0100990 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100991
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200992 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200993 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200994 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100995 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200996 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100997 break;
998 }
999 }
1000 if (!ant) {
1001 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001003 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001004 ret = LY_EINVAL;
1005 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001006 }
1007
Michal Vasko9f96a052020-03-10 09:41:45 +01001008 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001009 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001010 mt->parent = parent;
1011 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001012 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001013 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 +01001014 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001015 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1016 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1017 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001018
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001019 /* insert as the last attribute */
1020 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001021 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001022 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001023 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001024 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001025 }
1026
Michal Vasko9f96a052020-03-10 09:41:45 +01001027 if (meta) {
1028 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001029 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001030
1031cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001032 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001033}
1034
Michal Vaskoa5da3292020-08-12 13:10:50 +02001035void
1036lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1037{
1038 struct lyd_attr *last, *iter;
1039 struct lyd_node_opaq *opaq;
1040
1041 assert(parent && !parent->schema);
1042
1043 if (!attr) {
1044 return;
1045 }
1046
1047 opaq = (struct lyd_node_opaq *)parent;
1048 for (iter = attr; iter; iter = iter->next) {
1049 iter->parent = opaq;
1050 }
1051
1052 /* insert as the last attribute */
1053 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001054 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001055 last->next = attr;
1056 } else {
1057 opaq->attr = attr;
1058 }
1059}
1060
Michal Vasko52927e22020-03-16 17:26:14 +01001061LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001062lyd_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 +01001063 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 +02001064 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 +01001065{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001066 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001067 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001068
1069 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001070 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001071
Michal Vasko2a3722d2021-06-16 11:52:39 +02001072 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001073 value = "";
1074 }
1075
1076 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001077 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001078
Michal Vaskoad92b672020-11-12 13:11:31 +01001079 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001080 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001081 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001082 }
1083 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001084 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001085 }
1086
Michal Vasko52927e22020-03-16 17:26:14 +01001087 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001088 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1089 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001090 *dynamic = 0;
1091 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001092 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001093 }
Michal Vasko501af032020-11-11 20:27:44 +01001094 at->format = format;
1095 at->val_prefix_data = val_prefix_data;
1096 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001097
1098 /* insert as the last attribute */
1099 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001100 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001101 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001102 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001103 last->next = at;
1104 }
1105
Radek Krejci011e4aa2020-09-04 15:22:31 +02001106finish:
1107 if (ret) {
1108 lyd_free_attr_single(ctx, at);
1109 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001110 *attr = at;
1111 }
1112 return LY_SUCCESS;
1113}
1114
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001115LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001116lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001117{
Michal Vasko90189962023-02-28 12:10:34 +01001118 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001119
Michal Vasko90189962023-02-28 12:10:34 +01001120 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001121
Michal Vasko004d3152020-06-11 19:59:22 +02001122 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001123}
1124
aPiecek2f63f952021-03-30 12:22:18 +02001125/**
1126 * @brief Check the equality of the two schemas from different contexts.
1127 *
1128 * @param schema1 of first node.
1129 * @param schema2 of second node.
1130 * @return 1 if the schemas are equal otherwise 0.
1131 */
1132static ly_bool
1133lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1134{
1135 if (!schema1 && !schema2) {
1136 return 1;
1137 } else if (!schema1 || !schema2) {
1138 return 0;
1139 }
1140
1141 assert(schema1->module->ctx != schema2->module->ctx);
1142
1143 if (schema1->nodetype != schema2->nodetype) {
1144 return 0;
1145 }
1146
1147 if (strcmp(schema1->name, schema2->name)) {
1148 return 0;
1149 }
1150
1151 if (strcmp(schema1->module->name, schema2->module->name)) {
1152 return 0;
1153 }
1154
aPiecek2f63f952021-03-30 12:22:18 +02001155 return 1;
1156}
1157
1158/**
1159 * @brief Check the equality of the schemas for all parent nodes.
1160 *
1161 * Both nodes must be from different contexts.
1162 *
1163 * @param node1 Data of first node.
1164 * @param node2 Data of second node.
1165 * @return 1 if the all related parental schemas are equal otherwise 0.
1166 */
1167static ly_bool
1168lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1169{
1170 const struct lysc_node *parent1, *parent2;
1171
1172 assert(node1 && node2);
1173
1174 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1175 parent1 && parent2;
1176 parent1 = parent1->parent, parent2 = parent2->parent) {
1177 if (!lyd_compare_schema_equal(parent1, parent2)) {
1178 return 0;
1179 }
1180 }
1181
1182 if (parent1 || parent2) {
1183 return 0;
1184 }
1185
1186 return 1;
1187}
1188
1189/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001190 * @brief Compare 2 nodes values including opaque node values.
1191 *
1192 * @param[in] node1 First node to compare.
1193 * @param[in] node2 Second node to compare.
1194 * @return LY_SUCCESS if equal.
1195 * @return LY_ENOT if not equal.
1196 * @return LY_ERR on error.
1197 */
1198static LY_ERR
1199lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1200{
1201 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1202 const char *val1, *val2, *col;
1203 const struct lys_module *mod;
1204 char *val_dyn = NULL;
1205 LY_ERR rc = LY_SUCCESS;
1206
1207 if (!node1->schema) {
1208 opaq1 = (struct lyd_node_opaq *)node1;
1209 }
1210 if (!node2->schema) {
1211 opaq2 = (struct lyd_node_opaq *)node2;
1212 }
1213
1214 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1215 /* opaque XML and opaque XML node */
1216 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1217 opaq2->val_prefix_data)) {
1218 return LY_ENOT;
1219 }
1220 return LY_SUCCESS;
1221 }
1222
1223 /* get their values */
1224 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1225 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1226 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1227 if (!mod) {
1228 /* unable to compare */
1229 return LY_ENOT;
1230 }
1231
1232 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1233 LOGMEM(LYD_CTX(node1));
1234 return LY_EMEM;
1235 }
1236 val1 = val_dyn;
1237 } else {
1238 val1 = lyd_get_value(node1);
1239 }
1240 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1241 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1242 if (!mod) {
1243 return LY_ENOT;
1244 }
1245
1246 assert(!val_dyn);
1247 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1248 LOGMEM(LYD_CTX(node2));
1249 return LY_EMEM;
1250 }
1251 val2 = val_dyn;
1252 } else {
1253 val2 = lyd_get_value(node2);
1254 }
1255
1256 /* compare values */
1257 if (strcmp(val1, val2)) {
1258 rc = LY_ENOT;
1259 }
1260
1261 free(val_dyn);
1262 return rc;
1263}
1264
1265/**
Michal Vaskof277d362023-04-24 09:08:31 +02001266 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1267 *
1268 * Works correctly even if @p node1 and @p node2 have different contexts.
1269 *
1270 * @param[in] node1 The first node to compare.
1271 * @param[in] node2 The second node to compare.
1272 * @param[in] options Various @ref datacompareoptions.
1273 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1274 * @return LY_SUCCESS if the nodes are equivalent.
1275 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001276 */
1277static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001278lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001279 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001280{
aPiecek2f63f952021-03-30 12:22:18 +02001281 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1282 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001283 if (options & LYD_COMPARE_OPAQ) {
1284 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1285 return LY_ENOT;
1286 }
1287 } else {
1288 if (node1->schema != node2->schema) {
1289 return LY_ENOT;
1290 }
aPiecek2f63f952021-03-30 12:22:18 +02001291 }
1292 } else {
1293 /* different contexts */
1294 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1295 return LY_ENOT;
1296 }
1297 if (!parental_schemas_checked) {
1298 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1299 return LY_ENOT;
1300 }
1301 parental_schemas_checked = 1;
1302 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001303 }
1304
Michal Vaskof277d362023-04-24 09:08:31 +02001305 return LY_SUCCESS;
1306}
1307
1308/**
1309 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1310 *
1311 * Works correctly even if @p node1 and @p node2 have different contexts.
1312 *
1313 * @param[in] node1 The first node to compare.
1314 * @param[in] node2 The second node to compare.
1315 * @param[in] options Various @ref datacompareoptions.
1316 * @return LY_SUCCESS if the nodes are equivalent.
1317 * @return LY_ENOT if the nodes are not equivalent.
1318 */
1319static LY_ERR
1320lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1321{
1322 const struct lyd_node *iter1, *iter2;
1323 struct lyd_node_any *any1, *any2;
1324 int len1, len2;
1325 LY_ERR r;
1326
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001327 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001328 return LY_ENOT;
1329 }
aPiecek2f63f952021-03-30 12:22:18 +02001330 /* 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 +02001331
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001332 if (!node1->schema || !node2->schema) {
1333 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001334 return LY_ENOT;
1335 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001336 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1337 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1338 /* compare values only if there are any to compare */
1339 if ((r = lyd_compare_single_value(node1, node2))) {
1340 return r;
1341 }
Michal Vasko52927e22020-03-16 17:26:14 +01001342 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001343
Michal Vasko52927e22020-03-16 17:26:14 +01001344 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001345 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001346 }
1347 return LY_SUCCESS;
1348 } else {
1349 switch (node1->schema->nodetype) {
1350 case LYS_LEAF:
1351 case LYS_LEAFLIST:
1352 if (options & LYD_COMPARE_DEFAULTS) {
1353 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1354 return LY_ENOT;
1355 }
1356 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001357 if ((r = lyd_compare_single_value(node1, node2))) {
1358 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001359 }
1360
aPiecek2f63f952021-03-30 12:22:18 +02001361 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001362 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001363 case LYS_RPC:
1364 case LYS_ACTION:
1365 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001366 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001367 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001368 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001369 }
1370 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001371 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001372 iter1 = lyd_child(node1);
1373 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001374
Michal Vaskoee9b9482023-06-19 13:17:48 +02001375 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001376 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001377 } else if (node1->schema->flags & LYS_KEYLESS) {
1378 /* always equal */
1379 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001380 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001381
1382 /* lists with keys, their equivalence is based on their keys */
1383 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1384 key && (key->flags & LYS_KEY);
1385 key = key->next) {
1386 if (!iter1 || !iter2) {
1387 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1388 }
1389 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1390 LY_CHECK_RET(r);
1391 r = lyd_compare_single_data(iter1, iter2, options);
1392 LY_CHECK_RET(r);
1393
1394 iter1 = iter1->next;
1395 iter2 = iter2->next;
1396 }
1397
1398 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001399 case LYS_ANYXML:
1400 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001401 any1 = (struct lyd_node_any *)node1;
1402 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001403
1404 if (any1->value_type != any2->value_type) {
1405 return LY_ENOT;
1406 }
1407 switch (any1->value_type) {
1408 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001409 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001410 case LYD_ANYDATA_STRING:
1411 case LYD_ANYDATA_XML:
1412 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001413 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1414 return LY_ENOT;
1415 } else if (!any1->value.str && !any2->value.str) {
1416 return LY_SUCCESS;
1417 }
Michal Vasko52927e22020-03-16 17:26:14 +01001418 len1 = strlen(any1->value.str);
1419 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001420 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001421 return LY_ENOT;
1422 }
1423 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001424 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001425 len1 = lyd_lyb_data_length(any1->value.mem);
1426 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001427 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001428 return LY_ENOT;
1429 }
1430 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001431 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001432 }
1433 }
1434
Michal Vaskob7be7a82020-08-20 09:09:04 +02001435 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001436 return LY_EINT;
1437}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001438
Michal Vaskof277d362023-04-24 09:08:31 +02001439/**
1440 * @brief Compare all siblings at a node level.
1441 *
1442 * @param[in] node1 First sibling list.
1443 * @param[in] node2 Second sibling list.
1444 * @param[in] options Various @ref datacompareoptions.
1445 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1446 * @return LY_SUCCESS if equal.
1447 * @return LY_ENOT if not equal.
1448 * @return LY_ERR on error.
1449 */
1450static LY_ERR
1451lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1452 ly_bool parental_schemas_checked)
1453{
1454 LY_ERR r;
1455 const struct lyd_node *iter2;
1456
1457 while (node1 && node2) {
1458 /* schema match */
1459 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1460 LY_CHECK_RET(r);
1461
1462 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1463 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1464 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1465 /* find a matching instance in case they are ordered differently */
1466 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1467 if (r == LY_ENOTFOUND) {
1468 /* no matching instance, data not equal */
1469 r = LY_ENOT;
1470 }
1471 LY_CHECK_RET(r);
1472 } else {
1473 /* compare with the current node */
1474 iter2 = node2;
1475 }
1476
1477 /* data match */
1478 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1479 LY_CHECK_RET(r);
1480
1481 node1 = node1->next;
1482 node2 = node2->next;
1483 }
1484
1485 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1486}
1487
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001488LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001489lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1490{
Michal Vaskof277d362023-04-24 09:08:31 +02001491 LY_ERR r;
1492
1493 if (!node1 || !node2) {
1494 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1495 }
1496
1497 /* schema match */
1498 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1499 return r;
1500 }
1501
1502 /* data match */
1503 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001504}
1505
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001506LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001507lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001508{
Michal Vaskof277d362023-04-24 09:08:31 +02001509 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001510}
1511
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001512LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001513lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1514{
1515 if (!meta1 || !meta2) {
1516 if (meta1 == meta2) {
1517 return LY_SUCCESS;
1518 } else {
1519 return LY_ENOT;
1520 }
1521 }
1522
Michal Vaskoa8083062020-11-06 17:22:10 +01001523 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001524 return LY_ENOT;
1525 }
1526
Michal Vasko21725742020-06-29 11:49:25 +02001527 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1528}
1529
Radek Krejci22ebdba2019-07-25 13:59:43 +02001530/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001531 * @brief Create a copy of the attribute.
1532 *
1533 * @param[in] attr Attribute to copy.
1534 * @param[in] node Opaque where to append the new attribute.
1535 * @param[out] dup Optional created attribute copy.
1536 * @return LY_ERR value.
1537 */
1538static LY_ERR
1539lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1540{
1541 LY_ERR ret = LY_SUCCESS;
1542 struct lyd_attr *a, *last;
1543 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1544
1545 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1546
1547 /* create a copy */
1548 a = calloc(1, sizeof *attr);
1549 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1550
1551 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1552 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1553 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1554 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1555 a->hints = attr->hints;
1556 a->format = attr->format;
1557 if (attr->val_prefix_data) {
1558 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1559 LY_CHECK_GOTO(ret, finish);
1560 }
1561
1562 /* insert as the last attribute */
1563 a->parent = opaq;
1564 if (opaq->attr) {
1565 for (last = opaq->attr; last->next; last = last->next) {}
1566 last->next = a;
1567 } else {
1568 opaq->attr = a;
1569 }
1570
1571finish:
1572 if (ret) {
1573 lyd_free_attr_single(LYD_CTX(node), a);
1574 } else if (dup) {
1575 *dup = a;
1576 }
1577 return LY_SUCCESS;
1578}
1579
1580/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001581 * @brief Find @p schema equivalent in @p trg_ctx.
1582 *
1583 * @param[in] schema Schema node to find.
1584 * @param[in] trg_ctx Target context to search in.
1585 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001586 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001587 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1588 * @return LY_RRR value.
1589 */
1590static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001591lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1592 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001593{
Michal Vasko9beceb82022-04-05 12:14:15 +02001594 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1595 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001596 char *path;
1597
1598 if (!schema) {
1599 /* opaque node */
1600 *trg_schema = NULL;
1601 return LY_SUCCESS;
1602 }
1603
Michal Vasko9beceb82022-04-05 12:14:15 +02001604 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001605 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001606 trg_parent = parent->schema;
1607 src_parent = lysc_data_parent(schema);
1608 }
1609
1610 do {
1611 /* find the next parent */
1612 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001613 while (lysc_data_parent(sp) != src_parent) {
1614 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001615 }
1616 src_parent = sp;
1617
1618 if (!src_parent->parent) {
1619 /* find the module first */
1620 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1621 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001622 if (log) {
1623 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1624 src_parent->module->name);
1625 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001626 return LY_ENOTFOUND;
1627 }
1628 }
1629
1630 /* find the next parent */
1631 assert(trg_parent || trg_mod);
1632 tp = NULL;
1633 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1634 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1635 break;
1636 }
1637 }
1638 if (!tp) {
1639 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001640 if (log) {
1641 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1642 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1643 free(path);
1644 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001645 return LY_ENOTFOUND;
1646 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001647
Michal Vasko9beceb82022-04-05 12:14:15 +02001648 trg_parent = tp;
1649 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001650
Michal Vasko9beceb82022-04-05 12:14:15 +02001651 /* success */
1652 *trg_schema = trg_parent;
1653 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001654}
1655
1656/**
Michal Vasko52927e22020-03-16 17:26:14 +01001657 * @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 +02001658 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001659 * 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 +02001660 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001661 * @param[in] node Node to duplicate.
1662 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001663 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001664 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1665 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001666 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1667 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001668 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1669 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001670 */
Michal Vasko52927e22020-03-16 17:26:14 +01001671static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001672lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1673 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001674{
Michal Vasko52927e22020-03-16 17:26:14 +01001675 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001676 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001677 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001678 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001679 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001680 const struct lysc_type *type;
1681 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001682
Michal Vasko52927e22020-03-16 17:26:14 +01001683 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001684
Michal Vasko19175b62022-04-01 09:17:07 +02001685 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001686 if (options & LYD_DUP_NO_EXT) {
1687 /* no not duplicate this subtree */
1688 return LY_SUCCESS;
1689 }
1690
Michal Vasko19175b62022-04-01 09:17:07 +02001691 /* we need to use the same context */
1692 trg_ctx = LYD_CTX(node);
1693 }
1694
Michal Vasko52927e22020-03-16 17:26:14 +01001695 if (!node->schema) {
1696 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001697 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001698 } else {
1699 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001700 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001701 case LYS_ACTION:
1702 case LYS_NOTIF:
1703 case LYS_CONTAINER:
1704 case LYS_LIST:
1705 dup = calloc(1, sizeof(struct lyd_node_inner));
1706 break;
1707 case LYS_LEAF:
1708 case LYS_LEAFLIST:
1709 dup = calloc(1, sizeof(struct lyd_node_term));
1710 break;
1711 case LYS_ANYDATA:
1712 case LYS_ANYXML:
1713 dup = calloc(1, sizeof(struct lyd_node_any));
1714 break;
1715 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001716 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001717 ret = LY_EINT;
1718 goto error;
1719 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001720 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001721 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001722
Michal Vaskof6df0a02020-06-16 13:08:34 +02001723 if (options & LYD_DUP_WITH_FLAGS) {
1724 dup->flags = node->flags;
1725 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001726 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001727 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001728 if (trg_ctx == LYD_CTX(node)) {
1729 dup->schema = node->schema;
1730 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001731 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001732 if (ret) {
1733 /* has no schema but is not an opaque node */
1734 free(dup);
1735 dup = NULL;
1736 goto error;
1737 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001738 }
Michal Vasko52927e22020-03-16 17:26:14 +01001739 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001740
Michal Vasko9cf62422021-07-01 13:29:32 +02001741 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001742 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001743 if (!node->schema) {
1744 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1745 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1746 }
1747 } else {
1748 LY_LIST_FOR(node->meta, meta) {
1749 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1750 }
Michal Vasko25a32822020-07-09 15:48:22 +02001751 }
1752 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001753
1754 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001755 if (!dup->schema) {
1756 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1757 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1758 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001759
1760 if (options & LYD_DUP_RECURSIVE) {
1761 /* duplicate all the children */
1762 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001763 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001764 }
1765 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001766 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1767 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1768 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1769 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001770 opaq->hints = orig->hints;
1771 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001772 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001773 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001774 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001775 }
Michal Vasko52927e22020-03-16 17:26:14 +01001776 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1777 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1778 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1779
1780 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001781 if (trg_ctx == LYD_CTX(node)) {
1782 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1783 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1784 } else {
1785 /* store canonical value in the target context */
1786 val_can = lyd_get_value(node);
1787 type = ((struct lysc_node_leaf *)term->schema)->type;
1788 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1789 LYD_HINT_DATA, term->schema, NULL);
1790 LY_CHECK_GOTO(ret, error);
1791 }
Michal Vasko52927e22020-03-16 17:26:14 +01001792 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1793 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1794 struct lyd_node *child;
1795
1796 if (options & LYD_DUP_RECURSIVE) {
1797 /* duplicate all the children */
1798 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001799 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001800 }
Michal Vasko69730152020-10-09 16:30:07 +02001801 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001802 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001803 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001804 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001805 }
1806 }
1807 lyd_hash(dup);
1808 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001809 dup->hash = node->hash;
1810 any = (struct lyd_node_any *)node;
1811 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001812 }
1813
Michal Vasko52927e22020-03-16 17:26:14 +01001814 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001815 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001816
1817 if (dup_p) {
1818 *dup_p = dup;
1819 }
1820 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001821
1822error:
Michal Vasko52927e22020-03-16 17:26:14 +01001823 lyd_free_tree(dup);
1824 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001825}
1826
Michal Vasko29d674b2021-08-25 11:18:35 +02001827/**
1828 * @brief Get a parent node to connect duplicated subtree to.
1829 *
1830 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001831 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001832 * @param[in] parent Initial parent to connect to.
1833 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1834 * @param[out] dup_parent First duplicated parent node, if any.
1835 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1836 * @return LY_ERR value.
1837 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001838static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001839lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1840 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001841{
Michal Vasko58d89e92023-05-23 09:56:19 +02001842 const struct lyd_node *orig_parent;
1843 struct lyd_node *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001844 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001845
1846 *dup_parent = NULL;
1847 *local_parent = NULL;
1848
Michal Vasko83522192022-07-20 08:07:34 +02001849 if (node->flags & LYD_EXT) {
1850 ext_parent = 1;
1851 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001852 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001853 if (ext_parent) {
1854 /* use the standard context */
1855 trg_ctx = LYD_CTX(orig_parent);
1856 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001857 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001858 /* stop creating parents, connect what we have into the provided parent */
1859 iter = parent;
1860 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001861 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1862 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001863 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001864 iter = parent;
1865 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001866 } else {
1867 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001868 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001869 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001870
Michal Vasko3a41dff2020-07-15 14:30:28 +02001871 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001872 /* update local parent (created parent) */
1873 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001874 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001875
Michal Vasko3a41dff2020-07-15 14:30:28 +02001876 if (*dup_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001877 lyd_insert_node(iter, NULL, *dup_parent, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001878 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001879 *dup_parent = iter;
Michal Vasko83522192022-07-20 08:07:34 +02001880 if (orig_parent->flags & LYD_EXT) {
1881 ext_parent = 1;
1882 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001883 }
1884
1885 if (repeat && parent) {
1886 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001887 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1888 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001889 return LY_EINVAL;
1890 }
1891
1892 return LY_SUCCESS;
1893}
1894
1895static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001896lyd_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 +01001897 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001898{
1899 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001900 const struct lyd_node *orig; /* original node to be duplicated */
1901 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001902 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001903 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001904
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001905 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001906
1907 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001908 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 +02001909 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001910 } else {
1911 local_parent = parent;
1912 }
1913
Radek Krejci22ebdba2019-07-25 13:59:43 +02001914 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001915 if (lysc_is_key(orig->schema)) {
1916 if (local_parent) {
1917 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001918 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001919 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001920 } else {
1921 assert(!(options & LYD_DUP_WITH_PARENTS));
1922 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001923 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001924 LY_CHECK_GOTO(rc, error);
1925 }
1926 } else {
1927 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001928 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001929 LY_CHECK_GOTO(rc, error);
1930 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001931 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001932 break;
1933 }
1934 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001935
Michal Vasko3a41dff2020-07-15 14:30:28 +02001936 if (dup) {
1937 *dup = first;
1938 }
1939 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001940
1941error:
1942 if (top) {
1943 lyd_free_tree(top);
1944 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001945 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001946 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001947 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001948}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001949
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001950/**
1951 * @brief Check the context of node and parent when duplicating nodes.
1952 *
1953 * @param[in] node Node to duplicate.
1954 * @param[in] parent Parent of the duplicated node(s).
1955 * @return LY_ERR value.
1956 */
1957static LY_ERR
1958lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1959{
1960 const struct lyd_node *iter;
1961
1962 if (!node || !parent) {
1963 return LY_SUCCESS;
1964 }
1965
1966 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1967 /* try to find top-level ext data parent */
1968 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1969
1970 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001971 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001972 return LY_EINVAL;
1973 }
1974 }
1975
1976 return LY_SUCCESS;
1977}
1978
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001979LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001980lyd_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 +02001981{
Michal Vaskoddd76592022-01-17 13:34:48 +01001982 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001983 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001984
Michal Vasko58d89e92023-05-23 09:56:19 +02001985 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001986}
1987
1988LIBYANG_API_DEF LY_ERR
1989lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1990 uint32_t options, struct lyd_node **dup)
1991{
1992 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1993
Michal Vasko58d89e92023-05-23 09:56:19 +02001994 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001995}
1996
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001997LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001998lyd_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 +02001999{
Michal Vaskoddd76592022-01-17 13:34:48 +01002000 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002001 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002002
Michal Vasko58d89e92023-05-23 09:56:19 +02002003 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002004}
2005
2006LIBYANG_API_DEF LY_ERR
2007lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2008 uint32_t options, struct lyd_node **dup)
2009{
2010 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2011
Michal Vasko58d89e92023-05-23 09:56:19 +02002012 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002013}
2014
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002015LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002016lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002017{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002018 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002019 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002020 struct lyd_meta *mt, *last;
2021
Michal Vasko3a41dff2020-07-15 14:30:28 +02002022 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002023
Michal Vasko33c48972022-07-20 10:28:07 +02002024 /* log to node context but value must always use the annotation context */
2025 ctx = meta->annotation->module->ctx;
2026
Michal Vasko25a32822020-07-09 15:48:22 +02002027 /* create a copy */
2028 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002029 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002030 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002031 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002032 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002033 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002034
2035 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002036 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002037 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002038 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002039 last->next = mt;
2040 } else {
2041 node->meta = mt;
2042 }
2043
Radek Krejci011e4aa2020-09-04 15:22:31 +02002044finish:
2045 if (ret) {
2046 lyd_free_meta_single(mt);
2047 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002048 *dup = mt;
2049 }
2050 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002051}
2052
Michal Vasko4490d312020-06-16 13:08:55 +02002053/**
2054 * @brief Merge a source sibling into target siblings.
2055 *
2056 * @param[in,out] first_trg First target sibling, is updated if top-level.
2057 * @param[in] parent_trg Target parent.
2058 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002059 * @param[in] merge_cb Optional merge callback.
2060 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002061 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002062 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002063 * @return LY_ERR value.
2064 */
2065static LY_ERR
2066lyd_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 +02002067 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002068{
Michal Vasko4490d312020-06-16 13:08:55 +02002069 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002070 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002071 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002072 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002073 struct ly_ht *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002074 LY_ERR ret;
2075 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002076
2077 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002078 if (!sibling_src->schema) {
2079 /* try to find the same opaque node */
2080 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2081 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002082 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002083 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002084 } else {
2085 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002086 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002087 }
2088
Michal Vaskocd3f6172021-05-18 16:14:50 +02002089 if (match_trg) {
2090 /* update match as needed */
2091 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2092 } else {
2093 /* first instance of this node */
2094 first_inst = 1;
2095 }
2096
2097 if (match_trg) {
2098 /* call callback */
2099 if (merge_cb) {
2100 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2101 }
2102
Michal Vasko4490d312020-06-16 13:08:55 +02002103 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002104 if (!match_trg->schema) {
2105 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2106 /* update value */
2107 opaq_trg = (struct lyd_node_opaq *)match_trg;
2108 opaq_src = (struct lyd_node_opaq *)sibling_src;
2109
2110 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2111 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2112 opaq_trg->hints = opaq_src->hints;
2113
2114 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2115 opaq_trg->format = opaq_src->format;
2116 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2117 &opaq_trg->val_prefix_data);
2118 }
2119 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2120 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002121 /* since they are different, they cannot both be default */
2122 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2123
Michal Vasko3a41dff2020-07-15 14:30:28 +02002124 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2125 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002126 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002127 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2128 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002129 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002130
2131 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002132 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002133 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002134 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002135 /* update value */
2136 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002137 ((struct lyd_node_any *)sibling_src)->value_type));
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 Vaskocd3f6172021-05-18 16:14:50 +02002141 }
2142
2143 /* check descendants, recursively */
2144 ret = LY_SUCCESS;
2145 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2146 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2147 &child_dup_inst);
2148 if (ret) {
2149 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002150 }
2151 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002152 lyd_dup_inst_free(child_dup_inst);
2153 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002154 } else {
2155 /* node not found, merge it */
2156 if (options & LYD_MERGE_DESTRUCT) {
2157 dup_src = (struct lyd_node *)sibling_src;
2158 lyd_unlink_tree(dup_src);
2159 /* spend it */
2160 *sibling_src_p = NULL;
2161 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002162 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 +02002163 }
2164
Michal Vasko7e8315b2021-08-03 17:01:06 +02002165 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2166 /* set LYD_NEW for all the new nodes, required for validation */
2167 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2168 elem->flags |= LYD_NEW;
2169 LYD_TREE_DFS_END(dup_src, elem);
2170 }
Michal Vasko4490d312020-06-16 13:08:55 +02002171 }
2172
Michal Vaskocd3f6172021-05-18 16:14:50 +02002173 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002174 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002175
2176 if (first_inst) {
2177 /* remember not to find this instance next time */
2178 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2179 }
2180
2181 /* call callback, no source node */
2182 if (merge_cb) {
2183 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2184 }
Michal Vasko4490d312020-06-16 13:08:55 +02002185 }
2186
2187 return LY_SUCCESS;
2188}
2189
Michal Vasko3a41dff2020-07-15 14:30:28 +02002190static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002191lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2192 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002193{
2194 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002195 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002196 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002197 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002198
2199 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002200 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2201 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002202
2203 if (!source) {
2204 /* nothing to merge */
2205 return LY_SUCCESS;
2206 }
2207
Michal Vasko831422b2020-11-24 11:20:51 +01002208 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002209 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002210 return LY_EINVAL;
2211 }
2212
2213 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002214 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2215 /* skip data nodes from different modules */
2216 continue;
2217 }
2218
Radek Krejci857189e2020-09-01 13:26:36 +02002219 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002220 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2221 if (ret) {
2222 break;
2223 }
Michal Vasko4490d312020-06-16 13:08:55 +02002224 if (first && !sibling_src) {
2225 /* source was spent (unlinked), move to the next node */
2226 source = tmp;
2227 }
2228
Michal Vasko3a41dff2020-07-15 14:30:28 +02002229 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002230 break;
2231 }
2232 }
2233
2234 if (options & LYD_MERGE_DESTRUCT) {
2235 /* free any leftover source data that were not merged */
2236 lyd_free_siblings((struct lyd_node *)source);
2237 }
2238
Michal Vaskocd3f6172021-05-18 16:14:50 +02002239 lyd_dup_inst_free(dup_inst);
2240 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002241}
2242
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002243LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002244lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002245{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002246 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002247}
2248
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002249LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002250lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002251{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002252 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2253}
2254
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002255LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002256lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2257 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2258{
2259 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002260}
2261
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002262static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002263lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002264{
Michal Vasko14654712020-02-06 08:35:21 +01002265 /* ending \0 */
2266 ++reqlen;
2267
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002268 if (reqlen > *buflen) {
2269 if (is_static) {
2270 return LY_EINCOMPLETE;
2271 }
2272
2273 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2274 if (!*buffer) {
2275 return LY_EMEM;
2276 }
2277
2278 *buflen = reqlen;
2279 }
2280
2281 return LY_SUCCESS;
2282}
2283
Michal Vaskod59035b2020-07-08 12:00:06 +02002284LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002285lyd_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 +02002286{
2287 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002288 size_t len;
2289 const char *val;
2290 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002291
Michal Vasko824d0832021-11-04 15:36:51 +01002292 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002293 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002294 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002295 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002296
2297 quot = '\'';
2298 if (strchr(val, '\'')) {
2299 quot = '"';
2300 }
2301 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002302 }
2303
2304 return LY_SUCCESS;
2305}
2306
2307/**
2308 * @brief Append leaf-list value predicate to path.
2309 *
2310 * @param[in] node Node to print.
2311 * @param[in,out] buffer Buffer to print to.
2312 * @param[in,out] buflen Current buffer length.
2313 * @param[in,out] bufused Current number of characters used in @p buffer.
2314 * @param[in] is_static Whether buffer is static or can be reallocated.
2315 * @return LY_ERR
2316 */
2317static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002318lyd_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 +02002319{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002320 size_t len;
2321 const char *val;
2322 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002323
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002324 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002325 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002326 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002327
2328 quot = '\'';
2329 if (strchr(val, '\'')) {
2330 quot = '"';
2331 }
2332 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2333
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002334 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002335}
2336
2337/**
2338 * @brief Append node position (relative to its other instances) predicate to path.
2339 *
2340 * @param[in] node Node to print.
2341 * @param[in,out] buffer Buffer to print to.
2342 * @param[in,out] buflen Current buffer length.
2343 * @param[in,out] bufused Current number of characters used in @p buffer.
2344 * @param[in] is_static Whether buffer is static or can be reallocated.
2345 * @return LY_ERR
2346 */
2347static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002348lyd_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 +02002349{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002350 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002351 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002352 char *val = NULL;
2353 LY_ERR rc;
2354
Michal Vasko50cc0562021-05-18 16:15:43 +02002355 pos = lyd_list_pos(node);
2356 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002357 return LY_EMEM;
2358 }
2359
2360 len = 1 + strlen(val) + 1;
2361 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2362 if (rc != LY_SUCCESS) {
2363 goto cleanup;
2364 }
2365
2366 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2367
2368cleanup:
2369 free(val);
2370 return rc;
2371}
2372
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002373LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002374lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2375{
Radek Krejci857189e2020-09-01 13:26:36 +02002376 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002377 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002378 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002379 const struct lyd_node *iter, *parent;
2380 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002381 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002382
2383 LY_CHECK_ARG_RET(NULL, node, NULL);
2384 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002385 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002386 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002387 } else {
2388 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002389 }
2390
2391 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002392 case LYD_PATH_STD:
2393 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002394 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002395 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002396 ++depth;
2397 }
2398
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002399 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002400 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002401 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002402 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002403iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002404 /* get the module */
2405 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2406 parent = lyd_parent(iter);
2407 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2408 if (prev_mod == mod) {
2409 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002410 }
2411
2412 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002413 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2414 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002415 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2416 if (rc != LY_SUCCESS) {
2417 break;
2418 }
2419
2420 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002421 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002422
Michal Vasko790b2bc2020-08-03 13:35:06 +02002423 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002424 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002425 switch (iter->schema->nodetype) {
2426 case LYS_LIST:
2427 if (iter->schema->flags & LYS_KEYLESS) {
2428 /* print its position */
2429 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2430 } else {
2431 /* print all list keys in predicates */
2432 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2433 }
2434 break;
2435 case LYS_LEAFLIST:
2436 if (iter->schema->flags & LYS_CONFIG_W) {
2437 /* print leaf-list value */
2438 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2439 } else {
2440 /* print its position */
2441 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2442 }
2443 break;
2444 default:
2445 /* nothing to print more */
2446 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002447 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002448 }
2449 if (rc != LY_SUCCESS) {
2450 break;
2451 }
2452
Michal Vasko14654712020-02-06 08:35:21 +01002453 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002454 }
2455 break;
2456 }
2457
2458 return buffer;
2459}
Michal Vaskoe444f752020-02-10 12:20:06 +01002460
Michal Vaskodbf3e652022-10-21 08:46:25 +02002461char *
2462lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2463{
2464 uint32_t depth;
2465 size_t bufused = 0, buflen = 0, len;
2466 char *buffer = NULL;
2467 const struct lyd_node *iter, *parent;
2468 const struct lys_module *mod, *prev_mod;
2469 LY_ERR rc = LY_SUCCESS;
2470
2471 switch (pathtype) {
2472 case LYD_PATH_STD:
2473 case LYD_PATH_STD_NO_LAST_PRED:
2474 for (depth = 1; depth <= dnodes->count; ++depth) {
2475 /* current node */
2476 iter = dnodes->dnodes[depth - 1];
2477 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2478
2479 /* parent */
2480 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002481 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2482 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko8c320b82022-11-21 15:51:57 +01002483 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002484
2485 /* get module to print, if any */
2486 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2487 if (prev_mod == mod) {
2488 mod = NULL;
2489 }
2490
2491 /* realloc string */
2492 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2493 strlen(((struct lyd_node_opaq *)iter)->name.name));
2494 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2495 break;
2496 }
2497
2498 /* print next node */
2499 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2500
2501 /* do not always print the last (first) predicate */
2502 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2503 switch (iter->schema->nodetype) {
2504 case LYS_LIST:
2505 if (iter->schema->flags & LYS_KEYLESS) {
2506 /* print its position */
2507 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2508 } else {
2509 /* print all list keys in predicates */
2510 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2511 }
2512 break;
2513 case LYS_LEAFLIST:
2514 if (iter->schema->flags & LYS_CONFIG_W) {
2515 /* print leaf-list value */
2516 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2517 } else {
2518 /* print its position */
2519 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2520 }
2521 break;
2522 default:
2523 /* nothing to print more */
2524 break;
2525 }
2526 }
2527 if (rc) {
2528 break;
2529 }
2530 }
2531 break;
2532 }
2533
2534 return buffer;
2535}
2536
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002537LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002538lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2539{
2540 struct lyd_meta *ret = NULL;
2541 const struct ly_ctx *ctx;
2542 const char *prefix, *tmp;
2543 char *str;
2544 size_t pref_len, name_len;
2545
2546 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002547 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002548
2549 if (!first) {
2550 return NULL;
2551 }
2552
2553 ctx = first->annotation->module->ctx;
2554
2555 /* parse the name */
2556 tmp = name;
2557 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2558 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2559 return NULL;
2560 }
2561
2562 /* find the module */
2563 if (prefix) {
2564 str = strndup(prefix, pref_len);
2565 module = ly_ctx_get_module_latest(ctx, str);
2566 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002567 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 +02002568 }
2569
2570 /* find the metadata */
2571 LY_LIST_FOR(first, first) {
2572 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2573 ret = (struct lyd_meta *)first;
2574 break;
2575 }
2576 }
2577
2578 return ret;
2579}
2580
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002581LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002582lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2583{
Michal Vasko9beceb82022-04-05 12:14:15 +02002584 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002585 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002586 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002587
Michal Vaskof03ed032020-03-04 13:31:44 +01002588 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002589 if (!siblings) {
2590 /* no data */
2591 if (match) {
2592 *match = NULL;
2593 }
2594 return LY_ENOTFOUND;
2595 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002596
Michal Vasko9beceb82022-04-05 12:14:15 +02002597 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2598 /* create a duplicate in this context */
2599 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2600 target = dup;
2601 }
2602
2603 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2604 /* schema mismatch */
2605 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002606 if (match) {
2607 *match = NULL;
2608 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002609 return LY_ENOTFOUND;
2610 }
2611
Michal Vaskoe78faec2021-04-08 17:24:43 +02002612 /* get first sibling */
2613 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002614
Michal Vasko9e685082021-01-29 14:49:09 +01002615 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002616 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002617 assert(target->hash);
2618
Michal Vaskoe78faec2021-04-08 17:24:43 +02002619 if (lysc_is_dup_inst_list(target->schema)) {
2620 /* we must search the instances from beginning to find the first matching one */
2621 found = 0;
2622 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002623 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002624 found = 1;
2625 break;
2626 }
2627 }
2628 if (found) {
2629 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002630 } else {
2631 siblings = NULL;
2632 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002633 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002634 /* find by hash */
2635 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2636 siblings = *match_p;
2637 } else {
2638 /* not found */
2639 siblings = NULL;
2640 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002641 }
2642 } else {
2643 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002644 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002645 if (lysc_is_dup_inst_list(target->schema)) {
2646 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2647 break;
2648 }
2649 } else {
2650 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
2651 break;
2652 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002653 }
2654 }
2655 }
2656
Michal Vasko9beceb82022-04-05 12:14:15 +02002657 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002658 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002659 if (match) {
2660 *match = NULL;
2661 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002662 return LY_ENOTFOUND;
2663 }
2664
Michal Vasko9b368d32020-02-14 13:53:31 +01002665 if (match) {
2666 *match = (struct lyd_node *)siblings;
2667 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002668 return LY_SUCCESS;
2669}
2670
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002671LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002672lyd_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 +02002673 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002674{
2675 LY_ERR rc;
2676 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002677 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002678
Michal Vasko4c583e82020-07-17 12:16:14 +02002679 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002680 if (!siblings) {
2681 /* no data */
2682 if (match) {
2683 *match = NULL;
2684 }
2685 return LY_ENOTFOUND;
2686 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002687
Michal Vasko9beceb82022-04-05 12:14:15 +02002688 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2689 /* parent of ext nodes is useless */
2690 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002691 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2692 /* no schema node in siblings so certainly no data node either */
2693 if (match) {
2694 *match = NULL;
2695 }
2696 return LY_ENOTFOUND;
2697 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002698 }
2699
2700 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2701 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002702 if (match) {
2703 *match = NULL;
2704 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002705 return LY_ENOTFOUND;
2706 }
2707
Michal Vaskof03ed032020-03-04 13:31:44 +01002708 if (key_or_value && !val_len) {
2709 val_len = strlen(key_or_value);
2710 }
2711
Michal Vaskob104f112020-07-17 09:54:54 +02002712 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2713 /* create a data node and find the instance */
2714 if (schema->nodetype == LYS_LEAFLIST) {
2715 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002716 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 +02002717 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002718 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002719 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002720 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002721 }
2722
2723 /* find it */
2724 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002725 } else {
2726 /* find the first schema node instance */
2727 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002728 }
2729
Michal Vaskoe444f752020-02-10 12:20:06 +01002730 lyd_free_tree(target);
2731 return rc;
2732}
Michal Vaskoccc02342020-05-21 10:09:21 +02002733
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002734LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002735lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2736{
2737 struct lyd_node **match_p, *first, *iter;
2738 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002739 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002740
Michal Vasko83ae7772022-06-08 10:01:55 +02002741 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002742 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002743
2744 LY_CHECK_RET(ly_set_new(set));
2745
2746 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2747 /* no data or schema mismatch */
2748 return LY_ENOTFOUND;
2749 }
2750
Michal Vaskoee9b9482023-06-19 13:17:48 +02002751 /* set options */
2752 comp_opts = LYD_COMPARE_OPAQ | (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
2753
Michal Vaskoe78faec2021-04-08 17:24:43 +02002754 /* get first sibling */
2755 siblings = lyd_first_sibling(siblings);
2756
2757 parent = siblings->parent;
2758 if (parent && parent->schema && parent->children_ht) {
2759 assert(target->hash);
2760
2761 /* find the first instance */
2762 lyd_find_sibling_first(siblings, target, &first);
2763 if (first) {
2764 /* add it so that it is the first in the set */
2765 if (ly_set_add(*set, first, 1, NULL)) {
2766 goto error;
2767 }
2768
2769 /* find by hash */
2770 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2771 iter = *match_p;
2772 } else {
2773 /* not found */
2774 iter = NULL;
2775 }
2776 while (iter) {
2777 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002778 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002779 goto error;
2780 }
2781
2782 /* find next instance */
2783 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2784 iter = NULL;
2785 } else {
2786 iter = *match_p;
2787 }
2788 }
2789 }
2790 } else {
2791 /* no children hash table */
2792 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002793 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002794 ly_set_add(*set, (void *)siblings, 1, NULL);
2795 }
2796 }
2797 }
2798
2799 if (!(*set)->count) {
2800 return LY_ENOTFOUND;
2801 }
2802 return LY_SUCCESS;
2803
2804error:
2805 ly_set_free(*set, NULL);
2806 *set = NULL;
2807 return LY_EMEM;
2808}
2809
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002810LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002811lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2812{
2813 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2814
2815 for ( ; first; first = first->next) {
2816 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2817 break;
2818 }
2819 }
2820
2821 if (match) {
2822 *match = (struct lyd_node *)first;
2823 }
2824 return first ? LY_SUCCESS : LY_ENOTFOUND;
2825}
2826
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002827LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002828lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002829{
Michal Vaskod96e2372023-02-24 16:07:51 +01002830 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002831
Michal Vaskod96e2372023-02-24 16:07:51 +01002832 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2833}
Michal Vaskoccc02342020-05-21 10:09:21 +02002834
Michal Vaskod96e2372023-02-24 16:07:51 +01002835LIBYANG_API_DEF LY_ERR
2836lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2837{
2838 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, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002841}
Radek Krejcica989142020-11-05 11:32:22 +01002842
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002843LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002844lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2845 const struct lyxp_var *vars, struct ly_set **set)
2846{
2847 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2848
2849 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2850}
2851
2852LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002853lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2854 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002855{
Michal Vaskod96e2372023-02-24 16:07:51 +01002856 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002857
Michal Vaskod96e2372023-02-24 16:07:51 +01002858 *set = NULL;
2859
2860 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 +01002861}
2862
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002863LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002864lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002865{
Michal Vaskod96e2372023-02-24 16:07:51 +01002866 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002867}
2868
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002869LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002870lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2871{
2872 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2873}
2874
2875LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002876lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2877 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002878{
Michal Vaskod96e2372023-02-24 16:07:51 +01002879 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2880}
2881
2882LIBYANG_API_DEF LY_ERR
2883lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2884 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2885 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2886{
2887 LY_ERR ret = LY_SUCCESS;
2888 struct lyxp_set xp_set = {0};
2889 struct lyxp_expr *exp = NULL;
2890 uint32_t i;
2891
2892 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2893 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2894 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2895
2896 /* parse expression */
2897 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2898 LY_CHECK_GOTO(ret, cleanup);
2899
2900 /* evaluate expression */
2901 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2902 LYXP_IGNORE_WHEN);
2903 LY_CHECK_GOTO(ret, cleanup);
2904
2905 /* return expected result type without or with casting */
2906 if (node_set) {
2907 /* node set */
2908 if (xp_set.type == LYXP_SET_NODE_SET) {
2909 /* transform into a set */
2910 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2911 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2912 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2913 (*node_set)->size = xp_set.used;
2914 for (i = 0; i < xp_set.used; ++i) {
2915 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2916 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2917 LY_CHECK_GOTO(ret, cleanup);
2918 }
2919 }
2920 if (ret_type) {
2921 *ret_type = LY_XPATH_NODE_SET;
2922 }
2923 } else if (!string && !number && !boolean) {
2924 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2925 ret = LY_EINVAL;
2926 goto cleanup;
2927 }
2928 }
2929
2930 if (string) {
2931 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2932 /* cast into string */
2933 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2934 }
2935 if (xp_set.type == LYXP_SET_STRING) {
2936 /* string */
2937 *string = xp_set.val.str;
2938 xp_set.val.str = NULL;
2939 if (ret_type) {
2940 *ret_type = LY_XPATH_STRING;
2941 }
2942 }
2943 }
2944
2945 if (number) {
2946 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2947 /* cast into number */
2948 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2949 }
2950 if (xp_set.type == LYXP_SET_NUMBER) {
2951 /* number */
2952 *number = xp_set.val.num;
2953 if (ret_type) {
2954 *ret_type = LY_XPATH_NUMBER;
2955 }
2956 }
2957 }
2958
2959 if (boolean) {
2960 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2961 /* cast into boolean */
2962 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2963 }
2964 if (xp_set.type == LYXP_SET_BOOLEAN) {
2965 /* boolean */
2966 *boolean = xp_set.val.bln;
2967 if (ret_type) {
2968 *ret_type = LY_XPATH_BOOLEAN;
2969 }
2970 }
2971 }
2972
2973cleanup:
2974 lyxp_set_free_content(&xp_set);
2975 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
2976 return ret;
aPiecekfba75362021-10-07 12:39:48 +02002977}
2978
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002979LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002980lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2981{
2982 LY_ERR ret = LY_SUCCESS;
2983 struct lyxp_expr *expr = NULL;
2984 struct ly_path *lypath = NULL;
2985
2986 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2987
2988 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002989 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 +01002990 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002991 LY_CHECK_GOTO(ret, cleanup);
2992
2993 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002994 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002995 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 +01002996 LY_CHECK_GOTO(ret, cleanup);
2997
2998 /* evaluate the path */
Michal Vasko90189962023-02-28 12:10:34 +01002999 ret = ly_path_eval_partial(lypath, ctx_node, NULL, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003000
3001cleanup:
3002 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3003 ly_path_free(LYD_CTX(ctx_node), lypath);
3004 return ret;
3005}
3006
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003007LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003008lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3009{
3010 LY_ERR ret;
3011 struct lyd_node *m;
3012
3013 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3014
Michal Vasko90189962023-02-28 12:10:34 +01003015 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003016 if (ret) {
3017 if (match) {
3018 *match = NULL;
3019 }
3020 return LY_ENOTFOUND;
3021 }
3022
3023 if (match) {
3024 *match = m;
3025 }
3026 return LY_SUCCESS;
3027}