blob: d921b4ab3f7f0a86d91aaed8683a689fb94069bc [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"
Michal Vasko69730152020-10-09 16:30:07 +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
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
59 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Radek Krejci0b013302021-03-29 15:22:32 +020064 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vaskofeca4fb2020-10-05 08:58:40 +020066 if (incomplete) {
67 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020068 }
69
Michal Vasko405cc9e2020-12-01 12:01:27 +010070 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Radek Iša96b2de12021-02-16 10:32:57 +010071 if (dynamic) {
72 *dynamic = 0;
73 }
74
Michal Vasko90932a92020-02-12 14:33:03 +010075 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020076 if (incomplete) {
77 *incomplete = 1;
78 }
79 } else if (ret) {
80 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010081 LOGVAL(ctx, err->vecode, err->msg);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020082 ly_err_free(err);
83 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +010084 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 }
86 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010087 }
88
Michal Vaskofeca4fb2020-10-05 08:58:40 +020089 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010090}
91
Radek Krejci38d85362019-09-05 16:26:38 +020092LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020093lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010094 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020095{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +020097 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +020098
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100100
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200101 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
102 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200103 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100104 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejci38d85362019-09-05 16:26:38 +0200105 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200106 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200110 }
111
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200112 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200113}
114
Michal Vaskof937cfe2020-08-03 16:07:12 +0200115LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200116lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200117 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200118{
119 LY_ERR rc = LY_SUCCESS;
120 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200121 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200122 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200123
124 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
125
126 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
127 LOGARG(ctx, node);
128 return LY_EINVAL;
129 }
130
Michal Vasko22df3f02020-08-24 13:29:22 +0200131 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200132 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100133 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200134 if (rc == LY_EINCOMPLETE) {
135 /* actually success since we do not provide the context tree and call validation with
136 * LY_TYPE_OPTS_INCOMPLETE_DATA */
137 rc = LY_SUCCESS;
138 } else if (rc && err) {
139 if (ctx) {
140 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100141 LOG_LOCSET(NULL, NULL, err->path, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100142 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100143 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200144 }
Radek Krejci73dead22019-07-11 16:46:16 +0200145 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200146 }
147
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200148 if (!rc) {
149 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
150 }
Radek Krejci084289f2019-07-09 17:35:30 +0200151 return rc;
152}
153
154API LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200155lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
156 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
Radek Krejci084289f2019-07-09 17:35:30 +0200157{
158 LY_ERR rc;
159 struct ly_err_item *err = NULL;
160 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200161 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200162 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200163
Michal Vaskoaebbce02021-04-06 09:23:37 +0200164 LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
Radek Krejci084289f2019-07-09 17:35:30 +0200165
Michal Vaskoaebbce02021-04-06 09:23:37 +0200166 type = ((struct lysc_node_leaf *)schema)->type;
167
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200168 /* store */
Michal Vaskoaebbce02021-04-06 09:23:37 +0200169 rc = type->plugin->store(ctx ? ctx : schema->module->ctx, type, value, value_len, 0, LY_PREF_JSON, NULL,
170 LYD_HINT_DATA, schema, &val, NULL, &err);
171 if (!rc || (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200172 stored = 1;
Michal Vaskoaebbce02021-04-06 09:23:37 +0200173 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200174
Michal Vaskoaebbce02021-04-06 09:23:37 +0200175 if (ctx_node && (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 /* resolve */
Michal Vaskoaebbce02021-04-06 09:23:37 +0200177 rc = type->plugin->validate(ctx ? ctx : schema->module->ctx, type, ctx_node, ctx_node, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200178 }
179
Michal Vaskoaebbce02021-04-06 09:23:37 +0200180 if (rc && (rc != LY_EINCOMPLETE) && err) {
181 if (ctx) {
182 /* log error */
183 if (err->path) {
184 LOG_LOCSET(NULL, NULL, err->path, NULL);
185 } else if (ctx_node) {
186 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
187 } else {
188 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +0200189 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200190 LOGVAL(ctx, err->vecode, err->msg);
191 if (err->path) {
192 LOG_LOCBACK(0, 0, 1, 0);
193 } else if (ctx_node) {
194 LOG_LOCBACK(0, 1, 0, 0);
195 } else {
196 LOG_LOCBACK(1, 0, 0, 0);
197 }
Radek Krejci084289f2019-07-09 17:35:30 +0200198 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200199 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200200 }
201
Michal Vaskoaebbce02021-04-06 09:23:37 +0200202 if (!rc || (rc == LY_EINCOMPLETE)) {
203 if (realtype) {
204 /* return realtype */
205 if (val.realtype->basetype == LY_TYPE_UNION) {
206 *realtype = val.subvalue->value.realtype;
207 } else {
208 *realtype = val.realtype;
209 }
210 }
211
212 if (canonical) {
213 /* return canonical value */
214 lydict_insert(ctx ? ctx : schema->module->ctx, val.canonical, 0, canonical);
Michal Vasko29134f82020-11-13 18:03:20 +0100215 }
Michal Vasko3701af52020-08-03 14:29:38 +0200216 }
217
Michal Vaskoaebbce02021-04-06 09:23:37 +0200218 if (stored) {
219 /* free value */
220 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
221 }
222 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200223}
224
225API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200226lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200227{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200228 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200229 struct ly_ctx *ctx;
230 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200231 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200232
233 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
234
235 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200236 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200237
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200238 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100239 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100240 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100241 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200242 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200243
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200244 /* compare values */
245 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200246
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200247 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200248 return ret;
249}
250
Radek Krejci19611252020-10-04 13:54:53 +0200251API ly_bool
252lyd_is_default(const struct lyd_node *node)
253{
254 const struct lysc_node_leaf *leaf;
255 const struct lysc_node_leaflist *llist;
256 const struct lyd_node_term *term;
257 LY_ARRAY_COUNT_TYPE u;
258
259 assert(node->schema->nodetype & LYD_NODE_TERM);
260 term = (const struct lyd_node_term *)node;
261
262 if (node->schema->nodetype == LYS_LEAF) {
263 leaf = (const struct lysc_node_leaf *)node->schema;
264 if (!leaf->dflt) {
265 return 0;
266 }
267
268 /* compare with the default value */
269 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
270 return 0;
271 }
272 } else {
273 llist = (const struct lysc_node_leaflist *)node->schema;
274 if (!llist->dflts) {
275 return 0;
276 }
277
278 LY_ARRAY_FOR(llist->dflts, u) {
279 /* compare with each possible default value */
280 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
281 return 0;
282 }
283 }
284 }
285
286 return 1;
287}
288
Radek Krejci7931b192020-06-25 17:05:03 +0200289static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200290lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200291{
Michal Vasko69730152020-10-09 16:30:07 +0200292 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200293 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200294 const char *path = in->method.fpath.filepath;
295 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200296
297 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200298 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
301 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200302 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
304 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100306 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
307 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200308 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200309 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 }
311
Radek Krejci7931b192020-06-25 17:05:03 +0200312 return format;
313}
Radek Krejcie7b95092019-05-15 11:03:07 +0200314
Michal Vaskoe0665742021-02-11 11:08:44 +0100315/**
316 * @brief Parse YANG data into a data tree.
317 *
318 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100319 * @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 +0100320 * @param[in] parent Parent to connect the parsed nodes to, if any.
321 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
322 * @param[in] in Input handle to read the input from.
323 * @param[in] format Expected format of the data in @p in.
324 * @param[in] parse_opts Options for parser.
325 * @param[in] val_opts Options for validation.
326 * @param[out] op Optional pointer to the parsed operation, if any.
327 * @return LY_ERR value.
328 */
329static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100330lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
331 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 +0200332{
Michal Vaskoe0665742021-02-11 11:08:44 +0100333 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200334 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100335 struct ly_set parsed = {0};
336 struct lyd_node *first;
337 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200338
Michal Vaskoe0665742021-02-11 11:08:44 +0100339 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200340
341 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100342 if (first_p) {
343 *first_p = NULL;
344 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200345
Michal Vasko63f3d842020-07-08 10:10:14 +0200346 /* remember input position */
347 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200348
Michal Vaskoe0665742021-02-11 11:08:44 +0100349 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200350 switch (format) {
351 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100352 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200353 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200354 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100355 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200356 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200357 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100358 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200359 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200360 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100361 LOGARG(ctx, format);
362 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100363 break;
364 }
365 LY_CHECK_GOTO(rc, cleanup);
366
367 if (parent) {
368 /* get first top-level sibling */
369 for (first = parent; first->parent; first = lyd_parent(first)) {}
370 first = lyd_first_sibling(first);
371 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200372 }
373
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 if (!(parse_opts & LYD_PARSE_ONLY)) {
375 /* validate data */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200376 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_exts, &lydctx->node_types,
Michal Vaskoe0665742021-02-11 11:08:44 +0100377 &lydctx->meta_types, NULL);
378 LY_CHECK_GOTO(rc, cleanup);
379 }
Radek Krejci7931b192020-06-25 17:05:03 +0200380
Michal Vaskoe0665742021-02-11 11:08:44 +0100381 /* set the operation node */
382 if (op) {
383 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 }
385
386cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 if (lydctx) {
388 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200389 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 if (rc) {
391 if (parent) {
392 /* free all the parsed subtrees */
393 for (i = 0; i < parsed.count; ++i) {
394 lyd_free_tree(parsed.dnodes[i]);
395 }
396 } else {
397 /* free everything */
398 lyd_free_all(*first_p);
399 *first_p = NULL;
400 }
401 }
402 ly_set_erase(&parsed, NULL);
403 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200404}
405
406API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100407lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
408 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
409{
410 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
411
412 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
413 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
414 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
415
416 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
417}
418
419API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100420lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
421 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
422{
423 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
424 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
425 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
426
Radek Krejcif16e2542021-02-17 15:39:23 +0100427 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100428}
429
430API LY_ERR
431lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
432 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200433{
434 LY_ERR ret;
435 struct ly_in *in;
436
437 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100438 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200439
440 ly_in_free(in, 0);
441 return ret;
442}
443
444API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200445lyd_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 +0200446 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200447{
448 LY_ERR ret;
449 struct ly_in *in;
450
451 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100452 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200453
454 ly_in_free(in, 0);
455 return ret;
456}
457
458API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
460 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200461{
462 LY_ERR ret;
463 struct ly_in *in;
464
465 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100466 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200467
468 ly_in_free(in, 0);
469 return ret;
470}
471
Radek Krejcif16e2542021-02-17 15:39:23 +0100472/**
473 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
474 * for schema nodes locked inside the extension instance.
475 *
476 * At least one of @p parent, @p tree, or @p op must always be set.
477 *
478 * Specific @p data_type values have different parameter meaning as follows:
479 * - ::LYD_TYPE_RPC_NETCONF:
480 * - @p parent - must be NULL, the whole RPC is expected;
481 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
482 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
483 * a separate opaque data tree, even if the function fails, this may be returned;
484 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
485 *
486 * - ::LYD_TYPE_NOTIF_NETCONF:
487 * - @p parent - must be NULL, the whole notification is expected;
488 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
489 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
490 * a separate opaque data tree, even if the function fails, this may be returned;
491 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
492 *
493 * - ::LYD_TYPE_REPLY_NETCONF:
494 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
495 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
496 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
497 * a separate opaque data tree, even if the function fails, this may be returned;
498 * - @p op - must be NULL, the reply is appended to the RPC;
499 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
500 *
501 * @param[in] ctx libyang context.
502 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
503 * @param[in] parent Optional parent to connect the parsed nodes to.
504 * @param[in] in Input handle to read the input from.
505 * @param[in] format Expected format of the data in @p in.
506 * @param[in] data_type Expected operation to parse (@ref datatype).
507 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
508 * @param[out] op Optional parsed operation node.
509 * @return LY_ERR value.
510 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
511 */
512static LY_ERR
513lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
514 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 +0200515{
Michal Vaskoe0665742021-02-11 11:08:44 +0100516 LY_ERR rc = LY_SUCCESS;
517 struct lyd_ctx *lydctx = NULL;
518 struct ly_set parsed = {0};
519 struct lyd_node *first = NULL, *envp = NULL;
520 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200521
Michal Vasko27fb0262021-02-23 09:42:01 +0100522 if (!ctx) {
523 ctx = LYD_CTX(parent);
524 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 if (tree) {
526 *tree = NULL;
527 }
528 if (op) {
529 *op = NULL;
530 }
531
Radek Krejci7931b192020-06-25 17:05:03 +0200532 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200533
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 /* remember input position */
535 in->func_start = in->current;
536
Michal Vaskoe0665742021-02-11 11:08:44 +0100537 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100538 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100539 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100540 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
541 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
542 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100543 }
544 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
545 val_opts = 0;
546
547 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200548 switch (format) {
549 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100550 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100551 if (rc && envp) {
552 /* special situation when the envelopes were parsed successfully */
553 if (tree) {
554 *tree = envp;
555 }
556 ly_set_erase(&parsed, NULL);
557 return rc;
558 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100559 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200560 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100561 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100562 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200563 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100564 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100565 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200566 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100567 LOGARG(ctx, format);
568 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200569 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200570 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100571 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200572
Michal Vaskoe0665742021-02-11 11:08:44 +0100573 /* set out params correctly */
574 if (tree) {
575 if (envp) {
576 /* special out param meaning */
577 *tree = envp;
578 } else {
579 *tree = parent ? NULL : first;
580 }
581 }
582 if (op) {
583 *op = lydctx->op_node;
584 }
585
586cleanup:
587 if (lydctx) {
588 lydctx->free(lydctx);
589 }
590 if (rc) {
591 if (parent) {
592 /* free all the parsed subtrees */
593 for (i = 0; i < parsed.count; ++i) {
594 lyd_free_tree(parsed.dnodes[i]);
595 }
596 } else {
597 /* free everything (cannot occur in the current code, a safety) */
598 lyd_free_all(first);
599 if (tree) {
600 *tree = NULL;
601 }
602 if (op) {
603 *op = NULL;
604 }
605 }
606 }
607 ly_set_erase(&parsed, NULL);
608 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200609}
Radek Krejci084289f2019-07-09 17:35:30 +0200610
Radek Krejcif16e2542021-02-17 15:39:23 +0100611API LY_ERR
612lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
613 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
614{
615 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
616
617 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
618}
619
620API LY_ERR
621lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
622 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
623{
624 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
625
626 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
627
628 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
629}
630
Michal Vasko90932a92020-02-12 14:33:03 +0100631LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200632lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
633 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100634{
635 LY_ERR ret;
636 struct lyd_node_term *term;
637
Michal Vasko9b368d32020-02-14 13:53:31 +0100638 assert(schema->nodetype & LYD_NODE_TERM);
639
Michal Vasko90932a92020-02-12 14:33:03 +0100640 term = calloc(1, sizeof *term);
641 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
642
643 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100644 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100645 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100646
Radek Krejciddace2c2021-01-08 11:30:56 +0100647 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200648 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100650 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200651 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100652 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100653
Michal Vasko9e685082021-01-29 14:49:09 +0100654 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 return ret;
656}
657
658LY_ERR
659lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
660{
661 LY_ERR ret;
662 struct lyd_node_term *term;
663 struct lysc_type *type;
664
665 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200666 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100667
668 term = calloc(1, sizeof *term);
669 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
670
671 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100672 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100673 term->flags = LYD_NEW;
674
675 type = ((struct lysc_node_leaf *)schema)->type;
676 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
677 if (ret) {
678 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
679 free(term);
680 return ret;
681 }
Michal Vasko9e685082021-01-29 14:49:09 +0100682 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100683
Michal Vasko9e685082021-01-29 14:49:09 +0100684 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100685 return ret;
686}
687
688LY_ERR
689lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
690{
691 struct lyd_node_inner *in;
692
Michal Vasko9b368d32020-02-14 13:53:31 +0100693 assert(schema->nodetype & LYD_NODE_INNER);
694
Michal Vasko90932a92020-02-12 14:33:03 +0100695 in = calloc(1, sizeof *in);
696 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
697
698 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100699 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100700 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100701 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
702 in->flags |= LYD_DEFAULT;
703 }
Michal Vasko90932a92020-02-12 14:33:03 +0100704
Michal Vasko9b368d32020-02-14 13:53:31 +0100705 /* do not hash list with keys, we need them for the hash */
706 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100707 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100708 }
Michal Vasko90932a92020-02-12 14:33:03 +0100709
Michal Vasko9e685082021-01-29 14:49:09 +0100710 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100711 return LY_SUCCESS;
712}
713
Michal Vasko90932a92020-02-12 14:33:03 +0100714LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200715lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100716{
717 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100718 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200719 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100720
Michal Vasko004d3152020-06-11 19:59:22 +0200721 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100722
723 /* create list */
724 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
725
Radek Krejciddace2c2021-01-08 11:30:56 +0100726 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100727
Michal Vasko90932a92020-02-12 14:33:03 +0100728 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200729 LY_ARRAY_FOR(predicates, u) {
730 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100731 lyd_insert_node(list, NULL, key);
732 }
733
Michal Vasko9b368d32020-02-14 13:53:31 +0100734 /* hash having all the keys */
735 lyd_hash(list);
736
Michal Vasko90932a92020-02-12 14:33:03 +0100737 /* success */
738 *node = list;
739 list = NULL;
740
741cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100742 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100743 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200744 return ret;
745}
746
747static LY_ERR
748lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
749{
750 LY_ERR ret = LY_SUCCESS;
751 struct lyxp_expr *expr = NULL;
752 uint16_t exp_idx = 0;
753 enum ly_path_pred_type pred_type = 0;
754 struct ly_path_predicate *predicates = NULL;
755
Radek Krejciddace2c2021-01-08 11:30:56 +0100756 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100757
Michal Vasko004d3152020-06-11 19:59:22 +0200758 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200759 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko69730152020-10-09 16:30:07 +0200760 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200761
762 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
764 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200765
766 /* create the list node */
767 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
768
769cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100770 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200771 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200772 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100773 return ret;
774}
775
776LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100777lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
778 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100779{
Michal Vasko366a4a12020-12-04 16:23:57 +0100780 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100781 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100782 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100783
Michal Vasko9b368d32020-02-14 13:53:31 +0100784 assert(schema->nodetype & LYD_NODE_ANY);
785
Michal Vasko90932a92020-02-12 14:33:03 +0100786 any = calloc(1, sizeof *any);
787 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
788
789 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100790 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100791 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100792
Radek Krejci1798aae2020-07-14 13:26:06 +0200793 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100794
795 if (use_value) {
796 any->value.str = value;
797 any->value_type = value_type;
798 } else {
799 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100800 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100801 LY_CHECK_ERR_RET(ret, free(any), ret);
802 }
Michal Vasko9e685082021-01-29 14:49:09 +0100803 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100804
Michal Vasko9e685082021-01-29 14:49:09 +0100805 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100806 return LY_SUCCESS;
807}
808
Michal Vasko52927e22020-03-16 17:26:14 +0100809LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100810lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
811 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
812 LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100813{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200814 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100815 struct lyd_node_opaq *opaq;
816
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200817 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100818
819 if (!value_len) {
820 value = "";
821 }
822
823 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100824 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100825
Michal Vasko9e685082021-01-29 14:49:09 +0100826 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100827 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200828
Michal Vasko52927e22020-03-16 17:26:14 +0100829 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100830 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100831 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200832 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100833 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200834 }
Michal Vasko52927e22020-03-16 17:26:14 +0100835 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200836 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100837 *dynamic = 0;
838 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200839 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100840 }
Michal Vasko501af032020-11-11 20:27:44 +0100841
842 opaq->format = format;
843 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200844 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100845 opaq->ctx = ctx;
846
Radek Krejci011e4aa2020-09-04 15:22:31 +0200847finish:
848 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100849 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100850 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200851 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100852 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 }
854 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100855}
856
Michal Vasko3a41dff2020-07-15 14:30:28 +0200857API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100858lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
859 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100860{
861 struct lyd_node *ret = NULL;
862 const struct lysc_node *schema;
863 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
864
Michal Vasko6027eb92020-07-15 16:37:30 +0200865 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100866
Michal Vaskof03ed032020-03-04 13:31:44 +0100867 if (!module) {
868 module = parent->schema->module;
869 }
870
Michal Vasko3a41dff2020-07-15 14:30:28 +0200871 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100872 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100873 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100874
Michal Vasko3a41dff2020-07-15 14:30:28 +0200875 LY_CHECK_RET(lyd_create_inner(schema, &ret));
876 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100877 lyd_insert_node(parent, NULL, ret);
878 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200879
880 if (node) {
881 *node = ret;
882 }
883 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100884}
885
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100887lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
888{
889 struct lyd_node *ret = NULL;
890 const struct lysc_node *schema;
891 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
892
893 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
894
895 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
896 if (!schema) {
897 if (ext->argument) {
898 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
899 name, ext->argument, ext->def->name);
900 } else {
901 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
902 name, ext->def->name);
903 }
904 return LY_ENOTFOUND;
905 }
906 LY_CHECK_RET(lyd_create_inner(schema, &ret));
907
908 *node = ret;
909
910 return LY_SUCCESS;
911}
912
913API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100914lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
915 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100916{
917 struct lyd_node *ret = NULL, *key;
918 const struct lysc_node *schema, *key_s;
919 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
920 va_list ap;
921 const char *key_val;
922 LY_ERR rc = LY_SUCCESS;
923
Michal Vasko6027eb92020-07-15 16:37:30 +0200924 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100925
Michal Vaskof03ed032020-03-04 13:31:44 +0100926 if (!module) {
927 module = parent->schema->module;
928 }
929
Radek Krejci41ac9942020-11-02 14:47:56 +0100930 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200931 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100932
933 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200934 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100935
Michal Vasko3a41dff2020-07-15 14:30:28 +0200936 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100937
938 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100939 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
Michal Vasko013a8182020-03-03 10:46:53 +0100940 key_val = va_arg(ap, const char *);
941
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200942 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
943 NULL, &key);
944 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100945 lyd_insert_node(ret, NULL, key);
946 }
947
Michal Vasko013a8182020-03-03 10:46:53 +0100948 if (parent) {
949 lyd_insert_node(parent, NULL, ret);
950 }
951
952cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200953 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100954 if (rc) {
955 lyd_free_tree(ret);
956 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200957 } else if (node) {
958 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100959 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100961}
962
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +0100964lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
965{
966 struct lyd_node *ret = NULL, *key;
967 const struct lysc_node *schema, *key_s;
968 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
969 va_list ap;
970 const char *key_val;
971 LY_ERR rc = LY_SUCCESS;
972
973 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
974
975 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
976 if (!schema) {
977 if (ext->argument) {
978 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
979 name, ext->argument, ext->def->name);
980 } else {
981 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
982 }
983 return LY_ENOTFOUND;
984 }
985 /* create list inner node */
986 LY_CHECK_RET(lyd_create_inner(schema, &ret));
987
988 va_start(ap, node);
989
990 /* create and insert all the keys */
991 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
992 key_val = va_arg(ap, const char *);
993
994 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
995 NULL, &key);
996 LY_CHECK_GOTO(rc, cleanup);
997 lyd_insert_node(ret, NULL, key);
998 }
999
1000cleanup:
1001 va_end(ap);
1002 if (rc) {
1003 lyd_free_tree(ret);
1004 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001005 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001006 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001007 return rc;
1008}
1009
1010API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001011lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001012 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001013{
1014 struct lyd_node *ret = NULL;
1015 const struct lysc_node *schema;
1016 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1017
Michal Vasko6027eb92020-07-15 16:37:30 +02001018 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001019
Michal Vaskof03ed032020-03-04 13:31:44 +01001020 if (!module) {
1021 module = parent->schema->module;
1022 }
Michal Vasko004d3152020-06-11 19:59:22 +02001023 if (!keys) {
1024 keys = "";
1025 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001026
Michal Vasko004d3152020-06-11 19:59:22 +02001027 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001028 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001029 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001030
Michal Vasko004d3152020-06-11 19:59:22 +02001031 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1032 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001034 } else {
1035 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001036 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001037 }
Michal Vasko004d3152020-06-11 19:59:22 +02001038 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001039 lyd_insert_node(parent, NULL, ret);
1040 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001041
1042 if (node) {
1043 *node = ret;
1044 }
1045 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001046}
1047
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048API LY_ERR
1049lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci41ac9942020-11-02 14:47:56 +01001050 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001051{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001052 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001053 struct lyd_node *ret = NULL;
1054 const struct lysc_node *schema;
1055 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1056
Michal Vasko6027eb92020-07-15 16:37:30 +02001057 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001058
Michal Vaskof03ed032020-03-04 13:31:44 +01001059 if (!module) {
1060 module = parent->schema->module;
1061 }
1062
Radek Krejci41ac9942020-11-02 14:47:56 +01001063 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001064 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001065
Radek Krejci8a5afc22021-03-12 11:10:47 +01001066 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001067 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001068 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001069 lyd_insert_node(parent, NULL, ret);
1070 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001071
1072 if (node) {
1073 *node = ret;
1074 }
1075 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001076}
1077
Michal Vasko3a41dff2020-07-15 14:30:28 +02001078API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001079lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1080{
1081 LY_ERR rc;
1082 struct lyd_node *ret = NULL;
1083 const struct lysc_node *schema;
1084 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1085
1086 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1087
1088 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1089 if (!schema) {
1090 if (ext->argument) {
1091 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1092 name, ext->argument, ext->def->name);
1093 } else {
1094 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1095 }
1096 return LY_ENOTFOUND;
1097 }
1098 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
1099 LY_CHECK_RET(rc);
1100
1101 *node = ret;
1102
1103 return LY_SUCCESS;
1104}
1105
1106API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001107lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1108 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001109{
1110 struct lyd_node *ret = NULL;
1111 const struct lysc_node *schema;
1112 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1113
Michal Vasko6027eb92020-07-15 16:37:30 +02001114 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001115
Michal Vaskof03ed032020-03-04 13:31:44 +01001116 if (!module) {
1117 module = parent->schema->module;
1118 }
1119
Radek Krejci41ac9942020-11-02 14:47:56 +01001120 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001121 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001122
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001123 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001124 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001125 lyd_insert_node(parent, NULL, ret);
1126 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001127
1128 if (node) {
1129 *node = ret;
1130 }
1131 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001132}
1133
Radek Krejci0b963da2021-03-12 13:23:44 +01001134API LY_ERR
1135lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1136 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1137{
1138 struct lyd_node *ret = NULL;
1139 const struct lysc_node *schema;
1140 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1141
1142 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1143
1144 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1145 if (!schema) {
1146 if (ext->argument) {
1147 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1148 name, ext->argument, ext->def->name);
1149 } else {
1150 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1151 }
1152 return LY_ENOTFOUND;
1153 }
1154 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1155
1156 *node = ret;
1157
1158 return LY_SUCCESS;
1159}
1160
Michal Vasko4490d312020-06-16 13:08:55 +02001161/**
1162 * @brief Update node value.
1163 *
1164 * @param[in] node Node to update.
1165 * @param[in] value New value to set.
1166 * @param[in] value_type Type of @p value for any node.
1167 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1168 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1169 * @return LY_ERR value.
1170 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001171static LY_ERR
1172lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001173 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001174{
1175 LY_ERR ret = LY_SUCCESS;
1176 struct lyd_node *new_any;
1177
1178 switch (node->schema->nodetype) {
1179 case LYS_CONTAINER:
1180 case LYS_NOTIF:
1181 case LYS_RPC:
1182 case LYS_ACTION:
1183 case LYS_LIST:
1184 case LYS_LEAFLIST:
1185 /* if it exists, there is nothing to update */
1186 *new_parent = NULL;
1187 *new_node = NULL;
1188 break;
1189 case LYS_LEAF:
1190 ret = lyd_change_term(node, value);
1191 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1192 /* there was an actual change (at least of the default flag) */
1193 *new_parent = node;
1194 *new_node = node;
1195 ret = LY_SUCCESS;
1196 } else if (ret == LY_ENOT) {
1197 /* no change */
1198 *new_parent = NULL;
1199 *new_node = NULL;
1200 ret = LY_SUCCESS;
1201 } /* else error */
1202 break;
1203 case LYS_ANYDATA:
1204 case LYS_ANYXML:
1205 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001206 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001207
1208 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001209 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001210 /* not equal, switch values (so that we can use generic node free) */
1211 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1212 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1213 ((struct lyd_node_any *)node)->value.str = value;
1214 ((struct lyd_node_any *)node)->value_type = value_type;
1215
1216 *new_parent = node;
1217 *new_node = node;
1218 } else {
1219 /* they are equal */
1220 *new_parent = NULL;
1221 *new_node = NULL;
1222 }
1223 lyd_free_tree(new_any);
1224 break;
1225 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001226 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001227 ret = LY_EINT;
1228 break;
1229 }
1230
1231 return ret;
1232}
1233
Michal Vasko3a41dff2020-07-15 14:30:28 +02001234API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001235lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1236 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001237{
Michal Vaskod86997b2020-05-26 15:19:54 +02001238 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001239 size_t pref_len, name_len;
1240
Michal Vasko2b5344c2021-02-26 10:12:05 +01001241 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1242 if (!ctx) {
1243 ctx = LYD_CTX(parent);
1244 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001245
Michal Vasko871a0252020-11-11 18:35:24 +01001246 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001247 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001248 return LY_EINVAL;
1249 }
Michal Vasko610553d2020-11-18 18:15:05 +01001250 if (meta) {
1251 *meta = NULL;
1252 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001253
1254 /* parse the name */
1255 tmp = name;
1256 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1257 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001258 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001259 }
1260
1261 /* find the module */
1262 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001263 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001264 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001265 }
1266
1267 /* set value if none */
1268 if (!val_str) {
1269 val_str = "";
1270 }
1271
Michal Vasko871a0252020-11-11 18:35:24 +01001272 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
1273 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1274}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001275
Michal Vaskoba696702020-11-11 19:12:45 +01001276API LY_ERR
1277lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1278 struct lyd_meta **meta)
1279{
1280 const struct lys_module *mod;
1281
1282 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1283
1284 if (parent && !parent->schema) {
1285 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1286 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001287 }
Michal Vasko610553d2020-11-18 18:15:05 +01001288 if (meta) {
1289 *meta = NULL;
1290 }
Michal Vaskoba696702020-11-11 19:12:45 +01001291
1292 switch (attr->format) {
1293 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001294 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001295 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001296 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001297 return LY_ENOTFOUND;
1298 }
1299 break;
1300 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001301 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001302 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001303 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001304 return LY_ENOTFOUND;
1305 }
1306 break;
1307 default:
1308 LOGINT_RET(ctx);
1309 }
1310
Michal Vaskoad92b672020-11-12 13:11:31 +01001311 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoba696702020-11-11 19:12:45 +01001312 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001313}
1314
Michal Vasko3a41dff2020-07-15 14:30:28 +02001315API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001316lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001317 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001318{
1319 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001320
Michal Vasko0ab974d2021-02-24 13:18:26 +01001321 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001322
1323 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001324 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001325 }
1326 if (!value) {
1327 value = "";
1328 }
1329
Michal Vasko0ab974d2021-02-24 13:18:26 +01001330 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1331 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001332 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001333 lyd_insert_node(parent, NULL, ret);
1334 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001335
1336 if (node) {
1337 *node = ret;
1338 }
1339 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001340}
1341
Michal Vasko3a41dff2020-07-15 14:30:28 +02001342API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001343lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001344 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001345{
1346 struct lyd_node *ret = NULL;
1347
1348 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1349
1350 if (!ctx) {
1351 ctx = LYD_CTX(parent);
1352 }
1353 if (!value) {
1354 value = "";
1355 }
1356
Michal Vasko0ab974d2021-02-24 13:18:26 +01001357 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1358 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001359 if (parent) {
1360 lyd_insert_node(parent, NULL, ret);
1361 }
1362
1363 if (node) {
1364 *node = ret;
1365 }
1366 return LY_SUCCESS;
1367}
1368
1369API LY_ERR
1370lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001371 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001372{
Radek Krejci1798aae2020-07-14 13:26:06 +02001373 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001374 const struct ly_ctx *ctx;
1375 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001376 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001377
Michal Vasko3a41dff2020-07-15 14:30:28 +02001378 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001379
Michal Vaskob7be7a82020-08-20 09:09:04 +02001380 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001381
1382 /* parse the name */
1383 tmp = name;
1384 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001385 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001386 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001387 }
1388
Michal Vasko51d21b82020-11-13 18:03:54 +01001389 /* get the module */
1390 if (module_name) {
1391 mod_len = strlen(module_name);
1392 } else {
1393 module_name = prefix;
1394 mod_len = pref_len;
1395 }
1396
Michal Vasko00cbf532020-06-15 13:58:47 +02001397 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001398 if (!value) {
1399 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001400 }
1401
Michal Vasko8d65f852021-02-17 11:28:13 +01001402 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1403 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1404
1405 if (attr) {
1406 *attr = ret;
1407 }
1408 return LY_SUCCESS;
1409}
1410
1411API LY_ERR
1412lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1413 struct lyd_attr **attr)
1414{
1415 struct lyd_attr *ret = NULL;
1416 const struct ly_ctx *ctx;
1417 const char *prefix, *tmp;
1418 size_t pref_len, name_len;
1419
1420 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1421
1422 ctx = LYD_CTX(parent);
1423
1424 /* parse the name */
1425 tmp = name;
1426 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1427 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1428 return LY_EVALID;
1429 }
1430
1431 /* set value if none */
1432 if (!value) {
1433 value = "";
1434 }
1435
1436 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1437 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001438
1439 if (attr) {
1440 *attr = ret;
1441 }
1442 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001443}
1444
1445API LY_ERR
1446lyd_change_term(struct lyd_node *term, const char *val_str)
1447{
1448 LY_ERR ret = LY_SUCCESS;
1449 struct lysc_type *type;
1450 struct lyd_node_term *t;
1451 struct lyd_node *parent;
1452 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001453 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001454
1455 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1456
1457 if (!val_str) {
1458 val_str = "";
1459 }
1460 t = (struct lyd_node_term *)term;
1461 type = ((struct lysc_node_leaf *)term->schema)->type;
1462
1463 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001464 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001465 ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
Radek Krejci2efc45b2020-12-22 16:25:44 +01001466 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001467 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001468 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001469
1470 /* compare original and new value */
1471 if (type->plugin->compare(&t->value, &val)) {
1472 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001473 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001474 t->value = val;
1475 memset(&val, 0, sizeof val);
1476 val_change = 1;
1477 } else {
1478 val_change = 0;
1479 }
1480
1481 /* always clear the default flag */
1482 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001483 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001484 parent->flags &= ~LYD_DEFAULT;
1485 }
1486 dflt_change = 1;
1487 } else {
1488 dflt_change = 0;
1489 }
1490
1491 if (val_change || dflt_change) {
1492 /* make the node non-validated */
1493 term->flags &= LYD_NEW;
1494 }
1495
1496 if (val_change) {
1497 if (term->schema->nodetype == LYS_LEAFLIST) {
1498 /* leaf-list needs to be hashed again and re-inserted into parent */
1499 lyd_unlink_hash(term);
1500 lyd_hash(term);
1501 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1502 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1503 /* list needs to be updated if its key was changed */
1504 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001505 lyd_unlink_hash(lyd_parent(term));
1506 lyd_hash(lyd_parent(term));
1507 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001508 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1509 }
1510
1511 /* retrun value */
1512 if (!val_change) {
1513 if (dflt_change) {
1514 /* only default flag change */
1515 ret = LY_EEXIST;
1516 } else {
1517 /* no change */
1518 ret = LY_ENOT;
1519 }
1520 } /* else value changed, LY_SUCCESS */
1521
1522cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001523 if (val.realtype) {
1524 type->plugin->free(LYD_CTX(term), &val);
1525 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001526 return ret;
1527}
1528
Michal Vasko41586352020-07-13 13:54:25 +02001529API LY_ERR
1530lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1531{
1532 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001533 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001534 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001535 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001536
1537 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1538
1539 if (!val_str) {
1540 val_str = "";
1541 }
1542
1543 /* parse the new value into a new meta structure */
1544 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vasko871a0252020-11-11 18:35:24 +01001545 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001546
1547 /* compare original and new value */
1548 if (lyd_compare_meta(meta, m2)) {
1549 /* values differ, switch them */
1550 val = meta->value;
1551 meta->value = m2->value;
1552 m2->value = val;
1553 val_change = 1;
1554 } else {
1555 val_change = 0;
1556 }
1557
1558 /* retrun value */
1559 if (!val_change) {
1560 /* no change */
1561 ret = LY_ENOT;
1562 } /* else value changed, LY_SUCCESS */
1563
1564cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001565 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001566 return ret;
1567}
1568
Michal Vasko6741dc62021-02-04 09:27:45 +01001569static LY_ERR
1570lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1571{
1572 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci910d0132021-03-05 14:19:31 +01001573 const struct lysc_node *schema = NULL;
Michal Vasko6741dc62021-02-04 09:27:45 +01001574 struct ly_path_predicate *pred;
Radek Krejci910d0132021-03-05 14:19:31 +01001575 LY_ARRAY_COUNT_TYPE u, new_count = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001576
1577 assert(path);
1578
1579 /* go through all the compiled nodes */
1580 LY_ARRAY_FOR(path, u) {
1581 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001582 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001583 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1584 if (schema->flags & LYS_KEYLESS) {
1585 /* creating a new keyless list instance */
1586 break;
1587 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1588 LOG_LOCSET(schema, NULL, NULL, NULL);
1589 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1590 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1591 LOG_LOCBACK(1, 0, 0, 0);
1592 return LY_EINVAL;
1593 } /* else creating an opaque list without keys */
1594 }
1595 }
1596
Michal Vasko6f9b4262021-02-04 12:09:56 +01001597 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001598 /* parse leafref value into a predicate, if not defined in the path */
1599 if (!value) {
1600 value = "";
1601 }
1602
1603 r = LY_SUCCESS;
1604 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskoaebbce02021-04-06 09:23:37 +02001605 r = lys_value_validate(NULL, schema, value, strlen(value), LY_PREF_JSON, NULL);
Michal Vasko6741dc62021-02-04 09:27:45 +01001606 }
1607 if (!r) {
1608 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001609 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1610 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001611
1612 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1613 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1614 LY_CHECK_GOTO(ret, cleanup);
1615 ++((struct lysc_type *)pred->value.realtype)->refcount;
1616
1617 if (schema->flags & LYS_CONFIG_R) {
1618 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001619 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001620 }
1621 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1622 }
1623
1624 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001625 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001626 LY_ARRAY_DECREMENT(path);
1627 }
1628
1629cleanup:
1630 return ret;
1631}
1632
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001633/**
1634 * @brief Create a new node in the data tree based on a path. All node types can be created.
1635 *
1636 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
1637 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
1638 * and @p value is set, the predicate is preferred.
1639 *
1640 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1641 * nodes, they are always created.
1642 *
1643 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1644 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1645 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1646 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1647 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1648 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1649 * searching for the appropriate module.
1650 * @param[in] path [Path](@ref howtoXPath) to create.
1651 * @param[in] value Value of the new leaf/leaf-list (const char *) or anyxml/anydata (expected type depends on @p value_type).
1652 * For other node types, it is ignored.
1653 * @param[in] value_type Anyxml/anydata node @p value type.
1654 * @param[in] options Bitmask of options, see @ref pathoptions.
1655 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1656 * @param[out] new_node Optional last node created.
1657 * @return LY_ERR value.
1658 */
1659static LY_ERR
1660lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1661 const void *value, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1662 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001663{
1664 LY_ERR ret = LY_SUCCESS, r;
1665 struct lyxp_expr *exp = NULL;
1666 struct ly_path *p = NULL;
1667 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1668 const struct lysc_node *schema;
Radek Krejci910d0132021-03-05 14:19:31 +01001669 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001670
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001671 assert(parent || ctx);
1672 assert(path);
1673 assert((path[0] == '/') || parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001674
1675 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001676 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001677 }
1678
1679 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001680 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko69730152020-10-09 16:30:07 +02001681 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001682
1683 /* compile path */
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001684 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, ext, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001685 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001686 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001687
Michal Vasko6741dc62021-02-04 09:27:45 +01001688 /* check the compiled path before searching existing nodes, it may be shortened */
1689 orig_count = LY_ARRAY_COUNT(p);
1690 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001691
1692 /* try to find any existing nodes in the path */
1693 if (parent) {
1694 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1695 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001696 if (orig_count == LY_ARRAY_COUNT(p)) {
1697 /* the node exists, are we supposed to update it or is it just a default? */
1698 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1699 LOG_LOCSET(NULL, node, NULL, NULL);
1700 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1701 LOG_LOCBACK(0, 1, 0, 0);
1702 ret = LY_EEXIST;
1703 goto cleanup;
1704 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001705
Michal Vasko6741dc62021-02-04 09:27:45 +01001706 /* update the existing node */
1707 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1708 goto cleanup;
1709 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001710 } else if (ret == LY_EINCOMPLETE) {
1711 /* some nodes were found, adjust the iterator to the next segment */
1712 ++path_idx;
1713 } else if (ret == LY_ENOTFOUND) {
1714 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vasko45b0d202020-11-06 17:20:46 +01001715 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001716 node = parent;
1717 }
1718 } else {
1719 /* error */
1720 goto cleanup;
1721 }
1722 }
1723
Michal Vasko6741dc62021-02-04 09:27:45 +01001724 /* restore the full path for creating new nodes */
1725 while (orig_count > LY_ARRAY_COUNT(p)) {
1726 LY_ARRAY_INCREMENT(p);
1727 }
1728
Michal Vasko00cbf532020-06-15 13:58:47 +02001729 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001730 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001731 cur_parent = node;
1732 schema = p[path_idx].node;
1733
1734 switch (schema->nodetype) {
1735 case LYS_LIST:
1736 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001737 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001738 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001739 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1740 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1741 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001742 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001743 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1744 LOG_LOCSET(schema, NULL, NULL, NULL);
1745 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1746 lys_nodetype2str(schema->nodetype), schema->name, path);
1747 LOG_LOCBACK(1, 0, 0, 0);
1748 ret = LY_EINVAL;
1749 goto cleanup;
1750 }
1751
Michal Vasko00cbf532020-06-15 13:58:47 +02001752 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1753 }
1754 break;
1755 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001756 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001757 case LYS_CONTAINER:
1758 case LYS_NOTIF:
1759 case LYS_RPC:
1760 case LYS_ACTION:
1761 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1762 break;
1763 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001764 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001765 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001766 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1767 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1768 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001769 } else {
1770 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1771 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1772 }
1773 break;
1774 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001775 if (lysc_is_key(schema)) {
1776 /* it must have been already created or some error will occur later */
1777 assert(cur_parent);
1778 node = lyd_child(cur_parent);
1779 assert(node && (node->schema == schema));
1780 goto next_iter;
1781 }
1782
1783 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001784 if (!value) {
1785 value = "";
1786 }
1787
1788 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001789 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskoaebbce02021-04-06 09:23:37 +02001790 r = lys_value_validate(NULL, schema, value, strlen(value), LY_PREF_JSON, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001791 }
1792 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001793 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1794 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001795 } else {
1796 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001797 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1798 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001799 }
1800 break;
1801 case LYS_ANYDATA:
1802 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001803 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001804 break;
1805 default:
1806 LOGINT(ctx);
1807 ret = LY_EINT;
1808 goto cleanup;
1809 }
1810
1811 if (cur_parent) {
1812 /* connect to the parent */
1813 lyd_insert_node(cur_parent, NULL, node);
1814 } else if (parent) {
1815 /* connect to top-level siblings */
1816 lyd_insert_node(NULL, &parent, node);
1817 }
1818
Michal Vasko35880332020-12-08 13:08:12 +01001819next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001820 /* update remembered nodes */
1821 if (!nparent) {
1822 nparent = node;
1823 }
1824 nnode = node;
1825 }
1826
1827cleanup:
1828 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001829 if (p) {
1830 while (orig_count > LY_ARRAY_COUNT(p)) {
1831 LY_ARRAY_INCREMENT(p);
1832 }
1833 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001834 ly_path_free(ctx, p);
1835 if (!ret) {
1836 /* set out params only on success */
1837 if (new_parent) {
1838 *new_parent = nparent;
1839 }
1840 if (new_node) {
1841 *new_node = nnode;
1842 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001843 } else {
1844 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001845 }
1846 return ret;
1847}
1848
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001849API LY_ERR
1850lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1851 struct lyd_node **node)
1852{
1853 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1854 return lyd_new_path_(parent, ctx, NULL, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1855}
1856
1857API LY_ERR
1858lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1859 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
1860{
1861 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1862 return lyd_new_path_(parent, ctx, NULL, path, value, value_type, options, new_parent, new_node);
1863}
1864
1865API LY_ERR
1866lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1867 uint32_t options, struct lyd_node **node)
1868{
1869 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1870
1871 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, LY_EINVAL);
1872 return lyd_new_path_(parent, ctx, ext, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1873}
1874
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001875LY_ERR
1876lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001877 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
1878 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001879{
1880 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001881 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001882 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001883 struct lyd_value **dflts;
1884 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001885 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001886
1887 assert(first && (parent || sparent || mod));
1888
1889 if (!sparent && parent) {
1890 sparent = parent->schema;
1891 }
1892
Michal Vasko630d9892020-12-08 17:11:08 +01001893 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1894 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1895 getnext_opts |= LYS_GETNEXT_OUTPUT;
1896 }
1897
1898 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001899 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1900 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001901 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1902 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001903 }
1904
1905 switch (iter->nodetype) {
1906 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001907 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1908 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001909 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001910 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001911 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001912 } else if (node) {
1913 /* create any default data in the existing case */
1914 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001915 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_exts, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001916 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001917 }
1918 break;
1919 case LYS_CONTAINER:
1920 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1921 /* create default NP container */
1922 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001923 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001924 lyd_insert_node(parent, first, node);
1925
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001926 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001927 /* remember to resolve when */
1928 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1929 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001930 if (node_exts) {
1931 /* remember to call all the extension's validation callbacks */
1932 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
1933 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001934 if (diff) {
1935 /* add into diff */
1936 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1937 }
1938
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001939 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001940 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_exts, node_types,
Michal Vasko69730152020-10-09 16:30:07 +02001941 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001942 }
1943 break;
1944 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001945 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1946 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001947 /* create default leaf */
1948 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1949 if (ret == LY_EINCOMPLETE) {
1950 if (node_types) {
1951 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001952 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001953 }
1954 } else if (ret) {
1955 return ret;
1956 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001957 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001958 lyd_insert_node(parent, first, node);
1959
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001960 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001961 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001962 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001963 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001964 if (node_exts) {
1965 /* remember to call all the extension's validation callbacks */
1966 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
1967 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001968 if (diff) {
1969 /* add into diff */
1970 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1971 }
1972 }
1973 break;
1974 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001975 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1976 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001977 /* create all default leaf-lists */
1978 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1979 LY_ARRAY_FOR(dflts, u) {
1980 ret = lyd_create_term2(iter, dflts[u], &node);
1981 if (ret == LY_EINCOMPLETE) {
1982 if (node_types) {
1983 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001984 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001985 }
1986 } else if (ret) {
1987 return ret;
1988 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001989 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001990 lyd_insert_node(parent, first, node);
1991
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001992 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001993 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001994 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001995 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001996 if (node_exts) {
1997 /* remember to call all the extension's validation callbacks */
1998 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
1999 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002000 if (diff) {
2001 /* add into diff */
2002 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2003 }
2004 }
2005 }
2006 break;
2007 default:
2008 /* without defaults */
2009 break;
2010 }
2011 }
2012
2013 return LY_SUCCESS;
2014}
2015
2016API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002017lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002018{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002019 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002020 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002021 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002022
2023 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2024 if (diff) {
2025 *diff = NULL;
2026 }
2027
Michal Vasko56daf732020-08-10 10:57:18 +02002028 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002029 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002030 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2031 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002032 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, &node_exts,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01002033 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002034 }
2035
Michal Vasko56daf732020-08-10 10:57:18 +02002036 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002037 }
2038
Michal Vasko3488ada2020-12-03 14:18:19 +01002039 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002040 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002041
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002042cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002043 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002044 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002045 if (ret && diff) {
2046 lyd_free_all(*diff);
2047 *diff = NULL;
2048 }
2049 return ret;
2050}
2051
2052API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002053lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002054{
2055 const struct lys_module *mod;
2056 struct lyd_node *d = NULL;
2057 uint32_t i = 0;
2058 LY_ERR ret = LY_SUCCESS;
2059
2060 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2061 if (diff) {
2062 *diff = NULL;
2063 }
2064 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002065 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002066 }
2067
2068 /* add nodes for each module one-by-one */
2069 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2070 if (!mod->implemented) {
2071 continue;
2072 }
2073
2074 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2075 if (d) {
2076 /* merge into one diff */
2077 lyd_insert_sibling(*diff, d, diff);
2078
2079 d = NULL;
2080 }
2081 }
2082
2083cleanup:
2084 if (ret && diff) {
2085 lyd_free_all(*diff);
2086 *diff = NULL;
2087 }
2088 return ret;
2089}
2090
2091API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002092lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002093{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002094 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002095 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002096 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002097
2098 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2099 if (diff) {
2100 *diff = NULL;
2101 }
2102
2103 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002104 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, &node_exts, NULL, implicit_options, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002105
2106 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002107 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002108
2109 /* process nested nodes */
2110 LY_LIST_FOR(*tree, root) {
2111 /* skip added default nodes */
2112 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2113 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2114
2115 if (d) {
2116 /* merge into one diff */
2117 lyd_insert_sibling(*diff, d, diff);
2118
2119 d = NULL;
2120 }
2121 }
2122 }
2123
2124cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002125 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002126 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002127 if (ret && diff) {
2128 lyd_free_all(*diff);
2129 *diff = NULL;
2130 }
2131 return ret;
2132}
2133
Michal Vasko90932a92020-02-12 14:33:03 +01002134struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002135lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002136{
Michal Vaskob104f112020-07-17 09:54:54 +02002137 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002138 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002139 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002140 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002141
Michal Vaskob104f112020-07-17 09:54:54 +02002142 assert(new_node);
2143
2144 if (!first_sibling || !new_node->schema) {
2145 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002146 return NULL;
2147 }
2148
Michal Vasko93b16062020-12-09 18:14:18 +01002149 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002150 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002151 getnext_opts = LYS_GETNEXT_OUTPUT;
2152 }
2153
Michal Vaskob104f112020-07-17 09:54:54 +02002154 if (first_sibling->parent && first_sibling->parent->children_ht) {
2155 /* find the anchor using hashes */
2156 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002157 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002158 while (schema) {
2159 /* keep trying to find the first existing instance of the closest following schema sibling,
2160 * otherwise return NULL - inserting at the end */
2161 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2162 break;
2163 }
2164
Michal Vasko93b16062020-12-09 18:14:18 +01002165 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002166 }
2167 } else {
2168 /* find the anchor without hashes */
2169 match = (struct lyd_node *)first_sibling;
2170 if (!lysc_data_parent(new_node->schema)) {
2171 /* we are in top-level, skip all the data from preceding modules */
2172 LY_LIST_FOR(match, match) {
2173 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2174 break;
2175 }
2176 }
2177 }
2178
2179 /* get the first schema sibling */
2180 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01002181 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002182
2183 found = 0;
2184 LY_LIST_FOR(match, match) {
2185 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2186 /* we have found an opaque node, which must be at the end, so use it OR
2187 * modules do not match, so we must have traversed all the data from new_node module (if any),
2188 * we have found the first node of the next module, that is what we want */
2189 break;
2190 }
2191
2192 /* skip schema nodes until we find the instantiated one */
2193 while (!found) {
2194 if (new_node->schema == schema) {
2195 /* we have found the schema of the new node, continue search to find the first
2196 * data node with a different schema (after our schema) */
2197 found = 1;
2198 break;
2199 }
2200 if (match->schema == schema) {
2201 /* current node (match) is a data node still before the new node, continue search in data */
2202 break;
2203 }
Michal Vasko93b16062020-12-09 18:14:18 +01002204 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002205 assert(schema);
2206 }
2207
2208 if (found && (match->schema != new_node->schema)) {
2209 /* find the next node after we have found our node schema data instance */
2210 break;
2211 }
2212 }
Michal Vasko90932a92020-02-12 14:33:03 +01002213 }
2214
2215 return match;
2216}
2217
2218/**
2219 * @brief Insert node after a sibling.
2220 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002221 * Handles inserting into NP containers and key-less lists.
2222 *
Michal Vasko90932a92020-02-12 14:33:03 +01002223 * @param[in] sibling Sibling to insert after.
2224 * @param[in] node Node to insert.
2225 */
2226static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002227lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002228{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002229 struct lyd_node_inner *par;
2230
Michal Vasko90932a92020-02-12 14:33:03 +01002231 assert(!node->next && (node->prev == node));
2232
2233 node->next = sibling->next;
2234 node->prev = sibling;
2235 sibling->next = node;
2236 if (node->next) {
2237 /* sibling had a succeeding node */
2238 node->next->prev = node;
2239 } else {
2240 /* sibling was last, find first sibling and change its prev */
2241 if (sibling->parent) {
2242 sibling = sibling->parent->child;
2243 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002244 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002245 }
2246 sibling->prev = node;
2247 }
2248 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002249
Michal Vasko9f96a052020-03-10 09:41:45 +01002250 for (par = node->parent; par; par = par->parent) {
2251 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2252 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002253 par->flags &= ~LYD_DEFAULT;
2254 }
Michal Vaskob104f112020-07-17 09:54:54 +02002255 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002256 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002257 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002258 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002259 }
Michal Vasko90932a92020-02-12 14:33:03 +01002260}
2261
2262/**
2263 * @brief Insert node before a sibling.
2264 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002265 * Handles inserting into NP containers and key-less lists.
2266 *
Michal Vasko90932a92020-02-12 14:33:03 +01002267 * @param[in] sibling Sibling to insert before.
2268 * @param[in] node Node to insert.
2269 */
2270static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002271lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002272{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002273 struct lyd_node_inner *par;
2274
Michal Vasko90932a92020-02-12 14:33:03 +01002275 assert(!node->next && (node->prev == node));
2276
2277 node->next = sibling;
2278 /* covers situation of sibling being first */
2279 node->prev = sibling->prev;
2280 sibling->prev = node;
2281 if (node->prev->next) {
2282 /* sibling had a preceding node */
2283 node->prev->next = node;
2284 } else if (sibling->parent) {
2285 /* sibling was first and we must also change parent child pointer */
2286 sibling->parent->child = node;
2287 }
2288 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002289
Michal Vasko9f96a052020-03-10 09:41:45 +01002290 for (par = node->parent; par; par = par->parent) {
2291 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2292 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002293 par->flags &= ~LYD_DEFAULT;
2294 }
Michal Vaskob104f112020-07-17 09:54:54 +02002295 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002296 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002297 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002298 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002299 }
Michal Vasko90932a92020-02-12 14:33:03 +01002300}
2301
2302/**
Michal Vaskob104f112020-07-17 09:54:54 +02002303 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002304 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002305 * Handles inserting into NP containers and key-less lists.
2306 *
Michal Vasko90932a92020-02-12 14:33:03 +01002307 * @param[in] parent Parent to insert into.
2308 * @param[in] node Node to insert.
2309 */
2310static void
Michal Vaskob104f112020-07-17 09:54:54 +02002311lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002312{
2313 struct lyd_node_inner *par;
2314
Radek Krejcia1c1e542020-09-29 16:06:52 +02002315 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002316 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002317
2318 par = (struct lyd_node_inner *)parent;
2319
Michal Vaskob104f112020-07-17 09:54:54 +02002320 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002321 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002322
Michal Vaskod989ba02020-08-24 10:59:24 +02002323 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002324 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2325 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002326 par->flags &= ~LYD_DEFAULT;
2327 }
Michal Vasko52927e22020-03-16 17:26:14 +01002328 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002329 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002330 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002331 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002332 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002333}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002334
Michal Vasko751cb4d2020-07-14 12:25:28 +02002335/**
2336 * @brief Learn whether a list instance has all the keys.
2337 *
2338 * @param[in] list List instance to check.
2339 * @return non-zero if all the keys were found,
2340 * @return 0 otherwise.
2341 */
2342static int
2343lyd_insert_has_keys(const struct lyd_node *list)
2344{
2345 const struct lyd_node *key;
2346 const struct lysc_node *skey = NULL;
2347
2348 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002349 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002350 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2351 if (!key || (key->schema != skey)) {
2352 /* key missing */
2353 return 0;
2354 }
2355
2356 key = key->next;
2357 }
2358
2359 /* all keys found */
2360 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002361}
2362
2363void
Michal Vaskob104f112020-07-17 09:54:54 +02002364lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002365{
Michal Vaskob104f112020-07-17 09:54:54 +02002366 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002367
Michal Vaskob104f112020-07-17 09:54:54 +02002368 /* inserting list without its keys is not supported */
2369 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002370 assert(!parent || !parent->schema ||
2371 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002372
Michal Vaskob104f112020-07-17 09:54:54 +02002373 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002374 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002375 }
Michal Vasko90932a92020-02-12 14:33:03 +01002376
Michal Vaskob104f112020-07-17 09:54:54 +02002377 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002378 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002379
Michal Vaskob104f112020-07-17 09:54:54 +02002380 /* find the anchor, our next node, so we can insert before it */
2381 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2382 if (anchor) {
2383 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002384 if (!parent && (*first_sibling_p == anchor)) {
2385 /* move first sibling */
2386 *first_sibling_p = node;
2387 }
Michal Vaskob104f112020-07-17 09:54:54 +02002388 } else if (first_sibling) {
2389 lyd_insert_after_node(first_sibling->prev, node);
2390 } else if (parent) {
2391 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002392 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002393 *first_sibling_p = node;
2394 }
2395
2396 /* insert into parent HT */
2397 lyd_insert_hash(node);
2398
2399 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002400 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002401 lyd_hash(parent);
2402
2403 /* now we can insert even the list into its parent HT */
2404 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002405 }
Michal Vasko90932a92020-02-12 14:33:03 +01002406}
2407
Michal Vasko717a4c32020-12-07 09:40:10 +01002408/**
2409 * @brief Check schema place of a node to be inserted.
2410 *
2411 * @param[in] parent Schema node of the parent data node.
2412 * @param[in] sibling Schema node of a sibling data node.
2413 * @param[in] schema Schema node if the data node to be inserted.
2414 * @return LY_SUCCESS on success.
2415 * @return LY_EINVAL if the place is invalid.
2416 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002417static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002418lyd_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 +01002419{
2420 const struct lysc_node *par2;
2421
Michal Vasko62ed12d2020-05-21 10:08:25 +02002422 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002423 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2424 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002425
Michal Vasko717a4c32020-12-07 09:40:10 +01002426 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002427 /* opaque nodes can be inserted wherever */
2428 return LY_SUCCESS;
2429 }
2430
Michal Vasko717a4c32020-12-07 09:40:10 +01002431 if (!parent) {
2432 parent = lysc_data_parent(sibling);
2433 }
2434
Michal Vaskof03ed032020-03-04 13:31:44 +01002435 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002436 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002437
2438 if (parent) {
2439 /* inner node */
2440 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002441 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002442 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002443 return LY_EINVAL;
2444 }
2445 } else {
2446 /* top-level node */
2447 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002448 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002449 return LY_EINVAL;
2450 }
2451 }
2452
2453 return LY_SUCCESS;
2454}
2455
2456API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002457lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002458{
2459 struct lyd_node *iter;
2460
Michal Vasko0ab974d2021-02-24 13:18:26 +01002461 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002462
Michal Vasko717a4c32020-12-07 09:40:10 +01002463 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002464
Michal Vasko0ab974d2021-02-24 13:18:26 +01002465 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002466 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2467 return LY_EINVAL;
2468 }
2469
2470 if (node->parent || node->prev->next) {
2471 lyd_unlink_tree(node);
2472 }
2473
2474 while (node) {
2475 iter = node->next;
2476 lyd_unlink_tree(node);
2477 lyd_insert_node(parent, NULL, node);
2478 node = iter;
2479 }
2480 return LY_SUCCESS;
2481}
2482
2483API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002484lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002485{
2486 struct lyd_node *iter;
2487
Michal Vaskob104f112020-07-17 09:54:54 +02002488 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002489
Michal Vaskob104f112020-07-17 09:54:54 +02002490 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002491 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002492 }
2493
Michal Vasko553d0b52020-12-04 16:27:52 +01002494 if (sibling == node) {
2495 /* we need to keep the connection to siblings so we can insert into them */
2496 sibling = sibling->prev;
2497 }
2498
Michal Vaskob1b5c262020-03-05 14:29:47 +01002499 if (node->parent || node->prev->next) {
2500 lyd_unlink_tree(node);
2501 }
2502
2503 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002504 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002505 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002506 return LY_EINVAL;
2507 }
2508
Michal Vaskob1b5c262020-03-05 14:29:47 +01002509 iter = node->next;
2510 lyd_unlink_tree(node);
2511 lyd_insert_node(NULL, &sibling, node);
2512 node = iter;
2513 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002514
Michal Vaskob104f112020-07-17 09:54:54 +02002515 if (first) {
2516 /* find the first sibling */
2517 *first = sibling;
2518 while ((*first)->prev->next) {
2519 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002520 }
2521 }
2522
2523 return LY_SUCCESS;
2524}
2525
Michal Vaskob1b5c262020-03-05 14:29:47 +01002526API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002527lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2528{
Michal Vaskof03ed032020-03-04 13:31:44 +01002529 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2530
Michal Vasko717a4c32020-12-07 09:40:10 +01002531 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002532
Michal Vaskob104f112020-07-17 09:54:54 +02002533 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002534 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002535 return LY_EINVAL;
2536 }
2537
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002538 lyd_unlink_tree(node);
2539 lyd_insert_before_node(sibling, node);
2540 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002541
Michal Vaskof03ed032020-03-04 13:31:44 +01002542 return LY_SUCCESS;
2543}
2544
2545API LY_ERR
2546lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2547{
Michal Vaskof03ed032020-03-04 13:31:44 +01002548 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2549
Michal Vasko717a4c32020-12-07 09:40:10 +01002550 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002551
Michal Vaskob104f112020-07-17 09:54:54 +02002552 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002553 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002554 return LY_EINVAL;
2555 }
2556
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002557 lyd_unlink_tree(node);
2558 lyd_insert_after_node(sibling, node);
2559 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002560
Michal Vaskof03ed032020-03-04 13:31:44 +01002561 return LY_SUCCESS;
2562}
2563
2564API void
2565lyd_unlink_tree(struct lyd_node *node)
2566{
2567 struct lyd_node *iter;
2568
2569 if (!node) {
2570 return;
2571 }
2572
Michal Vaskob104f112020-07-17 09:54:54 +02002573 /* update hashes while still linked into the tree */
2574 lyd_unlink_hash(node);
2575
Michal Vaskof03ed032020-03-04 13:31:44 +01002576 /* unlink from siblings */
2577 if (node->prev->next) {
2578 node->prev->next = node->next;
2579 }
2580 if (node->next) {
2581 node->next->prev = node->prev;
2582 } else {
2583 /* unlinking the last node */
2584 if (node->parent) {
2585 iter = node->parent->child;
2586 } else {
2587 iter = node->prev;
2588 while (iter->prev != node) {
2589 iter = iter->prev;
2590 }
2591 }
2592 /* update the "last" pointer from the first node */
2593 iter->prev = node->prev;
2594 }
2595
2596 /* unlink from parent */
2597 if (node->parent) {
2598 if (node->parent->child == node) {
2599 /* the node is the first child */
2600 node->parent->child = node->next;
2601 }
2602
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002603 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002604 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2605 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002606 LY_LIST_FOR(node->parent->child, iter) {
2607 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2608 break;
2609 }
2610 }
2611 if (!iter) {
2612 node->parent->flags |= LYD_DEFAULT;
2613 }
2614 }
2615
Michal Vaskof03ed032020-03-04 13:31:44 +01002616 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002617 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002618 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002619 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002620 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002621 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002622 }
2623 }
2624
2625 node->parent = NULL;
2626 }
2627
2628 node->next = NULL;
2629 node->prev = node;
2630}
2631
Michal Vaskoa5da3292020-08-12 13:10:50 +02002632void
Michal Vasko871a0252020-11-11 18:35:24 +01002633lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002634{
2635 struct lyd_meta *last, *iter;
2636
2637 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002638
2639 if (!meta) {
2640 return;
2641 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002642
2643 for (iter = meta; iter; iter = iter->next) {
2644 iter->parent = parent;
2645 }
2646
2647 /* insert as the last attribute */
2648 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002649 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002650 last->next = meta;
2651 } else {
2652 parent->meta = meta;
2653 }
2654
2655 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002656 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002657 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002658 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002659 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002660}
2661
2662LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002663lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002664 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
Michal Vasko871a0252020-11-11 18:35:24 +01002665 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002666{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002667 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002668 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002669 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002670 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002671
Michal Vasko9f96a052020-03-10 09:41:45 +01002672 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002673
Radek Krejciddace2c2021-01-08 11:30:56 +01002674 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002675
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002676 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002677 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002678 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002679 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002680 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002681 break;
2682 }
2683 }
2684 if (!ant) {
2685 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002686 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002687 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002688 ret = LY_EINVAL;
2689 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002690 }
2691
Michal Vasko9f96a052020-03-10 09:41:45 +01002692 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002693 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002694 mt->parent = parent;
2695 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002696 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2697 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002698 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2699 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2700 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002701
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002702 /* insert as the last attribute */
2703 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002704 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002705 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002706 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002707 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002708 }
2709
Michal Vasko9f96a052020-03-10 09:41:45 +01002710 if (meta) {
2711 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002712 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002713
2714cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002715 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002716 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002717}
2718
Michal Vaskoa5da3292020-08-12 13:10:50 +02002719void
2720lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2721{
2722 struct lyd_attr *last, *iter;
2723 struct lyd_node_opaq *opaq;
2724
2725 assert(parent && !parent->schema);
2726
2727 if (!attr) {
2728 return;
2729 }
2730
2731 opaq = (struct lyd_node_opaq *)parent;
2732 for (iter = attr; iter; iter = iter->next) {
2733 iter->parent = opaq;
2734 }
2735
2736 /* insert as the last attribute */
2737 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002738 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002739 last->next = attr;
2740 } else {
2741 opaq->attr = attr;
2742 }
2743}
2744
Michal Vasko52927e22020-03-16 17:26:14 +01002745LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002746lyd_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 +01002747 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2748 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints)
Michal Vasko52927e22020-03-16 17:26:14 +01002749{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002750 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002751 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002752
2753 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002754 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002755
2756 if (!value_len) {
2757 value = "";
2758 }
2759
2760 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002761 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002762
Michal Vaskoad92b672020-11-12 13:11:31 +01002763 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002764 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002765 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002766 }
2767 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002768 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002769 }
2770
Michal Vasko52927e22020-03-16 17:26:14 +01002771 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002772 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2773 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002774 *dynamic = 0;
2775 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002776 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002777 }
Michal Vasko501af032020-11-11 20:27:44 +01002778 at->format = format;
2779 at->val_prefix_data = val_prefix_data;
2780 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002781
2782 /* insert as the last attribute */
2783 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002784 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002785 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002786 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002787 last->next = at;
2788 }
2789
Radek Krejci011e4aa2020-09-04 15:22:31 +02002790finish:
2791 if (ret) {
2792 lyd_free_attr_single(ctx, at);
2793 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002794 *attr = at;
2795 }
2796 return LY_SUCCESS;
2797}
2798
Radek Krejci084289f2019-07-09 17:35:30 +02002799API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002800lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002801{
Michal Vasko004d3152020-06-11 19:59:22 +02002802 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002803
Michal Vasko004d3152020-06-11 19:59:22 +02002804 if (ly_path_eval(path, tree, &target)) {
2805 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002806 }
2807
Michal Vasko004d3152020-06-11 19:59:22 +02002808 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002809}
2810
aPiecek2f63f952021-03-30 12:22:18 +02002811/**
2812 * @brief Check the equality of the two schemas from different contexts.
2813 *
2814 * @param schema1 of first node.
2815 * @param schema2 of second node.
2816 * @return 1 if the schemas are equal otherwise 0.
2817 */
2818static ly_bool
2819lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
2820{
2821 if (!schema1 && !schema2) {
2822 return 1;
2823 } else if (!schema1 || !schema2) {
2824 return 0;
2825 }
2826
2827 assert(schema1->module->ctx != schema2->module->ctx);
2828
2829 if (schema1->nodetype != schema2->nodetype) {
2830 return 0;
2831 }
2832
2833 if (strcmp(schema1->name, schema2->name)) {
2834 return 0;
2835 }
2836
2837 if (strcmp(schema1->module->name, schema2->module->name)) {
2838 return 0;
2839 }
2840
2841 if (schema1->module->revision || schema2->module->revision) {
2842 if (!schema1->module->revision || !schema2->module->revision) {
2843 return 0;
2844 }
2845 if (strcmp(schema1->module->revision, schema2->module->revision)) {
2846 return 0;
2847 }
2848 }
2849
2850 return 1;
2851}
2852
2853/**
2854 * @brief Check the equality of the schemas for all parent nodes.
2855 *
2856 * Both nodes must be from different contexts.
2857 *
2858 * @param node1 Data of first node.
2859 * @param node2 Data of second node.
2860 * @return 1 if the all related parental schemas are equal otherwise 0.
2861 */
2862static ly_bool
2863lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
2864{
2865 const struct lysc_node *parent1, *parent2;
2866
2867 assert(node1 && node2);
2868
2869 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
2870 parent1 && parent2;
2871 parent1 = parent1->parent, parent2 = parent2->parent) {
2872 if (!lyd_compare_schema_equal(parent1, parent2)) {
2873 return 0;
2874 }
2875 }
2876
2877 if (parent1 || parent2) {
2878 return 0;
2879 }
2880
2881 return 1;
2882}
2883
2884/**
2885 * @brief Internal implementation of @ref lyd_compare_single.
2886 * @copydoc lyd_compare_single
2887 * @param[in] parental_schemas_checked Flag used for optimization.
2888 * When this function is called for the first time, the flag must be set to 0.
2889 * The @ref lyd_compare_schema_parents_equal should be called only once during
2890 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
2891 */
2892static LY_ERR
2893lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
2894 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002895{
2896 const struct lyd_node *iter1, *iter2;
2897 struct lyd_node_term *term1, *term2;
2898 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002899 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002900 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002901
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002902 if (!node1 || !node2) {
2903 if (node1 == node2) {
2904 return LY_SUCCESS;
2905 } else {
2906 return LY_ENOT;
2907 }
2908 }
2909
aPiecek2f63f952021-03-30 12:22:18 +02002910 if (LYD_CTX(node1) == LYD_CTX(node2)) {
2911 /* same contexts */
2912 if (node1->schema != node2->schema) {
2913 return LY_ENOT;
2914 }
2915 } else {
2916 /* different contexts */
2917 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
2918 return LY_ENOT;
2919 }
2920 if (!parental_schemas_checked) {
2921 if (!lyd_compare_schema_parents_equal(node1, node2)) {
2922 return LY_ENOT;
2923 }
2924 parental_schemas_checked = 1;
2925 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002926 }
2927
2928 if (node1->hash != node2->hash) {
2929 return LY_ENOT;
2930 }
aPiecek2f63f952021-03-30 12:22:18 +02002931 /* 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 +02002932
Michal Vasko52927e22020-03-16 17:26:14 +01002933 if (!node1->schema) {
2934 opaq1 = (struct lyd_node_opaq *)node1;
2935 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02002936 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
2937 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002938 return LY_ENOT;
2939 }
Michal Vasko52927e22020-03-16 17:26:14 +01002940 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002941 case LY_PREF_XML:
aPiecek2f63f952021-03-30 12:22:18 +02002942 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002943 return LY_ENOT;
2944 }
2945 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002946 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002947 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2948 if (strcmp(opaq1->value, opaq2->value)) {
2949 return LY_ENOT;
2950 }
2951 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002952 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002953 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002954 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002955 return LY_EINT;
2956 }
2957 if (options & LYD_COMPARE_FULL_RECURSION) {
2958 iter1 = opaq1->child;
2959 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002960 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002961 }
2962 return LY_SUCCESS;
2963 } else {
2964 switch (node1->schema->nodetype) {
2965 case LYS_LEAF:
2966 case LYS_LEAFLIST:
2967 if (options & LYD_COMPARE_DEFAULTS) {
2968 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2969 return LY_ENOT;
2970 }
2971 }
2972
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002973 term1 = (struct lyd_node_term *)node1;
2974 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02002975
2976 /* same contexts */
2977 if (LYD_CTX(node1) == LYD_CTX(node2)) {
2978 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
2979 }
2980
2981 /* different contexts */
2982 if (strcmp(LYD_CANON_VALUE(node1), LYD_CANON_VALUE(node2))) {
2983 return LY_ENOT;
2984 }
2985 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002986 case LYS_CONTAINER:
2987 if (options & LYD_COMPARE_DEFAULTS) {
2988 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2989 return LY_ENOT;
2990 }
2991 }
2992 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002993 iter1 = lyd_child(node1);
2994 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002995 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002996 }
2997 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002998 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002999 case LYS_ACTION:
3000 if (options & LYD_COMPARE_FULL_RECURSION) {
3001 /* TODO action/RPC
3002 goto all_children_compare;
3003 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003004 }
3005 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003006 case LYS_NOTIF:
3007 if (options & LYD_COMPARE_FULL_RECURSION) {
3008 /* TODO Notification
3009 goto all_children_compare;
3010 */
3011 }
3012 return LY_SUCCESS;
3013 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003014 iter1 = lyd_child(node1);
3015 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003016
3017 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3018 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003019 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003020 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003021 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003022 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003023 return LY_ENOT;
3024 }
3025 iter1 = iter1->next;
3026 iter2 = iter2->next;
3027 }
3028 } else {
3029 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3030
Radek Krejci0f969882020-08-21 16:56:47 +02003031all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003032 if (!iter1 && !iter2) {
3033 /* no children, nothing to compare */
3034 return LY_SUCCESS;
3035 }
3036
Michal Vaskod989ba02020-08-24 10:59:24 +02003037 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003038 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003039 return LY_ENOT;
3040 }
3041 }
3042 if (iter1 || iter2) {
3043 return LY_ENOT;
3044 }
3045 }
3046 return LY_SUCCESS;
3047 case LYS_ANYXML:
3048 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003049 any1 = (struct lyd_node_any *)node1;
3050 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003051
3052 if (any1->value_type != any2->value_type) {
3053 return LY_ENOT;
3054 }
3055 switch (any1->value_type) {
3056 case LYD_ANYDATA_DATATREE:
3057 iter1 = any1->value.tree;
3058 iter2 = any2->value.tree;
3059 goto all_children_compare;
3060 case LYD_ANYDATA_STRING:
3061 case LYD_ANYDATA_XML:
3062 case LYD_ANYDATA_JSON:
3063 len1 = strlen(any1->value.str);
3064 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003065 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003066 return LY_ENOT;
3067 }
3068 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003069 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003070 len1 = lyd_lyb_data_length(any1->value.mem);
3071 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003072 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003073 return LY_ENOT;
3074 }
3075 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003076 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003077 }
3078 }
3079
Michal Vaskob7be7a82020-08-20 09:09:04 +02003080 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003081 return LY_EINT;
3082}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003083
Michal Vasko21725742020-06-29 11:49:25 +02003084API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003085lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3086{
3087 return lyd_compare_single_(node1, node2, options, 0);
3088}
3089
3090API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003091lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003092{
Michal Vaskod989ba02020-08-24 10:59:24 +02003093 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003094 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3095 }
3096
Michal Vasko11a81432020-07-28 16:31:29 +02003097 if (node1 == node2) {
3098 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003099 }
Michal Vasko11a81432020-07-28 16:31:29 +02003100 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003101}
3102
3103API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003104lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3105{
3106 if (!meta1 || !meta2) {
3107 if (meta1 == meta2) {
3108 return LY_SUCCESS;
3109 } else {
3110 return LY_ENOT;
3111 }
3112 }
3113
Michal Vaskoa8083062020-11-06 17:22:10 +01003114 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003115 return LY_ENOT;
3116 }
3117
Michal Vasko21725742020-06-29 11:49:25 +02003118 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3119}
3120
Radek Krejci22ebdba2019-07-25 13:59:43 +02003121/**
Michal Vasko52927e22020-03-16 17:26:14 +01003122 * @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 +02003123 *
3124 * 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 +02003125 *
3126 * @param[in] node Original node to duplicate
3127 * @param[in] parent Parent to insert into, NULL for top-level sibling.
3128 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3129 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3130 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3131 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003132 */
Michal Vasko52927e22020-03-16 17:26:14 +01003133static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003134lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02003135 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003136{
Michal Vasko52927e22020-03-16 17:26:14 +01003137 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003138 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003139 struct lyd_meta *meta;
3140 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003141
Michal Vasko52927e22020-03-16 17:26:14 +01003142 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003143
Michal Vasko52927e22020-03-16 17:26:14 +01003144 if (!node->schema) {
3145 dup = calloc(1, sizeof(struct lyd_node_opaq));
3146 } else {
3147 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003148 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003149 case LYS_ACTION:
3150 case LYS_NOTIF:
3151 case LYS_CONTAINER:
3152 case LYS_LIST:
3153 dup = calloc(1, sizeof(struct lyd_node_inner));
3154 break;
3155 case LYS_LEAF:
3156 case LYS_LEAFLIST:
3157 dup = calloc(1, sizeof(struct lyd_node_term));
3158 break;
3159 case LYS_ANYDATA:
3160 case LYS_ANYXML:
3161 dup = calloc(1, sizeof(struct lyd_node_any));
3162 break;
3163 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003164 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003165 ret = LY_EINT;
3166 goto error;
3167 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003168 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003169 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003170
Michal Vaskof6df0a02020-06-16 13:08:34 +02003171 if (options & LYD_DUP_WITH_FLAGS) {
3172 dup->flags = node->flags;
3173 } else {
3174 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3175 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003176 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003177 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003178
Michal Vasko25a32822020-07-09 15:48:22 +02003179 /* duplicate metadata */
3180 if (!(options & LYD_DUP_NO_META)) {
3181 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003182 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02003183 }
3184 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003185
3186 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003187 if (!dup->schema) {
3188 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3189 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3190 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003191
3192 if (options & LYD_DUP_RECURSIVE) {
3193 /* duplicate all the children */
3194 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003195 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003196 }
3197 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003198 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003199 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01003200 if (orig->name.prefix) {
3201 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003202 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003203 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.module_ns, 0, &opaq->name.module_ns), error);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003204 if (orig->val_prefix_data) {
3205 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3206 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003207 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02003208 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003209 opaq->ctx = orig->ctx;
3210 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3211 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3212 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3213
3214 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003215 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003216 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003217 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3218 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3219 struct lyd_node *child;
3220
3221 if (options & LYD_DUP_RECURSIVE) {
3222 /* duplicate all the children */
3223 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003224 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003225 }
Michal Vasko69730152020-10-09 16:30:07 +02003226 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003227 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003228 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003229 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003230 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003231 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003232 if (!child) {
3233 /* possibly not keys are present in filtered tree */
3234 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003235 } else if (child->schema != key) {
3236 /* possibly not all keys are present in filtered tree,
3237 * but there can be also some non-key nodes */
3238 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003239 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003240 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003241 child = child->next;
3242 }
3243 }
3244 lyd_hash(dup);
3245 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003246 dup->hash = node->hash;
3247 any = (struct lyd_node_any *)node;
3248 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003249 }
3250
Michal Vasko52927e22020-03-16 17:26:14 +01003251 /* insert */
3252 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01003253
3254 if (dup_p) {
3255 *dup_p = dup;
3256 }
3257 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003258
3259error:
Michal Vasko52927e22020-03-16 17:26:14 +01003260 lyd_free_tree(dup);
3261 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003262}
3263
Michal Vasko3a41dff2020-07-15 14:30:28 +02003264static LY_ERR
3265lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
Radek Krejci0f969882020-08-21 16:56:47 +02003266 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003267{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003268 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003269 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003270
3271 *dup_parent = NULL;
3272 *local_parent = NULL;
3273
3274 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3275 if (parent && (parent->schema == orig_parent->schema)) {
3276 /* stop creating parents, connect what we have into the provided parent */
3277 iter = parent;
3278 repeat = 0;
3279 } else {
3280 iter = NULL;
3281 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02003282 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003283 }
3284 if (!*local_parent) {
3285 *local_parent = (struct lyd_node_inner *)iter;
3286 }
3287 if (iter->child) {
3288 /* 1) list - add after keys
3289 * 2) provided parent with some children */
3290 iter->child->prev->next = *dup_parent;
3291 if (*dup_parent) {
3292 (*dup_parent)->prev = iter->child->prev;
3293 iter->child->prev = *dup_parent;
3294 }
3295 } else {
3296 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3297 }
3298 if (*dup_parent) {
3299 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3300 }
3301 *dup_parent = (struct lyd_node *)iter;
3302 }
3303
3304 if (repeat && parent) {
3305 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003306 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003307 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003308 return LY_EINVAL;
3309 }
3310
3311 return LY_SUCCESS;
3312}
3313
3314static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003315lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003316{
3317 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003318 const struct lyd_node *orig; /* original node to be duplicated */
3319 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003320 struct lyd_node *top = NULL; /* the most higher created node */
3321 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003322
Michal Vasko3a41dff2020-07-15 14:30:28 +02003323 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003324
3325 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003326 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003327 } else {
3328 local_parent = parent;
3329 }
3330
Radek Krejci22ebdba2019-07-25 13:59:43 +02003331 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003332 if (lysc_is_key(orig->schema)) {
3333 if (local_parent) {
3334 /* the key must already exist in the parent */
3335 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3336 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3337 } else {
3338 assert(!(options & LYD_DUP_WITH_PARENTS));
3339 /* duplicating a single key, okay, I suppose... */
3340 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3341 LY_CHECK_GOTO(rc, error);
3342 }
3343 } else {
3344 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003345 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003346 LY_CHECK_GOTO(rc, error);
3347 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003348 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003349 break;
3350 }
3351 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003352
3353 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003354 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003355 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003356 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003357 }
3358 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003359
3360 if (dup) {
3361 *dup = first;
3362 }
3363 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003364
3365error:
3366 if (top) {
3367 lyd_free_tree(top);
3368 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003369 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003370 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003371 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003372}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003373
Michal Vasko3a41dff2020-07-15 14:30:28 +02003374API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003375lyd_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 +02003376{
3377 return lyd_dup(node, parent, options, 1, dup);
3378}
3379
3380API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003381lyd_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 +02003382{
3383 return lyd_dup(node, parent, options, 0, dup);
3384}
3385
3386API LY_ERR
3387lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003388{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003389 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003390 struct lyd_meta *mt, *last;
3391
Michal Vasko3a41dff2020-07-15 14:30:28 +02003392 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003393
3394 /* create a copy */
3395 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003396 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003397 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003398 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003399 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3400 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003401
3402 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003403 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003404 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003405 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003406 last->next = mt;
3407 } else {
3408 node->meta = mt;
3409 }
3410
Radek Krejci011e4aa2020-09-04 15:22:31 +02003411finish:
3412 if (ret) {
3413 lyd_free_meta_single(mt);
3414 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003415 *dup = mt;
3416 }
3417 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003418}
3419
Michal Vasko4490d312020-06-16 13:08:55 +02003420/**
3421 * @brief Merge a source sibling into target siblings.
3422 *
3423 * @param[in,out] first_trg First target sibling, is updated if top-level.
3424 * @param[in] parent_trg Target parent.
3425 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3426 * @param[in] options Merge options.
3427 * @return LY_ERR value.
3428 */
3429static LY_ERR
3430lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Radek Krejci1deb5be2020-08-26 16:43:36 +02003431 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003432{
3433 LY_ERR ret;
3434 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003435 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003436 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003437
3438 sibling_src = *sibling_src_p;
3439 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3440 /* try to find the exact instance */
3441 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3442 } else {
3443 /* try to simply find the node, there cannot be more instances */
3444 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3445 }
3446
3447 if (!ret) {
3448 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003449 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003450 /* since they are different, they cannot both be default */
3451 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3452
Michal Vasko3a41dff2020-07-15 14:30:28 +02003453 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3454 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003455 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003456 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3457 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003458 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003459
3460 /* copy flags and add LYD_NEW */
3461 match_trg->flags = sibling_src->flags | LYD_NEW;
3462 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003463 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003464 /* update value */
3465 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003466 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003467
3468 /* copy flags and add LYD_NEW */
3469 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003470 } else {
3471 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003472 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003473 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, options));
Michal Vasko4490d312020-06-16 13:08:55 +02003474 }
3475 }
3476 } else {
3477 /* node not found, merge it */
3478 if (options & LYD_MERGE_DESTRUCT) {
3479 dup_src = (struct lyd_node *)sibling_src;
3480 lyd_unlink_tree(dup_src);
3481 /* spend it */
3482 *sibling_src_p = NULL;
3483 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003484 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 +02003485 }
3486
3487 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003488 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003489 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003490 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003491 }
3492
3493 lyd_insert_node(parent_trg, first_trg, dup_src);
3494 }
3495
3496 return LY_SUCCESS;
3497}
3498
Michal Vasko3a41dff2020-07-15 14:30:28 +02003499static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003500lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003501{
3502 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003503 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003504
3505 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3506
3507 if (!source) {
3508 /* nothing to merge */
3509 return LY_SUCCESS;
3510 }
3511
Michal Vasko831422b2020-11-24 11:20:51 +01003512 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003513 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003514 return LY_EINVAL;
3515 }
3516
3517 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003518 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003519 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3520 if (first && !sibling_src) {
3521 /* source was spent (unlinked), move to the next node */
3522 source = tmp;
3523 }
3524
Michal Vasko3a41dff2020-07-15 14:30:28 +02003525 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003526 break;
3527 }
3528 }
3529
3530 if (options & LYD_MERGE_DESTRUCT) {
3531 /* free any leftover source data that were not merged */
3532 lyd_free_siblings((struct lyd_node *)source);
3533 }
3534
3535 return LY_SUCCESS;
3536}
3537
Michal Vasko3a41dff2020-07-15 14:30:28 +02003538API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003539lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003540{
3541 return lyd_merge(target, source, options, 1);
3542}
3543
3544API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003545lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003546{
3547 return lyd_merge(target, source, options, 0);
3548}
3549
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003550static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003551lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003552{
Michal Vasko14654712020-02-06 08:35:21 +01003553 /* ending \0 */
3554 ++reqlen;
3555
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003556 if (reqlen > *buflen) {
3557 if (is_static) {
3558 return LY_EINCOMPLETE;
3559 }
3560
3561 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3562 if (!*buffer) {
3563 return LY_EMEM;
3564 }
3565
3566 *buflen = reqlen;
3567 }
3568
3569 return LY_SUCCESS;
3570}
3571
Michal Vaskod59035b2020-07-08 12:00:06 +02003572LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003573lyd_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 +02003574{
3575 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003576 size_t len;
3577 const char *val;
3578 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003579
Radek Krejcia1c1e542020-09-29 16:06:52 +02003580 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003581 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003582 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003583 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003584
3585 quot = '\'';
3586 if (strchr(val, '\'')) {
3587 quot = '"';
3588 }
3589 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003590 }
3591
3592 return LY_SUCCESS;
3593}
3594
3595/**
3596 * @brief Append leaf-list value predicate to path.
3597 *
3598 * @param[in] node Node to print.
3599 * @param[in,out] buffer Buffer to print to.
3600 * @param[in,out] buflen Current buffer length.
3601 * @param[in,out] bufused Current number of characters used in @p buffer.
3602 * @param[in] is_static Whether buffer is static or can be reallocated.
3603 * @return LY_ERR
3604 */
3605static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003606lyd_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 +02003607{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003608 size_t len;
3609 const char *val;
3610 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003611
Michal Vaskob7be7a82020-08-20 09:09:04 +02003612 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003613 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003614 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003615
3616 quot = '\'';
3617 if (strchr(val, '\'')) {
3618 quot = '"';
3619 }
3620 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3621
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003622 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003623}
3624
3625/**
3626 * @brief Append node position (relative to its other instances) predicate to path.
3627 *
3628 * @param[in] node Node to print.
3629 * @param[in,out] buffer Buffer to print to.
3630 * @param[in,out] buflen Current buffer length.
3631 * @param[in,out] bufused Current number of characters used in @p buffer.
3632 * @param[in] is_static Whether buffer is static or can be reallocated.
3633 * @return LY_ERR
3634 */
3635static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003636lyd_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 +02003637{
3638 const struct lyd_node *first, *iter;
3639 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003640 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003641 char *val = NULL;
3642 LY_ERR rc;
3643
3644 if (node->parent) {
3645 first = node->parent->child;
3646 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003647 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003648 }
3649 pos = 1;
3650 for (iter = first; iter != node; iter = iter->next) {
3651 if (iter->schema == node->schema) {
3652 ++pos;
3653 }
3654 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003655 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003656 return LY_EMEM;
3657 }
3658
3659 len = 1 + strlen(val) + 1;
3660 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3661 if (rc != LY_SUCCESS) {
3662 goto cleanup;
3663 }
3664
3665 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3666
3667cleanup:
3668 free(val);
3669 return rc;
3670}
3671
3672API char *
3673lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3674{
Radek Krejci857189e2020-09-01 13:26:36 +02003675 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003676 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003677 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003678 const struct lyd_node *iter;
3679 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003680 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003681
3682 LY_CHECK_ARG_RET(NULL, node, NULL);
3683 if (buffer) {
3684 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3685 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003686 } else {
3687 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003688 }
3689
3690 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003691 case LYD_PATH_STD:
3692 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003693 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003694 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003695 ++depth;
3696 }
3697
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003698 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003699 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003700 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003701 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003702iter_print:
3703 /* print prefix and name */
3704 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003705 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003706 mod = iter->schema->module;
3707 }
3708
3709 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003710 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3711 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003712 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3713 if (rc != LY_SUCCESS) {
3714 break;
3715 }
3716
3717 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003718 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003719 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003720
Michal Vasko790b2bc2020-08-03 13:35:06 +02003721 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003722 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003723 switch (iter->schema->nodetype) {
3724 case LYS_LIST:
3725 if (iter->schema->flags & LYS_KEYLESS) {
3726 /* print its position */
3727 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3728 } else {
3729 /* print all list keys in predicates */
3730 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3731 }
3732 break;
3733 case LYS_LEAFLIST:
3734 if (iter->schema->flags & LYS_CONFIG_W) {
3735 /* print leaf-list value */
3736 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3737 } else {
3738 /* print its position */
3739 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3740 }
3741 break;
3742 default:
3743 /* nothing to print more */
3744 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003745 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003746 }
3747 if (rc != LY_SUCCESS) {
3748 break;
3749 }
3750
Michal Vasko14654712020-02-06 08:35:21 +01003751 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003752 }
3753 break;
3754 }
3755
3756 return buffer;
3757}
Michal Vaskoe444f752020-02-10 12:20:06 +01003758
Michal Vasko25a32822020-07-09 15:48:22 +02003759API struct lyd_meta *
3760lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3761{
3762 struct lyd_meta *ret = NULL;
3763 const struct ly_ctx *ctx;
3764 const char *prefix, *tmp;
3765 char *str;
3766 size_t pref_len, name_len;
3767
3768 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3769
3770 if (!first) {
3771 return NULL;
3772 }
3773
3774 ctx = first->annotation->module->ctx;
3775
3776 /* parse the name */
3777 tmp = name;
3778 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3779 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3780 return NULL;
3781 }
3782
3783 /* find the module */
3784 if (prefix) {
3785 str = strndup(prefix, pref_len);
3786 module = ly_ctx_get_module_latest(ctx, str);
3787 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003788 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 +02003789 }
3790
3791 /* find the metadata */
3792 LY_LIST_FOR(first, first) {
3793 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3794 ret = (struct lyd_meta *)first;
3795 break;
3796 }
3797 }
3798
3799 return ret;
3800}
3801
Michal Vasko9b368d32020-02-14 13:53:31 +01003802API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003803lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3804{
3805 struct lyd_node **match_p;
3806 struct lyd_node_inner *parent;
3807
Michal Vaskof03ed032020-03-04 13:31:44 +01003808 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003809
Michal Vasko6da1e952020-12-09 18:14:38 +01003810 if (!siblings || (siblings->schema && target->schema &&
3811 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003812 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003813 if (match) {
3814 *match = NULL;
3815 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003816 return LY_ENOTFOUND;
3817 }
3818
3819 /* find first sibling */
3820 if (siblings->parent) {
3821 siblings = siblings->parent->child;
3822 } else {
3823 while (siblings->prev->next) {
3824 siblings = siblings->prev;
3825 }
3826 }
3827
Michal Vasko9e685082021-01-29 14:49:09 +01003828 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003829 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003830 assert(target->hash);
3831
3832 /* find by hash */
3833 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003834 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003835 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003836 siblings = *match_p;
3837 } else {
3838 siblings = NULL;
3839 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003840 } else {
3841 /* not found */
3842 siblings = NULL;
3843 }
3844 } else {
3845 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003846 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003847 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003848 break;
3849 }
3850 }
3851 }
3852
3853 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003854 if (match) {
3855 *match = NULL;
3856 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003857 return LY_ENOTFOUND;
3858 }
3859
Michal Vasko9b368d32020-02-14 13:53:31 +01003860 if (match) {
3861 *match = (struct lyd_node *)siblings;
3862 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003863 return LY_SUCCESS;
3864}
3865
Radek Krejci857189e2020-09-01 13:26:36 +02003866/**
3867 * @brief Comparison callback to match schema node with a schema of a data node.
3868 *
3869 * @param[in] val1_p Pointer to the schema node
3870 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003871 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003872 */
3873static ly_bool
3874lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko90932a92020-02-12 14:33:03 +01003875{
3876 struct lysc_node *val1;
3877 struct lyd_node *val2;
3878
3879 val1 = *((struct lysc_node **)val1_p);
3880 val2 = *((struct lyd_node **)val2_p);
3881
Michal Vasko90932a92020-02-12 14:33:03 +01003882 if (val1 == val2->schema) {
3883 /* schema match is enough */
3884 return 1;
3885 } else {
3886 return 0;
3887 }
3888}
3889
Michal Vasko92239c72020-11-18 18:17:25 +01003890/**
3891 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3892 * Uses hashes - should be used whenever possible for best performance.
3893 *
3894 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3895 * @param[in] schema Target data node schema to find.
3896 * @param[out] match Can be NULL, otherwise the found data node.
3897 * @return LY_SUCCESS on success, @p match set.
3898 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3899 * @return LY_ERR value if another error occurred.
3900 */
Michal Vasko90932a92020-02-12 14:33:03 +01003901static LY_ERR
3902lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3903{
3904 struct lyd_node **match_p;
3905 struct lyd_node_inner *parent;
3906 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003907 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003908
Michal Vaskob104f112020-07-17 09:54:54 +02003909 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003910
Michal Vasko9e685082021-01-29 14:49:09 +01003911 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003912 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003913 /* calculate our hash */
3914 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3915 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3916 hash = dict_hash_multi(hash, NULL, 0);
3917
3918 /* use special hash table function */
3919 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3920
3921 /* find by hash */
3922 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3923 siblings = *match_p;
Christian Hoppsd1f6c012021-04-06 07:27:45 -04003924 if (siblings->prev != siblings) {
3925 const struct lyd_node *first;
3926 for (first = parent->child;
3927 siblings != first && siblings->prev->schema == schema;
3928 siblings = siblings->prev)
3929 ;
3930 }
Michal Vasko90932a92020-02-12 14:33:03 +01003931 } else {
3932 /* not found */
3933 siblings = NULL;
3934 }
3935
3936 /* set the original hash table compare function back */
3937 lyht_set_cb(parent->children_ht, ht_cb);
3938 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003939 /* find first sibling */
3940 if (siblings->parent) {
3941 siblings = siblings->parent->child;
3942 } else {
3943 while (siblings->prev->next) {
3944 siblings = siblings->prev;
3945 }
3946 }
3947
3948 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003949 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003950 if (siblings->schema == schema) {
3951 /* schema match is enough */
3952 break;
3953 }
3954 }
3955 }
3956
3957 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003958 if (match) {
3959 *match = NULL;
3960 }
Michal Vasko90932a92020-02-12 14:33:03 +01003961 return LY_ENOTFOUND;
3962 }
3963
Michal Vasko9b368d32020-02-14 13:53:31 +01003964 if (match) {
3965 *match = (struct lyd_node *)siblings;
3966 }
Michal Vasko90932a92020-02-12 14:33:03 +01003967 return LY_SUCCESS;
3968}
3969
Michal Vaskoe444f752020-02-10 12:20:06 +01003970API LY_ERR
3971lyd_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 +02003972 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003973{
3974 LY_ERR rc;
3975 struct lyd_node *target = NULL;
3976
Michal Vasko4c583e82020-07-17 12:16:14 +02003977 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003978
Michal Vasko08fd6132020-11-18 18:17:45 +01003979 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003980 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003981 if (match) {
3982 *match = NULL;
3983 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003984 return LY_ENOTFOUND;
3985 }
3986
Michal Vaskof03ed032020-03-04 13:31:44 +01003987 if (key_or_value && !val_len) {
3988 val_len = strlen(key_or_value);
3989 }
3990
Michal Vaskob104f112020-07-17 09:54:54 +02003991 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3992 /* create a data node and find the instance */
3993 if (schema->nodetype == LYS_LEAFLIST) {
3994 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003995 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3996 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003997 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003998 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003999 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004000 }
4001
4002 /* find it */
4003 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004004 } else {
4005 /* find the first schema node instance */
4006 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004007 }
4008
Michal Vaskoe444f752020-02-10 12:20:06 +01004009 lyd_free_tree(target);
4010 return rc;
4011}
Michal Vaskoccc02342020-05-21 10:09:21 +02004012
4013API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004014lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4015{
4016 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4017
4018 for ( ; first; first = first->next) {
4019 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4020 break;
4021 }
4022 }
4023
4024 if (match) {
4025 *match = (struct lyd_node *)first;
4026 }
4027 return first ? LY_SUCCESS : LY_ENOTFOUND;
4028}
4029
4030API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02004031lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4032{
4033 LY_ERR ret = LY_SUCCESS;
4034 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02004035 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004036 uint32_t i;
4037
4038 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4039
4040 memset(&xp_set, 0, sizeof xp_set);
4041
4042 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02004043 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4044 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004045
4046 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01004047 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004048 LY_CHECK_GOTO(ret, cleanup);
4049
4050 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004051 ret = ly_set_new(set);
4052 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004053
4054 /* transform into ly_set */
4055 if (xp_set.type == LYXP_SET_NODE_SET) {
4056 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4057 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004058 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004059 (*set)->size = xp_set.used;
4060
4061 for (i = 0; i < xp_set.used; ++i) {
4062 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004063 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004064 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004065 }
4066 }
4067 }
4068
4069cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004070 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004071 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004072 if (ret) {
4073 ly_set_free(*set, NULL);
4074 *set = NULL;
4075 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004076 return ret;
4077}
Radek Krejcica989142020-11-05 11:32:22 +01004078
Michal Vasko3e1f6552021-01-14 09:27:55 +01004079API LY_ERR
4080lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4081{
4082 LY_ERR ret = LY_SUCCESS;
4083 struct lyxp_expr *expr = NULL;
4084 struct ly_path *lypath = NULL;
4085
4086 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4087
4088 /* parse the path */
4089 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
4090 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4091 LY_CHECK_GOTO(ret, cleanup);
4092
4093 /* compile the path */
Radek Krejcid5d37432021-03-12 13:46:40 +01004094 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, LY_PATH_LREF_FALSE,
Michal Vasko3e1f6552021-01-14 09:27:55 +01004095 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
4096 (void *)LYD_CTX(ctx_node), NULL, &lypath);
4097 LY_CHECK_GOTO(ret, cleanup);
4098
4099 /* evaluate the path */
4100 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4101
4102cleanup:
4103 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4104 ly_path_free(LYD_CTX(ctx_node), lypath);
4105 return ret;
4106}
4107
Radek Krejcica989142020-11-05 11:32:22 +01004108API uint32_t
4109lyd_list_pos(const struct lyd_node *instance)
4110{
4111 const struct lyd_node *iter = NULL;
4112 uint32_t pos = 0;
4113
4114 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4115 return 0;
4116 }
4117
4118 /* data instances are ordered, so we can stop when we found instance of other schema node */
4119 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004120 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004121 /* overrun to the end of the siblings list */
4122 break;
4123 }
4124 ++pos;
4125 }
4126
4127 return pos;
4128}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004129
4130API struct lyd_node *
4131lyd_first_sibling(const struct lyd_node *node)
4132{
4133 struct lyd_node *start;
4134
4135 if (!node) {
4136 return NULL;
4137 }
4138
4139 /* get the first sibling */
4140 if (node->parent) {
4141 start = node->parent->child;
4142 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004143 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004144 }
4145
4146 return start;
4147}