blob: ecfea866488e0ce98bc259262e8b8eb73c9ae924 [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
Radek Krejci7931b192020-06-25 17:05:03 +020055static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020056lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020057{
Michal Vasko69730152020-10-09 16:30:07 +020058 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020059 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020060 const char *path = in->method.fpath.filepath;
61 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020062
63 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020064 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020065
Radek Krejcif13b87b2020-12-01 22:02:17 +010066 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
67 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020068 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
70 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020071 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010072 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
73 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020074 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020075 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020076 }
77
Radek Krejci7931b192020-06-25 17:05:03 +020078 return format;
79}
Radek Krejcie7b95092019-05-15 11:03:07 +020080
Michal Vaskoe0665742021-02-11 11:08:44 +010081/**
82 * @brief Parse YANG data into a data tree.
83 *
84 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010085 * @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 +010086 * @param[in] parent Parent to connect the parsed nodes to, if any.
87 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
88 * @param[in] in Input handle to read the input from.
89 * @param[in] format Expected format of the data in @p in.
90 * @param[in] parse_opts Options for parser.
91 * @param[in] val_opts Options for validation.
92 * @param[out] op Optional pointer to the parsed operation, if any.
93 * @return LY_ERR value.
94 */
95static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010096lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
97 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 +020098{
Michal Vaskod027f382023-02-10 09:13:25 +010099 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100101 struct ly_set parsed = {0};
102 struct lyd_node *first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100103 uint32_t i, int_opts = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100104 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105
Michal Vaskoe0665742021-02-11 11:08:44 +0100106 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200107
108 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100109 if (first_p) {
110 *first_p = NULL;
111 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200112
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 /* remember input position */
114 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200115
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100116 /* set internal options */
117 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
118 int_opts = LYD_INTOPT_WITH_SIBLINGS;
119 }
120
Michal Vaskoe0665742021-02-11 11:08:44 +0100121 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200122 switch (format) {
123 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100124 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100125 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200126 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200127 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100128 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100129 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200131 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100132 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100133 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200135 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100136 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100137 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100138 break;
139 }
Michal Vaskod027f382023-02-10 09:13:25 +0100140 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100141
142 if (parent) {
143 /* get first top-level sibling */
144 for (first = parent; first->parent; first = lyd_parent(first)) {}
145 first = lyd_first_sibling(first);
146 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200147 }
148
Michal Vaskoe0665742021-02-11 11:08:44 +0100149 if (!(parse_opts & LYD_PARSE_ONLY)) {
150 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100151 rc = 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 +0200152 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100153 LY_CHECK_GOTO(rc, cleanup);
154 }
Radek Krejci7931b192020-06-25 17:05:03 +0200155
Michal Vaskoe0665742021-02-11 11:08:44 +0100156 /* set the operation node */
157 if (op) {
158 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200159 }
160
161cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100162 if (lydctx) {
163 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100165 if (rc) {
166 if (parent) {
167 /* free all the parsed subtrees */
168 for (i = 0; i < parsed.count; ++i) {
169 lyd_free_tree(parsed.dnodes[i]);
170 }
171 } else {
172 /* free everything */
173 lyd_free_all(*first_p);
174 *first_p = NULL;
175 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100176 } else if (subtree_sibling) {
177 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100178 }
179 ly_set_erase(&parsed, NULL);
180 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200181}
182
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100183LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100184lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
185 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
186{
187 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
188
189 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
190 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
191 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
192
193 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
194}
195
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100196LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100197lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
198 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
199{
200 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
201 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
202 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
203
Radek Krejcif16e2542021-02-17 15:39:23 +0100204 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100205}
206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100207LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100208lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
209 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200210{
211 LY_ERR ret;
212 struct ly_in *in;
213
214 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100215 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200216
217 ly_in_free(in, 0);
218 return ret;
219}
220
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100221LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200222lyd_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 +0200223 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200224{
225 LY_ERR ret;
226 struct ly_in *in;
227
228 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100229 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200230
231 ly_in_free(in, 0);
232 return ret;
233}
234
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100235LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200236lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
237 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200238{
239 LY_ERR ret;
240 struct ly_in *in;
241
242 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100243 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200244
245 ly_in_free(in, 0);
246 return ret;
247}
248
Radek Krejcif16e2542021-02-17 15:39:23 +0100249/**
250 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
251 * for schema nodes locked inside the extension instance.
252 *
253 * At least one of @p parent, @p tree, or @p op must always be set.
254 *
255 * Specific @p data_type values have different parameter meaning as follows:
256 * - ::LYD_TYPE_RPC_NETCONF:
257 * - @p parent - must be NULL, the whole RPC is expected;
258 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
259 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
260 * a separate opaque data tree, even if the function fails, this may be returned;
261 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
262 *
263 * - ::LYD_TYPE_NOTIF_NETCONF:
264 * - @p parent - must be NULL, the whole notification is expected;
265 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
266 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
267 * a separate opaque data tree, even if the function fails, this may be returned;
268 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
269 *
270 * - ::LYD_TYPE_REPLY_NETCONF:
271 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
272 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
273 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
274 * a separate opaque data tree, even if the function fails, this may be returned;
275 * - @p op - must be NULL, the reply is appended to the RPC;
276 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
277 *
278 * @param[in] ctx libyang context.
279 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
280 * @param[in] parent Optional parent to connect the parsed nodes to.
281 * @param[in] in Input handle to read the input from.
282 * @param[in] format Expected format of the data in @p in.
283 * @param[in] data_type Expected operation to parse (@ref datatype).
284 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
285 * @param[out] op Optional parsed operation node.
286 * @return LY_ERR value.
287 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
288 */
289static LY_ERR
290lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
291 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 +0200292{
Michal Vaskoe0665742021-02-11 11:08:44 +0100293 LY_ERR rc = LY_SUCCESS;
294 struct lyd_ctx *lydctx = NULL;
295 struct ly_set parsed = {0};
296 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100297 uint32_t i, parse_opts, val_opts, int_opts = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200298
Michal Vasko27fb0262021-02-23 09:42:01 +0100299 if (!ctx) {
300 ctx = LYD_CTX(parent);
301 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200302 if (tree) {
303 *tree = NULL;
304 }
305 if (op) {
306 *op = NULL;
307 }
308
Radek Krejci7931b192020-06-25 17:05:03 +0200309 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200310
Michal Vasko63f3d842020-07-08 10:10:14 +0200311 /* remember input position */
312 in->func_start = in->current;
313
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100314 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200315 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100316 val_opts = 0;
317
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100318 switch (data_type) {
319
320 /* special XML NETCONF data types */
321 case LYD_TYPE_RPC_NETCONF:
322 case LYD_TYPE_NOTIF_NETCONF:
323 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
324 /* fallthrough */
325 case LYD_TYPE_REPLY_NETCONF:
326 if (data_type == LYD_TYPE_REPLY_NETCONF) {
327 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
328 LY_EINVAL);
329 }
330
331 /* parse the NETCONF message */
332 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko3a163bc2023-01-10 14:23:56 +0100333 if (rc) {
334 if (envp) {
335 /* special situation when the envelopes were parsed successfully */
336 *tree = envp;
337 }
Michal Vasko73792a12022-05-10 09:28:45 +0200338 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100339 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100340
341 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100342 if (envp) {
343 /* special out param meaning */
344 *tree = envp;
345 } else {
346 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100347 }
348 if (op) {
349 *op = lydctx->op_node;
350 }
351 goto cleanup;
352
353 /* set internal opts */
354 case LYD_TYPE_RPC_YANG:
355 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
356 break;
357 case LYD_TYPE_NOTIF_YANG:
358 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
359 break;
360 case LYD_TYPE_REPLY_YANG:
361 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
362 break;
363 case LYD_TYPE_DATA_YANG:
364 LOGINT(ctx);
365 rc = LY_EINT;
366 goto cleanup;
367 }
368
369 /* parse the data */
370 switch (format) {
371 case LYD_XML:
372 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 +0100373 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200374 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100375 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 +0100376 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200377 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100378 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 +0100379 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200380 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100381 LOGARG(ctx, format);
382 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200383 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200384 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100385 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200386
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 /* set out params correctly */
388 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100389 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 }
391 if (op) {
392 *op = lydctx->op_node;
393 }
394
395cleanup:
396 if (lydctx) {
397 lydctx->free(lydctx);
398 }
399 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200400 /* free all the parsed nodes */
401 if (parsed.count) {
402 i = parsed.count;
403 do {
404 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100405 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200406 } while (i);
407 }
408 if (tree && ((format != LYD_XML) || !envp)) {
409 *tree = NULL;
410 }
411 if (op) {
412 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100413 }
414 }
415 ly_set_erase(&parsed, NULL);
416 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200417}
Radek Krejci084289f2019-07-09 17:35:30 +0200418
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100419LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100420lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
421 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
422{
423 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
424
425 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
426}
427
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100428LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100429lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
430 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
431{
432 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
433
434 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
435
436 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
437}
438
Michal Vasko90932a92020-02-12 14:33:03 +0100439struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200440lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100441{
Michal Vaskob104f112020-07-17 09:54:54 +0200442 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100443 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200444 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100445 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100446
Michal Vaskob104f112020-07-17 09:54:54 +0200447 assert(new_node);
448
Michal Vasko9beceb82022-04-05 12:14:15 +0200449 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200450 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100451 return NULL;
452 }
453
Michal Vasko93b16062020-12-09 18:14:18 +0100454 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100455 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100456 getnext_opts = LYS_GETNEXT_OUTPUT;
457 }
458
Michal Vaskoa2756f02021-07-09 13:20:28 +0200459 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200460 /* find the anchor using hashes */
461 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100462 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200463 while (schema) {
464 /* keep trying to find the first existing instance of the closest following schema sibling,
465 * otherwise return NULL - inserting at the end */
466 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
467 break;
468 }
469
Michal Vasko93b16062020-12-09 18:14:18 +0100470 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200471 }
472 } else {
473 /* find the anchor without hashes */
474 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200475 sparent = lysc_data_parent(new_node->schema);
476 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200477 /* we are in top-level, skip all the data from preceding modules */
478 LY_LIST_FOR(match, match) {
479 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
480 break;
481 }
482 }
483 }
484
485 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100486 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200487
488 found = 0;
489 LY_LIST_FOR(match, match) {
490 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
491 /* we have found an opaque node, which must be at the end, so use it OR
492 * modules do not match, so we must have traversed all the data from new_node module (if any),
493 * we have found the first node of the next module, that is what we want */
494 break;
495 }
496
497 /* skip schema nodes until we find the instantiated one */
498 while (!found) {
499 if (new_node->schema == schema) {
500 /* we have found the schema of the new node, continue search to find the first
501 * data node with a different schema (after our schema) */
502 found = 1;
503 break;
504 }
505 if (match->schema == schema) {
506 /* current node (match) is a data node still before the new node, continue search in data */
507 break;
508 }
Michal Vasko93b16062020-12-09 18:14:18 +0100509 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200510 assert(schema);
511 }
512
513 if (found && (match->schema != new_node->schema)) {
514 /* find the next node after we have found our node schema data instance */
515 break;
516 }
517 }
Michal Vasko90932a92020-02-12 14:33:03 +0100518 }
519
520 return match;
521}
522
Michal Vaskoc61dd062022-06-07 11:01:28 +0200523void
Michal Vaskof03ed032020-03-04 13:31:44 +0100524lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100525{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100526 struct lyd_node_inner *par;
527
Michal Vasko90932a92020-02-12 14:33:03 +0100528 assert(!node->next && (node->prev == node));
529
530 node->next = sibling->next;
531 node->prev = sibling;
532 sibling->next = node;
533 if (node->next) {
534 /* sibling had a succeeding node */
535 node->next->prev = node;
536 } else {
537 /* sibling was last, find first sibling and change its prev */
538 if (sibling->parent) {
539 sibling = sibling->parent->child;
540 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200541 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100542 }
543 sibling->prev = node;
544 }
545 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100546
Michal Vasko9f96a052020-03-10 09:41:45 +0100547 for (par = node->parent; par; par = par->parent) {
548 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
549 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100550 par->flags &= ~LYD_DEFAULT;
551 }
552 }
Michal Vasko90932a92020-02-12 14:33:03 +0100553}
554
Michal Vaskoc61dd062022-06-07 11:01:28 +0200555void
Michal Vaskof03ed032020-03-04 13:31:44 +0100556lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100557{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100558 struct lyd_node_inner *par;
559
Michal Vasko90932a92020-02-12 14:33:03 +0100560 assert(!node->next && (node->prev == node));
561
562 node->next = sibling;
563 /* covers situation of sibling being first */
564 node->prev = sibling->prev;
565 sibling->prev = node;
566 if (node->prev->next) {
567 /* sibling had a preceding node */
568 node->prev->next = node;
569 } else if (sibling->parent) {
570 /* sibling was first and we must also change parent child pointer */
571 sibling->parent->child = node;
572 }
573 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100574
Michal Vasko9f96a052020-03-10 09:41:45 +0100575 for (par = node->parent; par; par = par->parent) {
576 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
577 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100578 par->flags &= ~LYD_DEFAULT;
579 }
580 }
Michal Vasko90932a92020-02-12 14:33:03 +0100581}
582
583/**
Michal Vaskob104f112020-07-17 09:54:54 +0200584 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100585 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100586 * Handles inserting into NP containers and key-less lists.
587 *
Michal Vasko90932a92020-02-12 14:33:03 +0100588 * @param[in] parent Parent to insert into.
589 * @param[in] node Node to insert.
590 */
591static void
Michal Vaskob104f112020-07-17 09:54:54 +0200592lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100593{
594 struct lyd_node_inner *par;
595
Radek Krejcia1c1e542020-09-29 16:06:52 +0200596 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100597 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100598
599 par = (struct lyd_node_inner *)parent;
600
Michal Vaskob104f112020-07-17 09:54:54 +0200601 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100602 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100603
Michal Vaskod989ba02020-08-24 10:59:24 +0200604 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100605 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
606 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100607 par->flags &= ~LYD_DEFAULT;
608 }
609 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200610}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100611
Michal Vasko751cb4d2020-07-14 12:25:28 +0200612/**
613 * @brief Learn whether a list instance has all the keys.
614 *
615 * @param[in] list List instance to check.
616 * @return non-zero if all the keys were found,
617 * @return 0 otherwise.
618 */
619static int
620lyd_insert_has_keys(const struct lyd_node *list)
621{
622 const struct lyd_node *key;
623 const struct lysc_node *skey = NULL;
624
625 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200626 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200627 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
628 if (!key || (key->schema != skey)) {
629 /* key missing */
630 return 0;
631 }
632
633 key = key->next;
634 }
635
636 /* all keys found */
637 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100638}
639
640void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200641lyd_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 +0100642{
Michal Vaskob104f112020-07-17 09:54:54 +0200643 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100644
Michal Vaskob104f112020-07-17 09:54:54 +0200645 /* inserting list without its keys is not supported */
646 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100647 assert(!parent || !parent->schema ||
648 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100649
Michal Vaskob104f112020-07-17 09:54:54 +0200650 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100651 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100652 }
Michal Vasko90932a92020-02-12 14:33:03 +0100653
Michal Vaskob104f112020-07-17 09:54:54 +0200654 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100655 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100656
Michal Vasko9beceb82022-04-05 12:14:15 +0200657 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200658 /* no next anchor */
659 anchor = NULL;
660 } else {
661 /* find the anchor, our next node, so we can insert before it */
662 anchor = lyd_insert_get_next_anchor(first_sibling, node);
663 }
664
Michal Vaskob104f112020-07-17 09:54:54 +0200665 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200666 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200667 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100668 if (!parent && (*first_sibling_p == anchor)) {
669 /* move first sibling */
670 *first_sibling_p = node;
671 }
Michal Vaskob104f112020-07-17 09:54:54 +0200672 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200673 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200674 lyd_insert_after_node(first_sibling->prev, node);
675 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200676 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200677 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100678 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200679 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200680 *first_sibling_p = node;
681 }
682
683 /* insert into parent HT */
684 lyd_insert_hash(node);
685
686 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200687 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200688 lyd_hash(parent);
689
690 /* now we can insert even the list into its parent HT */
691 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100692 }
Michal Vasko90932a92020-02-12 14:33:03 +0100693}
694
Michal Vasko717a4c32020-12-07 09:40:10 +0100695/**
696 * @brief Check schema place of a node to be inserted.
697 *
698 * @param[in] parent Schema node of the parent data node.
699 * @param[in] sibling Schema node of a sibling data node.
700 * @param[in] schema Schema node if the data node to be inserted.
701 * @return LY_SUCCESS on success.
702 * @return LY_EINVAL if the place is invalid.
703 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100704static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100705lyd_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 +0100706{
707 const struct lysc_node *par2;
708
Michal Vasko62ed12d2020-05-21 10:08:25 +0200709 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100710 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
711 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100712
Michal Vasko717a4c32020-12-07 09:40:10 +0100713 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100714 /* opaque nodes can be inserted wherever */
715 return LY_SUCCESS;
716 }
717
Michal Vasko717a4c32020-12-07 09:40:10 +0100718 if (!parent) {
719 parent = lysc_data_parent(sibling);
720 }
721
Michal Vaskof03ed032020-03-04 13:31:44 +0100722 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200723 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100724
725 if (parent) {
726 /* inner node */
727 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200728 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200729 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100730 return LY_EINVAL;
731 }
732 } else {
733 /* top-level node */
734 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200735 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100736 return LY_EINVAL;
737 }
738 }
739
740 return LY_SUCCESS;
741}
742
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100743LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200744lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100745{
746 struct lyd_node *iter;
747
Michal Vasko0ab974d2021-02-24 13:18:26 +0100748 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100749 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100750
Michal Vasko717a4c32020-12-07 09:40:10 +0100751 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100752
Michal Vasko0ab974d2021-02-24 13:18:26 +0100753 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100754 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100755 return LY_EINVAL;
756 }
757
758 if (node->parent || node->prev->next) {
759 lyd_unlink_tree(node);
760 }
761
762 while (node) {
763 iter = node->next;
764 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200765 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100766 node = iter;
767 }
768 return LY_SUCCESS;
769}
770
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100771LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200772lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100773{
774 struct lyd_node *iter;
775
776 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
777 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
778
779 if (first->schema && (first->schema->flags & LYS_KEY)) {
780 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
781 return LY_EINVAL;
782 }
783
784 while (first) {
785 iter = first->next;
786 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100787 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100788 first = iter;
789 }
790 return LY_SUCCESS;
791}
792
793LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200794lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100795{
796 struct lyd_node *iter;
797
Michal Vaskob104f112020-07-17 09:54:54 +0200798 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100799
Michal Vaskob104f112020-07-17 09:54:54 +0200800 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100801 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100802 }
803
Michal Vasko553d0b52020-12-04 16:27:52 +0100804 if (sibling == node) {
805 /* we need to keep the connection to siblings so we can insert into them */
806 sibling = sibling->prev;
807 }
808
Michal Vaskob1b5c262020-03-05 14:29:47 +0100809 if (node->parent || node->prev->next) {
810 lyd_unlink_tree(node);
811 }
812
813 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100814 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200815 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200816 return LY_EINVAL;
817 }
818
Michal Vaskob1b5c262020-03-05 14:29:47 +0100819 iter = node->next;
820 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200821 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100822 node = iter;
823 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100824
Michal Vaskob104f112020-07-17 09:54:54 +0200825 if (first) {
826 /* find the first sibling */
827 *first = sibling;
828 while ((*first)->prev->next) {
829 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100830 }
831 }
832
833 return LY_SUCCESS;
834}
835
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100836LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100837lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
838{
Michal Vaskobce230b2022-04-19 09:55:31 +0200839 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100840 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100841
Michal Vasko717a4c32020-12-07 09:40:10 +0100842 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100843
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200844 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200845 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100846 return LY_EINVAL;
847 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200848 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
849 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100850 return LY_EINVAL;
851 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100852
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100853 lyd_unlink_tree(node);
854 lyd_insert_before_node(sibling, node);
855 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100856
Michal Vaskof03ed032020-03-04 13:31:44 +0100857 return LY_SUCCESS;
858}
859
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100860LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100861lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
862{
Michal Vaskobce230b2022-04-19 09:55:31 +0200863 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100864 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100865
Michal Vasko717a4c32020-12-07 09:40:10 +0100866 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100867
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200868 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200869 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100870 return LY_EINVAL;
871 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200872 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
873 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100874 return LY_EINVAL;
875 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100876
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100877 lyd_unlink_tree(node);
878 lyd_insert_after_node(sibling, node);
879 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100880
Michal Vaskof03ed032020-03-04 13:31:44 +0100881 return LY_SUCCESS;
882}
883
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100884LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200885lyd_unlink_siblings(struct lyd_node *node)
886{
887 struct lyd_node *next, *elem, *first = NULL;
888
889 LY_LIST_FOR_SAFE(node, next, elem) {
890 lyd_unlink_tree(elem);
891 lyd_insert_node(NULL, &first, elem, 1);
892 }
893}
894
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100895LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100896lyd_unlink_tree(struct lyd_node *node)
897{
898 struct lyd_node *iter;
899
900 if (!node) {
901 return;
902 }
903
Michal Vaskob104f112020-07-17 09:54:54 +0200904 /* update hashes while still linked into the tree */
905 lyd_unlink_hash(node);
906
Michal Vaskof03ed032020-03-04 13:31:44 +0100907 /* unlink from siblings */
908 if (node->prev->next) {
909 node->prev->next = node->next;
910 }
911 if (node->next) {
912 node->next->prev = node->prev;
913 } else {
914 /* unlinking the last node */
915 if (node->parent) {
916 iter = node->parent->child;
917 } else {
918 iter = node->prev;
919 while (iter->prev != node) {
920 iter = iter->prev;
921 }
922 }
923 /* update the "last" pointer from the first node */
924 iter->prev = node->prev;
925 }
926
927 /* unlink from parent */
928 if (node->parent) {
929 if (node->parent->child == node) {
930 /* the node is the first child */
931 node->parent->child = node->next;
932 }
933
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200934 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100935 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200936
Michal Vaskof03ed032020-03-04 13:31:44 +0100937 node->parent = NULL;
938 }
939
940 node->next = NULL;
941 node->prev = node;
942}
943
Michal Vaskoa5da3292020-08-12 13:10:50 +0200944void
Michal Vasko871a0252020-11-11 18:35:24 +0100945lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200946{
947 struct lyd_meta *last, *iter;
948
949 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200950
951 if (!meta) {
952 return;
953 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200954
955 for (iter = meta; iter; iter = iter->next) {
956 iter->parent = parent;
957 }
958
959 /* insert as the last attribute */
960 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200961 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200962 last->next = meta;
963 } else {
964 parent->meta = meta;
965 }
966
967 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100968 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100970 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200971 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200972}
973
974LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100975lyd_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 +0200976 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 +0100977 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 +0100978{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100979 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100980 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200981 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100982 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200983 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100984
Michal Vasko9f96a052020-03-10 09:41:45 +0100985 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100986
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200987 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200988 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200989 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100990 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200991 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100992 break;
993 }
994 }
995 if (!ant) {
996 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100997 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100998 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100999 ret = LY_EINVAL;
1000 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001001 }
1002
Michal Vasko9f96a052020-03-10 09:41:45 +01001003 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001004 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001005 mt->parent = parent;
1006 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001007 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001008 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 +01001009 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001010 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1011 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1012 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001013
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001014 /* insert as the last attribute */
1015 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001016 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001017 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001018 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001019 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001020 }
1021
Michal Vasko9f96a052020-03-10 09:41:45 +01001022 if (meta) {
1023 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001024 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001025
1026cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001027 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001028}
1029
Michal Vaskoa5da3292020-08-12 13:10:50 +02001030void
1031lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1032{
1033 struct lyd_attr *last, *iter;
1034 struct lyd_node_opaq *opaq;
1035
1036 assert(parent && !parent->schema);
1037
1038 if (!attr) {
1039 return;
1040 }
1041
1042 opaq = (struct lyd_node_opaq *)parent;
1043 for (iter = attr; iter; iter = iter->next) {
1044 iter->parent = opaq;
1045 }
1046
1047 /* insert as the last attribute */
1048 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001049 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001050 last->next = attr;
1051 } else {
1052 opaq->attr = attr;
1053 }
1054}
1055
Michal Vasko52927e22020-03-16 17:26:14 +01001056LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001057lyd_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 +01001058 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 +02001059 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 +01001060{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001061 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001062 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001063
1064 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001065 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001066
Michal Vasko2a3722d2021-06-16 11:52:39 +02001067 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001068 value = "";
1069 }
1070
1071 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001072 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001073
Michal Vaskoad92b672020-11-12 13:11:31 +01001074 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001075 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001076 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001077 }
1078 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001079 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001080 }
1081
Michal Vasko52927e22020-03-16 17:26:14 +01001082 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001083 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1084 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001085 *dynamic = 0;
1086 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001087 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001088 }
Michal Vasko501af032020-11-11 20:27:44 +01001089 at->format = format;
1090 at->val_prefix_data = val_prefix_data;
1091 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001092
1093 /* insert as the last attribute */
1094 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001095 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001096 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001097 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001098 last->next = at;
1099 }
1100
Radek Krejci011e4aa2020-09-04 15:22:31 +02001101finish:
1102 if (ret) {
1103 lyd_free_attr_single(ctx, at);
1104 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001105 *attr = at;
1106 }
1107 return LY_SUCCESS;
1108}
1109
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001110LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001111lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001112{
Michal Vasko90189962023-02-28 12:10:34 +01001113 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001114
Michal Vasko90189962023-02-28 12:10:34 +01001115 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001116
Michal Vasko004d3152020-06-11 19:59:22 +02001117 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001118}
1119
aPiecek2f63f952021-03-30 12:22:18 +02001120/**
1121 * @brief Check the equality of the two schemas from different contexts.
1122 *
1123 * @param schema1 of first node.
1124 * @param schema2 of second node.
1125 * @return 1 if the schemas are equal otherwise 0.
1126 */
1127static ly_bool
1128lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1129{
1130 if (!schema1 && !schema2) {
1131 return 1;
1132 } else if (!schema1 || !schema2) {
1133 return 0;
1134 }
1135
1136 assert(schema1->module->ctx != schema2->module->ctx);
1137
1138 if (schema1->nodetype != schema2->nodetype) {
1139 return 0;
1140 }
1141
1142 if (strcmp(schema1->name, schema2->name)) {
1143 return 0;
1144 }
1145
1146 if (strcmp(schema1->module->name, schema2->module->name)) {
1147 return 0;
1148 }
1149
aPiecek2f63f952021-03-30 12:22:18 +02001150 return 1;
1151}
1152
1153/**
1154 * @brief Check the equality of the schemas for all parent nodes.
1155 *
1156 * Both nodes must be from different contexts.
1157 *
1158 * @param node1 Data of first node.
1159 * @param node2 Data of second node.
1160 * @return 1 if the all related parental schemas are equal otherwise 0.
1161 */
1162static ly_bool
1163lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1164{
1165 const struct lysc_node *parent1, *parent2;
1166
1167 assert(node1 && node2);
1168
1169 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1170 parent1 && parent2;
1171 parent1 = parent1->parent, parent2 = parent2->parent) {
1172 if (!lyd_compare_schema_equal(parent1, parent2)) {
1173 return 0;
1174 }
1175 }
1176
1177 if (parent1 || parent2) {
1178 return 0;
1179 }
1180
1181 return 1;
1182}
1183
1184/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001185 * @brief Compare 2 nodes values including opaque node values.
1186 *
1187 * @param[in] node1 First node to compare.
1188 * @param[in] node2 Second node to compare.
1189 * @return LY_SUCCESS if equal.
1190 * @return LY_ENOT if not equal.
1191 * @return LY_ERR on error.
1192 */
1193static LY_ERR
1194lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1195{
1196 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1197 const char *val1, *val2, *col;
1198 const struct lys_module *mod;
1199 char *val_dyn = NULL;
1200 LY_ERR rc = LY_SUCCESS;
1201
1202 if (!node1->schema) {
1203 opaq1 = (struct lyd_node_opaq *)node1;
1204 }
1205 if (!node2->schema) {
1206 opaq2 = (struct lyd_node_opaq *)node2;
1207 }
1208
1209 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1210 /* opaque XML and opaque XML node */
1211 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1212 opaq2->val_prefix_data)) {
1213 return LY_ENOT;
1214 }
1215 return LY_SUCCESS;
1216 }
1217
1218 /* get their values */
1219 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1220 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1221 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1222 if (!mod) {
1223 /* unable to compare */
1224 return LY_ENOT;
1225 }
1226
1227 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1228 LOGMEM(LYD_CTX(node1));
1229 return LY_EMEM;
1230 }
1231 val1 = val_dyn;
1232 } else {
1233 val1 = lyd_get_value(node1);
1234 }
1235 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1236 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1237 if (!mod) {
1238 return LY_ENOT;
1239 }
1240
1241 assert(!val_dyn);
1242 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1243 LOGMEM(LYD_CTX(node2));
1244 return LY_EMEM;
1245 }
1246 val2 = val_dyn;
1247 } else {
1248 val2 = lyd_get_value(node2);
1249 }
1250
1251 /* compare values */
1252 if (strcmp(val1, val2)) {
1253 rc = LY_ENOT;
1254 }
1255
1256 free(val_dyn);
1257 return rc;
1258}
1259
1260/**
aPiecek2f63f952021-03-30 12:22:18 +02001261 * @brief Internal implementation of @ref lyd_compare_single.
1262 * @copydoc lyd_compare_single
1263 * @param[in] parental_schemas_checked Flag used for optimization.
1264 * When this function is called for the first time, the flag must be set to 0.
1265 * The @ref lyd_compare_schema_parents_equal should be called only once during
1266 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1267 */
1268static LY_ERR
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001269lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1270 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001271{
1272 const struct lyd_node *iter1, *iter2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001273 struct lyd_node_any *any1, *any2;
Michal Vaskof3c80a72022-08-17 10:22:04 +02001274 int len1, len2;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001275 LY_ERR r;
Radek Krejci084289f2019-07-09 17:35:30 +02001276
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001277 if (!node1 || !node2) {
1278 if (node1 == node2) {
1279 return LY_SUCCESS;
1280 } else {
1281 return LY_ENOT;
1282 }
1283 }
1284
aPiecek2f63f952021-03-30 12:22:18 +02001285 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1286 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001287 if (options & LYD_COMPARE_OPAQ) {
1288 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1289 return LY_ENOT;
1290 }
1291 } else {
1292 if (node1->schema != node2->schema) {
1293 return LY_ENOT;
1294 }
aPiecek2f63f952021-03-30 12:22:18 +02001295 }
1296 } else {
1297 /* different contexts */
1298 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1299 return LY_ENOT;
1300 }
1301 if (!parental_schemas_checked) {
1302 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1303 return LY_ENOT;
1304 }
1305 parental_schemas_checked = 1;
1306 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001307 }
1308
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001309 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001310 return LY_ENOT;
1311 }
aPiecek2f63f952021-03-30 12:22:18 +02001312 /* 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 +02001313
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001314 if (!node1->schema || !node2->schema) {
1315 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001316 return LY_ENOT;
1317 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001318 if ((r = lyd_compare_single_value(node1, node2))) {
1319 return r;
Michal Vasko52927e22020-03-16 17:26:14 +01001320 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001321
Michal Vasko52927e22020-03-16 17:26:14 +01001322 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001323 iter1 = lyd_child(node1);
1324 iter2 = lyd_child(node2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001325 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001326 }
1327 return LY_SUCCESS;
1328 } else {
1329 switch (node1->schema->nodetype) {
1330 case LYS_LEAF:
1331 case LYS_LEAFLIST:
1332 if (options & LYD_COMPARE_DEFAULTS) {
1333 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1334 return LY_ENOT;
1335 }
1336 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001337 if ((r = lyd_compare_single_value(node1, node2))) {
1338 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001339 }
1340
aPiecek2f63f952021-03-30 12:22:18 +02001341 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001342 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001343 case LYS_RPC:
1344 case LYS_ACTION:
1345 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001346 if (options & LYD_COMPARE_DEFAULTS) {
1347 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1348 return LY_ENOT;
1349 }
1350 }
1351 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001352 iter1 = lyd_child(node1);
1353 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001354 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001355 }
1356 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001357 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001358 iter1 = lyd_child(node1);
1359 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001360
1361 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1362 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001363 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001364 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001365 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001366 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001367 return LY_ENOT;
1368 }
1369 iter1 = iter1->next;
1370 iter2 = iter2->next;
1371 }
1372 } else {
1373 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1374
Radek Krejci0f969882020-08-21 16:56:47 +02001375all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001376 if (!iter1 && !iter2) {
1377 /* no children, nothing to compare */
1378 return LY_SUCCESS;
1379 }
1380
Michal Vaskod989ba02020-08-24 10:59:24 +02001381 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001382 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001383 return LY_ENOT;
1384 }
1385 }
1386 if (iter1 || iter2) {
1387 return LY_ENOT;
1388 }
1389 }
1390 return LY_SUCCESS;
1391 case LYS_ANYXML:
1392 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001393 any1 = (struct lyd_node_any *)node1;
1394 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001395
1396 if (any1->value_type != any2->value_type) {
1397 return LY_ENOT;
1398 }
1399 switch (any1->value_type) {
1400 case LYD_ANYDATA_DATATREE:
1401 iter1 = any1->value.tree;
1402 iter2 = any2->value.tree;
1403 goto all_children_compare;
1404 case LYD_ANYDATA_STRING:
1405 case LYD_ANYDATA_XML:
1406 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001407 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1408 return LY_ENOT;
1409 } else if (!any1->value.str && !any2->value.str) {
1410 return LY_SUCCESS;
1411 }
Michal Vasko52927e22020-03-16 17:26:14 +01001412 len1 = strlen(any1->value.str);
1413 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001414 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001415 return LY_ENOT;
1416 }
1417 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001418 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001419 len1 = lyd_lyb_data_length(any1->value.mem);
1420 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001421 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001422 return LY_ENOT;
1423 }
1424 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001425 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001426 }
1427 }
1428
Michal Vaskob7be7a82020-08-20 09:09:04 +02001429 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001430 return LY_EINT;
1431}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001432
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001433LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001434lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1435{
1436 return lyd_compare_single_(node1, node2, options, 0);
1437}
1438
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001439LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001440lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001441{
Michal Vaskod989ba02020-08-24 10:59:24 +02001442 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001443 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1444 }
1445
Michal Vasko11a81432020-07-28 16:31:29 +02001446 if (node1 == node2) {
1447 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001448 }
Michal Vasko11a81432020-07-28 16:31:29 +02001449 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001450}
1451
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001452LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001453lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1454{
1455 if (!meta1 || !meta2) {
1456 if (meta1 == meta2) {
1457 return LY_SUCCESS;
1458 } else {
1459 return LY_ENOT;
1460 }
1461 }
1462
Michal Vaskoa8083062020-11-06 17:22:10 +01001463 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001464 return LY_ENOT;
1465 }
1466
Michal Vasko21725742020-06-29 11:49:25 +02001467 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1468}
1469
Radek Krejci22ebdba2019-07-25 13:59:43 +02001470/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001471 * @brief Create a copy of the attribute.
1472 *
1473 * @param[in] attr Attribute to copy.
1474 * @param[in] node Opaque where to append the new attribute.
1475 * @param[out] dup Optional created attribute copy.
1476 * @return LY_ERR value.
1477 */
1478static LY_ERR
1479lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1480{
1481 LY_ERR ret = LY_SUCCESS;
1482 struct lyd_attr *a, *last;
1483 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1484
1485 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1486
1487 /* create a copy */
1488 a = calloc(1, sizeof *attr);
1489 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1490
1491 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1492 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1493 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1494 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1495 a->hints = attr->hints;
1496 a->format = attr->format;
1497 if (attr->val_prefix_data) {
1498 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1499 LY_CHECK_GOTO(ret, finish);
1500 }
1501
1502 /* insert as the last attribute */
1503 a->parent = opaq;
1504 if (opaq->attr) {
1505 for (last = opaq->attr; last->next; last = last->next) {}
1506 last->next = a;
1507 } else {
1508 opaq->attr = a;
1509 }
1510
1511finish:
1512 if (ret) {
1513 lyd_free_attr_single(LYD_CTX(node), a);
1514 } else if (dup) {
1515 *dup = a;
1516 }
1517 return LY_SUCCESS;
1518}
1519
1520/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001521 * @brief Find @p schema equivalent in @p trg_ctx.
1522 *
1523 * @param[in] schema Schema node to find.
1524 * @param[in] trg_ctx Target context to search in.
1525 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001526 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001527 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1528 * @return LY_RRR value.
1529 */
1530static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001531lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1532 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001533{
Michal Vasko9beceb82022-04-05 12:14:15 +02001534 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1535 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001536 char *path;
1537
1538 if (!schema) {
1539 /* opaque node */
1540 *trg_schema = NULL;
1541 return LY_SUCCESS;
1542 }
1543
Michal Vasko9beceb82022-04-05 12:14:15 +02001544 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001545 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001546 trg_parent = parent->schema;
1547 src_parent = lysc_data_parent(schema);
1548 }
1549
1550 do {
1551 /* find the next parent */
1552 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001553 while (lysc_data_parent(sp) != src_parent) {
1554 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001555 }
1556 src_parent = sp;
1557
1558 if (!src_parent->parent) {
1559 /* find the module first */
1560 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1561 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001562 if (log) {
1563 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1564 src_parent->module->name);
1565 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001566 return LY_ENOTFOUND;
1567 }
1568 }
1569
1570 /* find the next parent */
1571 assert(trg_parent || trg_mod);
1572 tp = NULL;
1573 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1574 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1575 break;
1576 }
1577 }
1578 if (!tp) {
1579 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001580 if (log) {
1581 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1582 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1583 free(path);
1584 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001585 return LY_ENOTFOUND;
1586 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001587
Michal Vasko9beceb82022-04-05 12:14:15 +02001588 trg_parent = tp;
1589 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001590
Michal Vasko9beceb82022-04-05 12:14:15 +02001591 /* success */
1592 *trg_schema = trg_parent;
1593 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001594}
1595
1596/**
Michal Vasko52927e22020-03-16 17:26:14 +01001597 * @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 +02001598 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001599 * 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 +02001600 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001601 * @param[in] node Node to duplicate.
1602 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001603 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001604 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1605 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001606 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1607 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001608 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1609 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001610 */
Michal Vasko52927e22020-03-16 17:26:14 +01001611static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001612lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1613 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001614{
Michal Vasko52927e22020-03-16 17:26:14 +01001615 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001616 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001617 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001618 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001619 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001620 const struct lysc_type *type;
1621 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001622
Michal Vasko52927e22020-03-16 17:26:14 +01001623 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001624
Michal Vasko19175b62022-04-01 09:17:07 +02001625 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001626 if (options & LYD_DUP_NO_EXT) {
1627 /* no not duplicate this subtree */
1628 return LY_SUCCESS;
1629 }
1630
Michal Vasko19175b62022-04-01 09:17:07 +02001631 /* we need to use the same context */
1632 trg_ctx = LYD_CTX(node);
1633 }
1634
Michal Vasko52927e22020-03-16 17:26:14 +01001635 if (!node->schema) {
1636 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001637 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001638 } else {
1639 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001640 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001641 case LYS_ACTION:
1642 case LYS_NOTIF:
1643 case LYS_CONTAINER:
1644 case LYS_LIST:
1645 dup = calloc(1, sizeof(struct lyd_node_inner));
1646 break;
1647 case LYS_LEAF:
1648 case LYS_LEAFLIST:
1649 dup = calloc(1, sizeof(struct lyd_node_term));
1650 break;
1651 case LYS_ANYDATA:
1652 case LYS_ANYXML:
1653 dup = calloc(1, sizeof(struct lyd_node_any));
1654 break;
1655 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001656 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001657 ret = LY_EINT;
1658 goto error;
1659 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001660 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001661 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001662
Michal Vaskof6df0a02020-06-16 13:08:34 +02001663 if (options & LYD_DUP_WITH_FLAGS) {
1664 dup->flags = node->flags;
1665 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001666 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001667 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001668 if (trg_ctx == LYD_CTX(node)) {
1669 dup->schema = node->schema;
1670 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001671 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001672 if (ret) {
1673 /* has no schema but is not an opaque node */
1674 free(dup);
1675 dup = NULL;
1676 goto error;
1677 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001678 }
Michal Vasko52927e22020-03-16 17:26:14 +01001679 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001680
Michal Vasko9cf62422021-07-01 13:29:32 +02001681 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001682 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001683 if (!node->schema) {
1684 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1685 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1686 }
1687 } else {
1688 LY_LIST_FOR(node->meta, meta) {
1689 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1690 }
Michal Vasko25a32822020-07-09 15:48:22 +02001691 }
1692 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001693
1694 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001695 if (!dup->schema) {
1696 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1697 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1698 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001699
1700 if (options & LYD_DUP_RECURSIVE) {
1701 /* duplicate all the children */
1702 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001703 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001704 }
1705 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001706 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1707 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1708 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1709 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001710 opaq->hints = orig->hints;
1711 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001712 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001713 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001714 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001715 }
Michal Vasko52927e22020-03-16 17:26:14 +01001716 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1717 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1718 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1719
1720 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001721 if (trg_ctx == LYD_CTX(node)) {
1722 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1723 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1724 } else {
1725 /* store canonical value in the target context */
1726 val_can = lyd_get_value(node);
1727 type = ((struct lysc_node_leaf *)term->schema)->type;
1728 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1729 LYD_HINT_DATA, term->schema, NULL);
1730 LY_CHECK_GOTO(ret, error);
1731 }
Michal Vasko52927e22020-03-16 17:26:14 +01001732 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1733 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1734 struct lyd_node *child;
1735
1736 if (options & LYD_DUP_RECURSIVE) {
1737 /* duplicate all the children */
1738 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001739 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001740 }
Michal Vasko69730152020-10-09 16:30:07 +02001741 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001742 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001743 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001744 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001745 }
1746 }
1747 lyd_hash(dup);
1748 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001749 dup->hash = node->hash;
1750 any = (struct lyd_node_any *)node;
1751 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001752 }
1753
Michal Vasko52927e22020-03-16 17:26:14 +01001754 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001755 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001756
1757 if (dup_p) {
1758 *dup_p = dup;
1759 }
1760 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001761
1762error:
Michal Vasko52927e22020-03-16 17:26:14 +01001763 lyd_free_tree(dup);
1764 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001765}
1766
Michal Vasko29d674b2021-08-25 11:18:35 +02001767/**
1768 * @brief Get a parent node to connect duplicated subtree to.
1769 *
1770 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001771 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001772 * @param[in] parent Initial parent to connect to.
1773 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1774 * @param[out] dup_parent First duplicated parent node, if any.
1775 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1776 * @return LY_ERR value.
1777 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001778static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001779lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1780 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001781{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001782 const struct lyd_node_inner *orig_parent, *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001783 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001784
1785 *dup_parent = NULL;
1786 *local_parent = NULL;
1787
Michal Vasko83522192022-07-20 08:07:34 +02001788 if (node->flags & LYD_EXT) {
1789 ext_parent = 1;
1790 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001791 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
Michal Vasko83522192022-07-20 08:07:34 +02001792 if (ext_parent) {
1793 /* use the standard context */
1794 trg_ctx = LYD_CTX(orig_parent);
1795 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001796 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001797 /* stop creating parents, connect what we have into the provided parent */
1798 iter = parent;
1799 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001800 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1801 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
1802 lyd_compare_schema_parents_equal(&parent->node, &orig_parent->node)) {
1803 iter = parent;
1804 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001805 } else {
1806 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001807 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options,
Radek Krejci0f969882020-08-21 16:56:47 +02001808 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001809 }
1810 if (!*local_parent) {
1811 *local_parent = (struct lyd_node_inner *)iter;
1812 }
1813 if (iter->child) {
1814 /* 1) list - add after keys
1815 * 2) provided parent with some children */
1816 iter->child->prev->next = *dup_parent;
1817 if (*dup_parent) {
1818 (*dup_parent)->prev = iter->child->prev;
1819 iter->child->prev = *dup_parent;
1820 }
1821 } else {
1822 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1823 }
1824 if (*dup_parent) {
1825 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1826 }
1827 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001828 if (orig_parent->flags & LYD_EXT) {
1829 ext_parent = 1;
1830 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001831 }
1832
1833 if (repeat && parent) {
1834 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001835 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001836 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001837 return LY_EINVAL;
1838 }
1839
1840 return LY_SUCCESS;
1841}
1842
1843static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001844lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1845 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001846{
1847 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001848 const struct lyd_node *orig; /* original node to be duplicated */
1849 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001850 struct lyd_node *top = NULL; /* the most higher created node */
1851 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001852
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001853 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001854
1855 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001856 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 +02001857 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001858 } else {
1859 local_parent = parent;
1860 }
1861
Radek Krejci22ebdba2019-07-25 13:59:43 +02001862 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001863 if (lysc_is_key(orig->schema)) {
1864 if (local_parent) {
1865 /* the key must already exist in the parent */
1866 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001867 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001868 } else {
1869 assert(!(options & LYD_DUP_WITH_PARENTS));
1870 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001871 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001872 LY_CHECK_GOTO(rc, error);
1873 }
1874 } else {
1875 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001876 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1877 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001878 LY_CHECK_GOTO(rc, error);
1879 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001880 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001881 break;
1882 }
1883 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001884
Michal Vasko3a41dff2020-07-15 14:30:28 +02001885 if (dup) {
1886 *dup = first;
1887 }
1888 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001889
1890error:
1891 if (top) {
1892 lyd_free_tree(top);
1893 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001894 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001895 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001896 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001897}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001898
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001899/**
1900 * @brief Check the context of node and parent when duplicating nodes.
1901 *
1902 * @param[in] node Node to duplicate.
1903 * @param[in] parent Parent of the duplicated node(s).
1904 * @return LY_ERR value.
1905 */
1906static LY_ERR
1907lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1908{
1909 const struct lyd_node *iter;
1910
1911 if (!node || !parent) {
1912 return LY_SUCCESS;
1913 }
1914
1915 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1916 /* try to find top-level ext data parent */
1917 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1918
1919 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001920 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001921 return LY_EINVAL;
1922 }
1923 }
1924
1925 return LY_SUCCESS;
1926}
1927
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001928LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001929lyd_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 +02001930{
Michal Vaskoddd76592022-01-17 13:34:48 +01001931 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001932 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001933
1934 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1935}
1936
1937LIBYANG_API_DEF LY_ERR
1938lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1939 uint32_t options, struct lyd_node **dup)
1940{
1941 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1942
1943 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001944}
1945
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001946LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001947lyd_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 +02001948{
Michal Vaskoddd76592022-01-17 13:34:48 +01001949 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001950 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001951
1952 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1953}
1954
1955LIBYANG_API_DEF LY_ERR
1956lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1957 uint32_t options, struct lyd_node **dup)
1958{
1959 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1960
1961 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001962}
1963
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001964LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001965lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001966{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001967 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02001968 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02001969 struct lyd_meta *mt, *last;
1970
Michal Vasko3a41dff2020-07-15 14:30:28 +02001971 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001972
Michal Vasko33c48972022-07-20 10:28:07 +02001973 /* log to node context but value must always use the annotation context */
1974 ctx = meta->annotation->module->ctx;
1975
Michal Vasko25a32822020-07-09 15:48:22 +02001976 /* create a copy */
1977 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001978 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001979 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02001980 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001981 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02001982 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001983
1984 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001985 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001986 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001987 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001988 last->next = mt;
1989 } else {
1990 node->meta = mt;
1991 }
1992
Radek Krejci011e4aa2020-09-04 15:22:31 +02001993finish:
1994 if (ret) {
1995 lyd_free_meta_single(mt);
1996 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001997 *dup = mt;
1998 }
1999 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002000}
2001
Michal Vasko4490d312020-06-16 13:08:55 +02002002/**
2003 * @brief Merge a source sibling into target siblings.
2004 *
2005 * @param[in,out] first_trg First target sibling, is updated if top-level.
2006 * @param[in] parent_trg Target parent.
2007 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002008 * @param[in] merge_cb Optional merge callback.
2009 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002010 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002011 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002012 * @return LY_ERR value.
2013 */
2014static LY_ERR
2015lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Michal Vasko271d2e32023-01-31 15:43:19 +01002016 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct hash_table **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002017{
Michal Vasko4490d312020-06-16 13:08:55 +02002018 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002019 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002020 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002021 struct lysc_type *type;
Michal Vasko271d2e32023-01-31 15:43:19 +01002022 struct hash_table *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002023 LY_ERR ret;
2024 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002025
2026 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002027 if (!sibling_src->schema) {
2028 /* try to find the same opaque node */
2029 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2030 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002031 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002032 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002033 } else {
2034 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002035 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002036 }
2037
Michal Vaskocd3f6172021-05-18 16:14:50 +02002038 if (match_trg) {
2039 /* update match as needed */
2040 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2041 } else {
2042 /* first instance of this node */
2043 first_inst = 1;
2044 }
2045
2046 if (match_trg) {
2047 /* call callback */
2048 if (merge_cb) {
2049 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2050 }
2051
Michal Vasko4490d312020-06-16 13:08:55 +02002052 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002053 if (!match_trg->schema) {
2054 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2055 /* update value */
2056 opaq_trg = (struct lyd_node_opaq *)match_trg;
2057 opaq_src = (struct lyd_node_opaq *)sibling_src;
2058
2059 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2060 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2061 opaq_trg->hints = opaq_src->hints;
2062
2063 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2064 opaq_trg->format = opaq_src->format;
2065 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2066 &opaq_trg->val_prefix_data);
2067 }
2068 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2069 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002070 /* since they are different, they cannot both be default */
2071 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2072
Michal Vasko3a41dff2020-07-15 14:30:28 +02002073 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2074 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002075 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002076 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2077 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002078 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002079
2080 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002081 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002082 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002083 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002084 /* update value */
2085 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002086 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002087
2088 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002089 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002090 }
2091
2092 /* check descendants, recursively */
2093 ret = LY_SUCCESS;
2094 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2095 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2096 &child_dup_inst);
2097 if (ret) {
2098 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002099 }
2100 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002101 lyd_dup_inst_free(child_dup_inst);
2102 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002103 } else {
2104 /* node not found, merge it */
2105 if (options & LYD_MERGE_DESTRUCT) {
2106 dup_src = (struct lyd_node *)sibling_src;
2107 lyd_unlink_tree(dup_src);
2108 /* spend it */
2109 *sibling_src_p = NULL;
2110 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002111 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 +02002112 }
2113
Michal Vasko7e8315b2021-08-03 17:01:06 +02002114 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2115 /* set LYD_NEW for all the new nodes, required for validation */
2116 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2117 elem->flags |= LYD_NEW;
2118 LYD_TREE_DFS_END(dup_src, elem);
2119 }
Michal Vasko4490d312020-06-16 13:08:55 +02002120 }
2121
Michal Vaskocd3f6172021-05-18 16:14:50 +02002122 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002123 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002124
2125 if (first_inst) {
2126 /* remember not to find this instance next time */
2127 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2128 }
2129
2130 /* call callback, no source node */
2131 if (merge_cb) {
2132 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2133 }
Michal Vasko4490d312020-06-16 13:08:55 +02002134 }
2135
2136 return LY_SUCCESS;
2137}
2138
Michal Vasko3a41dff2020-07-15 14:30:28 +02002139static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002140lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2141 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002142{
2143 const struct lyd_node *sibling_src, *tmp;
Michal Vasko271d2e32023-01-31 15:43:19 +01002144 struct hash_table *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002145 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002146 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002147
2148 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002149 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2150 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002151
2152 if (!source) {
2153 /* nothing to merge */
2154 return LY_SUCCESS;
2155 }
2156
Michal Vasko831422b2020-11-24 11:20:51 +01002157 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002158 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002159 return LY_EINVAL;
2160 }
2161
2162 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002163 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2164 /* skip data nodes from different modules */
2165 continue;
2166 }
2167
Radek Krejci857189e2020-09-01 13:26:36 +02002168 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002169 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2170 if (ret) {
2171 break;
2172 }
Michal Vasko4490d312020-06-16 13:08:55 +02002173 if (first && !sibling_src) {
2174 /* source was spent (unlinked), move to the next node */
2175 source = tmp;
2176 }
2177
Michal Vasko3a41dff2020-07-15 14:30:28 +02002178 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002179 break;
2180 }
2181 }
2182
2183 if (options & LYD_MERGE_DESTRUCT) {
2184 /* free any leftover source data that were not merged */
2185 lyd_free_siblings((struct lyd_node *)source);
2186 }
2187
Michal Vaskocd3f6172021-05-18 16:14:50 +02002188 lyd_dup_inst_free(dup_inst);
2189 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002190}
2191
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002192LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002193lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002194{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002195 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002196}
2197
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002198LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002199lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002200{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002201 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2202}
2203
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002204LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002205lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2206 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2207{
2208 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002209}
2210
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002211static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002212lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002213{
Michal Vasko14654712020-02-06 08:35:21 +01002214 /* ending \0 */
2215 ++reqlen;
2216
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002217 if (reqlen > *buflen) {
2218 if (is_static) {
2219 return LY_EINCOMPLETE;
2220 }
2221
2222 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2223 if (!*buffer) {
2224 return LY_EMEM;
2225 }
2226
2227 *buflen = reqlen;
2228 }
2229
2230 return LY_SUCCESS;
2231}
2232
Michal Vaskod59035b2020-07-08 12:00:06 +02002233LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002234lyd_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 +02002235{
2236 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002237 size_t len;
2238 const char *val;
2239 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002240
Michal Vasko824d0832021-11-04 15:36:51 +01002241 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002242 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002243 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002244 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002245
2246 quot = '\'';
2247 if (strchr(val, '\'')) {
2248 quot = '"';
2249 }
2250 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002251 }
2252
2253 return LY_SUCCESS;
2254}
2255
2256/**
2257 * @brief Append leaf-list value predicate to path.
2258 *
2259 * @param[in] node Node to print.
2260 * @param[in,out] buffer Buffer to print to.
2261 * @param[in,out] buflen Current buffer length.
2262 * @param[in,out] bufused Current number of characters used in @p buffer.
2263 * @param[in] is_static Whether buffer is static or can be reallocated.
2264 * @return LY_ERR
2265 */
2266static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002267lyd_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 +02002268{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002269 size_t len;
2270 const char *val;
2271 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002272
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002273 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002274 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002275 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002276
2277 quot = '\'';
2278 if (strchr(val, '\'')) {
2279 quot = '"';
2280 }
2281 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2282
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002283 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002284}
2285
2286/**
2287 * @brief Append node position (relative to its other instances) predicate to path.
2288 *
2289 * @param[in] node Node to print.
2290 * @param[in,out] buffer Buffer to print to.
2291 * @param[in,out] buflen Current buffer length.
2292 * @param[in,out] bufused Current number of characters used in @p buffer.
2293 * @param[in] is_static Whether buffer is static or can be reallocated.
2294 * @return LY_ERR
2295 */
2296static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002297lyd_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 +02002298{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002299 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002300 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002301 char *val = NULL;
2302 LY_ERR rc;
2303
Michal Vasko50cc0562021-05-18 16:15:43 +02002304 pos = lyd_list_pos(node);
2305 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002306 return LY_EMEM;
2307 }
2308
2309 len = 1 + strlen(val) + 1;
2310 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2311 if (rc != LY_SUCCESS) {
2312 goto cleanup;
2313 }
2314
2315 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2316
2317cleanup:
2318 free(val);
2319 return rc;
2320}
2321
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002322LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002323lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2324{
Radek Krejci857189e2020-09-01 13:26:36 +02002325 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002326 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002327 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002328 const struct lyd_node *iter, *parent;
2329 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002330 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002331
2332 LY_CHECK_ARG_RET(NULL, node, NULL);
2333 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002334 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002335 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002336 } else {
2337 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002338 }
2339
2340 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002341 case LYD_PATH_STD:
2342 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002343 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002344 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002345 ++depth;
2346 }
2347
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002348 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002349 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002350 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002351 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002352iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002353 /* get the module */
2354 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2355 parent = lyd_parent(iter);
2356 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2357 if (prev_mod == mod) {
2358 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002359 }
2360
2361 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002362 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2363 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002364 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2365 if (rc != LY_SUCCESS) {
2366 break;
2367 }
2368
2369 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002370 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002371
Michal Vasko790b2bc2020-08-03 13:35:06 +02002372 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002373 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002374 switch (iter->schema->nodetype) {
2375 case LYS_LIST:
2376 if (iter->schema->flags & LYS_KEYLESS) {
2377 /* print its position */
2378 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2379 } else {
2380 /* print all list keys in predicates */
2381 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2382 }
2383 break;
2384 case LYS_LEAFLIST:
2385 if (iter->schema->flags & LYS_CONFIG_W) {
2386 /* print leaf-list value */
2387 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2388 } else {
2389 /* print its position */
2390 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2391 }
2392 break;
2393 default:
2394 /* nothing to print more */
2395 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002396 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002397 }
2398 if (rc != LY_SUCCESS) {
2399 break;
2400 }
2401
Michal Vasko14654712020-02-06 08:35:21 +01002402 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002403 }
2404 break;
2405 }
2406
2407 return buffer;
2408}
Michal Vaskoe444f752020-02-10 12:20:06 +01002409
Michal Vaskodbf3e652022-10-21 08:46:25 +02002410char *
2411lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2412{
2413 uint32_t depth;
2414 size_t bufused = 0, buflen = 0, len;
2415 char *buffer = NULL;
2416 const struct lyd_node *iter, *parent;
2417 const struct lys_module *mod, *prev_mod;
2418 LY_ERR rc = LY_SUCCESS;
2419
2420 switch (pathtype) {
2421 case LYD_PATH_STD:
2422 case LYD_PATH_STD_NO_LAST_PRED:
2423 for (depth = 1; depth <= dnodes->count; ++depth) {
2424 /* current node */
2425 iter = dnodes->dnodes[depth - 1];
2426 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2427
2428 /* parent */
2429 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002430 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2431 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002432
2433 /* get module to print, if any */
2434 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2435 if (prev_mod == mod) {
2436 mod = NULL;
2437 }
2438
2439 /* realloc string */
2440 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2441 strlen(((struct lyd_node_opaq *)iter)->name.name));
2442 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2443 break;
2444 }
2445
2446 /* print next node */
2447 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2448
2449 /* do not always print the last (first) predicate */
2450 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2451 switch (iter->schema->nodetype) {
2452 case LYS_LIST:
2453 if (iter->schema->flags & LYS_KEYLESS) {
2454 /* print its position */
2455 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2456 } else {
2457 /* print all list keys in predicates */
2458 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2459 }
2460 break;
2461 case LYS_LEAFLIST:
2462 if (iter->schema->flags & LYS_CONFIG_W) {
2463 /* print leaf-list value */
2464 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2465 } else {
2466 /* print its position */
2467 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2468 }
2469 break;
2470 default:
2471 /* nothing to print more */
2472 break;
2473 }
2474 }
2475 if (rc) {
2476 break;
2477 }
2478 }
2479 break;
2480 }
2481
2482 return buffer;
2483}
2484
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002485LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002486lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2487{
2488 struct lyd_meta *ret = NULL;
2489 const struct ly_ctx *ctx;
2490 const char *prefix, *tmp;
2491 char *str;
2492 size_t pref_len, name_len;
2493
2494 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002495 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002496
2497 if (!first) {
2498 return NULL;
2499 }
2500
2501 ctx = first->annotation->module->ctx;
2502
2503 /* parse the name */
2504 tmp = name;
2505 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2506 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2507 return NULL;
2508 }
2509
2510 /* find the module */
2511 if (prefix) {
2512 str = strndup(prefix, pref_len);
2513 module = ly_ctx_get_module_latest(ctx, str);
2514 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002515 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 +02002516 }
2517
2518 /* find the metadata */
2519 LY_LIST_FOR(first, first) {
2520 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2521 ret = (struct lyd_meta *)first;
2522 break;
2523 }
2524 }
2525
2526 return ret;
2527}
2528
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002529LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002530lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2531{
Michal Vasko9beceb82022-04-05 12:14:15 +02002532 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002533 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002534 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002535
Michal Vaskof03ed032020-03-04 13:31:44 +01002536 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002537 if (!siblings) {
2538 /* no data */
2539 if (match) {
2540 *match = NULL;
2541 }
2542 return LY_ENOTFOUND;
2543 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002544
Michal Vasko9beceb82022-04-05 12:14:15 +02002545 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2546 /* create a duplicate in this context */
2547 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2548 target = dup;
2549 }
2550
2551 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2552 /* schema mismatch */
2553 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002554 if (match) {
2555 *match = NULL;
2556 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002557 return LY_ENOTFOUND;
2558 }
2559
Michal Vaskoe78faec2021-04-08 17:24:43 +02002560 /* get first sibling */
2561 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002562
Michal Vasko9e685082021-01-29 14:49:09 +01002563 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002564 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002565 assert(target->hash);
2566
Michal Vaskoe78faec2021-04-08 17:24:43 +02002567 if (lysc_is_dup_inst_list(target->schema)) {
2568 /* we must search the instances from beginning to find the first matching one */
2569 found = 0;
2570 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2571 if (!lyd_compare_single(target, iter, 0)) {
2572 found = 1;
2573 break;
2574 }
2575 }
2576 if (found) {
2577 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002578 } else {
2579 siblings = NULL;
2580 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002581 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002582 /* find by hash */
2583 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2584 siblings = *match_p;
2585 } else {
2586 /* not found */
2587 siblings = NULL;
2588 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002589 }
2590 } else {
2591 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002592 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002593 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002594 break;
2595 }
2596 }
2597 }
2598
Michal Vasko9beceb82022-04-05 12:14:15 +02002599 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002600 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002601 if (match) {
2602 *match = NULL;
2603 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002604 return LY_ENOTFOUND;
2605 }
2606
Michal Vasko9b368d32020-02-14 13:53:31 +01002607 if (match) {
2608 *match = (struct lyd_node *)siblings;
2609 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002610 return LY_SUCCESS;
2611}
2612
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002613LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002614lyd_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 +02002615 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002616{
2617 LY_ERR rc;
2618 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002619 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002620
Michal Vasko4c583e82020-07-17 12:16:14 +02002621 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002622 if (!siblings) {
2623 /* no data */
2624 if (match) {
2625 *match = NULL;
2626 }
2627 return LY_ENOTFOUND;
2628 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002629
Michal Vasko9beceb82022-04-05 12:14:15 +02002630 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2631 /* parent of ext nodes is useless */
2632 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002633 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2634 /* no schema node in siblings so certainly no data node either */
2635 if (match) {
2636 *match = NULL;
2637 }
2638 return LY_ENOTFOUND;
2639 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002640 }
2641
2642 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2643 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002644 if (match) {
2645 *match = NULL;
2646 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002647 return LY_ENOTFOUND;
2648 }
2649
Michal Vaskof03ed032020-03-04 13:31:44 +01002650 if (key_or_value && !val_len) {
2651 val_len = strlen(key_or_value);
2652 }
2653
Michal Vaskob104f112020-07-17 09:54:54 +02002654 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2655 /* create a data node and find the instance */
2656 if (schema->nodetype == LYS_LEAFLIST) {
2657 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002658 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 +02002659 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002660 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002661 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002662 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002663 }
2664
2665 /* find it */
2666 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002667 } else {
2668 /* find the first schema node instance */
2669 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002670 }
2671
Michal Vaskoe444f752020-02-10 12:20:06 +01002672 lyd_free_tree(target);
2673 return rc;
2674}
Michal Vaskoccc02342020-05-21 10:09:21 +02002675
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002676LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002677lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2678{
2679 struct lyd_node **match_p, *first, *iter;
2680 struct lyd_node_inner *parent;
2681
Michal Vasko83ae7772022-06-08 10:01:55 +02002682 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002683 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002684
2685 LY_CHECK_RET(ly_set_new(set));
2686
2687 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2688 /* no data or schema mismatch */
2689 return LY_ENOTFOUND;
2690 }
2691
2692 /* get first sibling */
2693 siblings = lyd_first_sibling(siblings);
2694
2695 parent = siblings->parent;
2696 if (parent && parent->schema && parent->children_ht) {
2697 assert(target->hash);
2698
2699 /* find the first instance */
2700 lyd_find_sibling_first(siblings, target, &first);
2701 if (first) {
2702 /* add it so that it is the first in the set */
2703 if (ly_set_add(*set, first, 1, NULL)) {
2704 goto error;
2705 }
2706
2707 /* find by hash */
2708 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2709 iter = *match_p;
2710 } else {
2711 /* not found */
2712 iter = NULL;
2713 }
2714 while (iter) {
2715 /* add all found nodes into the set */
2716 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2717 goto error;
2718 }
2719
2720 /* find next instance */
2721 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2722 iter = NULL;
2723 } else {
2724 iter = *match_p;
2725 }
2726 }
2727 }
2728 } else {
2729 /* no children hash table */
2730 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002731 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002732 ly_set_add(*set, (void *)siblings, 1, NULL);
2733 }
2734 }
2735 }
2736
2737 if (!(*set)->count) {
2738 return LY_ENOTFOUND;
2739 }
2740 return LY_SUCCESS;
2741
2742error:
2743 ly_set_free(*set, NULL);
2744 *set = NULL;
2745 return LY_EMEM;
2746}
2747
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002748LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002749lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2750{
2751 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2752
2753 for ( ; first; first = first->next) {
2754 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2755 break;
2756 }
2757 }
2758
2759 if (match) {
2760 *match = (struct lyd_node *)first;
2761 }
2762 return first ? LY_SUCCESS : LY_ENOTFOUND;
2763}
2764
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002765LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002766lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002767{
Michal Vaskod96e2372023-02-24 16:07:51 +01002768 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002769
Michal Vaskod96e2372023-02-24 16:07:51 +01002770 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2771}
Michal Vaskoccc02342020-05-21 10:09:21 +02002772
Michal Vaskod96e2372023-02-24 16:07:51 +01002773LIBYANG_API_DEF LY_ERR
2774lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2775{
2776 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002777
Michal Vaskod96e2372023-02-24 16:07:51 +01002778 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002779}
Radek Krejcica989142020-11-05 11:32:22 +01002780
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002781LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002782lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2783 const struct lyxp_var *vars, struct ly_set **set)
2784{
2785 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2786
2787 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2788}
2789
2790LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002791lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2792 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002793{
Michal Vaskod96e2372023-02-24 16:07:51 +01002794 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002795
Michal Vaskod96e2372023-02-24 16:07:51 +01002796 *set = NULL;
2797
2798 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 +01002799}
2800
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002801LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002802lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002803{
Michal Vaskod96e2372023-02-24 16:07:51 +01002804 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002805}
2806
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002807LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002808lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2809{
2810 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2811}
2812
2813LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002814lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2815 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002816{
Michal Vaskod96e2372023-02-24 16:07:51 +01002817 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2818}
2819
2820LIBYANG_API_DEF LY_ERR
2821lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2822 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2823 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2824{
2825 LY_ERR ret = LY_SUCCESS;
2826 struct lyxp_set xp_set = {0};
2827 struct lyxp_expr *exp = NULL;
2828 uint32_t i;
2829
2830 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2831 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2832 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2833
2834 /* parse expression */
2835 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2836 LY_CHECK_GOTO(ret, cleanup);
2837
2838 /* evaluate expression */
2839 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2840 LYXP_IGNORE_WHEN);
2841 LY_CHECK_GOTO(ret, cleanup);
2842
2843 /* return expected result type without or with casting */
2844 if (node_set) {
2845 /* node set */
2846 if (xp_set.type == LYXP_SET_NODE_SET) {
2847 /* transform into a set */
2848 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2849 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2850 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2851 (*node_set)->size = xp_set.used;
2852 for (i = 0; i < xp_set.used; ++i) {
2853 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2854 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2855 LY_CHECK_GOTO(ret, cleanup);
2856 }
2857 }
2858 if (ret_type) {
2859 *ret_type = LY_XPATH_NODE_SET;
2860 }
2861 } else if (!string && !number && !boolean) {
2862 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2863 ret = LY_EINVAL;
2864 goto cleanup;
2865 }
2866 }
2867
2868 if (string) {
2869 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2870 /* cast into string */
2871 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2872 }
2873 if (xp_set.type == LYXP_SET_STRING) {
2874 /* string */
2875 *string = xp_set.val.str;
2876 xp_set.val.str = NULL;
2877 if (ret_type) {
2878 *ret_type = LY_XPATH_STRING;
2879 }
2880 }
2881 }
2882
2883 if (number) {
2884 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2885 /* cast into number */
2886 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2887 }
2888 if (xp_set.type == LYXP_SET_NUMBER) {
2889 /* number */
2890 *number = xp_set.val.num;
2891 if (ret_type) {
2892 *ret_type = LY_XPATH_NUMBER;
2893 }
2894 }
2895 }
2896
2897 if (boolean) {
2898 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2899 /* cast into boolean */
2900 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2901 }
2902 if (xp_set.type == LYXP_SET_BOOLEAN) {
2903 /* boolean */
2904 *boolean = xp_set.val.bln;
2905 if (ret_type) {
2906 *ret_type = LY_XPATH_BOOLEAN;
2907 }
2908 }
2909 }
2910
2911cleanup:
2912 lyxp_set_free_content(&xp_set);
2913 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
2914 return ret;
aPiecekfba75362021-10-07 12:39:48 +02002915}
2916
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002917LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002918lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2919{
2920 LY_ERR ret = LY_SUCCESS;
2921 struct lyxp_expr *expr = NULL;
2922 struct ly_path *lypath = NULL;
2923
2924 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2925
2926 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002927 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 +01002928 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002929 LY_CHECK_GOTO(ret, cleanup);
2930
2931 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002932 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002933 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 +01002934 LY_CHECK_GOTO(ret, cleanup);
2935
2936 /* evaluate the path */
Michal Vasko90189962023-02-28 12:10:34 +01002937 ret = ly_path_eval_partial(lypath, ctx_node, NULL, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002938
2939cleanup:
2940 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2941 ly_path_free(LYD_CTX(ctx_node), lypath);
2942 return ret;
2943}
2944
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002945LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002946lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2947{
2948 LY_ERR ret;
2949 struct lyd_node *m;
2950
2951 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2952
Michal Vasko90189962023-02-28 12:10:34 +01002953 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02002954 if (ret) {
2955 if (match) {
2956 *match = NULL;
2957 }
2958 return LY_ENOTFOUND;
2959 }
2960
2961 if (match) {
2962 *match = m;
2963 }
2964 return LY_SUCCESS;
2965}