blob: 2760a65625a161bc63fc5eda281a1e1fd5106796 [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"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020049#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "xml.h"
51#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020052
Michal Vaskob104f112020-07-17 09:54:54 +020053static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020054 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020055
Radek Krejci084289f2019-07-09 17:35:30 +020056LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020057lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
58 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010059 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020060{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020061 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020062 struct ly_err_item *err = NULL;
Michal Vasko25d6ad02020-10-22 12:20:22 +020063 uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020064
Michal Vaskofeca4fb2020-10-05 08:58:40 +020065 if (incomplete) {
66 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020067 }
68
Michal Vasko405cc9e2020-12-01 12:01:27 +010069 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 +010070 if (dynamic) {
71 *dynamic = 0;
72 }
73
Michal Vasko90932a92020-02-12 14:33:03 +010074 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020075 if (incomplete) {
76 *incomplete = 1;
77 }
78 } else if (ret) {
79 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010080 LOGVAL(ctx, err->vecode, err->msg);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020081 ly_err_free(err);
82 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +010083 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020084 }
85 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010086 }
87
Michal Vaskofeca4fb2020-10-05 08:58:40 +020088 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010089}
90
Radek Krejci38d85362019-09-05 16:26:38 +020091LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010093 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020094{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020095 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +020096 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +020097
Michal Vaskofeca4fb2020-10-05 08:58:40 +020098 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +010099
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200100 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
101 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200102 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100103 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejci38d85362019-09-05 16:26:38 +0200104 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200105 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100106 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
Radek Krejci38d85362019-09-05 16:26:38 +0200107 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200108 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200109 }
110
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200111 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200112}
113
Michal Vaskof937cfe2020-08-03 16:07:12 +0200114LY_ERR
115_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 +0200116 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200117{
118 LY_ERR rc = LY_SUCCESS;
119 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200120 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200121 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200122
123 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
124
125 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
126 LOGARG(ctx, node);
127 return LY_EINVAL;
128 }
129
Michal Vasko22df3f02020-08-24 13:29:22 +0200130 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200131 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100132 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200133 if (rc == LY_EINCOMPLETE) {
134 /* actually success since we do not provide the context tree and call validation with
135 * LY_TYPE_OPTS_INCOMPLETE_DATA */
136 rc = LY_SUCCESS;
137 } else if (rc && err) {
138 if (ctx) {
139 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100140 LOG_LOCSET(NULL, NULL, err->path, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100141 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100142 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200143 }
Radek Krejci73dead22019-07-11 16:46:16 +0200144 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200145 }
146
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200147 if (!rc) {
148 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
149 }
Radek Krejci084289f2019-07-09 17:35:30 +0200150 return rc;
151}
152
153API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200154lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
155{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200156 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200157}
158
159API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100160lyd_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 +0200161 const struct lyd_node *tree, const struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200162{
163 LY_ERR rc;
164 struct ly_err_item *err = NULL;
165 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200166 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200167 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200168
169 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
170
Michal Vasko22df3f02020-08-24 13:29:22 +0200171 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200172 /* store */
173 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 +0100174 LYD_HINT_DATA, node->schema, &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200175 if (rc == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 stored = 1;
177
178 /* resolve */
Michal Vasko9e685082021-01-29 14:49:09 +0100179 rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, &node->node, tree, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200180 }
181
182 if (rc) {
Radek Krejci73dead22019-07-11 16:46:16 +0200183 if (err) {
184 if (ctx) {
Michal Vasko9e685082021-01-29 14:49:09 +0100185 LOG_LOCSET(NULL, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100186 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100187 LOG_LOCBACK(0, 1, 0, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200188 }
Radek Krejci73dead22019-07-11 16:46:16 +0200189 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200190 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200191 if (stored) {
192 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
193 }
Radek Krejci73dead22019-07-11 16:46:16 +0200194 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200195 }
196
Michal Vasko3701af52020-08-03 14:29:38 +0200197 if (realtype) {
Michal Vasko29134f82020-11-13 18:03:20 +0100198 if (val.realtype->basetype == LY_TYPE_UNION) {
199 *realtype = val.subvalue->value.realtype;
200 } else {
201 *realtype = val.realtype;
202 }
Michal Vasko3701af52020-08-03 14:29:38 +0200203 }
204
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200205 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200206 return LY_SUCCESS;
207}
208
209API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200210lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200211{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200212 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200213 struct ly_ctx *ctx;
214 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200215 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200216
217 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
218
219 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200220 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200221
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200222 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100223 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100224 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 +0100225 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200226 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200227
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200228 /* compare values */
229 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200230
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200231 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200232 return ret;
233}
234
Radek Krejci19611252020-10-04 13:54:53 +0200235API ly_bool
236lyd_is_default(const struct lyd_node *node)
237{
238 const struct lysc_node_leaf *leaf;
239 const struct lysc_node_leaflist *llist;
240 const struct lyd_node_term *term;
241 LY_ARRAY_COUNT_TYPE u;
242
243 assert(node->schema->nodetype & LYD_NODE_TERM);
244 term = (const struct lyd_node_term *)node;
245
246 if (node->schema->nodetype == LYS_LEAF) {
247 leaf = (const struct lysc_node_leaf *)node->schema;
248 if (!leaf->dflt) {
249 return 0;
250 }
251
252 /* compare with the default value */
253 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
254 return 0;
255 }
256 } else {
257 llist = (const struct lysc_node_leaflist *)node->schema;
258 if (!llist->dflts) {
259 return 0;
260 }
261
262 LY_ARRAY_FOR(llist->dflts, u) {
263 /* compare with each possible default value */
264 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
265 return 0;
266 }
267 }
268 }
269
270 return 1;
271}
272
Radek Krejci7931b192020-06-25 17:05:03 +0200273static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200274lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200275{
Michal Vasko69730152020-10-09 16:30:07 +0200276 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200277 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200278 const char *path = in->method.fpath.filepath;
279 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200280
281 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200282 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200283
Radek Krejcif13b87b2020-12-01 22:02:17 +0100284 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
285 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200286 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100287 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
288 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200289 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100290 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
291 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200292 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200293 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200294 }
295
Radek Krejci7931b192020-06-25 17:05:03 +0200296 return format;
297}
Radek Krejcie7b95092019-05-15 11:03:07 +0200298
Michal Vaskoe0665742021-02-11 11:08:44 +0100299/**
300 * @brief Parse YANG data into a data tree.
301 *
302 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100303 * @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 +0100304 * @param[in] parent Parent to connect the parsed nodes to, if any.
305 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
306 * @param[in] in Input handle to read the input from.
307 * @param[in] format Expected format of the data in @p in.
308 * @param[in] parse_opts Options for parser.
309 * @param[in] val_opts Options for validation.
310 * @param[out] op Optional pointer to the parsed operation, if any.
311 * @return LY_ERR value.
312 */
313static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100314lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
315 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 +0200316{
Michal Vaskoe0665742021-02-11 11:08:44 +0100317 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200318 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100319 struct ly_set parsed = {0};
320 struct lyd_node *first;
321 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200322
Michal Vaskoe0665742021-02-11 11:08:44 +0100323 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200324
325 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100326 if (first_p) {
327 *first_p = NULL;
328 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200329
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 /* remember input position */
331 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200332
Michal Vaskoe0665742021-02-11 11:08:44 +0100333 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200334 switch (format) {
335 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100336 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 +0200337 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200338 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100339 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 +0200340 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200341 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100342 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 +0200343 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200344 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100345 LOGARG(ctx, format);
346 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100347 break;
348 }
349 LY_CHECK_GOTO(rc, cleanup);
350
351 if (parent) {
352 /* get first top-level sibling */
353 for (first = parent; first->parent; first = lyd_parent(first)) {}
354 first = lyd_first_sibling(first);
355 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200356 }
357
Michal Vaskoe0665742021-02-11 11:08:44 +0100358 if (!(parse_opts & LYD_PARSE_ONLY)) {
359 /* validate data */
360 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types,
361 &lydctx->meta_types, NULL);
362 LY_CHECK_GOTO(rc, cleanup);
363 }
Radek Krejci7931b192020-06-25 17:05:03 +0200364
Michal Vaskoe0665742021-02-11 11:08:44 +0100365 /* set the operation node */
366 if (op) {
367 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200368 }
369
370cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100371 if (lydctx) {
372 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200373 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 if (rc) {
375 if (parent) {
376 /* free all the parsed subtrees */
377 for (i = 0; i < parsed.count; ++i) {
378 lyd_free_tree(parsed.dnodes[i]);
379 }
380 } else {
381 /* free everything */
382 lyd_free_all(*first_p);
383 *first_p = NULL;
384 }
385 }
386 ly_set_erase(&parsed, NULL);
387 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200388}
389
390API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100391lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
392 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
393{
394 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
395
396 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
397 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
398 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
399
400 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
401}
402
403API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100404lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
405 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
406{
407 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
408 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
409 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
410
Radek Krejcif16e2542021-02-17 15:39:23 +0100411 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100412}
413
414API LY_ERR
415lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
416 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200417{
418 LY_ERR ret;
419 struct ly_in *in;
420
421 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100422 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200423
424 ly_in_free(in, 0);
425 return ret;
426}
427
428API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200429lyd_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 +0200430 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200431{
432 LY_ERR ret;
433 struct ly_in *in;
434
435 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100436 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200437
438 ly_in_free(in, 0);
439 return ret;
440}
441
442API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200443lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
444 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200445{
446 LY_ERR ret;
447 struct ly_in *in;
448
449 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100450 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200451
452 ly_in_free(in, 0);
453 return ret;
454}
455
Radek Krejcif16e2542021-02-17 15:39:23 +0100456/**
457 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
458 * for schema nodes locked inside the extension instance.
459 *
460 * At least one of @p parent, @p tree, or @p op must always be set.
461 *
462 * Specific @p data_type values have different parameter meaning as follows:
463 * - ::LYD_TYPE_RPC_NETCONF:
464 * - @p parent - must be NULL, the whole RPC is expected;
465 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
466 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
467 * a separate opaque data tree, even if the function fails, this may be returned;
468 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
469 *
470 * - ::LYD_TYPE_NOTIF_NETCONF:
471 * - @p parent - must be NULL, the whole notification is expected;
472 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
473 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
474 * a separate opaque data tree, even if the function fails, this may be returned;
475 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
476 *
477 * - ::LYD_TYPE_REPLY_NETCONF:
478 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
479 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
480 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
481 * a separate opaque data tree, even if the function fails, this may be returned;
482 * - @p op - must be NULL, the reply is appended to the RPC;
483 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
484 *
485 * @param[in] ctx libyang context.
486 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
487 * @param[in] parent Optional parent to connect the parsed nodes to.
488 * @param[in] in Input handle to read the input from.
489 * @param[in] format Expected format of the data in @p in.
490 * @param[in] data_type Expected operation to parse (@ref datatype).
491 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
492 * @param[out] op Optional parsed operation node.
493 * @return LY_ERR value.
494 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
495 */
496static LY_ERR
497lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
498 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 +0200499{
Michal Vaskoe0665742021-02-11 11:08:44 +0100500 LY_ERR rc = LY_SUCCESS;
501 struct lyd_ctx *lydctx = NULL;
502 struct ly_set parsed = {0};
503 struct lyd_node *first = NULL, *envp = NULL;
504 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200505
Michal Vasko27fb0262021-02-23 09:42:01 +0100506 if (!ctx) {
507 ctx = LYD_CTX(parent);
508 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200509 if (tree) {
510 *tree = NULL;
511 }
512 if (op) {
513 *op = NULL;
514 }
515
Radek Krejci7931b192020-06-25 17:05:03 +0200516 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200517
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 /* remember input position */
519 in->func_start = in->current;
520
Michal Vaskoe0665742021-02-11 11:08:44 +0100521 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100522 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100523 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100524 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
525 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
526 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100527 }
528 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
529 val_opts = 0;
530
531 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200532 switch (format) {
533 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100534 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 +0100535 if (rc && envp) {
536 /* special situation when the envelopes were parsed successfully */
537 if (tree) {
538 *tree = envp;
539 }
540 ly_set_erase(&parsed, NULL);
541 return rc;
542 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100543 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200544 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100545 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100546 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200547 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100548 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100549 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200550 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100551 LOGARG(ctx, format);
552 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200553 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200554 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100555 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200556
Michal Vaskoe0665742021-02-11 11:08:44 +0100557 /* set out params correctly */
558 if (tree) {
559 if (envp) {
560 /* special out param meaning */
561 *tree = envp;
562 } else {
563 *tree = parent ? NULL : first;
564 }
565 }
566 if (op) {
567 *op = lydctx->op_node;
568 }
569
570cleanup:
571 if (lydctx) {
572 lydctx->free(lydctx);
573 }
574 if (rc) {
575 if (parent) {
576 /* free all the parsed subtrees */
577 for (i = 0; i < parsed.count; ++i) {
578 lyd_free_tree(parsed.dnodes[i]);
579 }
580 } else {
581 /* free everything (cannot occur in the current code, a safety) */
582 lyd_free_all(first);
583 if (tree) {
584 *tree = NULL;
585 }
586 if (op) {
587 *op = NULL;
588 }
589 }
590 }
591 ly_set_erase(&parsed, NULL);
592 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200593}
Radek Krejci084289f2019-07-09 17:35:30 +0200594
Radek Krejcif16e2542021-02-17 15:39:23 +0100595API LY_ERR
596lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
597 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
598{
599 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
600
601 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
602}
603
604API LY_ERR
605lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
606 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
607{
608 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
609
610 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
611
612 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
613}
614
Michal Vasko90932a92020-02-12 14:33:03 +0100615LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200616lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
617 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100618{
619 LY_ERR ret;
620 struct lyd_node_term *term;
621
Michal Vasko9b368d32020-02-14 13:53:31 +0100622 assert(schema->nodetype & LYD_NODE_TERM);
623
Michal Vasko90932a92020-02-12 14:33:03 +0100624 term = calloc(1, sizeof *term);
625 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
626
627 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100628 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100629 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100630
Radek Krejciddace2c2021-01-08 11:30:56 +0100631 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200632 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100633 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100634 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200635 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100636 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100637
Michal Vasko9e685082021-01-29 14:49:09 +0100638 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100639 return ret;
640}
641
642LY_ERR
643lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
644{
645 LY_ERR ret;
646 struct lyd_node_term *term;
647 struct lysc_type *type;
648
649 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200650 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100651
652 term = calloc(1, sizeof *term);
653 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
654
655 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100656 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100657 term->flags = LYD_NEW;
658
659 type = ((struct lysc_node_leaf *)schema)->type;
660 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
661 if (ret) {
662 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
663 free(term);
664 return ret;
665 }
Michal Vasko9e685082021-01-29 14:49:09 +0100666 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100667
Michal Vasko9e685082021-01-29 14:49:09 +0100668 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100669 return ret;
670}
671
672LY_ERR
673lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
674{
675 struct lyd_node_inner *in;
676
Michal Vasko9b368d32020-02-14 13:53:31 +0100677 assert(schema->nodetype & LYD_NODE_INNER);
678
Michal Vasko90932a92020-02-12 14:33:03 +0100679 in = calloc(1, sizeof *in);
680 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
681
682 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100683 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100684 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100685 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
686 in->flags |= LYD_DEFAULT;
687 }
Michal Vasko90932a92020-02-12 14:33:03 +0100688
Michal Vasko9b368d32020-02-14 13:53:31 +0100689 /* do not hash list with keys, we need them for the hash */
690 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100691 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100692 }
Michal Vasko90932a92020-02-12 14:33:03 +0100693
Michal Vasko9e685082021-01-29 14:49:09 +0100694 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100695 return LY_SUCCESS;
696}
697
Michal Vasko90932a92020-02-12 14:33:03 +0100698LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200699lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100700{
701 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100702 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200703 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100704
Michal Vasko004d3152020-06-11 19:59:22 +0200705 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100706
707 /* create list */
708 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
709
Radek Krejciddace2c2021-01-08 11:30:56 +0100710 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100711
Michal Vasko90932a92020-02-12 14:33:03 +0100712 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200713 LY_ARRAY_FOR(predicates, u) {
714 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100715 lyd_insert_node(list, NULL, key);
716 }
717
Michal Vasko9b368d32020-02-14 13:53:31 +0100718 /* hash having all the keys */
719 lyd_hash(list);
720
Michal Vasko90932a92020-02-12 14:33:03 +0100721 /* success */
722 *node = list;
723 list = NULL;
724
725cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100726 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100727 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200728 return ret;
729}
730
731static LY_ERR
732lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
733{
734 LY_ERR ret = LY_SUCCESS;
735 struct lyxp_expr *expr = NULL;
736 uint16_t exp_idx = 0;
737 enum ly_path_pred_type pred_type = 0;
738 struct ly_path_predicate *predicates = NULL;
739
Radek Krejciddace2c2021-01-08 11:30:56 +0100740 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100741
Michal Vasko004d3152020-06-11 19:59:22 +0200742 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200743 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 +0200744 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200745
746 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200747 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
748 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200749
750 /* create the list node */
751 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
752
753cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100754 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200755 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200756 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100757 return ret;
758}
759
760LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100761lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
762 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100763{
Michal Vasko366a4a12020-12-04 16:23:57 +0100764 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100765 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100766 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100767
Michal Vasko9b368d32020-02-14 13:53:31 +0100768 assert(schema->nodetype & LYD_NODE_ANY);
769
Michal Vasko90932a92020-02-12 14:33:03 +0100770 any = calloc(1, sizeof *any);
771 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
772
773 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100774 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100775 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100776
Radek Krejci1798aae2020-07-14 13:26:06 +0200777 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100778
779 if (use_value) {
780 any->value.str = value;
781 any->value_type = value_type;
782 } else {
783 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100784 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100785 LY_CHECK_ERR_RET(ret, free(any), ret);
786 }
Michal Vasko9e685082021-01-29 14:49:09 +0100787 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100788
Michal Vasko9e685082021-01-29 14:49:09 +0100789 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100790 return LY_SUCCESS;
791}
792
Michal Vasko52927e22020-03-16 17:26:14 +0100793LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100794lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
795 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
796 LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100797{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200798 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100799 struct lyd_node_opaq *opaq;
800
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200801 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100802
803 if (!value_len) {
804 value = "";
805 }
806
807 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100808 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100809
Michal Vasko9e685082021-01-29 14:49:09 +0100810 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100811 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200812
Michal Vasko52927e22020-03-16 17:26:14 +0100813 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100814 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100815 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200816 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100817 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200818 }
Michal Vasko52927e22020-03-16 17:26:14 +0100819 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200820 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100821 *dynamic = 0;
822 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200823 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100824 }
Michal Vasko501af032020-11-11 20:27:44 +0100825
826 opaq->format = format;
827 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200828 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100829 opaq->ctx = ctx;
830
Radek Krejci011e4aa2020-09-04 15:22:31 +0200831finish:
832 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100833 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100834 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200835 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100836 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200837 }
838 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100839}
840
Michal Vasko3a41dff2020-07-15 14:30:28 +0200841API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100842lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
843 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100844{
845 struct lyd_node *ret = NULL;
846 const struct lysc_node *schema;
847 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
848
Michal Vasko6027eb92020-07-15 16:37:30 +0200849 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100850
Michal Vaskof03ed032020-03-04 13:31:44 +0100851 if (!module) {
852 module = parent->schema->module;
853 }
854
Michal Vasko3a41dff2020-07-15 14:30:28 +0200855 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100856 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200857 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100858
Michal Vasko3a41dff2020-07-15 14:30:28 +0200859 LY_CHECK_RET(lyd_create_inner(schema, &ret));
860 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100861 lyd_insert_node(parent, NULL, ret);
862 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200863
864 if (node) {
865 *node = ret;
866 }
867 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100868}
869
Michal Vasko3a41dff2020-07-15 14:30:28 +0200870API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100871lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
872 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100873{
874 struct lyd_node *ret = NULL, *key;
875 const struct lysc_node *schema, *key_s;
876 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
877 va_list ap;
878 const char *key_val;
879 LY_ERR rc = LY_SUCCESS;
880
Michal Vasko6027eb92020-07-15 16:37:30 +0200881 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100882
Michal Vaskof03ed032020-03-04 13:31:44 +0100883 if (!module) {
884 module = parent->schema->module;
885 }
886
Radek Krejci41ac9942020-11-02 14:47:56 +0100887 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 +0200888 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100889
890 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200891 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100892
Michal Vasko3a41dff2020-07-15 14:30:28 +0200893 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100894
895 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100896 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 +0100897 key_val = va_arg(ap, const char *);
898
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200899 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
900 NULL, &key);
901 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100902 lyd_insert_node(ret, NULL, key);
903 }
904
Michal Vasko013a8182020-03-03 10:46:53 +0100905 if (parent) {
906 lyd_insert_node(parent, NULL, ret);
907 }
908
909cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200910 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100911 if (rc) {
912 lyd_free_tree(ret);
913 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200914 } else if (node) {
915 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100916 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200917 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100918}
919
Michal Vasko3a41dff2020-07-15 14:30:28 +0200920API LY_ERR
921lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +0100922 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100923{
924 struct lyd_node *ret = NULL;
925 const struct lysc_node *schema;
926 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
927
Michal Vasko6027eb92020-07-15 16:37:30 +0200928 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100929
Michal Vaskof03ed032020-03-04 13:31:44 +0100930 if (!module) {
931 module = parent->schema->module;
932 }
Michal Vasko004d3152020-06-11 19:59:22 +0200933 if (!keys) {
934 keys = "";
935 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100936
Michal Vasko004d3152020-06-11 19:59:22 +0200937 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +0100938 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 +0200939 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100940
Michal Vasko004d3152020-06-11 19:59:22 +0200941 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
942 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200943 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200944 } else {
945 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200946 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200947 }
Michal Vasko004d3152020-06-11 19:59:22 +0200948 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100949 lyd_insert_node(parent, NULL, ret);
950 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200951
952 if (node) {
953 *node = ret;
954 }
955 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100956}
957
Michal Vasko3a41dff2020-07-15 14:30:28 +0200958API LY_ERR
959lyd_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 +0100960 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100961{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200962 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100963 struct lyd_node *ret = NULL;
964 const struct lysc_node *schema;
965 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
966
Michal Vasko6027eb92020-07-15 16:37:30 +0200967 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100968
Michal Vaskof03ed032020-03-04 13:31:44 +0100969 if (!module) {
970 module = parent->schema->module;
971 }
972
Radek Krejci41ac9942020-11-02 14:47:56 +0100973 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 +0200974 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100975
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200976 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL,
977 &ret);
978 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200979 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100980 lyd_insert_node(parent, NULL, ret);
981 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982
983 if (node) {
984 *node = ret;
985 }
986 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100987}
988
Michal Vasko3a41dff2020-07-15 14:30:28 +0200989API LY_ERR
Michal Vasko219006c2020-12-04 16:26:52 +0100990lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, void *value,
Radek Krejci41ac9942020-11-02 14:47:56 +0100991 LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100992{
993 struct lyd_node *ret = NULL;
994 const struct lysc_node *schema;
995 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
996
Michal Vasko6027eb92020-07-15 16:37:30 +0200997 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100998
Michal Vaskof03ed032020-03-04 13:31:44 +0100999 if (!module) {
1000 module = parent->schema->module;
1001 }
1002
Radek Krejci41ac9942020-11-02 14:47:56 +01001003 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 +02001004 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001005
Michal Vasko366a4a12020-12-04 16:23:57 +01001006 LY_CHECK_RET(lyd_create_any(schema, value, value_type, 1, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001007 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001008 lyd_insert_node(parent, NULL, ret);
1009 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001010
1011 if (node) {
1012 *node = ret;
1013 }
1014 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001015}
1016
Michal Vasko4490d312020-06-16 13:08:55 +02001017/**
1018 * @brief Update node value.
1019 *
1020 * @param[in] node Node to update.
1021 * @param[in] value New value to set.
1022 * @param[in] value_type Type of @p value for any node.
1023 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1024 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1025 * @return LY_ERR value.
1026 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001027static LY_ERR
1028lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001029 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001030{
1031 LY_ERR ret = LY_SUCCESS;
1032 struct lyd_node *new_any;
1033
1034 switch (node->schema->nodetype) {
1035 case LYS_CONTAINER:
1036 case LYS_NOTIF:
1037 case LYS_RPC:
1038 case LYS_ACTION:
1039 case LYS_LIST:
1040 case LYS_LEAFLIST:
1041 /* if it exists, there is nothing to update */
1042 *new_parent = NULL;
1043 *new_node = NULL;
1044 break;
1045 case LYS_LEAF:
1046 ret = lyd_change_term(node, value);
1047 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1048 /* there was an actual change (at least of the default flag) */
1049 *new_parent = node;
1050 *new_node = node;
1051 ret = LY_SUCCESS;
1052 } else if (ret == LY_ENOT) {
1053 /* no change */
1054 *new_parent = NULL;
1055 *new_node = NULL;
1056 ret = LY_SUCCESS;
1057 } /* else error */
1058 break;
1059 case LYS_ANYDATA:
1060 case LYS_ANYXML:
1061 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001062 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001063
1064 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001065 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001066 /* not equal, switch values (so that we can use generic node free) */
1067 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1068 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1069 ((struct lyd_node_any *)node)->value.str = value;
1070 ((struct lyd_node_any *)node)->value_type = value_type;
1071
1072 *new_parent = node;
1073 *new_node = node;
1074 } else {
1075 /* they are equal */
1076 *new_parent = NULL;
1077 *new_node = NULL;
1078 }
1079 lyd_free_tree(new_any);
1080 break;
1081 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001082 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001083 ret = LY_EINT;
1084 break;
1085 }
1086
1087 return ret;
1088}
1089
Michal Vasko3a41dff2020-07-15 14:30:28 +02001090API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001091lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1092 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001093{
Michal Vaskod86997b2020-05-26 15:19:54 +02001094 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001095 size_t pref_len, name_len;
1096
Michal Vasko2b5344c2021-02-26 10:12:05 +01001097 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1098 if (!ctx) {
1099 ctx = LYD_CTX(parent);
1100 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001101
Michal Vasko871a0252020-11-11 18:35:24 +01001102 if (parent && !parent->schema) {
1103 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1104 return LY_EINVAL;
1105 }
Michal Vasko610553d2020-11-18 18:15:05 +01001106 if (meta) {
1107 *meta = NULL;
1108 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001109
1110 /* parse the name */
1111 tmp = name;
1112 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1113 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001114 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001115 }
1116
1117 /* find the module */
1118 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001119 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001120 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001121 }
1122
1123 /* set value if none */
1124 if (!val_str) {
1125 val_str = "";
1126 }
1127
Michal Vasko871a0252020-11-11 18:35:24 +01001128 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
1129 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1130}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001131
Michal Vaskoba696702020-11-11 19:12:45 +01001132API LY_ERR
1133lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1134 struct lyd_meta **meta)
1135{
1136 const struct lys_module *mod;
1137
1138 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1139
1140 if (parent && !parent->schema) {
1141 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1142 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001143 }
Michal Vasko610553d2020-11-18 18:15:05 +01001144 if (meta) {
1145 *meta = NULL;
1146 }
Michal Vaskoba696702020-11-11 19:12:45 +01001147
1148 switch (attr->format) {
1149 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001150 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001151 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001152 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001153 return LY_ENOTFOUND;
1154 }
1155 break;
1156 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001157 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001158 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001159 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001160 return LY_ENOTFOUND;
1161 }
1162 break;
1163 default:
1164 LOGINT_RET(ctx);
1165 }
1166
Michal Vaskoad92b672020-11-12 13:11:31 +01001167 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 +01001168 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001169}
1170
Michal Vasko3a41dff2020-07-15 14:30:28 +02001171API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001172lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001173 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001174{
1175 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001176
Michal Vasko0ab974d2021-02-24 13:18:26 +01001177 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 +02001178
1179 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001180 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001181 }
1182 if (!value) {
1183 value = "";
1184 }
1185
Michal Vasko0ab974d2021-02-24 13:18:26 +01001186 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1187 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001188 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001189 lyd_insert_node(parent, NULL, ret);
1190 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001191
1192 if (node) {
1193 *node = ret;
1194 }
1195 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001196}
1197
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001199lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001200 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001201{
1202 struct lyd_node *ret = NULL;
1203
1204 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1205
1206 if (!ctx) {
1207 ctx = LYD_CTX(parent);
1208 }
1209 if (!value) {
1210 value = "";
1211 }
1212
Michal Vasko0ab974d2021-02-24 13:18:26 +01001213 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1214 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001215 if (parent) {
1216 lyd_insert_node(parent, NULL, ret);
1217 }
1218
1219 if (node) {
1220 *node = ret;
1221 }
1222 return LY_SUCCESS;
1223}
1224
1225API LY_ERR
1226lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001227 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001228{
Radek Krejci1798aae2020-07-14 13:26:06 +02001229 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001230 const struct ly_ctx *ctx;
1231 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001232 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001233
Michal Vasko3a41dff2020-07-15 14:30:28 +02001234 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001235
Michal Vaskob7be7a82020-08-20 09:09:04 +02001236 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001237
1238 /* parse the name */
1239 tmp = name;
1240 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001241 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001242 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001243 }
1244
Michal Vasko51d21b82020-11-13 18:03:54 +01001245 /* get the module */
1246 if (module_name) {
1247 mod_len = strlen(module_name);
1248 } else {
1249 module_name = prefix;
1250 mod_len = pref_len;
1251 }
1252
Michal Vasko00cbf532020-06-15 13:58:47 +02001253 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001254 if (!value) {
1255 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001256 }
1257
Michal Vasko8d65f852021-02-17 11:28:13 +01001258 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1259 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1260
1261 if (attr) {
1262 *attr = ret;
1263 }
1264 return LY_SUCCESS;
1265}
1266
1267API LY_ERR
1268lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1269 struct lyd_attr **attr)
1270{
1271 struct lyd_attr *ret = NULL;
1272 const struct ly_ctx *ctx;
1273 const char *prefix, *tmp;
1274 size_t pref_len, name_len;
1275
1276 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1277
1278 ctx = LYD_CTX(parent);
1279
1280 /* parse the name */
1281 tmp = name;
1282 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1283 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1284 return LY_EVALID;
1285 }
1286
1287 /* set value if none */
1288 if (!value) {
1289 value = "";
1290 }
1291
1292 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1293 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294
1295 if (attr) {
1296 *attr = ret;
1297 }
1298 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001299}
1300
1301API LY_ERR
1302lyd_change_term(struct lyd_node *term, const char *val_str)
1303{
1304 LY_ERR ret = LY_SUCCESS;
1305 struct lysc_type *type;
1306 struct lyd_node_term *t;
1307 struct lyd_node *parent;
1308 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001309 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001310
1311 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1312
1313 if (!val_str) {
1314 val_str = "";
1315 }
1316 t = (struct lyd_node_term *)term;
1317 type = ((struct lysc_node_leaf *)term->schema)->type;
1318
1319 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001320 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001321 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 +01001322 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001323 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001324 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001325
1326 /* compare original and new value */
1327 if (type->plugin->compare(&t->value, &val)) {
1328 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001329 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001330 t->value = val;
1331 memset(&val, 0, sizeof val);
1332 val_change = 1;
1333 } else {
1334 val_change = 0;
1335 }
1336
1337 /* always clear the default flag */
1338 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001339 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001340 parent->flags &= ~LYD_DEFAULT;
1341 }
1342 dflt_change = 1;
1343 } else {
1344 dflt_change = 0;
1345 }
1346
1347 if (val_change || dflt_change) {
1348 /* make the node non-validated */
1349 term->flags &= LYD_NEW;
1350 }
1351
1352 if (val_change) {
1353 if (term->schema->nodetype == LYS_LEAFLIST) {
1354 /* leaf-list needs to be hashed again and re-inserted into parent */
1355 lyd_unlink_hash(term);
1356 lyd_hash(term);
1357 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1358 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1359 /* list needs to be updated if its key was changed */
1360 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001361 lyd_unlink_hash(lyd_parent(term));
1362 lyd_hash(lyd_parent(term));
1363 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001364 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1365 }
1366
1367 /* retrun value */
1368 if (!val_change) {
1369 if (dflt_change) {
1370 /* only default flag change */
1371 ret = LY_EEXIST;
1372 } else {
1373 /* no change */
1374 ret = LY_ENOT;
1375 }
1376 } /* else value changed, LY_SUCCESS */
1377
1378cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001379 if (val.realtype) {
1380 type->plugin->free(LYD_CTX(term), &val);
1381 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001382 return ret;
1383}
1384
Michal Vasko41586352020-07-13 13:54:25 +02001385API LY_ERR
1386lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1387{
1388 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001389 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001390 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001391 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001392
1393 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1394
1395 if (!val_str) {
1396 val_str = "";
1397 }
1398
1399 /* parse the new value into a new meta structure */
1400 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 +01001401 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001402
1403 /* compare original and new value */
1404 if (lyd_compare_meta(meta, m2)) {
1405 /* values differ, switch them */
1406 val = meta->value;
1407 meta->value = m2->value;
1408 m2->value = val;
1409 val_change = 1;
1410 } else {
1411 val_change = 0;
1412 }
1413
1414 /* retrun value */
1415 if (!val_change) {
1416 /* no change */
1417 ret = LY_ENOT;
1418 } /* else value changed, LY_SUCCESS */
1419
1420cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001421 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001422 return ret;
1423}
1424
Michal Vasko3a41dff2020-07-15 14:30:28 +02001425API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001426lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02001427 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001428{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001429 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001430}
1431
Michal Vasko6741dc62021-02-04 09:27:45 +01001432static LY_ERR
1433lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1434{
1435 LY_ERR ret = LY_SUCCESS, r;
1436 const struct lysc_node *schema;
1437 struct ly_path_predicate *pred;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001438 LY_ARRAY_COUNT_TYPE u, new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001439
1440 assert(path);
1441
1442 /* go through all the compiled nodes */
1443 LY_ARRAY_FOR(path, u) {
1444 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001445 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001446 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1447 if (schema->flags & LYS_KEYLESS) {
1448 /* creating a new keyless list instance */
1449 break;
1450 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1451 LOG_LOCSET(schema, NULL, NULL, NULL);
1452 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1453 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1454 LOG_LOCBACK(1, 0, 0, 0);
1455 return LY_EINVAL;
1456 } /* else creating an opaque list without keys */
1457 }
1458 }
1459
Michal Vasko6f9b4262021-02-04 12:09:56 +01001460 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001461 /* parse leafref value into a predicate, if not defined in the path */
1462 if (!value) {
1463 value = "";
1464 }
1465
1466 r = LY_SUCCESS;
1467 if (options & LYD_NEW_PATH_OPAQ) {
1468 r = lys_value_validate(NULL, schema, value, strlen(value));
1469 }
1470 if (!r) {
1471 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001472 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1473 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001474
1475 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1476 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1477 LY_CHECK_GOTO(ret, cleanup);
1478 ++((struct lysc_type *)pred->value.realtype)->refcount;
1479
1480 if (schema->flags & LYS_CONFIG_R) {
1481 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001482 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001483 }
1484 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1485 }
1486
1487 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001488 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001489 LY_ARRAY_DECREMENT(path);
1490 }
1491
1492cleanup:
1493 return ret;
1494}
1495
Michal Vasko00cbf532020-06-15 13:58:47 +02001496API LY_ERR
1497lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001498 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001499{
1500 LY_ERR ret = LY_SUCCESS, r;
1501 struct lyxp_expr *exp = NULL;
1502 struct ly_path *p = NULL;
1503 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1504 const struct lysc_node *schema;
Michal Vasko6741dc62021-02-04 09:27:45 +01001505 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count;
Michal Vasko00cbf532020-06-15 13:58:47 +02001506
1507 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1508
1509 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001510 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001511 }
1512
1513 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001514 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 +02001515 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001516
1517 /* compile path */
1518 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001519 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 +01001520 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001521
Michal Vasko6741dc62021-02-04 09:27:45 +01001522 /* check the compiled path before searching existing nodes, it may be shortened */
1523 orig_count = LY_ARRAY_COUNT(p);
1524 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001525
1526 /* try to find any existing nodes in the path */
1527 if (parent) {
1528 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1529 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001530 if (orig_count == LY_ARRAY_COUNT(p)) {
1531 /* the node exists, are we supposed to update it or is it just a default? */
1532 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1533 LOG_LOCSET(NULL, node, NULL, NULL);
1534 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1535 LOG_LOCBACK(0, 1, 0, 0);
1536 ret = LY_EEXIST;
1537 goto cleanup;
1538 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001539
Michal Vasko6741dc62021-02-04 09:27:45 +01001540 /* update the existing node */
1541 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1542 goto cleanup;
1543 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001544 } else if (ret == LY_EINCOMPLETE) {
1545 /* some nodes were found, adjust the iterator to the next segment */
1546 ++path_idx;
1547 } else if (ret == LY_ENOTFOUND) {
1548 /* 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 +01001549 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001550 node = parent;
1551 }
1552 } else {
1553 /* error */
1554 goto cleanup;
1555 }
1556 }
1557
Michal Vasko6741dc62021-02-04 09:27:45 +01001558 /* restore the full path for creating new nodes */
1559 while (orig_count > LY_ARRAY_COUNT(p)) {
1560 LY_ARRAY_INCREMENT(p);
1561 }
1562
Michal Vasko00cbf532020-06-15 13:58:47 +02001563 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001564 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001565 cur_parent = node;
1566 schema = p[path_idx].node;
1567
1568 switch (schema->nodetype) {
1569 case LYS_LIST:
1570 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001571 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001572 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001573 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1574 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1575 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001576 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001577 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1578 LOG_LOCSET(schema, NULL, NULL, NULL);
1579 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1580 lys_nodetype2str(schema->nodetype), schema->name, path);
1581 LOG_LOCBACK(1, 0, 0, 0);
1582 ret = LY_EINVAL;
1583 goto cleanup;
1584 }
1585
Michal Vasko00cbf532020-06-15 13:58:47 +02001586 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1587 }
1588 break;
1589 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001590 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001591 case LYS_CONTAINER:
1592 case LYS_NOTIF:
1593 case LYS_RPC:
1594 case LYS_ACTION:
1595 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1596 break;
1597 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001598 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001599 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001600 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1601 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1602 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001603 } else {
1604 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1605 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1606 }
1607 break;
1608 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001609 if (lysc_is_key(schema)) {
1610 /* it must have been already created or some error will occur later */
1611 assert(cur_parent);
1612 node = lyd_child(cur_parent);
1613 assert(node && (node->schema == schema));
1614 goto next_iter;
1615 }
1616
1617 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001618 if (!value) {
1619 value = "";
1620 }
1621
1622 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001623 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001624 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001625 }
1626 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001627 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1628 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001629 } else {
1630 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001631 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1632 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001633 }
1634 break;
1635 case LYS_ANYDATA:
1636 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001637 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001638 break;
1639 default:
1640 LOGINT(ctx);
1641 ret = LY_EINT;
1642 goto cleanup;
1643 }
1644
1645 if (cur_parent) {
1646 /* connect to the parent */
1647 lyd_insert_node(cur_parent, NULL, node);
1648 } else if (parent) {
1649 /* connect to top-level siblings */
1650 lyd_insert_node(NULL, &parent, node);
1651 }
1652
Michal Vasko35880332020-12-08 13:08:12 +01001653next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001654 /* update remembered nodes */
1655 if (!nparent) {
1656 nparent = node;
1657 }
1658 nnode = node;
1659 }
1660
1661cleanup:
1662 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001663 if (p) {
1664 while (orig_count > LY_ARRAY_COUNT(p)) {
1665 LY_ARRAY_INCREMENT(p);
1666 }
1667 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001668 ly_path_free(ctx, p);
1669 if (!ret) {
1670 /* set out params only on success */
1671 if (new_parent) {
1672 *new_parent = nparent;
1673 }
1674 if (new_node) {
1675 *new_node = nnode;
1676 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001677 } else {
1678 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001679 }
1680 return ret;
1681}
1682
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001683LY_ERR
1684lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001685 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, uint32_t impl_opts,
Radek Krejci0f969882020-08-21 16:56:47 +02001686 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001687{
1688 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001689 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001690 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001691 struct lyd_value **dflts;
1692 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001693 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001694
1695 assert(first && (parent || sparent || mod));
1696
1697 if (!sparent && parent) {
1698 sparent = parent->schema;
1699 }
1700
Michal Vasko630d9892020-12-08 17:11:08 +01001701 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1702 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1703 getnext_opts |= LYS_GETNEXT_OUTPUT;
1704 }
1705
1706 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001707 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1708 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001709 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1710 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001711 }
1712
1713 switch (iter->nodetype) {
1714 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001715 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1716 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001717 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001718 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vasko69730152020-10-09 16:30:07 +02001719 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001720 } else if (node) {
1721 /* create any default data in the existing case */
1722 assert(node->schema->parent->nodetype == LYS_CASE);
1723 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_types, node_when,
1724 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001725 }
1726 break;
1727 case LYS_CONTAINER:
1728 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1729 /* create default NP container */
1730 LY_CHECK_RET(lyd_create_inner(iter, &node));
Radek Krejci9a3823e2021-01-27 20:26:46 +01001731 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001732 lyd_insert_node(parent, first, node);
1733
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001734 if (lysc_node_when(iter) && node_when) {
1735 /* remember to resolve when */
1736 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1737 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001738 if (diff) {
1739 /* add into diff */
1740 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1741 }
1742
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001743 /* create any default children */
Michal Vaskoe0665742021-02-11 11:08:44 +01001744 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_types, node_when,
Michal Vasko69730152020-10-09 16:30:07 +02001745 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001746 }
1747 break;
1748 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001749 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1750 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001751 /* create default leaf */
1752 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1753 if (ret == LY_EINCOMPLETE) {
1754 if (node_types) {
1755 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001756 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001757 }
1758 } else if (ret) {
1759 return ret;
1760 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001761 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001762 lyd_insert_node(parent, first, node);
1763
Radek Krejci9a3823e2021-01-27 20:26:46 +01001764 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001765 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001766 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001767 }
1768 if (diff) {
1769 /* add into diff */
1770 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1771 }
1772 }
1773 break;
1774 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001775 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1776 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001777 /* create all default leaf-lists */
1778 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1779 LY_ARRAY_FOR(dflts, u) {
1780 ret = lyd_create_term2(iter, dflts[u], &node);
1781 if (ret == LY_EINCOMPLETE) {
1782 if (node_types) {
1783 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001784 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001785 }
1786 } else if (ret) {
1787 return ret;
1788 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001789 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001790 lyd_insert_node(parent, first, node);
1791
Radek Krejci9a3823e2021-01-27 20:26:46 +01001792 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001793 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001794 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001795 }
1796 if (diff) {
1797 /* add into diff */
1798 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1799 }
1800 }
1801 }
1802 break;
1803 default:
1804 /* without defaults */
1805 break;
1806 }
1807 }
1808
1809 return LY_SUCCESS;
1810}
1811
1812API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001813lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001814{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001815 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001816 struct lyd_node *node;
1817 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001818
1819 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1820 if (diff) {
1821 *diff = NULL;
1822 }
1823
Michal Vasko56daf732020-08-10 10:57:18 +02001824 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001825 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001826 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1827 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001828 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, NULL,
Michal Vasko3488ada2020-12-03 14:18:19 +01001829 &node_when, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001830 }
1831
Michal Vasko56daf732020-08-10 10:57:18 +02001832 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001833 }
1834
Michal Vasko3488ada2020-12-03 14:18:19 +01001835 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001836 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01001837
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001838cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001839 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001840 if (ret && diff) {
1841 lyd_free_all(*diff);
1842 *diff = NULL;
1843 }
1844 return ret;
1845}
1846
1847API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001848lyd_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 +02001849{
1850 const struct lys_module *mod;
1851 struct lyd_node *d = NULL;
1852 uint32_t i = 0;
1853 LY_ERR ret = LY_SUCCESS;
1854
1855 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1856 if (diff) {
1857 *diff = NULL;
1858 }
1859 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001860 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001861 }
1862
1863 /* add nodes for each module one-by-one */
1864 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1865 if (!mod->implemented) {
1866 continue;
1867 }
1868
1869 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1870 if (d) {
1871 /* merge into one diff */
1872 lyd_insert_sibling(*diff, d, diff);
1873
1874 d = NULL;
1875 }
1876 }
1877
1878cleanup:
1879 if (ret && diff) {
1880 lyd_free_all(*diff);
1881 *diff = NULL;
1882 }
1883 return ret;
1884}
1885
1886API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001887lyd_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 +02001888{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001889 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001890 struct lyd_node *root, *d = NULL;
1891 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001892
1893 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1894 if (diff) {
1895 *diff = NULL;
1896 }
1897
1898 /* add all top-level defaults for this module */
Michal Vasko3488ada2020-12-03 14:18:19 +01001899 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, &node_when, implicit_options, diff), cleanup);
1900
1901 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001902 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001903
1904 /* process nested nodes */
1905 LY_LIST_FOR(*tree, root) {
1906 /* skip added default nodes */
1907 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1908 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1909
1910 if (d) {
1911 /* merge into one diff */
1912 lyd_insert_sibling(*diff, d, diff);
1913
1914 d = NULL;
1915 }
1916 }
1917 }
1918
1919cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001920 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001921 if (ret && diff) {
1922 lyd_free_all(*diff);
1923 *diff = NULL;
1924 }
1925 return ret;
1926}
1927
Michal Vasko90932a92020-02-12 14:33:03 +01001928struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001929lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001930{
Michal Vaskob104f112020-07-17 09:54:54 +02001931 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001932 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001933 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01001934 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01001935
Michal Vaskob104f112020-07-17 09:54:54 +02001936 assert(new_node);
1937
1938 if (!first_sibling || !new_node->schema) {
1939 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001940 return NULL;
1941 }
1942
Michal Vasko93b16062020-12-09 18:14:18 +01001943 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001944 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01001945 getnext_opts = LYS_GETNEXT_OUTPUT;
1946 }
1947
Michal Vaskob104f112020-07-17 09:54:54 +02001948 if (first_sibling->parent && first_sibling->parent->children_ht) {
1949 /* find the anchor using hashes */
1950 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01001951 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001952 while (schema) {
1953 /* keep trying to find the first existing instance of the closest following schema sibling,
1954 * otherwise return NULL - inserting at the end */
1955 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1956 break;
1957 }
1958
Michal Vasko93b16062020-12-09 18:14:18 +01001959 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001960 }
1961 } else {
1962 /* find the anchor without hashes */
1963 match = (struct lyd_node *)first_sibling;
1964 if (!lysc_data_parent(new_node->schema)) {
1965 /* we are in top-level, skip all the data from preceding modules */
1966 LY_LIST_FOR(match, match) {
1967 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1968 break;
1969 }
1970 }
1971 }
1972
1973 /* get the first schema sibling */
1974 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01001975 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001976
1977 found = 0;
1978 LY_LIST_FOR(match, match) {
1979 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1980 /* we have found an opaque node, which must be at the end, so use it OR
1981 * modules do not match, so we must have traversed all the data from new_node module (if any),
1982 * we have found the first node of the next module, that is what we want */
1983 break;
1984 }
1985
1986 /* skip schema nodes until we find the instantiated one */
1987 while (!found) {
1988 if (new_node->schema == schema) {
1989 /* we have found the schema of the new node, continue search to find the first
1990 * data node with a different schema (after our schema) */
1991 found = 1;
1992 break;
1993 }
1994 if (match->schema == schema) {
1995 /* current node (match) is a data node still before the new node, continue search in data */
1996 break;
1997 }
Michal Vasko93b16062020-12-09 18:14:18 +01001998 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001999 assert(schema);
2000 }
2001
2002 if (found && (match->schema != new_node->schema)) {
2003 /* find the next node after we have found our node schema data instance */
2004 break;
2005 }
2006 }
Michal Vasko90932a92020-02-12 14:33:03 +01002007 }
2008
2009 return match;
2010}
2011
2012/**
2013 * @brief Insert node after a sibling.
2014 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002015 * Handles inserting into NP containers and key-less lists.
2016 *
Michal Vasko90932a92020-02-12 14:33:03 +01002017 * @param[in] sibling Sibling to insert after.
2018 * @param[in] node Node to insert.
2019 */
2020static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002021lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002022{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002023 struct lyd_node_inner *par;
2024
Michal Vasko90932a92020-02-12 14:33:03 +01002025 assert(!node->next && (node->prev == node));
2026
2027 node->next = sibling->next;
2028 node->prev = sibling;
2029 sibling->next = node;
2030 if (node->next) {
2031 /* sibling had a succeeding node */
2032 node->next->prev = node;
2033 } else {
2034 /* sibling was last, find first sibling and change its prev */
2035 if (sibling->parent) {
2036 sibling = sibling->parent->child;
2037 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002038 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002039 }
2040 sibling->prev = node;
2041 }
2042 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002043
Michal Vasko9f96a052020-03-10 09:41:45 +01002044 for (par = node->parent; par; par = par->parent) {
2045 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2046 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002047 par->flags &= ~LYD_DEFAULT;
2048 }
Michal Vaskob104f112020-07-17 09:54:54 +02002049 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002050 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002051 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002052 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002053 }
Michal Vasko90932a92020-02-12 14:33:03 +01002054}
2055
2056/**
2057 * @brief Insert node before a sibling.
2058 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002059 * Handles inserting into NP containers and key-less lists.
2060 *
Michal Vasko90932a92020-02-12 14:33:03 +01002061 * @param[in] sibling Sibling to insert before.
2062 * @param[in] node Node to insert.
2063 */
2064static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002065lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002066{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002067 struct lyd_node_inner *par;
2068
Michal Vasko90932a92020-02-12 14:33:03 +01002069 assert(!node->next && (node->prev == node));
2070
2071 node->next = sibling;
2072 /* covers situation of sibling being first */
2073 node->prev = sibling->prev;
2074 sibling->prev = node;
2075 if (node->prev->next) {
2076 /* sibling had a preceding node */
2077 node->prev->next = node;
2078 } else if (sibling->parent) {
2079 /* sibling was first and we must also change parent child pointer */
2080 sibling->parent->child = node;
2081 }
2082 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002083
Michal Vasko9f96a052020-03-10 09:41:45 +01002084 for (par = node->parent; par; par = par->parent) {
2085 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2086 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002087 par->flags &= ~LYD_DEFAULT;
2088 }
Michal Vaskob104f112020-07-17 09:54:54 +02002089 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002090 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002091 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002092 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002093 }
Michal Vasko90932a92020-02-12 14:33:03 +01002094}
2095
2096/**
Michal Vaskob104f112020-07-17 09:54:54 +02002097 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002098 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002099 * Handles inserting into NP containers and key-less lists.
2100 *
Michal Vasko90932a92020-02-12 14:33:03 +01002101 * @param[in] parent Parent to insert into.
2102 * @param[in] node Node to insert.
2103 */
2104static void
Michal Vaskob104f112020-07-17 09:54:54 +02002105lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002106{
2107 struct lyd_node_inner *par;
2108
Radek Krejcia1c1e542020-09-29 16:06:52 +02002109 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002110 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002111
2112 par = (struct lyd_node_inner *)parent;
2113
Michal Vaskob104f112020-07-17 09:54:54 +02002114 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002115 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002116
Michal Vaskod989ba02020-08-24 10:59:24 +02002117 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002118 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2119 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002120 par->flags &= ~LYD_DEFAULT;
2121 }
Michal Vasko52927e22020-03-16 17:26:14 +01002122 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002123 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002124 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002125 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002126 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002127}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002128
Michal Vasko751cb4d2020-07-14 12:25:28 +02002129/**
2130 * @brief Learn whether a list instance has all the keys.
2131 *
2132 * @param[in] list List instance to check.
2133 * @return non-zero if all the keys were found,
2134 * @return 0 otherwise.
2135 */
2136static int
2137lyd_insert_has_keys(const struct lyd_node *list)
2138{
2139 const struct lyd_node *key;
2140 const struct lysc_node *skey = NULL;
2141
2142 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002143 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002144 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2145 if (!key || (key->schema != skey)) {
2146 /* key missing */
2147 return 0;
2148 }
2149
2150 key = key->next;
2151 }
2152
2153 /* all keys found */
2154 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002155}
2156
2157void
Michal Vaskob104f112020-07-17 09:54:54 +02002158lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002159{
Michal Vaskob104f112020-07-17 09:54:54 +02002160 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002161
Michal Vaskob104f112020-07-17 09:54:54 +02002162 /* inserting list without its keys is not supported */
2163 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002164 assert(!parent || !parent->schema ||
2165 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002166
Michal Vaskob104f112020-07-17 09:54:54 +02002167 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002168 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002169 }
Michal Vasko90932a92020-02-12 14:33:03 +01002170
Michal Vaskob104f112020-07-17 09:54:54 +02002171 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002172 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002173
Michal Vaskob104f112020-07-17 09:54:54 +02002174 /* find the anchor, our next node, so we can insert before it */
2175 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2176 if (anchor) {
2177 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002178 if (!parent && (*first_sibling_p == anchor)) {
2179 /* move first sibling */
2180 *first_sibling_p = node;
2181 }
Michal Vaskob104f112020-07-17 09:54:54 +02002182 } else if (first_sibling) {
2183 lyd_insert_after_node(first_sibling->prev, node);
2184 } else if (parent) {
2185 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002186 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002187 *first_sibling_p = node;
2188 }
2189
2190 /* insert into parent HT */
2191 lyd_insert_hash(node);
2192
2193 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002194 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002195 lyd_hash(parent);
2196
2197 /* now we can insert even the list into its parent HT */
2198 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002199 }
Michal Vasko90932a92020-02-12 14:33:03 +01002200}
2201
Michal Vasko717a4c32020-12-07 09:40:10 +01002202/**
2203 * @brief Check schema place of a node to be inserted.
2204 *
2205 * @param[in] parent Schema node of the parent data node.
2206 * @param[in] sibling Schema node of a sibling data node.
2207 * @param[in] schema Schema node if the data node to be inserted.
2208 * @return LY_SUCCESS on success.
2209 * @return LY_EINVAL if the place is invalid.
2210 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002211static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002212lyd_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 +01002213{
2214 const struct lysc_node *par2;
2215
Michal Vasko62ed12d2020-05-21 10:08:25 +02002216 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002217 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2218 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002219
Michal Vasko717a4c32020-12-07 09:40:10 +01002220 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002221 /* opaque nodes can be inserted wherever */
2222 return LY_SUCCESS;
2223 }
2224
Michal Vasko717a4c32020-12-07 09:40:10 +01002225 if (!parent) {
2226 parent = lysc_data_parent(sibling);
2227 }
2228
Michal Vaskof03ed032020-03-04 13:31:44 +01002229 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002230 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002231
2232 if (parent) {
2233 /* inner node */
2234 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002235 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002236 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002237 return LY_EINVAL;
2238 }
2239 } else {
2240 /* top-level node */
2241 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002242 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002243 return LY_EINVAL;
2244 }
2245 }
2246
2247 return LY_SUCCESS;
2248}
2249
2250API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002251lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002252{
2253 struct lyd_node *iter;
2254
Michal Vasko0ab974d2021-02-24 13:18:26 +01002255 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002256
Michal Vasko717a4c32020-12-07 09:40:10 +01002257 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002258
Michal Vasko0ab974d2021-02-24 13:18:26 +01002259 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002260 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2261 return LY_EINVAL;
2262 }
2263
2264 if (node->parent || node->prev->next) {
2265 lyd_unlink_tree(node);
2266 }
2267
2268 while (node) {
2269 iter = node->next;
2270 lyd_unlink_tree(node);
2271 lyd_insert_node(parent, NULL, node);
2272 node = iter;
2273 }
2274 return LY_SUCCESS;
2275}
2276
2277API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002278lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002279{
2280 struct lyd_node *iter;
2281
Michal Vaskob104f112020-07-17 09:54:54 +02002282 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002283
Michal Vaskob104f112020-07-17 09:54:54 +02002284 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002285 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002286 }
2287
Michal Vasko553d0b52020-12-04 16:27:52 +01002288 if (sibling == node) {
2289 /* we need to keep the connection to siblings so we can insert into them */
2290 sibling = sibling->prev;
2291 }
2292
Michal Vaskob1b5c262020-03-05 14:29:47 +01002293 if (node->parent || node->prev->next) {
2294 lyd_unlink_tree(node);
2295 }
2296
2297 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002298 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002299 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002300 return LY_EINVAL;
2301 }
2302
Michal Vaskob1b5c262020-03-05 14:29:47 +01002303 iter = node->next;
2304 lyd_unlink_tree(node);
2305 lyd_insert_node(NULL, &sibling, node);
2306 node = iter;
2307 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002308
Michal Vaskob104f112020-07-17 09:54:54 +02002309 if (first) {
2310 /* find the first sibling */
2311 *first = sibling;
2312 while ((*first)->prev->next) {
2313 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002314 }
2315 }
2316
2317 return LY_SUCCESS;
2318}
2319
Michal Vaskob1b5c262020-03-05 14:29:47 +01002320API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002321lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2322{
Michal Vaskof03ed032020-03-04 13:31:44 +01002323 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2324
Michal Vasko717a4c32020-12-07 09:40:10 +01002325 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002326
Michal Vaskob104f112020-07-17 09:54:54 +02002327 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002328 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002329 return LY_EINVAL;
2330 }
2331
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002332 lyd_unlink_tree(node);
2333 lyd_insert_before_node(sibling, node);
2334 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002335
Michal Vaskof03ed032020-03-04 13:31:44 +01002336 return LY_SUCCESS;
2337}
2338
2339API LY_ERR
2340lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2341{
Michal Vaskof03ed032020-03-04 13:31:44 +01002342 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2343
Michal Vasko717a4c32020-12-07 09:40:10 +01002344 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002345
Michal Vaskob104f112020-07-17 09:54:54 +02002346 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002347 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002348 return LY_EINVAL;
2349 }
2350
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002351 lyd_unlink_tree(node);
2352 lyd_insert_after_node(sibling, node);
2353 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002354
Michal Vaskof03ed032020-03-04 13:31:44 +01002355 return LY_SUCCESS;
2356}
2357
2358API void
2359lyd_unlink_tree(struct lyd_node *node)
2360{
2361 struct lyd_node *iter;
2362
2363 if (!node) {
2364 return;
2365 }
2366
Michal Vaskob104f112020-07-17 09:54:54 +02002367 /* update hashes while still linked into the tree */
2368 lyd_unlink_hash(node);
2369
Michal Vaskof03ed032020-03-04 13:31:44 +01002370 /* unlink from siblings */
2371 if (node->prev->next) {
2372 node->prev->next = node->next;
2373 }
2374 if (node->next) {
2375 node->next->prev = node->prev;
2376 } else {
2377 /* unlinking the last node */
2378 if (node->parent) {
2379 iter = node->parent->child;
2380 } else {
2381 iter = node->prev;
2382 while (iter->prev != node) {
2383 iter = iter->prev;
2384 }
2385 }
2386 /* update the "last" pointer from the first node */
2387 iter->prev = node->prev;
2388 }
2389
2390 /* unlink from parent */
2391 if (node->parent) {
2392 if (node->parent->child == node) {
2393 /* the node is the first child */
2394 node->parent->child = node->next;
2395 }
2396
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002397 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002398 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2399 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002400 LY_LIST_FOR(node->parent->child, iter) {
2401 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2402 break;
2403 }
2404 }
2405 if (!iter) {
2406 node->parent->flags |= LYD_DEFAULT;
2407 }
2408 }
2409
Michal Vaskof03ed032020-03-04 13:31:44 +01002410 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002411 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002412 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002413 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002414 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002415 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002416 }
2417 }
2418
2419 node->parent = NULL;
2420 }
2421
2422 node->next = NULL;
2423 node->prev = node;
2424}
2425
Michal Vaskoa5da3292020-08-12 13:10:50 +02002426void
Michal Vasko871a0252020-11-11 18:35:24 +01002427lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002428{
2429 struct lyd_meta *last, *iter;
2430
2431 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002432
2433 if (!meta) {
2434 return;
2435 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002436
2437 for (iter = meta; iter; iter = iter->next) {
2438 iter->parent = parent;
2439 }
2440
2441 /* insert as the last attribute */
2442 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002443 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002444 last->next = meta;
2445 } else {
2446 parent->meta = meta;
2447 }
2448
2449 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002450 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002451 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002452 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002453 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002454}
2455
2456LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002457lyd_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 +02002458 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 +01002459 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002460{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002461 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002462 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002463 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002464 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002465
Michal Vasko9f96a052020-03-10 09:41:45 +01002466 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002467
Radek Krejciddace2c2021-01-08 11:30:56 +01002468 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002469
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002470 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002471 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002472 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002473 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002474 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002475 break;
2476 }
2477 }
2478 if (!ant) {
2479 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002480 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Michal Vasko90932a92020-02-12 14:33:03 +01002481 mod->name, name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002482 ret = LY_EINVAL;
2483 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002484 }
2485
Michal Vasko9f96a052020-03-10 09:41:45 +01002486 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002487 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002488 mt->parent = parent;
2489 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002490 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2491 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002492 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2493 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2494 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002495
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002496 /* insert as the last attribute */
2497 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002498 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002499 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002500 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002501 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002502 }
2503
Michal Vasko9f96a052020-03-10 09:41:45 +01002504 if (meta) {
2505 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002506 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002507
2508cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002509 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002510 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002511}
2512
Michal Vaskoa5da3292020-08-12 13:10:50 +02002513void
2514lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2515{
2516 struct lyd_attr *last, *iter;
2517 struct lyd_node_opaq *opaq;
2518
2519 assert(parent && !parent->schema);
2520
2521 if (!attr) {
2522 return;
2523 }
2524
2525 opaq = (struct lyd_node_opaq *)parent;
2526 for (iter = attr; iter; iter = iter->next) {
2527 iter->parent = opaq;
2528 }
2529
2530 /* insert as the last attribute */
2531 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002532 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002533 last->next = attr;
2534 } else {
2535 opaq->attr = attr;
2536 }
2537}
2538
Michal Vasko52927e22020-03-16 17:26:14 +01002539LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002540lyd_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 +01002541 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2542 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 +01002543{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002544 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002545 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002546
2547 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002548 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002549
2550 if (!value_len) {
2551 value = "";
2552 }
2553
2554 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002555 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002556
Michal Vaskoad92b672020-11-12 13:11:31 +01002557 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002558 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002559 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002560 }
2561 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002562 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002563 }
2564
Michal Vasko52927e22020-03-16 17:26:14 +01002565 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002566 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2567 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002568 *dynamic = 0;
2569 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002570 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002571 }
Michal Vasko501af032020-11-11 20:27:44 +01002572 at->format = format;
2573 at->val_prefix_data = val_prefix_data;
2574 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002575
2576 /* insert as the last attribute */
2577 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002578 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002579 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002580 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002581 last->next = at;
2582 }
2583
Radek Krejci011e4aa2020-09-04 15:22:31 +02002584finish:
2585 if (ret) {
2586 lyd_free_attr_single(ctx, at);
2587 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002588 *attr = at;
2589 }
2590 return LY_SUCCESS;
2591}
2592
Radek Krejci084289f2019-07-09 17:35:30 +02002593API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002594lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002595{
Michal Vasko004d3152020-06-11 19:59:22 +02002596 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002597
Michal Vasko004d3152020-06-11 19:59:22 +02002598 if (ly_path_eval(path, tree, &target)) {
2599 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002600 }
2601
Michal Vasko004d3152020-06-11 19:59:22 +02002602 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002603}
2604
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002605API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002606lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002607{
2608 const struct lyd_node *iter1, *iter2;
2609 struct lyd_node_term *term1, *term2;
2610 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002611 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002612 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002613
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002614 if (!node1 || !node2) {
2615 if (node1 == node2) {
2616 return LY_SUCCESS;
2617 } else {
2618 return LY_ENOT;
2619 }
2620 }
2621
Michal Vaskob7be7a82020-08-20 09:09:04 +02002622 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002623 return LY_ENOT;
2624 }
2625
2626 if (node1->hash != node2->hash) {
2627 return LY_ENOT;
2628 }
Michal Vasko52927e22020-03-16 17:26:14 +01002629 /* 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 +02002630
Michal Vasko52927e22020-03-16 17:26:14 +01002631 if (!node1->schema) {
2632 opaq1 = (struct lyd_node_opaq *)node1;
2633 opaq2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +01002634 if ((opaq1->name.name != opaq2->name.name) || (opaq1->format != opaq2->format) ||
2635 (opaq1->name.module_ns != opaq2->name.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002636 return LY_ENOT;
2637 }
Michal Vasko52927e22020-03-16 17:26:14 +01002638 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002639 case LY_PREF_XML:
2640 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 +01002641 return LY_ENOT;
2642 }
2643 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002644 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002645 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2646 if (strcmp(opaq1->value, opaq2->value)) {
2647 return LY_ENOT;
2648 }
2649 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002650 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002651 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002652 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002653 return LY_EINT;
2654 }
2655 if (options & LYD_COMPARE_FULL_RECURSION) {
2656 iter1 = opaq1->child;
2657 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002658 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002659 }
2660 return LY_SUCCESS;
2661 } else {
2662 switch (node1->schema->nodetype) {
2663 case LYS_LEAF:
2664 case LYS_LEAFLIST:
2665 if (options & LYD_COMPARE_DEFAULTS) {
2666 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2667 return LY_ENOT;
2668 }
2669 }
2670
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002671 term1 = (struct lyd_node_term *)node1;
2672 term2 = (struct lyd_node_term *)node2;
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002673 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002674 case LYS_CONTAINER:
2675 if (options & LYD_COMPARE_DEFAULTS) {
2676 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2677 return LY_ENOT;
2678 }
2679 }
2680 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002681 iter1 = lyd_child(node1);
2682 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002683 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002684 }
2685 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002686 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002687 case LYS_ACTION:
2688 if (options & LYD_COMPARE_FULL_RECURSION) {
2689 /* TODO action/RPC
2690 goto all_children_compare;
2691 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002692 }
2693 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002694 case LYS_NOTIF:
2695 if (options & LYD_COMPARE_FULL_RECURSION) {
2696 /* TODO Notification
2697 goto all_children_compare;
2698 */
2699 }
2700 return LY_SUCCESS;
2701 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01002702 iter1 = lyd_child(node1);
2703 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002704
2705 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2706 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01002707 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002708 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002709 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002710 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002711 return LY_ENOT;
2712 }
2713 iter1 = iter1->next;
2714 iter2 = iter2->next;
2715 }
2716 } else {
2717 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2718
Radek Krejci0f969882020-08-21 16:56:47 +02002719all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002720 if (!iter1 && !iter2) {
2721 /* no children, nothing to compare */
2722 return LY_SUCCESS;
2723 }
2724
Michal Vaskod989ba02020-08-24 10:59:24 +02002725 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002726 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002727 return LY_ENOT;
2728 }
2729 }
2730 if (iter1 || iter2) {
2731 return LY_ENOT;
2732 }
2733 }
2734 return LY_SUCCESS;
2735 case LYS_ANYXML:
2736 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002737 any1 = (struct lyd_node_any *)node1;
2738 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002739
2740 if (any1->value_type != any2->value_type) {
2741 return LY_ENOT;
2742 }
2743 switch (any1->value_type) {
2744 case LYD_ANYDATA_DATATREE:
2745 iter1 = any1->value.tree;
2746 iter2 = any2->value.tree;
2747 goto all_children_compare;
2748 case LYD_ANYDATA_STRING:
2749 case LYD_ANYDATA_XML:
2750 case LYD_ANYDATA_JSON:
2751 len1 = strlen(any1->value.str);
2752 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002753 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002754 return LY_ENOT;
2755 }
2756 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002757 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002758 len1 = lyd_lyb_data_length(any1->value.mem);
2759 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002760 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002761 return LY_ENOT;
2762 }
2763 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002764 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002765 }
2766 }
2767
Michal Vaskob7be7a82020-08-20 09:09:04 +02002768 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002769 return LY_EINT;
2770}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002771
Michal Vasko21725742020-06-29 11:49:25 +02002772API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002773lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002774{
Michal Vaskod989ba02020-08-24 10:59:24 +02002775 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002776 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2777 }
2778
Michal Vasko11a81432020-07-28 16:31:29 +02002779 if (node1 == node2) {
2780 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002781 }
Michal Vasko11a81432020-07-28 16:31:29 +02002782 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002783}
2784
2785API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002786lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2787{
2788 if (!meta1 || !meta2) {
2789 if (meta1 == meta2) {
2790 return LY_SUCCESS;
2791 } else {
2792 return LY_ENOT;
2793 }
2794 }
2795
Michal Vaskoa8083062020-11-06 17:22:10 +01002796 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002797 return LY_ENOT;
2798 }
2799
Michal Vasko21725742020-06-29 11:49:25 +02002800 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2801}
2802
Radek Krejci22ebdba2019-07-25 13:59:43 +02002803/**
Michal Vasko52927e22020-03-16 17:26:14 +01002804 * @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 +02002805 *
2806 * 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 +02002807 *
2808 * @param[in] node Original node to duplicate
2809 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2810 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2811 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2812 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2813 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002814 */
Michal Vasko52927e22020-03-16 17:26:14 +01002815static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002816lyd_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 +02002817 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002818{
Michal Vasko52927e22020-03-16 17:26:14 +01002819 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002820 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002821 struct lyd_meta *meta;
2822 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002823
Michal Vasko52927e22020-03-16 17:26:14 +01002824 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002825
Michal Vasko52927e22020-03-16 17:26:14 +01002826 if (!node->schema) {
2827 dup = calloc(1, sizeof(struct lyd_node_opaq));
2828 } else {
2829 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002830 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002831 case LYS_ACTION:
2832 case LYS_NOTIF:
2833 case LYS_CONTAINER:
2834 case LYS_LIST:
2835 dup = calloc(1, sizeof(struct lyd_node_inner));
2836 break;
2837 case LYS_LEAF:
2838 case LYS_LEAFLIST:
2839 dup = calloc(1, sizeof(struct lyd_node_term));
2840 break;
2841 case LYS_ANYDATA:
2842 case LYS_ANYXML:
2843 dup = calloc(1, sizeof(struct lyd_node_any));
2844 break;
2845 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002846 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002847 ret = LY_EINT;
2848 goto error;
2849 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002850 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002851 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002852
Michal Vaskof6df0a02020-06-16 13:08:34 +02002853 if (options & LYD_DUP_WITH_FLAGS) {
2854 dup->flags = node->flags;
2855 } else {
2856 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2857 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002858 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002859 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002860
Michal Vasko25a32822020-07-09 15:48:22 +02002861 /* duplicate metadata */
2862 if (!(options & LYD_DUP_NO_META)) {
2863 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002864 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002865 }
2866 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002867
2868 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002869 if (!dup->schema) {
2870 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2871 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2872 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002873
2874 if (options & LYD_DUP_RECURSIVE) {
2875 /* duplicate all the children */
2876 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002877 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002878 }
2879 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002880 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002881 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01002882 if (orig->name.prefix) {
2883 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002884 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002885 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 +01002886 if (orig->val_prefix_data) {
2887 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
2888 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002889 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002890 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002891 opaq->ctx = orig->ctx;
2892 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2893 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2894 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2895
2896 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002897 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002898 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002899 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2900 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2901 struct lyd_node *child;
2902
2903 if (options & LYD_DUP_RECURSIVE) {
2904 /* duplicate all the children */
2905 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002906 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002907 }
Michal Vasko69730152020-10-09 16:30:07 +02002908 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002909 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002910 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01002911 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002912 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002913 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002914 if (!child) {
2915 /* possibly not keys are present in filtered tree */
2916 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002917 } else if (child->schema != key) {
2918 /* possibly not all keys are present in filtered tree,
2919 * but there can be also some non-key nodes */
2920 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002921 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002922 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002923 child = child->next;
2924 }
2925 }
2926 lyd_hash(dup);
2927 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002928 dup->hash = node->hash;
2929 any = (struct lyd_node_any *)node;
2930 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002931 }
2932
Michal Vasko52927e22020-03-16 17:26:14 +01002933 /* insert */
2934 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002935
2936 if (dup_p) {
2937 *dup_p = dup;
2938 }
2939 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002940
2941error:
Michal Vasko52927e22020-03-16 17:26:14 +01002942 lyd_free_tree(dup);
2943 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002944}
2945
Michal Vasko3a41dff2020-07-15 14:30:28 +02002946static LY_ERR
2947lyd_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 +02002948 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002949{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002950 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002951 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002952
2953 *dup_parent = NULL;
2954 *local_parent = NULL;
2955
2956 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2957 if (parent && (parent->schema == orig_parent->schema)) {
2958 /* stop creating parents, connect what we have into the provided parent */
2959 iter = parent;
2960 repeat = 0;
2961 } else {
2962 iter = NULL;
2963 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002964 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002965 }
2966 if (!*local_parent) {
2967 *local_parent = (struct lyd_node_inner *)iter;
2968 }
2969 if (iter->child) {
2970 /* 1) list - add after keys
2971 * 2) provided parent with some children */
2972 iter->child->prev->next = *dup_parent;
2973 if (*dup_parent) {
2974 (*dup_parent)->prev = iter->child->prev;
2975 iter->child->prev = *dup_parent;
2976 }
2977 } else {
2978 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2979 }
2980 if (*dup_parent) {
2981 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2982 }
2983 *dup_parent = (struct lyd_node *)iter;
2984 }
2985
2986 if (repeat && parent) {
2987 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002988 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002989 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002990 return LY_EINVAL;
2991 }
2992
2993 return LY_SUCCESS;
2994}
2995
2996static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002997lyd_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 +02002998{
2999 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003000 const struct lyd_node *orig; /* original node to be duplicated */
3001 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003002 struct lyd_node *top = NULL; /* the most higher created node */
3003 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003004
Michal Vasko3a41dff2020-07-15 14:30:28 +02003005 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003006
3007 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003008 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003009 } else {
3010 local_parent = parent;
3011 }
3012
Radek Krejci22ebdba2019-07-25 13:59:43 +02003013 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003014 if (lysc_is_key(orig->schema)) {
3015 if (local_parent) {
3016 /* the key must already exist in the parent */
3017 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3018 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3019 } else {
3020 assert(!(options & LYD_DUP_WITH_PARENTS));
3021 /* duplicating a single key, okay, I suppose... */
3022 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3023 LY_CHECK_GOTO(rc, error);
3024 }
3025 } else {
3026 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003027 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003028 LY_CHECK_GOTO(rc, error);
3029 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003030 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003031 break;
3032 }
3033 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003034
3035 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003036 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003037 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003038 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003039 }
3040 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003041
3042 if (dup) {
3043 *dup = first;
3044 }
3045 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003046
3047error:
3048 if (top) {
3049 lyd_free_tree(top);
3050 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003051 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003052 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003053 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003054}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003055
Michal Vasko3a41dff2020-07-15 14:30:28 +02003056API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003057lyd_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 +02003058{
3059 return lyd_dup(node, parent, options, 1, dup);
3060}
3061
3062API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003063lyd_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 +02003064{
3065 return lyd_dup(node, parent, options, 0, dup);
3066}
3067
3068API LY_ERR
3069lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003070{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003071 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003072 struct lyd_meta *mt, *last;
3073
Michal Vasko3a41dff2020-07-15 14:30:28 +02003074 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003075
3076 /* create a copy */
3077 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003078 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003079 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003080 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003081 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3082 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003083
3084 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003085 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003086 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003087 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003088 last->next = mt;
3089 } else {
3090 node->meta = mt;
3091 }
3092
Radek Krejci011e4aa2020-09-04 15:22:31 +02003093finish:
3094 if (ret) {
3095 lyd_free_meta_single(mt);
3096 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003097 *dup = mt;
3098 }
3099 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003100}
3101
Michal Vasko4490d312020-06-16 13:08:55 +02003102/**
3103 * @brief Merge a source sibling into target siblings.
3104 *
3105 * @param[in,out] first_trg First target sibling, is updated if top-level.
3106 * @param[in] parent_trg Target parent.
3107 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3108 * @param[in] options Merge options.
3109 * @return LY_ERR value.
3110 */
3111static LY_ERR
3112lyd_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 +02003113 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003114{
3115 LY_ERR ret;
3116 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003117 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003118 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003119
3120 sibling_src = *sibling_src_p;
3121 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3122 /* try to find the exact instance */
3123 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3124 } else {
3125 /* try to simply find the node, there cannot be more instances */
3126 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3127 }
3128
3129 if (!ret) {
3130 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003131 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003132 /* since they are different, they cannot both be default */
3133 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3134
Michal Vasko3a41dff2020-07-15 14:30:28 +02003135 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3136 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003137 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003138 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3139 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003140 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003141
3142 /* copy flags and add LYD_NEW */
3143 match_trg->flags = sibling_src->flags | LYD_NEW;
3144 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003145 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003146 /* update value */
3147 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003148 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003149
3150 /* copy flags and add LYD_NEW */
3151 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003152 } else {
3153 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003154 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003155 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 +02003156 }
3157 }
3158 } else {
3159 /* node not found, merge it */
3160 if (options & LYD_MERGE_DESTRUCT) {
3161 dup_src = (struct lyd_node *)sibling_src;
3162 lyd_unlink_tree(dup_src);
3163 /* spend it */
3164 *sibling_src_p = NULL;
3165 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003166 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 +02003167 }
3168
3169 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003170 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003171 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003172 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003173 }
3174
3175 lyd_insert_node(parent_trg, first_trg, dup_src);
3176 }
3177
3178 return LY_SUCCESS;
3179}
3180
Michal Vasko3a41dff2020-07-15 14:30:28 +02003181static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003182lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003183{
3184 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003185 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003186
3187 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3188
3189 if (!source) {
3190 /* nothing to merge */
3191 return LY_SUCCESS;
3192 }
3193
Michal Vasko831422b2020-11-24 11:20:51 +01003194 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003195 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003196 return LY_EINVAL;
3197 }
3198
3199 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003200 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003201 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3202 if (first && !sibling_src) {
3203 /* source was spent (unlinked), move to the next node */
3204 source = tmp;
3205 }
3206
Michal Vasko3a41dff2020-07-15 14:30:28 +02003207 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003208 break;
3209 }
3210 }
3211
3212 if (options & LYD_MERGE_DESTRUCT) {
3213 /* free any leftover source data that were not merged */
3214 lyd_free_siblings((struct lyd_node *)source);
3215 }
3216
3217 return LY_SUCCESS;
3218}
3219
Michal Vasko3a41dff2020-07-15 14:30:28 +02003220API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003221lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003222{
3223 return lyd_merge(target, source, options, 1);
3224}
3225
3226API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003227lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003228{
3229 return lyd_merge(target, source, options, 0);
3230}
3231
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003232static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003233lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003234{
Michal Vasko14654712020-02-06 08:35:21 +01003235 /* ending \0 */
3236 ++reqlen;
3237
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003238 if (reqlen > *buflen) {
3239 if (is_static) {
3240 return LY_EINCOMPLETE;
3241 }
3242
3243 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3244 if (!*buffer) {
3245 return LY_EMEM;
3246 }
3247
3248 *buflen = reqlen;
3249 }
3250
3251 return LY_SUCCESS;
3252}
3253
Michal Vaskod59035b2020-07-08 12:00:06 +02003254LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003255lyd_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 +02003256{
3257 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003258 size_t len;
3259 const char *val;
3260 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003261
Radek Krejcia1c1e542020-09-29 16:06:52 +02003262 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003263 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003264 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003265 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003266
3267 quot = '\'';
3268 if (strchr(val, '\'')) {
3269 quot = '"';
3270 }
3271 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003272 }
3273
3274 return LY_SUCCESS;
3275}
3276
3277/**
3278 * @brief Append leaf-list value predicate to path.
3279 *
3280 * @param[in] node Node to print.
3281 * @param[in,out] buffer Buffer to print to.
3282 * @param[in,out] buflen Current buffer length.
3283 * @param[in,out] bufused Current number of characters used in @p buffer.
3284 * @param[in] is_static Whether buffer is static or can be reallocated.
3285 * @return LY_ERR
3286 */
3287static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003288lyd_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 +02003289{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003290 size_t len;
3291 const char *val;
3292 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003293
Michal Vaskob7be7a82020-08-20 09:09:04 +02003294 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003295 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003296 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003297
3298 quot = '\'';
3299 if (strchr(val, '\'')) {
3300 quot = '"';
3301 }
3302 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3303
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003304 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003305}
3306
3307/**
3308 * @brief Append node position (relative to its other instances) predicate to path.
3309 *
3310 * @param[in] node Node to print.
3311 * @param[in,out] buffer Buffer to print to.
3312 * @param[in,out] buflen Current buffer length.
3313 * @param[in,out] bufused Current number of characters used in @p buffer.
3314 * @param[in] is_static Whether buffer is static or can be reallocated.
3315 * @return LY_ERR
3316 */
3317static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003318lyd_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 +02003319{
3320 const struct lyd_node *first, *iter;
3321 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003322 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003323 char *val = NULL;
3324 LY_ERR rc;
3325
3326 if (node->parent) {
3327 first = node->parent->child;
3328 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003329 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003330 }
3331 pos = 1;
3332 for (iter = first; iter != node; iter = iter->next) {
3333 if (iter->schema == node->schema) {
3334 ++pos;
3335 }
3336 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003337 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003338 return LY_EMEM;
3339 }
3340
3341 len = 1 + strlen(val) + 1;
3342 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3343 if (rc != LY_SUCCESS) {
3344 goto cleanup;
3345 }
3346
3347 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3348
3349cleanup:
3350 free(val);
3351 return rc;
3352}
3353
3354API char *
3355lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3356{
Radek Krejci857189e2020-09-01 13:26:36 +02003357 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003358 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003359 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003360 const struct lyd_node *iter;
3361 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003362 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003363
3364 LY_CHECK_ARG_RET(NULL, node, NULL);
3365 if (buffer) {
3366 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3367 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003368 } else {
3369 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003370 }
3371
3372 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003373 case LYD_PATH_STD:
3374 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003375 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003376 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003377 ++depth;
3378 }
3379
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003380 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003381 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003382 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003383 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003384iter_print:
3385 /* print prefix and name */
3386 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003387 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003388 mod = iter->schema->module;
3389 }
3390
3391 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003392 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3393 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003394 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3395 if (rc != LY_SUCCESS) {
3396 break;
3397 }
3398
3399 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003400 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003401 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003402
Michal Vasko790b2bc2020-08-03 13:35:06 +02003403 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003404 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003405 switch (iter->schema->nodetype) {
3406 case LYS_LIST:
3407 if (iter->schema->flags & LYS_KEYLESS) {
3408 /* print its position */
3409 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3410 } else {
3411 /* print all list keys in predicates */
3412 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3413 }
3414 break;
3415 case LYS_LEAFLIST:
3416 if (iter->schema->flags & LYS_CONFIG_W) {
3417 /* print leaf-list value */
3418 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3419 } else {
3420 /* print its position */
3421 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3422 }
3423 break;
3424 default:
3425 /* nothing to print more */
3426 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003427 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003428 }
3429 if (rc != LY_SUCCESS) {
3430 break;
3431 }
3432
Michal Vasko14654712020-02-06 08:35:21 +01003433 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003434 }
3435 break;
3436 }
3437
3438 return buffer;
3439}
Michal Vaskoe444f752020-02-10 12:20:06 +01003440
Michal Vasko25a32822020-07-09 15:48:22 +02003441API struct lyd_meta *
3442lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3443{
3444 struct lyd_meta *ret = NULL;
3445 const struct ly_ctx *ctx;
3446 const char *prefix, *tmp;
3447 char *str;
3448 size_t pref_len, name_len;
3449
3450 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3451
3452 if (!first) {
3453 return NULL;
3454 }
3455
3456 ctx = first->annotation->module->ctx;
3457
3458 /* parse the name */
3459 tmp = name;
3460 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3461 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3462 return NULL;
3463 }
3464
3465 /* find the module */
3466 if (prefix) {
3467 str = strndup(prefix, pref_len);
3468 module = ly_ctx_get_module_latest(ctx, str);
3469 free(str);
3470 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3471 }
3472
3473 /* find the metadata */
3474 LY_LIST_FOR(first, first) {
3475 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3476 ret = (struct lyd_meta *)first;
3477 break;
3478 }
3479 }
3480
3481 return ret;
3482}
3483
Michal Vasko9b368d32020-02-14 13:53:31 +01003484API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003485lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3486{
3487 struct lyd_node **match_p;
3488 struct lyd_node_inner *parent;
3489
Michal Vaskof03ed032020-03-04 13:31:44 +01003490 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003491
Michal Vasko6da1e952020-12-09 18:14:38 +01003492 if (!siblings || (siblings->schema && target->schema &&
3493 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003494 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003495 if (match) {
3496 *match = NULL;
3497 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003498 return LY_ENOTFOUND;
3499 }
3500
3501 /* find first sibling */
3502 if (siblings->parent) {
3503 siblings = siblings->parent->child;
3504 } else {
3505 while (siblings->prev->next) {
3506 siblings = siblings->prev;
3507 }
3508 }
3509
Michal Vasko9e685082021-01-29 14:49:09 +01003510 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003511 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003512 assert(target->hash);
3513
3514 /* find by hash */
3515 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003516 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003517 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003518 siblings = *match_p;
3519 } else {
3520 siblings = NULL;
3521 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003522 } else {
3523 /* not found */
3524 siblings = NULL;
3525 }
3526 } else {
3527 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003528 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003529 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003530 break;
3531 }
3532 }
3533 }
3534
3535 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003536 if (match) {
3537 *match = NULL;
3538 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003539 return LY_ENOTFOUND;
3540 }
3541
Michal Vasko9b368d32020-02-14 13:53:31 +01003542 if (match) {
3543 *match = (struct lyd_node *)siblings;
3544 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003545 return LY_SUCCESS;
3546}
3547
Radek Krejci857189e2020-09-01 13:26:36 +02003548/**
3549 * @brief Comparison callback to match schema node with a schema of a data node.
3550 *
3551 * @param[in] val1_p Pointer to the schema node
3552 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003553 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003554 */
3555static ly_bool
3556lyd_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 +01003557{
3558 struct lysc_node *val1;
3559 struct lyd_node *val2;
3560
3561 val1 = *((struct lysc_node **)val1_p);
3562 val2 = *((struct lyd_node **)val2_p);
3563
Michal Vasko90932a92020-02-12 14:33:03 +01003564 if (val1 == val2->schema) {
3565 /* schema match is enough */
3566 return 1;
3567 } else {
3568 return 0;
3569 }
3570}
3571
Michal Vasko92239c72020-11-18 18:17:25 +01003572/**
3573 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3574 * Uses hashes - should be used whenever possible for best performance.
3575 *
3576 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3577 * @param[in] schema Target data node schema to find.
3578 * @param[out] match Can be NULL, otherwise the found data node.
3579 * @return LY_SUCCESS on success, @p match set.
3580 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3581 * @return LY_ERR value if another error occurred.
3582 */
Michal Vasko90932a92020-02-12 14:33:03 +01003583static LY_ERR
3584lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3585{
3586 struct lyd_node **match_p;
3587 struct lyd_node_inner *parent;
3588 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003589 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003590
Michal Vaskob104f112020-07-17 09:54:54 +02003591 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003592
Michal Vasko9e685082021-01-29 14:49:09 +01003593 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003594 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003595 /* calculate our hash */
3596 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3597 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3598 hash = dict_hash_multi(hash, NULL, 0);
3599
3600 /* use special hash table function */
3601 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3602
3603 /* find by hash */
3604 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3605 siblings = *match_p;
3606 } else {
3607 /* not found */
3608 siblings = NULL;
3609 }
3610
3611 /* set the original hash table compare function back */
3612 lyht_set_cb(parent->children_ht, ht_cb);
3613 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003614 /* find first sibling */
3615 if (siblings->parent) {
3616 siblings = siblings->parent->child;
3617 } else {
3618 while (siblings->prev->next) {
3619 siblings = siblings->prev;
3620 }
3621 }
3622
3623 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003624 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003625 if (siblings->schema == schema) {
3626 /* schema match is enough */
3627 break;
3628 }
3629 }
3630 }
3631
3632 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003633 if (match) {
3634 *match = NULL;
3635 }
Michal Vasko90932a92020-02-12 14:33:03 +01003636 return LY_ENOTFOUND;
3637 }
3638
Michal Vasko9b368d32020-02-14 13:53:31 +01003639 if (match) {
3640 *match = (struct lyd_node *)siblings;
3641 }
Michal Vasko90932a92020-02-12 14:33:03 +01003642 return LY_SUCCESS;
3643}
3644
Michal Vaskoe444f752020-02-10 12:20:06 +01003645API LY_ERR
3646lyd_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 +02003647 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003648{
3649 LY_ERR rc;
3650 struct lyd_node *target = NULL;
3651
Michal Vasko4c583e82020-07-17 12:16:14 +02003652 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003653
Michal Vasko08fd6132020-11-18 18:17:45 +01003654 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003655 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003656 if (match) {
3657 *match = NULL;
3658 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003659 return LY_ENOTFOUND;
3660 }
3661
Michal Vaskof03ed032020-03-04 13:31:44 +01003662 if (key_or_value && !val_len) {
3663 val_len = strlen(key_or_value);
3664 }
3665
Michal Vaskob104f112020-07-17 09:54:54 +02003666 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3667 /* create a data node and find the instance */
3668 if (schema->nodetype == LYS_LEAFLIST) {
3669 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003670 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3671 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003672 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003673 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003674 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003675 }
3676
3677 /* find it */
3678 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003679 } else {
3680 /* find the first schema node instance */
3681 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003682 }
3683
Michal Vaskoe444f752020-02-10 12:20:06 +01003684 lyd_free_tree(target);
3685 return rc;
3686}
Michal Vaskoccc02342020-05-21 10:09:21 +02003687
3688API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003689lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3690{
3691 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3692
3693 for ( ; first; first = first->next) {
3694 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
3695 break;
3696 }
3697 }
3698
3699 if (match) {
3700 *match = (struct lyd_node *)first;
3701 }
3702 return first ? LY_SUCCESS : LY_ENOTFOUND;
3703}
3704
3705API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02003706lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3707{
3708 LY_ERR ret = LY_SUCCESS;
3709 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003710 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003711 uint32_t i;
3712
3713 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3714
3715 memset(&xp_set, 0, sizeof xp_set);
3716
3717 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003718 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3719 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003720
3721 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01003722 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 +02003723 LY_CHECK_GOTO(ret, cleanup);
3724
3725 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003726 ret = ly_set_new(set);
3727 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003728
3729 /* transform into ly_set */
3730 if (xp_set.type == LYXP_SET_NODE_SET) {
3731 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3732 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003733 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003734 (*set)->size = xp_set.used;
3735
3736 for (i = 0; i < xp_set.used; ++i) {
3737 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003738 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003739 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003740 }
3741 }
3742 }
3743
3744cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003745 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003746 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01003747 if (ret) {
3748 ly_set_free(*set, NULL);
3749 *set = NULL;
3750 }
Michal Vaskoccc02342020-05-21 10:09:21 +02003751 return ret;
3752}
Radek Krejcica989142020-11-05 11:32:22 +01003753
Michal Vasko3e1f6552021-01-14 09:27:55 +01003754API LY_ERR
3755lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3756{
3757 LY_ERR ret = LY_SUCCESS;
3758 struct lyxp_expr *expr = NULL;
3759 struct ly_path *lypath = NULL;
3760
3761 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3762
3763 /* parse the path */
3764 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
3765 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
3766 LY_CHECK_GOTO(ret, cleanup);
3767
3768 /* compile the path */
3769 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, expr, LY_PATH_LREF_FALSE,
3770 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
3771 (void *)LYD_CTX(ctx_node), NULL, &lypath);
3772 LY_CHECK_GOTO(ret, cleanup);
3773
3774 /* evaluate the path */
3775 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
3776
3777cleanup:
3778 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3779 ly_path_free(LYD_CTX(ctx_node), lypath);
3780 return ret;
3781}
3782
Radek Krejcica989142020-11-05 11:32:22 +01003783API uint32_t
3784lyd_list_pos(const struct lyd_node *instance)
3785{
3786 const struct lyd_node *iter = NULL;
3787 uint32_t pos = 0;
3788
3789 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3790 return 0;
3791 }
3792
3793 /* data instances are ordered, so we can stop when we found instance of other schema node */
3794 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01003795 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01003796 /* overrun to the end of the siblings list */
3797 break;
3798 }
3799 ++pos;
3800 }
3801
3802 return pos;
3803}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003804
3805API struct lyd_node *
3806lyd_first_sibling(const struct lyd_node *node)
3807{
3808 struct lyd_node *start;
3809
3810 if (!node) {
3811 return NULL;
3812 }
3813
3814 /* get the first sibling */
3815 if (node->parent) {
3816 start = node->parent->child;
3817 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01003818 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003819 }
3820
3821 return start;
3822}