blob: fc9521997fa9c5623018ebe3a107b9005bef3b40 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02005 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci084289f2019-07-09 17:35:30 +020020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "context.h"
32#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020033#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020042#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010043#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010048#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020049#include "tree_schema.h"
50#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020051#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020052#include "xml.h"
53#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020054
Radek Krejci7931b192020-06-25 17:05:03 +020055static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020056lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020057{
Michal Vasko69730152020-10-09 16:30:07 +020058 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020059 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020060 const char *path = in->method.fpath.filepath;
61 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020062
63 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020064 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020065
Radek Krejcif13b87b2020-12-01 22:02:17 +010066 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
67 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020068 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
70 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020071 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010072 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
73 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020074 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020075 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020076 }
77
Radek Krejci7931b192020-06-25 17:05:03 +020078 return format;
79}
Radek Krejcie7b95092019-05-15 11:03:07 +020080
Michal Vaskoe0665742021-02-11 11:08:44 +010081/**
82 * @brief Parse YANG data into a data tree.
83 *
84 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010085 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010086 * @param[in] parent Parent to connect the parsed nodes to, if any.
87 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
88 * @param[in] in Input handle to read the input from.
89 * @param[in] format Expected format of the data in @p in.
90 * @param[in] parse_opts Options for parser.
91 * @param[in] val_opts Options for validation.
92 * @param[out] op Optional pointer to the parsed operation, if any.
93 * @return LY_ERR value.
94 */
95static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010096lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
97 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +020098{
Michal Vaskoe0665742021-02-11 11:08:44 +010099 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100101 struct ly_set parsed = {0};
102 struct lyd_node *first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100103 uint32_t i, int_opts = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100104 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105
Michal Vaskoe0665742021-02-11 11:08:44 +0100106 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200107
108 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100109 if (first_p) {
110 *first_p = NULL;
111 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200112
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 /* remember input position */
114 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200115
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100116 /* set internal options */
117 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
118 int_opts = LYD_INTOPT_WITH_SIBLINGS;
119 }
120
Michal Vaskoe0665742021-02-11 11:08:44 +0100121 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200122 switch (format) {
123 case LYD_XML:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100124 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100125 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200126 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200127 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100128 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100129 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200131 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100132 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100133 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200135 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100136 LOGARG(ctx, format);
137 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100138 break;
139 }
140 LY_CHECK_GOTO(rc, cleanup);
141
142 if (parent) {
143 /* get first top-level sibling */
144 for (first = parent; first->parent; first = lyd_parent(first)) {}
145 first = lyd_first_sibling(first);
146 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200147 }
148
Michal Vaskoe0665742021-02-11 11:08:44 +0100149 if (!(parse_opts & LYD_PARSE_ONLY)) {
150 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100151 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
Michal Vasko135719f2022-08-25 12:18:17 +0200152 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100153 LY_CHECK_GOTO(rc, cleanup);
154 }
Radek Krejci7931b192020-06-25 17:05:03 +0200155
Michal Vaskoe0665742021-02-11 11:08:44 +0100156 /* set the operation node */
157 if (op) {
158 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200159 }
160
161cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100162 if (lydctx) {
163 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100165 if (rc) {
166 if (parent) {
167 /* free all the parsed subtrees */
168 for (i = 0; i < parsed.count; ++i) {
169 lyd_free_tree(parsed.dnodes[i]);
170 }
171 } else {
172 /* free everything */
173 lyd_free_all(*first_p);
174 *first_p = NULL;
175 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100176 } else if (subtree_sibling) {
177 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100178 }
179 ly_set_erase(&parsed, NULL);
180 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200181}
182
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100183LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100184lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
185 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
186{
187 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
188
189 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
190 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
191 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
192
193 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
194}
195
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100196LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100197lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
198 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
199{
200 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
201 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
202 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
203
Radek Krejcif16e2542021-02-17 15:39:23 +0100204 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100205}
206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100207LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100208lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
209 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200210{
211 LY_ERR ret;
212 struct ly_in *in;
213
214 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100215 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200216
217 ly_in_free(in, 0);
218 return ret;
219}
220
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100221LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200222lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200223 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200224{
225 LY_ERR ret;
226 struct ly_in *in;
227
228 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100229 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200230
231 ly_in_free(in, 0);
232 return ret;
233}
234
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100235LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200236lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
237 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200238{
239 LY_ERR ret;
240 struct ly_in *in;
241
242 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100243 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200244
245 ly_in_free(in, 0);
246 return ret;
247}
248
Radek Krejcif16e2542021-02-17 15:39:23 +0100249/**
250 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
251 * for schema nodes locked inside the extension instance.
252 *
253 * At least one of @p parent, @p tree, or @p op must always be set.
254 *
255 * Specific @p data_type values have different parameter meaning as follows:
256 * - ::LYD_TYPE_RPC_NETCONF:
257 * - @p parent - must be NULL, the whole RPC is expected;
258 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
259 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
260 * a separate opaque data tree, even if the function fails, this may be returned;
261 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
262 *
263 * - ::LYD_TYPE_NOTIF_NETCONF:
264 * - @p parent - must be NULL, the whole notification is expected;
265 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
266 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
267 * a separate opaque data tree, even if the function fails, this may be returned;
268 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
269 *
270 * - ::LYD_TYPE_REPLY_NETCONF:
271 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
272 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
273 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
274 * a separate opaque data tree, even if the function fails, this may be returned;
275 * - @p op - must be NULL, the reply is appended to the RPC;
276 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
277 *
278 * @param[in] ctx libyang context.
279 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
280 * @param[in] parent Optional parent to connect the parsed nodes to.
281 * @param[in] in Input handle to read the input from.
282 * @param[in] format Expected format of the data in @p in.
283 * @param[in] data_type Expected operation to parse (@ref datatype).
284 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
285 * @param[out] op Optional parsed operation node.
286 * @return LY_ERR value.
287 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
288 */
289static LY_ERR
290lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
291 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200292{
Michal Vaskoe0665742021-02-11 11:08:44 +0100293 LY_ERR rc = LY_SUCCESS;
294 struct lyd_ctx *lydctx = NULL;
295 struct ly_set parsed = {0};
296 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100297 uint32_t i, parse_opts, val_opts, int_opts = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200298
Michal Vasko27fb0262021-02-23 09:42:01 +0100299 if (!ctx) {
300 ctx = LYD_CTX(parent);
301 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200302 if (tree) {
303 *tree = NULL;
304 }
305 if (op) {
306 *op = NULL;
307 }
308
Radek Krejci7931b192020-06-25 17:05:03 +0200309 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200310
Michal Vasko63f3d842020-07-08 10:10:14 +0200311 /* remember input position */
312 in->func_start = in->current;
313
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100314 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200315 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100316 val_opts = 0;
317
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100318 switch (data_type) {
319
320 /* special XML NETCONF data types */
321 case LYD_TYPE_RPC_NETCONF:
322 case LYD_TYPE_NOTIF_NETCONF:
323 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
324 /* fallthrough */
325 case LYD_TYPE_REPLY_NETCONF:
326 if (data_type == LYD_TYPE_REPLY_NETCONF) {
327 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
328 LY_EINVAL);
329 }
330
331 /* parse the NETCONF message */
332 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100333 if (rc && envp) {
334 /* special situation when the envelopes were parsed successfully */
Michal Vasko472baa82022-12-01 16:17:41 +0100335 *tree = envp;
Michal Vasko73792a12022-05-10 09:28:45 +0200336 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100337 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100338
339 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100340 if (envp) {
341 /* special out param meaning */
342 *tree = envp;
343 } else {
344 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100345 }
346 if (op) {
347 *op = lydctx->op_node;
348 }
349 goto cleanup;
350
351 /* set internal opts */
352 case LYD_TYPE_RPC_YANG:
353 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
354 break;
355 case LYD_TYPE_NOTIF_YANG:
356 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
357 break;
358 case LYD_TYPE_REPLY_YANG:
359 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
360 break;
361 case LYD_TYPE_DATA_YANG:
362 LOGINT(ctx);
363 rc = LY_EINT;
364 goto cleanup;
365 }
366
367 /* parse the data */
368 switch (format) {
369 case LYD_XML:
370 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100371 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200372 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100373 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200375 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100376 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100377 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200378 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100379 LOGARG(ctx, format);
380 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200381 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200382 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100383 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200384
Michal Vaskoe0665742021-02-11 11:08:44 +0100385 /* set out params correctly */
386 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100387 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100388 }
389 if (op) {
390 *op = lydctx->op_node;
391 }
392
393cleanup:
394 if (lydctx) {
395 lydctx->free(lydctx);
396 }
397 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200398 /* free all the parsed nodes */
399 if (parsed.count) {
400 i = parsed.count;
401 do {
402 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100403 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200404 } while (i);
405 }
406 if (tree && ((format != LYD_XML) || !envp)) {
407 *tree = NULL;
408 }
409 if (op) {
410 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100411 }
412 }
413 ly_set_erase(&parsed, NULL);
414 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200415}
Radek Krejci084289f2019-07-09 17:35:30 +0200416
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100417LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100418lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
419 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
420{
421 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
422
423 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
424}
425
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100426LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100427lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
428 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
429{
430 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
431
432 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
433
434 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
435}
436
Michal Vasko90932a92020-02-12 14:33:03 +0100437struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200438lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100439{
Michal Vaskob104f112020-07-17 09:54:54 +0200440 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100441 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200442 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100443 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100444
Michal Vaskob104f112020-07-17 09:54:54 +0200445 assert(new_node);
446
Michal Vasko9beceb82022-04-05 12:14:15 +0200447 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200448 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100449 return NULL;
450 }
451
Michal Vasko93b16062020-12-09 18:14:18 +0100452 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100453 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100454 getnext_opts = LYS_GETNEXT_OUTPUT;
455 }
456
Michal Vaskoa2756f02021-07-09 13:20:28 +0200457 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200458 /* find the anchor using hashes */
459 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100460 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200461 while (schema) {
462 /* keep trying to find the first existing instance of the closest following schema sibling,
463 * otherwise return NULL - inserting at the end */
464 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
465 break;
466 }
467
Michal Vasko93b16062020-12-09 18:14:18 +0100468 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200469 }
470 } else {
471 /* find the anchor without hashes */
472 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200473 sparent = lysc_data_parent(new_node->schema);
474 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200475 /* we are in top-level, skip all the data from preceding modules */
476 LY_LIST_FOR(match, match) {
477 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
478 break;
479 }
480 }
481 }
482
483 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100484 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200485
486 found = 0;
487 LY_LIST_FOR(match, match) {
488 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
489 /* we have found an opaque node, which must be at the end, so use it OR
490 * modules do not match, so we must have traversed all the data from new_node module (if any),
491 * we have found the first node of the next module, that is what we want */
492 break;
493 }
494
495 /* skip schema nodes until we find the instantiated one */
496 while (!found) {
497 if (new_node->schema == schema) {
498 /* we have found the schema of the new node, continue search to find the first
499 * data node with a different schema (after our schema) */
500 found = 1;
501 break;
502 }
503 if (match->schema == schema) {
504 /* current node (match) is a data node still before the new node, continue search in data */
505 break;
506 }
Michal Vasko93b16062020-12-09 18:14:18 +0100507 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200508 assert(schema);
509 }
510
511 if (found && (match->schema != new_node->schema)) {
512 /* find the next node after we have found our node schema data instance */
513 break;
514 }
515 }
Michal Vasko90932a92020-02-12 14:33:03 +0100516 }
517
518 return match;
519}
520
Michal Vaskoc61dd062022-06-07 11:01:28 +0200521void
Michal Vaskof03ed032020-03-04 13:31:44 +0100522lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100523{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100524 struct lyd_node_inner *par;
525
Michal Vasko90932a92020-02-12 14:33:03 +0100526 assert(!node->next && (node->prev == node));
527
528 node->next = sibling->next;
529 node->prev = sibling;
530 sibling->next = node;
531 if (node->next) {
532 /* sibling had a succeeding node */
533 node->next->prev = node;
534 } else {
535 /* sibling was last, find first sibling and change its prev */
536 if (sibling->parent) {
537 sibling = sibling->parent->child;
538 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200539 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100540 }
541 sibling->prev = node;
542 }
543 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100544
Michal Vasko9f96a052020-03-10 09:41:45 +0100545 for (par = node->parent; par; par = par->parent) {
546 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
547 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100548 par->flags &= ~LYD_DEFAULT;
549 }
550 }
Michal Vasko90932a92020-02-12 14:33:03 +0100551}
552
Michal Vaskoc61dd062022-06-07 11:01:28 +0200553void
Michal Vaskof03ed032020-03-04 13:31:44 +0100554lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100555{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100556 struct lyd_node_inner *par;
557
Michal Vasko90932a92020-02-12 14:33:03 +0100558 assert(!node->next && (node->prev == node));
559
560 node->next = sibling;
561 /* covers situation of sibling being first */
562 node->prev = sibling->prev;
563 sibling->prev = node;
564 if (node->prev->next) {
565 /* sibling had a preceding node */
566 node->prev->next = node;
567 } else if (sibling->parent) {
568 /* sibling was first and we must also change parent child pointer */
569 sibling->parent->child = node;
570 }
571 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100572
Michal Vasko9f96a052020-03-10 09:41:45 +0100573 for (par = node->parent; par; par = par->parent) {
574 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
575 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100576 par->flags &= ~LYD_DEFAULT;
577 }
578 }
Michal Vasko90932a92020-02-12 14:33:03 +0100579}
580
581/**
Michal Vaskob104f112020-07-17 09:54:54 +0200582 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100583 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100584 * Handles inserting into NP containers and key-less lists.
585 *
Michal Vasko90932a92020-02-12 14:33:03 +0100586 * @param[in] parent Parent to insert into.
587 * @param[in] node Node to insert.
588 */
589static void
Michal Vaskob104f112020-07-17 09:54:54 +0200590lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100591{
592 struct lyd_node_inner *par;
593
Radek Krejcia1c1e542020-09-29 16:06:52 +0200594 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100595 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100596
597 par = (struct lyd_node_inner *)parent;
598
Michal Vaskob104f112020-07-17 09:54:54 +0200599 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100600 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100601
Michal Vaskod989ba02020-08-24 10:59:24 +0200602 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100603 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
604 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100605 par->flags &= ~LYD_DEFAULT;
606 }
607 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200608}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100609
Michal Vasko751cb4d2020-07-14 12:25:28 +0200610/**
611 * @brief Learn whether a list instance has all the keys.
612 *
613 * @param[in] list List instance to check.
614 * @return non-zero if all the keys were found,
615 * @return 0 otherwise.
616 */
617static int
618lyd_insert_has_keys(const struct lyd_node *list)
619{
620 const struct lyd_node *key;
621 const struct lysc_node *skey = NULL;
622
623 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200624 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200625 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
626 if (!key || (key->schema != skey)) {
627 /* key missing */
628 return 0;
629 }
630
631 key = key->next;
632 }
633
634 /* all keys found */
635 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100636}
637
638void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200639lyd_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 +0100640{
Michal Vaskob104f112020-07-17 09:54:54 +0200641 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100642
Michal Vaskob104f112020-07-17 09:54:54 +0200643 /* inserting list without its keys is not supported */
644 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100645 assert(!parent || !parent->schema ||
646 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100647
Michal Vaskob104f112020-07-17 09:54:54 +0200648 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100649 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100650 }
Michal Vasko90932a92020-02-12 14:33:03 +0100651
Michal Vaskob104f112020-07-17 09:54:54 +0200652 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100653 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100654
Michal Vasko9beceb82022-04-05 12:14:15 +0200655 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200656 /* no next anchor */
657 anchor = NULL;
658 } else {
659 /* find the anchor, our next node, so we can insert before it */
660 anchor = lyd_insert_get_next_anchor(first_sibling, node);
661 }
662
Michal Vaskob104f112020-07-17 09:54:54 +0200663 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200664 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200665 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100666 if (!parent && (*first_sibling_p == anchor)) {
667 /* move first sibling */
668 *first_sibling_p = node;
669 }
Michal Vaskob104f112020-07-17 09:54:54 +0200670 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200671 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200672 lyd_insert_after_node(first_sibling->prev, node);
673 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200674 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200675 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100676 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200677 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200678 *first_sibling_p = node;
679 }
680
681 /* insert into parent HT */
682 lyd_insert_hash(node);
683
684 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200685 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200686 lyd_hash(parent);
687
688 /* now we can insert even the list into its parent HT */
689 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100690 }
Michal Vasko90932a92020-02-12 14:33:03 +0100691}
692
Michal Vasko717a4c32020-12-07 09:40:10 +0100693/**
694 * @brief Check schema place of a node to be inserted.
695 *
696 * @param[in] parent Schema node of the parent data node.
697 * @param[in] sibling Schema node of a sibling data node.
698 * @param[in] schema Schema node if the data node to be inserted.
699 * @return LY_SUCCESS on success.
700 * @return LY_EINVAL if the place is invalid.
701 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100702static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100703lyd_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 +0100704{
705 const struct lysc_node *par2;
706
Michal Vasko62ed12d2020-05-21 10:08:25 +0200707 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100708 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
709 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100710
Michal Vasko717a4c32020-12-07 09:40:10 +0100711 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100712 /* opaque nodes can be inserted wherever */
713 return LY_SUCCESS;
714 }
715
Michal Vasko717a4c32020-12-07 09:40:10 +0100716 if (!parent) {
717 parent = lysc_data_parent(sibling);
718 }
719
Michal Vaskof03ed032020-03-04 13:31:44 +0100720 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200721 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100722
723 if (parent) {
724 /* inner node */
725 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200726 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200727 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100728 return LY_EINVAL;
729 }
730 } else {
731 /* top-level node */
732 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200733 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100734 return LY_EINVAL;
735 }
736 }
737
738 return LY_SUCCESS;
739}
740
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100741LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200742lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100743{
744 struct lyd_node *iter;
745
Michal Vasko0ab974d2021-02-24 13:18:26 +0100746 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100747 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100748
Michal Vasko717a4c32020-12-07 09:40:10 +0100749 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100750
Michal Vasko0ab974d2021-02-24 13:18:26 +0100751 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100752 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100753 return LY_EINVAL;
754 }
755
756 if (node->parent || node->prev->next) {
757 lyd_unlink_tree(node);
758 }
759
760 while (node) {
761 iter = node->next;
762 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200763 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100764 node = iter;
765 }
766 return LY_SUCCESS;
767}
768
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100769LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200770lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100771{
772 struct lyd_node *iter;
773
774 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
775 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
776
777 if (first->schema && (first->schema->flags & LYS_KEY)) {
778 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
779 return LY_EINVAL;
780 }
781
782 while (first) {
783 iter = first->next;
784 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100785 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100786 first = iter;
787 }
788 return LY_SUCCESS;
789}
790
791LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200792lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100793{
794 struct lyd_node *iter;
795
Michal Vaskob104f112020-07-17 09:54:54 +0200796 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100797
Michal Vaskob104f112020-07-17 09:54:54 +0200798 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100799 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100800 }
801
Michal Vasko553d0b52020-12-04 16:27:52 +0100802 if (sibling == node) {
803 /* we need to keep the connection to siblings so we can insert into them */
804 sibling = sibling->prev;
805 }
806
Michal Vaskob1b5c262020-03-05 14:29:47 +0100807 if (node->parent || node->prev->next) {
808 lyd_unlink_tree(node);
809 }
810
811 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100812 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200813 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200814 return LY_EINVAL;
815 }
816
Michal Vaskob1b5c262020-03-05 14:29:47 +0100817 iter = node->next;
818 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200819 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100820 node = iter;
821 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100822
Michal Vaskob104f112020-07-17 09:54:54 +0200823 if (first) {
824 /* find the first sibling */
825 *first = sibling;
826 while ((*first)->prev->next) {
827 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100828 }
829 }
830
831 return LY_SUCCESS;
832}
833
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100834LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100835lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
836{
Michal Vaskobce230b2022-04-19 09:55:31 +0200837 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100838 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100839
Michal Vasko717a4c32020-12-07 09:40:10 +0100840 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100841
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200842 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200843 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100844 return LY_EINVAL;
845 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200846 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
847 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100848 return LY_EINVAL;
849 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100850
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100851 lyd_unlink_tree(node);
852 lyd_insert_before_node(sibling, node);
853 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100854
Michal Vaskof03ed032020-03-04 13:31:44 +0100855 return LY_SUCCESS;
856}
857
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100858LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100859lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
860{
Michal Vaskobce230b2022-04-19 09:55:31 +0200861 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100862 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100863
Michal Vasko717a4c32020-12-07 09:40:10 +0100864 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100865
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200866 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200867 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100868 return LY_EINVAL;
869 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200870 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
871 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100872 return LY_EINVAL;
873 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100874
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100875 lyd_unlink_tree(node);
876 lyd_insert_after_node(sibling, node);
877 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100878
Michal Vaskof03ed032020-03-04 13:31:44 +0100879 return LY_SUCCESS;
880}
881
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100882LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200883lyd_unlink_siblings(struct lyd_node *node)
884{
885 struct lyd_node *next, *elem, *first = NULL;
886
887 LY_LIST_FOR_SAFE(node, next, elem) {
888 lyd_unlink_tree(elem);
889 lyd_insert_node(NULL, &first, elem, 1);
890 }
891}
892
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100893LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100894lyd_unlink_tree(struct lyd_node *node)
895{
896 struct lyd_node *iter;
897
898 if (!node) {
899 return;
900 }
901
Michal Vaskob104f112020-07-17 09:54:54 +0200902 /* update hashes while still linked into the tree */
903 lyd_unlink_hash(node);
904
Michal Vaskof03ed032020-03-04 13:31:44 +0100905 /* unlink from siblings */
906 if (node->prev->next) {
907 node->prev->next = node->next;
908 }
909 if (node->next) {
910 node->next->prev = node->prev;
911 } else {
912 /* unlinking the last node */
913 if (node->parent) {
914 iter = node->parent->child;
915 } else {
916 iter = node->prev;
917 while (iter->prev != node) {
918 iter = iter->prev;
919 }
920 }
921 /* update the "last" pointer from the first node */
922 iter->prev = node->prev;
923 }
924
925 /* unlink from parent */
926 if (node->parent) {
927 if (node->parent->child == node) {
928 /* the node is the first child */
929 node->parent->child = node->next;
930 }
931
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200932 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100933 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200934
Michal Vaskof03ed032020-03-04 13:31:44 +0100935 node->parent = NULL;
936 }
937
938 node->next = NULL;
939 node->prev = node;
940}
941
Michal Vaskoa5da3292020-08-12 13:10:50 +0200942void
Michal Vasko871a0252020-11-11 18:35:24 +0100943lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200944{
945 struct lyd_meta *last, *iter;
946
947 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200948
949 if (!meta) {
950 return;
951 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200952
953 for (iter = meta; iter; iter = iter->next) {
954 iter->parent = parent;
955 }
956
957 /* insert as the last attribute */
958 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200959 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200960 last->next = meta;
961 } else {
962 parent->meta = meta;
963 }
964
965 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100966 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200967 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100968 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200970}
971
972LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100973lyd_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 +0200974 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 +0100975 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 +0100976{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100977 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100978 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200979 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100980 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200981 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100982
Michal Vasko9f96a052020-03-10 09:41:45 +0100983 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100984
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200985 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200986 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200987 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100988 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200989 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100990 break;
991 }
992 }
993 if (!ant) {
994 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100995 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100996 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100997 ret = LY_EINVAL;
998 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +0100999 }
1000
Michal Vasko9f96a052020-03-10 09:41:45 +01001001 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001003 mt->parent = parent;
1004 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001005 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko193dacd2022-10-13 08:43:05 +02001006 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 +01001007 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1009 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1010 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001011
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001012 /* insert as the last attribute */
1013 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001014 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001015 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001016 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001017 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001018 }
1019
Michal Vasko9f96a052020-03-10 09:41:45 +01001020 if (meta) {
1021 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001022 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001023
1024cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001025 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001026}
1027
Michal Vaskoa5da3292020-08-12 13:10:50 +02001028void
1029lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1030{
1031 struct lyd_attr *last, *iter;
1032 struct lyd_node_opaq *opaq;
1033
1034 assert(parent && !parent->schema);
1035
1036 if (!attr) {
1037 return;
1038 }
1039
1040 opaq = (struct lyd_node_opaq *)parent;
1041 for (iter = attr; iter; iter = iter->next) {
1042 iter->parent = opaq;
1043 }
1044
1045 /* insert as the last attribute */
1046 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001047 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001048 last->next = attr;
1049 } else {
1050 opaq->attr = attr;
1051 }
1052}
1053
Michal Vasko52927e22020-03-16 17:26:14 +01001054LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001055lyd_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 +01001056 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 +02001057 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 +01001058{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001059 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001060 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001061
1062 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001063 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001064
Michal Vasko2a3722d2021-06-16 11:52:39 +02001065 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001066 value = "";
1067 }
1068
1069 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001070 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001071
Michal Vaskoad92b672020-11-12 13:11:31 +01001072 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001073 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001074 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001075 }
1076 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001077 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001078 }
1079
Michal Vasko52927e22020-03-16 17:26:14 +01001080 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001081 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1082 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001083 *dynamic = 0;
1084 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001085 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001086 }
Michal Vasko501af032020-11-11 20:27:44 +01001087 at->format = format;
1088 at->val_prefix_data = val_prefix_data;
1089 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001090
1091 /* insert as the last attribute */
1092 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001093 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001094 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001095 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001096 last->next = at;
1097 }
1098
Radek Krejci011e4aa2020-09-04 15:22:31 +02001099finish:
1100 if (ret) {
1101 lyd_free_attr_single(ctx, at);
1102 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001103 *attr = at;
1104 }
1105 return LY_SUCCESS;
1106}
1107
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001108LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001109lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001110{
Michal Vasko004d3152020-06-11 19:59:22 +02001111 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001112
Michal Vasko004d3152020-06-11 19:59:22 +02001113 if (ly_path_eval(path, tree, &target)) {
1114 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001115 }
1116
Michal Vasko004d3152020-06-11 19:59:22 +02001117 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001118}
1119
aPiecek2f63f952021-03-30 12:22:18 +02001120/**
1121 * @brief Check the equality of the two schemas from different contexts.
1122 *
1123 * @param schema1 of first node.
1124 * @param schema2 of second node.
1125 * @return 1 if the schemas are equal otherwise 0.
1126 */
1127static ly_bool
1128lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1129{
1130 if (!schema1 && !schema2) {
1131 return 1;
1132 } else if (!schema1 || !schema2) {
1133 return 0;
1134 }
1135
1136 assert(schema1->module->ctx != schema2->module->ctx);
1137
1138 if (schema1->nodetype != schema2->nodetype) {
1139 return 0;
1140 }
1141
1142 if (strcmp(schema1->name, schema2->name)) {
1143 return 0;
1144 }
1145
1146 if (strcmp(schema1->module->name, schema2->module->name)) {
1147 return 0;
1148 }
1149
1150 if (schema1->module->revision || schema2->module->revision) {
1151 if (!schema1->module->revision || !schema2->module->revision) {
1152 return 0;
1153 }
1154 if (strcmp(schema1->module->revision, schema2->module->revision)) {
1155 return 0;
1156 }
1157 }
1158
1159 return 1;
1160}
1161
1162/**
1163 * @brief Check the equality of the schemas for all parent nodes.
1164 *
1165 * Both nodes must be from different contexts.
1166 *
1167 * @param node1 Data of first node.
1168 * @param node2 Data of second node.
1169 * @return 1 if the all related parental schemas are equal otherwise 0.
1170 */
1171static ly_bool
1172lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1173{
1174 const struct lysc_node *parent1, *parent2;
1175
1176 assert(node1 && node2);
1177
1178 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1179 parent1 && parent2;
1180 parent1 = parent1->parent, parent2 = parent2->parent) {
1181 if (!lyd_compare_schema_equal(parent1, parent2)) {
1182 return 0;
1183 }
1184 }
1185
1186 if (parent1 || parent2) {
1187 return 0;
1188 }
1189
1190 return 1;
1191}
1192
1193/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001194 * @brief Compare 2 nodes values including opaque node values.
1195 *
1196 * @param[in] node1 First node to compare.
1197 * @param[in] node2 Second node to compare.
1198 * @return LY_SUCCESS if equal.
1199 * @return LY_ENOT if not equal.
1200 * @return LY_ERR on error.
1201 */
1202static LY_ERR
1203lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1204{
1205 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1206 const char *val1, *val2, *col;
1207 const struct lys_module *mod;
1208 char *val_dyn = NULL;
1209 LY_ERR rc = LY_SUCCESS;
1210
1211 if (!node1->schema) {
1212 opaq1 = (struct lyd_node_opaq *)node1;
1213 }
1214 if (!node2->schema) {
1215 opaq2 = (struct lyd_node_opaq *)node2;
1216 }
1217
1218 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1219 /* opaque XML and opaque XML node */
1220 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1221 opaq2->val_prefix_data)) {
1222 return LY_ENOT;
1223 }
1224 return LY_SUCCESS;
1225 }
1226
1227 /* get their values */
1228 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1229 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1230 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1231 if (!mod) {
1232 /* unable to compare */
1233 return LY_ENOT;
1234 }
1235
1236 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1237 LOGMEM(LYD_CTX(node1));
1238 return LY_EMEM;
1239 }
1240 val1 = val_dyn;
1241 } else {
1242 val1 = lyd_get_value(node1);
1243 }
1244 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1245 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1246 if (!mod) {
1247 return LY_ENOT;
1248 }
1249
1250 assert(!val_dyn);
1251 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1252 LOGMEM(LYD_CTX(node2));
1253 return LY_EMEM;
1254 }
1255 val2 = val_dyn;
1256 } else {
1257 val2 = lyd_get_value(node2);
1258 }
1259
1260 /* compare values */
1261 if (strcmp(val1, val2)) {
1262 rc = LY_ENOT;
1263 }
1264
1265 free(val_dyn);
1266 return rc;
1267}
1268
1269/**
aPiecek2f63f952021-03-30 12:22:18 +02001270 * @brief Internal implementation of @ref lyd_compare_single.
1271 * @copydoc lyd_compare_single
1272 * @param[in] parental_schemas_checked Flag used for optimization.
1273 * When this function is called for the first time, the flag must be set to 0.
1274 * The @ref lyd_compare_schema_parents_equal should be called only once during
1275 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1276 */
1277static LY_ERR
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001278lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1279 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001280{
1281 const struct lyd_node *iter1, *iter2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001282 struct lyd_node_any *any1, *any2;
Michal Vaskof3c80a72022-08-17 10:22:04 +02001283 int len1, len2;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001284 LY_ERR r;
Radek Krejci084289f2019-07-09 17:35:30 +02001285
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001286 if (!node1 || !node2) {
1287 if (node1 == node2) {
1288 return LY_SUCCESS;
1289 } else {
1290 return LY_ENOT;
1291 }
1292 }
1293
aPiecek2f63f952021-03-30 12:22:18 +02001294 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1295 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001296 if (options & LYD_COMPARE_OPAQ) {
1297 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1298 return LY_ENOT;
1299 }
1300 } else {
1301 if (node1->schema != node2->schema) {
1302 return LY_ENOT;
1303 }
aPiecek2f63f952021-03-30 12:22:18 +02001304 }
1305 } else {
1306 /* different contexts */
1307 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1308 return LY_ENOT;
1309 }
1310 if (!parental_schemas_checked) {
1311 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1312 return LY_ENOT;
1313 }
1314 parental_schemas_checked = 1;
1315 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001316 }
1317
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001318 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001319 return LY_ENOT;
1320 }
aPiecek2f63f952021-03-30 12:22:18 +02001321 /* 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 +02001322
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001323 if (!node1->schema || !node2->schema) {
1324 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001325 return LY_ENOT;
1326 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001327 if ((r = lyd_compare_single_value(node1, node2))) {
1328 return r;
Michal Vasko52927e22020-03-16 17:26:14 +01001329 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001330
Michal Vasko52927e22020-03-16 17:26:14 +01001331 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001332 iter1 = lyd_child(node1);
1333 iter2 = lyd_child(node2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001334 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001335 }
1336 return LY_SUCCESS;
1337 } else {
1338 switch (node1->schema->nodetype) {
1339 case LYS_LEAF:
1340 case LYS_LEAFLIST:
1341 if (options & LYD_COMPARE_DEFAULTS) {
1342 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1343 return LY_ENOT;
1344 }
1345 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001346 if ((r = lyd_compare_single_value(node1, node2))) {
1347 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001348 }
1349
aPiecek2f63f952021-03-30 12:22:18 +02001350 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001351 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001352 case LYS_RPC:
1353 case LYS_ACTION:
1354 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001355 if (options & LYD_COMPARE_DEFAULTS) {
1356 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1357 return LY_ENOT;
1358 }
1359 }
1360 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001361 iter1 = lyd_child(node1);
1362 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001363 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001364 }
1365 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001366 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001367 iter1 = lyd_child(node1);
1368 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001369
1370 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1371 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001372 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001373 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001374 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001375 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001376 return LY_ENOT;
1377 }
1378 iter1 = iter1->next;
1379 iter2 = iter2->next;
1380 }
1381 } else {
1382 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1383
Radek Krejci0f969882020-08-21 16:56:47 +02001384all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001385 if (!iter1 && !iter2) {
1386 /* no children, nothing to compare */
1387 return LY_SUCCESS;
1388 }
1389
Michal Vaskod989ba02020-08-24 10:59:24 +02001390 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001391 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001392 return LY_ENOT;
1393 }
1394 }
1395 if (iter1 || iter2) {
1396 return LY_ENOT;
1397 }
1398 }
1399 return LY_SUCCESS;
1400 case LYS_ANYXML:
1401 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001402 any1 = (struct lyd_node_any *)node1;
1403 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001404
1405 if (any1->value_type != any2->value_type) {
1406 return LY_ENOT;
1407 }
1408 switch (any1->value_type) {
1409 case LYD_ANYDATA_DATATREE:
1410 iter1 = any1->value.tree;
1411 iter2 = any2->value.tree;
1412 goto all_children_compare;
1413 case LYD_ANYDATA_STRING:
1414 case LYD_ANYDATA_XML:
1415 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001416 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1417 return LY_ENOT;
1418 } else if (!any1->value.str && !any2->value.str) {
1419 return LY_SUCCESS;
1420 }
Michal Vasko52927e22020-03-16 17:26:14 +01001421 len1 = strlen(any1->value.str);
1422 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001423 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001424 return LY_ENOT;
1425 }
1426 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001427 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001428 len1 = lyd_lyb_data_length(any1->value.mem);
1429 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001430 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001431 return LY_ENOT;
1432 }
1433 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001434 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001435 }
1436 }
1437
Michal Vaskob7be7a82020-08-20 09:09:04 +02001438 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001439 return LY_EINT;
1440}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001441
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001442LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001443lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1444{
1445 return lyd_compare_single_(node1, node2, options, 0);
1446}
1447
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001448LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001449lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001450{
Michal Vaskod989ba02020-08-24 10:59:24 +02001451 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001452 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1453 }
1454
Michal Vasko11a81432020-07-28 16:31:29 +02001455 if (node1 == node2) {
1456 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001457 }
Michal Vasko11a81432020-07-28 16:31:29 +02001458 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001459}
1460
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001461LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001462lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1463{
1464 if (!meta1 || !meta2) {
1465 if (meta1 == meta2) {
1466 return LY_SUCCESS;
1467 } else {
1468 return LY_ENOT;
1469 }
1470 }
1471
Michal Vaskoa8083062020-11-06 17:22:10 +01001472 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001473 return LY_ENOT;
1474 }
1475
Michal Vasko21725742020-06-29 11:49:25 +02001476 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1477}
1478
Radek Krejci22ebdba2019-07-25 13:59:43 +02001479/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001480 * @brief Create a copy of the attribute.
1481 *
1482 * @param[in] attr Attribute to copy.
1483 * @param[in] node Opaque where to append the new attribute.
1484 * @param[out] dup Optional created attribute copy.
1485 * @return LY_ERR value.
1486 */
1487static LY_ERR
1488lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1489{
1490 LY_ERR ret = LY_SUCCESS;
1491 struct lyd_attr *a, *last;
1492 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1493
1494 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1495
1496 /* create a copy */
1497 a = calloc(1, sizeof *attr);
1498 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1499
1500 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1501 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1502 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1503 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1504 a->hints = attr->hints;
1505 a->format = attr->format;
1506 if (attr->val_prefix_data) {
1507 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1508 LY_CHECK_GOTO(ret, finish);
1509 }
1510
1511 /* insert as the last attribute */
1512 a->parent = opaq;
1513 if (opaq->attr) {
1514 for (last = opaq->attr; last->next; last = last->next) {}
1515 last->next = a;
1516 } else {
1517 opaq->attr = a;
1518 }
1519
1520finish:
1521 if (ret) {
1522 lyd_free_attr_single(LYD_CTX(node), a);
1523 } else if (dup) {
1524 *dup = a;
1525 }
1526 return LY_SUCCESS;
1527}
1528
1529/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001530 * @brief Find @p schema equivalent in @p trg_ctx.
1531 *
1532 * @param[in] schema Schema node to find.
1533 * @param[in] trg_ctx Target context to search in.
1534 * @param[in] parent Data parent of @p schema, if any.
1535 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1536 * @return LY_RRR value.
1537 */
1538static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02001539lyd_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 +01001540 const struct lysc_node **trg_schema)
1541{
Michal Vasko9beceb82022-04-05 12:14:15 +02001542 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1543 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001544 char *path;
1545
1546 if (!schema) {
1547 /* opaque node */
1548 *trg_schema = NULL;
1549 return LY_SUCCESS;
1550 }
1551
Michal Vasko9beceb82022-04-05 12:14:15 +02001552 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001553 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001554 trg_parent = parent->schema;
1555 src_parent = lysc_data_parent(schema);
1556 }
1557
1558 do {
1559 /* find the next parent */
1560 sp = schema;
1561 while (sp->parent != src_parent) {
1562 sp = sp->parent;
1563 }
1564 src_parent = sp;
1565
1566 if (!src_parent->parent) {
1567 /* find the module first */
1568 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1569 if (!trg_mod) {
1570 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1571 src_parent->module->name);
1572 return LY_ENOTFOUND;
1573 }
1574 }
1575
1576 /* find the next parent */
1577 assert(trg_parent || trg_mod);
1578 tp = NULL;
1579 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1580 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1581 break;
1582 }
1583 }
1584 if (!tp) {
1585 /* schema node not found */
1586 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1587 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1588 free(path);
Michal Vaskoddd76592022-01-17 13:34:48 +01001589 return LY_ENOTFOUND;
1590 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001591
Michal Vasko9beceb82022-04-05 12:14:15 +02001592 trg_parent = tp;
1593 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001594
Michal Vasko9beceb82022-04-05 12:14:15 +02001595 /* success */
1596 *trg_schema = trg_parent;
1597 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001598}
1599
1600/**
Michal Vasko52927e22020-03-16 17:26:14 +01001601 * @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 +02001602 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001603 * 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 +02001604 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001605 * @param[in] node Node to duplicate.
1606 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001607 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001608 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1609 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001610 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1611 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001612 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1613 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001614 */
Michal Vasko52927e22020-03-16 17:26:14 +01001615static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001616lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1617 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001618{
Michal Vasko52927e22020-03-16 17:26:14 +01001619 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001620 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001621 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001622 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001623 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001624 const struct lysc_type *type;
1625 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001626
Michal Vasko52927e22020-03-16 17:26:14 +01001627 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001628
Michal Vasko19175b62022-04-01 09:17:07 +02001629 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001630 if (options & LYD_DUP_NO_EXT) {
1631 /* no not duplicate this subtree */
1632 return LY_SUCCESS;
1633 }
1634
Michal Vasko19175b62022-04-01 09:17:07 +02001635 /* we need to use the same context */
1636 trg_ctx = LYD_CTX(node);
1637 }
1638
Michal Vasko52927e22020-03-16 17:26:14 +01001639 if (!node->schema) {
1640 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001641 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001642 } else {
1643 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001644 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001645 case LYS_ACTION:
1646 case LYS_NOTIF:
1647 case LYS_CONTAINER:
1648 case LYS_LIST:
1649 dup = calloc(1, sizeof(struct lyd_node_inner));
1650 break;
1651 case LYS_LEAF:
1652 case LYS_LEAFLIST:
1653 dup = calloc(1, sizeof(struct lyd_node_term));
1654 break;
1655 case LYS_ANYDATA:
1656 case LYS_ANYXML:
1657 dup = calloc(1, sizeof(struct lyd_node_any));
1658 break;
1659 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001660 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001661 ret = LY_EINT;
1662 goto error;
1663 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001664 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001665 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001666
Michal Vaskof6df0a02020-06-16 13:08:34 +02001667 if (options & LYD_DUP_WITH_FLAGS) {
1668 dup->flags = node->flags;
1669 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001670 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001671 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001672 if (trg_ctx == LYD_CTX(node)) {
1673 dup->schema = node->schema;
1674 } else {
Michal Vasko27f536f2022-04-01 09:17:30 +02001675 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
1676 if (ret) {
1677 /* has no schema but is not an opaque node */
1678 free(dup);
1679 dup = NULL;
1680 goto error;
1681 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001682 }
Michal Vasko52927e22020-03-16 17:26:14 +01001683 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001684
Michal Vasko9cf62422021-07-01 13:29:32 +02001685 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001686 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001687 if (!node->schema) {
1688 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1689 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1690 }
1691 } else {
1692 LY_LIST_FOR(node->meta, meta) {
1693 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1694 }
Michal Vasko25a32822020-07-09 15:48:22 +02001695 }
1696 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001697
1698 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001699 if (!dup->schema) {
1700 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1701 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1702 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001703
1704 if (options & LYD_DUP_RECURSIVE) {
1705 /* duplicate all the children */
1706 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001707 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001708 }
1709 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001710 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1711 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1712 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1713 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001714 opaq->hints = orig->hints;
1715 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001716 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001717 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001718 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001719 }
Michal Vasko52927e22020-03-16 17:26:14 +01001720 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1721 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1722 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1723
1724 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001725 if (trg_ctx == LYD_CTX(node)) {
1726 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1727 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1728 } else {
1729 /* store canonical value in the target context */
1730 val_can = lyd_get_value(node);
1731 type = ((struct lysc_node_leaf *)term->schema)->type;
1732 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1733 LYD_HINT_DATA, term->schema, NULL);
1734 LY_CHECK_GOTO(ret, error);
1735 }
Michal Vasko52927e22020-03-16 17:26:14 +01001736 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1737 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1738 struct lyd_node *child;
1739
1740 if (options & LYD_DUP_RECURSIVE) {
1741 /* duplicate all the children */
1742 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001743 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001744 }
Michal Vasko69730152020-10-09 16:30:07 +02001745 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001746 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001747 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001748 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001749 }
1750 }
1751 lyd_hash(dup);
1752 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001753 dup->hash = node->hash;
1754 any = (struct lyd_node_any *)node;
1755 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001756 }
1757
Michal Vasko52927e22020-03-16 17:26:14 +01001758 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001759 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001760
1761 if (dup_p) {
1762 *dup_p = dup;
1763 }
1764 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001765
1766error:
Michal Vasko52927e22020-03-16 17:26:14 +01001767 lyd_free_tree(dup);
1768 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001769}
1770
Michal Vasko29d674b2021-08-25 11:18:35 +02001771/**
1772 * @brief Get a parent node to connect duplicated subtree to.
1773 *
1774 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001775 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001776 * @param[in] parent Initial parent to connect to.
1777 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1778 * @param[out] dup_parent First duplicated parent node, if any.
1779 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1780 * @return LY_ERR value.
1781 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001782static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001783lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1784 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001785{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001786 const struct lyd_node_inner *orig_parent, *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001787 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001788
1789 *dup_parent = NULL;
1790 *local_parent = NULL;
1791
Michal Vasko83522192022-07-20 08:07:34 +02001792 if (node->flags & LYD_EXT) {
1793 ext_parent = 1;
1794 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001795 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
Michal Vasko83522192022-07-20 08:07:34 +02001796 if (ext_parent) {
1797 /* use the standard context */
1798 trg_ctx = LYD_CTX(orig_parent);
1799 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001800 if (parent && (parent->schema == orig_parent->schema)) {
1801 /* stop creating parents, connect what we have into the provided parent */
1802 iter = parent;
1803 repeat = 0;
1804 } else {
1805 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001806 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 +02001807 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001808 }
1809 if (!*local_parent) {
1810 *local_parent = (struct lyd_node_inner *)iter;
1811 }
1812 if (iter->child) {
1813 /* 1) list - add after keys
1814 * 2) provided parent with some children */
1815 iter->child->prev->next = *dup_parent;
1816 if (*dup_parent) {
1817 (*dup_parent)->prev = iter->child->prev;
1818 iter->child->prev = *dup_parent;
1819 }
1820 } else {
1821 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1822 }
1823 if (*dup_parent) {
1824 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1825 }
1826 *dup_parent = (struct lyd_node *)iter;
Michal Vasko83522192022-07-20 08:07:34 +02001827 if (orig_parent->flags & LYD_EXT) {
1828 ext_parent = 1;
1829 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001830 }
1831
1832 if (repeat && parent) {
1833 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001834 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001835 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001836 return LY_EINVAL;
1837 }
1838
1839 return LY_SUCCESS;
1840}
1841
1842static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001843lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1844 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001845{
1846 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001847 const struct lyd_node *orig; /* original node to be duplicated */
1848 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001849 struct lyd_node *top = NULL; /* the most higher created node */
1850 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001851
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001852 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001853
1854 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001855 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 +02001856 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001857 } else {
1858 local_parent = parent;
1859 }
1860
Radek Krejci22ebdba2019-07-25 13:59:43 +02001861 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001862 if (lysc_is_key(orig->schema)) {
1863 if (local_parent) {
1864 /* the key must already exist in the parent */
1865 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001866 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001867 } else {
1868 assert(!(options & LYD_DUP_WITH_PARENTS));
1869 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001870 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001871 LY_CHECK_GOTO(rc, error);
1872 }
1873 } else {
1874 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001875 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1876 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001877 LY_CHECK_GOTO(rc, error);
1878 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001879 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001880 break;
1881 }
1882 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001883
Michal Vasko3a41dff2020-07-15 14:30:28 +02001884 if (dup) {
1885 *dup = first;
1886 }
1887 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001888
1889error:
1890 if (top) {
1891 lyd_free_tree(top);
1892 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001893 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001894 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001895 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001896}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001897
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001898/**
1899 * @brief Check the context of node and parent when duplicating nodes.
1900 *
1901 * @param[in] node Node to duplicate.
1902 * @param[in] parent Parent of the duplicated node(s).
1903 * @return LY_ERR value.
1904 */
1905static LY_ERR
1906lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1907{
1908 const struct lyd_node *iter;
1909
1910 if (!node || !parent) {
1911 return LY_SUCCESS;
1912 }
1913
1914 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1915 /* try to find top-level ext data parent */
1916 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1917
1918 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
1919 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1920 return LY_EINVAL;
1921 }
1922 }
1923
1924 return LY_SUCCESS;
1925}
1926
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001927LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001928lyd_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 +02001929{
Michal Vaskoddd76592022-01-17 13:34:48 +01001930 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001931 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001932
1933 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1934}
1935
1936LIBYANG_API_DEF LY_ERR
1937lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1938 uint32_t options, struct lyd_node **dup)
1939{
1940 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1941
1942 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001943}
1944
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001945LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001946lyd_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 +02001947{
Michal Vaskoddd76592022-01-17 13:34:48 +01001948 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001949 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001950
1951 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1952}
1953
1954LIBYANG_API_DEF LY_ERR
1955lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1956 uint32_t options, struct lyd_node **dup)
1957{
1958 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1959
1960 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001961}
1962
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001963LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001964lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001965{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001966 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02001967 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02001968 struct lyd_meta *mt, *last;
1969
Michal Vasko3a41dff2020-07-15 14:30:28 +02001970 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001971
Michal Vasko33c48972022-07-20 10:28:07 +02001972 /* log to node context but value must always use the annotation context */
1973 ctx = meta->annotation->module->ctx;
1974
Michal Vasko25a32822020-07-09 15:48:22 +02001975 /* create a copy */
1976 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001977 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001978 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02001979 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001980 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02001981 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001982
1983 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001984 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001985 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001986 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001987 last->next = mt;
1988 } else {
1989 node->meta = mt;
1990 }
1991
Radek Krejci011e4aa2020-09-04 15:22:31 +02001992finish:
1993 if (ret) {
1994 lyd_free_meta_single(mt);
1995 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001996 *dup = mt;
1997 }
1998 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001999}
2000
Michal Vasko4490d312020-06-16 13:08:55 +02002001/**
2002 * @brief Merge a source sibling into target siblings.
2003 *
2004 * @param[in,out] first_trg First target sibling, is updated if top-level.
2005 * @param[in] parent_trg Target parent.
2006 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002007 * @param[in] merge_cb Optional merge callback.
2008 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002009 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002010 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002011 * @return LY_ERR value.
2012 */
2013static LY_ERR
2014lyd_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 +02002015 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002016{
Michal Vasko4490d312020-06-16 13:08:55 +02002017 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002018 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002019 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002020 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002021 struct lyd_dup_inst *child_dup_inst = NULL;
2022 LY_ERR ret;
2023 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002024
2025 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002026 if (!sibling_src->schema) {
2027 /* try to find the same opaque node */
2028 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2029 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002030 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002031 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002032 } else {
2033 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002034 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002035 }
2036
Michal Vaskocd3f6172021-05-18 16:14:50 +02002037 if (match_trg) {
2038 /* update match as needed */
2039 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2040 } else {
2041 /* first instance of this node */
2042 first_inst = 1;
2043 }
2044
2045 if (match_trg) {
2046 /* call callback */
2047 if (merge_cb) {
2048 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2049 }
2050
Michal Vasko4490d312020-06-16 13:08:55 +02002051 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002052 if (!match_trg->schema) {
2053 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2054 /* update value */
2055 opaq_trg = (struct lyd_node_opaq *)match_trg;
2056 opaq_src = (struct lyd_node_opaq *)sibling_src;
2057
2058 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2059 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2060 opaq_trg->hints = opaq_src->hints;
2061
2062 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2063 opaq_trg->format = opaq_src->format;
2064 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2065 &opaq_trg->val_prefix_data);
2066 }
2067 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2068 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002069 /* since they are different, they cannot both be default */
2070 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2071
Michal Vasko3a41dff2020-07-15 14:30:28 +02002072 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2073 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002074 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002075 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2076 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002077 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002078
2079 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002080 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002081 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002082 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002083 /* update value */
2084 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002085 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002086
2087 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002088 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002089 }
2090
2091 /* check descendants, recursively */
2092 ret = LY_SUCCESS;
2093 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2094 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2095 &child_dup_inst);
2096 if (ret) {
2097 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002098 }
2099 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002100 lyd_dup_inst_free(child_dup_inst);
2101 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002102 } else {
2103 /* node not found, merge it */
2104 if (options & LYD_MERGE_DESTRUCT) {
2105 dup_src = (struct lyd_node *)sibling_src;
2106 lyd_unlink_tree(dup_src);
2107 /* spend it */
2108 *sibling_src_p = NULL;
2109 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002110 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 +02002111 }
2112
Michal Vasko7e8315b2021-08-03 17:01:06 +02002113 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2114 /* set LYD_NEW for all the new nodes, required for validation */
2115 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2116 elem->flags |= LYD_NEW;
2117 LYD_TREE_DFS_END(dup_src, elem);
2118 }
Michal Vasko4490d312020-06-16 13:08:55 +02002119 }
2120
Michal Vaskocd3f6172021-05-18 16:14:50 +02002121 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002122 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002123
2124 if (first_inst) {
2125 /* remember not to find this instance next time */
2126 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2127 }
2128
2129 /* call callback, no source node */
2130 if (merge_cb) {
2131 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2132 }
Michal Vasko4490d312020-06-16 13:08:55 +02002133 }
2134
2135 return LY_SUCCESS;
2136}
2137
Michal Vasko3a41dff2020-07-15 14:30:28 +02002138static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002139lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2140 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002141{
2142 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002143 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002144 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002145 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002146
2147 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002148 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2149 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002150
2151 if (!source) {
2152 /* nothing to merge */
2153 return LY_SUCCESS;
2154 }
2155
Michal Vasko831422b2020-11-24 11:20:51 +01002156 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002157 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002158 return LY_EINVAL;
2159 }
2160
2161 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002162 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2163 /* skip data nodes from different modules */
2164 continue;
2165 }
2166
Radek Krejci857189e2020-09-01 13:26:36 +02002167 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002168 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2169 if (ret) {
2170 break;
2171 }
Michal Vasko4490d312020-06-16 13:08:55 +02002172 if (first && !sibling_src) {
2173 /* source was spent (unlinked), move to the next node */
2174 source = tmp;
2175 }
2176
Michal Vasko3a41dff2020-07-15 14:30:28 +02002177 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002178 break;
2179 }
2180 }
2181
2182 if (options & LYD_MERGE_DESTRUCT) {
2183 /* free any leftover source data that were not merged */
2184 lyd_free_siblings((struct lyd_node *)source);
2185 }
2186
Michal Vaskocd3f6172021-05-18 16:14:50 +02002187 lyd_dup_inst_free(dup_inst);
2188 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002189}
2190
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002191LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002192lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002193{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002194 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002195}
2196
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002197LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002198lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002199{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002200 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2201}
2202
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002203LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002204lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2205 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2206{
2207 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002208}
2209
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002210static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002211lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002212{
Michal Vasko14654712020-02-06 08:35:21 +01002213 /* ending \0 */
2214 ++reqlen;
2215
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002216 if (reqlen > *buflen) {
2217 if (is_static) {
2218 return LY_EINCOMPLETE;
2219 }
2220
2221 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2222 if (!*buffer) {
2223 return LY_EMEM;
2224 }
2225
2226 *buflen = reqlen;
2227 }
2228
2229 return LY_SUCCESS;
2230}
2231
Michal Vaskod59035b2020-07-08 12:00:06 +02002232LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002233lyd_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 +02002234{
2235 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002236 size_t len;
2237 const char *val;
2238 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002239
Michal Vasko824d0832021-11-04 15:36:51 +01002240 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002241 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002242 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002243 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002244
2245 quot = '\'';
2246 if (strchr(val, '\'')) {
2247 quot = '"';
2248 }
2249 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002250 }
2251
2252 return LY_SUCCESS;
2253}
2254
2255/**
2256 * @brief Append leaf-list value predicate to path.
2257 *
2258 * @param[in] node Node to print.
2259 * @param[in,out] buffer Buffer to print to.
2260 * @param[in,out] buflen Current buffer length.
2261 * @param[in,out] bufused Current number of characters used in @p buffer.
2262 * @param[in] is_static Whether buffer is static or can be reallocated.
2263 * @return LY_ERR
2264 */
2265static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002266lyd_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 +02002267{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002268 size_t len;
2269 const char *val;
2270 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002271
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002272 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002273 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002274 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002275
2276 quot = '\'';
2277 if (strchr(val, '\'')) {
2278 quot = '"';
2279 }
2280 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2281
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002282 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002283}
2284
2285/**
2286 * @brief Append node position (relative to its other instances) predicate to path.
2287 *
2288 * @param[in] node Node to print.
2289 * @param[in,out] buffer Buffer to print to.
2290 * @param[in,out] buflen Current buffer length.
2291 * @param[in,out] bufused Current number of characters used in @p buffer.
2292 * @param[in] is_static Whether buffer is static or can be reallocated.
2293 * @return LY_ERR
2294 */
2295static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002296lyd_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 +02002297{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002298 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002299 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002300 char *val = NULL;
2301 LY_ERR rc;
2302
Michal Vasko50cc0562021-05-18 16:15:43 +02002303 pos = lyd_list_pos(node);
2304 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002305 return LY_EMEM;
2306 }
2307
2308 len = 1 + strlen(val) + 1;
2309 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2310 if (rc != LY_SUCCESS) {
2311 goto cleanup;
2312 }
2313
2314 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2315
2316cleanup:
2317 free(val);
2318 return rc;
2319}
2320
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002321LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002322lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2323{
Radek Krejci857189e2020-09-01 13:26:36 +02002324 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002325 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002326 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002327 const struct lyd_node *iter, *parent;
2328 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002329 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002330
2331 LY_CHECK_ARG_RET(NULL, node, NULL);
2332 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002333 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002334 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002335 } else {
2336 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002337 }
2338
2339 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002340 case LYD_PATH_STD:
2341 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002342 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002343 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002344 ++depth;
2345 }
2346
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002347 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002348 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002349 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002350 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002351iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002352 /* get the module */
2353 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2354 parent = lyd_parent(iter);
2355 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2356 if (prev_mod == mod) {
2357 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002358 }
2359
2360 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002361 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2362 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002363 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2364 if (rc != LY_SUCCESS) {
2365 break;
2366 }
2367
2368 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002369 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002370
Michal Vasko790b2bc2020-08-03 13:35:06 +02002371 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002372 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002373 switch (iter->schema->nodetype) {
2374 case LYS_LIST:
2375 if (iter->schema->flags & LYS_KEYLESS) {
2376 /* print its position */
2377 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2378 } else {
2379 /* print all list keys in predicates */
2380 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2381 }
2382 break;
2383 case LYS_LEAFLIST:
2384 if (iter->schema->flags & LYS_CONFIG_W) {
2385 /* print leaf-list value */
2386 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2387 } else {
2388 /* print its position */
2389 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2390 }
2391 break;
2392 default:
2393 /* nothing to print more */
2394 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002395 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002396 }
2397 if (rc != LY_SUCCESS) {
2398 break;
2399 }
2400
Michal Vasko14654712020-02-06 08:35:21 +01002401 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002402 }
2403 break;
2404 }
2405
2406 return buffer;
2407}
Michal Vaskoe444f752020-02-10 12:20:06 +01002408
Michal Vaskodbf3e652022-10-21 08:46:25 +02002409char *
2410lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2411{
2412 uint32_t depth;
2413 size_t bufused = 0, buflen = 0, len;
2414 char *buffer = NULL;
2415 const struct lyd_node *iter, *parent;
2416 const struct lys_module *mod, *prev_mod;
2417 LY_ERR rc = LY_SUCCESS;
2418
2419 switch (pathtype) {
2420 case LYD_PATH_STD:
2421 case LYD_PATH_STD_NO_LAST_PRED:
2422 for (depth = 1; depth <= dnodes->count; ++depth) {
2423 /* current node */
2424 iter = dnodes->dnodes[depth - 1];
2425 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2426
2427 /* parent */
2428 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko8c320b82022-11-21 15:51:57 +01002429 assert(!parent || !iter->schema || !parent->schema || (lysc_data_parent(iter->schema) == parent->schema) ||
2430 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002431
2432 /* get module to print, if any */
2433 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2434 if (prev_mod == mod) {
2435 mod = NULL;
2436 }
2437
2438 /* realloc string */
2439 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2440 strlen(((struct lyd_node_opaq *)iter)->name.name));
2441 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2442 break;
2443 }
2444
2445 /* print next node */
2446 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2447
2448 /* do not always print the last (first) predicate */
2449 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2450 switch (iter->schema->nodetype) {
2451 case LYS_LIST:
2452 if (iter->schema->flags & LYS_KEYLESS) {
2453 /* print its position */
2454 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2455 } else {
2456 /* print all list keys in predicates */
2457 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2458 }
2459 break;
2460 case LYS_LEAFLIST:
2461 if (iter->schema->flags & LYS_CONFIG_W) {
2462 /* print leaf-list value */
2463 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2464 } else {
2465 /* print its position */
2466 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2467 }
2468 break;
2469 default:
2470 /* nothing to print more */
2471 break;
2472 }
2473 }
2474 if (rc) {
2475 break;
2476 }
2477 }
2478 break;
2479 }
2480
2481 return buffer;
2482}
2483
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002484LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002485lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2486{
2487 struct lyd_meta *ret = NULL;
2488 const struct ly_ctx *ctx;
2489 const char *prefix, *tmp;
2490 char *str;
2491 size_t pref_len, name_len;
2492
2493 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002494 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002495
2496 if (!first) {
2497 return NULL;
2498 }
2499
2500 ctx = first->annotation->module->ctx;
2501
2502 /* parse the name */
2503 tmp = name;
2504 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2505 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2506 return NULL;
2507 }
2508
2509 /* find the module */
2510 if (prefix) {
2511 str = strndup(prefix, pref_len);
2512 module = ly_ctx_get_module_latest(ctx, str);
2513 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002514 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 +02002515 }
2516
2517 /* find the metadata */
2518 LY_LIST_FOR(first, first) {
2519 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2520 ret = (struct lyd_meta *)first;
2521 break;
2522 }
2523 }
2524
2525 return ret;
2526}
2527
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002528LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002529lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2530{
Michal Vasko9beceb82022-04-05 12:14:15 +02002531 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002532 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002533 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002534
Michal Vaskof03ed032020-03-04 13:31:44 +01002535 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002536 if (!siblings) {
2537 /* no data */
2538 if (match) {
2539 *match = NULL;
2540 }
2541 return LY_ENOTFOUND;
2542 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002543
Michal Vasko9beceb82022-04-05 12:14:15 +02002544 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2545 /* create a duplicate in this context */
2546 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2547 target = dup;
2548 }
2549
2550 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2551 /* schema mismatch */
2552 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002553 if (match) {
2554 *match = NULL;
2555 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002556 return LY_ENOTFOUND;
2557 }
2558
Michal Vaskoe78faec2021-04-08 17:24:43 +02002559 /* get first sibling */
2560 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002561
Michal Vasko9e685082021-01-29 14:49:09 +01002562 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002563 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002564 assert(target->hash);
2565
Michal Vaskoe78faec2021-04-08 17:24:43 +02002566 if (lysc_is_dup_inst_list(target->schema)) {
2567 /* we must search the instances from beginning to find the first matching one */
2568 found = 0;
2569 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2570 if (!lyd_compare_single(target, iter, 0)) {
2571 found = 1;
2572 break;
2573 }
2574 }
2575 if (found) {
2576 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002577 } else {
2578 siblings = NULL;
2579 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002580 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002581 /* find by hash */
2582 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2583 siblings = *match_p;
2584 } else {
2585 /* not found */
2586 siblings = NULL;
2587 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002588 }
2589 } else {
2590 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002591 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002592 if (!lyd_compare_single(siblings, target, LYD_COMPARE_OPAQ)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002593 break;
2594 }
2595 }
2596 }
2597
Michal Vasko9beceb82022-04-05 12:14:15 +02002598 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002599 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002600 if (match) {
2601 *match = NULL;
2602 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002603 return LY_ENOTFOUND;
2604 }
2605
Michal Vasko9b368d32020-02-14 13:53:31 +01002606 if (match) {
2607 *match = (struct lyd_node *)siblings;
2608 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002609 return LY_SUCCESS;
2610}
2611
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002612LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002613lyd_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 +02002614 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002615{
2616 LY_ERR rc;
2617 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002618 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002619
Michal Vasko4c583e82020-07-17 12:16:14 +02002620 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002621 if (!siblings) {
2622 /* no data */
2623 if (match) {
2624 *match = NULL;
2625 }
2626 return LY_ENOTFOUND;
2627 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002628
Michal Vasko9beceb82022-04-05 12:14:15 +02002629 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2630 /* parent of ext nodes is useless */
2631 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
2632 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
2633 }
2634
2635 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2636 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002637 if (match) {
2638 *match = NULL;
2639 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002640 return LY_ENOTFOUND;
2641 }
2642
Michal Vaskof03ed032020-03-04 13:31:44 +01002643 if (key_or_value && !val_len) {
2644 val_len = strlen(key_or_value);
2645 }
2646
Michal Vaskob104f112020-07-17 09:54:54 +02002647 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2648 /* create a data node and find the instance */
2649 if (schema->nodetype == LYS_LEAFLIST) {
2650 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002651 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 +02002652 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002653 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002654 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002655 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002656 }
2657
2658 /* find it */
2659 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002660 } else {
2661 /* find the first schema node instance */
2662 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002663 }
2664
Michal Vaskoe444f752020-02-10 12:20:06 +01002665 lyd_free_tree(target);
2666 return rc;
2667}
Michal Vaskoccc02342020-05-21 10:09:21 +02002668
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002669LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002670lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2671{
2672 struct lyd_node **match_p, *first, *iter;
2673 struct lyd_node_inner *parent;
2674
Michal Vasko83ae7772022-06-08 10:01:55 +02002675 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002676 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002677
2678 LY_CHECK_RET(ly_set_new(set));
2679
2680 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2681 /* no data or schema mismatch */
2682 return LY_ENOTFOUND;
2683 }
2684
2685 /* get first sibling */
2686 siblings = lyd_first_sibling(siblings);
2687
2688 parent = siblings->parent;
2689 if (parent && parent->schema && parent->children_ht) {
2690 assert(target->hash);
2691
2692 /* find the first instance */
2693 lyd_find_sibling_first(siblings, target, &first);
2694 if (first) {
2695 /* add it so that it is the first in the set */
2696 if (ly_set_add(*set, first, 1, NULL)) {
2697 goto error;
2698 }
2699
2700 /* find by hash */
2701 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2702 iter = *match_p;
2703 } else {
2704 /* not found */
2705 iter = NULL;
2706 }
2707 while (iter) {
2708 /* add all found nodes into the set */
2709 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2710 goto error;
2711 }
2712
2713 /* find next instance */
2714 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2715 iter = NULL;
2716 } else {
2717 iter = *match_p;
2718 }
2719 }
2720 }
2721 } else {
2722 /* no children hash table */
2723 LY_LIST_FOR(siblings, siblings) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01002724 if (!lyd_compare_single(target, siblings, LYD_COMPARE_OPAQ)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002725 ly_set_add(*set, (void *)siblings, 1, NULL);
2726 }
2727 }
2728 }
2729
2730 if (!(*set)->count) {
2731 return LY_ENOTFOUND;
2732 }
2733 return LY_SUCCESS;
2734
2735error:
2736 ly_set_free(*set, NULL);
2737 *set = NULL;
2738 return LY_EMEM;
2739}
2740
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002741LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002742lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2743{
2744 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2745
2746 for ( ; first; first = first->next) {
2747 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2748 break;
2749 }
2750 }
2751
2752 if (match) {
2753 *match = (struct lyd_node *)first;
2754 }
2755 return first ? LY_SUCCESS : LY_ENOTFOUND;
2756}
2757
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002758LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002759lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2760 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002761{
2762 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002763 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002765 uint32_t i;
2766
Michal Vaskoddd76592022-01-17 13:34:48 +01002767 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002768
Michal Vasko37c69432021-04-12 14:48:24 +02002769 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002770
Michal Vaskoddd76592022-01-17 13:34:48 +01002771 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01002772 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002773 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002774
2775 /* evaluate expression */
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02002776 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2777 LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02002778 LY_CHECK_GOTO(ret, cleanup);
2779
Michal Vaskoc0d91642022-11-03 13:56:29 +01002780 if (xp_set.type != LYXP_SET_NODE_SET) {
2781 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2782 ret = LY_EINVAL;
2783 goto cleanup;
2784 }
2785
Michal Vaskoccc02342020-05-21 10:09:21 +02002786 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002787 ret = ly_set_new(set);
2788 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002789
Michal Vaskoc0d91642022-11-03 13:56:29 +01002790 /* transform into ly_set, allocate memory for all the elements once (even though not all items must be
2791 * elements but most likely will be) */
2792 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2793 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2794 (*set)->size = xp_set.used;
Michal Vaskoccc02342020-05-21 10:09:21 +02002795
Michal Vaskoc0d91642022-11-03 13:56:29 +01002796 for (i = 0; i < xp_set.used; ++i) {
2797 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2798 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
2799 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002800 }
2801 }
2802
2803cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002804 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002805 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01002806 if (ret) {
2807 ly_set_free(*set, NULL);
2808 *set = NULL;
2809 }
Michal Vaskoccc02342020-05-21 10:09:21 +02002810 return ret;
2811}
Radek Krejcica989142020-11-05 11:32:22 +01002812
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002813LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002814lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2815 const struct lyxp_var *vars, struct ly_set **set)
2816{
2817 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2818
2819 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2820}
2821
2822LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01002823lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2824{
2825 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2826
Michal Vaskoddd76592022-01-17 13:34:48 +01002827 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002828}
2829
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002830LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002831lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2832{
Michal Vaskoe3716b32021-12-13 16:58:25 +01002833 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2834
Michal Vaskoddd76592022-01-17 13:34:48 +01002835 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02002836}
2837
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002838LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002839lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2840 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002841{
2842 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002843 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002844 struct lyxp_expr *exp = NULL;
2845
2846 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
2847
2848 /* compile expression */
2849 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
2850 LY_CHECK_GOTO(ret, cleanup);
2851
2852 /* evaluate expression */
Michal Vasko10fabfc2022-08-09 08:55:43 +02002853 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 +02002854 LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002855 LY_CHECK_GOTO(ret, cleanup);
2856
2857 /* transform into boolean */
2858 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
2859 LY_CHECK_GOTO(ret, cleanup);
2860
2861 /* set result */
2862 *result = xp_set.val.bln;
2863
2864cleanup:
2865 lyxp_set_free_content(&xp_set);
2866 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
2867 return ret;
2868}
2869
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002870LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002871lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2872{
2873 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2874}
2875
2876LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002877lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
2878{
Michal Vasko10fabfc2022-08-09 08:55:43 +02002879 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
aPiecekfba75362021-10-07 12:39:48 +02002880}
2881
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002882LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002883lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2884{
2885 LY_ERR ret = LY_SUCCESS;
2886 struct lyxp_expr *expr = NULL;
2887 struct ly_path *lypath = NULL;
2888
2889 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2890
2891 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002892 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 +01002893 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
2894 LY_CHECK_GOTO(ret, cleanup);
2895
2896 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002897 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002898 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 +01002899 LY_CHECK_GOTO(ret, cleanup);
2900
2901 /* evaluate the path */
2902 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
2903
2904cleanup:
2905 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2906 ly_path_free(LYD_CTX(ctx_node), lypath);
2907 return ret;
2908}
2909
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002910LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002911lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2912{
2913 LY_ERR ret;
2914 struct lyd_node *m;
2915
2916 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2917
2918 ret = ly_path_eval(path, tree, &m);
2919 if (ret) {
2920 if (match) {
2921 *match = NULL;
2922 }
2923 return LY_ENOTFOUND;
2924 }
2925
2926 if (match) {
2927 *match = m;
2928 }
2929 return LY_SUCCESS;
2930}