blob: 21a0aba28614fcc6935e63f55010aa2950989fd4 [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 Vasko50da8cd2023-03-10 08:38:59 +0100140 if (r) {
141 rc = r;
142 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) ||
143 (ly_vecode(ctx) == LYVE_SYNTAX)) {
144 goto cleanup;
145 }
146 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100147
148 if (parent) {
149 /* get first top-level sibling */
150 for (first = parent; first->parent; first = lyd_parent(first)) {}
151 first = lyd_first_sibling(first);
152 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200153 }
154
Michal Vaskoe0665742021-02-11 11:08:44 +0100155 if (!(parse_opts & LYD_PARSE_ONLY)) {
156 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100157 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 +0200158 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100159 LY_CHECK_GOTO(rc, cleanup);
160 }
Radek Krejci7931b192020-06-25 17:05:03 +0200161
Michal Vaskoe0665742021-02-11 11:08:44 +0100162 /* set the operation node */
163 if (op) {
164 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200165 }
166
167cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100168 if (lydctx) {
169 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200170 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100171 if (rc) {
172 if (parent) {
173 /* free all the parsed subtrees */
174 for (i = 0; i < parsed.count; ++i) {
175 lyd_free_tree(parsed.dnodes[i]);
176 }
177 } else {
178 /* free everything */
179 lyd_free_all(*first_p);
180 *first_p = NULL;
181 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100182 } else if (subtree_sibling) {
183 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100184 }
185 ly_set_erase(&parsed, NULL);
186 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200187}
188
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100189LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100190lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
191 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
192{
193 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
194
195 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
196 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
197 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
198
199 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
200}
201
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100202LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100203lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
204 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
205{
206 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
207 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
208 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
209
Radek Krejcif16e2542021-02-17 15:39:23 +0100210 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100211}
212
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100213LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100214lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
215 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200216{
217 LY_ERR ret;
218 struct ly_in *in;
219
220 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100221 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200222
223 ly_in_free(in, 0);
224 return ret;
225}
226
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100227LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200228lyd_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 +0200229 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200230{
231 LY_ERR ret;
232 struct ly_in *in;
233
234 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100235 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200236
237 ly_in_free(in, 0);
238 return ret;
239}
240
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100241LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200242lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
243 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200244{
245 LY_ERR ret;
246 struct ly_in *in;
247
248 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100249 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200250
251 ly_in_free(in, 0);
252 return ret;
253}
254
Radek Krejcif16e2542021-02-17 15:39:23 +0100255/**
256 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
257 * for schema nodes locked inside the extension instance.
258 *
259 * At least one of @p parent, @p tree, or @p op must always be set.
260 *
261 * Specific @p data_type values have different parameter meaning as follows:
262 * - ::LYD_TYPE_RPC_NETCONF:
263 * - @p parent - must be NULL, the whole RPC is expected;
264 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
265 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
266 * a separate opaque data tree, even if the function fails, this may be returned;
267 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
268 *
269 * - ::LYD_TYPE_NOTIF_NETCONF:
270 * - @p parent - must be NULL, the whole notification is expected;
271 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
272 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
273 * a separate opaque data tree, even if the function fails, this may be returned;
274 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
275 *
276 * - ::LYD_TYPE_REPLY_NETCONF:
277 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
278 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
279 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
280 * a separate opaque data tree, even if the function fails, this may be returned;
281 * - @p op - must be NULL, the reply is appended to the RPC;
282 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
283 *
284 * @param[in] ctx libyang context.
285 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
286 * @param[in] parent Optional parent to connect the parsed nodes to.
287 * @param[in] in Input handle to read the input from.
288 * @param[in] format Expected format of the data in @p in.
289 * @param[in] data_type Expected operation to parse (@ref datatype).
290 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
291 * @param[out] op Optional parsed operation node.
292 * @return LY_ERR value.
293 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
294 */
295static LY_ERR
296lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
297 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 +0200298{
Michal Vaskoe0665742021-02-11 11:08:44 +0100299 LY_ERR rc = LY_SUCCESS;
300 struct lyd_ctx *lydctx = NULL;
301 struct ly_set parsed = {0};
302 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100303 uint32_t i, parse_opts, val_opts, int_opts = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200304
Michal Vasko27fb0262021-02-23 09:42:01 +0100305 if (!ctx) {
306 ctx = LYD_CTX(parent);
307 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200308 if (tree) {
309 *tree = NULL;
310 }
311 if (op) {
312 *op = NULL;
313 }
314
Radek Krejci7931b192020-06-25 17:05:03 +0200315 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200316
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 /* remember input position */
318 in->func_start = in->current;
319
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100320 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200321 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100322 val_opts = 0;
323
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100324 switch (data_type) {
325
326 /* special XML NETCONF data types */
327 case LYD_TYPE_RPC_NETCONF:
328 case LYD_TYPE_NOTIF_NETCONF:
329 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
330 /* fallthrough */
331 case LYD_TYPE_REPLY_NETCONF:
332 if (data_type == LYD_TYPE_REPLY_NETCONF) {
333 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
334 LY_EINVAL);
335 }
336
337 /* parse the NETCONF message */
338 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 +0100339 if (rc) {
340 if (envp) {
341 /* special situation when the envelopes were parsed successfully */
342 *tree = envp;
343 }
Michal Vasko73792a12022-05-10 09:28:45 +0200344 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100345 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100346
347 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100348 if (envp) {
349 /* special out param meaning */
350 *tree = envp;
351 } else {
352 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100353 }
354 if (op) {
355 *op = lydctx->op_node;
356 }
357 goto cleanup;
358
359 /* set internal opts */
360 case LYD_TYPE_RPC_YANG:
361 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
362 break;
363 case LYD_TYPE_NOTIF_YANG:
364 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
365 break;
366 case LYD_TYPE_REPLY_YANG:
367 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
368 break;
369 case LYD_TYPE_DATA_YANG:
370 LOGINT(ctx);
371 rc = LY_EINT;
372 goto cleanup;
373 }
374
375 /* parse the data */
376 switch (format) {
377 case LYD_XML:
378 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 +0100379 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200380 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100381 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 +0100382 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200383 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100384 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 +0100385 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200386 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100387 LOGARG(ctx, format);
388 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200389 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200390 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100391 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200392
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 /* set out params correctly */
394 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100395 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100396 }
397 if (op) {
398 *op = lydctx->op_node;
399 }
400
401cleanup:
402 if (lydctx) {
403 lydctx->free(lydctx);
404 }
405 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200406 /* free all the parsed nodes */
407 if (parsed.count) {
408 i = parsed.count;
409 do {
410 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100411 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200412 } while (i);
413 }
414 if (tree && ((format != LYD_XML) || !envp)) {
415 *tree = NULL;
416 }
417 if (op) {
418 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100419 }
420 }
421 ly_set_erase(&parsed, NULL);
422 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200423}
Radek Krejci084289f2019-07-09 17:35:30 +0200424
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100425LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100426lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
427 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
428{
429 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
430
431 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
432}
433
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100434LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100435lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
436 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
437{
438 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
439
440 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
441
442 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
443}
444
Michal Vasko90932a92020-02-12 14:33:03 +0100445struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200446lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100447{
Michal Vaskob104f112020-07-17 09:54:54 +0200448 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100449 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200450 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100451 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100452
Michal Vaskob104f112020-07-17 09:54:54 +0200453 assert(new_node);
454
Michal Vasko9beceb82022-04-05 12:14:15 +0200455 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200456 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100457 return NULL;
458 }
459
Michal Vasko93b16062020-12-09 18:14:18 +0100460 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100461 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100462 getnext_opts = LYS_GETNEXT_OUTPUT;
463 }
464
Michal Vaskoa2756f02021-07-09 13:20:28 +0200465 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200466 /* find the anchor using hashes */
467 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100468 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200469 while (schema) {
470 /* keep trying to find the first existing instance of the closest following schema sibling,
471 * otherwise return NULL - inserting at the end */
472 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
473 break;
474 }
475
Michal Vasko93b16062020-12-09 18:14:18 +0100476 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200477 }
478 } else {
479 /* find the anchor without hashes */
480 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200481 sparent = lysc_data_parent(new_node->schema);
482 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200483 /* we are in top-level, skip all the data from preceding modules */
484 LY_LIST_FOR(match, match) {
485 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
486 break;
487 }
488 }
489 }
490
491 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100492 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200493
494 found = 0;
495 LY_LIST_FOR(match, match) {
496 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
497 /* we have found an opaque node, which must be at the end, so use it OR
498 * modules do not match, so we must have traversed all the data from new_node module (if any),
499 * we have found the first node of the next module, that is what we want */
500 break;
501 }
502
503 /* skip schema nodes until we find the instantiated one */
504 while (!found) {
505 if (new_node->schema == schema) {
506 /* we have found the schema of the new node, continue search to find the first
507 * data node with a different schema (after our schema) */
508 found = 1;
509 break;
510 }
511 if (match->schema == schema) {
512 /* current node (match) is a data node still before the new node, continue search in data */
513 break;
514 }
Michal Vasko93b16062020-12-09 18:14:18 +0100515 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200516 assert(schema);
517 }
518
519 if (found && (match->schema != new_node->schema)) {
520 /* find the next node after we have found our node schema data instance */
521 break;
522 }
523 }
Michal Vasko90932a92020-02-12 14:33:03 +0100524 }
525
526 return match;
527}
528
Michal Vaskoc61dd062022-06-07 11:01:28 +0200529void
Michal Vaskof03ed032020-03-04 13:31:44 +0100530lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100531{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100532 struct lyd_node_inner *par;
533
Michal Vasko90932a92020-02-12 14:33:03 +0100534 assert(!node->next && (node->prev == node));
535
536 node->next = sibling->next;
537 node->prev = sibling;
538 sibling->next = node;
539 if (node->next) {
540 /* sibling had a succeeding node */
541 node->next->prev = node;
542 } else {
543 /* sibling was last, find first sibling and change its prev */
544 if (sibling->parent) {
545 sibling = sibling->parent->child;
546 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200547 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100548 }
549 sibling->prev = node;
550 }
551 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100552
Michal Vasko9f96a052020-03-10 09:41:45 +0100553 for (par = node->parent; par; par = par->parent) {
554 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
555 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100556 par->flags &= ~LYD_DEFAULT;
557 }
558 }
Michal Vasko90932a92020-02-12 14:33:03 +0100559}
560
Michal Vaskoc61dd062022-06-07 11:01:28 +0200561void
Michal Vaskof03ed032020-03-04 13:31:44 +0100562lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100563{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100564 struct lyd_node_inner *par;
565
Michal Vasko90932a92020-02-12 14:33:03 +0100566 assert(!node->next && (node->prev == node));
567
568 node->next = sibling;
569 /* covers situation of sibling being first */
570 node->prev = sibling->prev;
571 sibling->prev = node;
572 if (node->prev->next) {
573 /* sibling had a preceding node */
574 node->prev->next = node;
575 } else if (sibling->parent) {
576 /* sibling was first and we must also change parent child pointer */
577 sibling->parent->child = node;
578 }
579 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100580
Michal Vasko9f96a052020-03-10 09:41:45 +0100581 for (par = node->parent; par; par = par->parent) {
582 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
583 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100584 par->flags &= ~LYD_DEFAULT;
585 }
586 }
Michal Vasko90932a92020-02-12 14:33:03 +0100587}
588
589/**
Michal Vaskob104f112020-07-17 09:54:54 +0200590 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100591 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100592 * Handles inserting into NP containers and key-less lists.
593 *
Michal Vasko90932a92020-02-12 14:33:03 +0100594 * @param[in] parent Parent to insert into.
595 * @param[in] node Node to insert.
596 */
597static void
Michal Vaskob104f112020-07-17 09:54:54 +0200598lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100599{
600 struct lyd_node_inner *par;
601
Radek Krejcia1c1e542020-09-29 16:06:52 +0200602 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100603 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100604
605 par = (struct lyd_node_inner *)parent;
606
Michal Vaskob104f112020-07-17 09:54:54 +0200607 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100608 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100609
Michal Vaskod989ba02020-08-24 10:59:24 +0200610 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100611 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
612 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100613 par->flags &= ~LYD_DEFAULT;
614 }
615 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200616}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100617
Michal Vasko751cb4d2020-07-14 12:25:28 +0200618/**
619 * @brief Learn whether a list instance has all the keys.
620 *
621 * @param[in] list List instance to check.
622 * @return non-zero if all the keys were found,
623 * @return 0 otherwise.
624 */
625static int
626lyd_insert_has_keys(const struct lyd_node *list)
627{
628 const struct lyd_node *key;
629 const struct lysc_node *skey = NULL;
630
631 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200632 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200633 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
634 if (!key || (key->schema != skey)) {
635 /* key missing */
636 return 0;
637 }
638
639 key = key->next;
640 }
641
642 /* all keys found */
643 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100644}
645
646void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200647lyd_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 +0100648{
Michal Vaskob104f112020-07-17 09:54:54 +0200649 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100650
Michal Vaskob104f112020-07-17 09:54:54 +0200651 /* inserting list without its keys is not supported */
652 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100653 assert(!parent || !parent->schema ||
654 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100655
Michal Vaskob104f112020-07-17 09:54:54 +0200656 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100657 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100658 }
Michal Vasko90932a92020-02-12 14:33:03 +0100659
Michal Vaskob104f112020-07-17 09:54:54 +0200660 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100661 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100662
Michal Vasko9beceb82022-04-05 12:14:15 +0200663 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200664 /* no next anchor */
665 anchor = NULL;
666 } else {
667 /* find the anchor, our next node, so we can insert before it */
668 anchor = lyd_insert_get_next_anchor(first_sibling, node);
669 }
670
Michal Vaskob104f112020-07-17 09:54:54 +0200671 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200672 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200673 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100674 if (!parent && (*first_sibling_p == anchor)) {
675 /* move first sibling */
676 *first_sibling_p = node;
677 }
Michal Vaskob104f112020-07-17 09:54:54 +0200678 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200679 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200680 lyd_insert_after_node(first_sibling->prev, node);
681 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200682 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200683 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100684 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200685 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200686 *first_sibling_p = node;
687 }
688
689 /* insert into parent HT */
690 lyd_insert_hash(node);
691
692 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200693 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200694 lyd_hash(parent);
695
696 /* now we can insert even the list into its parent HT */
697 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100698 }
Michal Vasko90932a92020-02-12 14:33:03 +0100699}
700
Michal Vasko717a4c32020-12-07 09:40:10 +0100701/**
702 * @brief Check schema place of a node to be inserted.
703 *
704 * @param[in] parent Schema node of the parent data node.
705 * @param[in] sibling Schema node of a sibling data node.
706 * @param[in] schema Schema node if the data node to be inserted.
707 * @return LY_SUCCESS on success.
708 * @return LY_EINVAL if the place is invalid.
709 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100710static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100711lyd_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 +0100712{
713 const struct lysc_node *par2;
714
Michal Vasko62ed12d2020-05-21 10:08:25 +0200715 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100716 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
717 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100718
Michal Vasko717a4c32020-12-07 09:40:10 +0100719 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100720 /* opaque nodes can be inserted wherever */
721 return LY_SUCCESS;
722 }
723
Michal Vasko717a4c32020-12-07 09:40:10 +0100724 if (!parent) {
725 parent = lysc_data_parent(sibling);
726 }
727
Michal Vaskof03ed032020-03-04 13:31:44 +0100728 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200729 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100730
731 if (parent) {
732 /* inner node */
733 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200734 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200735 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100736 return LY_EINVAL;
737 }
738 } else {
739 /* top-level node */
740 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200741 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100742 return LY_EINVAL;
743 }
744 }
745
746 return LY_SUCCESS;
747}
748
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100749LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200750lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100751{
752 struct lyd_node *iter;
753
Michal Vasko0ab974d2021-02-24 13:18:26 +0100754 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100755 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100756
Michal Vasko717a4c32020-12-07 09:40:10 +0100757 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100758
Michal Vasko0ab974d2021-02-24 13:18:26 +0100759 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100760 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100761 return LY_EINVAL;
762 }
763
764 if (node->parent || node->prev->next) {
765 lyd_unlink_tree(node);
766 }
767
768 while (node) {
769 iter = node->next;
770 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200771 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100772 node = iter;
773 }
774 return LY_SUCCESS;
775}
776
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100777LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200778lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100779{
780 struct lyd_node *iter;
781
782 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
783 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
784
785 if (first->schema && (first->schema->flags & LYS_KEY)) {
786 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
787 return LY_EINVAL;
788 }
789
790 while (first) {
791 iter = first->next;
792 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100793 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100794 first = iter;
795 }
796 return LY_SUCCESS;
797}
798
799LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200800lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100801{
802 struct lyd_node *iter;
803
Michal Vaskob104f112020-07-17 09:54:54 +0200804 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100805
Michal Vaskob104f112020-07-17 09:54:54 +0200806 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100807 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100808 }
809
Michal Vasko553d0b52020-12-04 16:27:52 +0100810 if (sibling == node) {
811 /* we need to keep the connection to siblings so we can insert into them */
812 sibling = sibling->prev;
813 }
814
Michal Vaskob1b5c262020-03-05 14:29:47 +0100815 if (node->parent || node->prev->next) {
816 lyd_unlink_tree(node);
817 }
818
819 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100820 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200821 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200822 return LY_EINVAL;
823 }
824
Michal Vaskob1b5c262020-03-05 14:29:47 +0100825 iter = node->next;
826 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200827 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100828 node = iter;
829 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100830
Michal Vaskob104f112020-07-17 09:54:54 +0200831 if (first) {
832 /* find the first sibling */
833 *first = sibling;
834 while ((*first)->prev->next) {
835 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100836 }
837 }
838
839 return LY_SUCCESS;
840}
841
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100842LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100843lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
844{
Michal Vaskobce230b2022-04-19 09:55:31 +0200845 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100846 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100847
Michal Vasko717a4c32020-12-07 09:40:10 +0100848 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100849
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200850 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200851 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100852 return LY_EINVAL;
853 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200854 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
855 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100856 return LY_EINVAL;
857 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100858
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100859 lyd_unlink_tree(node);
860 lyd_insert_before_node(sibling, node);
861 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100862
Michal Vaskof03ed032020-03-04 13:31:44 +0100863 return LY_SUCCESS;
864}
865
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100866LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100867lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
868{
Michal Vaskobce230b2022-04-19 09:55:31 +0200869 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100870 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100871
Michal Vasko717a4c32020-12-07 09:40:10 +0100872 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100873
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200874 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200875 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100876 return LY_EINVAL;
877 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200878 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
879 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100880 return LY_EINVAL;
881 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100882
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100883 lyd_unlink_tree(node);
884 lyd_insert_after_node(sibling, node);
885 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100886
Michal Vaskof03ed032020-03-04 13:31:44 +0100887 return LY_SUCCESS;
888}
889
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100890LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200891lyd_unlink_siblings(struct lyd_node *node)
892{
893 struct lyd_node *next, *elem, *first = NULL;
894
895 LY_LIST_FOR_SAFE(node, next, elem) {
896 lyd_unlink_tree(elem);
897 lyd_insert_node(NULL, &first, elem, 1);
898 }
899}
900
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100901LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100902lyd_unlink_tree(struct lyd_node *node)
903{
904 struct lyd_node *iter;
905
906 if (!node) {
907 return;
908 }
909
Michal Vaskob104f112020-07-17 09:54:54 +0200910 /* update hashes while still linked into the tree */
911 lyd_unlink_hash(node);
912
Michal Vaskof03ed032020-03-04 13:31:44 +0100913 /* unlink from siblings */
914 if (node->prev->next) {
915 node->prev->next = node->next;
916 }
917 if (node->next) {
918 node->next->prev = node->prev;
919 } else {
920 /* unlinking the last node */
921 if (node->parent) {
922 iter = node->parent->child;
923 } else {
924 iter = node->prev;
925 while (iter->prev != node) {
926 iter = iter->prev;
927 }
928 }
929 /* update the "last" pointer from the first node */
930 iter->prev = node->prev;
931 }
932
933 /* unlink from parent */
934 if (node->parent) {
935 if (node->parent->child == node) {
936 /* the node is the first child */
937 node->parent->child = node->next;
938 }
939
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200940 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100941 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200942
Michal Vaskof03ed032020-03-04 13:31:44 +0100943 node->parent = NULL;
944 }
945
946 node->next = NULL;
947 node->prev = node;
948}
949
Michal Vaskoa5da3292020-08-12 13:10:50 +0200950void
Michal Vasko871a0252020-11-11 18:35:24 +0100951lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200952{
953 struct lyd_meta *last, *iter;
954
955 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200956
957 if (!meta) {
958 return;
959 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200960
961 for (iter = meta; iter; iter = iter->next) {
962 iter->parent = parent;
963 }
964
965 /* insert as the last attribute */
966 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200967 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200968 last->next = meta;
969 } else {
970 parent->meta = meta;
971 }
972
973 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100974 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200975 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100976 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200977 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200978}
979
980LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100981lyd_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 +0200982 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 +0100983 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 +0100984{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100985 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100986 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200987 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100988 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200989 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100990
Michal Vasko9f96a052020-03-10 09:41:45 +0100991 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100992
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200993 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200994 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200995 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100996 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200997 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100998 break;
999 }
1000 }
1001 if (!ant) {
1002 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001003 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001004 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001005 ret = LY_EINVAL;
1006 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001007 }
1008
Michal Vasko9f96a052020-03-10 09:41:45 +01001009 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001010 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001011 mt->parent = parent;
1012 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001013 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001014 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 +01001015 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001016 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1017 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1018 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001019
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001020 /* insert as the last attribute */
1021 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001022 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001023 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001024 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001025 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001026 }
1027
Michal Vasko9f96a052020-03-10 09:41:45 +01001028 if (meta) {
1029 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001030 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001031
1032cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001033 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001034}
1035
Michal Vaskoa5da3292020-08-12 13:10:50 +02001036void
1037lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1038{
1039 struct lyd_attr *last, *iter;
1040 struct lyd_node_opaq *opaq;
1041
1042 assert(parent && !parent->schema);
1043
1044 if (!attr) {
1045 return;
1046 }
1047
1048 opaq = (struct lyd_node_opaq *)parent;
1049 for (iter = attr; iter; iter = iter->next) {
1050 iter->parent = opaq;
1051 }
1052
1053 /* insert as the last attribute */
1054 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001055 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001056 last->next = attr;
1057 } else {
1058 opaq->attr = attr;
1059 }
1060}
1061
Michal Vasko52927e22020-03-16 17:26:14 +01001062LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001063lyd_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 +01001064 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 +02001065 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 +01001066{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001067 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001068 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001069
1070 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001071 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001072
Michal Vasko2a3722d2021-06-16 11:52:39 +02001073 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001074 value = "";
1075 }
1076
1077 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001078 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001079
Michal Vaskoad92b672020-11-12 13:11:31 +01001080 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001081 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001082 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001083 }
1084 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001085 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001086 }
1087
Michal Vasko52927e22020-03-16 17:26:14 +01001088 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001089 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1090 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001091 *dynamic = 0;
1092 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001093 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001094 }
Michal Vasko501af032020-11-11 20:27:44 +01001095 at->format = format;
1096 at->val_prefix_data = val_prefix_data;
1097 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001098
1099 /* insert as the last attribute */
1100 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001101 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001102 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001103 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001104 last->next = at;
1105 }
1106
Radek Krejci011e4aa2020-09-04 15:22:31 +02001107finish:
1108 if (ret) {
1109 lyd_free_attr_single(ctx, at);
1110 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001111 *attr = at;
1112 }
1113 return LY_SUCCESS;
1114}
1115
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001116LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001117lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001118{
Michal Vasko90189962023-02-28 12:10:34 +01001119 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001120
Michal Vasko90189962023-02-28 12:10:34 +01001121 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001122
Michal Vasko004d3152020-06-11 19:59:22 +02001123 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001124}
1125
aPiecek2f63f952021-03-30 12:22:18 +02001126/**
1127 * @brief Check the equality of the two schemas from different contexts.
1128 *
1129 * @param schema1 of first node.
1130 * @param schema2 of second node.
1131 * @return 1 if the schemas are equal otherwise 0.
1132 */
1133static ly_bool
1134lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1135{
1136 if (!schema1 && !schema2) {
1137 return 1;
1138 } else if (!schema1 || !schema2) {
1139 return 0;
1140 }
1141
1142 assert(schema1->module->ctx != schema2->module->ctx);
1143
1144 if (schema1->nodetype != schema2->nodetype) {
1145 return 0;
1146 }
1147
1148 if (strcmp(schema1->name, schema2->name)) {
1149 return 0;
1150 }
1151
1152 if (strcmp(schema1->module->name, schema2->module->name)) {
1153 return 0;
1154 }
1155
aPiecek2f63f952021-03-30 12:22:18 +02001156 return 1;
1157}
1158
1159/**
1160 * @brief Check the equality of the schemas for all parent nodes.
1161 *
1162 * Both nodes must be from different contexts.
1163 *
1164 * @param node1 Data of first node.
1165 * @param node2 Data of second node.
1166 * @return 1 if the all related parental schemas are equal otherwise 0.
1167 */
1168static ly_bool
1169lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1170{
1171 const struct lysc_node *parent1, *parent2;
1172
1173 assert(node1 && node2);
1174
1175 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1176 parent1 && parent2;
1177 parent1 = parent1->parent, parent2 = parent2->parent) {
1178 if (!lyd_compare_schema_equal(parent1, parent2)) {
1179 return 0;
1180 }
1181 }
1182
1183 if (parent1 || parent2) {
1184 return 0;
1185 }
1186
1187 return 1;
1188}
1189
1190/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001191 * @brief Compare 2 nodes values including opaque node values.
1192 *
1193 * @param[in] node1 First node to compare.
1194 * @param[in] node2 Second node to compare.
1195 * @return LY_SUCCESS if equal.
1196 * @return LY_ENOT if not equal.
1197 * @return LY_ERR on error.
1198 */
1199static LY_ERR
1200lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1201{
1202 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1203 const char *val1, *val2, *col;
1204 const struct lys_module *mod;
1205 char *val_dyn = NULL;
1206 LY_ERR rc = LY_SUCCESS;
1207
1208 if (!node1->schema) {
1209 opaq1 = (struct lyd_node_opaq *)node1;
1210 }
1211 if (!node2->schema) {
1212 opaq2 = (struct lyd_node_opaq *)node2;
1213 }
1214
1215 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1216 /* opaque XML and opaque XML node */
1217 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1218 opaq2->val_prefix_data)) {
1219 return LY_ENOT;
1220 }
1221 return LY_SUCCESS;
1222 }
1223
1224 /* get their values */
1225 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1226 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1227 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1228 if (!mod) {
1229 /* unable to compare */
1230 return LY_ENOT;
1231 }
1232
1233 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1234 LOGMEM(LYD_CTX(node1));
1235 return LY_EMEM;
1236 }
1237 val1 = val_dyn;
1238 } else {
1239 val1 = lyd_get_value(node1);
1240 }
1241 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1242 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1243 if (!mod) {
1244 return LY_ENOT;
1245 }
1246
1247 assert(!val_dyn);
1248 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1249 LOGMEM(LYD_CTX(node2));
1250 return LY_EMEM;
1251 }
1252 val2 = val_dyn;
1253 } else {
1254 val2 = lyd_get_value(node2);
1255 }
1256
1257 /* compare values */
1258 if (strcmp(val1, val2)) {
1259 rc = LY_ENOT;
1260 }
1261
1262 free(val_dyn);
1263 return rc;
1264}
1265
1266/**
aPiecek2f63f952021-03-30 12:22:18 +02001267 * @brief Internal implementation of @ref lyd_compare_single.
1268 * @copydoc lyd_compare_single
1269 * @param[in] parental_schemas_checked Flag used for optimization.
1270 * When this function is called for the first time, the flag must be set to 0.
1271 * The @ref lyd_compare_schema_parents_equal should be called only once during
1272 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1273 */
1274static LY_ERR
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001275lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1276 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001277{
1278 const struct lyd_node *iter1, *iter2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001279 struct lyd_node_any *any1, *any2;
Michal Vaskof3c80a72022-08-17 10:22:04 +02001280 int len1, len2;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001281 LY_ERR r;
Radek Krejci084289f2019-07-09 17:35:30 +02001282
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001283 if (!node1 || !node2) {
1284 if (node1 == node2) {
1285 return LY_SUCCESS;
1286 } else {
1287 return LY_ENOT;
1288 }
1289 }
1290
aPiecek2f63f952021-03-30 12:22:18 +02001291 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1292 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001293 if (options & LYD_COMPARE_OPAQ) {
1294 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1295 return LY_ENOT;
1296 }
1297 } else {
1298 if (node1->schema != node2->schema) {
1299 return LY_ENOT;
1300 }
aPiecek2f63f952021-03-30 12:22:18 +02001301 }
1302 } else {
1303 /* different contexts */
1304 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1305 return LY_ENOT;
1306 }
1307 if (!parental_schemas_checked) {
1308 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1309 return LY_ENOT;
1310 }
1311 parental_schemas_checked = 1;
1312 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001313 }
1314
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001315 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001316 return LY_ENOT;
1317 }
aPiecek2f63f952021-03-30 12:22:18 +02001318 /* 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 +02001319
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001320 if (!node1->schema || !node2->schema) {
1321 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001322 return LY_ENOT;
1323 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001324 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1325 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1326 /* compare values only if there are any to compare */
1327 if ((r = lyd_compare_single_value(node1, node2))) {
1328 return r;
1329 }
Michal Vasko52927e22020-03-16 17:26:14 +01001330 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001331
Michal Vasko52927e22020-03-16 17:26:14 +01001332 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001333 iter1 = lyd_child(node1);
1334 iter2 = lyd_child(node2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001335 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001336 }
1337 return LY_SUCCESS;
1338 } else {
1339 switch (node1->schema->nodetype) {
1340 case LYS_LEAF:
1341 case LYS_LEAFLIST:
1342 if (options & LYD_COMPARE_DEFAULTS) {
1343 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1344 return LY_ENOT;
1345 }
1346 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001347 if ((r = lyd_compare_single_value(node1, node2))) {
1348 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001349 }
1350
aPiecek2f63f952021-03-30 12:22:18 +02001351 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001352 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001353 case LYS_RPC:
1354 case LYS_ACTION:
1355 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001356 if (options & LYD_COMPARE_DEFAULTS) {
1357 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1358 return LY_ENOT;
1359 }
1360 }
1361 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001362 iter1 = lyd_child(node1);
1363 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001364 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001365 }
1366 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001367 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001368 iter1 = lyd_child(node1);
1369 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001370
1371 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1372 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001373 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001374 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001375 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001376 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001377 return LY_ENOT;
1378 }
1379 iter1 = iter1->next;
1380 iter2 = iter2->next;
1381 }
1382 } else {
1383 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1384
Radek Krejci0f969882020-08-21 16:56:47 +02001385all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001386 if (!iter1 && !iter2) {
1387 /* no children, nothing to compare */
1388 return LY_SUCCESS;
1389 }
1390
Michal Vaskod989ba02020-08-24 10:59:24 +02001391 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001392 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001393 return LY_ENOT;
1394 }
1395 }
1396 if (iter1 || iter2) {
1397 return LY_ENOT;
1398 }
1399 }
1400 return LY_SUCCESS;
1401 case LYS_ANYXML:
1402 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001403 any1 = (struct lyd_node_any *)node1;
1404 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001405
1406 if (any1->value_type != any2->value_type) {
1407 return LY_ENOT;
1408 }
1409 switch (any1->value_type) {
1410 case LYD_ANYDATA_DATATREE:
1411 iter1 = any1->value.tree;
1412 iter2 = any2->value.tree;
1413 goto all_children_compare;
1414 case LYD_ANYDATA_STRING:
1415 case LYD_ANYDATA_XML:
1416 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001417 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1418 return LY_ENOT;
1419 } else if (!any1->value.str && !any2->value.str) {
1420 return LY_SUCCESS;
1421 }
Michal Vasko52927e22020-03-16 17:26:14 +01001422 len1 = strlen(any1->value.str);
1423 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001424 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001425 return LY_ENOT;
1426 }
1427 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001428 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001429 len1 = lyd_lyb_data_length(any1->value.mem);
1430 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001431 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001432 return LY_ENOT;
1433 }
1434 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001435 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001436 }
1437 }
1438
Michal Vaskob7be7a82020-08-20 09:09:04 +02001439 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001440 return LY_EINT;
1441}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001442
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001443LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001444lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1445{
1446 return lyd_compare_single_(node1, node2, options, 0);
1447}
1448
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001449LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001450lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001451{
Michal Vaskod989ba02020-08-24 10:59:24 +02001452 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001453 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1454 }
1455
Michal Vasko11a81432020-07-28 16:31:29 +02001456 if (node1 == node2) {
1457 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001458 }
Michal Vasko11a81432020-07-28 16:31:29 +02001459 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001460}
1461
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001462LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001463lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1464{
1465 if (!meta1 || !meta2) {
1466 if (meta1 == meta2) {
1467 return LY_SUCCESS;
1468 } else {
1469 return LY_ENOT;
1470 }
1471 }
1472
Michal Vaskoa8083062020-11-06 17:22:10 +01001473 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001474 return LY_ENOT;
1475 }
1476
Michal Vasko21725742020-06-29 11:49:25 +02001477 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1478}
1479
Radek Krejci22ebdba2019-07-25 13:59:43 +02001480/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001481 * @brief Create a copy of the attribute.
1482 *
1483 * @param[in] attr Attribute to copy.
1484 * @param[in] node Opaque where to append the new attribute.
1485 * @param[out] dup Optional created attribute copy.
1486 * @return LY_ERR value.
1487 */
1488static LY_ERR
1489lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1490{
1491 LY_ERR ret = LY_SUCCESS;
1492 struct lyd_attr *a, *last;
1493 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1494
1495 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1496
1497 /* create a copy */
1498 a = calloc(1, sizeof *attr);
1499 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1500
1501 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1502 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1503 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1504 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1505 a->hints = attr->hints;
1506 a->format = attr->format;
1507 if (attr->val_prefix_data) {
1508 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1509 LY_CHECK_GOTO(ret, finish);
1510 }
1511
1512 /* insert as the last attribute */
1513 a->parent = opaq;
1514 if (opaq->attr) {
1515 for (last = opaq->attr; last->next; last = last->next) {}
1516 last->next = a;
1517 } else {
1518 opaq->attr = a;
1519 }
1520
1521finish:
1522 if (ret) {
1523 lyd_free_attr_single(LYD_CTX(node), a);
1524 } else if (dup) {
1525 *dup = a;
1526 }
1527 return LY_SUCCESS;
1528}
1529
1530/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001531 * @brief Find @p schema equivalent in @p trg_ctx.
1532 *
1533 * @param[in] schema Schema node to find.
1534 * @param[in] trg_ctx Target context to search in.
1535 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001536 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001537 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1538 * @return LY_RRR value.
1539 */
1540static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001541lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1542 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001543{
Michal Vasko9beceb82022-04-05 12:14:15 +02001544 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1545 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001546 char *path;
1547
1548 if (!schema) {
1549 /* opaque node */
1550 *trg_schema = NULL;
1551 return LY_SUCCESS;
1552 }
1553
Michal Vasko9beceb82022-04-05 12:14:15 +02001554 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001555 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001556 trg_parent = parent->schema;
1557 src_parent = lysc_data_parent(schema);
1558 }
1559
1560 do {
1561 /* find the next parent */
1562 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001563 while (lysc_data_parent(sp) != src_parent) {
1564 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001565 }
1566 src_parent = sp;
1567
1568 if (!src_parent->parent) {
1569 /* find the module first */
1570 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1571 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001572 if (log) {
1573 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1574 src_parent->module->name);
1575 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001576 return LY_ENOTFOUND;
1577 }
1578 }
1579
1580 /* find the next parent */
1581 assert(trg_parent || trg_mod);
1582 tp = NULL;
1583 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1584 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1585 break;
1586 }
1587 }
1588 if (!tp) {
1589 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001590 if (log) {
1591 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1592 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1593 free(path);
1594 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001595 return LY_ENOTFOUND;
1596 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001597
Michal Vasko9beceb82022-04-05 12:14:15 +02001598 trg_parent = tp;
1599 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001600
Michal Vasko9beceb82022-04-05 12:14:15 +02001601 /* success */
1602 *trg_schema = trg_parent;
1603 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001604}
1605
1606/**
Michal Vasko52927e22020-03-16 17:26:14 +01001607 * @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 +02001608 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001609 * 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 +02001610 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001611 * @param[in] node Node to duplicate.
1612 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001613 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001614 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1615 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001616 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1617 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001618 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1619 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001620 */
Michal Vasko52927e22020-03-16 17:26:14 +01001621static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001622lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1623 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001624{
Michal Vasko52927e22020-03-16 17:26:14 +01001625 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001626 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001627 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001628 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001629 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001630 const struct lysc_type *type;
1631 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001632
Michal Vasko52927e22020-03-16 17:26:14 +01001633 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001634
Michal Vasko19175b62022-04-01 09:17:07 +02001635 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001636 if (options & LYD_DUP_NO_EXT) {
1637 /* no not duplicate this subtree */
1638 return LY_SUCCESS;
1639 }
1640
Michal Vasko19175b62022-04-01 09:17:07 +02001641 /* we need to use the same context */
1642 trg_ctx = LYD_CTX(node);
1643 }
1644
Michal Vasko52927e22020-03-16 17:26:14 +01001645 if (!node->schema) {
1646 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001647 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001648 } else {
1649 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001650 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001651 case LYS_ACTION:
1652 case LYS_NOTIF:
1653 case LYS_CONTAINER:
1654 case LYS_LIST:
1655 dup = calloc(1, sizeof(struct lyd_node_inner));
1656 break;
1657 case LYS_LEAF:
1658 case LYS_LEAFLIST:
1659 dup = calloc(1, sizeof(struct lyd_node_term));
1660 break;
1661 case LYS_ANYDATA:
1662 case LYS_ANYXML:
1663 dup = calloc(1, sizeof(struct lyd_node_any));
1664 break;
1665 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001666 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001667 ret = LY_EINT;
1668 goto error;
1669 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001670 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001671 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001672
Michal Vaskof6df0a02020-06-16 13:08:34 +02001673 if (options & LYD_DUP_WITH_FLAGS) {
1674 dup->flags = node->flags;
1675 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001676 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001677 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001678 if (trg_ctx == LYD_CTX(node)) {
1679 dup->schema = node->schema;
1680 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001681 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001682 if (ret) {
1683 /* has no schema but is not an opaque node */
1684 free(dup);
1685 dup = NULL;
1686 goto error;
1687 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001688 }
Michal Vasko52927e22020-03-16 17:26:14 +01001689 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001690
Michal Vasko9cf62422021-07-01 13:29:32 +02001691 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001692 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001693 if (!node->schema) {
1694 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1695 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1696 }
1697 } else {
1698 LY_LIST_FOR(node->meta, meta) {
1699 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1700 }
Michal Vasko25a32822020-07-09 15:48:22 +02001701 }
1702 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001703
1704 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001705 if (!dup->schema) {
1706 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1707 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1708 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001709
1710 if (options & LYD_DUP_RECURSIVE) {
1711 /* duplicate all the children */
1712 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001713 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001714 }
1715 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001716 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1717 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1718 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1719 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001720 opaq->hints = orig->hints;
1721 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001722 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001723 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001724 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001725 }
Michal Vasko52927e22020-03-16 17:26:14 +01001726 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1727 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1728 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1729
1730 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001731 if (trg_ctx == LYD_CTX(node)) {
1732 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1733 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1734 } else {
1735 /* store canonical value in the target context */
1736 val_can = lyd_get_value(node);
1737 type = ((struct lysc_node_leaf *)term->schema)->type;
1738 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1739 LYD_HINT_DATA, term->schema, NULL);
1740 LY_CHECK_GOTO(ret, error);
1741 }
Michal Vasko52927e22020-03-16 17:26:14 +01001742 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1743 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1744 struct lyd_node *child;
1745
1746 if (options & LYD_DUP_RECURSIVE) {
1747 /* duplicate all the children */
1748 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001749 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001750 }
Michal Vasko69730152020-10-09 16:30:07 +02001751 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001752 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001753 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001754 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001755 }
1756 }
1757 lyd_hash(dup);
1758 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001759 dup->hash = node->hash;
1760 any = (struct lyd_node_any *)node;
1761 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001762 }
1763
Michal Vasko52927e22020-03-16 17:26:14 +01001764 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001765 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001766
1767 if (dup_p) {
1768 *dup_p = dup;
1769 }
1770 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001771
1772error:
Michal Vasko52927e22020-03-16 17:26:14 +01001773 lyd_free_tree(dup);
1774 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001775}
1776
Michal Vasko29d674b2021-08-25 11:18:35 +02001777/**
1778 * @brief Get a parent node to connect duplicated subtree to.
1779 *
1780 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001781 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001782 * @param[in] parent Initial parent to connect to.
1783 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1784 * @param[out] dup_parent First duplicated parent node, if any.
1785 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1786 * @return LY_ERR value.
1787 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001788static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001789lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1790 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001791{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001792 const struct lyd_node_inner *orig_parent, *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001793 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001794
1795 *dup_parent = NULL;
1796 *local_parent = NULL;
1797
Michal Vasko83522192022-07-20 08:07:34 +02001798 if (node->flags & LYD_EXT) {
1799 ext_parent = 1;
1800 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001801 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
Michal Vasko83522192022-07-20 08:07:34 +02001802 if (ext_parent) {
1803 /* use the standard context */
1804 trg_ctx = LYD_CTX(orig_parent);
1805 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001806 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001807 /* stop creating parents, connect what we have into the provided parent */
1808 iter = parent;
1809 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001810 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1811 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
1812 lyd_compare_schema_parents_equal(&parent->node, &orig_parent->node)) {
1813 iter = parent;
1814 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001815 } else {
1816 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001817 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 +02001818 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001819 }
1820 if (!*local_parent) {
1821 *local_parent = (struct lyd_node_inner *)iter;
1822 }
1823 if (iter->child) {
1824 /* 1) list - add after keys
1825 * 2) provided parent with some children */
1826 iter->child->prev->next = *dup_parent;
1827 if (*dup_parent) {
1828 (*dup_parent)->prev = iter->child->prev;
1829 iter->child->prev = *dup_parent;
1830 }
1831 } else {
1832 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1833 }
1834 if (*dup_parent) {
1835 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1836 }
1837 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001838 if (orig_parent->flags & LYD_EXT) {
1839 ext_parent = 1;
1840 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001841 }
1842
1843 if (repeat && parent) {
1844 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001845 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001846 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001847 return LY_EINVAL;
1848 }
1849
1850 return LY_SUCCESS;
1851}
1852
1853static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001854lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1855 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001856{
1857 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001858 const struct lyd_node *orig; /* original node to be duplicated */
1859 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001860 struct lyd_node *top = NULL; /* the most higher created node */
1861 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001862
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001863 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001864
1865 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001866 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 +02001867 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001868 } else {
1869 local_parent = parent;
1870 }
1871
Radek Krejci22ebdba2019-07-25 13:59:43 +02001872 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001873 if (lysc_is_key(orig->schema)) {
1874 if (local_parent) {
1875 /* the key must already exist in the parent */
1876 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001877 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001878 } else {
1879 assert(!(options & LYD_DUP_WITH_PARENTS));
1880 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001881 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001882 LY_CHECK_GOTO(rc, error);
1883 }
1884 } else {
1885 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001886 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1887 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001888 LY_CHECK_GOTO(rc, error);
1889 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001890 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001891 break;
1892 }
1893 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001894
Michal Vasko3a41dff2020-07-15 14:30:28 +02001895 if (dup) {
1896 *dup = first;
1897 }
1898 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001899
1900error:
1901 if (top) {
1902 lyd_free_tree(top);
1903 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001904 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001905 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001906 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001907}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001908
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001909/**
1910 * @brief Check the context of node and parent when duplicating nodes.
1911 *
1912 * @param[in] node Node to duplicate.
1913 * @param[in] parent Parent of the duplicated node(s).
1914 * @return LY_ERR value.
1915 */
1916static LY_ERR
1917lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1918{
1919 const struct lyd_node *iter;
1920
1921 if (!node || !parent) {
1922 return LY_SUCCESS;
1923 }
1924
1925 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1926 /* try to find top-level ext data parent */
1927 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1928
1929 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001930 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001931 return LY_EINVAL;
1932 }
1933 }
1934
1935 return LY_SUCCESS;
1936}
1937
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001938LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001939lyd_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 +02001940{
Michal Vaskoddd76592022-01-17 13:34:48 +01001941 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001942 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001943
1944 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1945}
1946
1947LIBYANG_API_DEF LY_ERR
1948lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1949 uint32_t options, struct lyd_node **dup)
1950{
1951 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1952
1953 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001954}
1955
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001956LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001957lyd_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 +02001958{
Michal Vaskoddd76592022-01-17 13:34:48 +01001959 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001960 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001961
1962 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1963}
1964
1965LIBYANG_API_DEF LY_ERR
1966lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1967 uint32_t options, struct lyd_node **dup)
1968{
1969 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1970
1971 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001972}
1973
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001974LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001975lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001976{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001977 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02001978 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02001979 struct lyd_meta *mt, *last;
1980
Michal Vasko3a41dff2020-07-15 14:30:28 +02001981 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001982
Michal Vasko33c48972022-07-20 10:28:07 +02001983 /* log to node context but value must always use the annotation context */
1984 ctx = meta->annotation->module->ctx;
1985
Michal Vasko25a32822020-07-09 15:48:22 +02001986 /* create a copy */
1987 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001988 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001989 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02001990 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001991 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02001992 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001993
1994 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001995 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001996 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001997 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001998 last->next = mt;
1999 } else {
2000 node->meta = mt;
2001 }
2002
Radek Krejci011e4aa2020-09-04 15:22:31 +02002003finish:
2004 if (ret) {
2005 lyd_free_meta_single(mt);
2006 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002007 *dup = mt;
2008 }
2009 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002010}
2011
Michal Vasko4490d312020-06-16 13:08:55 +02002012/**
2013 * @brief Merge a source sibling into target siblings.
2014 *
2015 * @param[in,out] first_trg First target sibling, is updated if top-level.
2016 * @param[in] parent_trg Target parent.
2017 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002018 * @param[in] merge_cb Optional merge callback.
2019 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002020 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002021 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002022 * @return LY_ERR value.
2023 */
2024static LY_ERR
2025lyd_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 +01002026 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct hash_table **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002027{
Michal Vasko4490d312020-06-16 13:08:55 +02002028 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002029 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002030 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002031 struct lysc_type *type;
Michal Vasko271d2e32023-01-31 15:43:19 +01002032 struct hash_table *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002033 LY_ERR ret;
2034 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002035
2036 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002037 if (!sibling_src->schema) {
2038 /* try to find the same opaque node */
2039 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2040 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002041 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002042 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002043 } else {
2044 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002045 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002046 }
2047
Michal Vaskocd3f6172021-05-18 16:14:50 +02002048 if (match_trg) {
2049 /* update match as needed */
2050 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2051 } else {
2052 /* first instance of this node */
2053 first_inst = 1;
2054 }
2055
2056 if (match_trg) {
2057 /* call callback */
2058 if (merge_cb) {
2059 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2060 }
2061
Michal Vasko4490d312020-06-16 13:08:55 +02002062 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002063 if (!match_trg->schema) {
2064 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2065 /* update value */
2066 opaq_trg = (struct lyd_node_opaq *)match_trg;
2067 opaq_src = (struct lyd_node_opaq *)sibling_src;
2068
2069 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2070 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2071 opaq_trg->hints = opaq_src->hints;
2072
2073 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2074 opaq_trg->format = opaq_src->format;
2075 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2076 &opaq_trg->val_prefix_data);
2077 }
2078 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2079 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002080 /* since they are different, they cannot both be default */
2081 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2082
Michal Vasko3a41dff2020-07-15 14:30:28 +02002083 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2084 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002085 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002086 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2087 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002088 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002089
2090 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002091 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002092 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002093 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002094 /* update value */
2095 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002096 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002097
2098 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002099 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002100 }
2101
2102 /* check descendants, recursively */
2103 ret = LY_SUCCESS;
2104 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2105 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2106 &child_dup_inst);
2107 if (ret) {
2108 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002109 }
2110 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002111 lyd_dup_inst_free(child_dup_inst);
2112 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002113 } else {
2114 /* node not found, merge it */
2115 if (options & LYD_MERGE_DESTRUCT) {
2116 dup_src = (struct lyd_node *)sibling_src;
2117 lyd_unlink_tree(dup_src);
2118 /* spend it */
2119 *sibling_src_p = NULL;
2120 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002121 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 +02002122 }
2123
Michal Vasko7e8315b2021-08-03 17:01:06 +02002124 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2125 /* set LYD_NEW for all the new nodes, required for validation */
2126 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2127 elem->flags |= LYD_NEW;
2128 LYD_TREE_DFS_END(dup_src, elem);
2129 }
Michal Vasko4490d312020-06-16 13:08:55 +02002130 }
2131
Michal Vaskocd3f6172021-05-18 16:14:50 +02002132 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002133 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002134
2135 if (first_inst) {
2136 /* remember not to find this instance next time */
2137 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2138 }
2139
2140 /* call callback, no source node */
2141 if (merge_cb) {
2142 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2143 }
Michal Vasko4490d312020-06-16 13:08:55 +02002144 }
2145
2146 return LY_SUCCESS;
2147}
2148
Michal Vasko3a41dff2020-07-15 14:30:28 +02002149static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002150lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2151 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002152{
2153 const struct lyd_node *sibling_src, *tmp;
Michal Vasko271d2e32023-01-31 15:43:19 +01002154 struct hash_table *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002155 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002156 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002157
2158 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002159 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2160 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002161
2162 if (!source) {
2163 /* nothing to merge */
2164 return LY_SUCCESS;
2165 }
2166
Michal Vasko831422b2020-11-24 11:20:51 +01002167 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002168 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002169 return LY_EINVAL;
2170 }
2171
2172 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002173 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2174 /* skip data nodes from different modules */
2175 continue;
2176 }
2177
Radek Krejci857189e2020-09-01 13:26:36 +02002178 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002179 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2180 if (ret) {
2181 break;
2182 }
Michal Vasko4490d312020-06-16 13:08:55 +02002183 if (first && !sibling_src) {
2184 /* source was spent (unlinked), move to the next node */
2185 source = tmp;
2186 }
2187
Michal Vasko3a41dff2020-07-15 14:30:28 +02002188 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002189 break;
2190 }
2191 }
2192
2193 if (options & LYD_MERGE_DESTRUCT) {
2194 /* free any leftover source data that were not merged */
2195 lyd_free_siblings((struct lyd_node *)source);
2196 }
2197
Michal Vaskocd3f6172021-05-18 16:14:50 +02002198 lyd_dup_inst_free(dup_inst);
2199 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002200}
2201
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002202LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002203lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002204{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002205 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002206}
2207
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002208LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002209lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002210{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002211 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2212}
2213
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002214LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002215lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2216 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2217{
2218 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002219}
2220
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002221static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002222lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002223{
Michal Vasko14654712020-02-06 08:35:21 +01002224 /* ending \0 */
2225 ++reqlen;
2226
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002227 if (reqlen > *buflen) {
2228 if (is_static) {
2229 return LY_EINCOMPLETE;
2230 }
2231
2232 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2233 if (!*buffer) {
2234 return LY_EMEM;
2235 }
2236
2237 *buflen = reqlen;
2238 }
2239
2240 return LY_SUCCESS;
2241}
2242
Michal Vaskod59035b2020-07-08 12:00:06 +02002243LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002244lyd_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 +02002245{
2246 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002247 size_t len;
2248 const char *val;
2249 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002250
Michal Vasko824d0832021-11-04 15:36:51 +01002251 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002252 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002253 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002254 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002255
2256 quot = '\'';
2257 if (strchr(val, '\'')) {
2258 quot = '"';
2259 }
2260 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002261 }
2262
2263 return LY_SUCCESS;
2264}
2265
2266/**
2267 * @brief Append leaf-list value predicate to path.
2268 *
2269 * @param[in] node Node to print.
2270 * @param[in,out] buffer Buffer to print to.
2271 * @param[in,out] buflen Current buffer length.
2272 * @param[in,out] bufused Current number of characters used in @p buffer.
2273 * @param[in] is_static Whether buffer is static or can be reallocated.
2274 * @return LY_ERR
2275 */
2276static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002277lyd_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 +02002278{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002279 size_t len;
2280 const char *val;
2281 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002282
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002283 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002284 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002285 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002286
2287 quot = '\'';
2288 if (strchr(val, '\'')) {
2289 quot = '"';
2290 }
2291 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2292
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002293 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002294}
2295
2296/**
2297 * @brief Append node position (relative to its other instances) predicate to path.
2298 *
2299 * @param[in] node Node to print.
2300 * @param[in,out] buffer Buffer to print to.
2301 * @param[in,out] buflen Current buffer length.
2302 * @param[in,out] bufused Current number of characters used in @p buffer.
2303 * @param[in] is_static Whether buffer is static or can be reallocated.
2304 * @return LY_ERR
2305 */
2306static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002307lyd_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 +02002308{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002309 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002310 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002311 char *val = NULL;
2312 LY_ERR rc;
2313
Michal Vasko50cc0562021-05-18 16:15:43 +02002314 pos = lyd_list_pos(node);
2315 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002316 return LY_EMEM;
2317 }
2318
2319 len = 1 + strlen(val) + 1;
2320 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2321 if (rc != LY_SUCCESS) {
2322 goto cleanup;
2323 }
2324
2325 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2326
2327cleanup:
2328 free(val);
2329 return rc;
2330}
2331
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002332LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002333lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2334{
Radek Krejci857189e2020-09-01 13:26:36 +02002335 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002336 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002337 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002338 const struct lyd_node *iter, *parent;
2339 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002340 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002341
2342 LY_CHECK_ARG_RET(NULL, node, NULL);
2343 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002344 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002345 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002346 } else {
2347 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002348 }
2349
2350 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002351 case LYD_PATH_STD:
2352 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002353 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002354 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002355 ++depth;
2356 }
2357
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002358 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002359 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002360 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002361 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002362iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002363 /* get the module */
2364 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2365 parent = lyd_parent(iter);
2366 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2367 if (prev_mod == mod) {
2368 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002369 }
2370
2371 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002372 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2373 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002374 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2375 if (rc != LY_SUCCESS) {
2376 break;
2377 }
2378
2379 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002380 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002381
Michal Vasko790b2bc2020-08-03 13:35:06 +02002382 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002383 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002384 switch (iter->schema->nodetype) {
2385 case LYS_LIST:
2386 if (iter->schema->flags & LYS_KEYLESS) {
2387 /* print its position */
2388 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2389 } else {
2390 /* print all list keys in predicates */
2391 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2392 }
2393 break;
2394 case LYS_LEAFLIST:
2395 if (iter->schema->flags & LYS_CONFIG_W) {
2396 /* print leaf-list value */
2397 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2398 } else {
2399 /* print its position */
2400 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2401 }
2402 break;
2403 default:
2404 /* nothing to print more */
2405 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002406 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002407 }
2408 if (rc != LY_SUCCESS) {
2409 break;
2410 }
2411
Michal Vasko14654712020-02-06 08:35:21 +01002412 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002413 }
2414 break;
2415 }
2416
2417 return buffer;
2418}
Michal Vaskoe444f752020-02-10 12:20:06 +01002419
Michal Vaskodbf3e652022-10-21 08:46:25 +02002420char *
2421lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2422{
2423 uint32_t depth;
2424 size_t bufused = 0, buflen = 0, len;
2425 char *buffer = NULL;
2426 const struct lyd_node *iter, *parent;
2427 const struct lys_module *mod, *prev_mod;
2428 LY_ERR rc = LY_SUCCESS;
2429
2430 switch (pathtype) {
2431 case LYD_PATH_STD:
2432 case LYD_PATH_STD_NO_LAST_PRED:
2433 for (depth = 1; depth <= dnodes->count; ++depth) {
2434 /* current node */
2435 iter = dnodes->dnodes[depth - 1];
2436 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2437
2438 /* parent */
2439 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002440 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2441 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002442
2443 /* get module to print, if any */
2444 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2445 if (prev_mod == mod) {
2446 mod = NULL;
2447 }
2448
2449 /* realloc string */
2450 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2451 strlen(((struct lyd_node_opaq *)iter)->name.name));
2452 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2453 break;
2454 }
2455
2456 /* print next node */
2457 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2458
2459 /* do not always print the last (first) predicate */
2460 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2461 switch (iter->schema->nodetype) {
2462 case LYS_LIST:
2463 if (iter->schema->flags & LYS_KEYLESS) {
2464 /* print its position */
2465 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2466 } else {
2467 /* print all list keys in predicates */
2468 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2469 }
2470 break;
2471 case LYS_LEAFLIST:
2472 if (iter->schema->flags & LYS_CONFIG_W) {
2473 /* print leaf-list value */
2474 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2475 } else {
2476 /* print its position */
2477 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2478 }
2479 break;
2480 default:
2481 /* nothing to print more */
2482 break;
2483 }
2484 }
2485 if (rc) {
2486 break;
2487 }
2488 }
2489 break;
2490 }
2491
2492 return buffer;
2493}
2494
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002495LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002496lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2497{
2498 struct lyd_meta *ret = NULL;
2499 const struct ly_ctx *ctx;
2500 const char *prefix, *tmp;
2501 char *str;
2502 size_t pref_len, name_len;
2503
2504 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002505 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002506
2507 if (!first) {
2508 return NULL;
2509 }
2510
2511 ctx = first->annotation->module->ctx;
2512
2513 /* parse the name */
2514 tmp = name;
2515 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2516 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2517 return NULL;
2518 }
2519
2520 /* find the module */
2521 if (prefix) {
2522 str = strndup(prefix, pref_len);
2523 module = ly_ctx_get_module_latest(ctx, str);
2524 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002525 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 +02002526 }
2527
2528 /* find the metadata */
2529 LY_LIST_FOR(first, first) {
2530 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2531 ret = (struct lyd_meta *)first;
2532 break;
2533 }
2534 }
2535
2536 return ret;
2537}
2538
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002539LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002540lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2541{
Michal Vasko9beceb82022-04-05 12:14:15 +02002542 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002543 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002544 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002545
Michal Vaskof03ed032020-03-04 13:31:44 +01002546 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002547 if (!siblings) {
2548 /* no data */
2549 if (match) {
2550 *match = NULL;
2551 }
2552 return LY_ENOTFOUND;
2553 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002554
Michal Vasko9beceb82022-04-05 12:14:15 +02002555 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2556 /* create a duplicate in this context */
2557 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2558 target = dup;
2559 }
2560
2561 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2562 /* schema mismatch */
2563 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002564 if (match) {
2565 *match = NULL;
2566 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002567 return LY_ENOTFOUND;
2568 }
2569
Michal Vaskoe78faec2021-04-08 17:24:43 +02002570 /* get first sibling */
2571 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002572
Michal Vasko9e685082021-01-29 14:49:09 +01002573 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002574 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002575 assert(target->hash);
2576
Michal Vaskoe78faec2021-04-08 17:24:43 +02002577 if (lysc_is_dup_inst_list(target->schema)) {
2578 /* we must search the instances from beginning to find the first matching one */
2579 found = 0;
2580 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2581 if (!lyd_compare_single(target, iter, 0)) {
2582 found = 1;
2583 break;
2584 }
2585 }
2586 if (found) {
2587 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002588 } else {
2589 siblings = NULL;
2590 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002591 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002592 /* find by hash */
2593 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2594 siblings = *match_p;
2595 } else {
2596 /* not found */
2597 siblings = NULL;
2598 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002599 }
2600 } else {
2601 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002602 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002603 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002604 break;
2605 }
2606 }
2607 }
2608
Michal Vasko9beceb82022-04-05 12:14:15 +02002609 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002610 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002611 if (match) {
2612 *match = NULL;
2613 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002614 return LY_ENOTFOUND;
2615 }
2616
Michal Vasko9b368d32020-02-14 13:53:31 +01002617 if (match) {
2618 *match = (struct lyd_node *)siblings;
2619 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002620 return LY_SUCCESS;
2621}
2622
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002623LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002624lyd_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 +02002625 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002626{
2627 LY_ERR rc;
2628 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002629 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002630
Michal Vasko4c583e82020-07-17 12:16:14 +02002631 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002632 if (!siblings) {
2633 /* no data */
2634 if (match) {
2635 *match = NULL;
2636 }
2637 return LY_ENOTFOUND;
2638 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002639
Michal Vasko9beceb82022-04-05 12:14:15 +02002640 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2641 /* parent of ext nodes is useless */
2642 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002643 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2644 /* no schema node in siblings so certainly no data node either */
2645 if (match) {
2646 *match = NULL;
2647 }
2648 return LY_ENOTFOUND;
2649 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002650 }
2651
2652 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2653 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002654 if (match) {
2655 *match = NULL;
2656 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002657 return LY_ENOTFOUND;
2658 }
2659
Michal Vaskof03ed032020-03-04 13:31:44 +01002660 if (key_or_value && !val_len) {
2661 val_len = strlen(key_or_value);
2662 }
2663
Michal Vaskob104f112020-07-17 09:54:54 +02002664 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2665 /* create a data node and find the instance */
2666 if (schema->nodetype == LYS_LEAFLIST) {
2667 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002668 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 +02002669 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002670 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002671 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002672 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002673 }
2674
2675 /* find it */
2676 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002677 } else {
2678 /* find the first schema node instance */
2679 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002680 }
2681
Michal Vaskoe444f752020-02-10 12:20:06 +01002682 lyd_free_tree(target);
2683 return rc;
2684}
Michal Vaskoccc02342020-05-21 10:09:21 +02002685
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002686LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002687lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2688{
2689 struct lyd_node **match_p, *first, *iter;
2690 struct lyd_node_inner *parent;
2691
Michal Vasko83ae7772022-06-08 10:01:55 +02002692 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002693 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002694
2695 LY_CHECK_RET(ly_set_new(set));
2696
2697 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2698 /* no data or schema mismatch */
2699 return LY_ENOTFOUND;
2700 }
2701
2702 /* get first sibling */
2703 siblings = lyd_first_sibling(siblings);
2704
2705 parent = siblings->parent;
2706 if (parent && parent->schema && parent->children_ht) {
2707 assert(target->hash);
2708
2709 /* find the first instance */
2710 lyd_find_sibling_first(siblings, target, &first);
2711 if (first) {
2712 /* add it so that it is the first in the set */
2713 if (ly_set_add(*set, first, 1, NULL)) {
2714 goto error;
2715 }
2716
2717 /* find by hash */
2718 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2719 iter = *match_p;
2720 } else {
2721 /* not found */
2722 iter = NULL;
2723 }
2724 while (iter) {
2725 /* add all found nodes into the set */
2726 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2727 goto error;
2728 }
2729
2730 /* find next instance */
2731 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2732 iter = NULL;
2733 } else {
2734 iter = *match_p;
2735 }
2736 }
2737 }
2738 } else {
2739 /* no children hash table */
2740 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002741 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002742 ly_set_add(*set, (void *)siblings, 1, NULL);
2743 }
2744 }
2745 }
2746
2747 if (!(*set)->count) {
2748 return LY_ENOTFOUND;
2749 }
2750 return LY_SUCCESS;
2751
2752error:
2753 ly_set_free(*set, NULL);
2754 *set = NULL;
2755 return LY_EMEM;
2756}
2757
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002758LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002759lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2760{
2761 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2762
2763 for ( ; first; first = first->next) {
2764 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2765 break;
2766 }
2767 }
2768
2769 if (match) {
2770 *match = (struct lyd_node *)first;
2771 }
2772 return first ? LY_SUCCESS : LY_ENOTFOUND;
2773}
2774
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002775LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002776lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002777{
Michal Vaskod96e2372023-02-24 16:07:51 +01002778 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002779
Michal Vaskod96e2372023-02-24 16:07:51 +01002780 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2781}
Michal Vaskoccc02342020-05-21 10:09:21 +02002782
Michal Vaskod96e2372023-02-24 16:07:51 +01002783LIBYANG_API_DEF LY_ERR
2784lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2785{
2786 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002787
Michal Vaskod96e2372023-02-24 16:07:51 +01002788 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002789}
Radek Krejcica989142020-11-05 11:32:22 +01002790
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002791LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002792lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2793 const struct lyxp_var *vars, struct ly_set **set)
2794{
2795 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2796
2797 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2798}
2799
2800LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002801lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2802 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002803{
Michal Vaskod96e2372023-02-24 16:07:51 +01002804 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002805
Michal Vaskod96e2372023-02-24 16:07:51 +01002806 *set = NULL;
2807
2808 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 +01002809}
2810
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002811LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002812lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002813{
Michal Vaskod96e2372023-02-24 16:07:51 +01002814 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002815}
2816
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002817LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002818lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2819{
2820 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2821}
2822
2823LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002824lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2825 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002826{
Michal Vaskod96e2372023-02-24 16:07:51 +01002827 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2828}
2829
2830LIBYANG_API_DEF LY_ERR
2831lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2832 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2833 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2834{
2835 LY_ERR ret = LY_SUCCESS;
2836 struct lyxp_set xp_set = {0};
2837 struct lyxp_expr *exp = NULL;
2838 uint32_t i;
2839
2840 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2841 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2842 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2843
2844 /* parse expression */
2845 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2846 LY_CHECK_GOTO(ret, cleanup);
2847
2848 /* evaluate expression */
2849 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2850 LYXP_IGNORE_WHEN);
2851 LY_CHECK_GOTO(ret, cleanup);
2852
2853 /* return expected result type without or with casting */
2854 if (node_set) {
2855 /* node set */
2856 if (xp_set.type == LYXP_SET_NODE_SET) {
2857 /* transform into a set */
2858 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2859 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2860 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2861 (*node_set)->size = xp_set.used;
2862 for (i = 0; i < xp_set.used; ++i) {
2863 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2864 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2865 LY_CHECK_GOTO(ret, cleanup);
2866 }
2867 }
2868 if (ret_type) {
2869 *ret_type = LY_XPATH_NODE_SET;
2870 }
2871 } else if (!string && !number && !boolean) {
2872 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2873 ret = LY_EINVAL;
2874 goto cleanup;
2875 }
2876 }
2877
2878 if (string) {
2879 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2880 /* cast into string */
2881 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2882 }
2883 if (xp_set.type == LYXP_SET_STRING) {
2884 /* string */
2885 *string = xp_set.val.str;
2886 xp_set.val.str = NULL;
2887 if (ret_type) {
2888 *ret_type = LY_XPATH_STRING;
2889 }
2890 }
2891 }
2892
2893 if (number) {
2894 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2895 /* cast into number */
2896 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2897 }
2898 if (xp_set.type == LYXP_SET_NUMBER) {
2899 /* number */
2900 *number = xp_set.val.num;
2901 if (ret_type) {
2902 *ret_type = LY_XPATH_NUMBER;
2903 }
2904 }
2905 }
2906
2907 if (boolean) {
2908 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2909 /* cast into boolean */
2910 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2911 }
2912 if (xp_set.type == LYXP_SET_BOOLEAN) {
2913 /* boolean */
2914 *boolean = xp_set.val.bln;
2915 if (ret_type) {
2916 *ret_type = LY_XPATH_BOOLEAN;
2917 }
2918 }
2919 }
2920
2921cleanup:
2922 lyxp_set_free_content(&xp_set);
2923 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
2924 return ret;
aPiecekfba75362021-10-07 12:39:48 +02002925}
2926
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002927LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002928lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2929{
2930 LY_ERR ret = LY_SUCCESS;
2931 struct lyxp_expr *expr = NULL;
2932 struct ly_path *lypath = NULL;
2933
2934 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2935
2936 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002937 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 +01002938 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002939 LY_CHECK_GOTO(ret, cleanup);
2940
2941 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002942 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002943 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 +01002944 LY_CHECK_GOTO(ret, cleanup);
2945
2946 /* evaluate the path */
Michal Vasko90189962023-02-28 12:10:34 +01002947 ret = ly_path_eval_partial(lypath, ctx_node, NULL, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01002948
2949cleanup:
2950 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2951 ly_path_free(LYD_CTX(ctx_node), lypath);
2952 return ret;
2953}
2954
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002955LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002956lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2957{
2958 LY_ERR ret;
2959 struct lyd_node *m;
2960
2961 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2962
Michal Vasko90189962023-02-28 12:10:34 +01002963 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02002964 if (ret) {
2965 if (match) {
2966 *match = NULL;
2967 }
2968 return LY_ENOTFOUND;
2969 }
2970
2971 if (match) {
2972 *match = m;
2973 }
2974 return LY_SUCCESS;
2975}