blob: d539a5ad033e22ff7b7361186cddfdcc91164adb [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
Radek Krejci8247bae2021-03-12 11:47:02 +0100949lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
950{
951 struct lyd_node *ret = NULL, *key;
952 const struct lysc_node *schema, *key_s;
953 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
954 va_list ap;
955 const char *key_val;
956 LY_ERR rc = LY_SUCCESS;
957
958 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
959
960 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
961 if (!schema) {
962 if (ext->argument) {
963 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
964 name, ext->argument, ext->def->name);
965 } else {
966 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
967 }
968 return LY_ENOTFOUND;
969 }
970 /* create list inner node */
971 LY_CHECK_RET(lyd_create_inner(schema, &ret));
972
973 va_start(ap, node);
974
975 /* create and insert all the keys */
976 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
977 key_val = va_arg(ap, const char *);
978
979 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
980 NULL, &key);
981 LY_CHECK_GOTO(rc, cleanup);
982 lyd_insert_node(ret, NULL, key);
983 }
984
985cleanup:
986 va_end(ap);
987 if (rc) {
988 lyd_free_tree(ret);
989 ret = NULL;
990 } else if (node) {
991 *node = ret;
992 }
993 return rc;
994}
995
996API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +0200997lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +0100998 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100999{
1000 struct lyd_node *ret = NULL;
1001 const struct lysc_node *schema;
1002 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1003
Michal Vasko6027eb92020-07-15 16:37:30 +02001004 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001005
Michal Vaskof03ed032020-03-04 13:31:44 +01001006 if (!module) {
1007 module = parent->schema->module;
1008 }
Michal Vasko004d3152020-06-11 19:59:22 +02001009 if (!keys) {
1010 keys = "";
1011 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001012
Michal Vasko004d3152020-06-11 19:59:22 +02001013 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001014 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 +02001015 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001016
Michal Vasko004d3152020-06-11 19:59:22 +02001017 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1018 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001019 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001020 } else {
1021 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001023 }
Michal Vasko004d3152020-06-11 19:59:22 +02001024 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001025 lyd_insert_node(parent, NULL, ret);
1026 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001027
1028 if (node) {
1029 *node = ret;
1030 }
1031 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001032}
1033
Michal Vasko3a41dff2020-07-15 14:30:28 +02001034API LY_ERR
1035lyd_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 +01001036 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001037{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001038 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001039 struct lyd_node *ret = NULL;
1040 const struct lysc_node *schema;
1041 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1042
Michal Vasko6027eb92020-07-15 16:37:30 +02001043 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001044
Michal Vaskof03ed032020-03-04 13:31:44 +01001045 if (!module) {
1046 module = parent->schema->module;
1047 }
1048
Radek Krejci41ac9942020-11-02 14:47:56 +01001049 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 +02001050 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001051
Radek Krejci8a5afc22021-03-12 11:10:47 +01001052 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 +02001053 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001054 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001055 lyd_insert_node(parent, NULL, ret);
1056 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001057
1058 if (node) {
1059 *node = ret;
1060 }
1061 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001062}
1063
Michal Vasko3a41dff2020-07-15 14:30:28 +02001064API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001065lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1066{
1067 LY_ERR rc;
1068 struct lyd_node *ret = NULL;
1069 const struct lysc_node *schema;
1070 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1071
1072 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1073
1074 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1075 if (!schema) {
1076 if (ext->argument) {
1077 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1078 name, ext->argument, ext->def->name);
1079 } else {
1080 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1081 }
1082 return LY_ENOTFOUND;
1083 }
1084 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
1085 LY_CHECK_RET(rc);
1086
1087 *node = ret;
1088
1089 return LY_SUCCESS;
1090}
1091
1092API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001093lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1094 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001095{
1096 struct lyd_node *ret = NULL;
1097 const struct lysc_node *schema;
1098 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1099
Michal Vasko6027eb92020-07-15 16:37:30 +02001100 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001101
Michal Vaskof03ed032020-03-04 13:31:44 +01001102 if (!module) {
1103 module = parent->schema->module;
1104 }
1105
Radek Krejci41ac9942020-11-02 14:47:56 +01001106 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 +02001107 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001108
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001109 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001110 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001111 lyd_insert_node(parent, NULL, ret);
1112 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001113
1114 if (node) {
1115 *node = ret;
1116 }
1117 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001118}
1119
Radek Krejci0b963da2021-03-12 13:23:44 +01001120API LY_ERR
1121lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1122 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1123{
1124 struct lyd_node *ret = NULL;
1125 const struct lysc_node *schema;
1126 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1127
1128 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1129
1130 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1131 if (!schema) {
1132 if (ext->argument) {
1133 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1134 name, ext->argument, ext->def->name);
1135 } else {
1136 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1137 }
1138 return LY_ENOTFOUND;
1139 }
1140 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1141
1142 *node = ret;
1143
1144 return LY_SUCCESS;
1145}
1146
Michal Vasko4490d312020-06-16 13:08:55 +02001147/**
1148 * @brief Update node value.
1149 *
1150 * @param[in] node Node to update.
1151 * @param[in] value New value to set.
1152 * @param[in] value_type Type of @p value for any node.
1153 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1154 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1155 * @return LY_ERR value.
1156 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001157static LY_ERR
1158lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001159 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001160{
1161 LY_ERR ret = LY_SUCCESS;
1162 struct lyd_node *new_any;
1163
1164 switch (node->schema->nodetype) {
1165 case LYS_CONTAINER:
1166 case LYS_NOTIF:
1167 case LYS_RPC:
1168 case LYS_ACTION:
1169 case LYS_LIST:
1170 case LYS_LEAFLIST:
1171 /* if it exists, there is nothing to update */
1172 *new_parent = NULL;
1173 *new_node = NULL;
1174 break;
1175 case LYS_LEAF:
1176 ret = lyd_change_term(node, value);
1177 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1178 /* there was an actual change (at least of the default flag) */
1179 *new_parent = node;
1180 *new_node = node;
1181 ret = LY_SUCCESS;
1182 } else if (ret == LY_ENOT) {
1183 /* no change */
1184 *new_parent = NULL;
1185 *new_node = NULL;
1186 ret = LY_SUCCESS;
1187 } /* else error */
1188 break;
1189 case LYS_ANYDATA:
1190 case LYS_ANYXML:
1191 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001192 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001193
1194 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001195 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001196 /* not equal, switch values (so that we can use generic node free) */
1197 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1198 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1199 ((struct lyd_node_any *)node)->value.str = value;
1200 ((struct lyd_node_any *)node)->value_type = value_type;
1201
1202 *new_parent = node;
1203 *new_node = node;
1204 } else {
1205 /* they are equal */
1206 *new_parent = NULL;
1207 *new_node = NULL;
1208 }
1209 lyd_free_tree(new_any);
1210 break;
1211 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001212 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001213 ret = LY_EINT;
1214 break;
1215 }
1216
1217 return ret;
1218}
1219
Michal Vasko3a41dff2020-07-15 14:30:28 +02001220API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001221lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1222 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001223{
Michal Vaskod86997b2020-05-26 15:19:54 +02001224 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001225 size_t pref_len, name_len;
1226
Michal Vasko2b5344c2021-02-26 10:12:05 +01001227 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1228 if (!ctx) {
1229 ctx = LYD_CTX(parent);
1230 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001231
Michal Vasko871a0252020-11-11 18:35:24 +01001232 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001233 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001234 return LY_EINVAL;
1235 }
Michal Vasko610553d2020-11-18 18:15:05 +01001236 if (meta) {
1237 *meta = NULL;
1238 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001239
1240 /* parse the name */
1241 tmp = name;
1242 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1243 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001244 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001245 }
1246
1247 /* find the module */
1248 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001249 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001250 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 +02001251 }
1252
1253 /* set value if none */
1254 if (!val_str) {
1255 val_str = "";
1256 }
1257
Michal Vasko871a0252020-11-11 18:35:24 +01001258 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
1259 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1260}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001261
Michal Vaskoba696702020-11-11 19:12:45 +01001262API LY_ERR
1263lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1264 struct lyd_meta **meta)
1265{
1266 const struct lys_module *mod;
1267
1268 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1269
1270 if (parent && !parent->schema) {
1271 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1272 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001273 }
Michal Vasko610553d2020-11-18 18:15:05 +01001274 if (meta) {
1275 *meta = NULL;
1276 }
Michal Vaskoba696702020-11-11 19:12:45 +01001277
1278 switch (attr->format) {
1279 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001280 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001281 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001282 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001283 return LY_ENOTFOUND;
1284 }
1285 break;
1286 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001287 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001288 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001289 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001290 return LY_ENOTFOUND;
1291 }
1292 break;
1293 default:
1294 LOGINT_RET(ctx);
1295 }
1296
Michal Vaskoad92b672020-11-12 13:11:31 +01001297 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 +01001298 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001299}
1300
Michal Vasko3a41dff2020-07-15 14:30:28 +02001301API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001302lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001303 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001304{
1305 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001306
Michal Vasko0ab974d2021-02-24 13:18:26 +01001307 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 +02001308
1309 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001310 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001311 }
1312 if (!value) {
1313 value = "";
1314 }
1315
Michal Vasko0ab974d2021-02-24 13:18:26 +01001316 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1317 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001318 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001319 lyd_insert_node(parent, NULL, ret);
1320 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001321
1322 if (node) {
1323 *node = ret;
1324 }
1325 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001326}
1327
Michal Vasko3a41dff2020-07-15 14:30:28 +02001328API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001329lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001330 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001331{
1332 struct lyd_node *ret = NULL;
1333
1334 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1335
1336 if (!ctx) {
1337 ctx = LYD_CTX(parent);
1338 }
1339 if (!value) {
1340 value = "";
1341 }
1342
Michal Vasko0ab974d2021-02-24 13:18:26 +01001343 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1344 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001345 if (parent) {
1346 lyd_insert_node(parent, NULL, ret);
1347 }
1348
1349 if (node) {
1350 *node = ret;
1351 }
1352 return LY_SUCCESS;
1353}
1354
1355API LY_ERR
1356lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001357 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001358{
Radek Krejci1798aae2020-07-14 13:26:06 +02001359 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001360 const struct ly_ctx *ctx;
1361 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001362 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001363
Michal Vasko3a41dff2020-07-15 14:30:28 +02001364 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001365
Michal Vaskob7be7a82020-08-20 09:09:04 +02001366 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001367
1368 /* parse the name */
1369 tmp = name;
1370 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001371 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001372 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001373 }
1374
Michal Vasko51d21b82020-11-13 18:03:54 +01001375 /* get the module */
1376 if (module_name) {
1377 mod_len = strlen(module_name);
1378 } else {
1379 module_name = prefix;
1380 mod_len = pref_len;
1381 }
1382
Michal Vasko00cbf532020-06-15 13:58:47 +02001383 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001384 if (!value) {
1385 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001386 }
1387
Michal Vasko8d65f852021-02-17 11:28:13 +01001388 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1389 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1390
1391 if (attr) {
1392 *attr = ret;
1393 }
1394 return LY_SUCCESS;
1395}
1396
1397API LY_ERR
1398lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1399 struct lyd_attr **attr)
1400{
1401 struct lyd_attr *ret = NULL;
1402 const struct ly_ctx *ctx;
1403 const char *prefix, *tmp;
1404 size_t pref_len, name_len;
1405
1406 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1407
1408 ctx = LYD_CTX(parent);
1409
1410 /* parse the name */
1411 tmp = name;
1412 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1413 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1414 return LY_EVALID;
1415 }
1416
1417 /* set value if none */
1418 if (!value) {
1419 value = "";
1420 }
1421
1422 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1423 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001424
1425 if (attr) {
1426 *attr = ret;
1427 }
1428 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001429}
1430
1431API LY_ERR
1432lyd_change_term(struct lyd_node *term, const char *val_str)
1433{
1434 LY_ERR ret = LY_SUCCESS;
1435 struct lysc_type *type;
1436 struct lyd_node_term *t;
1437 struct lyd_node *parent;
1438 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001439 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001440
1441 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1442
1443 if (!val_str) {
1444 val_str = "";
1445 }
1446 t = (struct lyd_node_term *)term;
1447 type = ((struct lysc_node_leaf *)term->schema)->type;
1448
1449 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001450 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001451 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 +01001452 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001453 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001454 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001455
1456 /* compare original and new value */
1457 if (type->plugin->compare(&t->value, &val)) {
1458 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001459 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001460 t->value = val;
1461 memset(&val, 0, sizeof val);
1462 val_change = 1;
1463 } else {
1464 val_change = 0;
1465 }
1466
1467 /* always clear the default flag */
1468 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001469 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001470 parent->flags &= ~LYD_DEFAULT;
1471 }
1472 dflt_change = 1;
1473 } else {
1474 dflt_change = 0;
1475 }
1476
1477 if (val_change || dflt_change) {
1478 /* make the node non-validated */
1479 term->flags &= LYD_NEW;
1480 }
1481
1482 if (val_change) {
1483 if (term->schema->nodetype == LYS_LEAFLIST) {
1484 /* leaf-list needs to be hashed again and re-inserted into parent */
1485 lyd_unlink_hash(term);
1486 lyd_hash(term);
1487 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1488 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1489 /* list needs to be updated if its key was changed */
1490 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001491 lyd_unlink_hash(lyd_parent(term));
1492 lyd_hash(lyd_parent(term));
1493 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001494 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1495 }
1496
1497 /* retrun value */
1498 if (!val_change) {
1499 if (dflt_change) {
1500 /* only default flag change */
1501 ret = LY_EEXIST;
1502 } else {
1503 /* no change */
1504 ret = LY_ENOT;
1505 }
1506 } /* else value changed, LY_SUCCESS */
1507
1508cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001509 if (val.realtype) {
1510 type->plugin->free(LYD_CTX(term), &val);
1511 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001512 return ret;
1513}
1514
Michal Vasko41586352020-07-13 13:54:25 +02001515API LY_ERR
1516lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1517{
1518 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001519 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001520 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001521 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001522
1523 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1524
1525 if (!val_str) {
1526 val_str = "";
1527 }
1528
1529 /* parse the new value into a new meta structure */
1530 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 +01001531 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001532
1533 /* compare original and new value */
1534 if (lyd_compare_meta(meta, m2)) {
1535 /* values differ, switch them */
1536 val = meta->value;
1537 meta->value = m2->value;
1538 m2->value = val;
1539 val_change = 1;
1540 } else {
1541 val_change = 0;
1542 }
1543
1544 /* retrun value */
1545 if (!val_change) {
1546 /* no change */
1547 ret = LY_ENOT;
1548 } /* else value changed, LY_SUCCESS */
1549
1550cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001551 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001552 return ret;
1553}
1554
Michal Vasko6741dc62021-02-04 09:27:45 +01001555static LY_ERR
1556lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1557{
1558 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci910d0132021-03-05 14:19:31 +01001559 const struct lysc_node *schema = NULL;
Michal Vasko6741dc62021-02-04 09:27:45 +01001560 struct ly_path_predicate *pred;
Radek Krejci910d0132021-03-05 14:19:31 +01001561 LY_ARRAY_COUNT_TYPE u, new_count = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001562
1563 assert(path);
1564
1565 /* go through all the compiled nodes */
1566 LY_ARRAY_FOR(path, u) {
1567 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001568 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001569 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1570 if (schema->flags & LYS_KEYLESS) {
1571 /* creating a new keyless list instance */
1572 break;
1573 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1574 LOG_LOCSET(schema, NULL, NULL, NULL);
1575 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1576 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1577 LOG_LOCBACK(1, 0, 0, 0);
1578 return LY_EINVAL;
1579 } /* else creating an opaque list without keys */
1580 }
1581 }
1582
Michal Vasko6f9b4262021-02-04 12:09:56 +01001583 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001584 /* parse leafref value into a predicate, if not defined in the path */
1585 if (!value) {
1586 value = "";
1587 }
1588
1589 r = LY_SUCCESS;
1590 if (options & LYD_NEW_PATH_OPAQ) {
1591 r = lys_value_validate(NULL, schema, value, strlen(value));
1592 }
1593 if (!r) {
1594 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001595 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1596 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001597
1598 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1599 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1600 LY_CHECK_GOTO(ret, cleanup);
1601 ++((struct lysc_type *)pred->value.realtype)->refcount;
1602
1603 if (schema->flags & LYS_CONFIG_R) {
1604 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001605 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001606 }
1607 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1608 }
1609
1610 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001611 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001612 LY_ARRAY_DECREMENT(path);
1613 }
1614
1615cleanup:
1616 return ret;
1617}
1618
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001619/**
1620 * @brief Create a new node in the data tree based on a path. All node types can be created.
1621 *
1622 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
1623 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
1624 * and @p value is set, the predicate is preferred.
1625 *
1626 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1627 * nodes, they are always created.
1628 *
1629 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1630 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1631 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1632 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1633 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1634 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1635 * searching for the appropriate module.
1636 * @param[in] path [Path](@ref howtoXPath) to create.
1637 * @param[in] value Value of the new leaf/leaf-list (const char *) or anyxml/anydata (expected type depends on @p value_type).
1638 * For other node types, it is ignored.
1639 * @param[in] value_type Anyxml/anydata node @p value type.
1640 * @param[in] options Bitmask of options, see @ref pathoptions.
1641 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1642 * @param[out] new_node Optional last node created.
1643 * @return LY_ERR value.
1644 */
1645static LY_ERR
1646lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1647 const void *value, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1648 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001649{
1650 LY_ERR ret = LY_SUCCESS, r;
1651 struct lyxp_expr *exp = NULL;
1652 struct ly_path *p = NULL;
1653 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1654 const struct lysc_node *schema;
Radek Krejci910d0132021-03-05 14:19:31 +01001655 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001656
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001657 assert(parent || ctx);
1658 assert(path);
1659 assert((path[0] == '/') || parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001660
1661 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001662 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001663 }
1664
1665 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001666 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 +02001667 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001668
1669 /* compile path */
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001670 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, ext, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001671 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 +01001672 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001673
Michal Vasko6741dc62021-02-04 09:27:45 +01001674 /* check the compiled path before searching existing nodes, it may be shortened */
1675 orig_count = LY_ARRAY_COUNT(p);
1676 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001677
1678 /* try to find any existing nodes in the path */
1679 if (parent) {
1680 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1681 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001682 if (orig_count == LY_ARRAY_COUNT(p)) {
1683 /* the node exists, are we supposed to update it or is it just a default? */
1684 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1685 LOG_LOCSET(NULL, node, NULL, NULL);
1686 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1687 LOG_LOCBACK(0, 1, 0, 0);
1688 ret = LY_EEXIST;
1689 goto cleanup;
1690 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001691
Michal Vasko6741dc62021-02-04 09:27:45 +01001692 /* update the existing node */
1693 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1694 goto cleanup;
1695 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001696 } else if (ret == LY_EINCOMPLETE) {
1697 /* some nodes were found, adjust the iterator to the next segment */
1698 ++path_idx;
1699 } else if (ret == LY_ENOTFOUND) {
1700 /* 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 +01001701 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001702 node = parent;
1703 }
1704 } else {
1705 /* error */
1706 goto cleanup;
1707 }
1708 }
1709
Michal Vasko6741dc62021-02-04 09:27:45 +01001710 /* restore the full path for creating new nodes */
1711 while (orig_count > LY_ARRAY_COUNT(p)) {
1712 LY_ARRAY_INCREMENT(p);
1713 }
1714
Michal Vasko00cbf532020-06-15 13:58:47 +02001715 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001716 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001717 cur_parent = node;
1718 schema = p[path_idx].node;
1719
1720 switch (schema->nodetype) {
1721 case LYS_LIST:
1722 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001723 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001724 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001725 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1726 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1727 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001728 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001729 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1730 LOG_LOCSET(schema, NULL, NULL, NULL);
1731 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1732 lys_nodetype2str(schema->nodetype), schema->name, path);
1733 LOG_LOCBACK(1, 0, 0, 0);
1734 ret = LY_EINVAL;
1735 goto cleanup;
1736 }
1737
Michal Vasko00cbf532020-06-15 13:58:47 +02001738 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1739 }
1740 break;
1741 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001742 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001743 case LYS_CONTAINER:
1744 case LYS_NOTIF:
1745 case LYS_RPC:
1746 case LYS_ACTION:
1747 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1748 break;
1749 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001750 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001751 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001752 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1753 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1754 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001755 } else {
1756 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1757 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1758 }
1759 break;
1760 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001761 if (lysc_is_key(schema)) {
1762 /* it must have been already created or some error will occur later */
1763 assert(cur_parent);
1764 node = lyd_child(cur_parent);
1765 assert(node && (node->schema == schema));
1766 goto next_iter;
1767 }
1768
1769 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001770 if (!value) {
1771 value = "";
1772 }
1773
1774 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001775 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001776 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001777 }
1778 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001779 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1780 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001781 } else {
1782 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001783 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1784 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001785 }
1786 break;
1787 case LYS_ANYDATA:
1788 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001789 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001790 break;
1791 default:
1792 LOGINT(ctx);
1793 ret = LY_EINT;
1794 goto cleanup;
1795 }
1796
1797 if (cur_parent) {
1798 /* connect to the parent */
1799 lyd_insert_node(cur_parent, NULL, node);
1800 } else if (parent) {
1801 /* connect to top-level siblings */
1802 lyd_insert_node(NULL, &parent, node);
1803 }
1804
Michal Vasko35880332020-12-08 13:08:12 +01001805next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001806 /* update remembered nodes */
1807 if (!nparent) {
1808 nparent = node;
1809 }
1810 nnode = node;
1811 }
1812
1813cleanup:
1814 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001815 if (p) {
1816 while (orig_count > LY_ARRAY_COUNT(p)) {
1817 LY_ARRAY_INCREMENT(p);
1818 }
1819 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001820 ly_path_free(ctx, p);
1821 if (!ret) {
1822 /* set out params only on success */
1823 if (new_parent) {
1824 *new_parent = nparent;
1825 }
1826 if (new_node) {
1827 *new_node = nnode;
1828 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001829 } else {
1830 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001831 }
1832 return ret;
1833}
1834
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001835API LY_ERR
1836lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1837 struct lyd_node **node)
1838{
1839 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1840 return lyd_new_path_(parent, ctx, NULL, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1841}
1842
1843API LY_ERR
1844lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1845 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
1846{
1847 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1848 return lyd_new_path_(parent, ctx, NULL, path, value, value_type, options, new_parent, new_node);
1849}
1850
1851API LY_ERR
1852lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1853 uint32_t options, struct lyd_node **node)
1854{
1855 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1856
1857 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, LY_EINVAL);
1858 return lyd_new_path_(parent, ctx, ext, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1859}
1860
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001861LY_ERR
1862lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001863 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 +02001864 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001865{
1866 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001867 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001868 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001869 struct lyd_value **dflts;
1870 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001871 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001872
1873 assert(first && (parent || sparent || mod));
1874
1875 if (!sparent && parent) {
1876 sparent = parent->schema;
1877 }
1878
Michal Vasko630d9892020-12-08 17:11:08 +01001879 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1880 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1881 getnext_opts |= LYS_GETNEXT_OUTPUT;
1882 }
1883
1884 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001885 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1886 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001887 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1888 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001889 }
1890
1891 switch (iter->nodetype) {
1892 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001893 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1894 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001895 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001896 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001897 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001898 } else if (node) {
1899 /* create any default data in the existing case */
1900 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001901 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001902 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001903 }
1904 break;
1905 case LYS_CONTAINER:
1906 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1907 /* create default NP container */
1908 LY_CHECK_RET(lyd_create_inner(iter, &node));
Radek Krejci9a3823e2021-01-27 20:26:46 +01001909 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001910 lyd_insert_node(parent, first, node);
1911
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001912 if (lysc_node_when(iter) && node_when) {
1913 /* remember to resolve when */
1914 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1915 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001916 if (diff) {
1917 /* add into diff */
1918 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1919 }
1920
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001921 /* create any default children */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001922 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 +02001923 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001924 }
1925 break;
1926 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001927 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1928 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001929 /* create default leaf */
1930 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1931 if (ret == LY_EINCOMPLETE) {
1932 if (node_types) {
1933 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001934 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001935 }
1936 } else if (ret) {
1937 return ret;
1938 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001939 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001940 lyd_insert_node(parent, first, node);
1941
Radek Krejci9a3823e2021-01-27 20:26:46 +01001942 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001943 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001944 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001945 }
1946 if (diff) {
1947 /* add into diff */
1948 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1949 }
1950 }
1951 break;
1952 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001953 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1954 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001955 /* create all default leaf-lists */
1956 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1957 LY_ARRAY_FOR(dflts, u) {
1958 ret = lyd_create_term2(iter, dflts[u], &node);
1959 if (ret == LY_EINCOMPLETE) {
1960 if (node_types) {
1961 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001962 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001963 }
1964 } else if (ret) {
1965 return ret;
1966 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001967 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001968 lyd_insert_node(parent, first, node);
1969
Radek Krejci9a3823e2021-01-27 20:26:46 +01001970 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001971 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001972 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001973 }
1974 if (diff) {
1975 /* add into diff */
1976 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1977 }
1978 }
1979 }
1980 break;
1981 default:
1982 /* without defaults */
1983 break;
1984 }
1985 }
1986
1987 return LY_SUCCESS;
1988}
1989
1990API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001991lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001992{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001993 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001994 struct lyd_node *node;
1995 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001996
1997 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1998 if (diff) {
1999 *diff = NULL;
2000 }
2001
Michal Vasko56daf732020-08-10 10:57:18 +02002002 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002003 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002004 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2005 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01002006 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when,
2007 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002008 }
2009
Michal Vasko56daf732020-08-10 10:57:18 +02002010 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002011 }
2012
Michal Vasko3488ada2020-12-03 14:18:19 +01002013 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01002014 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002015
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002016cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002017 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002018 if (ret && diff) {
2019 lyd_free_all(*diff);
2020 *diff = NULL;
2021 }
2022 return ret;
2023}
2024
2025API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002026lyd_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 +02002027{
2028 const struct lys_module *mod;
2029 struct lyd_node *d = NULL;
2030 uint32_t i = 0;
2031 LY_ERR ret = LY_SUCCESS;
2032
2033 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2034 if (diff) {
2035 *diff = NULL;
2036 }
2037 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002038 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002039 }
2040
2041 /* add nodes for each module one-by-one */
2042 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2043 if (!mod->implemented) {
2044 continue;
2045 }
2046
2047 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2048 if (d) {
2049 /* merge into one diff */
2050 lyd_insert_sibling(*diff, d, diff);
2051
2052 d = NULL;
2053 }
2054 }
2055
2056cleanup:
2057 if (ret && diff) {
2058 lyd_free_all(*diff);
2059 *diff = NULL;
2060 }
2061 return ret;
2062}
2063
2064API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002065lyd_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 +02002066{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002067 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002068 struct lyd_node *root, *d = NULL;
2069 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002070
2071 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2072 if (diff) {
2073 *diff = NULL;
2074 }
2075
2076 /* add all top-level defaults for this module */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01002077 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 +01002078
2079 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01002080 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002081
2082 /* process nested nodes */
2083 LY_LIST_FOR(*tree, root) {
2084 /* skip added default nodes */
2085 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2086 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2087
2088 if (d) {
2089 /* merge into one diff */
2090 lyd_insert_sibling(*diff, d, diff);
2091
2092 d = NULL;
2093 }
2094 }
2095 }
2096
2097cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002098 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002099 if (ret && diff) {
2100 lyd_free_all(*diff);
2101 *diff = NULL;
2102 }
2103 return ret;
2104}
2105
Michal Vasko90932a92020-02-12 14:33:03 +01002106struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002107lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002108{
Michal Vaskob104f112020-07-17 09:54:54 +02002109 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002110 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002111 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002112 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002113
Michal Vaskob104f112020-07-17 09:54:54 +02002114 assert(new_node);
2115
2116 if (!first_sibling || !new_node->schema) {
2117 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002118 return NULL;
2119 }
2120
Michal Vasko93b16062020-12-09 18:14:18 +01002121 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002122 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002123 getnext_opts = LYS_GETNEXT_OUTPUT;
2124 }
2125
Michal Vaskob104f112020-07-17 09:54:54 +02002126 if (first_sibling->parent && first_sibling->parent->children_ht) {
2127 /* find the anchor using hashes */
2128 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002129 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002130 while (schema) {
2131 /* keep trying to find the first existing instance of the closest following schema sibling,
2132 * otherwise return NULL - inserting at the end */
2133 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2134 break;
2135 }
2136
Michal Vasko93b16062020-12-09 18:14:18 +01002137 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002138 }
2139 } else {
2140 /* find the anchor without hashes */
2141 match = (struct lyd_node *)first_sibling;
2142 if (!lysc_data_parent(new_node->schema)) {
2143 /* we are in top-level, skip all the data from preceding modules */
2144 LY_LIST_FOR(match, match) {
2145 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2146 break;
2147 }
2148 }
2149 }
2150
2151 /* get the first schema sibling */
2152 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01002153 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002154
2155 found = 0;
2156 LY_LIST_FOR(match, match) {
2157 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2158 /* we have found an opaque node, which must be at the end, so use it OR
2159 * modules do not match, so we must have traversed all the data from new_node module (if any),
2160 * we have found the first node of the next module, that is what we want */
2161 break;
2162 }
2163
2164 /* skip schema nodes until we find the instantiated one */
2165 while (!found) {
2166 if (new_node->schema == schema) {
2167 /* we have found the schema of the new node, continue search to find the first
2168 * data node with a different schema (after our schema) */
2169 found = 1;
2170 break;
2171 }
2172 if (match->schema == schema) {
2173 /* current node (match) is a data node still before the new node, continue search in data */
2174 break;
2175 }
Michal Vasko93b16062020-12-09 18:14:18 +01002176 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002177 assert(schema);
2178 }
2179
2180 if (found && (match->schema != new_node->schema)) {
2181 /* find the next node after we have found our node schema data instance */
2182 break;
2183 }
2184 }
Michal Vasko90932a92020-02-12 14:33:03 +01002185 }
2186
2187 return match;
2188}
2189
2190/**
2191 * @brief Insert node after a sibling.
2192 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002193 * Handles inserting into NP containers and key-less lists.
2194 *
Michal Vasko90932a92020-02-12 14:33:03 +01002195 * @param[in] sibling Sibling to insert after.
2196 * @param[in] node Node to insert.
2197 */
2198static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002199lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002200{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002201 struct lyd_node_inner *par;
2202
Michal Vasko90932a92020-02-12 14:33:03 +01002203 assert(!node->next && (node->prev == node));
2204
2205 node->next = sibling->next;
2206 node->prev = sibling;
2207 sibling->next = node;
2208 if (node->next) {
2209 /* sibling had a succeeding node */
2210 node->next->prev = node;
2211 } else {
2212 /* sibling was last, find first sibling and change its prev */
2213 if (sibling->parent) {
2214 sibling = sibling->parent->child;
2215 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002216 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002217 }
2218 sibling->prev = node;
2219 }
2220 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002221
Michal Vasko9f96a052020-03-10 09:41:45 +01002222 for (par = node->parent; par; par = par->parent) {
2223 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2224 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002225 par->flags &= ~LYD_DEFAULT;
2226 }
Michal Vaskob104f112020-07-17 09:54:54 +02002227 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002228 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002229 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002230 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002231 }
Michal Vasko90932a92020-02-12 14:33:03 +01002232}
2233
2234/**
2235 * @brief Insert node before a sibling.
2236 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002237 * Handles inserting into NP containers and key-less lists.
2238 *
Michal Vasko90932a92020-02-12 14:33:03 +01002239 * @param[in] sibling Sibling to insert before.
2240 * @param[in] node Node to insert.
2241 */
2242static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002243lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002244{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002245 struct lyd_node_inner *par;
2246
Michal Vasko90932a92020-02-12 14:33:03 +01002247 assert(!node->next && (node->prev == node));
2248
2249 node->next = sibling;
2250 /* covers situation of sibling being first */
2251 node->prev = sibling->prev;
2252 sibling->prev = node;
2253 if (node->prev->next) {
2254 /* sibling had a preceding node */
2255 node->prev->next = node;
2256 } else if (sibling->parent) {
2257 /* sibling was first and we must also change parent child pointer */
2258 sibling->parent->child = node;
2259 }
2260 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002261
Michal Vasko9f96a052020-03-10 09:41:45 +01002262 for (par = node->parent; par; par = par->parent) {
2263 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2264 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002265 par->flags &= ~LYD_DEFAULT;
2266 }
Michal Vaskob104f112020-07-17 09:54:54 +02002267 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002268 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002269 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002270 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002271 }
Michal Vasko90932a92020-02-12 14:33:03 +01002272}
2273
2274/**
Michal Vaskob104f112020-07-17 09:54:54 +02002275 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002276 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002277 * Handles inserting into NP containers and key-less lists.
2278 *
Michal Vasko90932a92020-02-12 14:33:03 +01002279 * @param[in] parent Parent to insert into.
2280 * @param[in] node Node to insert.
2281 */
2282static void
Michal Vaskob104f112020-07-17 09:54:54 +02002283lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002284{
2285 struct lyd_node_inner *par;
2286
Radek Krejcia1c1e542020-09-29 16:06:52 +02002287 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002288 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002289
2290 par = (struct lyd_node_inner *)parent;
2291
Michal Vaskob104f112020-07-17 09:54:54 +02002292 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002293 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002294
Michal Vaskod989ba02020-08-24 10:59:24 +02002295 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002296 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2297 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002298 par->flags &= ~LYD_DEFAULT;
2299 }
Michal Vasko52927e22020-03-16 17:26:14 +01002300 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002301 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002302 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002303 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002304 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002305}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002306
Michal Vasko751cb4d2020-07-14 12:25:28 +02002307/**
2308 * @brief Learn whether a list instance has all the keys.
2309 *
2310 * @param[in] list List instance to check.
2311 * @return non-zero if all the keys were found,
2312 * @return 0 otherwise.
2313 */
2314static int
2315lyd_insert_has_keys(const struct lyd_node *list)
2316{
2317 const struct lyd_node *key;
2318 const struct lysc_node *skey = NULL;
2319
2320 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002321 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002322 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2323 if (!key || (key->schema != skey)) {
2324 /* key missing */
2325 return 0;
2326 }
2327
2328 key = key->next;
2329 }
2330
2331 /* all keys found */
2332 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002333}
2334
2335void
Michal Vaskob104f112020-07-17 09:54:54 +02002336lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002337{
Michal Vaskob104f112020-07-17 09:54:54 +02002338 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002339
Michal Vaskob104f112020-07-17 09:54:54 +02002340 /* inserting list without its keys is not supported */
2341 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002342 assert(!parent || !parent->schema ||
2343 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002344
Michal Vaskob104f112020-07-17 09:54:54 +02002345 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002346 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002347 }
Michal Vasko90932a92020-02-12 14:33:03 +01002348
Michal Vaskob104f112020-07-17 09:54:54 +02002349 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002350 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002351
Michal Vaskob104f112020-07-17 09:54:54 +02002352 /* find the anchor, our next node, so we can insert before it */
2353 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2354 if (anchor) {
2355 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002356 if (!parent && (*first_sibling_p == anchor)) {
2357 /* move first sibling */
2358 *first_sibling_p = node;
2359 }
Michal Vaskob104f112020-07-17 09:54:54 +02002360 } else if (first_sibling) {
2361 lyd_insert_after_node(first_sibling->prev, node);
2362 } else if (parent) {
2363 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002364 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002365 *first_sibling_p = node;
2366 }
2367
2368 /* insert into parent HT */
2369 lyd_insert_hash(node);
2370
2371 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002372 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002373 lyd_hash(parent);
2374
2375 /* now we can insert even the list into its parent HT */
2376 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002377 }
Michal Vasko90932a92020-02-12 14:33:03 +01002378}
2379
Michal Vasko717a4c32020-12-07 09:40:10 +01002380/**
2381 * @brief Check schema place of a node to be inserted.
2382 *
2383 * @param[in] parent Schema node of the parent data node.
2384 * @param[in] sibling Schema node of a sibling data node.
2385 * @param[in] schema Schema node if the data node to be inserted.
2386 * @return LY_SUCCESS on success.
2387 * @return LY_EINVAL if the place is invalid.
2388 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002389static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002390lyd_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 +01002391{
2392 const struct lysc_node *par2;
2393
Michal Vasko62ed12d2020-05-21 10:08:25 +02002394 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002395 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2396 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002397
Michal Vasko717a4c32020-12-07 09:40:10 +01002398 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002399 /* opaque nodes can be inserted wherever */
2400 return LY_SUCCESS;
2401 }
2402
Michal Vasko717a4c32020-12-07 09:40:10 +01002403 if (!parent) {
2404 parent = lysc_data_parent(sibling);
2405 }
2406
Michal Vaskof03ed032020-03-04 13:31:44 +01002407 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002408 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002409
2410 if (parent) {
2411 /* inner node */
2412 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002413 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002414 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002415 return LY_EINVAL;
2416 }
2417 } else {
2418 /* top-level node */
2419 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002420 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002421 return LY_EINVAL;
2422 }
2423 }
2424
2425 return LY_SUCCESS;
2426}
2427
2428API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002429lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002430{
2431 struct lyd_node *iter;
2432
Michal Vasko0ab974d2021-02-24 13:18:26 +01002433 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002434
Michal Vasko717a4c32020-12-07 09:40:10 +01002435 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002436
Michal Vasko0ab974d2021-02-24 13:18:26 +01002437 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002438 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2439 return LY_EINVAL;
2440 }
2441
2442 if (node->parent || node->prev->next) {
2443 lyd_unlink_tree(node);
2444 }
2445
2446 while (node) {
2447 iter = node->next;
2448 lyd_unlink_tree(node);
2449 lyd_insert_node(parent, NULL, node);
2450 node = iter;
2451 }
2452 return LY_SUCCESS;
2453}
2454
2455API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002456lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002457{
2458 struct lyd_node *iter;
2459
Michal Vaskob104f112020-07-17 09:54:54 +02002460 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002461
Michal Vaskob104f112020-07-17 09:54:54 +02002462 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002463 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002464 }
2465
Michal Vasko553d0b52020-12-04 16:27:52 +01002466 if (sibling == node) {
2467 /* we need to keep the connection to siblings so we can insert into them */
2468 sibling = sibling->prev;
2469 }
2470
Michal Vaskob1b5c262020-03-05 14:29:47 +01002471 if (node->parent || node->prev->next) {
2472 lyd_unlink_tree(node);
2473 }
2474
2475 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002476 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002477 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002478 return LY_EINVAL;
2479 }
2480
Michal Vaskob1b5c262020-03-05 14:29:47 +01002481 iter = node->next;
2482 lyd_unlink_tree(node);
2483 lyd_insert_node(NULL, &sibling, node);
2484 node = iter;
2485 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002486
Michal Vaskob104f112020-07-17 09:54:54 +02002487 if (first) {
2488 /* find the first sibling */
2489 *first = sibling;
2490 while ((*first)->prev->next) {
2491 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002492 }
2493 }
2494
2495 return LY_SUCCESS;
2496}
2497
Michal Vaskob1b5c262020-03-05 14:29:47 +01002498API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002499lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2500{
Michal Vaskof03ed032020-03-04 13:31:44 +01002501 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2502
Michal Vasko717a4c32020-12-07 09:40:10 +01002503 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002504
Michal Vaskob104f112020-07-17 09:54:54 +02002505 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002506 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002507 return LY_EINVAL;
2508 }
2509
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002510 lyd_unlink_tree(node);
2511 lyd_insert_before_node(sibling, node);
2512 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002513
Michal Vaskof03ed032020-03-04 13:31:44 +01002514 return LY_SUCCESS;
2515}
2516
2517API LY_ERR
2518lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2519{
Michal Vaskof03ed032020-03-04 13:31:44 +01002520 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2521
Michal Vasko717a4c32020-12-07 09:40:10 +01002522 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002523
Michal Vaskob104f112020-07-17 09:54:54 +02002524 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002525 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002526 return LY_EINVAL;
2527 }
2528
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002529 lyd_unlink_tree(node);
2530 lyd_insert_after_node(sibling, node);
2531 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002532
Michal Vaskof03ed032020-03-04 13:31:44 +01002533 return LY_SUCCESS;
2534}
2535
2536API void
2537lyd_unlink_tree(struct lyd_node *node)
2538{
2539 struct lyd_node *iter;
2540
2541 if (!node) {
2542 return;
2543 }
2544
Michal Vaskob104f112020-07-17 09:54:54 +02002545 /* update hashes while still linked into the tree */
2546 lyd_unlink_hash(node);
2547
Michal Vaskof03ed032020-03-04 13:31:44 +01002548 /* unlink from siblings */
2549 if (node->prev->next) {
2550 node->prev->next = node->next;
2551 }
2552 if (node->next) {
2553 node->next->prev = node->prev;
2554 } else {
2555 /* unlinking the last node */
2556 if (node->parent) {
2557 iter = node->parent->child;
2558 } else {
2559 iter = node->prev;
2560 while (iter->prev != node) {
2561 iter = iter->prev;
2562 }
2563 }
2564 /* update the "last" pointer from the first node */
2565 iter->prev = node->prev;
2566 }
2567
2568 /* unlink from parent */
2569 if (node->parent) {
2570 if (node->parent->child == node) {
2571 /* the node is the first child */
2572 node->parent->child = node->next;
2573 }
2574
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002575 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002576 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2577 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002578 LY_LIST_FOR(node->parent->child, iter) {
2579 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2580 break;
2581 }
2582 }
2583 if (!iter) {
2584 node->parent->flags |= LYD_DEFAULT;
2585 }
2586 }
2587
Michal Vaskof03ed032020-03-04 13:31:44 +01002588 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002589 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002590 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002591 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002592 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002593 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002594 }
2595 }
2596
2597 node->parent = NULL;
2598 }
2599
2600 node->next = NULL;
2601 node->prev = node;
2602}
2603
Michal Vaskoa5da3292020-08-12 13:10:50 +02002604void
Michal Vasko871a0252020-11-11 18:35:24 +01002605lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002606{
2607 struct lyd_meta *last, *iter;
2608
2609 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002610
2611 if (!meta) {
2612 return;
2613 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002614
2615 for (iter = meta; iter; iter = iter->next) {
2616 iter->parent = parent;
2617 }
2618
2619 /* insert as the last attribute */
2620 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002621 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002622 last->next = meta;
2623 } else {
2624 parent->meta = meta;
2625 }
2626
2627 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002628 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002629 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002630 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002631 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002632}
2633
2634LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002635lyd_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 +02002636 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 +01002637 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002638{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002639 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002640 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002641 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002642 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002643
Michal Vasko9f96a052020-03-10 09:41:45 +01002644 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002645
Radek Krejciddace2c2021-01-08 11:30:56 +01002646 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002647
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002648 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002649 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002650 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002651 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002652 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002653 break;
2654 }
2655 }
2656 if (!ant) {
2657 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002658 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002659 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002660 ret = LY_EINVAL;
2661 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002662 }
2663
Michal Vasko9f96a052020-03-10 09:41:45 +01002664 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002665 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002666 mt->parent = parent;
2667 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002668 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2669 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002670 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2671 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2672 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002673
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002674 /* insert as the last attribute */
2675 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002676 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002677 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002678 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002679 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002680 }
2681
Michal Vasko9f96a052020-03-10 09:41:45 +01002682 if (meta) {
2683 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002684 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002685
2686cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002687 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002688 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002689}
2690
Michal Vaskoa5da3292020-08-12 13:10:50 +02002691void
2692lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2693{
2694 struct lyd_attr *last, *iter;
2695 struct lyd_node_opaq *opaq;
2696
2697 assert(parent && !parent->schema);
2698
2699 if (!attr) {
2700 return;
2701 }
2702
2703 opaq = (struct lyd_node_opaq *)parent;
2704 for (iter = attr; iter; iter = iter->next) {
2705 iter->parent = opaq;
2706 }
2707
2708 /* insert as the last attribute */
2709 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002710 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002711 last->next = attr;
2712 } else {
2713 opaq->attr = attr;
2714 }
2715}
2716
Michal Vasko52927e22020-03-16 17:26:14 +01002717LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002718lyd_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 +01002719 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2720 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 +01002721{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002722 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002723 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002724
2725 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002726 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002727
2728 if (!value_len) {
2729 value = "";
2730 }
2731
2732 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002733 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002734
Michal Vaskoad92b672020-11-12 13:11:31 +01002735 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002736 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002737 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002738 }
2739 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002740 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002741 }
2742
Michal Vasko52927e22020-03-16 17:26:14 +01002743 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002744 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2745 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002746 *dynamic = 0;
2747 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002748 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002749 }
Michal Vasko501af032020-11-11 20:27:44 +01002750 at->format = format;
2751 at->val_prefix_data = val_prefix_data;
2752 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002753
2754 /* insert as the last attribute */
2755 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002756 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002757 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002758 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002759 last->next = at;
2760 }
2761
Radek Krejci011e4aa2020-09-04 15:22:31 +02002762finish:
2763 if (ret) {
2764 lyd_free_attr_single(ctx, at);
2765 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002766 *attr = at;
2767 }
2768 return LY_SUCCESS;
2769}
2770
Radek Krejci084289f2019-07-09 17:35:30 +02002771API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002772lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002773{
Michal Vasko004d3152020-06-11 19:59:22 +02002774 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002775
Michal Vasko004d3152020-06-11 19:59:22 +02002776 if (ly_path_eval(path, tree, &target)) {
2777 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002778 }
2779
Michal Vasko004d3152020-06-11 19:59:22 +02002780 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002781}
2782
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002783API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002784lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002785{
2786 const struct lyd_node *iter1, *iter2;
2787 struct lyd_node_term *term1, *term2;
2788 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002789 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002790 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002791
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002792 if (!node1 || !node2) {
2793 if (node1 == node2) {
2794 return LY_SUCCESS;
2795 } else {
2796 return LY_ENOT;
2797 }
2798 }
2799
Michal Vaskob7be7a82020-08-20 09:09:04 +02002800 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002801 return LY_ENOT;
2802 }
2803
2804 if (node1->hash != node2->hash) {
2805 return LY_ENOT;
2806 }
Michal Vasko52927e22020-03-16 17:26:14 +01002807 /* 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 +02002808
Michal Vasko52927e22020-03-16 17:26:14 +01002809 if (!node1->schema) {
2810 opaq1 = (struct lyd_node_opaq *)node1;
2811 opaq2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +01002812 if ((opaq1->name.name != opaq2->name.name) || (opaq1->format != opaq2->format) ||
2813 (opaq1->name.module_ns != opaq2->name.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002814 return LY_ENOT;
2815 }
Michal Vasko52927e22020-03-16 17:26:14 +01002816 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002817 case LY_PREF_XML:
2818 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 +01002819 return LY_ENOT;
2820 }
2821 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002822 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002823 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2824 if (strcmp(opaq1->value, opaq2->value)) {
2825 return LY_ENOT;
2826 }
2827 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002828 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002829 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002830 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002831 return LY_EINT;
2832 }
2833 if (options & LYD_COMPARE_FULL_RECURSION) {
2834 iter1 = opaq1->child;
2835 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002836 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002837 }
2838 return LY_SUCCESS;
2839 } else {
2840 switch (node1->schema->nodetype) {
2841 case LYS_LEAF:
2842 case LYS_LEAFLIST:
2843 if (options & LYD_COMPARE_DEFAULTS) {
2844 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2845 return LY_ENOT;
2846 }
2847 }
2848
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002849 term1 = (struct lyd_node_term *)node1;
2850 term2 = (struct lyd_node_term *)node2;
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002851 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002852 case LYS_CONTAINER:
2853 if (options & LYD_COMPARE_DEFAULTS) {
2854 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2855 return LY_ENOT;
2856 }
2857 }
2858 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002859 iter1 = lyd_child(node1);
2860 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002861 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002862 }
2863 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002864 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002865 case LYS_ACTION:
2866 if (options & LYD_COMPARE_FULL_RECURSION) {
2867 /* TODO action/RPC
2868 goto all_children_compare;
2869 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002870 }
2871 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002872 case LYS_NOTIF:
2873 if (options & LYD_COMPARE_FULL_RECURSION) {
2874 /* TODO Notification
2875 goto all_children_compare;
2876 */
2877 }
2878 return LY_SUCCESS;
2879 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01002880 iter1 = lyd_child(node1);
2881 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002882
2883 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2884 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01002885 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002886 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002887 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002888 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002889 return LY_ENOT;
2890 }
2891 iter1 = iter1->next;
2892 iter2 = iter2->next;
2893 }
2894 } else {
2895 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2896
Radek Krejci0f969882020-08-21 16:56:47 +02002897all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002898 if (!iter1 && !iter2) {
2899 /* no children, nothing to compare */
2900 return LY_SUCCESS;
2901 }
2902
Michal Vaskod989ba02020-08-24 10:59:24 +02002903 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002904 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002905 return LY_ENOT;
2906 }
2907 }
2908 if (iter1 || iter2) {
2909 return LY_ENOT;
2910 }
2911 }
2912 return LY_SUCCESS;
2913 case LYS_ANYXML:
2914 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002915 any1 = (struct lyd_node_any *)node1;
2916 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002917
2918 if (any1->value_type != any2->value_type) {
2919 return LY_ENOT;
2920 }
2921 switch (any1->value_type) {
2922 case LYD_ANYDATA_DATATREE:
2923 iter1 = any1->value.tree;
2924 iter2 = any2->value.tree;
2925 goto all_children_compare;
2926 case LYD_ANYDATA_STRING:
2927 case LYD_ANYDATA_XML:
2928 case LYD_ANYDATA_JSON:
2929 len1 = strlen(any1->value.str);
2930 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002931 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002932 return LY_ENOT;
2933 }
2934 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002935 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002936 len1 = lyd_lyb_data_length(any1->value.mem);
2937 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002938 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002939 return LY_ENOT;
2940 }
2941 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002942 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002943 }
2944 }
2945
Michal Vaskob7be7a82020-08-20 09:09:04 +02002946 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002947 return LY_EINT;
2948}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002949
Michal Vasko21725742020-06-29 11:49:25 +02002950API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002951lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002952{
Michal Vaskod989ba02020-08-24 10:59:24 +02002953 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002954 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2955 }
2956
Michal Vasko11a81432020-07-28 16:31:29 +02002957 if (node1 == node2) {
2958 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002959 }
Michal Vasko11a81432020-07-28 16:31:29 +02002960 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002961}
2962
2963API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002964lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2965{
2966 if (!meta1 || !meta2) {
2967 if (meta1 == meta2) {
2968 return LY_SUCCESS;
2969 } else {
2970 return LY_ENOT;
2971 }
2972 }
2973
Michal Vaskoa8083062020-11-06 17:22:10 +01002974 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002975 return LY_ENOT;
2976 }
2977
Michal Vasko21725742020-06-29 11:49:25 +02002978 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2979}
2980
Radek Krejci22ebdba2019-07-25 13:59:43 +02002981/**
Michal Vasko52927e22020-03-16 17:26:14 +01002982 * @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 +02002983 *
2984 * 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 +02002985 *
2986 * @param[in] node Original node to duplicate
2987 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2988 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2989 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2990 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2991 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002992 */
Michal Vasko52927e22020-03-16 17:26:14 +01002993static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002994lyd_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 +02002995 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002996{
Michal Vasko52927e22020-03-16 17:26:14 +01002997 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002998 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002999 struct lyd_meta *meta;
3000 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003001
Michal Vasko52927e22020-03-16 17:26:14 +01003002 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003003
Michal Vasko52927e22020-03-16 17:26:14 +01003004 if (!node->schema) {
3005 dup = calloc(1, sizeof(struct lyd_node_opaq));
3006 } else {
3007 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003008 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003009 case LYS_ACTION:
3010 case LYS_NOTIF:
3011 case LYS_CONTAINER:
3012 case LYS_LIST:
3013 dup = calloc(1, sizeof(struct lyd_node_inner));
3014 break;
3015 case LYS_LEAF:
3016 case LYS_LEAFLIST:
3017 dup = calloc(1, sizeof(struct lyd_node_term));
3018 break;
3019 case LYS_ANYDATA:
3020 case LYS_ANYXML:
3021 dup = calloc(1, sizeof(struct lyd_node_any));
3022 break;
3023 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003024 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003025 ret = LY_EINT;
3026 goto error;
3027 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003028 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003029 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003030
Michal Vaskof6df0a02020-06-16 13:08:34 +02003031 if (options & LYD_DUP_WITH_FLAGS) {
3032 dup->flags = node->flags;
3033 } else {
3034 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3035 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003036 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003037 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003038
Michal Vasko25a32822020-07-09 15:48:22 +02003039 /* duplicate metadata */
3040 if (!(options & LYD_DUP_NO_META)) {
3041 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003042 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02003043 }
3044 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003045
3046 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003047 if (!dup->schema) {
3048 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3049 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3050 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003051
3052 if (options & LYD_DUP_RECURSIVE) {
3053 /* duplicate all the children */
3054 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003055 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003056 }
3057 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003058 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003059 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01003060 if (orig->name.prefix) {
3061 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003062 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003063 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 +01003064 if (orig->val_prefix_data) {
3065 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3066 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003067 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02003068 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003069 opaq->ctx = orig->ctx;
3070 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3071 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3072 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3073
3074 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003075 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003076 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003077 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3078 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3079 struct lyd_node *child;
3080
3081 if (options & LYD_DUP_RECURSIVE) {
3082 /* duplicate all the children */
3083 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003084 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003085 }
Michal Vasko69730152020-10-09 16:30:07 +02003086 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003087 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003088 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003089 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003090 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003091 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003092 if (!child) {
3093 /* possibly not keys are present in filtered tree */
3094 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003095 } else if (child->schema != key) {
3096 /* possibly not all keys are present in filtered tree,
3097 * but there can be also some non-key nodes */
3098 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003099 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003100 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003101 child = child->next;
3102 }
3103 }
3104 lyd_hash(dup);
3105 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003106 dup->hash = node->hash;
3107 any = (struct lyd_node_any *)node;
3108 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003109 }
3110
Michal Vasko52927e22020-03-16 17:26:14 +01003111 /* insert */
3112 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01003113
3114 if (dup_p) {
3115 *dup_p = dup;
3116 }
3117 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003118
3119error:
Michal Vasko52927e22020-03-16 17:26:14 +01003120 lyd_free_tree(dup);
3121 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003122}
3123
Michal Vasko3a41dff2020-07-15 14:30:28 +02003124static LY_ERR
3125lyd_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 +02003126 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003127{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003128 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003129 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003130
3131 *dup_parent = NULL;
3132 *local_parent = NULL;
3133
3134 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3135 if (parent && (parent->schema == orig_parent->schema)) {
3136 /* stop creating parents, connect what we have into the provided parent */
3137 iter = parent;
3138 repeat = 0;
3139 } else {
3140 iter = NULL;
3141 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02003142 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003143 }
3144 if (!*local_parent) {
3145 *local_parent = (struct lyd_node_inner *)iter;
3146 }
3147 if (iter->child) {
3148 /* 1) list - add after keys
3149 * 2) provided parent with some children */
3150 iter->child->prev->next = *dup_parent;
3151 if (*dup_parent) {
3152 (*dup_parent)->prev = iter->child->prev;
3153 iter->child->prev = *dup_parent;
3154 }
3155 } else {
3156 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3157 }
3158 if (*dup_parent) {
3159 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3160 }
3161 *dup_parent = (struct lyd_node *)iter;
3162 }
3163
3164 if (repeat && parent) {
3165 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003166 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003167 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003168 return LY_EINVAL;
3169 }
3170
3171 return LY_SUCCESS;
3172}
3173
3174static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003175lyd_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 +02003176{
3177 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003178 const struct lyd_node *orig; /* original node to be duplicated */
3179 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003180 struct lyd_node *top = NULL; /* the most higher created node */
3181 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003182
Michal Vasko3a41dff2020-07-15 14:30:28 +02003183 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003184
3185 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003186 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003187 } else {
3188 local_parent = parent;
3189 }
3190
Radek Krejci22ebdba2019-07-25 13:59:43 +02003191 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003192 if (lysc_is_key(orig->schema)) {
3193 if (local_parent) {
3194 /* the key must already exist in the parent */
3195 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3196 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3197 } else {
3198 assert(!(options & LYD_DUP_WITH_PARENTS));
3199 /* duplicating a single key, okay, I suppose... */
3200 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3201 LY_CHECK_GOTO(rc, error);
3202 }
3203 } else {
3204 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003205 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003206 LY_CHECK_GOTO(rc, error);
3207 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003208 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003209 break;
3210 }
3211 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003212
3213 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003214 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003215 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003216 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003217 }
3218 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003219
3220 if (dup) {
3221 *dup = first;
3222 }
3223 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003224
3225error:
3226 if (top) {
3227 lyd_free_tree(top);
3228 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003229 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003230 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003231 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003232}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003233
Michal Vasko3a41dff2020-07-15 14:30:28 +02003234API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003235lyd_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 +02003236{
3237 return lyd_dup(node, parent, options, 1, dup);
3238}
3239
3240API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003241lyd_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 +02003242{
3243 return lyd_dup(node, parent, options, 0, dup);
3244}
3245
3246API LY_ERR
3247lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003248{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003249 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003250 struct lyd_meta *mt, *last;
3251
Michal Vasko3a41dff2020-07-15 14:30:28 +02003252 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003253
3254 /* create a copy */
3255 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003256 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003257 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003258 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003259 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3260 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003261
3262 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003263 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003264 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003265 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003266 last->next = mt;
3267 } else {
3268 node->meta = mt;
3269 }
3270
Radek Krejci011e4aa2020-09-04 15:22:31 +02003271finish:
3272 if (ret) {
3273 lyd_free_meta_single(mt);
3274 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003275 *dup = mt;
3276 }
3277 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003278}
3279
Michal Vasko4490d312020-06-16 13:08:55 +02003280/**
3281 * @brief Merge a source sibling into target siblings.
3282 *
3283 * @param[in,out] first_trg First target sibling, is updated if top-level.
3284 * @param[in] parent_trg Target parent.
3285 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3286 * @param[in] options Merge options.
3287 * @return LY_ERR value.
3288 */
3289static LY_ERR
3290lyd_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 +02003291 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003292{
3293 LY_ERR ret;
3294 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003295 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003296 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003297
3298 sibling_src = *sibling_src_p;
3299 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3300 /* try to find the exact instance */
3301 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3302 } else {
3303 /* try to simply find the node, there cannot be more instances */
3304 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3305 }
3306
3307 if (!ret) {
3308 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003309 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003310 /* since they are different, they cannot both be default */
3311 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3312
Michal Vasko3a41dff2020-07-15 14:30:28 +02003313 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3314 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003315 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003316 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3317 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003318 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003319
3320 /* copy flags and add LYD_NEW */
3321 match_trg->flags = sibling_src->flags | LYD_NEW;
3322 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003323 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003324 /* update value */
3325 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003326 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003327
3328 /* copy flags and add LYD_NEW */
3329 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003330 } else {
3331 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003332 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003333 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 +02003334 }
3335 }
3336 } else {
3337 /* node not found, merge it */
3338 if (options & LYD_MERGE_DESTRUCT) {
3339 dup_src = (struct lyd_node *)sibling_src;
3340 lyd_unlink_tree(dup_src);
3341 /* spend it */
3342 *sibling_src_p = NULL;
3343 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003344 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 +02003345 }
3346
3347 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003348 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003349 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003350 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003351 }
3352
3353 lyd_insert_node(parent_trg, first_trg, dup_src);
3354 }
3355
3356 return LY_SUCCESS;
3357}
3358
Michal Vasko3a41dff2020-07-15 14:30:28 +02003359static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003360lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003361{
3362 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003363 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003364
3365 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3366
3367 if (!source) {
3368 /* nothing to merge */
3369 return LY_SUCCESS;
3370 }
3371
Michal Vasko831422b2020-11-24 11:20:51 +01003372 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003373 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003374 return LY_EINVAL;
3375 }
3376
3377 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003378 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003379 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3380 if (first && !sibling_src) {
3381 /* source was spent (unlinked), move to the next node */
3382 source = tmp;
3383 }
3384
Michal Vasko3a41dff2020-07-15 14:30:28 +02003385 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003386 break;
3387 }
3388 }
3389
3390 if (options & LYD_MERGE_DESTRUCT) {
3391 /* free any leftover source data that were not merged */
3392 lyd_free_siblings((struct lyd_node *)source);
3393 }
3394
3395 return LY_SUCCESS;
3396}
3397
Michal Vasko3a41dff2020-07-15 14:30:28 +02003398API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003399lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003400{
3401 return lyd_merge(target, source, options, 1);
3402}
3403
3404API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003405lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003406{
3407 return lyd_merge(target, source, options, 0);
3408}
3409
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003410static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003411lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003412{
Michal Vasko14654712020-02-06 08:35:21 +01003413 /* ending \0 */
3414 ++reqlen;
3415
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003416 if (reqlen > *buflen) {
3417 if (is_static) {
3418 return LY_EINCOMPLETE;
3419 }
3420
3421 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3422 if (!*buffer) {
3423 return LY_EMEM;
3424 }
3425
3426 *buflen = reqlen;
3427 }
3428
3429 return LY_SUCCESS;
3430}
3431
Michal Vaskod59035b2020-07-08 12:00:06 +02003432LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003433lyd_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 +02003434{
3435 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003436 size_t len;
3437 const char *val;
3438 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003439
Radek Krejcia1c1e542020-09-29 16:06:52 +02003440 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003441 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003442 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003443 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003444
3445 quot = '\'';
3446 if (strchr(val, '\'')) {
3447 quot = '"';
3448 }
3449 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003450 }
3451
3452 return LY_SUCCESS;
3453}
3454
3455/**
3456 * @brief Append leaf-list value predicate to path.
3457 *
3458 * @param[in] node Node to print.
3459 * @param[in,out] buffer Buffer to print to.
3460 * @param[in,out] buflen Current buffer length.
3461 * @param[in,out] bufused Current number of characters used in @p buffer.
3462 * @param[in] is_static Whether buffer is static or can be reallocated.
3463 * @return LY_ERR
3464 */
3465static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003466lyd_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 +02003467{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003468 size_t len;
3469 const char *val;
3470 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003471
Michal Vaskob7be7a82020-08-20 09:09:04 +02003472 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003473 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003474 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003475
3476 quot = '\'';
3477 if (strchr(val, '\'')) {
3478 quot = '"';
3479 }
3480 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3481
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003482 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003483}
3484
3485/**
3486 * @brief Append node position (relative to its other instances) predicate to path.
3487 *
3488 * @param[in] node Node to print.
3489 * @param[in,out] buffer Buffer to print to.
3490 * @param[in,out] buflen Current buffer length.
3491 * @param[in,out] bufused Current number of characters used in @p buffer.
3492 * @param[in] is_static Whether buffer is static or can be reallocated.
3493 * @return LY_ERR
3494 */
3495static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003496lyd_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 +02003497{
3498 const struct lyd_node *first, *iter;
3499 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003500 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003501 char *val = NULL;
3502 LY_ERR rc;
3503
3504 if (node->parent) {
3505 first = node->parent->child;
3506 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003507 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003508 }
3509 pos = 1;
3510 for (iter = first; iter != node; iter = iter->next) {
3511 if (iter->schema == node->schema) {
3512 ++pos;
3513 }
3514 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003515 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003516 return LY_EMEM;
3517 }
3518
3519 len = 1 + strlen(val) + 1;
3520 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3521 if (rc != LY_SUCCESS) {
3522 goto cleanup;
3523 }
3524
3525 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3526
3527cleanup:
3528 free(val);
3529 return rc;
3530}
3531
3532API char *
3533lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3534{
Radek Krejci857189e2020-09-01 13:26:36 +02003535 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003536 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003537 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003538 const struct lyd_node *iter;
3539 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003540 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003541
3542 LY_CHECK_ARG_RET(NULL, node, NULL);
3543 if (buffer) {
3544 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3545 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003546 } else {
3547 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003548 }
3549
3550 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003551 case LYD_PATH_STD:
3552 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003553 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003554 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003555 ++depth;
3556 }
3557
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003558 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003559 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003560 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003561 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003562iter_print:
3563 /* print prefix and name */
3564 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003565 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003566 mod = iter->schema->module;
3567 }
3568
3569 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003570 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3571 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003572 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3573 if (rc != LY_SUCCESS) {
3574 break;
3575 }
3576
3577 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003578 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003579 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003580
Michal Vasko790b2bc2020-08-03 13:35:06 +02003581 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003582 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003583 switch (iter->schema->nodetype) {
3584 case LYS_LIST:
3585 if (iter->schema->flags & LYS_KEYLESS) {
3586 /* print its position */
3587 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3588 } else {
3589 /* print all list keys in predicates */
3590 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3591 }
3592 break;
3593 case LYS_LEAFLIST:
3594 if (iter->schema->flags & LYS_CONFIG_W) {
3595 /* print leaf-list value */
3596 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3597 } else {
3598 /* print its position */
3599 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3600 }
3601 break;
3602 default:
3603 /* nothing to print more */
3604 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003605 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003606 }
3607 if (rc != LY_SUCCESS) {
3608 break;
3609 }
3610
Michal Vasko14654712020-02-06 08:35:21 +01003611 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003612 }
3613 break;
3614 }
3615
3616 return buffer;
3617}
Michal Vaskoe444f752020-02-10 12:20:06 +01003618
Michal Vasko25a32822020-07-09 15:48:22 +02003619API struct lyd_meta *
3620lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3621{
3622 struct lyd_meta *ret = NULL;
3623 const struct ly_ctx *ctx;
3624 const char *prefix, *tmp;
3625 char *str;
3626 size_t pref_len, name_len;
3627
3628 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3629
3630 if (!first) {
3631 return NULL;
3632 }
3633
3634 ctx = first->annotation->module->ctx;
3635
3636 /* parse the name */
3637 tmp = name;
3638 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3639 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3640 return NULL;
3641 }
3642
3643 /* find the module */
3644 if (prefix) {
3645 str = strndup(prefix, pref_len);
3646 module = ly_ctx_get_module_latest(ctx, str);
3647 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003648 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 +02003649 }
3650
3651 /* find the metadata */
3652 LY_LIST_FOR(first, first) {
3653 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3654 ret = (struct lyd_meta *)first;
3655 break;
3656 }
3657 }
3658
3659 return ret;
3660}
3661
Michal Vasko9b368d32020-02-14 13:53:31 +01003662API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003663lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3664{
3665 struct lyd_node **match_p;
3666 struct lyd_node_inner *parent;
3667
Michal Vaskof03ed032020-03-04 13:31:44 +01003668 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003669
Michal Vasko6da1e952020-12-09 18:14:38 +01003670 if (!siblings || (siblings->schema && target->schema &&
3671 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003672 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003673 if (match) {
3674 *match = NULL;
3675 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003676 return LY_ENOTFOUND;
3677 }
3678
3679 /* find first sibling */
3680 if (siblings->parent) {
3681 siblings = siblings->parent->child;
3682 } else {
3683 while (siblings->prev->next) {
3684 siblings = siblings->prev;
3685 }
3686 }
3687
Michal Vasko9e685082021-01-29 14:49:09 +01003688 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003689 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003690 assert(target->hash);
3691
3692 /* find by hash */
3693 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003694 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003695 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003696 siblings = *match_p;
3697 } else {
3698 siblings = NULL;
3699 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003700 } else {
3701 /* not found */
3702 siblings = NULL;
3703 }
3704 } else {
3705 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003706 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003707 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003708 break;
3709 }
3710 }
3711 }
3712
3713 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003714 if (match) {
3715 *match = NULL;
3716 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003717 return LY_ENOTFOUND;
3718 }
3719
Michal Vasko9b368d32020-02-14 13:53:31 +01003720 if (match) {
3721 *match = (struct lyd_node *)siblings;
3722 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003723 return LY_SUCCESS;
3724}
3725
Radek Krejci857189e2020-09-01 13:26:36 +02003726/**
3727 * @brief Comparison callback to match schema node with a schema of a data node.
3728 *
3729 * @param[in] val1_p Pointer to the schema node
3730 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003731 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003732 */
3733static ly_bool
3734lyd_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 +01003735{
3736 struct lysc_node *val1;
3737 struct lyd_node *val2;
3738
3739 val1 = *((struct lysc_node **)val1_p);
3740 val2 = *((struct lyd_node **)val2_p);
3741
Michal Vasko90932a92020-02-12 14:33:03 +01003742 if (val1 == val2->schema) {
3743 /* schema match is enough */
3744 return 1;
3745 } else {
3746 return 0;
3747 }
3748}
3749
Michal Vasko92239c72020-11-18 18:17:25 +01003750/**
3751 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3752 * Uses hashes - should be used whenever possible for best performance.
3753 *
3754 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3755 * @param[in] schema Target data node schema to find.
3756 * @param[out] match Can be NULL, otherwise the found data node.
3757 * @return LY_SUCCESS on success, @p match set.
3758 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3759 * @return LY_ERR value if another error occurred.
3760 */
Michal Vasko90932a92020-02-12 14:33:03 +01003761static LY_ERR
3762lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3763{
3764 struct lyd_node **match_p;
3765 struct lyd_node_inner *parent;
3766 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003767 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003768
Michal Vaskob104f112020-07-17 09:54:54 +02003769 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003770
Michal Vasko9e685082021-01-29 14:49:09 +01003771 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003772 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003773 /* calculate our hash */
3774 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3775 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3776 hash = dict_hash_multi(hash, NULL, 0);
3777
3778 /* use special hash table function */
3779 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3780
3781 /* find by hash */
3782 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3783 siblings = *match_p;
3784 } else {
3785 /* not found */
3786 siblings = NULL;
3787 }
3788
3789 /* set the original hash table compare function back */
3790 lyht_set_cb(parent->children_ht, ht_cb);
3791 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003792 /* find first sibling */
3793 if (siblings->parent) {
3794 siblings = siblings->parent->child;
3795 } else {
3796 while (siblings->prev->next) {
3797 siblings = siblings->prev;
3798 }
3799 }
3800
3801 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003802 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003803 if (siblings->schema == schema) {
3804 /* schema match is enough */
3805 break;
3806 }
3807 }
3808 }
3809
3810 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003811 if (match) {
3812 *match = NULL;
3813 }
Michal Vasko90932a92020-02-12 14:33:03 +01003814 return LY_ENOTFOUND;
3815 }
3816
Michal Vasko9b368d32020-02-14 13:53:31 +01003817 if (match) {
3818 *match = (struct lyd_node *)siblings;
3819 }
Michal Vasko90932a92020-02-12 14:33:03 +01003820 return LY_SUCCESS;
3821}
3822
Michal Vaskoe444f752020-02-10 12:20:06 +01003823API LY_ERR
3824lyd_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 +02003825 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003826{
3827 LY_ERR rc;
3828 struct lyd_node *target = NULL;
3829
Michal Vasko4c583e82020-07-17 12:16:14 +02003830 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003831
Michal Vasko08fd6132020-11-18 18:17:45 +01003832 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003833 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003834 if (match) {
3835 *match = NULL;
3836 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003837 return LY_ENOTFOUND;
3838 }
3839
Michal Vaskof03ed032020-03-04 13:31:44 +01003840 if (key_or_value && !val_len) {
3841 val_len = strlen(key_or_value);
3842 }
3843
Michal Vaskob104f112020-07-17 09:54:54 +02003844 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3845 /* create a data node and find the instance */
3846 if (schema->nodetype == LYS_LEAFLIST) {
3847 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003848 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3849 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003850 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003851 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003852 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003853 }
3854
3855 /* find it */
3856 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003857 } else {
3858 /* find the first schema node instance */
3859 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003860 }
3861
Michal Vaskoe444f752020-02-10 12:20:06 +01003862 lyd_free_tree(target);
3863 return rc;
3864}
Michal Vaskoccc02342020-05-21 10:09:21 +02003865
3866API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003867lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3868{
3869 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3870
3871 for ( ; first; first = first->next) {
3872 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
3873 break;
3874 }
3875 }
3876
3877 if (match) {
3878 *match = (struct lyd_node *)first;
3879 }
3880 return first ? LY_SUCCESS : LY_ENOTFOUND;
3881}
3882
3883API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02003884lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3885{
3886 LY_ERR ret = LY_SUCCESS;
3887 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003888 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003889 uint32_t i;
3890
3891 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3892
3893 memset(&xp_set, 0, sizeof xp_set);
3894
3895 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003896 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3897 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003898
3899 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01003900 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 +02003901 LY_CHECK_GOTO(ret, cleanup);
3902
3903 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003904 ret = ly_set_new(set);
3905 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003906
3907 /* transform into ly_set */
3908 if (xp_set.type == LYXP_SET_NODE_SET) {
3909 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3910 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003911 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003912 (*set)->size = xp_set.used;
3913
3914 for (i = 0; i < xp_set.used; ++i) {
3915 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003916 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003917 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003918 }
3919 }
3920 }
3921
3922cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003923 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003924 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01003925 if (ret) {
3926 ly_set_free(*set, NULL);
3927 *set = NULL;
3928 }
Michal Vaskoccc02342020-05-21 10:09:21 +02003929 return ret;
3930}
Radek Krejcica989142020-11-05 11:32:22 +01003931
Michal Vasko3e1f6552021-01-14 09:27:55 +01003932API LY_ERR
3933lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3934{
3935 LY_ERR ret = LY_SUCCESS;
3936 struct lyxp_expr *expr = NULL;
3937 struct ly_path *lypath = NULL;
3938
3939 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3940
3941 /* parse the path */
3942 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
3943 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
3944 LY_CHECK_GOTO(ret, cleanup);
3945
3946 /* compile the path */
Radek Krejcid5d37432021-03-12 13:46:40 +01003947 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, LY_PATH_LREF_FALSE,
Michal Vasko3e1f6552021-01-14 09:27:55 +01003948 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
3949 (void *)LYD_CTX(ctx_node), NULL, &lypath);
3950 LY_CHECK_GOTO(ret, cleanup);
3951
3952 /* evaluate the path */
3953 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
3954
3955cleanup:
3956 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3957 ly_path_free(LYD_CTX(ctx_node), lypath);
3958 return ret;
3959}
3960
Radek Krejcica989142020-11-05 11:32:22 +01003961API uint32_t
3962lyd_list_pos(const struct lyd_node *instance)
3963{
3964 const struct lyd_node *iter = NULL;
3965 uint32_t pos = 0;
3966
3967 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3968 return 0;
3969 }
3970
3971 /* data instances are ordered, so we can stop when we found instance of other schema node */
3972 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01003973 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01003974 /* overrun to the end of the siblings list */
3975 break;
3976 }
3977 ++pos;
3978 }
3979
3980 return pos;
3981}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003982
3983API struct lyd_node *
3984lyd_first_sibling(const struct lyd_node *node)
3985{
3986 struct lyd_node *start;
3987
3988 if (!node) {
3989 return NULL;
3990 }
3991
3992 /* get the first sibling */
3993 if (node->parent) {
3994 start = node->parent->child;
3995 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01003996 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003997 }
3998
3999 return start;
4000}