blob: f611481e18b993e4e952fff601a139e238de8904 [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 Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Michal Vasko90932a92020-02-12 14:33:03 +010041#include "plugins_exts_internal.h"
Michal Vasko69730152020-10-09 16:30:07 +020042#include "plugins_exts_metadata.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;
Michal Vasko25d6ad02020-10-22 12:20:22 +020064 uint32_t options = (dynamic && *dynamic ? LY_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
116_lys_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 Vaskof937cfe2020-08-03 16:07:12 +0200155lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
156{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200157 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200158}
159
160API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100161lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200162 const struct lyd_node *tree, const struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200163{
164 LY_ERR rc;
165 struct ly_err_item *err = NULL;
166 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200167 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200168 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200169
170 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
171
Michal Vasko22df3f02020-08-24 13:29:22 +0200172 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200173 /* store */
174 rc = type->plugin->store(ctx ? ctx : LYD_CTX(node), type, value, value_len, 0, LY_PREF_JSON, NULL,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100175 LYD_HINT_DATA, node->schema, &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200176 if (rc == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200177 stored = 1;
178
179 /* resolve */
Michal Vasko9e685082021-01-29 14:49:09 +0100180 rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, &node->node, tree, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200181 }
182
183 if (rc) {
Radek Krejci73dead22019-07-11 16:46:16 +0200184 if (err) {
185 if (ctx) {
Michal Vasko9e685082021-01-29 14:49:09 +0100186 LOG_LOCSET(NULL, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100187 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100188 LOG_LOCBACK(0, 1, 0, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200189 }
Radek Krejci73dead22019-07-11 16:46:16 +0200190 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200191 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200192 if (stored) {
193 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
194 }
Radek Krejci73dead22019-07-11 16:46:16 +0200195 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200196 }
197
Michal Vasko3701af52020-08-03 14:29:38 +0200198 if (realtype) {
Michal Vasko29134f82020-11-13 18:03:20 +0100199 if (val.realtype->basetype == LY_TYPE_UNION) {
200 *realtype = val.subvalue->value.realtype;
201 } else {
202 *realtype = val.realtype;
203 }
Michal Vasko3701af52020-08-03 14:29:38 +0200204 }
205
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200206 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200207 return LY_SUCCESS;
208}
209
210API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200211lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200212{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200213 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200214 struct ly_ctx *ctx;
215 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200216 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200217
218 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
219
220 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200221 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200222
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200223 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100224 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100225 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 +0100226 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200227 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200228
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200229 /* compare values */
230 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200231
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200232 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200233 return ret;
234}
235
Radek Krejci19611252020-10-04 13:54:53 +0200236API ly_bool
237lyd_is_default(const struct lyd_node *node)
238{
239 const struct lysc_node_leaf *leaf;
240 const struct lysc_node_leaflist *llist;
241 const struct lyd_node_term *term;
242 LY_ARRAY_COUNT_TYPE u;
243
244 assert(node->schema->nodetype & LYD_NODE_TERM);
245 term = (const struct lyd_node_term *)node;
246
247 if (node->schema->nodetype == LYS_LEAF) {
248 leaf = (const struct lysc_node_leaf *)node->schema;
249 if (!leaf->dflt) {
250 return 0;
251 }
252
253 /* compare with the default value */
254 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
255 return 0;
256 }
257 } else {
258 llist = (const struct lysc_node_leaflist *)node->schema;
259 if (!llist->dflts) {
260 return 0;
261 }
262
263 LY_ARRAY_FOR(llist->dflts, u) {
264 /* compare with each possible default value */
265 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
266 return 0;
267 }
268 }
269 }
270
271 return 1;
272}
273
Radek Krejci7931b192020-06-25 17:05:03 +0200274static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200275lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200276{
Michal Vasko69730152020-10-09 16:30:07 +0200277 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200278 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200279 const char *path = in->method.fpath.filepath;
280 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200281
282 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200283 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200284
Radek Krejcif13b87b2020-12-01 22:02:17 +0100285 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
286 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100288 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
289 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100291 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
292 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200293 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200294 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200295 }
296
Radek Krejci7931b192020-06-25 17:05:03 +0200297 return format;
298}
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Michal Vaskoe0665742021-02-11 11:08:44 +0100300/**
301 * @brief Parse YANG data into a data tree.
302 *
303 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100304 * @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 +0100305 * @param[in] parent Parent to connect the parsed nodes to, if any.
306 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
307 * @param[in] in Input handle to read the input from.
308 * @param[in] format Expected format of the data in @p in.
309 * @param[in] parse_opts Options for parser.
310 * @param[in] val_opts Options for validation.
311 * @param[out] op Optional pointer to the parsed operation, if any.
312 * @return LY_ERR value.
313 */
314static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100315lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
316 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 +0200317{
Michal Vaskoe0665742021-02-11 11:08:44 +0100318 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200319 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100320 struct ly_set parsed = {0};
321 struct lyd_node *first;
322 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200323
Michal Vaskoe0665742021-02-11 11:08:44 +0100324 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200325
326 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100327 if (first_p) {
328 *first_p = NULL;
329 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200330
Michal Vasko63f3d842020-07-08 10:10:14 +0200331 /* remember input position */
332 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200333
Michal Vaskoe0665742021-02-11 11:08:44 +0100334 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200335 switch (format) {
336 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100337 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 +0200338 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200339 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100340 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 +0200341 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200342 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100343 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 +0200344 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200345 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100346 LOGARG(ctx, format);
347 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100348 break;
349 }
350 LY_CHECK_GOTO(rc, cleanup);
351
352 if (parent) {
353 /* get first top-level sibling */
354 for (first = parent; first->parent; first = lyd_parent(first)) {}
355 first = lyd_first_sibling(first);
356 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200357 }
358
Michal Vaskoe0665742021-02-11 11:08:44 +0100359 if (!(parse_opts & LYD_PARSE_ONLY)) {
360 /* validate data */
361 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types,
362 &lydctx->meta_types, NULL);
363 LY_CHECK_GOTO(rc, cleanup);
364 }
Radek Krejci7931b192020-06-25 17:05:03 +0200365
Michal Vaskoe0665742021-02-11 11:08:44 +0100366 /* set the operation node */
367 if (op) {
368 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200369 }
370
371cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100372 if (lydctx) {
373 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100375 if (rc) {
376 if (parent) {
377 /* free all the parsed subtrees */
378 for (i = 0; i < parsed.count; ++i) {
379 lyd_free_tree(parsed.dnodes[i]);
380 }
381 } else {
382 /* free everything */
383 lyd_free_all(*first_p);
384 *first_p = NULL;
385 }
386 }
387 ly_set_erase(&parsed, NULL);
388 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200389}
390
391API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100392lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
393 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
394{
395 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
396
397 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
398 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
399 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
400
401 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
402}
403
404API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100405lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
406 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
407{
408 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
409 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
410 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
411
Radek Krejcif16e2542021-02-17 15:39:23 +0100412 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100413}
414
415API LY_ERR
416lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
417 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200418{
419 LY_ERR ret;
420 struct ly_in *in;
421
422 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100423 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200424
425 ly_in_free(in, 0);
426 return ret;
427}
428
429API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200430lyd_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 +0200431 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200432{
433 LY_ERR ret;
434 struct ly_in *in;
435
436 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100437 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200438
439 ly_in_free(in, 0);
440 return ret;
441}
442
443API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200444lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
445 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200446{
447 LY_ERR ret;
448 struct ly_in *in;
449
450 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100451 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200452
453 ly_in_free(in, 0);
454 return ret;
455}
456
Radek Krejcif16e2542021-02-17 15:39:23 +0100457/**
458 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
459 * for schema nodes locked inside the extension instance.
460 *
461 * At least one of @p parent, @p tree, or @p op must always be set.
462 *
463 * Specific @p data_type values have different parameter meaning as follows:
464 * - ::LYD_TYPE_RPC_NETCONF:
465 * - @p parent - must be NULL, the whole RPC is expected;
466 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
467 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
468 * a separate opaque data tree, even if the function fails, this may be returned;
469 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
470 *
471 * - ::LYD_TYPE_NOTIF_NETCONF:
472 * - @p parent - must be NULL, the whole notification is expected;
473 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
474 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
475 * a separate opaque data tree, even if the function fails, this may be returned;
476 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
477 *
478 * - ::LYD_TYPE_REPLY_NETCONF:
479 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
480 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
481 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
482 * a separate opaque data tree, even if the function fails, this may be returned;
483 * - @p op - must be NULL, the reply is appended to the RPC;
484 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
485 *
486 * @param[in] ctx libyang context.
487 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
488 * @param[in] parent Optional parent to connect the parsed nodes to.
489 * @param[in] in Input handle to read the input from.
490 * @param[in] format Expected format of the data in @p in.
491 * @param[in] data_type Expected operation to parse (@ref datatype).
492 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
493 * @param[out] op Optional parsed operation node.
494 * @return LY_ERR value.
495 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
496 */
497static LY_ERR
498lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
499 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 +0200500{
Michal Vaskoe0665742021-02-11 11:08:44 +0100501 LY_ERR rc = LY_SUCCESS;
502 struct lyd_ctx *lydctx = NULL;
503 struct ly_set parsed = {0};
504 struct lyd_node *first = NULL, *envp = NULL;
505 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200506
Michal Vasko27fb0262021-02-23 09:42:01 +0100507 if (!ctx) {
508 ctx = LYD_CTX(parent);
509 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 if (tree) {
511 *tree = NULL;
512 }
513 if (op) {
514 *op = NULL;
515 }
516
Radek Krejci7931b192020-06-25 17:05:03 +0200517 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200518
Michal Vasko63f3d842020-07-08 10:10:14 +0200519 /* remember input position */
520 in->func_start = in->current;
521
Michal Vaskoe0665742021-02-11 11:08:44 +0100522 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100523 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100524 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100525 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
526 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
527 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100528 }
529 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
530 val_opts = 0;
531
532 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200533 switch (format) {
534 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100535 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 +0100536 if (rc && envp) {
537 /* special situation when the envelopes were parsed successfully */
538 if (tree) {
539 *tree = envp;
540 }
541 ly_set_erase(&parsed, NULL);
542 return rc;
543 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100544 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200545 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100546 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100547 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200548 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100549 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100550 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200551 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100552 LOGARG(ctx, format);
553 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200554 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200555 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200557
Michal Vaskoe0665742021-02-11 11:08:44 +0100558 /* set out params correctly */
559 if (tree) {
560 if (envp) {
561 /* special out param meaning */
562 *tree = envp;
563 } else {
564 *tree = parent ? NULL : first;
565 }
566 }
567 if (op) {
568 *op = lydctx->op_node;
569 }
570
571cleanup:
572 if (lydctx) {
573 lydctx->free(lydctx);
574 }
575 if (rc) {
576 if (parent) {
577 /* free all the parsed subtrees */
578 for (i = 0; i < parsed.count; ++i) {
579 lyd_free_tree(parsed.dnodes[i]);
580 }
581 } else {
582 /* free everything (cannot occur in the current code, a safety) */
583 lyd_free_all(first);
584 if (tree) {
585 *tree = NULL;
586 }
587 if (op) {
588 *op = NULL;
589 }
590 }
591 }
592 ly_set_erase(&parsed, NULL);
593 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200594}
Radek Krejci084289f2019-07-09 17:35:30 +0200595
Radek Krejcif16e2542021-02-17 15:39:23 +0100596API LY_ERR
597lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
598 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
599{
600 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
601
602 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
603}
604
605API LY_ERR
606lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
607 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
608{
609 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
610
611 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
612
613 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
614}
615
Michal Vasko90932a92020-02-12 14:33:03 +0100616LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200617lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
618 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100619{
620 LY_ERR ret;
621 struct lyd_node_term *term;
622
Michal Vasko9b368d32020-02-14 13:53:31 +0100623 assert(schema->nodetype & LYD_NODE_TERM);
624
Michal Vasko90932a92020-02-12 14:33:03 +0100625 term = calloc(1, sizeof *term);
626 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
627
628 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100629 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100630 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100631
Radek Krejciddace2c2021-01-08 11:30:56 +0100632 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200633 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100634 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100635 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200636 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100637 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100638
Michal Vasko9e685082021-01-29 14:49:09 +0100639 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100640 return ret;
641}
642
643LY_ERR
644lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
645{
646 LY_ERR ret;
647 struct lyd_node_term *term;
648 struct lysc_type *type;
649
650 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200651 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100652
653 term = calloc(1, sizeof *term);
654 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
655
656 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100657 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100658 term->flags = LYD_NEW;
659
660 type = ((struct lysc_node_leaf *)schema)->type;
661 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
662 if (ret) {
663 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
664 free(term);
665 return ret;
666 }
Michal Vasko9e685082021-01-29 14:49:09 +0100667 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100668
Michal Vasko9e685082021-01-29 14:49:09 +0100669 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100670 return ret;
671}
672
673LY_ERR
674lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
675{
676 struct lyd_node_inner *in;
677
Michal Vasko9b368d32020-02-14 13:53:31 +0100678 assert(schema->nodetype & LYD_NODE_INNER);
679
Michal Vasko90932a92020-02-12 14:33:03 +0100680 in = calloc(1, sizeof *in);
681 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
682
683 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100684 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100685 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100686 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
687 in->flags |= LYD_DEFAULT;
688 }
Michal Vasko90932a92020-02-12 14:33:03 +0100689
Michal Vasko9b368d32020-02-14 13:53:31 +0100690 /* do not hash list with keys, we need them for the hash */
691 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100692 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100693 }
Michal Vasko90932a92020-02-12 14:33:03 +0100694
Michal Vasko9e685082021-01-29 14:49:09 +0100695 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100696 return LY_SUCCESS;
697}
698
Michal Vasko90932a92020-02-12 14:33:03 +0100699LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200700lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100701{
702 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100703 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200704 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100705
Michal Vasko004d3152020-06-11 19:59:22 +0200706 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100707
708 /* create list */
709 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
710
Radek Krejciddace2c2021-01-08 11:30:56 +0100711 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100712
Michal Vasko90932a92020-02-12 14:33:03 +0100713 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200714 LY_ARRAY_FOR(predicates, u) {
715 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100716 lyd_insert_node(list, NULL, key);
717 }
718
Michal Vasko9b368d32020-02-14 13:53:31 +0100719 /* hash having all the keys */
720 lyd_hash(list);
721
Michal Vasko90932a92020-02-12 14:33:03 +0100722 /* success */
723 *node = list;
724 list = NULL;
725
726cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100727 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100728 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200729 return ret;
730}
731
732static LY_ERR
733lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
734{
735 LY_ERR ret = LY_SUCCESS;
736 struct lyxp_expr *expr = NULL;
737 uint16_t exp_idx = 0;
738 enum ly_path_pred_type pred_type = 0;
739 struct ly_path_predicate *predicates = NULL;
740
Radek Krejciddace2c2021-01-08 11:30:56 +0100741 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100742
Michal Vasko004d3152020-06-11 19:59:22 +0200743 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200744 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 +0200745 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200746
747 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200748 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
749 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200750
751 /* create the list node */
752 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
753
754cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100755 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200756 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200757 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100758 return ret;
759}
760
761LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100762lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
763 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100764{
Michal Vasko366a4a12020-12-04 16:23:57 +0100765 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100766 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100767 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100768
Michal Vasko9b368d32020-02-14 13:53:31 +0100769 assert(schema->nodetype & LYD_NODE_ANY);
770
Michal Vasko90932a92020-02-12 14:33:03 +0100771 any = calloc(1, sizeof *any);
772 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
773
774 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100775 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100776 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100777
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100779
780 if (use_value) {
781 any->value.str = value;
782 any->value_type = value_type;
783 } else {
784 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100785 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100786 LY_CHECK_ERR_RET(ret, free(any), ret);
787 }
Michal Vasko9e685082021-01-29 14:49:09 +0100788 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100789
Michal Vasko9e685082021-01-29 14:49:09 +0100790 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100791 return LY_SUCCESS;
792}
793
Michal Vasko52927e22020-03-16 17:26:14 +0100794LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100795lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
796 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
797 LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100798{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200799 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100800 struct lyd_node_opaq *opaq;
801
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200802 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100803
804 if (!value_len) {
805 value = "";
806 }
807
808 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100809 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100810
Michal Vasko9e685082021-01-29 14:49:09 +0100811 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100812 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200813
Michal Vasko52927e22020-03-16 17:26:14 +0100814 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100815 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100816 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100818 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200819 }
Michal Vasko52927e22020-03-16 17:26:14 +0100820 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200821 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100822 *dynamic = 0;
823 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200824 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100825 }
Michal Vasko501af032020-11-11 20:27:44 +0100826
827 opaq->format = format;
828 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200829 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100830 opaq->ctx = ctx;
831
Radek Krejci011e4aa2020-09-04 15:22:31 +0200832finish:
833 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100834 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100835 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200836 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100837 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200838 }
839 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100840}
841
Michal Vasko3a41dff2020-07-15 14:30:28 +0200842API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100843lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
844 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100845{
846 struct lyd_node *ret = NULL;
847 const struct lysc_node *schema;
848 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
849
Michal Vasko6027eb92020-07-15 16:37:30 +0200850 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100851
Michal Vaskof03ed032020-03-04 13:31:44 +0100852 if (!module) {
853 module = parent->schema->module;
854 }
855
Michal Vasko3a41dff2020-07-15 14:30:28 +0200856 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100857 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100858 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 +0100859
Michal Vasko3a41dff2020-07-15 14:30:28 +0200860 LY_CHECK_RET(lyd_create_inner(schema, &ret));
861 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100862 lyd_insert_node(parent, NULL, ret);
863 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200864
865 if (node) {
866 *node = ret;
867 }
868 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100869}
870
Michal Vasko3a41dff2020-07-15 14:30:28 +0200871API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100872lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
873{
874 struct lyd_node *ret = NULL;
875 const struct lysc_node *schema;
876 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
877
878 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
879
880 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
881 if (!schema) {
882 if (ext->argument) {
883 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
884 name, ext->argument, ext->def->name);
885 } else {
886 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
887 name, ext->def->name);
888 }
889 return LY_ENOTFOUND;
890 }
891 LY_CHECK_RET(lyd_create_inner(schema, &ret));
892
893 *node = ret;
894
895 return LY_SUCCESS;
896}
897
898API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100899lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
900 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100901{
902 struct lyd_node *ret = NULL, *key;
903 const struct lysc_node *schema, *key_s;
904 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
905 va_list ap;
906 const char *key_val;
907 LY_ERR rc = LY_SUCCESS;
908
Michal Vasko6027eb92020-07-15 16:37:30 +0200909 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100910
Michal Vaskof03ed032020-03-04 13:31:44 +0100911 if (!module) {
912 module = parent->schema->module;
913 }
914
Radek Krejci41ac9942020-11-02 14:47:56 +0100915 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 +0200916 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100917
918 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200919 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100920
Michal Vasko3a41dff2020-07-15 14:30:28 +0200921 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100922
923 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100924 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 +0100925 key_val = va_arg(ap, const char *);
926
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200927 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
928 NULL, &key);
929 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100930 lyd_insert_node(ret, NULL, key);
931 }
932
Michal Vasko013a8182020-03-03 10:46:53 +0100933 if (parent) {
934 lyd_insert_node(parent, NULL, ret);
935 }
936
937cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200938 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100939 if (rc) {
940 lyd_free_tree(ret);
941 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200942 } else if (node) {
943 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100944 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100946}
947
Michal Vasko3a41dff2020-07-15 14:30:28 +0200948API LY_ERR
949lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +0100950 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100951{
952 struct lyd_node *ret = NULL;
953 const struct lysc_node *schema;
954 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
955
Michal Vasko6027eb92020-07-15 16:37:30 +0200956 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100957
Michal Vaskof03ed032020-03-04 13:31:44 +0100958 if (!module) {
959 module = parent->schema->module;
960 }
Michal Vasko004d3152020-06-11 19:59:22 +0200961 if (!keys) {
962 keys = "";
963 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100964
Michal Vasko004d3152020-06-11 19:59:22 +0200965 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +0100966 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 +0200967 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100968
Michal Vasko004d3152020-06-11 19:59:22 +0200969 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
970 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200971 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200972 } else {
973 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200975 }
Michal Vasko004d3152020-06-11 19:59:22 +0200976 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100977 lyd_insert_node(parent, NULL, ret);
978 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200979
980 if (node) {
981 *node = ret;
982 }
983 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100984}
985
Michal Vasko3a41dff2020-07-15 14:30:28 +0200986API LY_ERR
987lyd_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 +0100988 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100989{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200990 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100991 struct lyd_node *ret = NULL;
992 const struct lysc_node *schema;
993 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
994
Michal Vasko6027eb92020-07-15 16:37:30 +0200995 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100996
Michal Vaskof03ed032020-03-04 13:31:44 +0100997 if (!module) {
998 module = parent->schema->module;
999 }
1000
Radek Krejci41ac9942020-11-02 14:47:56 +01001001 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 +02001002 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001003
Radek Krejci8a5afc22021-03-12 11:10:47 +01001004 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 +02001005 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001006 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001007 lyd_insert_node(parent, NULL, ret);
1008 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001009
1010 if (node) {
1011 *node = ret;
1012 }
1013 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001014}
1015
Michal Vasko3a41dff2020-07-15 14:30:28 +02001016API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001017lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1018{
1019 LY_ERR rc;
1020 struct lyd_node *ret = NULL;
1021 const struct lysc_node *schema;
1022 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1023
1024 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1025
1026 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1027 if (!schema) {
1028 if (ext->argument) {
1029 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1030 name, ext->argument, ext->def->name);
1031 } else {
1032 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1033 }
1034 return LY_ENOTFOUND;
1035 }
1036 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
1037 LY_CHECK_RET(rc);
1038
1039 *node = ret;
1040
1041 return LY_SUCCESS;
1042}
1043
1044API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001045lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1046 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001047{
1048 struct lyd_node *ret = NULL;
1049 const struct lysc_node *schema;
1050 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1051
Michal Vasko6027eb92020-07-15 16:37:30 +02001052 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001053
Michal Vaskof03ed032020-03-04 13:31:44 +01001054 if (!module) {
1055 module = parent->schema->module;
1056 }
1057
Radek Krejci41ac9942020-11-02 14:47:56 +01001058 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 +02001059 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001060
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001061 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001062 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001063 lyd_insert_node(parent, NULL, ret);
1064 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065
1066 if (node) {
1067 *node = ret;
1068 }
1069 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001070}
1071
Michal Vasko4490d312020-06-16 13:08:55 +02001072/**
1073 * @brief Update node value.
1074 *
1075 * @param[in] node Node to update.
1076 * @param[in] value New value to set.
1077 * @param[in] value_type Type of @p value for any node.
1078 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1079 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1080 * @return LY_ERR value.
1081 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001082static LY_ERR
1083lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001084 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001085{
1086 LY_ERR ret = LY_SUCCESS;
1087 struct lyd_node *new_any;
1088
1089 switch (node->schema->nodetype) {
1090 case LYS_CONTAINER:
1091 case LYS_NOTIF:
1092 case LYS_RPC:
1093 case LYS_ACTION:
1094 case LYS_LIST:
1095 case LYS_LEAFLIST:
1096 /* if it exists, there is nothing to update */
1097 *new_parent = NULL;
1098 *new_node = NULL;
1099 break;
1100 case LYS_LEAF:
1101 ret = lyd_change_term(node, value);
1102 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1103 /* there was an actual change (at least of the default flag) */
1104 *new_parent = node;
1105 *new_node = node;
1106 ret = LY_SUCCESS;
1107 } else if (ret == LY_ENOT) {
1108 /* no change */
1109 *new_parent = NULL;
1110 *new_node = NULL;
1111 ret = LY_SUCCESS;
1112 } /* else error */
1113 break;
1114 case LYS_ANYDATA:
1115 case LYS_ANYXML:
1116 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001117 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001118
1119 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001120 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001121 /* not equal, switch values (so that we can use generic node free) */
1122 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1123 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1124 ((struct lyd_node_any *)node)->value.str = value;
1125 ((struct lyd_node_any *)node)->value_type = value_type;
1126
1127 *new_parent = node;
1128 *new_node = node;
1129 } else {
1130 /* they are equal */
1131 *new_parent = NULL;
1132 *new_node = NULL;
1133 }
1134 lyd_free_tree(new_any);
1135 break;
1136 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001137 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001138 ret = LY_EINT;
1139 break;
1140 }
1141
1142 return ret;
1143}
1144
Michal Vasko3a41dff2020-07-15 14:30:28 +02001145API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001146lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1147 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001148{
Michal Vaskod86997b2020-05-26 15:19:54 +02001149 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001150 size_t pref_len, name_len;
1151
Michal Vasko2b5344c2021-02-26 10:12:05 +01001152 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1153 if (!ctx) {
1154 ctx = LYD_CTX(parent);
1155 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001156
Michal Vasko871a0252020-11-11 18:35:24 +01001157 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001158 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001159 return LY_EINVAL;
1160 }
Michal Vasko610553d2020-11-18 18:15:05 +01001161 if (meta) {
1162 *meta = NULL;
1163 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001164
1165 /* parse the name */
1166 tmp = name;
1167 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1168 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001169 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001170 }
1171
1172 /* find the module */
1173 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001174 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001175 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 +02001176 }
1177
1178 /* set value if none */
1179 if (!val_str) {
1180 val_str = "";
1181 }
1182
Michal Vasko871a0252020-11-11 18:35:24 +01001183 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
1184 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1185}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001186
Michal Vaskoba696702020-11-11 19:12:45 +01001187API LY_ERR
1188lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1189 struct lyd_meta **meta)
1190{
1191 const struct lys_module *mod;
1192
1193 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1194
1195 if (parent && !parent->schema) {
1196 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1197 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198 }
Michal Vasko610553d2020-11-18 18:15:05 +01001199 if (meta) {
1200 *meta = NULL;
1201 }
Michal Vaskoba696702020-11-11 19:12:45 +01001202
1203 switch (attr->format) {
1204 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001205 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001206 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001207 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001208 return LY_ENOTFOUND;
1209 }
1210 break;
1211 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001212 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001213 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001214 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001215 return LY_ENOTFOUND;
1216 }
1217 break;
1218 default:
1219 LOGINT_RET(ctx);
1220 }
1221
Michal Vaskoad92b672020-11-12 13:11:31 +01001222 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 +01001223 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001224}
1225
Michal Vasko3a41dff2020-07-15 14:30:28 +02001226API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001227lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001228 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001229{
1230 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001231
Michal Vasko0ab974d2021-02-24 13:18:26 +01001232 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 +02001233
1234 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001235 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001236 }
1237 if (!value) {
1238 value = "";
1239 }
1240
Michal Vasko0ab974d2021-02-24 13:18:26 +01001241 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1242 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001243 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001244 lyd_insert_node(parent, NULL, ret);
1245 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001246
1247 if (node) {
1248 *node = ret;
1249 }
1250 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001251}
1252
Michal Vasko3a41dff2020-07-15 14:30:28 +02001253API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001254lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001255 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001256{
1257 struct lyd_node *ret = NULL;
1258
1259 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1260
1261 if (!ctx) {
1262 ctx = LYD_CTX(parent);
1263 }
1264 if (!value) {
1265 value = "";
1266 }
1267
Michal Vasko0ab974d2021-02-24 13:18:26 +01001268 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1269 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001270 if (parent) {
1271 lyd_insert_node(parent, NULL, ret);
1272 }
1273
1274 if (node) {
1275 *node = ret;
1276 }
1277 return LY_SUCCESS;
1278}
1279
1280API LY_ERR
1281lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001282 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001283{
Radek Krejci1798aae2020-07-14 13:26:06 +02001284 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001285 const struct ly_ctx *ctx;
1286 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001287 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001288
Michal Vasko3a41dff2020-07-15 14:30:28 +02001289 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001290
Michal Vaskob7be7a82020-08-20 09:09:04 +02001291 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001292
1293 /* parse the name */
1294 tmp = name;
1295 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001296 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001297 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001298 }
1299
Michal Vasko51d21b82020-11-13 18:03:54 +01001300 /* get the module */
1301 if (module_name) {
1302 mod_len = strlen(module_name);
1303 } else {
1304 module_name = prefix;
1305 mod_len = pref_len;
1306 }
1307
Michal Vasko00cbf532020-06-15 13:58:47 +02001308 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001309 if (!value) {
1310 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001311 }
1312
Michal Vasko8d65f852021-02-17 11:28:13 +01001313 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1314 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1315
1316 if (attr) {
1317 *attr = ret;
1318 }
1319 return LY_SUCCESS;
1320}
1321
1322API LY_ERR
1323lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1324 struct lyd_attr **attr)
1325{
1326 struct lyd_attr *ret = NULL;
1327 const struct ly_ctx *ctx;
1328 const char *prefix, *tmp;
1329 size_t pref_len, name_len;
1330
1331 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1332
1333 ctx = LYD_CTX(parent);
1334
1335 /* parse the name */
1336 tmp = name;
1337 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1338 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1339 return LY_EVALID;
1340 }
1341
1342 /* set value if none */
1343 if (!value) {
1344 value = "";
1345 }
1346
1347 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1348 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001349
1350 if (attr) {
1351 *attr = ret;
1352 }
1353 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001354}
1355
1356API LY_ERR
1357lyd_change_term(struct lyd_node *term, const char *val_str)
1358{
1359 LY_ERR ret = LY_SUCCESS;
1360 struct lysc_type *type;
1361 struct lyd_node_term *t;
1362 struct lyd_node *parent;
1363 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001364 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001365
1366 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1367
1368 if (!val_str) {
1369 val_str = "";
1370 }
1371 t = (struct lyd_node_term *)term;
1372 type = ((struct lysc_node_leaf *)term->schema)->type;
1373
1374 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001375 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001376 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 +01001377 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001378 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001379 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001380
1381 /* compare original and new value */
1382 if (type->plugin->compare(&t->value, &val)) {
1383 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001384 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001385 t->value = val;
1386 memset(&val, 0, sizeof val);
1387 val_change = 1;
1388 } else {
1389 val_change = 0;
1390 }
1391
1392 /* always clear the default flag */
1393 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001394 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001395 parent->flags &= ~LYD_DEFAULT;
1396 }
1397 dflt_change = 1;
1398 } else {
1399 dflt_change = 0;
1400 }
1401
1402 if (val_change || dflt_change) {
1403 /* make the node non-validated */
1404 term->flags &= LYD_NEW;
1405 }
1406
1407 if (val_change) {
1408 if (term->schema->nodetype == LYS_LEAFLIST) {
1409 /* leaf-list needs to be hashed again and re-inserted into parent */
1410 lyd_unlink_hash(term);
1411 lyd_hash(term);
1412 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1413 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1414 /* list needs to be updated if its key was changed */
1415 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001416 lyd_unlink_hash(lyd_parent(term));
1417 lyd_hash(lyd_parent(term));
1418 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001419 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1420 }
1421
1422 /* retrun value */
1423 if (!val_change) {
1424 if (dflt_change) {
1425 /* only default flag change */
1426 ret = LY_EEXIST;
1427 } else {
1428 /* no change */
1429 ret = LY_ENOT;
1430 }
1431 } /* else value changed, LY_SUCCESS */
1432
1433cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001434 if (val.realtype) {
1435 type->plugin->free(LYD_CTX(term), &val);
1436 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001437 return ret;
1438}
1439
Michal Vasko41586352020-07-13 13:54:25 +02001440API LY_ERR
1441lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1442{
1443 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001444 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001445 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001446 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001447
1448 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1449
1450 if (!val_str) {
1451 val_str = "";
1452 }
1453
1454 /* parse the new value into a new meta structure */
1455 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 +01001456 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001457
1458 /* compare original and new value */
1459 if (lyd_compare_meta(meta, m2)) {
1460 /* values differ, switch them */
1461 val = meta->value;
1462 meta->value = m2->value;
1463 m2->value = val;
1464 val_change = 1;
1465 } else {
1466 val_change = 0;
1467 }
1468
1469 /* retrun value */
1470 if (!val_change) {
1471 /* no change */
1472 ret = LY_ENOT;
1473 } /* else value changed, LY_SUCCESS */
1474
1475cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001476 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001477 return ret;
1478}
1479
Michal Vasko3a41dff2020-07-15 14:30:28 +02001480API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001481lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02001482 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001483{
Michal Vaskod5e67442021-03-04 15:56:31 +01001484 return lyd_new_path2(parent, ctx, path, value, LYD_ANYDATA_STRING, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001485}
1486
Michal Vasko6741dc62021-02-04 09:27:45 +01001487static LY_ERR
1488lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1489{
1490 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci910d0132021-03-05 14:19:31 +01001491 const struct lysc_node *schema = NULL;
Michal Vasko6741dc62021-02-04 09:27:45 +01001492 struct ly_path_predicate *pred;
Radek Krejci910d0132021-03-05 14:19:31 +01001493 LY_ARRAY_COUNT_TYPE u, new_count = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001494
1495 assert(path);
1496
1497 /* go through all the compiled nodes */
1498 LY_ARRAY_FOR(path, u) {
1499 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001500 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001501 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1502 if (schema->flags & LYS_KEYLESS) {
1503 /* creating a new keyless list instance */
1504 break;
1505 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1506 LOG_LOCSET(schema, NULL, NULL, NULL);
1507 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1508 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1509 LOG_LOCBACK(1, 0, 0, 0);
1510 return LY_EINVAL;
1511 } /* else creating an opaque list without keys */
1512 }
1513 }
1514
Michal Vasko6f9b4262021-02-04 12:09:56 +01001515 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001516 /* parse leafref value into a predicate, if not defined in the path */
1517 if (!value) {
1518 value = "";
1519 }
1520
1521 r = LY_SUCCESS;
1522 if (options & LYD_NEW_PATH_OPAQ) {
1523 r = lys_value_validate(NULL, schema, value, strlen(value));
1524 }
1525 if (!r) {
1526 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001527 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1528 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001529
1530 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1531 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1532 LY_CHECK_GOTO(ret, cleanup);
1533 ++((struct lysc_type *)pred->value.realtype)->refcount;
1534
1535 if (schema->flags & LYS_CONFIG_R) {
1536 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001537 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001538 }
1539 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1540 }
1541
1542 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001543 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001544 LY_ARRAY_DECREMENT(path);
1545 }
1546
1547cleanup:
1548 return ret;
1549}
1550
Michal Vasko00cbf532020-06-15 13:58:47 +02001551API LY_ERR
1552lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001553 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001554{
1555 LY_ERR ret = LY_SUCCESS, r;
1556 struct lyxp_expr *exp = NULL;
1557 struct ly_path *p = NULL;
1558 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1559 const struct lysc_node *schema;
Radek Krejci910d0132021-03-05 14:19:31 +01001560 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001561
1562 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1563
1564 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001565 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001566 }
1567
1568 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001569 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 +02001570 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001571
1572 /* compile path */
1573 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001574 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 +01001575 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001576
Michal Vasko6741dc62021-02-04 09:27:45 +01001577 /* check the compiled path before searching existing nodes, it may be shortened */
1578 orig_count = LY_ARRAY_COUNT(p);
1579 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001580
1581 /* try to find any existing nodes in the path */
1582 if (parent) {
1583 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1584 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001585 if (orig_count == LY_ARRAY_COUNT(p)) {
1586 /* the node exists, are we supposed to update it or is it just a default? */
1587 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1588 LOG_LOCSET(NULL, node, NULL, NULL);
1589 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1590 LOG_LOCBACK(0, 1, 0, 0);
1591 ret = LY_EEXIST;
1592 goto cleanup;
1593 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001594
Michal Vasko6741dc62021-02-04 09:27:45 +01001595 /* update the existing node */
1596 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1597 goto cleanup;
1598 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001599 } else if (ret == LY_EINCOMPLETE) {
1600 /* some nodes were found, adjust the iterator to the next segment */
1601 ++path_idx;
1602 } else if (ret == LY_ENOTFOUND) {
1603 /* 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 +01001604 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001605 node = parent;
1606 }
1607 } else {
1608 /* error */
1609 goto cleanup;
1610 }
1611 }
1612
Michal Vasko6741dc62021-02-04 09:27:45 +01001613 /* restore the full path for creating new nodes */
1614 while (orig_count > LY_ARRAY_COUNT(p)) {
1615 LY_ARRAY_INCREMENT(p);
1616 }
1617
Michal Vasko00cbf532020-06-15 13:58:47 +02001618 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001619 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001620 cur_parent = node;
1621 schema = p[path_idx].node;
1622
1623 switch (schema->nodetype) {
1624 case LYS_LIST:
1625 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001626 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001627 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001628 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1629 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1630 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001631 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001632 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1633 LOG_LOCSET(schema, NULL, NULL, NULL);
1634 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1635 lys_nodetype2str(schema->nodetype), schema->name, path);
1636 LOG_LOCBACK(1, 0, 0, 0);
1637 ret = LY_EINVAL;
1638 goto cleanup;
1639 }
1640
Michal Vasko00cbf532020-06-15 13:58:47 +02001641 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1642 }
1643 break;
1644 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001645 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001646 case LYS_CONTAINER:
1647 case LYS_NOTIF:
1648 case LYS_RPC:
1649 case LYS_ACTION:
1650 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1651 break;
1652 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001653 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001654 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001655 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1656 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1657 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001658 } else {
1659 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1660 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1661 }
1662 break;
1663 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001664 if (lysc_is_key(schema)) {
1665 /* it must have been already created or some error will occur later */
1666 assert(cur_parent);
1667 node = lyd_child(cur_parent);
1668 assert(node && (node->schema == schema));
1669 goto next_iter;
1670 }
1671
1672 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001673 if (!value) {
1674 value = "";
1675 }
1676
1677 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001678 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001679 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001680 }
1681 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001682 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1683 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001684 } else {
1685 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001686 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1687 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001688 }
1689 break;
1690 case LYS_ANYDATA:
1691 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001692 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001693 break;
1694 default:
1695 LOGINT(ctx);
1696 ret = LY_EINT;
1697 goto cleanup;
1698 }
1699
1700 if (cur_parent) {
1701 /* connect to the parent */
1702 lyd_insert_node(cur_parent, NULL, node);
1703 } else if (parent) {
1704 /* connect to top-level siblings */
1705 lyd_insert_node(NULL, &parent, node);
1706 }
1707
Michal Vasko35880332020-12-08 13:08:12 +01001708next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001709 /* update remembered nodes */
1710 if (!nparent) {
1711 nparent = node;
1712 }
1713 nnode = node;
1714 }
1715
1716cleanup:
1717 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001718 if (p) {
1719 while (orig_count > LY_ARRAY_COUNT(p)) {
1720 LY_ARRAY_INCREMENT(p);
1721 }
1722 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001723 ly_path_free(ctx, p);
1724 if (!ret) {
1725 /* set out params only on success */
1726 if (new_parent) {
1727 *new_parent = nparent;
1728 }
1729 if (new_node) {
1730 *new_node = nnode;
1731 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001732 } else {
1733 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001734 }
1735 return ret;
1736}
1737
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001738LY_ERR
1739lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001740 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
Radek Krejci0f969882020-08-21 16:56:47 +02001741 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001742{
1743 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001744 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001745 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001746 struct lyd_value **dflts;
1747 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001748 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001749
1750 assert(first && (parent || sparent || mod));
1751
1752 if (!sparent && parent) {
1753 sparent = parent->schema;
1754 }
1755
Michal Vasko630d9892020-12-08 17:11:08 +01001756 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1757 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1758 getnext_opts |= LYS_GETNEXT_OUTPUT;
1759 }
1760
1761 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001762 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1763 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001764 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1765 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001766 }
1767
1768 switch (iter->nodetype) {
1769 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001770 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1771 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001772 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001773 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001774 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001775 } else if (node) {
1776 /* create any default data in the existing case */
1777 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001778 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001779 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001780 }
1781 break;
1782 case LYS_CONTAINER:
1783 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1784 /* create default NP container */
1785 LY_CHECK_RET(lyd_create_inner(iter, &node));
Radek Krejci9a3823e2021-01-27 20:26:46 +01001786 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001787 lyd_insert_node(parent, first, node);
1788
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001789 if (lysc_node_when(iter) && node_when) {
1790 /* remember to resolve when */
1791 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1792 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001793 if (diff) {
1794 /* add into diff */
1795 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1796 }
1797
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001798 /* create any default children */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001799 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types,
Michal Vasko69730152020-10-09 16:30:07 +02001800 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001801 }
1802 break;
1803 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001804 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1805 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001806 /* create default leaf */
1807 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1808 if (ret == LY_EINCOMPLETE) {
1809 if (node_types) {
1810 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001811 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001812 }
1813 } else if (ret) {
1814 return ret;
1815 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001816 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001817 lyd_insert_node(parent, first, node);
1818
Radek Krejci9a3823e2021-01-27 20:26:46 +01001819 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001820 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001821 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001822 }
1823 if (diff) {
1824 /* add into diff */
1825 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1826 }
1827 }
1828 break;
1829 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001830 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1831 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001832 /* create all default leaf-lists */
1833 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1834 LY_ARRAY_FOR(dflts, u) {
1835 ret = lyd_create_term2(iter, dflts[u], &node);
1836 if (ret == LY_EINCOMPLETE) {
1837 if (node_types) {
1838 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001839 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001840 }
1841 } else if (ret) {
1842 return ret;
1843 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001844 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001845 lyd_insert_node(parent, first, node);
1846
Radek Krejci9a3823e2021-01-27 20:26:46 +01001847 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001848 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001849 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001850 }
1851 if (diff) {
1852 /* add into diff */
1853 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1854 }
1855 }
1856 }
1857 break;
1858 default:
1859 /* without defaults */
1860 break;
1861 }
1862 }
1863
1864 return LY_SUCCESS;
1865}
1866
1867API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001868lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001869{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001870 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001871 struct lyd_node *node;
1872 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001873
1874 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1875 if (diff) {
1876 *diff = NULL;
1877 }
1878
Michal Vasko56daf732020-08-10 10:57:18 +02001879 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001880 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001881 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1882 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001883 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when,
1884 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001885 }
1886
Michal Vasko56daf732020-08-10 10:57:18 +02001887 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001888 }
1889
Michal Vasko3488ada2020-12-03 14:18:19 +01001890 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001891 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01001892
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001893cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001894 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001895 if (ret && diff) {
1896 lyd_free_all(*diff);
1897 *diff = NULL;
1898 }
1899 return ret;
1900}
1901
1902API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001903lyd_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 +02001904{
1905 const struct lys_module *mod;
1906 struct lyd_node *d = NULL;
1907 uint32_t i = 0;
1908 LY_ERR ret = LY_SUCCESS;
1909
1910 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1911 if (diff) {
1912 *diff = NULL;
1913 }
1914 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001915 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001916 }
1917
1918 /* add nodes for each module one-by-one */
1919 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1920 if (!mod->implemented) {
1921 continue;
1922 }
1923
1924 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1925 if (d) {
1926 /* merge into one diff */
1927 lyd_insert_sibling(*diff, d, diff);
1928
1929 d = NULL;
1930 }
1931 }
1932
1933cleanup:
1934 if (ret && diff) {
1935 lyd_free_all(*diff);
1936 *diff = NULL;
1937 }
1938 return ret;
1939}
1940
1941API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001942lyd_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 +02001943{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001944 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001945 struct lyd_node *root, *d = NULL;
1946 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001947
1948 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1949 if (diff) {
1950 *diff = NULL;
1951 }
1952
1953 /* add all top-level defaults for this module */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001954 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01001955
1956 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001957 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001958
1959 /* process nested nodes */
1960 LY_LIST_FOR(*tree, root) {
1961 /* skip added default nodes */
1962 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1963 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1964
1965 if (d) {
1966 /* merge into one diff */
1967 lyd_insert_sibling(*diff, d, diff);
1968
1969 d = NULL;
1970 }
1971 }
1972 }
1973
1974cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001975 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001976 if (ret && diff) {
1977 lyd_free_all(*diff);
1978 *diff = NULL;
1979 }
1980 return ret;
1981}
1982
Michal Vasko90932a92020-02-12 14:33:03 +01001983struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001984lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001985{
Michal Vaskob104f112020-07-17 09:54:54 +02001986 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001987 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001988 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01001989 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01001990
Michal Vaskob104f112020-07-17 09:54:54 +02001991 assert(new_node);
1992
1993 if (!first_sibling || !new_node->schema) {
1994 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001995 return NULL;
1996 }
1997
Michal Vasko93b16062020-12-09 18:14:18 +01001998 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001999 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002000 getnext_opts = LYS_GETNEXT_OUTPUT;
2001 }
2002
Michal Vaskob104f112020-07-17 09:54:54 +02002003 if (first_sibling->parent && first_sibling->parent->children_ht) {
2004 /* find the anchor using hashes */
2005 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002006 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002007 while (schema) {
2008 /* keep trying to find the first existing instance of the closest following schema sibling,
2009 * otherwise return NULL - inserting at the end */
2010 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2011 break;
2012 }
2013
Michal Vasko93b16062020-12-09 18:14:18 +01002014 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002015 }
2016 } else {
2017 /* find the anchor without hashes */
2018 match = (struct lyd_node *)first_sibling;
2019 if (!lysc_data_parent(new_node->schema)) {
2020 /* we are in top-level, skip all the data from preceding modules */
2021 LY_LIST_FOR(match, match) {
2022 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2023 break;
2024 }
2025 }
2026 }
2027
2028 /* get the first schema sibling */
2029 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01002030 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002031
2032 found = 0;
2033 LY_LIST_FOR(match, match) {
2034 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2035 /* we have found an opaque node, which must be at the end, so use it OR
2036 * modules do not match, so we must have traversed all the data from new_node module (if any),
2037 * we have found the first node of the next module, that is what we want */
2038 break;
2039 }
2040
2041 /* skip schema nodes until we find the instantiated one */
2042 while (!found) {
2043 if (new_node->schema == schema) {
2044 /* we have found the schema of the new node, continue search to find the first
2045 * data node with a different schema (after our schema) */
2046 found = 1;
2047 break;
2048 }
2049 if (match->schema == schema) {
2050 /* current node (match) is a data node still before the new node, continue search in data */
2051 break;
2052 }
Michal Vasko93b16062020-12-09 18:14:18 +01002053 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002054 assert(schema);
2055 }
2056
2057 if (found && (match->schema != new_node->schema)) {
2058 /* find the next node after we have found our node schema data instance */
2059 break;
2060 }
2061 }
Michal Vasko90932a92020-02-12 14:33:03 +01002062 }
2063
2064 return match;
2065}
2066
2067/**
2068 * @brief Insert node after a sibling.
2069 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002070 * Handles inserting into NP containers and key-less lists.
2071 *
Michal Vasko90932a92020-02-12 14:33:03 +01002072 * @param[in] sibling Sibling to insert after.
2073 * @param[in] node Node to insert.
2074 */
2075static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002076lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002077{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002078 struct lyd_node_inner *par;
2079
Michal Vasko90932a92020-02-12 14:33:03 +01002080 assert(!node->next && (node->prev == node));
2081
2082 node->next = sibling->next;
2083 node->prev = sibling;
2084 sibling->next = node;
2085 if (node->next) {
2086 /* sibling had a succeeding node */
2087 node->next->prev = node;
2088 } else {
2089 /* sibling was last, find first sibling and change its prev */
2090 if (sibling->parent) {
2091 sibling = sibling->parent->child;
2092 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002093 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002094 }
2095 sibling->prev = node;
2096 }
2097 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002098
Michal Vasko9f96a052020-03-10 09:41:45 +01002099 for (par = node->parent; par; par = par->parent) {
2100 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2101 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002102 par->flags &= ~LYD_DEFAULT;
2103 }
Michal Vaskob104f112020-07-17 09:54:54 +02002104 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002105 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002106 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002107 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002108 }
Michal Vasko90932a92020-02-12 14:33:03 +01002109}
2110
2111/**
2112 * @brief Insert node before a sibling.
2113 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002114 * Handles inserting into NP containers and key-less lists.
2115 *
Michal Vasko90932a92020-02-12 14:33:03 +01002116 * @param[in] sibling Sibling to insert before.
2117 * @param[in] node Node to insert.
2118 */
2119static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002120lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002121{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002122 struct lyd_node_inner *par;
2123
Michal Vasko90932a92020-02-12 14:33:03 +01002124 assert(!node->next && (node->prev == node));
2125
2126 node->next = sibling;
2127 /* covers situation of sibling being first */
2128 node->prev = sibling->prev;
2129 sibling->prev = node;
2130 if (node->prev->next) {
2131 /* sibling had a preceding node */
2132 node->prev->next = node;
2133 } else if (sibling->parent) {
2134 /* sibling was first and we must also change parent child pointer */
2135 sibling->parent->child = node;
2136 }
2137 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002138
Michal Vasko9f96a052020-03-10 09:41:45 +01002139 for (par = node->parent; par; par = par->parent) {
2140 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2141 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002142 par->flags &= ~LYD_DEFAULT;
2143 }
Michal Vaskob104f112020-07-17 09:54:54 +02002144 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002145 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002146 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002147 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002148 }
Michal Vasko90932a92020-02-12 14:33:03 +01002149}
2150
2151/**
Michal Vaskob104f112020-07-17 09:54:54 +02002152 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002153 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002154 * Handles inserting into NP containers and key-less lists.
2155 *
Michal Vasko90932a92020-02-12 14:33:03 +01002156 * @param[in] parent Parent to insert into.
2157 * @param[in] node Node to insert.
2158 */
2159static void
Michal Vaskob104f112020-07-17 09:54:54 +02002160lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002161{
2162 struct lyd_node_inner *par;
2163
Radek Krejcia1c1e542020-09-29 16:06:52 +02002164 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002165 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002166
2167 par = (struct lyd_node_inner *)parent;
2168
Michal Vaskob104f112020-07-17 09:54:54 +02002169 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002170 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002171
Michal Vaskod989ba02020-08-24 10:59:24 +02002172 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002173 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2174 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002175 par->flags &= ~LYD_DEFAULT;
2176 }
Michal Vasko52927e22020-03-16 17:26:14 +01002177 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002178 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002179 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002180 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002181 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002182}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002183
Michal Vasko751cb4d2020-07-14 12:25:28 +02002184/**
2185 * @brief Learn whether a list instance has all the keys.
2186 *
2187 * @param[in] list List instance to check.
2188 * @return non-zero if all the keys were found,
2189 * @return 0 otherwise.
2190 */
2191static int
2192lyd_insert_has_keys(const struct lyd_node *list)
2193{
2194 const struct lyd_node *key;
2195 const struct lysc_node *skey = NULL;
2196
2197 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002198 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002199 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2200 if (!key || (key->schema != skey)) {
2201 /* key missing */
2202 return 0;
2203 }
2204
2205 key = key->next;
2206 }
2207
2208 /* all keys found */
2209 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002210}
2211
2212void
Michal Vaskob104f112020-07-17 09:54:54 +02002213lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002214{
Michal Vaskob104f112020-07-17 09:54:54 +02002215 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002216
Michal Vaskob104f112020-07-17 09:54:54 +02002217 /* inserting list without its keys is not supported */
2218 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002219 assert(!parent || !parent->schema ||
2220 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002221
Michal Vaskob104f112020-07-17 09:54:54 +02002222 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002223 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002224 }
Michal Vasko90932a92020-02-12 14:33:03 +01002225
Michal Vaskob104f112020-07-17 09:54:54 +02002226 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002227 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002228
Michal Vaskob104f112020-07-17 09:54:54 +02002229 /* find the anchor, our next node, so we can insert before it */
2230 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2231 if (anchor) {
2232 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002233 if (!parent && (*first_sibling_p == anchor)) {
2234 /* move first sibling */
2235 *first_sibling_p = node;
2236 }
Michal Vaskob104f112020-07-17 09:54:54 +02002237 } else if (first_sibling) {
2238 lyd_insert_after_node(first_sibling->prev, node);
2239 } else if (parent) {
2240 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002241 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002242 *first_sibling_p = node;
2243 }
2244
2245 /* insert into parent HT */
2246 lyd_insert_hash(node);
2247
2248 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002249 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002250 lyd_hash(parent);
2251
2252 /* now we can insert even the list into its parent HT */
2253 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002254 }
Michal Vasko90932a92020-02-12 14:33:03 +01002255}
2256
Michal Vasko717a4c32020-12-07 09:40:10 +01002257/**
2258 * @brief Check schema place of a node to be inserted.
2259 *
2260 * @param[in] parent Schema node of the parent data node.
2261 * @param[in] sibling Schema node of a sibling data node.
2262 * @param[in] schema Schema node if the data node to be inserted.
2263 * @return LY_SUCCESS on success.
2264 * @return LY_EINVAL if the place is invalid.
2265 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002266static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002267lyd_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 +01002268{
2269 const struct lysc_node *par2;
2270
Michal Vasko62ed12d2020-05-21 10:08:25 +02002271 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002272 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2273 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002274
Michal Vasko717a4c32020-12-07 09:40:10 +01002275 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002276 /* opaque nodes can be inserted wherever */
2277 return LY_SUCCESS;
2278 }
2279
Michal Vasko717a4c32020-12-07 09:40:10 +01002280 if (!parent) {
2281 parent = lysc_data_parent(sibling);
2282 }
2283
Michal Vaskof03ed032020-03-04 13:31:44 +01002284 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002285 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002286
2287 if (parent) {
2288 /* inner node */
2289 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002290 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002291 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002292 return LY_EINVAL;
2293 }
2294 } else {
2295 /* top-level node */
2296 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002297 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002298 return LY_EINVAL;
2299 }
2300 }
2301
2302 return LY_SUCCESS;
2303}
2304
2305API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002306lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002307{
2308 struct lyd_node *iter;
2309
Michal Vasko0ab974d2021-02-24 13:18:26 +01002310 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002311
Michal Vasko717a4c32020-12-07 09:40:10 +01002312 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002313
Michal Vasko0ab974d2021-02-24 13:18:26 +01002314 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002315 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2316 return LY_EINVAL;
2317 }
2318
2319 if (node->parent || node->prev->next) {
2320 lyd_unlink_tree(node);
2321 }
2322
2323 while (node) {
2324 iter = node->next;
2325 lyd_unlink_tree(node);
2326 lyd_insert_node(parent, NULL, node);
2327 node = iter;
2328 }
2329 return LY_SUCCESS;
2330}
2331
2332API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002333lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002334{
2335 struct lyd_node *iter;
2336
Michal Vaskob104f112020-07-17 09:54:54 +02002337 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002338
Michal Vaskob104f112020-07-17 09:54:54 +02002339 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002340 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002341 }
2342
Michal Vasko553d0b52020-12-04 16:27:52 +01002343 if (sibling == node) {
2344 /* we need to keep the connection to siblings so we can insert into them */
2345 sibling = sibling->prev;
2346 }
2347
Michal Vaskob1b5c262020-03-05 14:29:47 +01002348 if (node->parent || node->prev->next) {
2349 lyd_unlink_tree(node);
2350 }
2351
2352 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002353 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002354 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002355 return LY_EINVAL;
2356 }
2357
Michal Vaskob1b5c262020-03-05 14:29:47 +01002358 iter = node->next;
2359 lyd_unlink_tree(node);
2360 lyd_insert_node(NULL, &sibling, node);
2361 node = iter;
2362 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002363
Michal Vaskob104f112020-07-17 09:54:54 +02002364 if (first) {
2365 /* find the first sibling */
2366 *first = sibling;
2367 while ((*first)->prev->next) {
2368 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002369 }
2370 }
2371
2372 return LY_SUCCESS;
2373}
2374
Michal Vaskob1b5c262020-03-05 14:29:47 +01002375API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002376lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2377{
Michal Vaskof03ed032020-03-04 13:31:44 +01002378 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2379
Michal Vasko717a4c32020-12-07 09:40:10 +01002380 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002381
Michal Vaskob104f112020-07-17 09:54:54 +02002382 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002383 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002384 return LY_EINVAL;
2385 }
2386
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002387 lyd_unlink_tree(node);
2388 lyd_insert_before_node(sibling, node);
2389 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002390
Michal Vaskof03ed032020-03-04 13:31:44 +01002391 return LY_SUCCESS;
2392}
2393
2394API LY_ERR
2395lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2396{
Michal Vaskof03ed032020-03-04 13:31:44 +01002397 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2398
Michal Vasko717a4c32020-12-07 09:40:10 +01002399 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002400
Michal Vaskob104f112020-07-17 09:54:54 +02002401 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002402 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002403 return LY_EINVAL;
2404 }
2405
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002406 lyd_unlink_tree(node);
2407 lyd_insert_after_node(sibling, node);
2408 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002409
Michal Vaskof03ed032020-03-04 13:31:44 +01002410 return LY_SUCCESS;
2411}
2412
2413API void
2414lyd_unlink_tree(struct lyd_node *node)
2415{
2416 struct lyd_node *iter;
2417
2418 if (!node) {
2419 return;
2420 }
2421
Michal Vaskob104f112020-07-17 09:54:54 +02002422 /* update hashes while still linked into the tree */
2423 lyd_unlink_hash(node);
2424
Michal Vaskof03ed032020-03-04 13:31:44 +01002425 /* unlink from siblings */
2426 if (node->prev->next) {
2427 node->prev->next = node->next;
2428 }
2429 if (node->next) {
2430 node->next->prev = node->prev;
2431 } else {
2432 /* unlinking the last node */
2433 if (node->parent) {
2434 iter = node->parent->child;
2435 } else {
2436 iter = node->prev;
2437 while (iter->prev != node) {
2438 iter = iter->prev;
2439 }
2440 }
2441 /* update the "last" pointer from the first node */
2442 iter->prev = node->prev;
2443 }
2444
2445 /* unlink from parent */
2446 if (node->parent) {
2447 if (node->parent->child == node) {
2448 /* the node is the first child */
2449 node->parent->child = node->next;
2450 }
2451
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002452 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002453 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2454 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002455 LY_LIST_FOR(node->parent->child, iter) {
2456 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2457 break;
2458 }
2459 }
2460 if (!iter) {
2461 node->parent->flags |= LYD_DEFAULT;
2462 }
2463 }
2464
Michal Vaskof03ed032020-03-04 13:31:44 +01002465 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002466 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002467 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002468 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002469 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002470 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002471 }
2472 }
2473
2474 node->parent = NULL;
2475 }
2476
2477 node->next = NULL;
2478 node->prev = node;
2479}
2480
Michal Vaskoa5da3292020-08-12 13:10:50 +02002481void
Michal Vasko871a0252020-11-11 18:35:24 +01002482lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002483{
2484 struct lyd_meta *last, *iter;
2485
2486 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002487
2488 if (!meta) {
2489 return;
2490 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002491
2492 for (iter = meta; iter; iter = iter->next) {
2493 iter->parent = parent;
2494 }
2495
2496 /* insert as the last attribute */
2497 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002498 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002499 last->next = meta;
2500 } else {
2501 parent->meta = meta;
2502 }
2503
2504 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002505 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002506 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002507 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002508 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002509}
2510
2511LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002512lyd_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 +02002513 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 +01002514 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002515{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002516 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002517 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002518 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002519 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002520
Michal Vasko9f96a052020-03-10 09:41:45 +01002521 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002522
Radek Krejciddace2c2021-01-08 11:30:56 +01002523 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002524
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002525 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002526 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002527 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002528 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002529 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002530 break;
2531 }
2532 }
2533 if (!ant) {
2534 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002535 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002536 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002537 ret = LY_EINVAL;
2538 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002539 }
2540
Michal Vasko9f96a052020-03-10 09:41:45 +01002541 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002542 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002543 mt->parent = parent;
2544 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002545 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2546 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002547 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2548 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2549 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002550
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002551 /* insert as the last attribute */
2552 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002553 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002554 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002555 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002556 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002557 }
2558
Michal Vasko9f96a052020-03-10 09:41:45 +01002559 if (meta) {
2560 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002561 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002562
2563cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002564 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002565 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002566}
2567
Michal Vaskoa5da3292020-08-12 13:10:50 +02002568void
2569lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2570{
2571 struct lyd_attr *last, *iter;
2572 struct lyd_node_opaq *opaq;
2573
2574 assert(parent && !parent->schema);
2575
2576 if (!attr) {
2577 return;
2578 }
2579
2580 opaq = (struct lyd_node_opaq *)parent;
2581 for (iter = attr; iter; iter = iter->next) {
2582 iter->parent = opaq;
2583 }
2584
2585 /* insert as the last attribute */
2586 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002587 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002588 last->next = attr;
2589 } else {
2590 opaq->attr = attr;
2591 }
2592}
2593
Michal Vasko52927e22020-03-16 17:26:14 +01002594LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002595lyd_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 +01002596 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2597 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 +01002598{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002599 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002600 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002601
2602 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002603 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002604
2605 if (!value_len) {
2606 value = "";
2607 }
2608
2609 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002610 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002611
Michal Vaskoad92b672020-11-12 13:11:31 +01002612 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002613 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002614 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002615 }
2616 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002617 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002618 }
2619
Michal Vasko52927e22020-03-16 17:26:14 +01002620 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002621 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2622 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002623 *dynamic = 0;
2624 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002625 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002626 }
Michal Vasko501af032020-11-11 20:27:44 +01002627 at->format = format;
2628 at->val_prefix_data = val_prefix_data;
2629 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002630
2631 /* insert as the last attribute */
2632 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002633 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002634 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002635 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002636 last->next = at;
2637 }
2638
Radek Krejci011e4aa2020-09-04 15:22:31 +02002639finish:
2640 if (ret) {
2641 lyd_free_attr_single(ctx, at);
2642 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002643 *attr = at;
2644 }
2645 return LY_SUCCESS;
2646}
2647
Radek Krejci084289f2019-07-09 17:35:30 +02002648API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002649lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002650{
Michal Vasko004d3152020-06-11 19:59:22 +02002651 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002652
Michal Vasko004d3152020-06-11 19:59:22 +02002653 if (ly_path_eval(path, tree, &target)) {
2654 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002655 }
2656
Michal Vasko004d3152020-06-11 19:59:22 +02002657 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002658}
2659
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002660API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002661lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002662{
2663 const struct lyd_node *iter1, *iter2;
2664 struct lyd_node_term *term1, *term2;
2665 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002666 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002667 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002668
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002669 if (!node1 || !node2) {
2670 if (node1 == node2) {
2671 return LY_SUCCESS;
2672 } else {
2673 return LY_ENOT;
2674 }
2675 }
2676
Michal Vaskob7be7a82020-08-20 09:09:04 +02002677 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002678 return LY_ENOT;
2679 }
2680
2681 if (node1->hash != node2->hash) {
2682 return LY_ENOT;
2683 }
Michal Vasko52927e22020-03-16 17:26:14 +01002684 /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002685
Michal Vasko52927e22020-03-16 17:26:14 +01002686 if (!node1->schema) {
2687 opaq1 = (struct lyd_node_opaq *)node1;
2688 opaq2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +01002689 if ((opaq1->name.name != opaq2->name.name) || (opaq1->format != opaq2->format) ||
2690 (opaq1->name.module_ns != opaq2->name.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002691 return LY_ENOT;
2692 }
Michal Vasko52927e22020-03-16 17:26:14 +01002693 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002694 case LY_PREF_XML:
2695 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, opaq2->value, opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002696 return LY_ENOT;
2697 }
2698 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002699 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002700 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2701 if (strcmp(opaq1->value, opaq2->value)) {
2702 return LY_ENOT;
2703 }
2704 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002705 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002706 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002707 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002708 return LY_EINT;
2709 }
2710 if (options & LYD_COMPARE_FULL_RECURSION) {
2711 iter1 = opaq1->child;
2712 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002713 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002714 }
2715 return LY_SUCCESS;
2716 } else {
2717 switch (node1->schema->nodetype) {
2718 case LYS_LEAF:
2719 case LYS_LEAFLIST:
2720 if (options & LYD_COMPARE_DEFAULTS) {
2721 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2722 return LY_ENOT;
2723 }
2724 }
2725
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002726 term1 = (struct lyd_node_term *)node1;
2727 term2 = (struct lyd_node_term *)node2;
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002728 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002729 case LYS_CONTAINER:
2730 if (options & LYD_COMPARE_DEFAULTS) {
2731 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2732 return LY_ENOT;
2733 }
2734 }
2735 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002736 iter1 = lyd_child(node1);
2737 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002738 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002739 }
2740 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002741 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002742 case LYS_ACTION:
2743 if (options & LYD_COMPARE_FULL_RECURSION) {
2744 /* TODO action/RPC
2745 goto all_children_compare;
2746 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002747 }
2748 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002749 case LYS_NOTIF:
2750 if (options & LYD_COMPARE_FULL_RECURSION) {
2751 /* TODO Notification
2752 goto all_children_compare;
2753 */
2754 }
2755 return LY_SUCCESS;
2756 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01002757 iter1 = lyd_child(node1);
2758 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002759
2760 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2761 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01002762 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002763 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002764 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002765 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002766 return LY_ENOT;
2767 }
2768 iter1 = iter1->next;
2769 iter2 = iter2->next;
2770 }
2771 } else {
2772 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2773
Radek Krejci0f969882020-08-21 16:56:47 +02002774all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002775 if (!iter1 && !iter2) {
2776 /* no children, nothing to compare */
2777 return LY_SUCCESS;
2778 }
2779
Michal Vaskod989ba02020-08-24 10:59:24 +02002780 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002781 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002782 return LY_ENOT;
2783 }
2784 }
2785 if (iter1 || iter2) {
2786 return LY_ENOT;
2787 }
2788 }
2789 return LY_SUCCESS;
2790 case LYS_ANYXML:
2791 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002792 any1 = (struct lyd_node_any *)node1;
2793 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002794
2795 if (any1->value_type != any2->value_type) {
2796 return LY_ENOT;
2797 }
2798 switch (any1->value_type) {
2799 case LYD_ANYDATA_DATATREE:
2800 iter1 = any1->value.tree;
2801 iter2 = any2->value.tree;
2802 goto all_children_compare;
2803 case LYD_ANYDATA_STRING:
2804 case LYD_ANYDATA_XML:
2805 case LYD_ANYDATA_JSON:
2806 len1 = strlen(any1->value.str);
2807 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002808 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002809 return LY_ENOT;
2810 }
2811 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002812 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002813 len1 = lyd_lyb_data_length(any1->value.mem);
2814 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002815 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002816 return LY_ENOT;
2817 }
2818 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002819 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002820 }
2821 }
2822
Michal Vaskob7be7a82020-08-20 09:09:04 +02002823 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002824 return LY_EINT;
2825}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002826
Michal Vasko21725742020-06-29 11:49:25 +02002827API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002828lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002829{
Michal Vaskod989ba02020-08-24 10:59:24 +02002830 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002831 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2832 }
2833
Michal Vasko11a81432020-07-28 16:31:29 +02002834 if (node1 == node2) {
2835 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002836 }
Michal Vasko11a81432020-07-28 16:31:29 +02002837 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002838}
2839
2840API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002841lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2842{
2843 if (!meta1 || !meta2) {
2844 if (meta1 == meta2) {
2845 return LY_SUCCESS;
2846 } else {
2847 return LY_ENOT;
2848 }
2849 }
2850
Michal Vaskoa8083062020-11-06 17:22:10 +01002851 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002852 return LY_ENOT;
2853 }
2854
Michal Vasko21725742020-06-29 11:49:25 +02002855 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2856}
2857
Radek Krejci22ebdba2019-07-25 13:59:43 +02002858/**
Michal Vasko52927e22020-03-16 17:26:14 +01002859 * @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 +02002860 *
2861 * 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 +02002862 *
2863 * @param[in] node Original node to duplicate
2864 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2865 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2866 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2867 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2868 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002869 */
Michal Vasko52927e22020-03-16 17:26:14 +01002870static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002871lyd_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 +02002872 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002873{
Michal Vasko52927e22020-03-16 17:26:14 +01002874 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002875 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002876 struct lyd_meta *meta;
2877 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002878
Michal Vasko52927e22020-03-16 17:26:14 +01002879 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002880
Michal Vasko52927e22020-03-16 17:26:14 +01002881 if (!node->schema) {
2882 dup = calloc(1, sizeof(struct lyd_node_opaq));
2883 } else {
2884 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002885 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002886 case LYS_ACTION:
2887 case LYS_NOTIF:
2888 case LYS_CONTAINER:
2889 case LYS_LIST:
2890 dup = calloc(1, sizeof(struct lyd_node_inner));
2891 break;
2892 case LYS_LEAF:
2893 case LYS_LEAFLIST:
2894 dup = calloc(1, sizeof(struct lyd_node_term));
2895 break;
2896 case LYS_ANYDATA:
2897 case LYS_ANYXML:
2898 dup = calloc(1, sizeof(struct lyd_node_any));
2899 break;
2900 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002901 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002902 ret = LY_EINT;
2903 goto error;
2904 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002905 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002906 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002907
Michal Vaskof6df0a02020-06-16 13:08:34 +02002908 if (options & LYD_DUP_WITH_FLAGS) {
2909 dup->flags = node->flags;
2910 } else {
2911 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2912 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002913 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002914 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002915
Michal Vasko25a32822020-07-09 15:48:22 +02002916 /* duplicate metadata */
2917 if (!(options & LYD_DUP_NO_META)) {
2918 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002919 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002920 }
2921 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002922
2923 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002924 if (!dup->schema) {
2925 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2926 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2927 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002928
2929 if (options & LYD_DUP_RECURSIVE) {
2930 /* duplicate all the children */
2931 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002932 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002933 }
2934 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002935 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002936 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01002937 if (orig->name.prefix) {
2938 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002939 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002940 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 +01002941 if (orig->val_prefix_data) {
2942 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
2943 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002944 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002945 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002946 opaq->ctx = orig->ctx;
2947 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2948 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2949 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2950
2951 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002952 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002953 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002954 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2955 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2956 struct lyd_node *child;
2957
2958 if (options & LYD_DUP_RECURSIVE) {
2959 /* duplicate all the children */
2960 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002961 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002962 }
Michal Vasko69730152020-10-09 16:30:07 +02002963 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002964 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002965 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01002966 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002967 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002968 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002969 if (!child) {
2970 /* possibly not keys are present in filtered tree */
2971 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002972 } else if (child->schema != key) {
2973 /* possibly not all keys are present in filtered tree,
2974 * but there can be also some non-key nodes */
2975 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002976 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002977 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002978 child = child->next;
2979 }
2980 }
2981 lyd_hash(dup);
2982 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002983 dup->hash = node->hash;
2984 any = (struct lyd_node_any *)node;
2985 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002986 }
2987
Michal Vasko52927e22020-03-16 17:26:14 +01002988 /* insert */
2989 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002990
2991 if (dup_p) {
2992 *dup_p = dup;
2993 }
2994 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002995
2996error:
Michal Vasko52927e22020-03-16 17:26:14 +01002997 lyd_free_tree(dup);
2998 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002999}
3000
Michal Vasko3a41dff2020-07-15 14:30:28 +02003001static LY_ERR
3002lyd_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 +02003003 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003004{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003005 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003006 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003007
3008 *dup_parent = NULL;
3009 *local_parent = NULL;
3010
3011 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3012 if (parent && (parent->schema == orig_parent->schema)) {
3013 /* stop creating parents, connect what we have into the provided parent */
3014 iter = parent;
3015 repeat = 0;
3016 } else {
3017 iter = NULL;
3018 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02003019 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003020 }
3021 if (!*local_parent) {
3022 *local_parent = (struct lyd_node_inner *)iter;
3023 }
3024 if (iter->child) {
3025 /* 1) list - add after keys
3026 * 2) provided parent with some children */
3027 iter->child->prev->next = *dup_parent;
3028 if (*dup_parent) {
3029 (*dup_parent)->prev = iter->child->prev;
3030 iter->child->prev = *dup_parent;
3031 }
3032 } else {
3033 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3034 }
3035 if (*dup_parent) {
3036 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3037 }
3038 *dup_parent = (struct lyd_node *)iter;
3039 }
3040
3041 if (repeat && parent) {
3042 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003043 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003044 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003045 return LY_EINVAL;
3046 }
3047
3048 return LY_SUCCESS;
3049}
3050
3051static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003052lyd_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 +02003053{
3054 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003055 const struct lyd_node *orig; /* original node to be duplicated */
3056 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003057 struct lyd_node *top = NULL; /* the most higher created node */
3058 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003059
Michal Vasko3a41dff2020-07-15 14:30:28 +02003060 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003061
3062 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003063 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003064 } else {
3065 local_parent = parent;
3066 }
3067
Radek Krejci22ebdba2019-07-25 13:59:43 +02003068 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003069 if (lysc_is_key(orig->schema)) {
3070 if (local_parent) {
3071 /* the key must already exist in the parent */
3072 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3073 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3074 } else {
3075 assert(!(options & LYD_DUP_WITH_PARENTS));
3076 /* duplicating a single key, okay, I suppose... */
3077 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3078 LY_CHECK_GOTO(rc, error);
3079 }
3080 } else {
3081 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003082 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003083 LY_CHECK_GOTO(rc, error);
3084 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003085 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003086 break;
3087 }
3088 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003089
3090 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003091 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003092 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003093 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003094 }
3095 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003096
3097 if (dup) {
3098 *dup = first;
3099 }
3100 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003101
3102error:
3103 if (top) {
3104 lyd_free_tree(top);
3105 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003106 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003107 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003108 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003109}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003110
Michal Vasko3a41dff2020-07-15 14:30:28 +02003111API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003112lyd_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 +02003113{
3114 return lyd_dup(node, parent, options, 1, dup);
3115}
3116
3117API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003118lyd_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 +02003119{
3120 return lyd_dup(node, parent, options, 0, dup);
3121}
3122
3123API LY_ERR
3124lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003125{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003126 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003127 struct lyd_meta *mt, *last;
3128
Michal Vasko3a41dff2020-07-15 14:30:28 +02003129 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003130
3131 /* create a copy */
3132 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003133 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003134 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003135 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003136 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3137 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003138
3139 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003140 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003141 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003142 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003143 last->next = mt;
3144 } else {
3145 node->meta = mt;
3146 }
3147
Radek Krejci011e4aa2020-09-04 15:22:31 +02003148finish:
3149 if (ret) {
3150 lyd_free_meta_single(mt);
3151 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003152 *dup = mt;
3153 }
3154 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003155}
3156
Michal Vasko4490d312020-06-16 13:08:55 +02003157/**
3158 * @brief Merge a source sibling into target siblings.
3159 *
3160 * @param[in,out] first_trg First target sibling, is updated if top-level.
3161 * @param[in] parent_trg Target parent.
3162 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3163 * @param[in] options Merge options.
3164 * @return LY_ERR value.
3165 */
3166static LY_ERR
3167lyd_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 +02003168 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003169{
3170 LY_ERR ret;
3171 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003172 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003173 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003174
3175 sibling_src = *sibling_src_p;
3176 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3177 /* try to find the exact instance */
3178 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3179 } else {
3180 /* try to simply find the node, there cannot be more instances */
3181 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3182 }
3183
3184 if (!ret) {
3185 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003186 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003187 /* since they are different, they cannot both be default */
3188 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3189
Michal Vasko3a41dff2020-07-15 14:30:28 +02003190 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3191 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003192 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003193 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3194 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003195 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003196
3197 /* copy flags and add LYD_NEW */
3198 match_trg->flags = sibling_src->flags | LYD_NEW;
3199 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003200 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003201 /* update value */
3202 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003203 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003204
3205 /* copy flags and add LYD_NEW */
3206 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003207 } else {
3208 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003209 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003210 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 +02003211 }
3212 }
3213 } else {
3214 /* node not found, merge it */
3215 if (options & LYD_MERGE_DESTRUCT) {
3216 dup_src = (struct lyd_node *)sibling_src;
3217 lyd_unlink_tree(dup_src);
3218 /* spend it */
3219 *sibling_src_p = NULL;
3220 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003221 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 +02003222 }
3223
3224 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003225 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003226 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003227 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003228 }
3229
3230 lyd_insert_node(parent_trg, first_trg, dup_src);
3231 }
3232
3233 return LY_SUCCESS;
3234}
3235
Michal Vasko3a41dff2020-07-15 14:30:28 +02003236static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003237lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003238{
3239 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003240 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003241
3242 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3243
3244 if (!source) {
3245 /* nothing to merge */
3246 return LY_SUCCESS;
3247 }
3248
Michal Vasko831422b2020-11-24 11:20:51 +01003249 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003250 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003251 return LY_EINVAL;
3252 }
3253
3254 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003255 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003256 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3257 if (first && !sibling_src) {
3258 /* source was spent (unlinked), move to the next node */
3259 source = tmp;
3260 }
3261
Michal Vasko3a41dff2020-07-15 14:30:28 +02003262 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003263 break;
3264 }
3265 }
3266
3267 if (options & LYD_MERGE_DESTRUCT) {
3268 /* free any leftover source data that were not merged */
3269 lyd_free_siblings((struct lyd_node *)source);
3270 }
3271
3272 return LY_SUCCESS;
3273}
3274
Michal Vasko3a41dff2020-07-15 14:30:28 +02003275API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003276lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003277{
3278 return lyd_merge(target, source, options, 1);
3279}
3280
3281API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003282lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003283{
3284 return lyd_merge(target, source, options, 0);
3285}
3286
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003287static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003288lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003289{
Michal Vasko14654712020-02-06 08:35:21 +01003290 /* ending \0 */
3291 ++reqlen;
3292
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003293 if (reqlen > *buflen) {
3294 if (is_static) {
3295 return LY_EINCOMPLETE;
3296 }
3297
3298 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3299 if (!*buffer) {
3300 return LY_EMEM;
3301 }
3302
3303 *buflen = reqlen;
3304 }
3305
3306 return LY_SUCCESS;
3307}
3308
Michal Vaskod59035b2020-07-08 12:00:06 +02003309LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003310lyd_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 +02003311{
3312 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003313 size_t len;
3314 const char *val;
3315 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003316
Radek Krejcia1c1e542020-09-29 16:06:52 +02003317 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003318 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003319 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003320 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003321
3322 quot = '\'';
3323 if (strchr(val, '\'')) {
3324 quot = '"';
3325 }
3326 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003327 }
3328
3329 return LY_SUCCESS;
3330}
3331
3332/**
3333 * @brief Append leaf-list value predicate to path.
3334 *
3335 * @param[in] node Node to print.
3336 * @param[in,out] buffer Buffer to print to.
3337 * @param[in,out] buflen Current buffer length.
3338 * @param[in,out] bufused Current number of characters used in @p buffer.
3339 * @param[in] is_static Whether buffer is static or can be reallocated.
3340 * @return LY_ERR
3341 */
3342static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003343lyd_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 +02003344{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003345 size_t len;
3346 const char *val;
3347 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003348
Michal Vaskob7be7a82020-08-20 09:09:04 +02003349 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003350 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003351 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003352
3353 quot = '\'';
3354 if (strchr(val, '\'')) {
3355 quot = '"';
3356 }
3357 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3358
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003359 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003360}
3361
3362/**
3363 * @brief Append node position (relative to its other instances) predicate to path.
3364 *
3365 * @param[in] node Node to print.
3366 * @param[in,out] buffer Buffer to print to.
3367 * @param[in,out] buflen Current buffer length.
3368 * @param[in,out] bufused Current number of characters used in @p buffer.
3369 * @param[in] is_static Whether buffer is static or can be reallocated.
3370 * @return LY_ERR
3371 */
3372static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003373lyd_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 +02003374{
3375 const struct lyd_node *first, *iter;
3376 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003377 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003378 char *val = NULL;
3379 LY_ERR rc;
3380
3381 if (node->parent) {
3382 first = node->parent->child;
3383 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003384 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003385 }
3386 pos = 1;
3387 for (iter = first; iter != node; iter = iter->next) {
3388 if (iter->schema == node->schema) {
3389 ++pos;
3390 }
3391 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003392 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003393 return LY_EMEM;
3394 }
3395
3396 len = 1 + strlen(val) + 1;
3397 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3398 if (rc != LY_SUCCESS) {
3399 goto cleanup;
3400 }
3401
3402 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3403
3404cleanup:
3405 free(val);
3406 return rc;
3407}
3408
3409API char *
3410lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3411{
Radek Krejci857189e2020-09-01 13:26:36 +02003412 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003413 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003414 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003415 const struct lyd_node *iter;
3416 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003417 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003418
3419 LY_CHECK_ARG_RET(NULL, node, NULL);
3420 if (buffer) {
3421 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3422 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003423 } else {
3424 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003425 }
3426
3427 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003428 case LYD_PATH_STD:
3429 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003430 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003431 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003432 ++depth;
3433 }
3434
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003435 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003436 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003437 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003438 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003439iter_print:
3440 /* print prefix and name */
3441 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003442 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003443 mod = iter->schema->module;
3444 }
3445
3446 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003447 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3448 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003449 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3450 if (rc != LY_SUCCESS) {
3451 break;
3452 }
3453
3454 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003455 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003456 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003457
Michal Vasko790b2bc2020-08-03 13:35:06 +02003458 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003459 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003460 switch (iter->schema->nodetype) {
3461 case LYS_LIST:
3462 if (iter->schema->flags & LYS_KEYLESS) {
3463 /* print its position */
3464 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3465 } else {
3466 /* print all list keys in predicates */
3467 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3468 }
3469 break;
3470 case LYS_LEAFLIST:
3471 if (iter->schema->flags & LYS_CONFIG_W) {
3472 /* print leaf-list value */
3473 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3474 } else {
3475 /* print its position */
3476 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3477 }
3478 break;
3479 default:
3480 /* nothing to print more */
3481 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003482 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003483 }
3484 if (rc != LY_SUCCESS) {
3485 break;
3486 }
3487
Michal Vasko14654712020-02-06 08:35:21 +01003488 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003489 }
3490 break;
3491 }
3492
3493 return buffer;
3494}
Michal Vaskoe444f752020-02-10 12:20:06 +01003495
Michal Vasko25a32822020-07-09 15:48:22 +02003496API struct lyd_meta *
3497lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3498{
3499 struct lyd_meta *ret = NULL;
3500 const struct ly_ctx *ctx;
3501 const char *prefix, *tmp;
3502 char *str;
3503 size_t pref_len, name_len;
3504
3505 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3506
3507 if (!first) {
3508 return NULL;
3509 }
3510
3511 ctx = first->annotation->module->ctx;
3512
3513 /* parse the name */
3514 tmp = name;
3515 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3516 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3517 return NULL;
3518 }
3519
3520 /* find the module */
3521 if (prefix) {
3522 str = strndup(prefix, pref_len);
3523 module = ly_ctx_get_module_latest(ctx, str);
3524 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003525 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 +02003526 }
3527
3528 /* find the metadata */
3529 LY_LIST_FOR(first, first) {
3530 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3531 ret = (struct lyd_meta *)first;
3532 break;
3533 }
3534 }
3535
3536 return ret;
3537}
3538
Michal Vasko9b368d32020-02-14 13:53:31 +01003539API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003540lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3541{
3542 struct lyd_node **match_p;
3543 struct lyd_node_inner *parent;
3544
Michal Vaskof03ed032020-03-04 13:31:44 +01003545 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003546
Michal Vasko6da1e952020-12-09 18:14:38 +01003547 if (!siblings || (siblings->schema && target->schema &&
3548 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003549 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003550 if (match) {
3551 *match = NULL;
3552 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003553 return LY_ENOTFOUND;
3554 }
3555
3556 /* find first sibling */
3557 if (siblings->parent) {
3558 siblings = siblings->parent->child;
3559 } else {
3560 while (siblings->prev->next) {
3561 siblings = siblings->prev;
3562 }
3563 }
3564
Michal Vasko9e685082021-01-29 14:49:09 +01003565 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003566 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003567 assert(target->hash);
3568
3569 /* find by hash */
3570 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003571 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003572 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003573 siblings = *match_p;
3574 } else {
3575 siblings = NULL;
3576 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003577 } else {
3578 /* not found */
3579 siblings = NULL;
3580 }
3581 } else {
3582 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003583 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003584 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003585 break;
3586 }
3587 }
3588 }
3589
3590 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003591 if (match) {
3592 *match = NULL;
3593 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003594 return LY_ENOTFOUND;
3595 }
3596
Michal Vasko9b368d32020-02-14 13:53:31 +01003597 if (match) {
3598 *match = (struct lyd_node *)siblings;
3599 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003600 return LY_SUCCESS;
3601}
3602
Radek Krejci857189e2020-09-01 13:26:36 +02003603/**
3604 * @brief Comparison callback to match schema node with a schema of a data node.
3605 *
3606 * @param[in] val1_p Pointer to the schema node
3607 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003608 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003609 */
3610static ly_bool
3611lyd_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 +01003612{
3613 struct lysc_node *val1;
3614 struct lyd_node *val2;
3615
3616 val1 = *((struct lysc_node **)val1_p);
3617 val2 = *((struct lyd_node **)val2_p);
3618
Michal Vasko90932a92020-02-12 14:33:03 +01003619 if (val1 == val2->schema) {
3620 /* schema match is enough */
3621 return 1;
3622 } else {
3623 return 0;
3624 }
3625}
3626
Michal Vasko92239c72020-11-18 18:17:25 +01003627/**
3628 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3629 * Uses hashes - should be used whenever possible for best performance.
3630 *
3631 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3632 * @param[in] schema Target data node schema to find.
3633 * @param[out] match Can be NULL, otherwise the found data node.
3634 * @return LY_SUCCESS on success, @p match set.
3635 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3636 * @return LY_ERR value if another error occurred.
3637 */
Michal Vasko90932a92020-02-12 14:33:03 +01003638static LY_ERR
3639lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3640{
3641 struct lyd_node **match_p;
3642 struct lyd_node_inner *parent;
3643 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003644 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003645
Michal Vaskob104f112020-07-17 09:54:54 +02003646 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003647
Michal Vasko9e685082021-01-29 14:49:09 +01003648 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003649 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003650 /* calculate our hash */
3651 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3652 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3653 hash = dict_hash_multi(hash, NULL, 0);
3654
3655 /* use special hash table function */
3656 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3657
3658 /* find by hash */
3659 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3660 siblings = *match_p;
3661 } else {
3662 /* not found */
3663 siblings = NULL;
3664 }
3665
3666 /* set the original hash table compare function back */
3667 lyht_set_cb(parent->children_ht, ht_cb);
3668 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003669 /* find first sibling */
3670 if (siblings->parent) {
3671 siblings = siblings->parent->child;
3672 } else {
3673 while (siblings->prev->next) {
3674 siblings = siblings->prev;
3675 }
3676 }
3677
3678 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003679 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003680 if (siblings->schema == schema) {
3681 /* schema match is enough */
3682 break;
3683 }
3684 }
3685 }
3686
3687 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003688 if (match) {
3689 *match = NULL;
3690 }
Michal Vasko90932a92020-02-12 14:33:03 +01003691 return LY_ENOTFOUND;
3692 }
3693
Michal Vasko9b368d32020-02-14 13:53:31 +01003694 if (match) {
3695 *match = (struct lyd_node *)siblings;
3696 }
Michal Vasko90932a92020-02-12 14:33:03 +01003697 return LY_SUCCESS;
3698}
3699
Michal Vaskoe444f752020-02-10 12:20:06 +01003700API LY_ERR
3701lyd_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 +02003702 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003703{
3704 LY_ERR rc;
3705 struct lyd_node *target = NULL;
3706
Michal Vasko4c583e82020-07-17 12:16:14 +02003707 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003708
Michal Vasko08fd6132020-11-18 18:17:45 +01003709 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003710 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003711 if (match) {
3712 *match = NULL;
3713 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003714 return LY_ENOTFOUND;
3715 }
3716
Michal Vaskof03ed032020-03-04 13:31:44 +01003717 if (key_or_value && !val_len) {
3718 val_len = strlen(key_or_value);
3719 }
3720
Michal Vaskob104f112020-07-17 09:54:54 +02003721 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3722 /* create a data node and find the instance */
3723 if (schema->nodetype == LYS_LEAFLIST) {
3724 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003725 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3726 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003727 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003728 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003729 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003730 }
3731
3732 /* find it */
3733 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003734 } else {
3735 /* find the first schema node instance */
3736 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003737 }
3738
Michal Vaskoe444f752020-02-10 12:20:06 +01003739 lyd_free_tree(target);
3740 return rc;
3741}
Michal Vaskoccc02342020-05-21 10:09:21 +02003742
3743API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003744lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3745{
3746 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3747
3748 for ( ; first; first = first->next) {
3749 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
3750 break;
3751 }
3752 }
3753
3754 if (match) {
3755 *match = (struct lyd_node *)first;
3756 }
3757 return first ? LY_SUCCESS : LY_ENOTFOUND;
3758}
3759
3760API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02003761lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3762{
3763 LY_ERR ret = LY_SUCCESS;
3764 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003765 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003766 uint32_t i;
3767
3768 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3769
3770 memset(&xp_set, 0, sizeof xp_set);
3771
3772 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003773 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3774 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003775
3776 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01003777 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 +02003778 LY_CHECK_GOTO(ret, cleanup);
3779
3780 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003781 ret = ly_set_new(set);
3782 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003783
3784 /* transform into ly_set */
3785 if (xp_set.type == LYXP_SET_NODE_SET) {
3786 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3787 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003788 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003789 (*set)->size = xp_set.used;
3790
3791 for (i = 0; i < xp_set.used; ++i) {
3792 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003793 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003794 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003795 }
3796 }
3797 }
3798
3799cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003800 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003801 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01003802 if (ret) {
3803 ly_set_free(*set, NULL);
3804 *set = NULL;
3805 }
Michal Vaskoccc02342020-05-21 10:09:21 +02003806 return ret;
3807}
Radek Krejcica989142020-11-05 11:32:22 +01003808
Michal Vasko3e1f6552021-01-14 09:27:55 +01003809API LY_ERR
3810lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3811{
3812 LY_ERR ret = LY_SUCCESS;
3813 struct lyxp_expr *expr = NULL;
3814 struct ly_path *lypath = NULL;
3815
3816 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3817
3818 /* parse the path */
3819 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
3820 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
3821 LY_CHECK_GOTO(ret, cleanup);
3822
3823 /* compile the path */
3824 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, expr, LY_PATH_LREF_FALSE,
3825 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
3826 (void *)LYD_CTX(ctx_node), NULL, &lypath);
3827 LY_CHECK_GOTO(ret, cleanup);
3828
3829 /* evaluate the path */
3830 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
3831
3832cleanup:
3833 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3834 ly_path_free(LYD_CTX(ctx_node), lypath);
3835 return ret;
3836}
3837
Radek Krejcica989142020-11-05 11:32:22 +01003838API uint32_t
3839lyd_list_pos(const struct lyd_node *instance)
3840{
3841 const struct lyd_node *iter = NULL;
3842 uint32_t pos = 0;
3843
3844 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3845 return 0;
3846 }
3847
3848 /* data instances are ordered, so we can stop when we found instance of other schema node */
3849 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01003850 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01003851 /* overrun to the end of the siblings list */
3852 break;
3853 }
3854 ++pos;
3855 }
3856
3857 return pos;
3858}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003859
3860API struct lyd_node *
3861lyd_first_sibling(const struct lyd_node *node)
3862{
3863 struct lyd_node *start;
3864
3865 if (!node) {
3866 return NULL;
3867 }
3868
3869 /* get the first sibling */
3870 if (node->parent) {
3871 start = node->parent->child;
3872 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01003873 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003874 }
3875
3876 return start;
3877}