blob: a082d8acc8f3a50ed8ae59f33e522ff2ae3532da [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 Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Radek Krejci7931b192020-06-25 17:05:03 +020054static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020055lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020056{
Michal Vasko69730152020-10-09 16:30:07 +020057 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020058 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020059 const char *path = in->method.fpath.filepath;
60 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020061
62 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020063 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020064
Radek Krejcif13b87b2020-12-01 22:02:17 +010065 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
66 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020067 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010068 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
69 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020070 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010071 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
72 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020073 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020074 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020075 }
76
Radek Krejci7931b192020-06-25 17:05:03 +020077 return format;
78}
Radek Krejcie7b95092019-05-15 11:03:07 +020079
Michal Vaskoe0665742021-02-11 11:08:44 +010080/**
81 * @brief Parse YANG data into a data tree.
82 *
83 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010084 * @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 +010085 * @param[in] parent Parent to connect the parsed nodes to, if any.
86 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
87 * @param[in] in Input handle to read the input from.
88 * @param[in] format Expected format of the data in @p in.
89 * @param[in] parse_opts Options for parser.
90 * @param[in] val_opts Options for validation.
91 * @param[out] op Optional pointer to the parsed operation, if any.
92 * @return LY_ERR value.
93 */
94static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010095lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
96 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 +020097{
Michal Vaskoe0665742021-02-11 11:08:44 +010098 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +020099 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100100 struct ly_set parsed = {0};
101 struct lyd_node *first;
102 uint32_t i;
Michal Vaskoddd76592022-01-17 13:34:48 +0100103 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200104
Michal Vaskoe0665742021-02-11 11:08:44 +0100105 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200106
107 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100108 if (first_p) {
109 *first_p = NULL;
110 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200111
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 /* remember input position */
113 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200114
Michal Vaskoe0665742021-02-11 11:08:44 +0100115 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200116 switch (format) {
117 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100118 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed,
119 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200121 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100122 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
123 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200124 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200125 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100126 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
127 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200128 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200129 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100130 LOGARG(ctx, format);
131 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100132 break;
133 }
134 LY_CHECK_GOTO(rc, cleanup);
135
136 if (parent) {
137 /* get first top-level sibling */
138 for (first = parent; first->parent; first = lyd_parent(first)) {}
139 first = lyd_first_sibling(first);
140 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200141 }
142
Michal Vaskoe0665742021-02-11 11:08:44 +0100143 if (!(parse_opts & LYD_PARSE_ONLY)) {
144 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100145 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 +0200146 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100147 LY_CHECK_GOTO(rc, cleanup);
148 }
Radek Krejci7931b192020-06-25 17:05:03 +0200149
Michal Vaskoe0665742021-02-11 11:08:44 +0100150 /* set the operation node */
151 if (op) {
152 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200153 }
154
155cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100156 if (lydctx) {
157 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200158 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100159 if (rc) {
160 if (parent) {
161 /* free all the parsed subtrees */
162 for (i = 0; i < parsed.count; ++i) {
163 lyd_free_tree(parsed.dnodes[i]);
164 }
165 } else {
166 /* free everything */
167 lyd_free_all(*first_p);
168 *first_p = NULL;
169 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100170 } else if (subtree_sibling) {
171 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100172 }
173 ly_set_erase(&parsed, NULL);
174 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200175}
176
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100177LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100178lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
179 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
180{
181 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
182
183 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
184 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
185 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
186
187 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
188}
189
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100190LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100191lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
192 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
193{
194 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
195 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
196 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
197
Radek Krejcif16e2542021-02-17 15:39:23 +0100198 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100199}
200
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100201LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100202lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
203 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200204{
205 LY_ERR ret;
206 struct ly_in *in;
207
208 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100209 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200210
211 ly_in_free(in, 0);
212 return ret;
213}
214
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100215LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200216lyd_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 +0200217 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200218{
219 LY_ERR ret;
220 struct ly_in *in;
221
222 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100223 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200224
225 ly_in_free(in, 0);
226 return ret;
227}
228
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100229LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200230lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
231 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200232{
233 LY_ERR ret;
234 struct ly_in *in;
235
236 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100237 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200238
239 ly_in_free(in, 0);
240 return ret;
241}
242
Radek Krejcif16e2542021-02-17 15:39:23 +0100243/**
244 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
245 * for schema nodes locked inside the extension instance.
246 *
247 * At least one of @p parent, @p tree, or @p op must always be set.
248 *
249 * Specific @p data_type values have different parameter meaning as follows:
250 * - ::LYD_TYPE_RPC_NETCONF:
251 * - @p parent - must be NULL, the whole RPC is expected;
252 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
253 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
254 * a separate opaque data tree, even if the function fails, this may be returned;
255 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
256 *
257 * - ::LYD_TYPE_NOTIF_NETCONF:
258 * - @p parent - must be NULL, the whole notification is expected;
259 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
260 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
261 * a separate opaque data tree, even if the function fails, this may be returned;
262 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
263 *
264 * - ::LYD_TYPE_REPLY_NETCONF:
265 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
266 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
267 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
268 * a separate opaque data tree, even if the function fails, this may be returned;
269 * - @p op - must be NULL, the reply is appended to the RPC;
270 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
271 *
272 * @param[in] ctx libyang context.
273 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
274 * @param[in] parent Optional parent to connect the parsed nodes to.
275 * @param[in] in Input handle to read the input from.
276 * @param[in] format Expected format of the data in @p in.
277 * @param[in] data_type Expected operation to parse (@ref datatype).
278 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
279 * @param[out] op Optional parsed operation node.
280 * @return LY_ERR value.
281 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
282 */
283static LY_ERR
284lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
285 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 +0200286{
Michal Vaskoe0665742021-02-11 11:08:44 +0100287 LY_ERR rc = LY_SUCCESS;
288 struct lyd_ctx *lydctx = NULL;
289 struct ly_set parsed = {0};
290 struct lyd_node *first = NULL, *envp = NULL;
291 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200292
Michal Vasko27fb0262021-02-23 09:42:01 +0100293 if (!ctx) {
294 ctx = LYD_CTX(parent);
295 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200296 if (tree) {
297 *tree = NULL;
298 }
299 if (op) {
300 *op = NULL;
301 }
302
Radek Krejci7931b192020-06-25 17:05:03 +0200303 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200304
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 /* remember input position */
306 in->func_start = in->current;
307
Michal Vaskoe0665742021-02-11 11:08:44 +0100308 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100309 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100310 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100311 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
312 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
313 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100314 }
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
318 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200319 switch (format) {
320 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100321 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100322 if (rc && envp) {
323 /* special situation when the envelopes were parsed successfully */
324 if (tree) {
325 *tree = envp;
Michal Vasko73792a12022-05-10 09:28:45 +0200326 } else {
327 lyd_free_all(envp);
Michal Vasko299d5d12021-02-16 16:36:37 +0100328 }
Michal Vasko73792a12022-05-10 09:28:45 +0200329 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100330 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100331 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200332 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100333 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100334 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200335 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100336 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100337 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200338 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100339 LOGARG(ctx, format);
340 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200341 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200342 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100343 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200344
Michal Vaskoe0665742021-02-11 11:08:44 +0100345 /* set out params correctly */
346 if (tree) {
347 if (envp) {
348 /* special out param meaning */
349 *tree = envp;
350 } else {
351 *tree = parent ? NULL : first;
352 }
353 }
354 if (op) {
355 *op = lydctx->op_node;
356 }
357
358cleanup:
359 if (lydctx) {
360 lydctx->free(lydctx);
361 }
362 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200363 /* free all the parsed nodes */
364 if (parsed.count) {
365 i = parsed.count;
366 do {
367 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100368 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200369 } while (i);
370 }
371 if (tree && ((format != LYD_XML) || !envp)) {
372 *tree = NULL;
373 }
374 if (op) {
375 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100376 }
377 }
378 ly_set_erase(&parsed, NULL);
379 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200380}
Radek Krejci084289f2019-07-09 17:35:30 +0200381
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100382LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100383lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
384 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
385{
386 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
387
388 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
389}
390
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100391LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100392lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
393 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
394{
395 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
396
397 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
398
399 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
400}
401
Michal Vasko90932a92020-02-12 14:33:03 +0100402struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200403lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100404{
Michal Vaskob104f112020-07-17 09:54:54 +0200405 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100406 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200407 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100408 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100409
Michal Vaskob104f112020-07-17 09:54:54 +0200410 assert(new_node);
411
Michal Vasko9beceb82022-04-05 12:14:15 +0200412 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200413 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100414 return NULL;
415 }
416
Michal Vasko93b16062020-12-09 18:14:18 +0100417 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100418 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100419 getnext_opts = LYS_GETNEXT_OUTPUT;
420 }
421
Michal Vaskoa2756f02021-07-09 13:20:28 +0200422 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200423 /* find the anchor using hashes */
424 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100425 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200426 while (schema) {
427 /* keep trying to find the first existing instance of the closest following schema sibling,
428 * otherwise return NULL - inserting at the end */
429 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
430 break;
431 }
432
Michal Vasko93b16062020-12-09 18:14:18 +0100433 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200434 }
435 } else {
436 /* find the anchor without hashes */
437 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200438 sparent = lysc_data_parent(new_node->schema);
439 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200440 /* we are in top-level, skip all the data from preceding modules */
441 LY_LIST_FOR(match, match) {
442 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
443 break;
444 }
445 }
446 }
447
448 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100449 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200450
451 found = 0;
452 LY_LIST_FOR(match, match) {
453 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
454 /* we have found an opaque node, which must be at the end, so use it OR
455 * modules do not match, so we must have traversed all the data from new_node module (if any),
456 * we have found the first node of the next module, that is what we want */
457 break;
458 }
459
460 /* skip schema nodes until we find the instantiated one */
461 while (!found) {
462 if (new_node->schema == schema) {
463 /* we have found the schema of the new node, continue search to find the first
464 * data node with a different schema (after our schema) */
465 found = 1;
466 break;
467 }
468 if (match->schema == schema) {
469 /* current node (match) is a data node still before the new node, continue search in data */
470 break;
471 }
Michal Vasko93b16062020-12-09 18:14:18 +0100472 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200473 assert(schema);
474 }
475
476 if (found && (match->schema != new_node->schema)) {
477 /* find the next node after we have found our node schema data instance */
478 break;
479 }
480 }
Michal Vasko90932a92020-02-12 14:33:03 +0100481 }
482
483 return match;
484}
485
Michal Vaskoc61dd062022-06-07 11:01:28 +0200486void
Michal Vaskof03ed032020-03-04 13:31:44 +0100487lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100488{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100489 struct lyd_node_inner *par;
490
Michal Vasko90932a92020-02-12 14:33:03 +0100491 assert(!node->next && (node->prev == node));
492
493 node->next = sibling->next;
494 node->prev = sibling;
495 sibling->next = node;
496 if (node->next) {
497 /* sibling had a succeeding node */
498 node->next->prev = node;
499 } else {
500 /* sibling was last, find first sibling and change its prev */
501 if (sibling->parent) {
502 sibling = sibling->parent->child;
503 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200504 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100505 }
506 sibling->prev = node;
507 }
508 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100509
Michal Vasko9f96a052020-03-10 09:41:45 +0100510 for (par = node->parent; par; par = par->parent) {
511 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
512 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100513 par->flags &= ~LYD_DEFAULT;
514 }
515 }
Michal Vasko90932a92020-02-12 14:33:03 +0100516}
517
Michal Vaskoc61dd062022-06-07 11:01:28 +0200518void
Michal Vaskof03ed032020-03-04 13:31:44 +0100519lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100520{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100521 struct lyd_node_inner *par;
522
Michal Vasko90932a92020-02-12 14:33:03 +0100523 assert(!node->next && (node->prev == node));
524
525 node->next = sibling;
526 /* covers situation of sibling being first */
527 node->prev = sibling->prev;
528 sibling->prev = node;
529 if (node->prev->next) {
530 /* sibling had a preceding node */
531 node->prev->next = node;
532 } else if (sibling->parent) {
533 /* sibling was first and we must also change parent child pointer */
534 sibling->parent->child = node;
535 }
536 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100537
Michal Vasko9f96a052020-03-10 09:41:45 +0100538 for (par = node->parent; par; par = par->parent) {
539 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
540 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100541 par->flags &= ~LYD_DEFAULT;
542 }
543 }
Michal Vasko90932a92020-02-12 14:33:03 +0100544}
545
546/**
Michal Vaskob104f112020-07-17 09:54:54 +0200547 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100548 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100549 * Handles inserting into NP containers and key-less lists.
550 *
Michal Vasko90932a92020-02-12 14:33:03 +0100551 * @param[in] parent Parent to insert into.
552 * @param[in] node Node to insert.
553 */
554static void
Michal Vaskob104f112020-07-17 09:54:54 +0200555lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100556{
557 struct lyd_node_inner *par;
558
Radek Krejcia1c1e542020-09-29 16:06:52 +0200559 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100560 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100561
562 par = (struct lyd_node_inner *)parent;
563
Michal Vaskob104f112020-07-17 09:54:54 +0200564 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100565 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100566
Michal Vaskod989ba02020-08-24 10:59:24 +0200567 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100568 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
569 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100570 par->flags &= ~LYD_DEFAULT;
571 }
572 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200573}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100574
Michal Vasko751cb4d2020-07-14 12:25:28 +0200575/**
576 * @brief Learn whether a list instance has all the keys.
577 *
578 * @param[in] list List instance to check.
579 * @return non-zero if all the keys were found,
580 * @return 0 otherwise.
581 */
582static int
583lyd_insert_has_keys(const struct lyd_node *list)
584{
585 const struct lyd_node *key;
586 const struct lysc_node *skey = NULL;
587
588 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200589 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200590 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
591 if (!key || (key->schema != skey)) {
592 /* key missing */
593 return 0;
594 }
595
596 key = key->next;
597 }
598
599 /* all keys found */
600 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100601}
602
603void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200604lyd_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 +0100605{
Michal Vaskob104f112020-07-17 09:54:54 +0200606 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100607
Michal Vaskob104f112020-07-17 09:54:54 +0200608 /* inserting list without its keys is not supported */
609 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100610 assert(!parent || !parent->schema ||
611 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100612
Michal Vaskob104f112020-07-17 09:54:54 +0200613 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100614 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100615 }
Michal Vasko90932a92020-02-12 14:33:03 +0100616
Michal Vaskob104f112020-07-17 09:54:54 +0200617 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100618 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100619
Michal Vasko9beceb82022-04-05 12:14:15 +0200620 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200621 /* no next anchor */
622 anchor = NULL;
623 } else {
624 /* find the anchor, our next node, so we can insert before it */
625 anchor = lyd_insert_get_next_anchor(first_sibling, node);
626 }
627
Michal Vaskob104f112020-07-17 09:54:54 +0200628 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200629 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200630 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100631 if (!parent && (*first_sibling_p == anchor)) {
632 /* move first sibling */
633 *first_sibling_p = node;
634 }
Michal Vaskob104f112020-07-17 09:54:54 +0200635 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200636 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200637 lyd_insert_after_node(first_sibling->prev, node);
638 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200639 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200640 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100641 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200642 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200643 *first_sibling_p = node;
644 }
645
646 /* insert into parent HT */
647 lyd_insert_hash(node);
648
649 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200650 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200651 lyd_hash(parent);
652
653 /* now we can insert even the list into its parent HT */
654 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100655 }
Michal Vasko90932a92020-02-12 14:33:03 +0100656}
657
Michal Vasko717a4c32020-12-07 09:40:10 +0100658/**
659 * @brief Check schema place of a node to be inserted.
660 *
661 * @param[in] parent Schema node of the parent data node.
662 * @param[in] sibling Schema node of a sibling data node.
663 * @param[in] schema Schema node if the data node to be inserted.
664 * @return LY_SUCCESS on success.
665 * @return LY_EINVAL if the place is invalid.
666 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100667static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100668lyd_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 +0100669{
670 const struct lysc_node *par2;
671
Michal Vasko62ed12d2020-05-21 10:08:25 +0200672 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100673 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
674 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100675
Michal Vasko717a4c32020-12-07 09:40:10 +0100676 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100677 /* opaque nodes can be inserted wherever */
678 return LY_SUCCESS;
679 }
680
Michal Vasko717a4c32020-12-07 09:40:10 +0100681 if (!parent) {
682 parent = lysc_data_parent(sibling);
683 }
684
Michal Vaskof03ed032020-03-04 13:31:44 +0100685 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200686 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100687
688 if (parent) {
689 /* inner node */
690 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200691 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200692 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100693 return LY_EINVAL;
694 }
695 } else {
696 /* top-level node */
697 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200698 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100699 return LY_EINVAL;
700 }
701 }
702
703 return LY_SUCCESS;
704}
705
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100706LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200707lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100708{
709 struct lyd_node *iter;
710
Michal Vasko0ab974d2021-02-24 13:18:26 +0100711 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100712 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100713
Michal Vasko717a4c32020-12-07 09:40:10 +0100714 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100715
Michal Vasko0ab974d2021-02-24 13:18:26 +0100716 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100717 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100718 return LY_EINVAL;
719 }
720
721 if (node->parent || node->prev->next) {
722 lyd_unlink_tree(node);
723 }
724
725 while (node) {
726 iter = node->next;
727 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200728 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100729 node = iter;
730 }
731 return LY_SUCCESS;
732}
733
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100734LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200735lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100736{
737 struct lyd_node *iter;
738
739 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
740 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
741
742 if (first->schema && (first->schema->flags & LYS_KEY)) {
743 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
744 return LY_EINVAL;
745 }
746
747 while (first) {
748 iter = first->next;
749 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100750 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100751 first = iter;
752 }
753 return LY_SUCCESS;
754}
755
756LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200757lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100758{
759 struct lyd_node *iter;
760
Michal Vaskob104f112020-07-17 09:54:54 +0200761 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100762
Michal Vaskob104f112020-07-17 09:54:54 +0200763 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100764 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100765 }
766
Michal Vasko553d0b52020-12-04 16:27:52 +0100767 if (sibling == node) {
768 /* we need to keep the connection to siblings so we can insert into them */
769 sibling = sibling->prev;
770 }
771
Michal Vaskob1b5c262020-03-05 14:29:47 +0100772 if (node->parent || node->prev->next) {
773 lyd_unlink_tree(node);
774 }
775
776 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100777 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200778 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200779 return LY_EINVAL;
780 }
781
Michal Vaskob1b5c262020-03-05 14:29:47 +0100782 iter = node->next;
783 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200784 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100785 node = iter;
786 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100787
Michal Vaskob104f112020-07-17 09:54:54 +0200788 if (first) {
789 /* find the first sibling */
790 *first = sibling;
791 while ((*first)->prev->next) {
792 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100793 }
794 }
795
796 return LY_SUCCESS;
797}
798
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100799LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100800lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
801{
Michal Vaskobce230b2022-04-19 09:55:31 +0200802 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100803 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100804
Michal Vasko717a4c32020-12-07 09:40:10 +0100805 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100806
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200807 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200808 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100809 return LY_EINVAL;
810 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200811 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
812 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100813 return LY_EINVAL;
814 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100815
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100816 lyd_unlink_tree(node);
817 lyd_insert_before_node(sibling, node);
818 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100819
Michal Vaskof03ed032020-03-04 13:31:44 +0100820 return LY_SUCCESS;
821}
822
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100823LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100824lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
825{
Michal Vaskobce230b2022-04-19 09:55:31 +0200826 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100827 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100828
Michal Vasko717a4c32020-12-07 09:40:10 +0100829 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100830
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200831 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200832 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100833 return LY_EINVAL;
834 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200835 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
836 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100837 return LY_EINVAL;
838 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100839
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100840 lyd_unlink_tree(node);
841 lyd_insert_after_node(sibling, node);
842 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100843
Michal Vaskof03ed032020-03-04 13:31:44 +0100844 return LY_SUCCESS;
845}
846
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100847LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200848lyd_unlink_siblings(struct lyd_node *node)
849{
850 struct lyd_node *next, *elem, *first = NULL;
851
852 LY_LIST_FOR_SAFE(node, next, elem) {
853 lyd_unlink_tree(elem);
854 lyd_insert_node(NULL, &first, elem, 1);
855 }
856}
857
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100858LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100859lyd_unlink_tree(struct lyd_node *node)
860{
861 struct lyd_node *iter;
862
863 if (!node) {
864 return;
865 }
866
Michal Vaskob104f112020-07-17 09:54:54 +0200867 /* update hashes while still linked into the tree */
868 lyd_unlink_hash(node);
869
Michal Vaskof03ed032020-03-04 13:31:44 +0100870 /* unlink from siblings */
871 if (node->prev->next) {
872 node->prev->next = node->next;
873 }
874 if (node->next) {
875 node->next->prev = node->prev;
876 } else {
877 /* unlinking the last node */
878 if (node->parent) {
879 iter = node->parent->child;
880 } else {
881 iter = node->prev;
882 while (iter->prev != node) {
883 iter = iter->prev;
884 }
885 }
886 /* update the "last" pointer from the first node */
887 iter->prev = node->prev;
888 }
889
890 /* unlink from parent */
891 if (node->parent) {
892 if (node->parent->child == node) {
893 /* the node is the first child */
894 node->parent->child = node->next;
895 }
896
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200897 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +0200898 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
899 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200900 LY_LIST_FOR(node->parent->child, iter) {
901 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
902 break;
903 }
904 }
905 if (!iter) {
906 node->parent->flags |= LYD_DEFAULT;
907 }
908 }
909
Michal Vaskof03ed032020-03-04 13:31:44 +0100910 node->parent = NULL;
911 }
912
913 node->next = NULL;
914 node->prev = node;
915}
916
Michal Vaskoa5da3292020-08-12 13:10:50 +0200917void
Michal Vasko871a0252020-11-11 18:35:24 +0100918lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200919{
920 struct lyd_meta *last, *iter;
921
922 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200923
924 if (!meta) {
925 return;
926 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200927
928 for (iter = meta; iter; iter = iter->next) {
929 iter->parent = parent;
930 }
931
932 /* insert as the last attribute */
933 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200934 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200935 last->next = meta;
936 } else {
937 parent->meta = meta;
938 }
939
940 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100941 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200942 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100943 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200944 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200945}
946
947LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100948lyd_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 +0200949 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 +0100950 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 +0100951{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100952 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100953 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200954 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100955 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200956 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100957
Michal Vasko9f96a052020-03-10 09:41:45 +0100958 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100959
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200960 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200961 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200962 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100963 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200964 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100965 break;
966 }
967 }
968 if (!ant) {
969 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100970 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100971 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100972 ret = LY_EINVAL;
973 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +0100974 }
975
Michal Vasko9f96a052020-03-10 09:41:45 +0100976 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100977 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +0100978 mt->parent = parent;
979 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +0100980 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +0200981 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 +0100982 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100983 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
984 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
985 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100986
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100987 /* insert as the last attribute */
988 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +0100989 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +0100990 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200991 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +0100992 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +0100993 }
994
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 if (meta) {
996 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +0100997 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100998
999cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001000 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001001}
1002
Michal Vaskoa5da3292020-08-12 13:10:50 +02001003void
1004lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1005{
1006 struct lyd_attr *last, *iter;
1007 struct lyd_node_opaq *opaq;
1008
1009 assert(parent && !parent->schema);
1010
1011 if (!attr) {
1012 return;
1013 }
1014
1015 opaq = (struct lyd_node_opaq *)parent;
1016 for (iter = attr; iter; iter = iter->next) {
1017 iter->parent = opaq;
1018 }
1019
1020 /* insert as the last attribute */
1021 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001022 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001023 last->next = attr;
1024 } else {
1025 opaq->attr = attr;
1026 }
1027}
1028
Michal Vasko52927e22020-03-16 17:26:14 +01001029LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001030lyd_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 +01001031 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 +02001032 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 +01001033{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001034 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001035 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001036
1037 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001038 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001039
Michal Vasko2a3722d2021-06-16 11:52:39 +02001040 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001041 value = "";
1042 }
1043
1044 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001045 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001046
Michal Vaskoad92b672020-11-12 13:11:31 +01001047 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001048 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001049 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001050 }
1051 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001052 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001053 }
1054
Michal Vasko52927e22020-03-16 17:26:14 +01001055 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001056 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1057 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001058 *dynamic = 0;
1059 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001060 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001061 }
Michal Vasko501af032020-11-11 20:27:44 +01001062 at->format = format;
1063 at->val_prefix_data = val_prefix_data;
1064 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001065
1066 /* insert as the last attribute */
1067 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001068 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001069 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001070 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001071 last->next = at;
1072 }
1073
Radek Krejci011e4aa2020-09-04 15:22:31 +02001074finish:
1075 if (ret) {
1076 lyd_free_attr_single(ctx, at);
1077 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001078 *attr = at;
1079 }
1080 return LY_SUCCESS;
1081}
1082
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001083LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001084lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001085{
Michal Vasko004d3152020-06-11 19:59:22 +02001086 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001087
Michal Vasko004d3152020-06-11 19:59:22 +02001088 if (ly_path_eval(path, tree, &target)) {
1089 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001090 }
1091
Michal Vasko004d3152020-06-11 19:59:22 +02001092 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001093}
1094
aPiecek2f63f952021-03-30 12:22:18 +02001095/**
1096 * @brief Check the equality of the two schemas from different contexts.
1097 *
1098 * @param schema1 of first node.
1099 * @param schema2 of second node.
1100 * @return 1 if the schemas are equal otherwise 0.
1101 */
1102static ly_bool
1103lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1104{
1105 if (!schema1 && !schema2) {
1106 return 1;
1107 } else if (!schema1 || !schema2) {
1108 return 0;
1109 }
1110
1111 assert(schema1->module->ctx != schema2->module->ctx);
1112
1113 if (schema1->nodetype != schema2->nodetype) {
1114 return 0;
1115 }
1116
1117 if (strcmp(schema1->name, schema2->name)) {
1118 return 0;
1119 }
1120
1121 if (strcmp(schema1->module->name, schema2->module->name)) {
1122 return 0;
1123 }
1124
1125 if (schema1->module->revision || schema2->module->revision) {
1126 if (!schema1->module->revision || !schema2->module->revision) {
1127 return 0;
1128 }
1129 if (strcmp(schema1->module->revision, schema2->module->revision)) {
1130 return 0;
1131 }
1132 }
1133
1134 return 1;
1135}
1136
1137/**
1138 * @brief Check the equality of the schemas for all parent nodes.
1139 *
1140 * Both nodes must be from different contexts.
1141 *
1142 * @param node1 Data of first node.
1143 * @param node2 Data of second node.
1144 * @return 1 if the all related parental schemas are equal otherwise 0.
1145 */
1146static ly_bool
1147lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1148{
1149 const struct lysc_node *parent1, *parent2;
1150
1151 assert(node1 && node2);
1152
1153 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1154 parent1 && parent2;
1155 parent1 = parent1->parent, parent2 = parent2->parent) {
1156 if (!lyd_compare_schema_equal(parent1, parent2)) {
1157 return 0;
1158 }
1159 }
1160
1161 if (parent1 || parent2) {
1162 return 0;
1163 }
1164
1165 return 1;
1166}
1167
1168/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001169 * @brief Compare 2 nodes values including opaque node values.
1170 *
1171 * @param[in] node1 First node to compare.
1172 * @param[in] node2 Second node to compare.
1173 * @return LY_SUCCESS if equal.
1174 * @return LY_ENOT if not equal.
1175 * @return LY_ERR on error.
1176 */
1177static LY_ERR
1178lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1179{
1180 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1181 const char *val1, *val2, *col;
1182 const struct lys_module *mod;
1183 char *val_dyn = NULL;
1184 LY_ERR rc = LY_SUCCESS;
1185
1186 if (!node1->schema) {
1187 opaq1 = (struct lyd_node_opaq *)node1;
1188 }
1189 if (!node2->schema) {
1190 opaq2 = (struct lyd_node_opaq *)node2;
1191 }
1192
1193 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1194 /* opaque XML and opaque XML node */
1195 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1196 opaq2->val_prefix_data)) {
1197 return LY_ENOT;
1198 }
1199 return LY_SUCCESS;
1200 }
1201
1202 /* get their values */
1203 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1204 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1205 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1206 if (!mod) {
1207 /* unable to compare */
1208 return LY_ENOT;
1209 }
1210
1211 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1212 LOGMEM(LYD_CTX(node1));
1213 return LY_EMEM;
1214 }
1215 val1 = val_dyn;
1216 } else {
1217 val1 = lyd_get_value(node1);
1218 }
1219 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1220 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1221 if (!mod) {
1222 return LY_ENOT;
1223 }
1224
1225 assert(!val_dyn);
1226 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1227 LOGMEM(LYD_CTX(node2));
1228 return LY_EMEM;
1229 }
1230 val2 = val_dyn;
1231 } else {
1232 val2 = lyd_get_value(node2);
1233 }
1234
1235 /* compare values */
1236 if (strcmp(val1, val2)) {
1237 rc = LY_ENOT;
1238 }
1239
1240 free(val_dyn);
1241 return rc;
1242}
1243
1244/**
aPiecek2f63f952021-03-30 12:22:18 +02001245 * @brief Internal implementation of @ref lyd_compare_single.
1246 * @copydoc lyd_compare_single
1247 * @param[in] parental_schemas_checked Flag used for optimization.
1248 * When this function is called for the first time, the flag must be set to 0.
1249 * The @ref lyd_compare_schema_parents_equal should be called only once during
1250 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1251 */
1252static LY_ERR
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001253lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1254 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001255{
1256 const struct lyd_node *iter1, *iter2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001257 struct lyd_node_any *any1, *any2;
Michal Vaskof3c80a72022-08-17 10:22:04 +02001258 int len1, len2;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001259 LY_ERR r;
Radek Krejci084289f2019-07-09 17:35:30 +02001260
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001261 if (!node1 || !node2) {
1262 if (node1 == node2) {
1263 return LY_SUCCESS;
1264 } else {
1265 return LY_ENOT;
1266 }
1267 }
1268
aPiecek2f63f952021-03-30 12:22:18 +02001269 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1270 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001271 if (options & LYD_COMPARE_OPAQ) {
1272 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1273 return LY_ENOT;
1274 }
1275 } else {
1276 if (node1->schema != node2->schema) {
1277 return LY_ENOT;
1278 }
aPiecek2f63f952021-03-30 12:22:18 +02001279 }
1280 } else {
1281 /* different contexts */
1282 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1283 return LY_ENOT;
1284 }
1285 if (!parental_schemas_checked) {
1286 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1287 return LY_ENOT;
1288 }
1289 parental_schemas_checked = 1;
1290 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001291 }
1292
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001293 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001294 return LY_ENOT;
1295 }
aPiecek2f63f952021-03-30 12:22:18 +02001296 /* 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 +02001297
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001298 if (!node1->schema || !node2->schema) {
1299 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001300 return LY_ENOT;
1301 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001302 if ((r = lyd_compare_single_value(node1, node2))) {
1303 return r;
Michal Vasko52927e22020-03-16 17:26:14 +01001304 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001305
Michal Vasko52927e22020-03-16 17:26:14 +01001306 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001307 iter1 = lyd_child(node1);
1308 iter2 = lyd_child(node2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001309 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001310 }
1311 return LY_SUCCESS;
1312 } else {
1313 switch (node1->schema->nodetype) {
1314 case LYS_LEAF:
1315 case LYS_LEAFLIST:
1316 if (options & LYD_COMPARE_DEFAULTS) {
1317 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1318 return LY_ENOT;
1319 }
1320 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001321 if ((r = lyd_compare_single_value(node1, node2))) {
1322 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001323 }
1324
aPiecek2f63f952021-03-30 12:22:18 +02001325 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001326 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001327 case LYS_RPC:
1328 case LYS_ACTION:
1329 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001330 if (options & LYD_COMPARE_DEFAULTS) {
1331 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1332 return LY_ENOT;
1333 }
1334 }
1335 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001336 iter1 = lyd_child(node1);
1337 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001338 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001339 }
1340 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001341 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001342 iter1 = lyd_child(node1);
1343 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001344
1345 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1346 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001347 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001348 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001349 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001350 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001351 return LY_ENOT;
1352 }
1353 iter1 = iter1->next;
1354 iter2 = iter2->next;
1355 }
1356 } else {
1357 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1358
Radek Krejci0f969882020-08-21 16:56:47 +02001359all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001360 if (!iter1 && !iter2) {
1361 /* no children, nothing to compare */
1362 return LY_SUCCESS;
1363 }
1364
Michal Vaskod989ba02020-08-24 10:59:24 +02001365 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001366 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001367 return LY_ENOT;
1368 }
1369 }
1370 if (iter1 || iter2) {
1371 return LY_ENOT;
1372 }
1373 }
1374 return LY_SUCCESS;
1375 case LYS_ANYXML:
1376 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001377 any1 = (struct lyd_node_any *)node1;
1378 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001379
1380 if (any1->value_type != any2->value_type) {
1381 return LY_ENOT;
1382 }
1383 switch (any1->value_type) {
1384 case LYD_ANYDATA_DATATREE:
1385 iter1 = any1->value.tree;
1386 iter2 = any2->value.tree;
1387 goto all_children_compare;
1388 case LYD_ANYDATA_STRING:
1389 case LYD_ANYDATA_XML:
1390 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001391 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1392 return LY_ENOT;
1393 } else if (!any1->value.str && !any2->value.str) {
1394 return LY_SUCCESS;
1395 }
Michal Vasko52927e22020-03-16 17:26:14 +01001396 len1 = strlen(any1->value.str);
1397 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001398 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001399 return LY_ENOT;
1400 }
1401 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001402 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001403 len1 = lyd_lyb_data_length(any1->value.mem);
1404 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001405 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001406 return LY_ENOT;
1407 }
1408 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001409 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001410 }
1411 }
1412
Michal Vaskob7be7a82020-08-20 09:09:04 +02001413 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001414 return LY_EINT;
1415}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001416
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001417LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001418lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1419{
1420 return lyd_compare_single_(node1, node2, options, 0);
1421}
1422
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001423LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001424lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001425{
Michal Vaskod989ba02020-08-24 10:59:24 +02001426 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001427 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1428 }
1429
Michal Vasko11a81432020-07-28 16:31:29 +02001430 if (node1 == node2) {
1431 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001432 }
Michal Vasko11a81432020-07-28 16:31:29 +02001433 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001434}
1435
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001436LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001437lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1438{
1439 if (!meta1 || !meta2) {
1440 if (meta1 == meta2) {
1441 return LY_SUCCESS;
1442 } else {
1443 return LY_ENOT;
1444 }
1445 }
1446
Michal Vaskoa8083062020-11-06 17:22:10 +01001447 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001448 return LY_ENOT;
1449 }
1450
Michal Vasko21725742020-06-29 11:49:25 +02001451 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1452}
1453
Radek Krejci22ebdba2019-07-25 13:59:43 +02001454/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001455 * @brief Create a copy of the attribute.
1456 *
1457 * @param[in] attr Attribute to copy.
1458 * @param[in] node Opaque where to append the new attribute.
1459 * @param[out] dup Optional created attribute copy.
1460 * @return LY_ERR value.
1461 */
1462static LY_ERR
1463lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1464{
1465 LY_ERR ret = LY_SUCCESS;
1466 struct lyd_attr *a, *last;
1467 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1468
1469 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1470
1471 /* create a copy */
1472 a = calloc(1, sizeof *attr);
1473 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1474
1475 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1476 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1477 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1478 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1479 a->hints = attr->hints;
1480 a->format = attr->format;
1481 if (attr->val_prefix_data) {
1482 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1483 LY_CHECK_GOTO(ret, finish);
1484 }
1485
1486 /* insert as the last attribute */
1487 a->parent = opaq;
1488 if (opaq->attr) {
1489 for (last = opaq->attr; last->next; last = last->next) {}
1490 last->next = a;
1491 } else {
1492 opaq->attr = a;
1493 }
1494
1495finish:
1496 if (ret) {
1497 lyd_free_attr_single(LYD_CTX(node), a);
1498 } else if (dup) {
1499 *dup = a;
1500 }
1501 return LY_SUCCESS;
1502}
1503
1504/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001505 * @brief Find @p schema equivalent in @p trg_ctx.
1506 *
1507 * @param[in] schema Schema node to find.
1508 * @param[in] trg_ctx Target context to search in.
1509 * @param[in] parent Data parent of @p schema, if any.
1510 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1511 * @return LY_RRR value.
1512 */
1513static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02001514lyd_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 +01001515 const struct lysc_node **trg_schema)
1516{
Michal Vasko9beceb82022-04-05 12:14:15 +02001517 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1518 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001519 char *path;
1520
1521 if (!schema) {
1522 /* opaque node */
1523 *trg_schema = NULL;
1524 return LY_SUCCESS;
1525 }
1526
Michal Vasko9beceb82022-04-05 12:14:15 +02001527 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001528 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001529 trg_parent = parent->schema;
1530 src_parent = lysc_data_parent(schema);
1531 }
1532
1533 do {
1534 /* find the next parent */
1535 sp = schema;
1536 while (sp->parent != src_parent) {
1537 sp = sp->parent;
1538 }
1539 src_parent = sp;
1540
1541 if (!src_parent->parent) {
1542 /* find the module first */
1543 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1544 if (!trg_mod) {
1545 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1546 src_parent->module->name);
1547 return LY_ENOTFOUND;
1548 }
1549 }
1550
1551 /* find the next parent */
1552 assert(trg_parent || trg_mod);
1553 tp = NULL;
1554 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1555 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1556 break;
1557 }
1558 }
1559 if (!tp) {
1560 /* schema node not found */
1561 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1562 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1563 free(path);
Michal Vaskoddd76592022-01-17 13:34:48 +01001564 return LY_ENOTFOUND;
1565 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001566
Michal Vasko9beceb82022-04-05 12:14:15 +02001567 trg_parent = tp;
1568 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001569
Michal Vasko9beceb82022-04-05 12:14:15 +02001570 /* success */
1571 *trg_schema = trg_parent;
1572 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001573}
1574
1575/**
Michal Vasko52927e22020-03-16 17:26:14 +01001576 * @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 +02001577 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001578 * 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 +02001579 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001580 * @param[in] node Node to duplicate.
1581 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001582 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001583 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1584 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001585 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1586 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001587 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1588 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001589 */
Michal Vasko52927e22020-03-16 17:26:14 +01001590static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001591lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1592 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001593{
Michal Vasko52927e22020-03-16 17:26:14 +01001594 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001595 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001596 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001597 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001598 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001599 const struct lysc_type *type;
1600 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001601
Michal Vasko52927e22020-03-16 17:26:14 +01001602 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001603
Michal Vasko19175b62022-04-01 09:17:07 +02001604 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001605 if (options & LYD_DUP_NO_EXT) {
1606 /* no not duplicate this subtree */
1607 return LY_SUCCESS;
1608 }
1609
Michal Vasko19175b62022-04-01 09:17:07 +02001610 /* we need to use the same context */
1611 trg_ctx = LYD_CTX(node);
1612 }
1613
Michal Vasko52927e22020-03-16 17:26:14 +01001614 if (!node->schema) {
1615 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001616 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001617 } else {
1618 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001619 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001620 case LYS_ACTION:
1621 case LYS_NOTIF:
1622 case LYS_CONTAINER:
1623 case LYS_LIST:
1624 dup = calloc(1, sizeof(struct lyd_node_inner));
1625 break;
1626 case LYS_LEAF:
1627 case LYS_LEAFLIST:
1628 dup = calloc(1, sizeof(struct lyd_node_term));
1629 break;
1630 case LYS_ANYDATA:
1631 case LYS_ANYXML:
1632 dup = calloc(1, sizeof(struct lyd_node_any));
1633 break;
1634 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001635 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001636 ret = LY_EINT;
1637 goto error;
1638 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001639 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001640 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001641
Michal Vaskof6df0a02020-06-16 13:08:34 +02001642 if (options & LYD_DUP_WITH_FLAGS) {
1643 dup->flags = node->flags;
1644 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001645 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001646 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001647 if (trg_ctx == LYD_CTX(node)) {
1648 dup->schema = node->schema;
1649 } else {
Michal Vasko27f536f2022-04-01 09:17:30 +02001650 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
1651 if (ret) {
1652 /* has no schema but is not an opaque node */
1653 free(dup);
1654 dup = NULL;
1655 goto error;
1656 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001657 }
Michal Vasko52927e22020-03-16 17:26:14 +01001658 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001659
Michal Vasko9cf62422021-07-01 13:29:32 +02001660 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001661 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001662 if (!node->schema) {
1663 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1664 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1665 }
1666 } else {
1667 LY_LIST_FOR(node->meta, meta) {
1668 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1669 }
Michal Vasko25a32822020-07-09 15:48:22 +02001670 }
1671 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001672
1673 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001674 if (!dup->schema) {
1675 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1676 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1677 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001678
1679 if (options & LYD_DUP_RECURSIVE) {
1680 /* duplicate all the children */
1681 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001682 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001683 }
1684 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001685 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1686 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1687 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1688 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001689 opaq->hints = orig->hints;
1690 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001691 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001692 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001693 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001694 }
Michal Vasko52927e22020-03-16 17:26:14 +01001695 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1696 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1697 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1698
1699 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001700 if (trg_ctx == LYD_CTX(node)) {
1701 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1702 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1703 } else {
1704 /* store canonical value in the target context */
1705 val_can = lyd_get_value(node);
1706 type = ((struct lysc_node_leaf *)term->schema)->type;
1707 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1708 LYD_HINT_DATA, term->schema, NULL);
1709 LY_CHECK_GOTO(ret, error);
1710 }
Michal Vasko52927e22020-03-16 17:26:14 +01001711 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1712 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1713 struct lyd_node *child;
1714
1715 if (options & LYD_DUP_RECURSIVE) {
1716 /* duplicate all the children */
1717 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001718 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001719 }
Michal Vasko69730152020-10-09 16:30:07 +02001720 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001721 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001722 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001723 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001724 }
1725 }
1726 lyd_hash(dup);
1727 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001728 dup->hash = node->hash;
1729 any = (struct lyd_node_any *)node;
1730 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001731 }
1732
Michal Vasko52927e22020-03-16 17:26:14 +01001733 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001734 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001735
1736 if (dup_p) {
1737 *dup_p = dup;
1738 }
1739 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001740
1741error:
Michal Vasko52927e22020-03-16 17:26:14 +01001742 lyd_free_tree(dup);
1743 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001744}
1745
Michal Vasko29d674b2021-08-25 11:18:35 +02001746/**
1747 * @brief Get a parent node to connect duplicated subtree to.
1748 *
1749 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001750 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001751 * @param[in] parent Initial parent to connect to.
1752 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1753 * @param[out] dup_parent First duplicated parent node, if any.
1754 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1755 * @return LY_ERR value.
1756 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001757static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001758lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1759 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001760{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001761 const struct lyd_node_inner *orig_parent, *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001762 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001763
1764 *dup_parent = NULL;
1765 *local_parent = NULL;
1766
Michal Vasko83522192022-07-20 08:07:34 +02001767 if (node->flags & LYD_EXT) {
1768 ext_parent = 1;
1769 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001770 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
Michal Vasko83522192022-07-20 08:07:34 +02001771 if (ext_parent) {
1772 /* use the standard context */
1773 trg_ctx = LYD_CTX(orig_parent);
1774 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001775 if (parent && (parent->schema == orig_parent->schema)) {
1776 /* stop creating parents, connect what we have into the provided parent */
1777 iter = parent;
1778 repeat = 0;
1779 } else {
1780 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001781 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 +02001782 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001783 }
1784 if (!*local_parent) {
1785 *local_parent = (struct lyd_node_inner *)iter;
1786 }
1787 if (iter->child) {
1788 /* 1) list - add after keys
1789 * 2) provided parent with some children */
1790 iter->child->prev->next = *dup_parent;
1791 if (*dup_parent) {
1792 (*dup_parent)->prev = iter->child->prev;
1793 iter->child->prev = *dup_parent;
1794 }
1795 } else {
1796 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1797 }
1798 if (*dup_parent) {
1799 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1800 }
1801 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001802 if (orig_parent->flags & LYD_EXT) {
1803 ext_parent = 1;
1804 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001805 }
1806
1807 if (repeat && parent) {
1808 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001809 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001810 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001811 return LY_EINVAL;
1812 }
1813
1814 return LY_SUCCESS;
1815}
1816
1817static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001818lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1819 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001820{
1821 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001822 const struct lyd_node *orig; /* original node to be duplicated */
1823 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001824 struct lyd_node *top = NULL; /* the most higher created node */
1825 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001826
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001827 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001828
1829 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001830 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 +02001831 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001832 } else {
1833 local_parent = parent;
1834 }
1835
Radek Krejci22ebdba2019-07-25 13:59:43 +02001836 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001837 if (lysc_is_key(orig->schema)) {
1838 if (local_parent) {
1839 /* the key must already exist in the parent */
1840 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001841 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001842 } else {
1843 assert(!(options & LYD_DUP_WITH_PARENTS));
1844 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001845 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001846 LY_CHECK_GOTO(rc, error);
1847 }
1848 } else {
1849 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001850 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1851 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001852 LY_CHECK_GOTO(rc, error);
1853 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001854 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001855 break;
1856 }
1857 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001858
Michal Vasko3a41dff2020-07-15 14:30:28 +02001859 if (dup) {
1860 *dup = first;
1861 }
1862 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001863
1864error:
1865 if (top) {
1866 lyd_free_tree(top);
1867 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001868 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001869 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001870 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001871}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001872
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001873/**
1874 * @brief Check the context of node and parent when duplicating nodes.
1875 *
1876 * @param[in] node Node to duplicate.
1877 * @param[in] parent Parent of the duplicated node(s).
1878 * @return LY_ERR value.
1879 */
1880static LY_ERR
1881lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1882{
1883 const struct lyd_node *iter;
1884
1885 if (!node || !parent) {
1886 return LY_SUCCESS;
1887 }
1888
1889 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1890 /* try to find top-level ext data parent */
1891 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1892
1893 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
1894 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1895 return LY_EINVAL;
1896 }
1897 }
1898
1899 return LY_SUCCESS;
1900}
1901
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001902LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001903lyd_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 +02001904{
Michal Vaskoddd76592022-01-17 13:34:48 +01001905 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001906 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001907
1908 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1909}
1910
1911LIBYANG_API_DEF LY_ERR
1912lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1913 uint32_t options, struct lyd_node **dup)
1914{
1915 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1916
1917 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001918}
1919
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001920LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001921lyd_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 +02001922{
Michal Vaskoddd76592022-01-17 13:34:48 +01001923 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001924 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001925
1926 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1927}
1928
1929LIBYANG_API_DEF LY_ERR
1930lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1931 uint32_t options, struct lyd_node **dup)
1932{
1933 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1934
1935 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001936}
1937
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001938LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001939lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001940{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001941 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02001942 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02001943 struct lyd_meta *mt, *last;
1944
Michal Vasko3a41dff2020-07-15 14:30:28 +02001945 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001946
Michal Vasko33c48972022-07-20 10:28:07 +02001947 /* log to node context but value must always use the annotation context */
1948 ctx = meta->annotation->module->ctx;
1949
Michal Vasko25a32822020-07-09 15:48:22 +02001950 /* create a copy */
1951 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001952 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001953 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02001954 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001955 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02001956 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001957
1958 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001959 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001960 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001961 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001962 last->next = mt;
1963 } else {
1964 node->meta = mt;
1965 }
1966
Radek Krejci011e4aa2020-09-04 15:22:31 +02001967finish:
1968 if (ret) {
1969 lyd_free_meta_single(mt);
1970 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001971 *dup = mt;
1972 }
1973 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001974}
1975
Michal Vasko4490d312020-06-16 13:08:55 +02001976/**
1977 * @brief Merge a source sibling into target siblings.
1978 *
1979 * @param[in,out] first_trg First target sibling, is updated if top-level.
1980 * @param[in] parent_trg Target parent.
1981 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001982 * @param[in] merge_cb Optional merge callback.
1983 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02001984 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001985 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02001986 * @return LY_ERR value.
1987 */
1988static LY_ERR
1989lyd_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 +02001990 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02001991{
Michal Vasko4490d312020-06-16 13:08:55 +02001992 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02001993 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01001994 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02001995 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02001996 struct lyd_dup_inst *child_dup_inst = NULL;
1997 LY_ERR ret;
1998 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02001999
2000 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002001 if (!sibling_src->schema) {
2002 /* try to find the same opaque node */
2003 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2004 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002005 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002006 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002007 } else {
2008 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002009 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002010 }
2011
Michal Vaskocd3f6172021-05-18 16:14:50 +02002012 if (match_trg) {
2013 /* update match as needed */
2014 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2015 } else {
2016 /* first instance of this node */
2017 first_inst = 1;
2018 }
2019
2020 if (match_trg) {
2021 /* call callback */
2022 if (merge_cb) {
2023 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2024 }
2025
Michal Vasko4490d312020-06-16 13:08:55 +02002026 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002027 if (!match_trg->schema) {
2028 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2029 /* update value */
2030 opaq_trg = (struct lyd_node_opaq *)match_trg;
2031 opaq_src = (struct lyd_node_opaq *)sibling_src;
2032
2033 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2034 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2035 opaq_trg->hints = opaq_src->hints;
2036
2037 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2038 opaq_trg->format = opaq_src->format;
2039 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2040 &opaq_trg->val_prefix_data);
2041 }
2042 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2043 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002044 /* since they are different, they cannot both be default */
2045 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2046
Michal Vasko3a41dff2020-07-15 14:30:28 +02002047 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2048 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002049 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002050 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2051 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002052 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002053
2054 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002055 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002056 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002057 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002058 /* update value */
2059 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002060 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002061
2062 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002063 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002064 }
2065
2066 /* check descendants, recursively */
2067 ret = LY_SUCCESS;
2068 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2069 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2070 &child_dup_inst);
2071 if (ret) {
2072 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002073 }
2074 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002075 lyd_dup_inst_free(child_dup_inst);
2076 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002077 } else {
2078 /* node not found, merge it */
2079 if (options & LYD_MERGE_DESTRUCT) {
2080 dup_src = (struct lyd_node *)sibling_src;
2081 lyd_unlink_tree(dup_src);
2082 /* spend it */
2083 *sibling_src_p = NULL;
2084 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002085 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 +02002086 }
2087
Michal Vasko7e8315b2021-08-03 17:01:06 +02002088 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2089 /* set LYD_NEW for all the new nodes, required for validation */
2090 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2091 elem->flags |= LYD_NEW;
2092 LYD_TREE_DFS_END(dup_src, elem);
2093 }
Michal Vasko4490d312020-06-16 13:08:55 +02002094 }
2095
Michal Vaskocd3f6172021-05-18 16:14:50 +02002096 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002097 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002098
2099 if (first_inst) {
2100 /* remember not to find this instance next time */
2101 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2102 }
2103
2104 /* call callback, no source node */
2105 if (merge_cb) {
2106 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2107 }
Michal Vasko4490d312020-06-16 13:08:55 +02002108 }
2109
2110 return LY_SUCCESS;
2111}
2112
Michal Vasko3a41dff2020-07-15 14:30:28 +02002113static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002114lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2115 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002116{
2117 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002118 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002119 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002120 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002121
2122 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002123 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2124 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002125
2126 if (!source) {
2127 /* nothing to merge */
2128 return LY_SUCCESS;
2129 }
2130
Michal Vasko831422b2020-11-24 11:20:51 +01002131 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002132 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002133 return LY_EINVAL;
2134 }
2135
2136 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002137 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2138 /* skip data nodes from different modules */
2139 continue;
2140 }
2141
Radek Krejci857189e2020-09-01 13:26:36 +02002142 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002143 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2144 if (ret) {
2145 break;
2146 }
Michal Vasko4490d312020-06-16 13:08:55 +02002147 if (first && !sibling_src) {
2148 /* source was spent (unlinked), move to the next node */
2149 source = tmp;
2150 }
2151
Michal Vasko3a41dff2020-07-15 14:30:28 +02002152 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002153 break;
2154 }
2155 }
2156
2157 if (options & LYD_MERGE_DESTRUCT) {
2158 /* free any leftover source data that were not merged */
2159 lyd_free_siblings((struct lyd_node *)source);
2160 }
2161
Michal Vaskocd3f6172021-05-18 16:14:50 +02002162 lyd_dup_inst_free(dup_inst);
2163 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002164}
2165
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002166LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002167lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002168{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002169 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002170}
2171
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002172LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002173lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002174{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002175 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2176}
2177
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002178LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002179lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2180 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2181{
2182 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002183}
2184
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002185static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002186lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002187{
Michal Vasko14654712020-02-06 08:35:21 +01002188 /* ending \0 */
2189 ++reqlen;
2190
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002191 if (reqlen > *buflen) {
2192 if (is_static) {
2193 return LY_EINCOMPLETE;
2194 }
2195
2196 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2197 if (!*buffer) {
2198 return LY_EMEM;
2199 }
2200
2201 *buflen = reqlen;
2202 }
2203
2204 return LY_SUCCESS;
2205}
2206
Michal Vaskod59035b2020-07-08 12:00:06 +02002207LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002208lyd_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 +02002209{
2210 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002211 size_t len;
2212 const char *val;
2213 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002214
Michal Vasko824d0832021-11-04 15:36:51 +01002215 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002216 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002217 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002218 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002219
2220 quot = '\'';
2221 if (strchr(val, '\'')) {
2222 quot = '"';
2223 }
2224 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002225 }
2226
2227 return LY_SUCCESS;
2228}
2229
2230/**
2231 * @brief Append leaf-list value predicate to path.
2232 *
2233 * @param[in] node Node to print.
2234 * @param[in,out] buffer Buffer to print to.
2235 * @param[in,out] buflen Current buffer length.
2236 * @param[in,out] bufused Current number of characters used in @p buffer.
2237 * @param[in] is_static Whether buffer is static or can be reallocated.
2238 * @return LY_ERR
2239 */
2240static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002241lyd_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 +02002242{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002243 size_t len;
2244 const char *val;
2245 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002246
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002247 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002248 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
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, "[.=%c%s%c]", quot, val, quot);
2256
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002257 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002258}
2259
2260/**
2261 * @brief Append node position (relative to its other instances) predicate to path.
2262 *
2263 * @param[in] node Node to print.
2264 * @param[in,out] buffer Buffer to print to.
2265 * @param[in,out] buflen Current buffer length.
2266 * @param[in,out] bufused Current number of characters used in @p buffer.
2267 * @param[in] is_static Whether buffer is static or can be reallocated.
2268 * @return LY_ERR
2269 */
2270static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002271lyd_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 +02002272{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002273 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002274 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002275 char *val = NULL;
2276 LY_ERR rc;
2277
Michal Vasko50cc0562021-05-18 16:15:43 +02002278 pos = lyd_list_pos(node);
2279 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002280 return LY_EMEM;
2281 }
2282
2283 len = 1 + strlen(val) + 1;
2284 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2285 if (rc != LY_SUCCESS) {
2286 goto cleanup;
2287 }
2288
2289 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2290
2291cleanup:
2292 free(val);
2293 return rc;
2294}
2295
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002296LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002297lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2298{
Radek Krejci857189e2020-09-01 13:26:36 +02002299 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002300 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002301 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002302 const struct lyd_node *iter, *parent;
2303 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002304 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002305
2306 LY_CHECK_ARG_RET(NULL, node, NULL);
2307 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002308 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002309 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002310 } else {
2311 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002312 }
2313
2314 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002315 case LYD_PATH_STD:
2316 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002317 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002318 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002319 ++depth;
2320 }
2321
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002322 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002323 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002324 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002325 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002326iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002327 /* get the module */
2328 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2329 parent = lyd_parent(iter);
2330 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2331 if (prev_mod == mod) {
2332 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002333 }
2334
2335 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002336 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2337 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002338 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2339 if (rc != LY_SUCCESS) {
2340 break;
2341 }
2342
2343 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002344 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002345
Michal Vasko790b2bc2020-08-03 13:35:06 +02002346 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002347 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002348 switch (iter->schema->nodetype) {
2349 case LYS_LIST:
2350 if (iter->schema->flags & LYS_KEYLESS) {
2351 /* print its position */
2352 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2353 } else {
2354 /* print all list keys in predicates */
2355 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2356 }
2357 break;
2358 case LYS_LEAFLIST:
2359 if (iter->schema->flags & LYS_CONFIG_W) {
2360 /* print leaf-list value */
2361 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2362 } else {
2363 /* print its position */
2364 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2365 }
2366 break;
2367 default:
2368 /* nothing to print more */
2369 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002370 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002371 }
2372 if (rc != LY_SUCCESS) {
2373 break;
2374 }
2375
Michal Vasko14654712020-02-06 08:35:21 +01002376 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002377 }
2378 break;
2379 }
2380
2381 return buffer;
2382}
Michal Vaskoe444f752020-02-10 12:20:06 +01002383
Michal Vaskodbf3e652022-10-21 08:46:25 +02002384char *
2385lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2386{
2387 uint32_t depth;
2388 size_t bufused = 0, buflen = 0, len;
2389 char *buffer = NULL;
2390 const struct lyd_node *iter, *parent;
2391 const struct lys_module *mod, *prev_mod;
2392 LY_ERR rc = LY_SUCCESS;
2393
2394 switch (pathtype) {
2395 case LYD_PATH_STD:
2396 case LYD_PATH_STD_NO_LAST_PRED:
2397 for (depth = 1; depth <= dnodes->count; ++depth) {
2398 /* current node */
2399 iter = dnodes->dnodes[depth - 1];
2400 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2401
2402 /* parent */
2403 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002404 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2405 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002406
2407 /* get module to print, if any */
2408 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2409 if (prev_mod == mod) {
2410 mod = NULL;
2411 }
2412
2413 /* realloc string */
2414 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2415 strlen(((struct lyd_node_opaq *)iter)->name.name));
2416 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2417 break;
2418 }
2419
2420 /* print next node */
2421 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2422
2423 /* do not always print the last (first) predicate */
2424 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2425 switch (iter->schema->nodetype) {
2426 case LYS_LIST:
2427 if (iter->schema->flags & LYS_KEYLESS) {
2428 /* print its position */
2429 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2430 } else {
2431 /* print all list keys in predicates */
2432 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2433 }
2434 break;
2435 case LYS_LEAFLIST:
2436 if (iter->schema->flags & LYS_CONFIG_W) {
2437 /* print leaf-list value */
2438 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2439 } else {
2440 /* print its position */
2441 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2442 }
2443 break;
2444 default:
2445 /* nothing to print more */
2446 break;
2447 }
2448 }
2449 if (rc) {
2450 break;
2451 }
2452 }
2453 break;
2454 }
2455
2456 return buffer;
2457}
2458
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002459LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002460lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2461{
2462 struct lyd_meta *ret = NULL;
2463 const struct ly_ctx *ctx;
2464 const char *prefix, *tmp;
2465 char *str;
2466 size_t pref_len, name_len;
2467
2468 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002469 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002470
2471 if (!first) {
2472 return NULL;
2473 }
2474
2475 ctx = first->annotation->module->ctx;
2476
2477 /* parse the name */
2478 tmp = name;
2479 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2480 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2481 return NULL;
2482 }
2483
2484 /* find the module */
2485 if (prefix) {
2486 str = strndup(prefix, pref_len);
2487 module = ly_ctx_get_module_latest(ctx, str);
2488 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002489 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 +02002490 }
2491
2492 /* find the metadata */
2493 LY_LIST_FOR(first, first) {
2494 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2495 ret = (struct lyd_meta *)first;
2496 break;
2497 }
2498 }
2499
2500 return ret;
2501}
2502
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002503LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002504lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2505{
Michal Vasko9beceb82022-04-05 12:14:15 +02002506 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002507 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002508 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002509
Michal Vaskof03ed032020-03-04 13:31:44 +01002510 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002511 if (!siblings) {
2512 /* no data */
2513 if (match) {
2514 *match = NULL;
2515 }
2516 return LY_ENOTFOUND;
2517 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002518
Michal Vasko9beceb82022-04-05 12:14:15 +02002519 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2520 /* create a duplicate in this context */
2521 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2522 target = dup;
2523 }
2524
2525 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2526 /* schema mismatch */
2527 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002528 if (match) {
2529 *match = NULL;
2530 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002531 return LY_ENOTFOUND;
2532 }
2533
Michal Vaskoe78faec2021-04-08 17:24:43 +02002534 /* get first sibling */
2535 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002536
Michal Vasko9e685082021-01-29 14:49:09 +01002537 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002538 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002539 assert(target->hash);
2540
Michal Vaskoe78faec2021-04-08 17:24:43 +02002541 if (lysc_is_dup_inst_list(target->schema)) {
2542 /* we must search the instances from beginning to find the first matching one */
2543 found = 0;
2544 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2545 if (!lyd_compare_single(target, iter, 0)) {
2546 found = 1;
2547 break;
2548 }
2549 }
2550 if (found) {
2551 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002552 } else {
2553 siblings = NULL;
2554 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002555 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002556 /* find by hash */
2557 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2558 siblings = *match_p;
2559 } else {
2560 /* not found */
2561 siblings = NULL;
2562 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002563 }
2564 } else {
2565 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002566 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002567 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002568 break;
2569 }
2570 }
2571 }
2572
Michal Vasko9beceb82022-04-05 12:14:15 +02002573 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002574 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002575 if (match) {
2576 *match = NULL;
2577 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002578 return LY_ENOTFOUND;
2579 }
2580
Michal Vasko9b368d32020-02-14 13:53:31 +01002581 if (match) {
2582 *match = (struct lyd_node *)siblings;
2583 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002584 return LY_SUCCESS;
2585}
2586
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002587LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002588lyd_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 +02002589 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002590{
2591 LY_ERR rc;
2592 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002593 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002594
Michal Vasko4c583e82020-07-17 12:16:14 +02002595 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002596 if (!siblings) {
2597 /* no data */
2598 if (match) {
2599 *match = NULL;
2600 }
2601 return LY_ENOTFOUND;
2602 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002603
Michal Vasko9beceb82022-04-05 12:14:15 +02002604 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2605 /* parent of ext nodes is useless */
2606 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
2607 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
2608 }
2609
2610 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2611 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002612 if (match) {
2613 *match = NULL;
2614 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002615 return LY_ENOTFOUND;
2616 }
2617
Michal Vaskof03ed032020-03-04 13:31:44 +01002618 if (key_or_value && !val_len) {
2619 val_len = strlen(key_or_value);
2620 }
2621
Michal Vaskob104f112020-07-17 09:54:54 +02002622 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2623 /* create a data node and find the instance */
2624 if (schema->nodetype == LYS_LEAFLIST) {
2625 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002626 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 +02002627 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002628 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002629 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002630 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002631 }
2632
2633 /* find it */
2634 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002635 } else {
2636 /* find the first schema node instance */
2637 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002638 }
2639
Michal Vaskoe444f752020-02-10 12:20:06 +01002640 lyd_free_tree(target);
2641 return rc;
2642}
Michal Vaskoccc02342020-05-21 10:09:21 +02002643
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002644LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002645lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2646{
2647 struct lyd_node **match_p, *first, *iter;
2648 struct lyd_node_inner *parent;
2649
Michal Vasko83ae7772022-06-08 10:01:55 +02002650 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002651 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002652
2653 LY_CHECK_RET(ly_set_new(set));
2654
2655 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2656 /* no data or schema mismatch */
2657 return LY_ENOTFOUND;
2658 }
2659
2660 /* get first sibling */
2661 siblings = lyd_first_sibling(siblings);
2662
2663 parent = siblings->parent;
2664 if (parent && parent->schema && parent->children_ht) {
2665 assert(target->hash);
2666
2667 /* find the first instance */
2668 lyd_find_sibling_first(siblings, target, &first);
2669 if (first) {
2670 /* add it so that it is the first in the set */
2671 if (ly_set_add(*set, first, 1, NULL)) {
2672 goto error;
2673 }
2674
2675 /* find by hash */
2676 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2677 iter = *match_p;
2678 } else {
2679 /* not found */
2680 iter = NULL;
2681 }
2682 while (iter) {
2683 /* add all found nodes into the set */
2684 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2685 goto error;
2686 }
2687
2688 /* find next instance */
2689 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2690 iter = NULL;
2691 } else {
2692 iter = *match_p;
2693 }
2694 }
2695 }
2696 } else {
2697 /* no children hash table */
2698 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002699 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002700 ly_set_add(*set, (void *)siblings, 1, NULL);
2701 }
2702 }
2703 }
2704
2705 if (!(*set)->count) {
2706 return LY_ENOTFOUND;
2707 }
2708 return LY_SUCCESS;
2709
2710error:
2711 ly_set_free(*set, NULL);
2712 *set = NULL;
2713 return LY_EMEM;
2714}
2715
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002716LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002717lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2718{
2719 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2720
2721 for ( ; first; first = first->next) {
2722 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2723 break;
2724 }
2725 }
2726
2727 if (match) {
2728 *match = (struct lyd_node *)first;
2729 }
2730 return first ? LY_SUCCESS : LY_ENOTFOUND;
2731}
2732
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002733LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002734lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2735 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002736{
2737 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002738 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02002739 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002740 uint32_t i;
2741
Michal Vaskoddd76592022-01-17 13:34:48 +01002742 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002743
Michal Vasko37c69432021-04-12 14:48:24 +02002744 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002745
Michal Vaskoddd76592022-01-17 13:34:48 +01002746 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01002747 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002749
2750 /* evaluate expression */
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02002751 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2752 LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02002753 LY_CHECK_GOTO(ret, cleanup);
2754
Michal Vaskoc0d91642022-11-03 13:56:29 +01002755 if (xp_set.type != LYXP_SET_NODE_SET) {
2756 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2757 ret = LY_EINVAL;
2758 goto cleanup;
2759 }
2760
Michal Vaskoccc02342020-05-21 10:09:21 +02002761 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002762 ret = ly_set_new(set);
2763 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002764
Michal Vaskoc0d91642022-11-03 13:56:29 +01002765 /* transform into ly_set, allocate memory for all the elements once (even though not all items must be
2766 * elements but most likely will be) */
2767 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2768 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2769 (*set)->size = xp_set.used;
Michal Vaskoccc02342020-05-21 10:09:21 +02002770
Michal Vaskoc0d91642022-11-03 13:56:29 +01002771 for (i = 0; i < xp_set.used; ++i) {
2772 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2773 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
2774 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002775 }
2776 }
2777
2778cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002779 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002780 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01002781 if (ret) {
2782 ly_set_free(*set, NULL);
2783 *set = NULL;
2784 }
Michal Vaskoccc02342020-05-21 10:09:21 +02002785 return ret;
2786}
Radek Krejcica989142020-11-05 11:32:22 +01002787
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002788LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002789lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2790 const struct lyxp_var *vars, struct ly_set **set)
2791{
2792 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2793
2794 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2795}
2796
2797LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01002798lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2799{
2800 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2801
Michal Vaskoddd76592022-01-17 13:34:48 +01002802 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002803}
2804
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002805LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002806lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2807{
Michal Vaskoe3716b32021-12-13 16:58:25 +01002808 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2809
Michal Vaskoddd76592022-01-17 13:34:48 +01002810 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02002811}
2812
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002813LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002814lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2815 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002816{
2817 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002818 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002819 struct lyxp_expr *exp = NULL;
2820
2821 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
2822
2823 /* compile expression */
2824 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
2825 LY_CHECK_GOTO(ret, cleanup);
2826
2827 /* evaluate expression */
Michal Vasko10fabfc2022-08-09 08:55:43 +02002828 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 +02002829 LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002830 LY_CHECK_GOTO(ret, cleanup);
2831
2832 /* transform into boolean */
2833 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
2834 LY_CHECK_GOTO(ret, cleanup);
2835
2836 /* set result */
2837 *result = xp_set.val.bln;
2838
2839cleanup:
2840 lyxp_set_free_content(&xp_set);
2841 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
2842 return ret;
2843}
2844
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002845LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002846lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2847{
2848 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2849}
2850
2851LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002852lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
2853{
Michal Vasko10fabfc2022-08-09 08:55:43 +02002854 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
aPiecekfba75362021-10-07 12:39:48 +02002855}
2856
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002857LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002858lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2859{
2860 LY_ERR ret = LY_SUCCESS;
2861 struct lyxp_expr *expr = NULL;
2862 struct ly_path *lypath = NULL;
2863
2864 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2865
2866 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002867 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 +01002868 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
2869 LY_CHECK_GOTO(ret, cleanup);
2870
2871 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002872 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002873 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 +01002874 LY_CHECK_GOTO(ret, cleanup);
2875
2876 /* evaluate the path */
2877 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
2878
2879cleanup:
2880 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2881 ly_path_free(LYD_CTX(ctx_node), lypath);
2882 return ret;
2883}
2884
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002885LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002886lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2887{
2888 LY_ERR ret;
2889 struct lyd_node *m;
2890
2891 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2892
2893 ret = ly_path_eval(path, tree, &m);
2894 if (ret) {
2895 if (match) {
2896 *match = NULL;
2897 }
2898 return LY_ENOTFOUND;
2899 }
2900
2901 if (match) {
2902 *match = m;
2903 }
2904 return LY_SUCCESS;
2905}