blob: cb7a581a3b62a82bbca56d71572ba758460da624 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Radek Krejci7931b192020-06-25 17:05:03 +020054static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020055lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020056{
Michal Vasko69730152020-10-09 16:30:07 +020057 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020058 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020059 const char *path = in->method.fpath.filepath;
60 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020061
62 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020063 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020064
Radek Krejcif13b87b2020-12-01 22:02:17 +010065 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
66 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020067 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010068 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
69 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020070 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010071 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
72 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020073 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020074 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020075 }
76
Radek Krejci7931b192020-06-25 17:05:03 +020077 return format;
78}
Radek Krejcie7b95092019-05-15 11:03:07 +020079
Michal Vaskoe0665742021-02-11 11:08:44 +010080/**
81 * @brief Parse YANG data into a data tree.
82 *
83 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010084 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010085 * @param[in] parent Parent to connect the parsed nodes to, if any.
86 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
87 * @param[in] in Input handle to read the input from.
88 * @param[in] format Expected format of the data in @p in.
89 * @param[in] parse_opts Options for parser.
90 * @param[in] val_opts Options for validation.
91 * @param[out] op Optional pointer to the parsed operation, if any.
92 * @return LY_ERR value.
93 */
94static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010095lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
96 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +020097{
Michal Vaskoe0665742021-02-11 11:08:44 +010098 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +020099 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100100 struct ly_set parsed = {0};
101 struct lyd_node *first;
102 uint32_t i;
Michal Vaskoddd76592022-01-17 13:34:48 +0100103 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200104
Michal Vaskoe0665742021-02-11 11:08:44 +0100105 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200106
107 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100108 if (first_p) {
109 *first_p = NULL;
110 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200111
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 /* remember input position */
113 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200114
Michal Vaskoe0665742021-02-11 11:08:44 +0100115 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200116 switch (format) {
117 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100118 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed,
119 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200121 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100122 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
123 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200124 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200125 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100126 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
127 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200128 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200129 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100130 LOGARG(ctx, format);
131 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100132 break;
133 }
134 LY_CHECK_GOTO(rc, cleanup);
135
136 if (parent) {
137 /* get first top-level sibling */
138 for (first = parent; first->parent; first = lyd_parent(first)) {}
139 first = lyd_first_sibling(first);
140 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200141 }
142
Michal Vaskoe0665742021-02-11 11:08:44 +0100143 if (!(parse_opts & LYD_PARSE_ONLY)) {
144 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100145 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
146 &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100147 LY_CHECK_GOTO(rc, cleanup);
148 }
Radek Krejci7931b192020-06-25 17:05:03 +0200149
Michal Vaskoe0665742021-02-11 11:08:44 +0100150 /* set the operation node */
151 if (op) {
152 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200153 }
154
155cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100156 if (lydctx) {
157 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200158 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100159 if (rc) {
160 if (parent) {
161 /* free all the parsed subtrees */
162 for (i = 0; i < parsed.count; ++i) {
163 lyd_free_tree(parsed.dnodes[i]);
164 }
165 } else {
166 /* free everything */
167 lyd_free_all(*first_p);
168 *first_p = NULL;
169 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100170 } else if (subtree_sibling) {
171 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100172 }
173 ly_set_erase(&parsed, NULL);
174 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200175}
176
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100177LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100178lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
179 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
180{
181 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
182
183 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
184 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
185 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
186
187 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
188}
189
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100190LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100191lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
192 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
193{
194 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
195 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
196 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
197
Radek Krejcif16e2542021-02-17 15:39:23 +0100198 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100199}
200
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100201LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100202lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
203 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200204{
205 LY_ERR ret;
206 struct ly_in *in;
207
208 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100209 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200210
211 ly_in_free(in, 0);
212 return ret;
213}
214
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100215LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200216lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200217 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200218{
219 LY_ERR ret;
220 struct ly_in *in;
221
222 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100223 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200224
225 ly_in_free(in, 0);
226 return ret;
227}
228
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100229LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200230lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
231 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200232{
233 LY_ERR ret;
234 struct ly_in *in;
235
236 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100237 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200238
239 ly_in_free(in, 0);
240 return ret;
241}
242
Radek Krejcif16e2542021-02-17 15:39:23 +0100243/**
244 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
245 * for schema nodes locked inside the extension instance.
246 *
247 * At least one of @p parent, @p tree, or @p op must always be set.
248 *
249 * Specific @p data_type values have different parameter meaning as follows:
250 * - ::LYD_TYPE_RPC_NETCONF:
251 * - @p parent - must be NULL, the whole RPC is expected;
252 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
253 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
254 * a separate opaque data tree, even if the function fails, this may be returned;
255 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
256 *
257 * - ::LYD_TYPE_NOTIF_NETCONF:
258 * - @p parent - must be NULL, the whole notification is expected;
259 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
260 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
261 * a separate opaque data tree, even if the function fails, this may be returned;
262 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
263 *
264 * - ::LYD_TYPE_REPLY_NETCONF:
265 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
266 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
267 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
268 * a separate opaque data tree, even if the function fails, this may be returned;
269 * - @p op - must be NULL, the reply is appended to the RPC;
270 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
271 *
272 * @param[in] ctx libyang context.
273 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
274 * @param[in] parent Optional parent to connect the parsed nodes to.
275 * @param[in] in Input handle to read the input from.
276 * @param[in] format Expected format of the data in @p in.
277 * @param[in] data_type Expected operation to parse (@ref datatype).
278 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
279 * @param[out] op Optional parsed operation node.
280 * @return LY_ERR value.
281 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
282 */
283static LY_ERR
284lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
285 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200286{
Michal Vaskoe0665742021-02-11 11:08:44 +0100287 LY_ERR rc = LY_SUCCESS;
288 struct lyd_ctx *lydctx = NULL;
289 struct ly_set parsed = {0};
290 struct lyd_node *first = NULL, *envp = NULL;
291 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200292
Michal Vasko27fb0262021-02-23 09:42:01 +0100293 if (!ctx) {
294 ctx = LYD_CTX(parent);
295 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200296 if (tree) {
297 *tree = NULL;
298 }
299 if (op) {
300 *op = NULL;
301 }
302
Radek Krejci7931b192020-06-25 17:05:03 +0200303 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200304
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 /* remember input position */
306 in->func_start = in->current;
307
Michal Vaskoe0665742021-02-11 11:08:44 +0100308 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100309 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100310 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100311 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
312 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
313 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100314 }
Michal Vasko140ede92022-05-10 09:27:30 +0200315 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100316 val_opts = 0;
317
318 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200319 switch (format) {
320 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100321 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100322 if (rc && envp) {
323 /* special situation when the envelopes were parsed successfully */
324 if (tree) {
325 *tree = envp;
Michal Vasko73792a12022-05-10 09:28:45 +0200326 } else {
327 lyd_free_all(envp);
Michal Vasko299d5d12021-02-16 16:36:37 +0100328 }
Michal Vasko73792a12022-05-10 09:28:45 +0200329 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100330 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100331 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200332 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100333 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100334 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200335 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100336 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100337 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200338 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100339 LOGARG(ctx, format);
340 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200341 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200342 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100343 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200344
Michal Vaskoe0665742021-02-11 11:08:44 +0100345 /* set out params correctly */
346 if (tree) {
347 if (envp) {
348 /* special out param meaning */
349 *tree = envp;
350 } else {
351 *tree = parent ? NULL : first;
352 }
353 }
354 if (op) {
355 *op = lydctx->op_node;
356 }
357
358cleanup:
359 if (lydctx) {
360 lydctx->free(lydctx);
361 }
362 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200363 /* free all the parsed nodes */
364 if (parsed.count) {
365 i = parsed.count;
366 do {
367 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100368 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200369 } while (i);
370 }
371 if (tree && ((format != LYD_XML) || !envp)) {
372 *tree = NULL;
373 }
374 if (op) {
375 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100376 }
377 }
378 ly_set_erase(&parsed, NULL);
379 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200380}
Radek Krejci084289f2019-07-09 17:35:30 +0200381
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100382LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100383lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
384 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
385{
386 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
387
388 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
389}
390
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100391LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100392lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
393 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
394{
395 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
396
397 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
398
399 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
400}
401
Michal Vasko90932a92020-02-12 14:33:03 +0100402struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200403lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100404{
Michal Vaskob104f112020-07-17 09:54:54 +0200405 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100406 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200407 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100408 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100409
Michal Vaskob104f112020-07-17 09:54:54 +0200410 assert(new_node);
411
Michal Vasko9beceb82022-04-05 12:14:15 +0200412 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200413 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100414 return NULL;
415 }
416
Michal Vasko93b16062020-12-09 18:14:18 +0100417 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100418 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100419 getnext_opts = LYS_GETNEXT_OUTPUT;
420 }
421
Michal Vaskoa2756f02021-07-09 13:20:28 +0200422 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200423 /* find the anchor using hashes */
424 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100425 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200426 while (schema) {
427 /* keep trying to find the first existing instance of the closest following schema sibling,
428 * otherwise return NULL - inserting at the end */
429 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
430 break;
431 }
432
Michal Vasko93b16062020-12-09 18:14:18 +0100433 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200434 }
435 } else {
436 /* find the anchor without hashes */
437 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200438 sparent = lysc_data_parent(new_node->schema);
439 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200440 /* we are in top-level, skip all the data from preceding modules */
441 LY_LIST_FOR(match, match) {
442 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
443 break;
444 }
445 }
446 }
447
448 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100449 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200450
451 found = 0;
452 LY_LIST_FOR(match, match) {
453 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
454 /* we have found an opaque node, which must be at the end, so use it OR
455 * modules do not match, so we must have traversed all the data from new_node module (if any),
456 * we have found the first node of the next module, that is what we want */
457 break;
458 }
459
460 /* skip schema nodes until we find the instantiated one */
461 while (!found) {
462 if (new_node->schema == schema) {
463 /* we have found the schema of the new node, continue search to find the first
464 * data node with a different schema (after our schema) */
465 found = 1;
466 break;
467 }
468 if (match->schema == schema) {
469 /* current node (match) is a data node still before the new node, continue search in data */
470 break;
471 }
Michal Vasko93b16062020-12-09 18:14:18 +0100472 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200473 assert(schema);
474 }
475
476 if (found && (match->schema != new_node->schema)) {
477 /* find the next node after we have found our node schema data instance */
478 break;
479 }
480 }
Michal Vasko90932a92020-02-12 14:33:03 +0100481 }
482
483 return match;
484}
485
Michal Vaskoc61dd062022-06-07 11:01:28 +0200486void
Michal Vaskof03ed032020-03-04 13:31:44 +0100487lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100488{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100489 struct lyd_node_inner *par;
490
Michal Vasko90932a92020-02-12 14:33:03 +0100491 assert(!node->next && (node->prev == node));
492
493 node->next = sibling->next;
494 node->prev = sibling;
495 sibling->next = node;
496 if (node->next) {
497 /* sibling had a succeeding node */
498 node->next->prev = node;
499 } else {
500 /* sibling was last, find first sibling and change its prev */
501 if (sibling->parent) {
502 sibling = sibling->parent->child;
503 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200504 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100505 }
506 sibling->prev = node;
507 }
508 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100509
Michal Vasko9f96a052020-03-10 09:41:45 +0100510 for (par = node->parent; par; par = par->parent) {
511 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
512 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100513 par->flags &= ~LYD_DEFAULT;
514 }
515 }
Michal Vasko90932a92020-02-12 14:33:03 +0100516}
517
Michal Vaskoc61dd062022-06-07 11:01:28 +0200518void
Michal Vaskof03ed032020-03-04 13:31:44 +0100519lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100520{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100521 struct lyd_node_inner *par;
522
Michal Vasko90932a92020-02-12 14:33:03 +0100523 assert(!node->next && (node->prev == node));
524
525 node->next = sibling;
526 /* covers situation of sibling being first */
527 node->prev = sibling->prev;
528 sibling->prev = node;
529 if (node->prev->next) {
530 /* sibling had a preceding node */
531 node->prev->next = node;
532 } else if (sibling->parent) {
533 /* sibling was first and we must also change parent child pointer */
534 sibling->parent->child = node;
535 }
536 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100537
Michal Vasko9f96a052020-03-10 09:41:45 +0100538 for (par = node->parent; par; par = par->parent) {
539 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
540 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100541 par->flags &= ~LYD_DEFAULT;
542 }
543 }
Michal Vasko90932a92020-02-12 14:33:03 +0100544}
545
546/**
Michal Vaskob104f112020-07-17 09:54:54 +0200547 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100548 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100549 * Handles inserting into NP containers and key-less lists.
550 *
Michal Vasko90932a92020-02-12 14:33:03 +0100551 * @param[in] parent Parent to insert into.
552 * @param[in] node Node to insert.
553 */
554static void
Michal Vaskob104f112020-07-17 09:54:54 +0200555lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100556{
557 struct lyd_node_inner *par;
558
Radek Krejcia1c1e542020-09-29 16:06:52 +0200559 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100560 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100561
562 par = (struct lyd_node_inner *)parent;
563
Michal Vaskob104f112020-07-17 09:54:54 +0200564 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100565 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100566
Michal Vaskod989ba02020-08-24 10:59:24 +0200567 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100568 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
569 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100570 par->flags &= ~LYD_DEFAULT;
571 }
572 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200573}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100574
Michal Vasko751cb4d2020-07-14 12:25:28 +0200575/**
576 * @brief Learn whether a list instance has all the keys.
577 *
578 * @param[in] list List instance to check.
579 * @return non-zero if all the keys were found,
580 * @return 0 otherwise.
581 */
582static int
583lyd_insert_has_keys(const struct lyd_node *list)
584{
585 const struct lyd_node *key;
586 const struct lysc_node *skey = NULL;
587
588 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200589 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200590 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
591 if (!key || (key->schema != skey)) {
592 /* key missing */
593 return 0;
594 }
595
596 key = key->next;
597 }
598
599 /* all keys found */
600 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100601}
602
603void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200604lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +0100605{
Michal Vaskob104f112020-07-17 09:54:54 +0200606 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100607
Michal Vaskob104f112020-07-17 09:54:54 +0200608 /* inserting list without its keys is not supported */
609 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100610 assert(!parent || !parent->schema ||
611 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100612
Michal Vaskob104f112020-07-17 09:54:54 +0200613 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100614 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100615 }
Michal Vasko90932a92020-02-12 14:33:03 +0100616
Michal Vaskob104f112020-07-17 09:54:54 +0200617 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100618 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100619
Michal Vasko9beceb82022-04-05 12:14:15 +0200620 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200621 /* no next anchor */
622 anchor = NULL;
623 } else {
624 /* find the anchor, our next node, so we can insert before it */
625 anchor = lyd_insert_get_next_anchor(first_sibling, node);
626 }
627
Michal Vaskob104f112020-07-17 09:54:54 +0200628 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200629 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200630 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100631 if (!parent && (*first_sibling_p == anchor)) {
632 /* move first sibling */
633 *first_sibling_p = node;
634 }
Michal Vaskob104f112020-07-17 09:54:54 +0200635 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200636 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200637 lyd_insert_after_node(first_sibling->prev, node);
638 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200639 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200640 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100641 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200642 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200643 *first_sibling_p = node;
644 }
645
646 /* insert into parent HT */
647 lyd_insert_hash(node);
648
649 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200650 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200651 lyd_hash(parent);
652
653 /* now we can insert even the list into its parent HT */
654 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100655 }
Michal Vasko90932a92020-02-12 14:33:03 +0100656}
657
Michal Vasko717a4c32020-12-07 09:40:10 +0100658/**
659 * @brief Check schema place of a node to be inserted.
660 *
661 * @param[in] parent Schema node of the parent data node.
662 * @param[in] sibling Schema node of a sibling data node.
663 * @param[in] schema Schema node if the data node to be inserted.
664 * @return LY_SUCCESS on success.
665 * @return LY_EINVAL if the place is invalid.
666 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100667static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100668lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema)
Michal Vaskof03ed032020-03-04 13:31:44 +0100669{
670 const struct lysc_node *par2;
671
Michal Vasko62ed12d2020-05-21 10:08:25 +0200672 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100673 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
674 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100675
Michal Vasko717a4c32020-12-07 09:40:10 +0100676 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100677 /* opaque nodes can be inserted wherever */
678 return LY_SUCCESS;
679 }
680
Michal Vasko717a4c32020-12-07 09:40:10 +0100681 if (!parent) {
682 parent = lysc_data_parent(sibling);
683 }
684
Michal Vaskof03ed032020-03-04 13:31:44 +0100685 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200686 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100687
688 if (parent) {
689 /* inner node */
690 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200691 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200692 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100693 return LY_EINVAL;
694 }
695 } else {
696 /* top-level node */
697 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200698 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100699 return LY_EINVAL;
700 }
701 }
702
703 return LY_SUCCESS;
704}
705
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100706LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200707lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100708{
709 struct lyd_node *iter;
710
Michal Vasko0ab974d2021-02-24 13:18:26 +0100711 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100712 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100713
Michal Vasko717a4c32020-12-07 09:40:10 +0100714 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100715
Michal Vasko0ab974d2021-02-24 13:18:26 +0100716 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100717 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100718 return LY_EINVAL;
719 }
720
721 if (node->parent || node->prev->next) {
722 lyd_unlink_tree(node);
723 }
724
725 while (node) {
726 iter = node->next;
727 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200728 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100729 node = iter;
730 }
731 return LY_SUCCESS;
732}
733
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100734LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100735lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
736{
737 struct lyd_node *iter;
738
739 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
740 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
741
742 if (first->schema && (first->schema->flags & LYS_KEY)) {
743 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
744 return LY_EINVAL;
745 }
746
747 while (first) {
748 iter = first->next;
749 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100750 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100751 first = iter;
752 }
753 return LY_SUCCESS;
754}
755
756LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200757lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100758{
759 struct lyd_node *iter;
760
Michal Vaskob104f112020-07-17 09:54:54 +0200761 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100762 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100763
Michal Vaskob104f112020-07-17 09:54:54 +0200764 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100765 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100766 }
767
Michal Vasko553d0b52020-12-04 16:27:52 +0100768 if (sibling == node) {
769 /* we need to keep the connection to siblings so we can insert into them */
770 sibling = sibling->prev;
771 }
772
Michal Vaskob1b5c262020-03-05 14:29:47 +0100773 if (node->parent || node->prev->next) {
774 lyd_unlink_tree(node);
775 }
776
777 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100778 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200779 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200780 return LY_EINVAL;
781 }
782
Michal Vaskob1b5c262020-03-05 14:29:47 +0100783 iter = node->next;
784 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200785 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100786 node = iter;
787 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100788
Michal Vaskob104f112020-07-17 09:54:54 +0200789 if (first) {
790 /* find the first sibling */
791 *first = sibling;
792 while ((*first)->prev->next) {
793 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100794 }
795 }
796
797 return LY_SUCCESS;
798}
799
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100800LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100801lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
802{
Michal Vaskobce230b2022-04-19 09:55:31 +0200803 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100804 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100805
Michal Vasko717a4c32020-12-07 09:40:10 +0100806 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100807
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200808 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200809 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100810 return LY_EINVAL;
811 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200812 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
813 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100814 return LY_EINVAL;
815 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100816
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100817 lyd_unlink_tree(node);
818 lyd_insert_before_node(sibling, node);
819 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100820
Michal Vaskof03ed032020-03-04 13:31:44 +0100821 return LY_SUCCESS;
822}
823
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100824LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100825lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
826{
Michal Vaskobce230b2022-04-19 09:55:31 +0200827 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100828 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100829
Michal Vasko717a4c32020-12-07 09:40:10 +0100830 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100831
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200832 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200833 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100834 return LY_EINVAL;
835 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200836 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
837 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100838 return LY_EINVAL;
839 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100840
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100841 lyd_unlink_tree(node);
842 lyd_insert_after_node(sibling, node);
843 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100844
Michal Vaskof03ed032020-03-04 13:31:44 +0100845 return LY_SUCCESS;
846}
847
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100848LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200849lyd_unlink_siblings(struct lyd_node *node)
850{
851 struct lyd_node *next, *elem, *first = NULL;
852
853 LY_LIST_FOR_SAFE(node, next, elem) {
854 lyd_unlink_tree(elem);
855 lyd_insert_node(NULL, &first, elem, 1);
856 }
857}
858
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100859LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100860lyd_unlink_tree(struct lyd_node *node)
861{
862 struct lyd_node *iter;
863
864 if (!node) {
865 return;
866 }
867
Michal Vaskob104f112020-07-17 09:54:54 +0200868 /* update hashes while still linked into the tree */
869 lyd_unlink_hash(node);
870
Michal Vaskof03ed032020-03-04 13:31:44 +0100871 /* unlink from siblings */
872 if (node->prev->next) {
873 node->prev->next = node->next;
874 }
875 if (node->next) {
876 node->next->prev = node->prev;
877 } else {
878 /* unlinking the last node */
879 if (node->parent) {
880 iter = node->parent->child;
881 } else {
882 iter = node->prev;
883 while (iter->prev != node) {
884 iter = iter->prev;
885 }
886 }
887 /* update the "last" pointer from the first node */
888 iter->prev = node->prev;
889 }
890
891 /* unlink from parent */
892 if (node->parent) {
893 if (node->parent->child == node) {
894 /* the node is the first child */
895 node->parent->child = node->next;
896 }
897
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200898 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +0200899 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
900 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200901 LY_LIST_FOR(node->parent->child, iter) {
902 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
903 break;
904 }
905 }
906 if (!iter) {
907 node->parent->flags |= LYD_DEFAULT;
908 }
909 }
910
Michal Vaskof03ed032020-03-04 13:31:44 +0100911 node->parent = NULL;
912 }
913
914 node->next = NULL;
915 node->prev = node;
916}
917
Michal Vaskoa5da3292020-08-12 13:10:50 +0200918void
Michal Vasko871a0252020-11-11 18:35:24 +0100919lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200920{
921 struct lyd_meta *last, *iter;
922
923 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200924
925 if (!meta) {
926 return;
927 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200928
929 for (iter = meta; iter; iter = iter->next) {
930 iter->parent = parent;
931 }
932
933 /* insert as the last attribute */
934 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200935 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 last->next = meta;
937 } else {
938 parent->meta = meta;
939 }
940
941 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100942 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200943 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100944 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200945 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200946}
947
948LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100949lyd_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 +0200950 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 +0100951 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 +0100952{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100953 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100954 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +0200955 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100956 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200957 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100958
Michal Vasko9f96a052020-03-10 09:41:45 +0100959 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100960
Michal Vaskoddd76592022-01-17 13:34:48 +0100961 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100962
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200963 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +0100964 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200965 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100966 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200967 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100968 break;
969 }
970 }
971 if (!ant) {
972 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100973 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100974 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100975 ret = LY_EINVAL;
976 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +0100977 }
978
Michal Vasko9f96a052020-03-10 09:41:45 +0100979 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100980 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +0100981 mt->parent = parent;
982 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +0200983 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
984 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 +0100985 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100986 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
987 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
988 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100989
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100990 /* insert as the last attribute */
991 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +0100992 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +0100993 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200994 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +0100996 }
997
Michal Vasko9f96a052020-03-10 09:41:45 +0100998 if (meta) {
999 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001000 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001
1002cleanup:
Michal Vaskoddd76592022-01-17 13:34:48 +01001003 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001004 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001005}
1006
Michal Vaskoa5da3292020-08-12 13:10:50 +02001007void
1008lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1009{
1010 struct lyd_attr *last, *iter;
1011 struct lyd_node_opaq *opaq;
1012
1013 assert(parent && !parent->schema);
1014
1015 if (!attr) {
1016 return;
1017 }
1018
1019 opaq = (struct lyd_node_opaq *)parent;
1020 for (iter = attr; iter; iter = iter->next) {
1021 iter->parent = opaq;
1022 }
1023
1024 /* insert as the last attribute */
1025 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001026 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001027 last->next = attr;
1028 } else {
1029 opaq->attr = attr;
1030 }
1031}
1032
Michal Vasko52927e22020-03-16 17:26:14 +01001033LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001034lyd_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 +01001035 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 +02001036 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 +01001037{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001038 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001039 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001040
1041 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001042 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001043
Michal Vasko2a3722d2021-06-16 11:52:39 +02001044 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001045 value = "";
1046 }
1047
1048 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001049 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001050
Michal Vaskoad92b672020-11-12 13:11:31 +01001051 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001052 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001053 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001054 }
1055 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001056 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001057 }
1058
Michal Vasko52927e22020-03-16 17:26:14 +01001059 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001060 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1061 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001062 *dynamic = 0;
1063 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001064 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001065 }
Michal Vasko501af032020-11-11 20:27:44 +01001066 at->format = format;
1067 at->val_prefix_data = val_prefix_data;
1068 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001069
1070 /* insert as the last attribute */
1071 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001072 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001073 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001074 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001075 last->next = at;
1076 }
1077
Radek Krejci011e4aa2020-09-04 15:22:31 +02001078finish:
1079 if (ret) {
1080 lyd_free_attr_single(ctx, at);
1081 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001082 *attr = at;
1083 }
1084 return LY_SUCCESS;
1085}
1086
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001087LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001088lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001089{
Michal Vasko004d3152020-06-11 19:59:22 +02001090 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001091
Michal Vasko004d3152020-06-11 19:59:22 +02001092 if (ly_path_eval(path, tree, &target)) {
1093 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001094 }
1095
Michal Vasko004d3152020-06-11 19:59:22 +02001096 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001097}
1098
aPiecek2f63f952021-03-30 12:22:18 +02001099/**
1100 * @brief Check the equality of the two schemas from different contexts.
1101 *
1102 * @param schema1 of first node.
1103 * @param schema2 of second node.
1104 * @return 1 if the schemas are equal otherwise 0.
1105 */
1106static ly_bool
1107lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1108{
1109 if (!schema1 && !schema2) {
1110 return 1;
1111 } else if (!schema1 || !schema2) {
1112 return 0;
1113 }
1114
1115 assert(schema1->module->ctx != schema2->module->ctx);
1116
1117 if (schema1->nodetype != schema2->nodetype) {
1118 return 0;
1119 }
1120
1121 if (strcmp(schema1->name, schema2->name)) {
1122 return 0;
1123 }
1124
1125 if (strcmp(schema1->module->name, schema2->module->name)) {
1126 return 0;
1127 }
1128
1129 if (schema1->module->revision || schema2->module->revision) {
1130 if (!schema1->module->revision || !schema2->module->revision) {
1131 return 0;
1132 }
1133 if (strcmp(schema1->module->revision, schema2->module->revision)) {
1134 return 0;
1135 }
1136 }
1137
1138 return 1;
1139}
1140
1141/**
1142 * @brief Check the equality of the schemas for all parent nodes.
1143 *
1144 * Both nodes must be from different contexts.
1145 *
1146 * @param node1 Data of first node.
1147 * @param node2 Data of second node.
1148 * @return 1 if the all related parental schemas are equal otherwise 0.
1149 */
1150static ly_bool
1151lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1152{
1153 const struct lysc_node *parent1, *parent2;
1154
1155 assert(node1 && node2);
1156
1157 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1158 parent1 && parent2;
1159 parent1 = parent1->parent, parent2 = parent2->parent) {
1160 if (!lyd_compare_schema_equal(parent1, parent2)) {
1161 return 0;
1162 }
1163 }
1164
1165 if (parent1 || parent2) {
1166 return 0;
1167 }
1168
1169 return 1;
1170}
1171
1172/**
1173 * @brief Internal implementation of @ref lyd_compare_single.
1174 * @copydoc lyd_compare_single
1175 * @param[in] parental_schemas_checked Flag used for optimization.
1176 * When this function is called for the first time, the flag must be set to 0.
1177 * The @ref lyd_compare_schema_parents_equal should be called only once during
1178 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1179 */
1180static LY_ERR
1181lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
1182 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001183{
1184 const struct lyd_node *iter1, *iter2;
1185 struct lyd_node_term *term1, *term2;
1186 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001187 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001188 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001189
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001190 if (!node1 || !node2) {
1191 if (node1 == node2) {
1192 return LY_SUCCESS;
1193 } else {
1194 return LY_ENOT;
1195 }
1196 }
1197
aPiecek2f63f952021-03-30 12:22:18 +02001198 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1199 /* same contexts */
1200 if (node1->schema != node2->schema) {
1201 return LY_ENOT;
1202 }
1203 } else {
1204 /* different contexts */
1205 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1206 return LY_ENOT;
1207 }
1208 if (!parental_schemas_checked) {
1209 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1210 return LY_ENOT;
1211 }
1212 parental_schemas_checked = 1;
1213 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001214 }
1215
1216 if (node1->hash != node2->hash) {
1217 return LY_ENOT;
1218 }
aPiecek2f63f952021-03-30 12:22:18 +02001219 /* 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 +02001220
Michal Vasko52927e22020-03-16 17:26:14 +01001221 if (!node1->schema) {
1222 opaq1 = (struct lyd_node_opaq *)node1;
1223 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02001224 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
1225 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001226 return LY_ENOT;
1227 }
Michal Vasko52927e22020-03-16 17:26:14 +01001228 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001229 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02001230 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1231 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001232 return LY_ENOT;
1233 }
1234 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001235 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02001236 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
1237 if (strcmp(opaq1->value, opaq2->value)) {
1238 return LY_ENOT;
1239 }
1240 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001241 default:
Michal Vasko52927e22020-03-16 17:26:14 +01001242 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001243 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01001244 return LY_EINT;
1245 }
1246 if (options & LYD_COMPARE_FULL_RECURSION) {
1247 iter1 = opaq1->child;
1248 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001249 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001250 }
1251 return LY_SUCCESS;
1252 } else {
1253 switch (node1->schema->nodetype) {
1254 case LYS_LEAF:
1255 case LYS_LEAFLIST:
1256 if (options & LYD_COMPARE_DEFAULTS) {
1257 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1258 return LY_ENOT;
1259 }
1260 }
1261
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001262 term1 = (struct lyd_node_term *)node1;
1263 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02001264
1265 /* same contexts */
1266 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1267 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
1268 }
1269
1270 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001271 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02001272 return LY_ENOT;
1273 }
1274 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001275 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001276 case LYS_RPC:
1277 case LYS_ACTION:
1278 case LYS_NOTIF:
Michal Vasko52927e22020-03-16 17:26:14 +01001279 if (options & LYD_COMPARE_DEFAULTS) {
1280 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1281 return LY_ENOT;
1282 }
1283 }
1284 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001285 iter1 = lyd_child(node1);
1286 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001287 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001288 }
1289 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001290 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001291 iter1 = lyd_child(node1);
1292 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001293
1294 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1295 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001296 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001297 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001298 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001299 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001300 return LY_ENOT;
1301 }
1302 iter1 = iter1->next;
1303 iter2 = iter2->next;
1304 }
1305 } else {
1306 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1307
Radek Krejci0f969882020-08-21 16:56:47 +02001308all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001309 if (!iter1 && !iter2) {
1310 /* no children, nothing to compare */
1311 return LY_SUCCESS;
1312 }
1313
Michal Vaskod989ba02020-08-24 10:59:24 +02001314 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001315 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001316 return LY_ENOT;
1317 }
1318 }
1319 if (iter1 || iter2) {
1320 return LY_ENOT;
1321 }
1322 }
1323 return LY_SUCCESS;
1324 case LYS_ANYXML:
1325 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001326 any1 = (struct lyd_node_any *)node1;
1327 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001328
1329 if (any1->value_type != any2->value_type) {
1330 return LY_ENOT;
1331 }
1332 switch (any1->value_type) {
1333 case LYD_ANYDATA_DATATREE:
1334 iter1 = any1->value.tree;
1335 iter2 = any2->value.tree;
1336 goto all_children_compare;
1337 case LYD_ANYDATA_STRING:
1338 case LYD_ANYDATA_XML:
1339 case LYD_ANYDATA_JSON:
1340 len1 = strlen(any1->value.str);
1341 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001342 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001343 return LY_ENOT;
1344 }
1345 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001346 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001347 len1 = lyd_lyb_data_length(any1->value.mem);
1348 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02001349 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001350 return LY_ENOT;
1351 }
1352 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001353 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001354 }
1355 }
1356
Michal Vaskob7be7a82020-08-20 09:09:04 +02001357 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001358 return LY_EINT;
1359}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001360
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001361LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001362lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1363{
1364 return lyd_compare_single_(node1, node2, options, 0);
1365}
1366
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001367LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001368lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001369{
Michal Vaskod989ba02020-08-24 10:59:24 +02001370 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001371 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1372 }
1373
Michal Vasko11a81432020-07-28 16:31:29 +02001374 if (node1 == node2) {
1375 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001376 }
Michal Vasko11a81432020-07-28 16:31:29 +02001377 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001378}
1379
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001380LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001381lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1382{
1383 if (!meta1 || !meta2) {
1384 if (meta1 == meta2) {
1385 return LY_SUCCESS;
1386 } else {
1387 return LY_ENOT;
1388 }
1389 }
1390
Michal Vaskoa8083062020-11-06 17:22:10 +01001391 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001392 return LY_ENOT;
1393 }
1394
Michal Vasko21725742020-06-29 11:49:25 +02001395 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1396}
1397
Radek Krejci22ebdba2019-07-25 13:59:43 +02001398/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001399 * @brief Create a copy of the attribute.
1400 *
1401 * @param[in] attr Attribute to copy.
1402 * @param[in] node Opaque where to append the new attribute.
1403 * @param[out] dup Optional created attribute copy.
1404 * @return LY_ERR value.
1405 */
1406static LY_ERR
1407lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1408{
1409 LY_ERR ret = LY_SUCCESS;
1410 struct lyd_attr *a, *last;
1411 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1412
1413 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1414
1415 /* create a copy */
1416 a = calloc(1, sizeof *attr);
1417 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1418
1419 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1420 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1421 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1422 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1423 a->hints = attr->hints;
1424 a->format = attr->format;
1425 if (attr->val_prefix_data) {
1426 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1427 LY_CHECK_GOTO(ret, finish);
1428 }
1429
1430 /* insert as the last attribute */
1431 a->parent = opaq;
1432 if (opaq->attr) {
1433 for (last = opaq->attr; last->next; last = last->next) {}
1434 last->next = a;
1435 } else {
1436 opaq->attr = a;
1437 }
1438
1439finish:
1440 if (ret) {
1441 lyd_free_attr_single(LYD_CTX(node), a);
1442 } else if (dup) {
1443 *dup = a;
1444 }
1445 return LY_SUCCESS;
1446}
1447
1448/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001449 * @brief Find @p schema equivalent in @p trg_ctx.
1450 *
1451 * @param[in] schema Schema node to find.
1452 * @param[in] trg_ctx Target context to search in.
1453 * @param[in] parent Data parent of @p schema, if any.
1454 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1455 * @return LY_RRR value.
1456 */
1457static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02001458lyd_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 +01001459 const struct lysc_node **trg_schema)
1460{
Michal Vasko9beceb82022-04-05 12:14:15 +02001461 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1462 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001463 char *path;
1464
1465 if (!schema) {
1466 /* opaque node */
1467 *trg_schema = NULL;
1468 return LY_SUCCESS;
1469 }
1470
Michal Vasko9beceb82022-04-05 12:14:15 +02001471 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001472 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001473 trg_parent = parent->schema;
1474 src_parent = lysc_data_parent(schema);
1475 }
1476
1477 do {
1478 /* find the next parent */
1479 sp = schema;
1480 while (sp->parent != src_parent) {
1481 sp = sp->parent;
1482 }
1483 src_parent = sp;
1484
1485 if (!src_parent->parent) {
1486 /* find the module first */
1487 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1488 if (!trg_mod) {
1489 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1490 src_parent->module->name);
1491 return LY_ENOTFOUND;
1492 }
1493 }
1494
1495 /* find the next parent */
1496 assert(trg_parent || trg_mod);
1497 tp = NULL;
1498 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1499 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1500 break;
1501 }
1502 }
1503 if (!tp) {
1504 /* schema node not found */
1505 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1506 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1507 free(path);
Michal Vaskoddd76592022-01-17 13:34:48 +01001508 return LY_ENOTFOUND;
1509 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001510
Michal Vasko9beceb82022-04-05 12:14:15 +02001511 trg_parent = tp;
1512 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001513
Michal Vasko9beceb82022-04-05 12:14:15 +02001514 /* success */
1515 *trg_schema = trg_parent;
1516 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001517}
1518
1519/**
Michal Vasko52927e22020-03-16 17:26:14 +01001520 * @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 +02001521 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001522 * 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 +02001523 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001524 * @param[in] node Node to duplicate.
1525 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001526 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001527 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1528 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001529 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1530 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001531 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1532 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001533 */
Michal Vasko52927e22020-03-16 17:26:14 +01001534static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001535lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1536 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001537{
Michal Vasko52927e22020-03-16 17:26:14 +01001538 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001539 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001540 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001541 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001542 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001543 const struct lysc_type *type;
1544 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001545
Michal Vasko52927e22020-03-16 17:26:14 +01001546 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001547
Michal Vasko19175b62022-04-01 09:17:07 +02001548 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001549 if (options & LYD_DUP_NO_EXT) {
1550 /* no not duplicate this subtree */
1551 return LY_SUCCESS;
1552 }
1553
Michal Vasko19175b62022-04-01 09:17:07 +02001554 /* we need to use the same context */
1555 trg_ctx = LYD_CTX(node);
1556 }
1557
Michal Vasko52927e22020-03-16 17:26:14 +01001558 if (!node->schema) {
1559 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001560 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001561 } else {
1562 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001563 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001564 case LYS_ACTION:
1565 case LYS_NOTIF:
1566 case LYS_CONTAINER:
1567 case LYS_LIST:
1568 dup = calloc(1, sizeof(struct lyd_node_inner));
1569 break;
1570 case LYS_LEAF:
1571 case LYS_LEAFLIST:
1572 dup = calloc(1, sizeof(struct lyd_node_term));
1573 break;
1574 case LYS_ANYDATA:
1575 case LYS_ANYXML:
1576 dup = calloc(1, sizeof(struct lyd_node_any));
1577 break;
1578 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001579 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001580 ret = LY_EINT;
1581 goto error;
1582 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001583 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001584 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001585
Michal Vaskof6df0a02020-06-16 13:08:34 +02001586 if (options & LYD_DUP_WITH_FLAGS) {
1587 dup->flags = node->flags;
1588 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001589 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001590 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001591 if (trg_ctx == LYD_CTX(node)) {
1592 dup->schema = node->schema;
1593 } else {
Michal Vasko27f536f2022-04-01 09:17:30 +02001594 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
1595 if (ret) {
1596 /* has no schema but is not an opaque node */
1597 free(dup);
1598 dup = NULL;
1599 goto error;
1600 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001601 }
Michal Vasko52927e22020-03-16 17:26:14 +01001602 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001603
Michal Vasko9cf62422021-07-01 13:29:32 +02001604 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001605 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001606 if (!node->schema) {
1607 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1608 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1609 }
1610 } else {
1611 LY_LIST_FOR(node->meta, meta) {
1612 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1613 }
Michal Vasko25a32822020-07-09 15:48:22 +02001614 }
1615 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001616
1617 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001618 if (!dup->schema) {
1619 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1620 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1621 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001622
1623 if (options & LYD_DUP_RECURSIVE) {
1624 /* duplicate all the children */
1625 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001626 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001627 }
1628 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001629 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1630 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1631 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1632 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001633 opaq->hints = orig->hints;
1634 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001635 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001636 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001637 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001638 }
Michal Vasko52927e22020-03-16 17:26:14 +01001639 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1640 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1641 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1642
1643 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001644 if (trg_ctx == LYD_CTX(node)) {
1645 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1646 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1647 } else {
1648 /* store canonical value in the target context */
1649 val_can = lyd_get_value(node);
1650 type = ((struct lysc_node_leaf *)term->schema)->type;
1651 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1652 LYD_HINT_DATA, term->schema, NULL);
1653 LY_CHECK_GOTO(ret, error);
1654 }
Michal Vasko52927e22020-03-16 17:26:14 +01001655 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1656 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1657 struct lyd_node *child;
1658
1659 if (options & LYD_DUP_RECURSIVE) {
1660 /* duplicate all the children */
1661 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001662 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001663 }
Michal Vasko69730152020-10-09 16:30:07 +02001664 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001665 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001666 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001667 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001668 }
1669 }
1670 lyd_hash(dup);
1671 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001672 dup->hash = node->hash;
1673 any = (struct lyd_node_any *)node;
1674 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001675 }
1676
Michal Vasko52927e22020-03-16 17:26:14 +01001677 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001678 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001679
1680 if (dup_p) {
1681 *dup_p = dup;
1682 }
1683 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001684
1685error:
Michal Vasko52927e22020-03-16 17:26:14 +01001686 lyd_free_tree(dup);
1687 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001688}
1689
Michal Vasko29d674b2021-08-25 11:18:35 +02001690/**
1691 * @brief Get a parent node to connect duplicated subtree to.
1692 *
1693 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001694 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001695 * @param[in] parent Initial parent to connect to.
1696 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1697 * @param[out] dup_parent First duplicated parent node, if any.
1698 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1699 * @return LY_ERR value.
1700 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001701static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001702lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1703 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001704{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001705 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02001706 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001707
1708 *dup_parent = NULL;
1709 *local_parent = NULL;
1710
1711 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1712 if (parent && (parent->schema == orig_parent->schema)) {
1713 /* stop creating parents, connect what we have into the provided parent */
1714 iter = parent;
1715 repeat = 0;
1716 } else {
1717 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001718 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 +02001719 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001720 }
1721 if (!*local_parent) {
1722 *local_parent = (struct lyd_node_inner *)iter;
1723 }
1724 if (iter->child) {
1725 /* 1) list - add after keys
1726 * 2) provided parent with some children */
1727 iter->child->prev->next = *dup_parent;
1728 if (*dup_parent) {
1729 (*dup_parent)->prev = iter->child->prev;
1730 iter->child->prev = *dup_parent;
1731 }
1732 } else {
1733 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1734 }
1735 if (*dup_parent) {
1736 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1737 }
1738 *dup_parent = (struct lyd_node *)iter;
1739 }
1740
1741 if (repeat && parent) {
1742 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001743 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001744 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001745 return LY_EINVAL;
1746 }
1747
1748 return LY_SUCCESS;
1749}
1750
1751static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001752lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1753 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001754{
1755 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001756 const struct lyd_node *orig; /* original node to be duplicated */
1757 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001758 struct lyd_node *top = NULL; /* the most higher created node */
1759 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001760
Michal Vaskoddd76592022-01-17 13:34:48 +01001761 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001762
1763 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001764 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 +02001765 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001766 } else {
1767 local_parent = parent;
1768 }
1769
Radek Krejci22ebdba2019-07-25 13:59:43 +02001770 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001771 if (lysc_is_key(orig->schema)) {
1772 if (local_parent) {
1773 /* the key must already exist in the parent */
1774 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001775 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001776 } else {
1777 assert(!(options & LYD_DUP_WITH_PARENTS));
1778 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001779 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001780 LY_CHECK_GOTO(rc, error);
1781 }
1782 } else {
1783 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001784 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1785 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001786 LY_CHECK_GOTO(rc, error);
1787 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001788 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001789 break;
1790 }
1791 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001792
Michal Vasko3a41dff2020-07-15 14:30:28 +02001793 if (dup) {
1794 *dup = first;
1795 }
1796 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001797
1798error:
1799 if (top) {
1800 lyd_free_tree(top);
1801 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001802 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001803 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001804 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001805}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001806
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001807LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001808lyd_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 +02001809{
Michal Vaskoddd76592022-01-17 13:34:48 +01001810 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1811 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
1812 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1813 return LY_EINVAL;
1814 }
1815
1816 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1817}
1818
1819LIBYANG_API_DEF LY_ERR
1820lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1821 uint32_t options, struct lyd_node **dup)
1822{
1823 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1824
1825 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001826}
1827
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001828LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001829lyd_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 +02001830{
Michal Vaskoddd76592022-01-17 13:34:48 +01001831 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1832 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
1833 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1834 return LY_EINVAL;
1835 }
1836
1837 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1838}
1839
1840LIBYANG_API_DEF LY_ERR
1841lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1842 uint32_t options, struct lyd_node **dup)
1843{
1844 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1845
1846 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001847}
1848
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001849LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001850lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001851{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001852 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001853 struct lyd_meta *mt, *last;
1854
Michal Vasko3a41dff2020-07-15 14:30:28 +02001855 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001856
1857 /* create a copy */
1858 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001859 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001860 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001861 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001862 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
1863 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001864
1865 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001866 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001867 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001868 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001869 last->next = mt;
1870 } else {
1871 node->meta = mt;
1872 }
1873
Radek Krejci011e4aa2020-09-04 15:22:31 +02001874finish:
1875 if (ret) {
1876 lyd_free_meta_single(mt);
1877 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001878 *dup = mt;
1879 }
1880 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001881}
1882
Michal Vasko4490d312020-06-16 13:08:55 +02001883/**
1884 * @brief Merge a source sibling into target siblings.
1885 *
1886 * @param[in,out] first_trg First target sibling, is updated if top-level.
1887 * @param[in] parent_trg Target parent.
1888 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001889 * @param[in] merge_cb Optional merge callback.
1890 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02001891 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001892 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02001893 * @return LY_ERR value.
1894 */
1895static LY_ERR
1896lyd_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 +02001897 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02001898{
Michal Vasko4490d312020-06-16 13:08:55 +02001899 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02001900 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01001901 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02001902 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02001903 struct lyd_dup_inst *child_dup_inst = NULL;
1904 LY_ERR ret;
1905 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02001906
1907 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02001908 if (!sibling_src->schema) {
1909 /* try to find the same opaque node */
1910 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
1911 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001912 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02001913 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02001914 } else {
1915 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02001916 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02001917 }
1918
Michal Vaskocd3f6172021-05-18 16:14:50 +02001919 if (match_trg) {
1920 /* update match as needed */
1921 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
1922 } else {
1923 /* first instance of this node */
1924 first_inst = 1;
1925 }
1926
1927 if (match_trg) {
1928 /* call callback */
1929 if (merge_cb) {
1930 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
1931 }
1932
Michal Vasko4490d312020-06-16 13:08:55 +02001933 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01001934 if (!match_trg->schema) {
1935 if (lyd_compare_single(sibling_src, match_trg, 0)) {
1936 /* update value */
1937 opaq_trg = (struct lyd_node_opaq *)match_trg;
1938 opaq_src = (struct lyd_node_opaq *)sibling_src;
1939
1940 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
1941 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
1942 opaq_trg->hints = opaq_src->hints;
1943
1944 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
1945 opaq_trg->format = opaq_src->format;
1946 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
1947 &opaq_trg->val_prefix_data);
1948 }
1949 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
1950 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001951 /* since they are different, they cannot both be default */
1952 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
1953
Michal Vasko3a41dff2020-07-15 14:30:28 +02001954 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
1955 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001956 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001957 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
1958 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02001959 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02001960
1961 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02001962 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02001963 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02001964 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02001965 /* update value */
1966 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02001967 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02001968
1969 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02001970 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02001971 }
1972
1973 /* check descendants, recursively */
1974 ret = LY_SUCCESS;
1975 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
1976 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
1977 &child_dup_inst);
1978 if (ret) {
1979 break;
Michal Vasko4490d312020-06-16 13:08:55 +02001980 }
1981 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02001982 lyd_dup_inst_free(child_dup_inst);
1983 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02001984 } else {
1985 /* node not found, merge it */
1986 if (options & LYD_MERGE_DESTRUCT) {
1987 dup_src = (struct lyd_node *)sibling_src;
1988 lyd_unlink_tree(dup_src);
1989 /* spend it */
1990 *sibling_src_p = NULL;
1991 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001992 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 +02001993 }
1994
Michal Vasko7e8315b2021-08-03 17:01:06 +02001995 if (!(options & LYD_MERGE_WITH_FLAGS)) {
1996 /* set LYD_NEW for all the new nodes, required for validation */
1997 LYD_TREE_DFS_BEGIN(dup_src, elem) {
1998 elem->flags |= LYD_NEW;
1999 LYD_TREE_DFS_END(dup_src, elem);
2000 }
Michal Vasko4490d312020-06-16 13:08:55 +02002001 }
2002
Michal Vaskocd3f6172021-05-18 16:14:50 +02002003 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002004 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002005
2006 if (first_inst) {
2007 /* remember not to find this instance next time */
2008 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2009 }
2010
2011 /* call callback, no source node */
2012 if (merge_cb) {
2013 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2014 }
Michal Vasko4490d312020-06-16 13:08:55 +02002015 }
2016
2017 return LY_SUCCESS;
2018}
2019
Michal Vasko3a41dff2020-07-15 14:30:28 +02002020static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002021lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2022 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002023{
2024 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002025 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002026 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002027 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002028
2029 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002030 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2031 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002032
2033 if (!source) {
2034 /* nothing to merge */
2035 return LY_SUCCESS;
2036 }
2037
Michal Vasko831422b2020-11-24 11:20:51 +01002038 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002039 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002040 return LY_EINVAL;
2041 }
2042
2043 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002044 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2045 /* skip data nodes from different modules */
2046 continue;
2047 }
2048
Radek Krejci857189e2020-09-01 13:26:36 +02002049 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002050 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2051 if (ret) {
2052 break;
2053 }
Michal Vasko4490d312020-06-16 13:08:55 +02002054 if (first && !sibling_src) {
2055 /* source was spent (unlinked), move to the next node */
2056 source = tmp;
2057 }
2058
Michal Vasko3a41dff2020-07-15 14:30:28 +02002059 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002060 break;
2061 }
2062 }
2063
2064 if (options & LYD_MERGE_DESTRUCT) {
2065 /* free any leftover source data that were not merged */
2066 lyd_free_siblings((struct lyd_node *)source);
2067 }
2068
Michal Vaskocd3f6172021-05-18 16:14:50 +02002069 lyd_dup_inst_free(dup_inst);
2070 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002071}
2072
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002073LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002074lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002075{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002076 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002077}
2078
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002079LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002080lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002081{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002082 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2083}
2084
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002085LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002086lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2087 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2088{
2089 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002090}
2091
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002092static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002093lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002094{
Michal Vasko14654712020-02-06 08:35:21 +01002095 /* ending \0 */
2096 ++reqlen;
2097
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002098 if (reqlen > *buflen) {
2099 if (is_static) {
2100 return LY_EINCOMPLETE;
2101 }
2102
2103 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2104 if (!*buffer) {
2105 return LY_EMEM;
2106 }
2107
2108 *buflen = reqlen;
2109 }
2110
2111 return LY_SUCCESS;
2112}
2113
Michal Vaskod59035b2020-07-08 12:00:06 +02002114LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002115lyd_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 +02002116{
2117 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002118 size_t len;
2119 const char *val;
2120 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002121
Michal Vasko824d0832021-11-04 15:36:51 +01002122 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002123 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002124 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002125 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002126
2127 quot = '\'';
2128 if (strchr(val, '\'')) {
2129 quot = '"';
2130 }
2131 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002132 }
2133
2134 return LY_SUCCESS;
2135}
2136
2137/**
2138 * @brief Append leaf-list value predicate to path.
2139 *
2140 * @param[in] node Node to print.
2141 * @param[in,out] buffer Buffer to print to.
2142 * @param[in,out] buflen Current buffer length.
2143 * @param[in,out] bufused Current number of characters used in @p buffer.
2144 * @param[in] is_static Whether buffer is static or can be reallocated.
2145 * @return LY_ERR
2146 */
2147static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002148lyd_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 +02002149{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002150 size_t len;
2151 const char *val;
2152 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002153
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002154 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002155 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002156 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002157
2158 quot = '\'';
2159 if (strchr(val, '\'')) {
2160 quot = '"';
2161 }
2162 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2163
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002164 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002165}
2166
2167/**
2168 * @brief Append node position (relative to its other instances) predicate to path.
2169 *
2170 * @param[in] node Node to print.
2171 * @param[in,out] buffer Buffer to print to.
2172 * @param[in,out] buflen Current buffer length.
2173 * @param[in,out] bufused Current number of characters used in @p buffer.
2174 * @param[in] is_static Whether buffer is static or can be reallocated.
2175 * @return LY_ERR
2176 */
2177static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002178lyd_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 +02002179{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002180 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002181 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002182 char *val = NULL;
2183 LY_ERR rc;
2184
Michal Vasko50cc0562021-05-18 16:15:43 +02002185 pos = lyd_list_pos(node);
2186 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002187 return LY_EMEM;
2188 }
2189
2190 len = 1 + strlen(val) + 1;
2191 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2192 if (rc != LY_SUCCESS) {
2193 goto cleanup;
2194 }
2195
2196 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2197
2198cleanup:
2199 free(val);
2200 return rc;
2201}
2202
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002203LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002204lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2205{
Radek Krejci857189e2020-09-01 13:26:36 +02002206 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002207 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002208 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002209 const struct lyd_node *iter, *parent;
2210 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002211 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002212
2213 LY_CHECK_ARG_RET(NULL, node, NULL);
2214 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002215 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002216 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002217 } else {
2218 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002219 }
2220
2221 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002222 case LYD_PATH_STD:
2223 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002224 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002225 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002226 ++depth;
2227 }
2228
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002229 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002230 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002231 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002232 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002233iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002234 /* get the module */
2235 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2236 parent = lyd_parent(iter);
2237 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2238 if (prev_mod == mod) {
2239 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002240 }
2241
2242 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002243 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2244 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002245 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2246 if (rc != LY_SUCCESS) {
2247 break;
2248 }
2249
2250 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02002251 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01002252 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002253
Michal Vasko790b2bc2020-08-03 13:35:06 +02002254 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002255 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002256 switch (iter->schema->nodetype) {
2257 case LYS_LIST:
2258 if (iter->schema->flags & LYS_KEYLESS) {
2259 /* print its position */
2260 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2261 } else {
2262 /* print all list keys in predicates */
2263 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2264 }
2265 break;
2266 case LYS_LEAFLIST:
2267 if (iter->schema->flags & LYS_CONFIG_W) {
2268 /* print leaf-list value */
2269 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2270 } else {
2271 /* print its position */
2272 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2273 }
2274 break;
2275 default:
2276 /* nothing to print more */
2277 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002278 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002279 }
2280 if (rc != LY_SUCCESS) {
2281 break;
2282 }
2283
Michal Vasko14654712020-02-06 08:35:21 +01002284 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002285 }
2286 break;
2287 }
2288
2289 return buffer;
2290}
Michal Vaskoe444f752020-02-10 12:20:06 +01002291
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002292LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002293lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2294{
2295 struct lyd_meta *ret = NULL;
2296 const struct ly_ctx *ctx;
2297 const char *prefix, *tmp;
2298 char *str;
2299 size_t pref_len, name_len;
2300
2301 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002302 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002303
2304 if (!first) {
2305 return NULL;
2306 }
2307
2308 ctx = first->annotation->module->ctx;
2309
2310 /* parse the name */
2311 tmp = name;
2312 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2313 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2314 return NULL;
2315 }
2316
2317 /* find the module */
2318 if (prefix) {
2319 str = strndup(prefix, pref_len);
2320 module = ly_ctx_get_module_latest(ctx, str);
2321 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002322 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 +02002323 }
2324
2325 /* find the metadata */
2326 LY_LIST_FOR(first, first) {
2327 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2328 ret = (struct lyd_meta *)first;
2329 break;
2330 }
2331 }
2332
2333 return ret;
2334}
2335
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002336LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002337lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2338{
Michal Vasko9beceb82022-04-05 12:14:15 +02002339 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002340 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002341 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002342
Michal Vaskof03ed032020-03-04 13:31:44 +01002343 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002344 if (!siblings) {
2345 /* no data */
2346 if (match) {
2347 *match = NULL;
2348 }
2349 return LY_ENOTFOUND;
2350 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002351
Michal Vasko9beceb82022-04-05 12:14:15 +02002352 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2353 /* create a duplicate in this context */
2354 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2355 target = dup;
2356 }
2357
2358 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2359 /* schema mismatch */
2360 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002361 if (match) {
2362 *match = NULL;
2363 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002364 return LY_ENOTFOUND;
2365 }
2366
Michal Vaskoe78faec2021-04-08 17:24:43 +02002367 /* get first sibling */
2368 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002369
Michal Vasko9e685082021-01-29 14:49:09 +01002370 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002371 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002372 assert(target->hash);
2373
Michal Vaskoe78faec2021-04-08 17:24:43 +02002374 if (lysc_is_dup_inst_list(target->schema)) {
2375 /* we must search the instances from beginning to find the first matching one */
2376 found = 0;
2377 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2378 if (!lyd_compare_single(target, iter, 0)) {
2379 found = 1;
2380 break;
2381 }
2382 }
2383 if (found) {
2384 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002385 } else {
2386 siblings = NULL;
2387 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002388 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002389 /* find by hash */
2390 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2391 siblings = *match_p;
2392 } else {
2393 /* not found */
2394 siblings = NULL;
2395 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002396 }
2397 } else {
2398 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002399 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002400 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002401 break;
2402 }
2403 }
2404 }
2405
Michal Vasko9beceb82022-04-05 12:14:15 +02002406 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002407 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002408 if (match) {
2409 *match = NULL;
2410 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002411 return LY_ENOTFOUND;
2412 }
2413
Michal Vasko9b368d32020-02-14 13:53:31 +01002414 if (match) {
2415 *match = (struct lyd_node *)siblings;
2416 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002417 return LY_SUCCESS;
2418}
2419
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002420LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002421lyd_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 +02002422 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002423{
2424 LY_ERR rc;
2425 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002426 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002427
Michal Vasko4c583e82020-07-17 12:16:14 +02002428 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002429 if (!siblings) {
2430 /* no data */
2431 if (match) {
2432 *match = NULL;
2433 }
2434 return LY_ENOTFOUND;
2435 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002436
Michal Vasko9beceb82022-04-05 12:14:15 +02002437 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2438 /* parent of ext nodes is useless */
2439 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
2440 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
2441 }
2442
2443 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2444 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002445 if (match) {
2446 *match = NULL;
2447 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002448 return LY_ENOTFOUND;
2449 }
2450
Michal Vaskof03ed032020-03-04 13:31:44 +01002451 if (key_or_value && !val_len) {
2452 val_len = strlen(key_or_value);
2453 }
2454
Michal Vaskob104f112020-07-17 09:54:54 +02002455 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2456 /* create a data node and find the instance */
2457 if (schema->nodetype == LYS_LEAFLIST) {
2458 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002459 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 +02002460 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002461 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002462 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002463 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002464 }
2465
2466 /* find it */
2467 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002468 } else {
2469 /* find the first schema node instance */
2470 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002471 }
2472
Michal Vaskoe444f752020-02-10 12:20:06 +01002473 lyd_free_tree(target);
2474 return rc;
2475}
Michal Vaskoccc02342020-05-21 10:09:21 +02002476
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002477LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002478lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2479{
2480 struct lyd_node **match_p, *first, *iter;
2481 struct lyd_node_inner *parent;
2482
Michal Vasko83ae7772022-06-08 10:01:55 +02002483 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002484 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002485
2486 LY_CHECK_RET(ly_set_new(set));
2487
2488 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2489 /* no data or schema mismatch */
2490 return LY_ENOTFOUND;
2491 }
2492
2493 /* get first sibling */
2494 siblings = lyd_first_sibling(siblings);
2495
2496 parent = siblings->parent;
2497 if (parent && parent->schema && parent->children_ht) {
2498 assert(target->hash);
2499
2500 /* find the first instance */
2501 lyd_find_sibling_first(siblings, target, &first);
2502 if (first) {
2503 /* add it so that it is the first in the set */
2504 if (ly_set_add(*set, first, 1, NULL)) {
2505 goto error;
2506 }
2507
2508 /* find by hash */
2509 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2510 iter = *match_p;
2511 } else {
2512 /* not found */
2513 iter = NULL;
2514 }
2515 while (iter) {
2516 /* add all found nodes into the set */
2517 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2518 goto error;
2519 }
2520
2521 /* find next instance */
2522 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2523 iter = NULL;
2524 } else {
2525 iter = *match_p;
2526 }
2527 }
2528 }
2529 } else {
2530 /* no children hash table */
2531 LY_LIST_FOR(siblings, siblings) {
2532 if (!lyd_compare_single(target, siblings, 0)) {
2533 ly_set_add(*set, (void *)siblings, 1, NULL);
2534 }
2535 }
2536 }
2537
2538 if (!(*set)->count) {
2539 return LY_ENOTFOUND;
2540 }
2541 return LY_SUCCESS;
2542
2543error:
2544 ly_set_free(*set, NULL);
2545 *set = NULL;
2546 return LY_EMEM;
2547}
2548
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002549LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002550lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2551{
2552 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2553
2554 for ( ; first; first = first->next) {
2555 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2556 break;
2557 }
2558 }
2559
2560 if (match) {
2561 *match = (struct lyd_node *)first;
2562 }
2563 return first ? LY_SUCCESS : LY_ENOTFOUND;
2564}
2565
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002566LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002567lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2568 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002569{
2570 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002571 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02002572 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002573 uint32_t i;
2574
Michal Vaskoddd76592022-01-17 13:34:48 +01002575 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002576
Michal Vasko37c69432021-04-12 14:48:24 +02002577 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002578
Michal Vaskoddd76592022-01-17 13:34:48 +01002579 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01002580 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002581 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002582
2583 /* evaluate expression */
Michal Vaskoddd76592022-01-17 13:34:48 +01002584 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02002585 LY_CHECK_GOTO(ret, cleanup);
2586
2587 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002588 ret = ly_set_new(set);
2589 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002590
2591 /* transform into ly_set */
2592 if (xp_set.type == LYXP_SET_NODE_SET) {
2593 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
2594 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002595 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002596 (*set)->size = xp_set.used;
2597
2598 for (i = 0; i < xp_set.used; ++i) {
2599 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02002600 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002601 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002602 }
2603 }
2604 }
2605
2606cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002607 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002608 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01002609 if (ret) {
2610 ly_set_free(*set, NULL);
2611 *set = NULL;
2612 }
Michal Vaskoccc02342020-05-21 10:09:21 +02002613 return ret;
2614}
Radek Krejcica989142020-11-05 11:32:22 +01002615
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002616LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002617lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2618 const struct lyxp_var *vars, struct ly_set **set)
2619{
2620 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2621
2622 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2623}
2624
2625LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01002626lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2627{
2628 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2629
Michal Vaskoddd76592022-01-17 13:34:48 +01002630 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002631}
2632
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002633LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002634lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2635{
Michal Vaskoe3716b32021-12-13 16:58:25 +01002636 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2637
Michal Vaskoddd76592022-01-17 13:34:48 +01002638 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02002639}
2640
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002641LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002642lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002643{
2644 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002645 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002646 struct lyxp_expr *exp = NULL;
2647
2648 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
2649
2650 /* compile expression */
2651 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
2652 LY_CHECK_GOTO(ret, cleanup);
2653
2654 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02002655 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002656 LY_CHECK_GOTO(ret, cleanup);
2657
2658 /* transform into boolean */
2659 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
2660 LY_CHECK_GOTO(ret, cleanup);
2661
2662 /* set result */
2663 *result = xp_set.val.bln;
2664
2665cleanup:
2666 lyxp_set_free_content(&xp_set);
2667 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
2668 return ret;
2669}
2670
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002671LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002672lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
2673{
2674 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
2675}
2676
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002677LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002678lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2679{
2680 LY_ERR ret = LY_SUCCESS;
2681 struct lyxp_expr *expr = NULL;
2682 struct ly_path *lypath = NULL;
2683
2684 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2685
2686 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002687 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 +01002688 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
2689 LY_CHECK_GOTO(ret, cleanup);
2690
2691 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002692 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002693 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 +01002694 LY_CHECK_GOTO(ret, cleanup);
2695
2696 /* evaluate the path */
2697 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
2698
2699cleanup:
2700 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2701 ly_path_free(LYD_CTX(ctx_node), lypath);
2702 return ret;
2703}
2704
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002705LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002706lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2707{
2708 LY_ERR ret;
2709 struct lyd_node *m;
2710
2711 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2712
2713 ret = ly_path_eval(path, tree, &m);
2714 if (ret) {
2715 if (match) {
2716 *match = NULL;
2717 }
2718 return LY_ENOTFOUND;
2719 }
2720
2721 if (match) {
2722 *match = m;
2723 }
2724 return LY_SUCCESS;
2725}