blob: 4b247147d87228e3a88fb13cb903859b7d947b66 [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 Vaskoe0665742021-02-11 11:08:44 +010099 LY_ERR 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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100124 rc = 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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100128 rc = 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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100132 rc = 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);
137 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100138 break;
139 }
140 LY_CHECK_GOTO(rc, cleanup);
141
142 if (parent) {
143 /* get first top-level sibling */
144 for (first = parent; first->parent; first = lyd_parent(first)) {}
145 first = lyd_first_sibling(first);
146 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200147 }
148
Michal Vaskoe0665742021-02-11 11:08:44 +0100149 if (!(parse_opts & LYD_PARSE_ONLY)) {
150 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100151 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
Michal Vasko135719f2022-08-25 12:18:17 +0200152 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100153 LY_CHECK_GOTO(rc, cleanup);
154 }
Radek Krejci7931b192020-06-25 17:05:03 +0200155
Michal Vaskoe0665742021-02-11 11:08:44 +0100156 /* set the operation node */
157 if (op) {
158 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200159 }
160
161cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100162 if (lydctx) {
163 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100165 if (rc) {
166 if (parent) {
167 /* free all the parsed subtrees */
168 for (i = 0; i < parsed.count; ++i) {
169 lyd_free_tree(parsed.dnodes[i]);
170 }
171 } else {
172 /* free everything */
173 lyd_free_all(*first_p);
174 *first_p = NULL;
175 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100176 } else if (subtree_sibling) {
177 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100178 }
179 ly_set_erase(&parsed, NULL);
180 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200181}
182
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100183LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100184lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
185 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
186{
187 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
188
189 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
190 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
191 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
192
193 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
194}
195
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100196LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100197lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
198 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
199{
200 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
201 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
202 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
203
Radek Krejcif16e2542021-02-17 15:39:23 +0100204 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100205}
206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100207LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100208lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
209 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200210{
211 LY_ERR ret;
212 struct ly_in *in;
213
214 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100215 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200216
217 ly_in_free(in, 0);
218 return ret;
219}
220
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100221LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200222lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200223 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200224{
225 LY_ERR ret;
226 struct ly_in *in;
227
228 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100229 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200230
231 ly_in_free(in, 0);
232 return ret;
233}
234
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100235LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200236lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
237 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200238{
239 LY_ERR ret;
240 struct ly_in *in;
241
242 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100243 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200244
245 ly_in_free(in, 0);
246 return ret;
247}
248
Radek Krejcif16e2542021-02-17 15:39:23 +0100249/**
250 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
251 * for schema nodes locked inside the extension instance.
252 *
253 * At least one of @p parent, @p tree, or @p op must always be set.
254 *
255 * Specific @p data_type values have different parameter meaning as follows:
256 * - ::LYD_TYPE_RPC_NETCONF:
257 * - @p parent - must be NULL, the whole RPC is expected;
258 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
259 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
260 * a separate opaque data tree, even if the function fails, this may be returned;
261 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
262 *
263 * - ::LYD_TYPE_NOTIF_NETCONF:
264 * - @p parent - must be NULL, the whole notification is expected;
265 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
266 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
267 * a separate opaque data tree, even if the function fails, this may be returned;
268 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
269 *
270 * - ::LYD_TYPE_REPLY_NETCONF:
271 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
272 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
273 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
274 * a separate opaque data tree, even if the function fails, this may be returned;
275 * - @p op - must be NULL, the reply is appended to the RPC;
276 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
277 *
278 * @param[in] ctx libyang context.
279 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
280 * @param[in] parent Optional parent to connect the parsed nodes to.
281 * @param[in] in Input handle to read the input from.
282 * @param[in] format Expected format of the data in @p in.
283 * @param[in] data_type Expected operation to parse (@ref datatype).
284 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
285 * @param[out] op Optional parsed operation node.
286 * @return LY_ERR value.
287 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
288 */
289static LY_ERR
290lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
291 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200292{
Michal Vaskoe0665742021-02-11 11:08:44 +0100293 LY_ERR rc = LY_SUCCESS;
294 struct lyd_ctx *lydctx = NULL;
295 struct ly_set parsed = {0};
296 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100297 uint32_t i, parse_opts, val_opts, int_opts = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200298
Michal Vasko27fb0262021-02-23 09:42:01 +0100299 if (!ctx) {
300 ctx = LYD_CTX(parent);
301 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200302 if (tree) {
303 *tree = NULL;
304 }
305 if (op) {
306 *op = NULL;
307 }
308
Radek Krejci7931b192020-06-25 17:05:03 +0200309 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200310
Michal Vasko63f3d842020-07-08 10:10:14 +0200311 /* remember input position */
312 in->func_start = in->current;
313
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100314 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200315 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100316 val_opts = 0;
317
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100318 switch (data_type) {
319
320 /* special XML NETCONF data types */
321 case LYD_TYPE_RPC_NETCONF:
322 case LYD_TYPE_NOTIF_NETCONF:
323 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
324 /* fallthrough */
325 case LYD_TYPE_REPLY_NETCONF:
326 if (data_type == LYD_TYPE_REPLY_NETCONF) {
327 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
328 LY_EINVAL);
329 }
330
331 /* parse the NETCONF message */
332 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100333 if (rc && envp) {
334 /* special situation when the envelopes were parsed successfully */
335 if (tree) {
336 *tree = envp;
Michal Vasko73792a12022-05-10 09:28:45 +0200337 } else {
338 lyd_free_all(envp);
Michal Vasko299d5d12021-02-16 16:36:37 +0100339 }
Michal Vasko73792a12022-05-10 09:28:45 +0200340 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100341 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100342
343 /* set out params correctly */
344 if (tree) {
345 if (envp) {
346 /* special out param meaning */
347 *tree = envp;
348 } else {
349 *tree = parent ? NULL : first;
350 }
351 }
352 if (op) {
353 *op = lydctx->op_node;
354 }
355 goto cleanup;
356
357 /* set internal opts */
358 case LYD_TYPE_RPC_YANG:
359 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
360 break;
361 case LYD_TYPE_NOTIF_YANG:
362 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
363 break;
364 case LYD_TYPE_REPLY_YANG:
365 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
366 break;
367 case LYD_TYPE_DATA_YANG:
368 LOGINT(ctx);
369 rc = LY_EINT;
370 goto cleanup;
371 }
372
373 /* parse the data */
374 switch (format) {
375 case LYD_XML:
376 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 +0100377 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200378 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100379 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 +0100380 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200381 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100382 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 +0100383 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200384 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100385 LOGARG(ctx, format);
386 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200387 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200388 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100389 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200390
Michal Vaskoe0665742021-02-11 11:08:44 +0100391 /* set out params correctly */
392 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100393 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100394 }
395 if (op) {
396 *op = lydctx->op_node;
397 }
398
399cleanup:
400 if (lydctx) {
401 lydctx->free(lydctx);
402 }
403 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200404 /* free all the parsed nodes */
405 if (parsed.count) {
406 i = parsed.count;
407 do {
408 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100409 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200410 } while (i);
411 }
412 if (tree && ((format != LYD_XML) || !envp)) {
413 *tree = NULL;
414 }
415 if (op) {
416 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100417 }
418 }
419 ly_set_erase(&parsed, NULL);
420 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200421}
Radek Krejci084289f2019-07-09 17:35:30 +0200422
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100423LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100424lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
425 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
426{
427 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
428
429 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
430}
431
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100432LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100433lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
434 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
435{
436 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
437
438 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
439
440 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
441}
442
Michal Vasko90932a92020-02-12 14:33:03 +0100443struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200444lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100445{
Michal Vaskob104f112020-07-17 09:54:54 +0200446 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100447 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200448 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100449 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100450
Michal Vaskob104f112020-07-17 09:54:54 +0200451 assert(new_node);
452
Michal Vasko9beceb82022-04-05 12:14:15 +0200453 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200454 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100455 return NULL;
456 }
457
Michal Vasko93b16062020-12-09 18:14:18 +0100458 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100459 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100460 getnext_opts = LYS_GETNEXT_OUTPUT;
461 }
462
Michal Vaskoa2756f02021-07-09 13:20:28 +0200463 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200464 /* find the anchor using hashes */
465 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100466 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200467 while (schema) {
468 /* keep trying to find the first existing instance of the closest following schema sibling,
469 * otherwise return NULL - inserting at the end */
470 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
471 break;
472 }
473
Michal Vasko93b16062020-12-09 18:14:18 +0100474 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200475 }
476 } else {
477 /* find the anchor without hashes */
478 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200479 sparent = lysc_data_parent(new_node->schema);
480 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200481 /* we are in top-level, skip all the data from preceding modules */
482 LY_LIST_FOR(match, match) {
483 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
484 break;
485 }
486 }
487 }
488
489 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100490 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200491
492 found = 0;
493 LY_LIST_FOR(match, match) {
494 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
495 /* we have found an opaque node, which must be at the end, so use it OR
496 * modules do not match, so we must have traversed all the data from new_node module (if any),
497 * we have found the first node of the next module, that is what we want */
498 break;
499 }
500
501 /* skip schema nodes until we find the instantiated one */
502 while (!found) {
503 if (new_node->schema == schema) {
504 /* we have found the schema of the new node, continue search to find the first
505 * data node with a different schema (after our schema) */
506 found = 1;
507 break;
508 }
509 if (match->schema == schema) {
510 /* current node (match) is a data node still before the new node, continue search in data */
511 break;
512 }
Michal Vasko93b16062020-12-09 18:14:18 +0100513 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200514 assert(schema);
515 }
516
517 if (found && (match->schema != new_node->schema)) {
518 /* find the next node after we have found our node schema data instance */
519 break;
520 }
521 }
Michal Vasko90932a92020-02-12 14:33:03 +0100522 }
523
524 return match;
525}
526
Michal Vaskoc61dd062022-06-07 11:01:28 +0200527void
Michal Vaskof03ed032020-03-04 13:31:44 +0100528lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100529{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100530 struct lyd_node_inner *par;
531
Michal Vasko90932a92020-02-12 14:33:03 +0100532 assert(!node->next && (node->prev == node));
533
534 node->next = sibling->next;
535 node->prev = sibling;
536 sibling->next = node;
537 if (node->next) {
538 /* sibling had a succeeding node */
539 node->next->prev = node;
540 } else {
541 /* sibling was last, find first sibling and change its prev */
542 if (sibling->parent) {
543 sibling = sibling->parent->child;
544 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200545 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100546 }
547 sibling->prev = node;
548 }
549 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100550
Michal Vasko9f96a052020-03-10 09:41:45 +0100551 for (par = node->parent; par; par = par->parent) {
552 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
553 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100554 par->flags &= ~LYD_DEFAULT;
555 }
556 }
Michal Vasko90932a92020-02-12 14:33:03 +0100557}
558
Michal Vaskoc61dd062022-06-07 11:01:28 +0200559void
Michal Vaskof03ed032020-03-04 13:31:44 +0100560lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100561{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100562 struct lyd_node_inner *par;
563
Michal Vasko90932a92020-02-12 14:33:03 +0100564 assert(!node->next && (node->prev == node));
565
566 node->next = sibling;
567 /* covers situation of sibling being first */
568 node->prev = sibling->prev;
569 sibling->prev = node;
570 if (node->prev->next) {
571 /* sibling had a preceding node */
572 node->prev->next = node;
573 } else if (sibling->parent) {
574 /* sibling was first and we must also change parent child pointer */
575 sibling->parent->child = node;
576 }
577 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100578
Michal Vasko9f96a052020-03-10 09:41:45 +0100579 for (par = node->parent; par; par = par->parent) {
580 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
581 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100582 par->flags &= ~LYD_DEFAULT;
583 }
584 }
Michal Vasko90932a92020-02-12 14:33:03 +0100585}
586
587/**
Michal Vaskob104f112020-07-17 09:54:54 +0200588 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100589 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100590 * Handles inserting into NP containers and key-less lists.
591 *
Michal Vasko90932a92020-02-12 14:33:03 +0100592 * @param[in] parent Parent to insert into.
593 * @param[in] node Node to insert.
594 */
595static void
Michal Vaskob104f112020-07-17 09:54:54 +0200596lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100597{
598 struct lyd_node_inner *par;
599
Radek Krejcia1c1e542020-09-29 16:06:52 +0200600 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100601 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100602
603 par = (struct lyd_node_inner *)parent;
604
Michal Vaskob104f112020-07-17 09:54:54 +0200605 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100606 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100607
Michal Vaskod989ba02020-08-24 10:59:24 +0200608 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100609 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
610 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100611 par->flags &= ~LYD_DEFAULT;
612 }
613 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200614}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100615
Michal Vasko751cb4d2020-07-14 12:25:28 +0200616/**
617 * @brief Learn whether a list instance has all the keys.
618 *
619 * @param[in] list List instance to check.
620 * @return non-zero if all the keys were found,
621 * @return 0 otherwise.
622 */
623static int
624lyd_insert_has_keys(const struct lyd_node *list)
625{
626 const struct lyd_node *key;
627 const struct lysc_node *skey = NULL;
628
629 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200630 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200631 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
632 if (!key || (key->schema != skey)) {
633 /* key missing */
634 return 0;
635 }
636
637 key = key->next;
638 }
639
640 /* all keys found */
641 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100642}
643
644void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200645lyd_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 +0100646{
Michal Vaskob104f112020-07-17 09:54:54 +0200647 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100648
Michal Vaskob104f112020-07-17 09:54:54 +0200649 /* inserting list without its keys is not supported */
650 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100651 assert(!parent || !parent->schema ||
652 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100653
Michal Vaskob104f112020-07-17 09:54:54 +0200654 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100655 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100656 }
Michal Vasko90932a92020-02-12 14:33:03 +0100657
Michal Vaskob104f112020-07-17 09:54:54 +0200658 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100659 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100660
Michal Vasko9beceb82022-04-05 12:14:15 +0200661 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200662 /* no next anchor */
663 anchor = NULL;
664 } else {
665 /* find the anchor, our next node, so we can insert before it */
666 anchor = lyd_insert_get_next_anchor(first_sibling, node);
667 }
668
Michal Vaskob104f112020-07-17 09:54:54 +0200669 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200670 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200671 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100672 if (!parent && (*first_sibling_p == anchor)) {
673 /* move first sibling */
674 *first_sibling_p = node;
675 }
Michal Vaskob104f112020-07-17 09:54:54 +0200676 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200677 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200678 lyd_insert_after_node(first_sibling->prev, node);
679 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200680 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200681 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100682 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200683 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200684 *first_sibling_p = node;
685 }
686
687 /* insert into parent HT */
688 lyd_insert_hash(node);
689
690 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200691 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200692 lyd_hash(parent);
693
694 /* now we can insert even the list into its parent HT */
695 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100696 }
Michal Vasko90932a92020-02-12 14:33:03 +0100697}
698
Michal Vasko717a4c32020-12-07 09:40:10 +0100699/**
700 * @brief Check schema place of a node to be inserted.
701 *
702 * @param[in] parent Schema node of the parent data node.
703 * @param[in] sibling Schema node of a sibling data node.
704 * @param[in] schema Schema node if the data node to be inserted.
705 * @return LY_SUCCESS on success.
706 * @return LY_EINVAL if the place is invalid.
707 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100708static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100709lyd_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 +0100710{
711 const struct lysc_node *par2;
712
Michal Vasko62ed12d2020-05-21 10:08:25 +0200713 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100714 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
715 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100716
Michal Vasko717a4c32020-12-07 09:40:10 +0100717 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100718 /* opaque nodes can be inserted wherever */
719 return LY_SUCCESS;
720 }
721
Michal Vasko717a4c32020-12-07 09:40:10 +0100722 if (!parent) {
723 parent = lysc_data_parent(sibling);
724 }
725
Michal Vaskof03ed032020-03-04 13:31:44 +0100726 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200727 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100728
729 if (parent) {
730 /* inner node */
731 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200732 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200733 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100734 return LY_EINVAL;
735 }
736 } else {
737 /* top-level node */
738 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200739 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100740 return LY_EINVAL;
741 }
742 }
743
744 return LY_SUCCESS;
745}
746
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100747LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200748lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100749{
750 struct lyd_node *iter;
751
Michal Vasko0ab974d2021-02-24 13:18:26 +0100752 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100753 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100754
Michal Vasko717a4c32020-12-07 09:40:10 +0100755 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100756
Michal Vasko0ab974d2021-02-24 13:18:26 +0100757 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100758 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100759 return LY_EINVAL;
760 }
761
762 if (node->parent || node->prev->next) {
763 lyd_unlink_tree(node);
764 }
765
766 while (node) {
767 iter = node->next;
768 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200769 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100770 node = iter;
771 }
772 return LY_SUCCESS;
773}
774
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100775LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200776lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100777{
778 struct lyd_node *iter;
779
780 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
781 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
782
783 if (first->schema && (first->schema->flags & LYS_KEY)) {
784 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
785 return LY_EINVAL;
786 }
787
788 while (first) {
789 iter = first->next;
790 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100791 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100792 first = iter;
793 }
794 return LY_SUCCESS;
795}
796
797LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200798lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100799{
800 struct lyd_node *iter;
801
Michal Vaskob104f112020-07-17 09:54:54 +0200802 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100803
Michal Vaskob104f112020-07-17 09:54:54 +0200804 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100805 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100806 }
807
Michal Vasko553d0b52020-12-04 16:27:52 +0100808 if (sibling == node) {
809 /* we need to keep the connection to siblings so we can insert into them */
810 sibling = sibling->prev;
811 }
812
Michal Vaskob1b5c262020-03-05 14:29:47 +0100813 if (node->parent || node->prev->next) {
814 lyd_unlink_tree(node);
815 }
816
817 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100818 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200819 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200820 return LY_EINVAL;
821 }
822
Michal Vaskob1b5c262020-03-05 14:29:47 +0100823 iter = node->next;
824 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200825 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100826 node = iter;
827 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100828
Michal Vaskob104f112020-07-17 09:54:54 +0200829 if (first) {
830 /* find the first sibling */
831 *first = sibling;
832 while ((*first)->prev->next) {
833 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100834 }
835 }
836
837 return LY_SUCCESS;
838}
839
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100840LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100841lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
842{
Michal Vaskobce230b2022-04-19 09:55:31 +0200843 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100844 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100845
Michal Vasko717a4c32020-12-07 09:40:10 +0100846 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100847
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200848 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200849 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100850 return LY_EINVAL;
851 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200852 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
853 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100854 return LY_EINVAL;
855 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100856
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100857 lyd_unlink_tree(node);
858 lyd_insert_before_node(sibling, node);
859 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100860
Michal Vaskof03ed032020-03-04 13:31:44 +0100861 return LY_SUCCESS;
862}
863
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100864LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100865lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
866{
Michal Vaskobce230b2022-04-19 09:55:31 +0200867 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100868 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100869
Michal Vasko717a4c32020-12-07 09:40:10 +0100870 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100871
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200872 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200873 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100874 return LY_EINVAL;
875 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200876 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
877 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100878 return LY_EINVAL;
879 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100880
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100881 lyd_unlink_tree(node);
882 lyd_insert_after_node(sibling, node);
883 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100884
Michal Vaskof03ed032020-03-04 13:31:44 +0100885 return LY_SUCCESS;
886}
887
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100888LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200889lyd_unlink_siblings(struct lyd_node *node)
890{
891 struct lyd_node *next, *elem, *first = NULL;
892
893 LY_LIST_FOR_SAFE(node, next, elem) {
894 lyd_unlink_tree(elem);
895 lyd_insert_node(NULL, &first, elem, 1);
896 }
897}
898
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100899LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100900lyd_unlink_tree(struct lyd_node *node)
901{
902 struct lyd_node *iter;
903
904 if (!node) {
905 return;
906 }
907
Michal Vaskob104f112020-07-17 09:54:54 +0200908 /* update hashes while still linked into the tree */
909 lyd_unlink_hash(node);
910
Michal Vaskof03ed032020-03-04 13:31:44 +0100911 /* unlink from siblings */
912 if (node->prev->next) {
913 node->prev->next = node->next;
914 }
915 if (node->next) {
916 node->next->prev = node->prev;
917 } else {
918 /* unlinking the last node */
919 if (node->parent) {
920 iter = node->parent->child;
921 } else {
922 iter = node->prev;
923 while (iter->prev != node) {
924 iter = iter->prev;
925 }
926 }
927 /* update the "last" pointer from the first node */
928 iter->prev = node->prev;
929 }
930
931 /* unlink from parent */
932 if (node->parent) {
933 if (node->parent->child == node) {
934 /* the node is the first child */
935 node->parent->child = node->next;
936 }
937
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200938 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100939 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200940
Michal Vaskof03ed032020-03-04 13:31:44 +0100941 node->parent = NULL;
942 }
943
944 node->next = NULL;
945 node->prev = node;
946}
947
Michal Vaskoa5da3292020-08-12 13:10:50 +0200948void
Michal Vasko871a0252020-11-11 18:35:24 +0100949lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200950{
951 struct lyd_meta *last, *iter;
952
953 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200954
955 if (!meta) {
956 return;
957 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200958
959 for (iter = meta; iter; iter = iter->next) {
960 iter->parent = parent;
961 }
962
963 /* insert as the last attribute */
964 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200965 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200966 last->next = meta;
967 } else {
968 parent->meta = meta;
969 }
970
971 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100972 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200973 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100974 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200975 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200976}
977
978LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100979lyd_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 +0200980 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 +0100981 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 +0100982{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100983 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100984 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200985 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100986 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200987 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100988
Michal Vasko9f96a052020-03-10 09:41:45 +0100989 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100990
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200991 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200992 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200993 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100994 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200995 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100996 break;
997 }
998 }
999 if (!ant) {
1000 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001002 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001003 ret = LY_EINVAL;
1004 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001005 }
1006
Michal Vasko9f96a052020-03-10 09:41:45 +01001007 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001009 mt->parent = parent;
1010 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001011 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001012 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 +01001013 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001014 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1015 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1016 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001017
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001018 /* insert as the last attribute */
1019 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001020 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001021 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001022 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001023 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001024 }
1025
Michal Vasko9f96a052020-03-10 09:41:45 +01001026 if (meta) {
1027 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001028 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001029
1030cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001031 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001032}
1033
Michal Vaskoa5da3292020-08-12 13:10:50 +02001034void
1035lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1036{
1037 struct lyd_attr *last, *iter;
1038 struct lyd_node_opaq *opaq;
1039
1040 assert(parent && !parent->schema);
1041
1042 if (!attr) {
1043 return;
1044 }
1045
1046 opaq = (struct lyd_node_opaq *)parent;
1047 for (iter = attr; iter; iter = iter->next) {
1048 iter->parent = opaq;
1049 }
1050
1051 /* insert as the last attribute */
1052 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001053 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001054 last->next = attr;
1055 } else {
1056 opaq->attr = attr;
1057 }
1058}
1059
Michal Vasko52927e22020-03-16 17:26:14 +01001060LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001061lyd_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 +01001062 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 +02001063 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 +01001064{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001065 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001066 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001067
1068 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001069 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001070
Michal Vasko2a3722d2021-06-16 11:52:39 +02001071 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001072 value = "";
1073 }
1074
1075 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001076 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001077
Michal Vaskoad92b672020-11-12 13:11:31 +01001078 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001079 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001080 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001081 }
1082 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001083 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001084 }
1085
Michal Vasko52927e22020-03-16 17:26:14 +01001086 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001087 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1088 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001089 *dynamic = 0;
1090 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001091 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001092 }
Michal Vasko501af032020-11-11 20:27:44 +01001093 at->format = format;
1094 at->val_prefix_data = val_prefix_data;
1095 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001096
1097 /* insert as the last attribute */
1098 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001099 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001100 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001101 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001102 last->next = at;
1103 }
1104
Radek Krejci011e4aa2020-09-04 15:22:31 +02001105finish:
1106 if (ret) {
1107 lyd_free_attr_single(ctx, at);
1108 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001109 *attr = at;
1110 }
1111 return LY_SUCCESS;
1112}
1113
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001114LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001115lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001116{
Michal Vasko004d3152020-06-11 19:59:22 +02001117 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001118
Michal Vasko004d3152020-06-11 19:59:22 +02001119 if (ly_path_eval(path, tree, &target)) {
1120 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001121 }
1122
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
1156 if (schema1->module->revision || schema2->module->revision) {
1157 if (!schema1->module->revision || !schema2->module->revision) {
1158 return 0;
1159 }
1160 if (strcmp(schema1->module->revision, schema2->module->revision)) {
1161 return 0;
1162 }
1163 }
1164
1165 return 1;
1166}
1167
1168/**
1169 * @brief Check the equality of the schemas for all parent nodes.
1170 *
1171 * Both nodes must be from different contexts.
1172 *
1173 * @param node1 Data of first node.
1174 * @param node2 Data of second node.
1175 * @return 1 if the all related parental schemas are equal otherwise 0.
1176 */
1177static ly_bool
1178lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1179{
1180 const struct lysc_node *parent1, *parent2;
1181
1182 assert(node1 && node2);
1183
1184 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1185 parent1 && parent2;
1186 parent1 = parent1->parent, parent2 = parent2->parent) {
1187 if (!lyd_compare_schema_equal(parent1, parent2)) {
1188 return 0;
1189 }
1190 }
1191
1192 if (parent1 || parent2) {
1193 return 0;
1194 }
1195
1196 return 1;
1197}
1198
1199/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001200 * @brief Compare 2 nodes values including opaque node values.
1201 *
1202 * @param[in] node1 First node to compare.
1203 * @param[in] node2 Second node to compare.
1204 * @return LY_SUCCESS if equal.
1205 * @return LY_ENOT if not equal.
1206 * @return LY_ERR on error.
1207 */
1208static LY_ERR
1209lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1210{
1211 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1212 const char *val1, *val2, *col;
1213 const struct lys_module *mod;
1214 char *val_dyn = NULL;
1215 LY_ERR rc = LY_SUCCESS;
1216
1217 if (!node1->schema) {
1218 opaq1 = (struct lyd_node_opaq *)node1;
1219 }
1220 if (!node2->schema) {
1221 opaq2 = (struct lyd_node_opaq *)node2;
1222 }
1223
1224 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1225 /* opaque XML and opaque XML node */
1226 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1227 opaq2->val_prefix_data)) {
1228 return LY_ENOT;
1229 }
1230 return LY_SUCCESS;
1231 }
1232
1233 /* get their values */
1234 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1235 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1236 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1237 if (!mod) {
1238 /* unable to compare */
1239 return LY_ENOT;
1240 }
1241
1242 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1243 LOGMEM(LYD_CTX(node1));
1244 return LY_EMEM;
1245 }
1246 val1 = val_dyn;
1247 } else {
1248 val1 = lyd_get_value(node1);
1249 }
1250 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1251 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1252 if (!mod) {
1253 return LY_ENOT;
1254 }
1255
1256 assert(!val_dyn);
1257 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1258 LOGMEM(LYD_CTX(node2));
1259 return LY_EMEM;
1260 }
1261 val2 = val_dyn;
1262 } else {
1263 val2 = lyd_get_value(node2);
1264 }
1265
1266 /* compare values */
1267 if (strcmp(val1, val2)) {
1268 rc = LY_ENOT;
1269 }
1270
1271 free(val_dyn);
1272 return rc;
1273}
1274
1275/**
aPiecek2f63f952021-03-30 12:22:18 +02001276 * @brief Internal implementation of @ref lyd_compare_single.
1277 * @copydoc lyd_compare_single
1278 * @param[in] parental_schemas_checked Flag used for optimization.
1279 * When this function is called for the first time, the flag must be set to 0.
1280 * The @ref lyd_compare_schema_parents_equal should be called only once during
1281 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1282 */
1283static LY_ERR
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001284lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1285 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001286{
1287 const struct lyd_node *iter1, *iter2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001288 struct lyd_node_any *any1, *any2;
Michal Vaskof3c80a72022-08-17 10:22:04 +02001289 int len1, len2;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001290 LY_ERR r;
Radek Krejci084289f2019-07-09 17:35:30 +02001291
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001292 if (!node1 || !node2) {
1293 if (node1 == node2) {
1294 return LY_SUCCESS;
1295 } else {
1296 return LY_ENOT;
1297 }
1298 }
1299
aPiecek2f63f952021-03-30 12:22:18 +02001300 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1301 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001302 if (options & LYD_COMPARE_OPAQ) {
1303 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1304 return LY_ENOT;
1305 }
1306 } else {
1307 if (node1->schema != node2->schema) {
1308 return LY_ENOT;
1309 }
aPiecek2f63f952021-03-30 12:22:18 +02001310 }
1311 } else {
1312 /* different contexts */
1313 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1314 return LY_ENOT;
1315 }
1316 if (!parental_schemas_checked) {
1317 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1318 return LY_ENOT;
1319 }
1320 parental_schemas_checked = 1;
1321 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001322 }
1323
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001324 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001325 return LY_ENOT;
1326 }
aPiecek2f63f952021-03-30 12:22:18 +02001327 /* 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 +02001328
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001329 if (!node1->schema || !node2->schema) {
1330 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001331 return LY_ENOT;
1332 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001333 if ((r = lyd_compare_single_value(node1, node2))) {
1334 return r;
Michal Vasko52927e22020-03-16 17:26:14 +01001335 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001336
Michal Vasko52927e22020-03-16 17:26:14 +01001337 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001338 iter1 = lyd_child(node1);
1339 iter2 = lyd_child(node2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001340 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001341 }
1342 return LY_SUCCESS;
1343 } else {
1344 switch (node1->schema->nodetype) {
1345 case LYS_LEAF:
1346 case LYS_LEAFLIST:
1347 if (options & LYD_COMPARE_DEFAULTS) {
1348 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1349 return LY_ENOT;
1350 }
1351 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001352 if ((r = lyd_compare_single_value(node1, node2))) {
1353 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001354 }
1355
aPiecek2f63f952021-03-30 12:22:18 +02001356 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001357 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001358 case LYS_RPC:
1359 case LYS_ACTION:
1360 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001361 if (options & LYD_COMPARE_DEFAULTS) {
1362 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1363 return LY_ENOT;
1364 }
1365 }
1366 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001367 iter1 = lyd_child(node1);
1368 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001369 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001370 }
1371 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001372 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001373 iter1 = lyd_child(node1);
1374 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001375
1376 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1377 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001378 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001379 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001380 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001381 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001382 return LY_ENOT;
1383 }
1384 iter1 = iter1->next;
1385 iter2 = iter2->next;
1386 }
1387 } else {
1388 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1389
Radek Krejci0f969882020-08-21 16:56:47 +02001390all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001391 if (!iter1 && !iter2) {
1392 /* no children, nothing to compare */
1393 return LY_SUCCESS;
1394 }
1395
Michal Vaskod989ba02020-08-24 10:59:24 +02001396 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001397 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001398 return LY_ENOT;
1399 }
1400 }
1401 if (iter1 || iter2) {
1402 return LY_ENOT;
1403 }
1404 }
1405 return LY_SUCCESS;
1406 case LYS_ANYXML:
1407 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001408 any1 = (struct lyd_node_any *)node1;
1409 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001410
1411 if (any1->value_type != any2->value_type) {
1412 return LY_ENOT;
1413 }
1414 switch (any1->value_type) {
1415 case LYD_ANYDATA_DATATREE:
1416 iter1 = any1->value.tree;
1417 iter2 = any2->value.tree;
1418 goto all_children_compare;
1419 case LYD_ANYDATA_STRING:
1420 case LYD_ANYDATA_XML:
1421 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001422 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1423 return LY_ENOT;
1424 } else if (!any1->value.str && !any2->value.str) {
1425 return LY_SUCCESS;
1426 }
Michal Vasko52927e22020-03-16 17:26:14 +01001427 len1 = strlen(any1->value.str);
1428 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001429 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001430 return LY_ENOT;
1431 }
1432 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001433 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001434 len1 = lyd_lyb_data_length(any1->value.mem);
1435 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001436 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001437 return LY_ENOT;
1438 }
1439 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001440 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001441 }
1442 }
1443
Michal Vaskob7be7a82020-08-20 09:09:04 +02001444 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001445 return LY_EINT;
1446}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001447
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001448LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001449lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1450{
1451 return lyd_compare_single_(node1, node2, options, 0);
1452}
1453
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001454LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001455lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001456{
Michal Vaskod989ba02020-08-24 10:59:24 +02001457 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001458 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1459 }
1460
Michal Vasko11a81432020-07-28 16:31:29 +02001461 if (node1 == node2) {
1462 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001463 }
Michal Vasko11a81432020-07-28 16:31:29 +02001464 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001465}
1466
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001467LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001468lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1469{
1470 if (!meta1 || !meta2) {
1471 if (meta1 == meta2) {
1472 return LY_SUCCESS;
1473 } else {
1474 return LY_ENOT;
1475 }
1476 }
1477
Michal Vaskoa8083062020-11-06 17:22:10 +01001478 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001479 return LY_ENOT;
1480 }
1481
Michal Vasko21725742020-06-29 11:49:25 +02001482 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1483}
1484
Radek Krejci22ebdba2019-07-25 13:59:43 +02001485/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001486 * @brief Create a copy of the attribute.
1487 *
1488 * @param[in] attr Attribute to copy.
1489 * @param[in] node Opaque where to append the new attribute.
1490 * @param[out] dup Optional created attribute copy.
1491 * @return LY_ERR value.
1492 */
1493static LY_ERR
1494lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1495{
1496 LY_ERR ret = LY_SUCCESS;
1497 struct lyd_attr *a, *last;
1498 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1499
1500 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1501
1502 /* create a copy */
1503 a = calloc(1, sizeof *attr);
1504 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1505
1506 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1507 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1508 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1509 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1510 a->hints = attr->hints;
1511 a->format = attr->format;
1512 if (attr->val_prefix_data) {
1513 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1514 LY_CHECK_GOTO(ret, finish);
1515 }
1516
1517 /* insert as the last attribute */
1518 a->parent = opaq;
1519 if (opaq->attr) {
1520 for (last = opaq->attr; last->next; last = last->next) {}
1521 last->next = a;
1522 } else {
1523 opaq->attr = a;
1524 }
1525
1526finish:
1527 if (ret) {
1528 lyd_free_attr_single(LYD_CTX(node), a);
1529 } else if (dup) {
1530 *dup = a;
1531 }
1532 return LY_SUCCESS;
1533}
1534
1535/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001536 * @brief Find @p schema equivalent in @p trg_ctx.
1537 *
1538 * @param[in] schema Schema node to find.
1539 * @param[in] trg_ctx Target context to search in.
1540 * @param[in] parent Data parent of @p schema, if any.
1541 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1542 * @return LY_RRR value.
1543 */
1544static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02001545lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
Michal Vaskoddd76592022-01-17 13:34:48 +01001546 const struct lysc_node **trg_schema)
1547{
Michal Vasko9beceb82022-04-05 12:14:15 +02001548 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1549 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001550 char *path;
1551
1552 if (!schema) {
1553 /* opaque node */
1554 *trg_schema = NULL;
1555 return LY_SUCCESS;
1556 }
1557
Michal Vasko9beceb82022-04-05 12:14:15 +02001558 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001559 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001560 trg_parent = parent->schema;
1561 src_parent = lysc_data_parent(schema);
1562 }
1563
1564 do {
1565 /* find the next parent */
1566 sp = schema;
1567 while (sp->parent != src_parent) {
1568 sp = sp->parent;
1569 }
1570 src_parent = sp;
1571
1572 if (!src_parent->parent) {
1573 /* find the module first */
1574 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1575 if (!trg_mod) {
1576 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1577 src_parent->module->name);
1578 return LY_ENOTFOUND;
1579 }
1580 }
1581
1582 /* find the next parent */
1583 assert(trg_parent || trg_mod);
1584 tp = NULL;
1585 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1586 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1587 break;
1588 }
1589 }
1590 if (!tp) {
1591 /* schema node not found */
1592 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1593 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1594 free(path);
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 Vasko27f536f2022-04-01 09:17:30 +02001681 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
1682 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 Vasko3a41dff2020-07-15 14:30:28 +02001806 if (parent && (parent->schema == orig_parent->schema)) {
1807 /* stop creating parents, connect what we have into the provided parent */
1808 iter = parent;
1809 repeat = 0;
1810 } else {
1811 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001812 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 +02001813 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001814 }
1815 if (!*local_parent) {
1816 *local_parent = (struct lyd_node_inner *)iter;
1817 }
1818 if (iter->child) {
1819 /* 1) list - add after keys
1820 * 2) provided parent with some children */
1821 iter->child->prev->next = *dup_parent;
1822 if (*dup_parent) {
1823 (*dup_parent)->prev = iter->child->prev;
1824 iter->child->prev = *dup_parent;
1825 }
1826 } else {
1827 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1828 }
1829 if (*dup_parent) {
1830 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1831 }
1832 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001833 if (orig_parent->flags & LYD_EXT) {
1834 ext_parent = 1;
1835 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001836 }
1837
1838 if (repeat && parent) {
1839 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001840 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001841 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001842 return LY_EINVAL;
1843 }
1844
1845 return LY_SUCCESS;
1846}
1847
1848static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001849lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1850 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001851{
1852 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001853 const struct lyd_node *orig; /* original node to be duplicated */
1854 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001855 struct lyd_node *top = NULL; /* the most higher created node */
1856 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001857
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001858 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001859
1860 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001861 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 +02001862 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001863 } else {
1864 local_parent = parent;
1865 }
1866
Radek Krejci22ebdba2019-07-25 13:59:43 +02001867 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001868 if (lysc_is_key(orig->schema)) {
1869 if (local_parent) {
1870 /* the key must already exist in the parent */
1871 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001872 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001873 } else {
1874 assert(!(options & LYD_DUP_WITH_PARENTS));
1875 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001876 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001877 LY_CHECK_GOTO(rc, error);
1878 }
1879 } else {
1880 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001881 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1882 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001883 LY_CHECK_GOTO(rc, error);
1884 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001885 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001886 break;
1887 }
1888 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001889
Michal Vasko3a41dff2020-07-15 14:30:28 +02001890 if (dup) {
1891 *dup = first;
1892 }
1893 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001894
1895error:
1896 if (top) {
1897 lyd_free_tree(top);
1898 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001899 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001900 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001901 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001902}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001903
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001904/**
1905 * @brief Check the context of node and parent when duplicating nodes.
1906 *
1907 * @param[in] node Node to duplicate.
1908 * @param[in] parent Parent of the duplicated node(s).
1909 * @return LY_ERR value.
1910 */
1911static LY_ERR
1912lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1913{
1914 const struct lyd_node *iter;
1915
1916 if (!node || !parent) {
1917 return LY_SUCCESS;
1918 }
1919
1920 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1921 /* try to find top-level ext data parent */
1922 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1923
1924 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
1925 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1926 return LY_EINVAL;
1927 }
1928 }
1929
1930 return LY_SUCCESS;
1931}
1932
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001933LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001934lyd_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 +02001935{
Michal Vaskoddd76592022-01-17 13:34:48 +01001936 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001937 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001938
1939 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1940}
1941
1942LIBYANG_API_DEF LY_ERR
1943lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1944 uint32_t options, struct lyd_node **dup)
1945{
1946 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1947
1948 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001949}
1950
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001951LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001952lyd_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 +02001953{
Michal Vaskoddd76592022-01-17 13:34:48 +01001954 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001955 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001956
1957 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1958}
1959
1960LIBYANG_API_DEF LY_ERR
1961lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1962 uint32_t options, struct lyd_node **dup)
1963{
1964 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1965
1966 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001967}
1968
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001969LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001970lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001971{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001972 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02001973 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02001974 struct lyd_meta *mt, *last;
1975
Michal Vasko3a41dff2020-07-15 14:30:28 +02001976 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001977
Michal Vasko33c48972022-07-20 10:28:07 +02001978 /* log to node context but value must always use the annotation context */
1979 ctx = meta->annotation->module->ctx;
1980
Michal Vasko25a32822020-07-09 15:48:22 +02001981 /* create a copy */
1982 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001983 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001984 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02001985 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001986 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02001987 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001988
1989 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001990 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001991 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001992 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001993 last->next = mt;
1994 } else {
1995 node->meta = mt;
1996 }
1997
Radek Krejci011e4aa2020-09-04 15:22:31 +02001998finish:
1999 if (ret) {
2000 lyd_free_meta_single(mt);
2001 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002002 *dup = mt;
2003 }
2004 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002005}
2006
Michal Vasko4490d312020-06-16 13:08:55 +02002007/**
2008 * @brief Merge a source sibling into target siblings.
2009 *
2010 * @param[in,out] first_trg First target sibling, is updated if top-level.
2011 * @param[in] parent_trg Target parent.
2012 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002013 * @param[in] merge_cb Optional merge callback.
2014 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002015 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002016 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002017 * @return LY_ERR value.
2018 */
2019static LY_ERR
2020lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Michal Vaskocd3f6172021-05-18 16:14:50 +02002021 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002022{
Michal Vasko4490d312020-06-16 13:08:55 +02002023 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002024 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002025 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002026 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002027 struct lyd_dup_inst *child_dup_inst = NULL;
2028 LY_ERR ret;
2029 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002030
2031 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002032 if (!sibling_src->schema) {
2033 /* try to find the same opaque node */
2034 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2035 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002036 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002037 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002038 } else {
2039 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002040 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002041 }
2042
Michal Vaskocd3f6172021-05-18 16:14:50 +02002043 if (match_trg) {
2044 /* update match as needed */
2045 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2046 } else {
2047 /* first instance of this node */
2048 first_inst = 1;
2049 }
2050
2051 if (match_trg) {
2052 /* call callback */
2053 if (merge_cb) {
2054 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2055 }
2056
Michal Vasko4490d312020-06-16 13:08:55 +02002057 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002058 if (!match_trg->schema) {
2059 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2060 /* update value */
2061 opaq_trg = (struct lyd_node_opaq *)match_trg;
2062 opaq_src = (struct lyd_node_opaq *)sibling_src;
2063
2064 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2065 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2066 opaq_trg->hints = opaq_src->hints;
2067
2068 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2069 opaq_trg->format = opaq_src->format;
2070 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2071 &opaq_trg->val_prefix_data);
2072 }
2073 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2074 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002075 /* since they are different, they cannot both be default */
2076 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2077
Michal Vasko3a41dff2020-07-15 14:30:28 +02002078 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2079 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002080 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002081 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2082 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002083 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002084
2085 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002086 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002087 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002088 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002089 /* update value */
2090 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002091 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002092
2093 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002094 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002095 }
2096
2097 /* check descendants, recursively */
2098 ret = LY_SUCCESS;
2099 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2100 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2101 &child_dup_inst);
2102 if (ret) {
2103 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002104 }
2105 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002106 lyd_dup_inst_free(child_dup_inst);
2107 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002108 } else {
2109 /* node not found, merge it */
2110 if (options & LYD_MERGE_DESTRUCT) {
2111 dup_src = (struct lyd_node *)sibling_src;
2112 lyd_unlink_tree(dup_src);
2113 /* spend it */
2114 *sibling_src_p = NULL;
2115 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002116 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 +02002117 }
2118
Michal Vasko7e8315b2021-08-03 17:01:06 +02002119 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2120 /* set LYD_NEW for all the new nodes, required for validation */
2121 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2122 elem->flags |= LYD_NEW;
2123 LYD_TREE_DFS_END(dup_src, elem);
2124 }
Michal Vasko4490d312020-06-16 13:08:55 +02002125 }
2126
Michal Vaskocd3f6172021-05-18 16:14:50 +02002127 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002128 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002129
2130 if (first_inst) {
2131 /* remember not to find this instance next time */
2132 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2133 }
2134
2135 /* call callback, no source node */
2136 if (merge_cb) {
2137 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2138 }
Michal Vasko4490d312020-06-16 13:08:55 +02002139 }
2140
2141 return LY_SUCCESS;
2142}
2143
Michal Vasko3a41dff2020-07-15 14:30:28 +02002144static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002145lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2146 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002147{
2148 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002149 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002150 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002151 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002152
2153 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002154 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2155 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002156
2157 if (!source) {
2158 /* nothing to merge */
2159 return LY_SUCCESS;
2160 }
2161
Michal Vasko831422b2020-11-24 11:20:51 +01002162 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002163 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002164 return LY_EINVAL;
2165 }
2166
2167 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002168 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2169 /* skip data nodes from different modules */
2170 continue;
2171 }
2172
Radek Krejci857189e2020-09-01 13:26:36 +02002173 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002174 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2175 if (ret) {
2176 break;
2177 }
Michal Vasko4490d312020-06-16 13:08:55 +02002178 if (first && !sibling_src) {
2179 /* source was spent (unlinked), move to the next node */
2180 source = tmp;
2181 }
2182
Michal Vasko3a41dff2020-07-15 14:30:28 +02002183 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002184 break;
2185 }
2186 }
2187
2188 if (options & LYD_MERGE_DESTRUCT) {
2189 /* free any leftover source data that were not merged */
2190 lyd_free_siblings((struct lyd_node *)source);
2191 }
2192
Michal Vaskocd3f6172021-05-18 16:14:50 +02002193 lyd_dup_inst_free(dup_inst);
2194 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002195}
2196
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002197LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002198lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002199{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002200 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002201}
2202
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002203LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002204lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002205{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002206 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2207}
2208
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002209LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002210lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2211 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2212{
2213 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002214}
2215
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002216static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002217lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002218{
Michal Vasko14654712020-02-06 08:35:21 +01002219 /* ending \0 */
2220 ++reqlen;
2221
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002222 if (reqlen > *buflen) {
2223 if (is_static) {
2224 return LY_EINCOMPLETE;
2225 }
2226
2227 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2228 if (!*buffer) {
2229 return LY_EMEM;
2230 }
2231
2232 *buflen = reqlen;
2233 }
2234
2235 return LY_SUCCESS;
2236}
2237
Michal Vaskod59035b2020-07-08 12:00:06 +02002238LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002239lyd_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 +02002240{
2241 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002242 size_t len;
2243 const char *val;
2244 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002245
Michal Vasko824d0832021-11-04 15:36:51 +01002246 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002247 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002248 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002249 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002250
2251 quot = '\'';
2252 if (strchr(val, '\'')) {
2253 quot = '"';
2254 }
2255 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002256 }
2257
2258 return LY_SUCCESS;
2259}
2260
2261/**
2262 * @brief Append leaf-list value predicate to path.
2263 *
2264 * @param[in] node Node to print.
2265 * @param[in,out] buffer Buffer to print to.
2266 * @param[in,out] buflen Current buffer length.
2267 * @param[in,out] bufused Current number of characters used in @p buffer.
2268 * @param[in] is_static Whether buffer is static or can be reallocated.
2269 * @return LY_ERR
2270 */
2271static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002272lyd_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 +02002273{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002274 size_t len;
2275 const char *val;
2276 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002277
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002278 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002279 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002280 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002281
2282 quot = '\'';
2283 if (strchr(val, '\'')) {
2284 quot = '"';
2285 }
2286 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2287
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002288 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002289}
2290
2291/**
2292 * @brief Append node position (relative to its other instances) predicate to path.
2293 *
2294 * @param[in] node Node to print.
2295 * @param[in,out] buffer Buffer to print to.
2296 * @param[in,out] buflen Current buffer length.
2297 * @param[in,out] bufused Current number of characters used in @p buffer.
2298 * @param[in] is_static Whether buffer is static or can be reallocated.
2299 * @return LY_ERR
2300 */
2301static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002302lyd_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 +02002303{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002304 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002305 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002306 char *val = NULL;
2307 LY_ERR rc;
2308
Michal Vasko50cc0562021-05-18 16:15:43 +02002309 pos = lyd_list_pos(node);
2310 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002311 return LY_EMEM;
2312 }
2313
2314 len = 1 + strlen(val) + 1;
2315 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2316 if (rc != LY_SUCCESS) {
2317 goto cleanup;
2318 }
2319
2320 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2321
2322cleanup:
2323 free(val);
2324 return rc;
2325}
2326
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002327LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002328lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2329{
Radek Krejci857189e2020-09-01 13:26:36 +02002330 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002331 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002332 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002333 const struct lyd_node *iter, *parent;
2334 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002335 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002336
2337 LY_CHECK_ARG_RET(NULL, node, NULL);
2338 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002339 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002340 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002341 } else {
2342 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002343 }
2344
2345 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002346 case LYD_PATH_STD:
2347 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002348 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002349 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002350 ++depth;
2351 }
2352
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002353 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002354 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002355 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002356 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002357iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002358 /* get the module */
2359 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2360 parent = lyd_parent(iter);
2361 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2362 if (prev_mod == mod) {
2363 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002364 }
2365
2366 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002367 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2368 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002369 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2370 if (rc != LY_SUCCESS) {
2371 break;
2372 }
2373
2374 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002375 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002376
Michal Vasko790b2bc2020-08-03 13:35:06 +02002377 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002378 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002379 switch (iter->schema->nodetype) {
2380 case LYS_LIST:
2381 if (iter->schema->flags & LYS_KEYLESS) {
2382 /* print its position */
2383 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2384 } else {
2385 /* print all list keys in predicates */
2386 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2387 }
2388 break;
2389 case LYS_LEAFLIST:
2390 if (iter->schema->flags & LYS_CONFIG_W) {
2391 /* print leaf-list value */
2392 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2393 } else {
2394 /* print its position */
2395 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2396 }
2397 break;
2398 default:
2399 /* nothing to print more */
2400 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002401 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002402 }
2403 if (rc != LY_SUCCESS) {
2404 break;
2405 }
2406
Michal Vasko14654712020-02-06 08:35:21 +01002407 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002408 }
2409 break;
2410 }
2411
2412 return buffer;
2413}
Michal Vaskoe444f752020-02-10 12:20:06 +01002414
Michal Vaskodbf3e652022-10-21 08:46:25 +02002415char *
2416lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2417{
2418 uint32_t depth;
2419 size_t bufused = 0, buflen = 0, len;
2420 char *buffer = NULL;
2421 const struct lyd_node *iter, *parent;
2422 const struct lys_module *mod, *prev_mod;
2423 LY_ERR rc = LY_SUCCESS;
2424
2425 switch (pathtype) {
2426 case LYD_PATH_STD:
2427 case LYD_PATH_STD_NO_LAST_PRED:
2428 for (depth = 1; depth <= dnodes->count; ++depth) {
2429 /* current node */
2430 iter = dnodes->dnodes[depth - 1];
2431 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2432
2433 /* parent */
2434 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002435 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2436 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002437
2438 /* get module to print, if any */
2439 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2440 if (prev_mod == mod) {
2441 mod = NULL;
2442 }
2443
2444 /* realloc string */
2445 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2446 strlen(((struct lyd_node_opaq *)iter)->name.name));
2447 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2448 break;
2449 }
2450
2451 /* print next node */
2452 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2453
2454 /* do not always print the last (first) predicate */
2455 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2456 switch (iter->schema->nodetype) {
2457 case LYS_LIST:
2458 if (iter->schema->flags & LYS_KEYLESS) {
2459 /* print its position */
2460 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2461 } else {
2462 /* print all list keys in predicates */
2463 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2464 }
2465 break;
2466 case LYS_LEAFLIST:
2467 if (iter->schema->flags & LYS_CONFIG_W) {
2468 /* print leaf-list value */
2469 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2470 } else {
2471 /* print its position */
2472 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2473 }
2474 break;
2475 default:
2476 /* nothing to print more */
2477 break;
2478 }
2479 }
2480 if (rc) {
2481 break;
2482 }
2483 }
2484 break;
2485 }
2486
2487 return buffer;
2488}
2489
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002490LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002491lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2492{
2493 struct lyd_meta *ret = NULL;
2494 const struct ly_ctx *ctx;
2495 const char *prefix, *tmp;
2496 char *str;
2497 size_t pref_len, name_len;
2498
2499 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002500 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002501
2502 if (!first) {
2503 return NULL;
2504 }
2505
2506 ctx = first->annotation->module->ctx;
2507
2508 /* parse the name */
2509 tmp = name;
2510 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2511 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2512 return NULL;
2513 }
2514
2515 /* find the module */
2516 if (prefix) {
2517 str = strndup(prefix, pref_len);
2518 module = ly_ctx_get_module_latest(ctx, str);
2519 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002520 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 +02002521 }
2522
2523 /* find the metadata */
2524 LY_LIST_FOR(first, first) {
2525 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2526 ret = (struct lyd_meta *)first;
2527 break;
2528 }
2529 }
2530
2531 return ret;
2532}
2533
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002534LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002535lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2536{
Michal Vasko9beceb82022-04-05 12:14:15 +02002537 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002538 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002539 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002540
Michal Vaskof03ed032020-03-04 13:31:44 +01002541 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002542 if (!siblings) {
2543 /* no data */
2544 if (match) {
2545 *match = NULL;
2546 }
2547 return LY_ENOTFOUND;
2548 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002549
Michal Vasko9beceb82022-04-05 12:14:15 +02002550 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2551 /* create a duplicate in this context */
2552 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2553 target = dup;
2554 }
2555
2556 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2557 /* schema mismatch */
2558 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002559 if (match) {
2560 *match = NULL;
2561 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002562 return LY_ENOTFOUND;
2563 }
2564
Michal Vaskoe78faec2021-04-08 17:24:43 +02002565 /* get first sibling */
2566 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002567
Michal Vasko9e685082021-01-29 14:49:09 +01002568 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002569 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002570 assert(target->hash);
2571
Michal Vaskoe78faec2021-04-08 17:24:43 +02002572 if (lysc_is_dup_inst_list(target->schema)) {
2573 /* we must search the instances from beginning to find the first matching one */
2574 found = 0;
2575 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2576 if (!lyd_compare_single(target, iter, 0)) {
2577 found = 1;
2578 break;
2579 }
2580 }
2581 if (found) {
2582 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002583 } else {
2584 siblings = NULL;
2585 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002586 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002587 /* find by hash */
2588 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2589 siblings = *match_p;
2590 } else {
2591 /* not found */
2592 siblings = NULL;
2593 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002594 }
2595 } else {
2596 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002597 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002598 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002599 break;
2600 }
2601 }
2602 }
2603
Michal Vasko9beceb82022-04-05 12:14:15 +02002604 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002605 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002606 if (match) {
2607 *match = NULL;
2608 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002609 return LY_ENOTFOUND;
2610 }
2611
Michal Vasko9b368d32020-02-14 13:53:31 +01002612 if (match) {
2613 *match = (struct lyd_node *)siblings;
2614 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002615 return LY_SUCCESS;
2616}
2617
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002618LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002619lyd_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 +02002620 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002621{
2622 LY_ERR rc;
2623 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002624 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002625
Michal Vasko4c583e82020-07-17 12:16:14 +02002626 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002627 if (!siblings) {
2628 /* no data */
2629 if (match) {
2630 *match = NULL;
2631 }
2632 return LY_ENOTFOUND;
2633 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002634
Michal Vasko9beceb82022-04-05 12:14:15 +02002635 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2636 /* parent of ext nodes is useless */
2637 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
2638 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
2639 }
2640
2641 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2642 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002643 if (match) {
2644 *match = NULL;
2645 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002646 return LY_ENOTFOUND;
2647 }
2648
Michal Vaskof03ed032020-03-04 13:31:44 +01002649 if (key_or_value && !val_len) {
2650 val_len = strlen(key_or_value);
2651 }
2652
Michal Vaskob104f112020-07-17 09:54:54 +02002653 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2654 /* create a data node and find the instance */
2655 if (schema->nodetype == LYS_LEAFLIST) {
2656 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002657 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 +02002658 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002659 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002660 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002661 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002662 }
2663
2664 /* find it */
2665 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002666 } else {
2667 /* find the first schema node instance */
2668 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002669 }
2670
Michal Vaskoe444f752020-02-10 12:20:06 +01002671 lyd_free_tree(target);
2672 return rc;
2673}
Michal Vaskoccc02342020-05-21 10:09:21 +02002674
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002675LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002676lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2677{
2678 struct lyd_node **match_p, *first, *iter;
2679 struct lyd_node_inner *parent;
2680
Michal Vasko83ae7772022-06-08 10:01:55 +02002681 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002682 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002683
2684 LY_CHECK_RET(ly_set_new(set));
2685
2686 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2687 /* no data or schema mismatch */
2688 return LY_ENOTFOUND;
2689 }
2690
2691 /* get first sibling */
2692 siblings = lyd_first_sibling(siblings);
2693
2694 parent = siblings->parent;
2695 if (parent && parent->schema && parent->children_ht) {
2696 assert(target->hash);
2697
2698 /* find the first instance */
2699 lyd_find_sibling_first(siblings, target, &first);
2700 if (first) {
2701 /* add it so that it is the first in the set */
2702 if (ly_set_add(*set, first, 1, NULL)) {
2703 goto error;
2704 }
2705
2706 /* find by hash */
2707 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2708 iter = *match_p;
2709 } else {
2710 /* not found */
2711 iter = NULL;
2712 }
2713 while (iter) {
2714 /* add all found nodes into the set */
2715 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2716 goto error;
2717 }
2718
2719 /* find next instance */
2720 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2721 iter = NULL;
2722 } else {
2723 iter = *match_p;
2724 }
2725 }
2726 }
2727 } else {
2728 /* no children hash table */
2729 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002730 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002731 ly_set_add(*set, (void *)siblings, 1, NULL);
2732 }
2733 }
2734 }
2735
2736 if (!(*set)->count) {
2737 return LY_ENOTFOUND;
2738 }
2739 return LY_SUCCESS;
2740
2741error:
2742 ly_set_free(*set, NULL);
2743 *set = NULL;
2744 return LY_EMEM;
2745}
2746
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002747LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002748lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2749{
2750 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2751
2752 for ( ; first; first = first->next) {
2753 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2754 break;
2755 }
2756 }
2757
2758 if (match) {
2759 *match = (struct lyd_node *)first;
2760 }
2761 return first ? LY_SUCCESS : LY_ENOTFOUND;
2762}
2763
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002764LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002765lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2766 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002767{
2768 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002769 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002771 uint32_t i;
2772
Michal Vaskoddd76592022-01-17 13:34:48 +01002773 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002774
Michal Vasko37c69432021-04-12 14:48:24 +02002775 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002776
Michal Vaskoddd76592022-01-17 13:34:48 +01002777 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01002778 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002780
2781 /* evaluate expression */
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02002782 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2783 LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02002784 LY_CHECK_GOTO(ret, cleanup);
2785
Michal Vaskoc0d91642022-11-03 13:56:29 +01002786 if (xp_set.type != LYXP_SET_NODE_SET) {
2787 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2788 ret = LY_EINVAL;
2789 goto cleanup;
2790 }
2791
Michal Vaskoccc02342020-05-21 10:09:21 +02002792 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002793 ret = ly_set_new(set);
2794 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002795
Michal Vaskoc0d91642022-11-03 13:56:29 +01002796 /* transform into ly_set, allocate memory for all the elements once (even though not all items must be
2797 * elements but most likely will be) */
2798 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2799 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2800 (*set)->size = xp_set.used;
Michal Vaskoccc02342020-05-21 10:09:21 +02002801
Michal Vaskoc0d91642022-11-03 13:56:29 +01002802 for (i = 0; i < xp_set.used; ++i) {
2803 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2804 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
2805 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002806 }
2807 }
2808
2809cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002810 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002811 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01002812 if (ret) {
2813 ly_set_free(*set, NULL);
2814 *set = NULL;
2815 }
Michal Vaskoccc02342020-05-21 10:09:21 +02002816 return ret;
2817}
Radek Krejcica989142020-11-05 11:32:22 +01002818
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002819LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002820lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2821 const struct lyxp_var *vars, struct ly_set **set)
2822{
2823 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2824
2825 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2826}
2827
2828LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01002829lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2830{
2831 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2832
Michal Vaskoddd76592022-01-17 13:34:48 +01002833 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002834}
2835
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002836LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002837lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2838{
Michal Vaskoe3716b32021-12-13 16:58:25 +01002839 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2840
Michal Vaskoddd76592022-01-17 13:34:48 +01002841 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02002842}
2843
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002844LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002845lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2846 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002847{
2848 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002849 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002850 struct lyxp_expr *exp = NULL;
2851
2852 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
2853
2854 /* compile expression */
2855 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
2856 LY_CHECK_GOTO(ret, cleanup);
2857
2858 /* evaluate expression */
Michal Vasko10fabfc2022-08-09 08:55:43 +02002859 ret = lyxp_eval(LYD_CTX(ctx_node), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, ctx_node, vars, &xp_set,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02002860 LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002861 LY_CHECK_GOTO(ret, cleanup);
2862
2863 /* transform into boolean */
2864 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
2865 LY_CHECK_GOTO(ret, cleanup);
2866
2867 /* set result */
2868 *result = xp_set.val.bln;
2869
2870cleanup:
2871 lyxp_set_free_content(&xp_set);
2872 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
2873 return ret;
2874}
2875
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002876LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002877lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2878{
2879 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2880}
2881
2882LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002883lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
2884{
Michal Vasko10fabfc2022-08-09 08:55:43 +02002885 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
aPiecekfba75362021-10-07 12:39:48 +02002886}
2887
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002888LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002889lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2890{
2891 LY_ERR ret = LY_SUCCESS;
2892 struct lyxp_expr *expr = NULL;
2893 struct ly_path *lypath = NULL;
2894
2895 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2896
2897 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002898 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER,
Michal Vasko3e1f6552021-01-14 09:27:55 +01002899 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
2900 LY_CHECK_GOTO(ret, cleanup);
2901
2902 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002903 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002904 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 +01002905 LY_CHECK_GOTO(ret, cleanup);
2906
2907 /* evaluate the path */
2908 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
2909
2910cleanup:
2911 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2912 ly_path_free(LYD_CTX(ctx_node), lypath);
2913 return ret;
2914}
2915
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002916LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002917lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2918{
2919 LY_ERR ret;
2920 struct lyd_node *m;
2921
2922 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2923
2924 ret = ly_path_eval(path, tree, &m);
2925 if (ret) {
2926 if (match) {
2927 *match = NULL;
2928 }
2929 return LY_ENOTFOUND;
2930 }
2931
2932 if (match) {
2933 *match = m;
2934 }
2935 return LY_SUCCESS;
2936}