blob: ea3ad9c72c430f5ed785ee0c2313c3eabb596101 [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
486/**
487 * @brief Insert node after a sibling.
488 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100489 * Handles inserting into NP containers and key-less lists.
490 *
Michal Vasko90932a92020-02-12 14:33:03 +0100491 * @param[in] sibling Sibling to insert after.
492 * @param[in] node Node to insert.
493 */
494static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100495lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100496{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100497 struct lyd_node_inner *par;
498
Michal Vasko90932a92020-02-12 14:33:03 +0100499 assert(!node->next && (node->prev == node));
500
501 node->next = sibling->next;
502 node->prev = sibling;
503 sibling->next = node;
504 if (node->next) {
505 /* sibling had a succeeding node */
506 node->next->prev = node;
507 } else {
508 /* sibling was last, find first sibling and change its prev */
509 if (sibling->parent) {
510 sibling = sibling->parent->child;
511 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200512 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100513 }
514 sibling->prev = node;
515 }
516 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100517
Michal Vasko9f96a052020-03-10 09:41:45 +0100518 for (par = node->parent; par; par = par->parent) {
519 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
520 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100521 par->flags &= ~LYD_DEFAULT;
522 }
523 }
Michal Vasko90932a92020-02-12 14:33:03 +0100524}
525
526/**
527 * @brief Insert node before a sibling.
528 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100529 * Handles inserting into NP containers and key-less lists.
530 *
Michal Vasko90932a92020-02-12 14:33:03 +0100531 * @param[in] sibling Sibling to insert before.
532 * @param[in] node Node to insert.
533 */
534static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100535lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100536{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100537 struct lyd_node_inner *par;
538
Michal Vasko90932a92020-02-12 14:33:03 +0100539 assert(!node->next && (node->prev == node));
540
541 node->next = sibling;
542 /* covers situation of sibling being first */
543 node->prev = sibling->prev;
544 sibling->prev = node;
545 if (node->prev->next) {
546 /* sibling had a preceding node */
547 node->prev->next = node;
548 } else if (sibling->parent) {
549 /* sibling was first and we must also change parent child pointer */
550 sibling->parent->child = node;
551 }
552 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100553
Michal Vasko9f96a052020-03-10 09:41:45 +0100554 for (par = node->parent; par; par = par->parent) {
555 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
556 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100557 par->flags &= ~LYD_DEFAULT;
558 }
559 }
Michal Vasko90932a92020-02-12 14:33:03 +0100560}
561
562/**
Michal Vaskob104f112020-07-17 09:54:54 +0200563 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100564 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100565 * Handles inserting into NP containers and key-less lists.
566 *
Michal Vasko90932a92020-02-12 14:33:03 +0100567 * @param[in] parent Parent to insert into.
568 * @param[in] node Node to insert.
569 */
570static void
Michal Vaskob104f112020-07-17 09:54:54 +0200571lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100572{
573 struct lyd_node_inner *par;
574
Radek Krejcia1c1e542020-09-29 16:06:52 +0200575 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100576 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100577
578 par = (struct lyd_node_inner *)parent;
579
Michal Vaskob104f112020-07-17 09:54:54 +0200580 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100581 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100582
Michal Vaskod989ba02020-08-24 10:59:24 +0200583 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100584 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
585 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100586 par->flags &= ~LYD_DEFAULT;
587 }
588 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200589}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100590
Michal Vasko751cb4d2020-07-14 12:25:28 +0200591/**
592 * @brief Learn whether a list instance has all the keys.
593 *
594 * @param[in] list List instance to check.
595 * @return non-zero if all the keys were found,
596 * @return 0 otherwise.
597 */
598static int
599lyd_insert_has_keys(const struct lyd_node *list)
600{
601 const struct lyd_node *key;
602 const struct lysc_node *skey = NULL;
603
604 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200605 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200606 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
607 if (!key || (key->schema != skey)) {
608 /* key missing */
609 return 0;
610 }
611
612 key = key->next;
613 }
614
615 /* all keys found */
616 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100617}
618
619void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200620lyd_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 +0100621{
Michal Vaskob104f112020-07-17 09:54:54 +0200622 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100623
Michal Vaskob104f112020-07-17 09:54:54 +0200624 /* inserting list without its keys is not supported */
625 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100626 assert(!parent || !parent->schema ||
627 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100628
Michal Vaskob104f112020-07-17 09:54:54 +0200629 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100630 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100631 }
Michal Vasko90932a92020-02-12 14:33:03 +0100632
Michal Vaskob104f112020-07-17 09:54:54 +0200633 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100634 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100635
Michal Vasko9beceb82022-04-05 12:14:15 +0200636 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200637 /* no next anchor */
638 anchor = NULL;
639 } else {
640 /* find the anchor, our next node, so we can insert before it */
641 anchor = lyd_insert_get_next_anchor(first_sibling, node);
642 }
643
Michal Vaskob104f112020-07-17 09:54:54 +0200644 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200645 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200646 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100647 if (!parent && (*first_sibling_p == anchor)) {
648 /* move first sibling */
649 *first_sibling_p = node;
650 }
Michal Vaskob104f112020-07-17 09:54:54 +0200651 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200652 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200653 lyd_insert_after_node(first_sibling->prev, node);
654 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200655 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200656 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100657 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200658 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200659 *first_sibling_p = node;
660 }
661
662 /* insert into parent HT */
663 lyd_insert_hash(node);
664
665 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +0200666 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200667 lyd_hash(parent);
668
669 /* now we can insert even the list into its parent HT */
670 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100671 }
Michal Vasko90932a92020-02-12 14:33:03 +0100672}
673
Michal Vasko717a4c32020-12-07 09:40:10 +0100674/**
675 * @brief Check schema place of a node to be inserted.
676 *
677 * @param[in] parent Schema node of the parent data node.
678 * @param[in] sibling Schema node of a sibling data node.
679 * @param[in] schema Schema node if the data node to be inserted.
680 * @return LY_SUCCESS on success.
681 * @return LY_EINVAL if the place is invalid.
682 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100683static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100684lyd_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 +0100685{
686 const struct lysc_node *par2;
687
Michal Vasko62ed12d2020-05-21 10:08:25 +0200688 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100689 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
690 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100691
Michal Vasko717a4c32020-12-07 09:40:10 +0100692 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100693 /* opaque nodes can be inserted wherever */
694 return LY_SUCCESS;
695 }
696
Michal Vasko717a4c32020-12-07 09:40:10 +0100697 if (!parent) {
698 parent = lysc_data_parent(sibling);
699 }
700
Michal Vaskof03ed032020-03-04 13:31:44 +0100701 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200702 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100703
704 if (parent) {
705 /* inner node */
706 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200707 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200708 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100709 return LY_EINVAL;
710 }
711 } else {
712 /* top-level node */
713 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200714 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100715 return LY_EINVAL;
716 }
717 }
718
719 return LY_SUCCESS;
720}
721
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100722LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200723lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100724{
725 struct lyd_node *iter;
726
Michal Vasko0ab974d2021-02-24 13:18:26 +0100727 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100728 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100729
Michal Vasko717a4c32020-12-07 09:40:10 +0100730 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100731
Michal Vasko0ab974d2021-02-24 13:18:26 +0100732 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100733 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100734 return LY_EINVAL;
735 }
736
737 if (node->parent || node->prev->next) {
738 lyd_unlink_tree(node);
739 }
740
741 while (node) {
742 iter = node->next;
743 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200744 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100745 node = iter;
746 }
747 return LY_SUCCESS;
748}
749
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100750LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100751lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
752{
753 struct lyd_node *iter;
754
755 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
756 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
757
758 if (first->schema && (first->schema->flags & LYS_KEY)) {
759 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
760 return LY_EINVAL;
761 }
762
763 while (first) {
764 iter = first->next;
765 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100766 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100767 first = iter;
768 }
769 return LY_SUCCESS;
770}
771
772LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200773lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100774{
775 struct lyd_node *iter;
776
Michal Vaskob104f112020-07-17 09:54:54 +0200777 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100778 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100779
Michal Vaskob104f112020-07-17 09:54:54 +0200780 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100781 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100782 }
783
Michal Vasko553d0b52020-12-04 16:27:52 +0100784 if (sibling == node) {
785 /* we need to keep the connection to siblings so we can insert into them */
786 sibling = sibling->prev;
787 }
788
Michal Vaskob1b5c262020-03-05 14:29:47 +0100789 if (node->parent || node->prev->next) {
790 lyd_unlink_tree(node);
791 }
792
793 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100794 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200795 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200796 return LY_EINVAL;
797 }
798
Michal Vaskob1b5c262020-03-05 14:29:47 +0100799 iter = node->next;
800 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200801 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100802 node = iter;
803 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100804
Michal Vaskob104f112020-07-17 09:54:54 +0200805 if (first) {
806 /* find the first sibling */
807 *first = sibling;
808 while ((*first)->prev->next) {
809 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100810 }
811 }
812
813 return LY_SUCCESS;
814}
815
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100816LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100817lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
818{
Michal Vaskobce230b2022-04-19 09:55:31 +0200819 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100820 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100821
Michal Vasko717a4c32020-12-07 09:40:10 +0100822 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100823
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200824 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200825 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100826 return LY_EINVAL;
827 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200828 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
829 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100830 return LY_EINVAL;
831 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100832
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100833 lyd_unlink_tree(node);
834 lyd_insert_before_node(sibling, node);
835 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100836
Michal Vaskof03ed032020-03-04 13:31:44 +0100837 return LY_SUCCESS;
838}
839
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100840LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100841lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
842{
Michal Vaskobce230b2022-04-19 09:55:31 +0200843 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100844 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100845
Michal Vasko717a4c32020-12-07 09:40:10 +0100846 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100847
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200848 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200849 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100850 return LY_EINVAL;
851 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200852 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
853 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100854 return LY_EINVAL;
855 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100856
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100857 lyd_unlink_tree(node);
858 lyd_insert_after_node(sibling, node);
859 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100860
Michal Vaskof03ed032020-03-04 13:31:44 +0100861 return LY_SUCCESS;
862}
863
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100864LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200865lyd_unlink_siblings(struct lyd_node *node)
866{
867 struct lyd_node *next, *elem, *first = NULL;
868
869 LY_LIST_FOR_SAFE(node, next, elem) {
870 lyd_unlink_tree(elem);
871 lyd_insert_node(NULL, &first, elem, 1);
872 }
873}
874
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100875LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100876lyd_unlink_tree(struct lyd_node *node)
877{
878 struct lyd_node *iter;
879
880 if (!node) {
881 return;
882 }
883
Michal Vaskob104f112020-07-17 09:54:54 +0200884 /* update hashes while still linked into the tree */
885 lyd_unlink_hash(node);
886
Michal Vaskof03ed032020-03-04 13:31:44 +0100887 /* unlink from siblings */
888 if (node->prev->next) {
889 node->prev->next = node->next;
890 }
891 if (node->next) {
892 node->next->prev = node->prev;
893 } else {
894 /* unlinking the last node */
895 if (node->parent) {
896 iter = node->parent->child;
897 } else {
898 iter = node->prev;
899 while (iter->prev != node) {
900 iter = iter->prev;
901 }
902 }
903 /* update the "last" pointer from the first node */
904 iter->prev = node->prev;
905 }
906
907 /* unlink from parent */
908 if (node->parent) {
909 if (node->parent->child == node) {
910 /* the node is the first child */
911 node->parent->child = node->next;
912 }
913
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200914 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +0200915 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
916 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200917 LY_LIST_FOR(node->parent->child, iter) {
918 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
919 break;
920 }
921 }
922 if (!iter) {
923 node->parent->flags |= LYD_DEFAULT;
924 }
925 }
926
Michal Vaskof03ed032020-03-04 13:31:44 +0100927 node->parent = NULL;
928 }
929
930 node->next = NULL;
931 node->prev = node;
932}
933
Michal Vaskoa5da3292020-08-12 13:10:50 +0200934void
Michal Vasko871a0252020-11-11 18:35:24 +0100935lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200936{
937 struct lyd_meta *last, *iter;
938
939 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200940
941 if (!meta) {
942 return;
943 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200944
945 for (iter = meta; iter; iter = iter->next) {
946 iter->parent = parent;
947 }
948
949 /* insert as the last attribute */
950 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200951 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200952 last->next = meta;
953 } else {
954 parent->meta = meta;
955 }
956
957 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100958 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200959 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100960 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200962}
963
964LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100965lyd_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 +0200966 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 +0100967 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 +0100968{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100969 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100970 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +0200971 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +0100972 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200973 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100974
Michal Vasko9f96a052020-03-10 09:41:45 +0100975 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100976
Michal Vaskoddd76592022-01-17 13:34:48 +0100977 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100978
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200979 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +0100980 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200981 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +0100982 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200983 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +0100984 break;
985 }
986 }
987 if (!ant) {
988 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100989 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100990 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100991 ret = LY_EINVAL;
992 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +0100993 }
994
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100996 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +0100997 mt->parent = parent;
998 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +0200999 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
1000 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 +01001001 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1003 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1004 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001005
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001006 /* insert as the last attribute */
1007 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001008 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001009 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001010 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001011 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001012 }
1013
Michal Vasko9f96a052020-03-10 09:41:45 +01001014 if (meta) {
1015 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001016 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001017
1018cleanup:
Michal Vaskoddd76592022-01-17 13:34:48 +01001019 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001020 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001021}
1022
Michal Vaskoa5da3292020-08-12 13:10:50 +02001023void
1024lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1025{
1026 struct lyd_attr *last, *iter;
1027 struct lyd_node_opaq *opaq;
1028
1029 assert(parent && !parent->schema);
1030
1031 if (!attr) {
1032 return;
1033 }
1034
1035 opaq = (struct lyd_node_opaq *)parent;
1036 for (iter = attr; iter; iter = iter->next) {
1037 iter->parent = opaq;
1038 }
1039
1040 /* insert as the last attribute */
1041 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001042 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001043 last->next = attr;
1044 } else {
1045 opaq->attr = attr;
1046 }
1047}
1048
Michal Vasko52927e22020-03-16 17:26:14 +01001049LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001050lyd_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 +01001051 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 +02001052 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 +01001053{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001054 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001055 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001056
1057 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001058 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001059
Michal Vasko2a3722d2021-06-16 11:52:39 +02001060 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001061 value = "";
1062 }
1063
1064 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001065 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001066
Michal Vaskoad92b672020-11-12 13:11:31 +01001067 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001068 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001069 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001070 }
1071 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001072 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001073 }
1074
Michal Vasko52927e22020-03-16 17:26:14 +01001075 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001076 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1077 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001078 *dynamic = 0;
1079 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001080 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001081 }
Michal Vasko501af032020-11-11 20:27:44 +01001082 at->format = format;
1083 at->val_prefix_data = val_prefix_data;
1084 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001085
1086 /* insert as the last attribute */
1087 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001088 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001089 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001090 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001091 last->next = at;
1092 }
1093
Radek Krejci011e4aa2020-09-04 15:22:31 +02001094finish:
1095 if (ret) {
1096 lyd_free_attr_single(ctx, at);
1097 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001098 *attr = at;
1099 }
1100 return LY_SUCCESS;
1101}
1102
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001103LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001104lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001105{
Michal Vasko004d3152020-06-11 19:59:22 +02001106 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001107
Michal Vasko004d3152020-06-11 19:59:22 +02001108 if (ly_path_eval(path, tree, &target)) {
1109 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001110 }
1111
Michal Vasko004d3152020-06-11 19:59:22 +02001112 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001113}
1114
aPiecek2f63f952021-03-30 12:22:18 +02001115/**
1116 * @brief Check the equality of the two schemas from different contexts.
1117 *
1118 * @param schema1 of first node.
1119 * @param schema2 of second node.
1120 * @return 1 if the schemas are equal otherwise 0.
1121 */
1122static ly_bool
1123lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1124{
1125 if (!schema1 && !schema2) {
1126 return 1;
1127 } else if (!schema1 || !schema2) {
1128 return 0;
1129 }
1130
1131 assert(schema1->module->ctx != schema2->module->ctx);
1132
1133 if (schema1->nodetype != schema2->nodetype) {
1134 return 0;
1135 }
1136
1137 if (strcmp(schema1->name, schema2->name)) {
1138 return 0;
1139 }
1140
1141 if (strcmp(schema1->module->name, schema2->module->name)) {
1142 return 0;
1143 }
1144
1145 if (schema1->module->revision || schema2->module->revision) {
1146 if (!schema1->module->revision || !schema2->module->revision) {
1147 return 0;
1148 }
1149 if (strcmp(schema1->module->revision, schema2->module->revision)) {
1150 return 0;
1151 }
1152 }
1153
1154 return 1;
1155}
1156
1157/**
1158 * @brief Check the equality of the schemas for all parent nodes.
1159 *
1160 * Both nodes must be from different contexts.
1161 *
1162 * @param node1 Data of first node.
1163 * @param node2 Data of second node.
1164 * @return 1 if the all related parental schemas are equal otherwise 0.
1165 */
1166static ly_bool
1167lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1168{
1169 const struct lysc_node *parent1, *parent2;
1170
1171 assert(node1 && node2);
1172
1173 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1174 parent1 && parent2;
1175 parent1 = parent1->parent, parent2 = parent2->parent) {
1176 if (!lyd_compare_schema_equal(parent1, parent2)) {
1177 return 0;
1178 }
1179 }
1180
1181 if (parent1 || parent2) {
1182 return 0;
1183 }
1184
1185 return 1;
1186}
1187
1188/**
1189 * @brief Internal implementation of @ref lyd_compare_single.
1190 * @copydoc lyd_compare_single
1191 * @param[in] parental_schemas_checked Flag used for optimization.
1192 * When this function is called for the first time, the flag must be set to 0.
1193 * The @ref lyd_compare_schema_parents_equal should be called only once during
1194 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
1195 */
1196static LY_ERR
1197lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
1198 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001199{
1200 const struct lyd_node *iter1, *iter2;
1201 struct lyd_node_term *term1, *term2;
1202 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001203 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001204 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001205
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001206 if (!node1 || !node2) {
1207 if (node1 == node2) {
1208 return LY_SUCCESS;
1209 } else {
1210 return LY_ENOT;
1211 }
1212 }
1213
aPiecek2f63f952021-03-30 12:22:18 +02001214 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1215 /* same contexts */
1216 if (node1->schema != node2->schema) {
1217 return LY_ENOT;
1218 }
1219 } else {
1220 /* different contexts */
1221 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1222 return LY_ENOT;
1223 }
1224 if (!parental_schemas_checked) {
1225 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1226 return LY_ENOT;
1227 }
1228 parental_schemas_checked = 1;
1229 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001230 }
1231
1232 if (node1->hash != node2->hash) {
1233 return LY_ENOT;
1234 }
aPiecek2f63f952021-03-30 12:22:18 +02001235 /* 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 +02001236
Michal Vasko52927e22020-03-16 17:26:14 +01001237 if (!node1->schema) {
1238 opaq1 = (struct lyd_node_opaq *)node1;
1239 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02001240 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
1241 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001242 return LY_ENOT;
1243 }
Michal Vasko52927e22020-03-16 17:26:14 +01001244 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001245 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02001246 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1247 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001248 return LY_ENOT;
1249 }
1250 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001251 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02001252 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
1253 if (strcmp(opaq1->value, opaq2->value)) {
1254 return LY_ENOT;
1255 }
1256 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001257 default:
Michal Vasko52927e22020-03-16 17:26:14 +01001258 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001259 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01001260 return LY_EINT;
1261 }
1262 if (options & LYD_COMPARE_FULL_RECURSION) {
1263 iter1 = opaq1->child;
1264 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001265 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001266 }
1267 return LY_SUCCESS;
1268 } else {
1269 switch (node1->schema->nodetype) {
1270 case LYS_LEAF:
1271 case LYS_LEAFLIST:
1272 if (options & LYD_COMPARE_DEFAULTS) {
1273 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1274 return LY_ENOT;
1275 }
1276 }
1277
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001278 term1 = (struct lyd_node_term *)node1;
1279 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02001280
1281 /* same contexts */
1282 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1283 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
1284 }
1285
1286 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001287 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02001288 return LY_ENOT;
1289 }
1290 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001291 case LYS_CONTAINER:
1292 if (options & LYD_COMPARE_DEFAULTS) {
1293 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1294 return LY_ENOT;
1295 }
1296 }
1297 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01001298 iter1 = lyd_child(node1);
1299 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001300 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001301 }
1302 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001303 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001304 case LYS_ACTION:
1305 if (options & LYD_COMPARE_FULL_RECURSION) {
1306 /* TODO action/RPC
1307 goto all_children_compare;
1308 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001309 }
1310 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001311 case LYS_NOTIF:
1312 if (options & LYD_COMPARE_FULL_RECURSION) {
1313 /* TODO Notification
1314 goto all_children_compare;
1315 */
1316 }
1317 return LY_SUCCESS;
1318 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001319 iter1 = lyd_child(node1);
1320 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001321
1322 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1323 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01001324 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001325 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001326 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001327 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001328 return LY_ENOT;
1329 }
1330 iter1 = iter1->next;
1331 iter2 = iter2->next;
1332 }
1333 } else {
1334 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1335
Radek Krejci0f969882020-08-21 16:56:47 +02001336all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01001337 if (!iter1 && !iter2) {
1338 /* no children, nothing to compare */
1339 return LY_SUCCESS;
1340 }
1341
Michal Vaskod989ba02020-08-24 10:59:24 +02001342 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02001343 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001344 return LY_ENOT;
1345 }
1346 }
1347 if (iter1 || iter2) {
1348 return LY_ENOT;
1349 }
1350 }
1351 return LY_SUCCESS;
1352 case LYS_ANYXML:
1353 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001354 any1 = (struct lyd_node_any *)node1;
1355 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001356
1357 if (any1->value_type != any2->value_type) {
1358 return LY_ENOT;
1359 }
1360 switch (any1->value_type) {
1361 case LYD_ANYDATA_DATATREE:
1362 iter1 = any1->value.tree;
1363 iter2 = any2->value.tree;
1364 goto all_children_compare;
1365 case LYD_ANYDATA_STRING:
1366 case LYD_ANYDATA_XML:
1367 case LYD_ANYDATA_JSON:
1368 len1 = strlen(any1->value.str);
1369 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001370 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001371 return LY_ENOT;
1372 }
1373 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001374 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001375 len1 = lyd_lyb_data_length(any1->value.mem);
1376 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02001377 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001378 return LY_ENOT;
1379 }
1380 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001381 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001382 }
1383 }
1384
Michal Vaskob7be7a82020-08-20 09:09:04 +02001385 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001386 return LY_EINT;
1387}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001388
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001389LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001390lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1391{
1392 return lyd_compare_single_(node1, node2, options, 0);
1393}
1394
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001395LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001396lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001397{
Michal Vaskod989ba02020-08-24 10:59:24 +02001398 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02001399 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
1400 }
1401
Michal Vasko11a81432020-07-28 16:31:29 +02001402 if (node1 == node2) {
1403 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001404 }
Michal Vasko11a81432020-07-28 16:31:29 +02001405 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02001406}
1407
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001408LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001409lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1410{
1411 if (!meta1 || !meta2) {
1412 if (meta1 == meta2) {
1413 return LY_SUCCESS;
1414 } else {
1415 return LY_ENOT;
1416 }
1417 }
1418
Michal Vaskoa8083062020-11-06 17:22:10 +01001419 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001420 return LY_ENOT;
1421 }
1422
Michal Vasko21725742020-06-29 11:49:25 +02001423 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1424}
1425
Radek Krejci22ebdba2019-07-25 13:59:43 +02001426/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001427 * @brief Create a copy of the attribute.
1428 *
1429 * @param[in] attr Attribute to copy.
1430 * @param[in] node Opaque where to append the new attribute.
1431 * @param[out] dup Optional created attribute copy.
1432 * @return LY_ERR value.
1433 */
1434static LY_ERR
1435lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1436{
1437 LY_ERR ret = LY_SUCCESS;
1438 struct lyd_attr *a, *last;
1439 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1440
1441 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1442
1443 /* create a copy */
1444 a = calloc(1, sizeof *attr);
1445 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1446
1447 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1448 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1449 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1450 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1451 a->hints = attr->hints;
1452 a->format = attr->format;
1453 if (attr->val_prefix_data) {
1454 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1455 LY_CHECK_GOTO(ret, finish);
1456 }
1457
1458 /* insert as the last attribute */
1459 a->parent = opaq;
1460 if (opaq->attr) {
1461 for (last = opaq->attr; last->next; last = last->next) {}
1462 last->next = a;
1463 } else {
1464 opaq->attr = a;
1465 }
1466
1467finish:
1468 if (ret) {
1469 lyd_free_attr_single(LYD_CTX(node), a);
1470 } else if (dup) {
1471 *dup = a;
1472 }
1473 return LY_SUCCESS;
1474}
1475
1476/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001477 * @brief Find @p schema equivalent in @p trg_ctx.
1478 *
1479 * @param[in] schema Schema node to find.
1480 * @param[in] trg_ctx Target context to search in.
1481 * @param[in] parent Data parent of @p schema, if any.
1482 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1483 * @return LY_RRR value.
1484 */
1485static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02001486lyd_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 +01001487 const struct lysc_node **trg_schema)
1488{
Michal Vasko9beceb82022-04-05 12:14:15 +02001489 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1490 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001491 char *path;
1492
1493 if (!schema) {
1494 /* opaque node */
1495 *trg_schema = NULL;
1496 return LY_SUCCESS;
1497 }
1498
Michal Vasko9beceb82022-04-05 12:14:15 +02001499 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001500 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001501 trg_parent = parent->schema;
1502 src_parent = lysc_data_parent(schema);
1503 }
1504
1505 do {
1506 /* find the next parent */
1507 sp = schema;
1508 while (sp->parent != src_parent) {
1509 sp = sp->parent;
1510 }
1511 src_parent = sp;
1512
1513 if (!src_parent->parent) {
1514 /* find the module first */
1515 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1516 if (!trg_mod) {
1517 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1518 src_parent->module->name);
1519 return LY_ENOTFOUND;
1520 }
1521 }
1522
1523 /* find the next parent */
1524 assert(trg_parent || trg_mod);
1525 tp = NULL;
1526 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1527 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1528 break;
1529 }
1530 }
1531 if (!tp) {
1532 /* schema node not found */
1533 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1534 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1535 free(path);
Michal Vaskoddd76592022-01-17 13:34:48 +01001536 return LY_ENOTFOUND;
1537 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001538
Michal Vasko9beceb82022-04-05 12:14:15 +02001539 trg_parent = tp;
1540 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001541
Michal Vasko9beceb82022-04-05 12:14:15 +02001542 /* success */
1543 *trg_schema = trg_parent;
1544 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001545}
1546
1547/**
Michal Vasko52927e22020-03-16 17:26:14 +01001548 * @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 +02001549 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001550 * 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 +02001551 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001552 * @param[in] node Node to duplicate.
1553 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001554 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001555 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1556 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001557 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1558 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001559 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1560 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001561 */
Michal Vasko52927e22020-03-16 17:26:14 +01001562static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001563lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1564 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001565{
Michal Vasko52927e22020-03-16 17:26:14 +01001566 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001567 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001568 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001569 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001570 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001571 const struct lysc_type *type;
1572 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001573
Michal Vasko52927e22020-03-16 17:26:14 +01001574 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001575
Michal Vasko19175b62022-04-01 09:17:07 +02001576 if (node->flags & LYD_EXT) {
1577 /* we need to use the same context */
1578 trg_ctx = LYD_CTX(node);
1579 }
1580
Michal Vasko52927e22020-03-16 17:26:14 +01001581 if (!node->schema) {
1582 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001583 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001584 } else {
1585 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001586 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001587 case LYS_ACTION:
1588 case LYS_NOTIF:
1589 case LYS_CONTAINER:
1590 case LYS_LIST:
1591 dup = calloc(1, sizeof(struct lyd_node_inner));
1592 break;
1593 case LYS_LEAF:
1594 case LYS_LEAFLIST:
1595 dup = calloc(1, sizeof(struct lyd_node_term));
1596 break;
1597 case LYS_ANYDATA:
1598 case LYS_ANYXML:
1599 dup = calloc(1, sizeof(struct lyd_node_any));
1600 break;
1601 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001602 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001603 ret = LY_EINT;
1604 goto error;
1605 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001606 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001607 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001608
Michal Vaskof6df0a02020-06-16 13:08:34 +02001609 if (options & LYD_DUP_WITH_FLAGS) {
1610 dup->flags = node->flags;
1611 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001612 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001613 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001614 if (trg_ctx == LYD_CTX(node)) {
1615 dup->schema = node->schema;
1616 } else {
Michal Vasko27f536f2022-04-01 09:17:30 +02001617 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
1618 if (ret) {
1619 /* has no schema but is not an opaque node */
1620 free(dup);
1621 dup = NULL;
1622 goto error;
1623 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001624 }
Michal Vasko52927e22020-03-16 17:26:14 +01001625 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001626
Michal Vasko9cf62422021-07-01 13:29:32 +02001627 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001628 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001629 if (!node->schema) {
1630 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1631 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1632 }
1633 } else {
1634 LY_LIST_FOR(node->meta, meta) {
1635 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1636 }
Michal Vasko25a32822020-07-09 15:48:22 +02001637 }
1638 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001639
1640 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001641 if (!dup->schema) {
1642 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1643 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1644 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001645
1646 if (options & LYD_DUP_RECURSIVE) {
1647 /* duplicate all the children */
1648 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001649 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001650 }
1651 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001652 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1653 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1654 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1655 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001656 opaq->hints = orig->hints;
1657 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001658 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001659 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001660 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001661 }
Michal Vasko52927e22020-03-16 17:26:14 +01001662 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1663 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1664 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1665
1666 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001667 if (trg_ctx == LYD_CTX(node)) {
1668 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1669 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1670 } else {
1671 /* store canonical value in the target context */
1672 val_can = lyd_get_value(node);
1673 type = ((struct lysc_node_leaf *)term->schema)->type;
1674 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
1675 LYD_HINT_DATA, term->schema, NULL);
1676 LY_CHECK_GOTO(ret, error);
1677 }
Michal Vasko52927e22020-03-16 17:26:14 +01001678 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1679 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1680 struct lyd_node *child;
1681
1682 if (options & LYD_DUP_RECURSIVE) {
1683 /* duplicate all the children */
1684 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001685 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001686 }
Michal Vasko69730152020-10-09 16:30:07 +02001687 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001688 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001689 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001690 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001691 }
1692 }
1693 lyd_hash(dup);
1694 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001695 dup->hash = node->hash;
1696 any = (struct lyd_node_any *)node;
1697 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001698 }
1699
Michal Vasko52927e22020-03-16 17:26:14 +01001700 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001701 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001702
1703 if (dup_p) {
1704 *dup_p = dup;
1705 }
1706 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001707
1708error:
Michal Vasko52927e22020-03-16 17:26:14 +01001709 lyd_free_tree(dup);
1710 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001711}
1712
Michal Vasko29d674b2021-08-25 11:18:35 +02001713/**
1714 * @brief Get a parent node to connect duplicated subtree to.
1715 *
1716 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001717 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001718 * @param[in] parent Initial parent to connect to.
1719 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1720 * @param[out] dup_parent First duplicated parent node, if any.
1721 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1722 * @return LY_ERR value.
1723 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001724static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001725lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
1726 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001727{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001728 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02001729 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001730
1731 *dup_parent = NULL;
1732 *local_parent = NULL;
1733
1734 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1735 if (parent && (parent->schema == orig_parent->schema)) {
1736 /* stop creating parents, connect what we have into the provided parent */
1737 iter = parent;
1738 repeat = 0;
1739 } else {
1740 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001741 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 +02001742 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001743 }
1744 if (!*local_parent) {
1745 *local_parent = (struct lyd_node_inner *)iter;
1746 }
1747 if (iter->child) {
1748 /* 1) list - add after keys
1749 * 2) provided parent with some children */
1750 iter->child->prev->next = *dup_parent;
1751 if (*dup_parent) {
1752 (*dup_parent)->prev = iter->child->prev;
1753 iter->child->prev = *dup_parent;
1754 }
1755 } else {
1756 ((struct lyd_node_inner *)iter)->child = *dup_parent;
1757 }
1758 if (*dup_parent) {
1759 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
1760 }
1761 *dup_parent = (struct lyd_node *)iter;
1762 }
1763
1764 if (repeat && parent) {
1765 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01001766 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02001767 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001768 return LY_EINVAL;
1769 }
1770
1771 return LY_SUCCESS;
1772}
1773
1774static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001775lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
1776 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001777{
1778 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001779 const struct lyd_node *orig; /* original node to be duplicated */
1780 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001781 struct lyd_node *top = NULL; /* the most higher created node */
1782 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001783
Michal Vaskoddd76592022-01-17 13:34:48 +01001784 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001785
1786 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001787 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 +02001788 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001789 } else {
1790 local_parent = parent;
1791 }
1792
Radek Krejci22ebdba2019-07-25 13:59:43 +02001793 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001794 if (lysc_is_key(orig->schema)) {
1795 if (local_parent) {
1796 /* the key must already exist in the parent */
1797 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001798 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001799 } else {
1800 assert(!(options & LYD_DUP_WITH_PARENTS));
1801 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001802 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001803 LY_CHECK_GOTO(rc, error);
1804 }
1805 } else {
1806 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01001807 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
1808 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001809 LY_CHECK_GOTO(rc, error);
1810 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001811 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001812 break;
1813 }
1814 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001815
Michal Vasko3a41dff2020-07-15 14:30:28 +02001816 if (dup) {
1817 *dup = first;
1818 }
1819 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001820
1821error:
1822 if (top) {
1823 lyd_free_tree(top);
1824 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001825 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001826 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001827 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001828}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001829
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001830LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001831lyd_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 +02001832{
Michal Vaskoddd76592022-01-17 13:34:48 +01001833 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1834 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
1835 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1836 return LY_EINVAL;
1837 }
1838
1839 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
1840}
1841
1842LIBYANG_API_DEF LY_ERR
1843lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1844 uint32_t options, struct lyd_node **dup)
1845{
1846 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1847
1848 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001849}
1850
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001851LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001852lyd_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 +02001853{
Michal Vaskoddd76592022-01-17 13:34:48 +01001854 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
1855 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
1856 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
1857 return LY_EINVAL;
1858 }
1859
1860 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
1861}
1862
1863LIBYANG_API_DEF LY_ERR
1864lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
1865 uint32_t options, struct lyd_node **dup)
1866{
1867 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
1868
1869 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001870}
1871
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001872LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001873lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02001874{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001875 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001876 struct lyd_meta *mt, *last;
1877
Michal Vasko3a41dff2020-07-15 14:30:28 +02001878 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02001879
1880 /* create a copy */
1881 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001882 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02001883 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001884 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001885 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
1886 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02001887
1888 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001889 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02001890 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001891 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02001892 last->next = mt;
1893 } else {
1894 node->meta = mt;
1895 }
1896
Radek Krejci011e4aa2020-09-04 15:22:31 +02001897finish:
1898 if (ret) {
1899 lyd_free_meta_single(mt);
1900 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001901 *dup = mt;
1902 }
1903 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02001904}
1905
Michal Vasko4490d312020-06-16 13:08:55 +02001906/**
1907 * @brief Merge a source sibling into target siblings.
1908 *
1909 * @param[in,out] first_trg First target sibling, is updated if top-level.
1910 * @param[in] parent_trg Target parent.
1911 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001912 * @param[in] merge_cb Optional merge callback.
1913 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02001914 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02001915 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02001916 * @return LY_ERR value.
1917 */
1918static LY_ERR
1919lyd_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 +02001920 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02001921{
Michal Vasko4490d312020-06-16 13:08:55 +02001922 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02001923 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01001924 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02001925 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02001926 struct lyd_dup_inst *child_dup_inst = NULL;
1927 LY_ERR ret;
1928 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02001929
1930 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02001931 if (!sibling_src->schema) {
1932 /* try to find the same opaque node */
1933 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
1934 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001935 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02001936 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02001937 } else {
1938 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02001939 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02001940 }
1941
Michal Vaskocd3f6172021-05-18 16:14:50 +02001942 if (match_trg) {
1943 /* update match as needed */
1944 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
1945 } else {
1946 /* first instance of this node */
1947 first_inst = 1;
1948 }
1949
1950 if (match_trg) {
1951 /* call callback */
1952 if (merge_cb) {
1953 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
1954 }
1955
Michal Vasko4490d312020-06-16 13:08:55 +02001956 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01001957 if (!match_trg->schema) {
1958 if (lyd_compare_single(sibling_src, match_trg, 0)) {
1959 /* update value */
1960 opaq_trg = (struct lyd_node_opaq *)match_trg;
1961 opaq_src = (struct lyd_node_opaq *)sibling_src;
1962
1963 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
1964 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
1965 opaq_trg->hints = opaq_src->hints;
1966
1967 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
1968 opaq_trg->format = opaq_src->format;
1969 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
1970 &opaq_trg->val_prefix_data);
1971 }
1972 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
1973 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001974 /* since they are different, they cannot both be default */
1975 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
1976
Michal Vasko3a41dff2020-07-15 14:30:28 +02001977 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
1978 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02001979 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001980 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
1981 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02001982 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02001983
1984 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02001985 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02001986 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02001987 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02001988 /* update value */
1989 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02001990 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02001991
1992 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02001993 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02001994 }
1995
1996 /* check descendants, recursively */
1997 ret = LY_SUCCESS;
1998 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
1999 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2000 &child_dup_inst);
2001 if (ret) {
2002 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002003 }
2004 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002005 lyd_dup_inst_free(child_dup_inst);
2006 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002007 } else {
2008 /* node not found, merge it */
2009 if (options & LYD_MERGE_DESTRUCT) {
2010 dup_src = (struct lyd_node *)sibling_src;
2011 lyd_unlink_tree(dup_src);
2012 /* spend it */
2013 *sibling_src_p = NULL;
2014 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002015 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 +02002016 }
2017
Michal Vasko7e8315b2021-08-03 17:01:06 +02002018 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2019 /* set LYD_NEW for all the new nodes, required for validation */
2020 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2021 elem->flags |= LYD_NEW;
2022 LYD_TREE_DFS_END(dup_src, elem);
2023 }
Michal Vasko4490d312020-06-16 13:08:55 +02002024 }
2025
Michal Vaskocd3f6172021-05-18 16:14:50 +02002026 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002027 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002028
2029 if (first_inst) {
2030 /* remember not to find this instance next time */
2031 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2032 }
2033
2034 /* call callback, no source node */
2035 if (merge_cb) {
2036 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2037 }
Michal Vasko4490d312020-06-16 13:08:55 +02002038 }
2039
2040 return LY_SUCCESS;
2041}
2042
Michal Vasko3a41dff2020-07-15 14:30:28 +02002043static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002044lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2045 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002046{
2047 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002048 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002049 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002050 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002051
2052 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002053 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2054 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002055
2056 if (!source) {
2057 /* nothing to merge */
2058 return LY_SUCCESS;
2059 }
2060
Michal Vasko831422b2020-11-24 11:20:51 +01002061 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002062 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002063 return LY_EINVAL;
2064 }
2065
2066 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002067 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2068 /* skip data nodes from different modules */
2069 continue;
2070 }
2071
Radek Krejci857189e2020-09-01 13:26:36 +02002072 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002073 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2074 if (ret) {
2075 break;
2076 }
Michal Vasko4490d312020-06-16 13:08:55 +02002077 if (first && !sibling_src) {
2078 /* source was spent (unlinked), move to the next node */
2079 source = tmp;
2080 }
2081
Michal Vasko3a41dff2020-07-15 14:30:28 +02002082 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002083 break;
2084 }
2085 }
2086
2087 if (options & LYD_MERGE_DESTRUCT) {
2088 /* free any leftover source data that were not merged */
2089 lyd_free_siblings((struct lyd_node *)source);
2090 }
2091
Michal Vaskocd3f6172021-05-18 16:14:50 +02002092 lyd_dup_inst_free(dup_inst);
2093 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002094}
2095
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002096LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002097lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002098{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002099 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002100}
2101
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002102LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002103lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002104{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002105 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2106}
2107
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002108LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002109lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2110 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2111{
2112 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002113}
2114
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002115static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002116lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002117{
Michal Vasko14654712020-02-06 08:35:21 +01002118 /* ending \0 */
2119 ++reqlen;
2120
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002121 if (reqlen > *buflen) {
2122 if (is_static) {
2123 return LY_EINCOMPLETE;
2124 }
2125
2126 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2127 if (!*buffer) {
2128 return LY_EMEM;
2129 }
2130
2131 *buflen = reqlen;
2132 }
2133
2134 return LY_SUCCESS;
2135}
2136
Michal Vaskod59035b2020-07-08 12:00:06 +02002137LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002138lyd_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 +02002139{
2140 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002141 size_t len;
2142 const char *val;
2143 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002144
Michal Vasko824d0832021-11-04 15:36:51 +01002145 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002146 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002147 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002148 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002149
2150 quot = '\'';
2151 if (strchr(val, '\'')) {
2152 quot = '"';
2153 }
2154 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002155 }
2156
2157 return LY_SUCCESS;
2158}
2159
2160/**
2161 * @brief Append leaf-list value predicate to path.
2162 *
2163 * @param[in] node Node to print.
2164 * @param[in,out] buffer Buffer to print to.
2165 * @param[in,out] buflen Current buffer length.
2166 * @param[in,out] bufused Current number of characters used in @p buffer.
2167 * @param[in] is_static Whether buffer is static or can be reallocated.
2168 * @return LY_ERR
2169 */
2170static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002171lyd_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 +02002172{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002173 size_t len;
2174 const char *val;
2175 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002176
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002177 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002178 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002179 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002180
2181 quot = '\'';
2182 if (strchr(val, '\'')) {
2183 quot = '"';
2184 }
2185 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2186
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002187 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002188}
2189
2190/**
2191 * @brief Append node position (relative to its other instances) predicate to path.
2192 *
2193 * @param[in] node Node to print.
2194 * @param[in,out] buffer Buffer to print to.
2195 * @param[in,out] buflen Current buffer length.
2196 * @param[in,out] bufused Current number of characters used in @p buffer.
2197 * @param[in] is_static Whether buffer is static or can be reallocated.
2198 * @return LY_ERR
2199 */
2200static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002201lyd_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 +02002202{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002203 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002204 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002205 char *val = NULL;
2206 LY_ERR rc;
2207
Michal Vasko50cc0562021-05-18 16:15:43 +02002208 pos = lyd_list_pos(node);
2209 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002210 return LY_EMEM;
2211 }
2212
2213 len = 1 + strlen(val) + 1;
2214 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2215 if (rc != LY_SUCCESS) {
2216 goto cleanup;
2217 }
2218
2219 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2220
2221cleanup:
2222 free(val);
2223 return rc;
2224}
2225
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002226LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002227lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2228{
Radek Krejci857189e2020-09-01 13:26:36 +02002229 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002230 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002231 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002232 const struct lyd_node *iter, *parent;
2233 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002234 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002235
2236 LY_CHECK_ARG_RET(NULL, node, NULL);
2237 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002238 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002239 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002240 } else {
2241 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002242 }
2243
2244 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002245 case LYD_PATH_STD:
2246 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002247 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002248 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002249 ++depth;
2250 }
2251
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002252 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002253 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002254 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002255 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002256iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002257 /* get the module */
2258 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
2259 parent = lyd_parent(iter);
2260 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
2261 if (prev_mod == mod) {
2262 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002263 }
2264
2265 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002266 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2267 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002268 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2269 if (rc != LY_SUCCESS) {
2270 break;
2271 }
2272
2273 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02002274 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01002275 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002276
Michal Vasko790b2bc2020-08-03 13:35:06 +02002277 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002278 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002279 switch (iter->schema->nodetype) {
2280 case LYS_LIST:
2281 if (iter->schema->flags & LYS_KEYLESS) {
2282 /* print its position */
2283 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2284 } else {
2285 /* print all list keys in predicates */
2286 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2287 }
2288 break;
2289 case LYS_LEAFLIST:
2290 if (iter->schema->flags & LYS_CONFIG_W) {
2291 /* print leaf-list value */
2292 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2293 } else {
2294 /* print its position */
2295 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2296 }
2297 break;
2298 default:
2299 /* nothing to print more */
2300 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002301 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002302 }
2303 if (rc != LY_SUCCESS) {
2304 break;
2305 }
2306
Michal Vasko14654712020-02-06 08:35:21 +01002307 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002308 }
2309 break;
2310 }
2311
2312 return buffer;
2313}
Michal Vaskoe444f752020-02-10 12:20:06 +01002314
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002315LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002316lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2317{
2318 struct lyd_meta *ret = NULL;
2319 const struct ly_ctx *ctx;
2320 const char *prefix, *tmp;
2321 char *str;
2322 size_t pref_len, name_len;
2323
2324 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002325 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002326
2327 if (!first) {
2328 return NULL;
2329 }
2330
2331 ctx = first->annotation->module->ctx;
2332
2333 /* parse the name */
2334 tmp = name;
2335 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2336 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2337 return NULL;
2338 }
2339
2340 /* find the module */
2341 if (prefix) {
2342 str = strndup(prefix, pref_len);
2343 module = ly_ctx_get_module_latest(ctx, str);
2344 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002345 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 +02002346 }
2347
2348 /* find the metadata */
2349 LY_LIST_FOR(first, first) {
2350 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2351 ret = (struct lyd_meta *)first;
2352 break;
2353 }
2354 }
2355
2356 return ret;
2357}
2358
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002359LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002360lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2361{
Michal Vasko9beceb82022-04-05 12:14:15 +02002362 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002363 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002364 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002365
Michal Vaskof03ed032020-03-04 13:31:44 +01002366 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002367 if (!siblings) {
2368 /* no data */
2369 if (match) {
2370 *match = NULL;
2371 }
2372 return LY_ENOTFOUND;
2373 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002374
Michal Vasko9beceb82022-04-05 12:14:15 +02002375 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2376 /* create a duplicate in this context */
2377 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2378 target = dup;
2379 }
2380
2381 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2382 /* schema mismatch */
2383 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002384 if (match) {
2385 *match = NULL;
2386 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002387 return LY_ENOTFOUND;
2388 }
2389
Michal Vaskoe78faec2021-04-08 17:24:43 +02002390 /* get first sibling */
2391 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002392
Michal Vasko9e685082021-01-29 14:49:09 +01002393 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01002394 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002395 assert(target->hash);
2396
Michal Vaskoe78faec2021-04-08 17:24:43 +02002397 if (lysc_is_dup_inst_list(target->schema)) {
2398 /* we must search the instances from beginning to find the first matching one */
2399 found = 0;
2400 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
2401 if (!lyd_compare_single(target, iter, 0)) {
2402 found = 1;
2403 break;
2404 }
2405 }
2406 if (found) {
2407 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002408 } else {
2409 siblings = NULL;
2410 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002411 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002412 /* find by hash */
2413 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2414 siblings = *match_p;
2415 } else {
2416 /* not found */
2417 siblings = NULL;
2418 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002419 }
2420 } else {
2421 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02002422 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002423 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002424 break;
2425 }
2426 }
2427 }
2428
Michal Vasko9beceb82022-04-05 12:14:15 +02002429 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002430 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002431 if (match) {
2432 *match = NULL;
2433 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002434 return LY_ENOTFOUND;
2435 }
2436
Michal Vasko9b368d32020-02-14 13:53:31 +01002437 if (match) {
2438 *match = (struct lyd_node *)siblings;
2439 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002440 return LY_SUCCESS;
2441}
2442
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002443LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002444lyd_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 +02002445 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002446{
2447 LY_ERR rc;
2448 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002449 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002450
Michal Vasko4c583e82020-07-17 12:16:14 +02002451 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002452 if (!siblings) {
2453 /* no data */
2454 if (match) {
2455 *match = NULL;
2456 }
2457 return LY_ENOTFOUND;
2458 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002459
Michal Vasko9beceb82022-04-05 12:14:15 +02002460 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2461 /* parent of ext nodes is useless */
2462 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
2463 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
2464 }
2465
2466 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2467 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002468 if (match) {
2469 *match = NULL;
2470 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002471 return LY_ENOTFOUND;
2472 }
2473
Michal Vaskof03ed032020-03-04 13:31:44 +01002474 if (key_or_value && !val_len) {
2475 val_len = strlen(key_or_value);
2476 }
2477
Michal Vaskob104f112020-07-17 09:54:54 +02002478 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2479 /* create a data node and find the instance */
2480 if (schema->nodetype == LYS_LEAFLIST) {
2481 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02002482 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 +02002483 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002484 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002485 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002486 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002487 }
2488
2489 /* find it */
2490 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002491 } else {
2492 /* find the first schema node instance */
2493 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002494 }
2495
Michal Vaskoe444f752020-02-10 12:20:06 +01002496 lyd_free_tree(target);
2497 return rc;
2498}
Michal Vaskoccc02342020-05-21 10:09:21 +02002499
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002500LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002501lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2502{
2503 struct lyd_node **match_p, *first, *iter;
2504 struct lyd_node_inner *parent;
2505
2506 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002507 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002508
2509 LY_CHECK_RET(ly_set_new(set));
2510
2511 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2512 /* no data or schema mismatch */
2513 return LY_ENOTFOUND;
2514 }
2515
2516 /* get first sibling */
2517 siblings = lyd_first_sibling(siblings);
2518
2519 parent = siblings->parent;
2520 if (parent && parent->schema && parent->children_ht) {
2521 assert(target->hash);
2522
2523 /* find the first instance */
2524 lyd_find_sibling_first(siblings, target, &first);
2525 if (first) {
2526 /* add it so that it is the first in the set */
2527 if (ly_set_add(*set, first, 1, NULL)) {
2528 goto error;
2529 }
2530
2531 /* find by hash */
2532 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2533 iter = *match_p;
2534 } else {
2535 /* not found */
2536 iter = NULL;
2537 }
2538 while (iter) {
2539 /* add all found nodes into the set */
2540 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
2541 goto error;
2542 }
2543
2544 /* find next instance */
2545 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2546 iter = NULL;
2547 } else {
2548 iter = *match_p;
2549 }
2550 }
2551 }
2552 } else {
2553 /* no children hash table */
2554 LY_LIST_FOR(siblings, siblings) {
2555 if (!lyd_compare_single(target, siblings, 0)) {
2556 ly_set_add(*set, (void *)siblings, 1, NULL);
2557 }
2558 }
2559 }
2560
2561 if (!(*set)->count) {
2562 return LY_ENOTFOUND;
2563 }
2564 return LY_SUCCESS;
2565
2566error:
2567 ly_set_free(*set, NULL);
2568 *set = NULL;
2569 return LY_EMEM;
2570}
2571
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002572LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002573lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2574{
2575 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2576
2577 for ( ; first; first = first->next) {
2578 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
2579 break;
2580 }
2581 }
2582
2583 if (match) {
2584 *match = (struct lyd_node *)first;
2585 }
2586 return first ? LY_SUCCESS : LY_ENOTFOUND;
2587}
2588
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002589LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002590lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2591 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002592{
2593 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002594 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02002595 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002596 uint32_t i;
2597
Michal Vaskoddd76592022-01-17 13:34:48 +01002598 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002599
Michal Vasko37c69432021-04-12 14:48:24 +02002600 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02002601
Michal Vaskoddd76592022-01-17 13:34:48 +01002602 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01002603 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002604 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002605
2606 /* evaluate expression */
Michal Vaskoddd76592022-01-17 13:34:48 +01002607 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 +02002608 LY_CHECK_GOTO(ret, cleanup);
2609
2610 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02002611 ret = ly_set_new(set);
2612 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002613
2614 /* transform into ly_set */
2615 if (xp_set.type == LYXP_SET_NODE_SET) {
2616 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
2617 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002618 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002619 (*set)->size = xp_set.used;
2620
2621 for (i = 0; i < xp_set.used; ++i) {
2622 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02002623 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002624 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02002625 }
2626 }
2627 }
2628
2629cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002630 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002631 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01002632 if (ret) {
2633 ly_set_free(*set, NULL);
2634 *set = NULL;
2635 }
Michal Vaskoccc02342020-05-21 10:09:21 +02002636 return ret;
2637}
Radek Krejcica989142020-11-05 11:32:22 +01002638
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002639LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002640lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2641 const struct lyxp_var *vars, struct ly_set **set)
2642{
2643 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2644
2645 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2646}
2647
2648LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01002649lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2650{
2651 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2652
Michal Vaskoddd76592022-01-17 13:34:48 +01002653 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002654}
2655
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002656LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002657lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2658{
Michal Vaskoe3716b32021-12-13 16:58:25 +01002659 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2660
Michal Vaskoddd76592022-01-17 13:34:48 +01002661 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02002662}
2663
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002664LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002665lyd_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 +02002666{
2667 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02002668 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002669 struct lyxp_expr *exp = NULL;
2670
2671 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
2672
2673 /* compile expression */
2674 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
2675 LY_CHECK_GOTO(ret, cleanup);
2676
2677 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02002678 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 +02002679 LY_CHECK_GOTO(ret, cleanup);
2680
2681 /* transform into boolean */
2682 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
2683 LY_CHECK_GOTO(ret, cleanup);
2684
2685 /* set result */
2686 *result = xp_set.val.bln;
2687
2688cleanup:
2689 lyxp_set_free_content(&xp_set);
2690 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
2691 return ret;
2692}
2693
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002694LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02002695lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
2696{
2697 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
2698}
2699
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002700LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01002701lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
2702{
2703 LY_ERR ret = LY_SUCCESS;
2704 struct lyxp_expr *expr = NULL;
2705 struct ly_path *lypath = NULL;
2706
2707 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
2708
2709 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002710 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 +01002711 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
2712 LY_CHECK_GOTO(ret, cleanup);
2713
2714 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02002715 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02002716 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 +01002717 LY_CHECK_GOTO(ret, cleanup);
2718
2719 /* evaluate the path */
2720 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
2721
2722cleanup:
2723 lyxp_expr_free(LYD_CTX(ctx_node), expr);
2724 ly_path_free(LYD_CTX(ctx_node), lypath);
2725 return ret;
2726}
2727
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002728LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02002729lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
2730{
2731 LY_ERR ret;
2732 struct lyd_node *m;
2733
2734 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
2735
2736 ret = ly_path_eval(path, tree, &m);
2737 if (ret) {
2738 if (match) {
2739 *match = NULL;
2740 }
2741 return LY_ENOTFOUND;
2742 }
2743
2744 if (match) {
2745 *match = m;
2746 }
2747 return LY_SUCCESS;
2748}