blob: a8e36c109e463c62cbd7e46044418c82252c2662 [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;
Michal Vasko0ab974d2021-02-24 13:18:26 +01001176 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 +02001177
1178 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001179 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001180 }
1181 if (!value) {
1182 value = "";
1183 }
1184
Michal Vasko0ab974d2021-02-24 13:18:26 +01001185 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1186 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001187 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001188 lyd_insert_node(parent, NULL, ret);
1189 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001190
1191 if (node) {
1192 *node = ret;
1193 }
1194 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001195}
1196
Michal Vasko3a41dff2020-07-15 14:30:28 +02001197API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001198lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001199 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001200{
1201 struct lyd_node *ret = NULL;
1202
1203 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1204
1205 if (!ctx) {
1206 ctx = LYD_CTX(parent);
1207 }
1208 if (!value) {
1209 value = "";
1210 }
1211
Michal Vasko0ab974d2021-02-24 13:18:26 +01001212 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1213 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001214 if (parent) {
1215 lyd_insert_node(parent, NULL, ret);
1216 }
1217
1218 if (node) {
1219 *node = ret;
1220 }
1221 return LY_SUCCESS;
1222}
1223
1224API LY_ERR
1225lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001226 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001227{
Radek Krejci1798aae2020-07-14 13:26:06 +02001228 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001229 const struct ly_ctx *ctx;
1230 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001231 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001232
Michal Vasko3a41dff2020-07-15 14:30:28 +02001233 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001234
Michal Vaskob7be7a82020-08-20 09:09:04 +02001235 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001236
1237 /* parse the name */
1238 tmp = name;
1239 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001240 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001241 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001242 }
1243
Michal Vasko51d21b82020-11-13 18:03:54 +01001244 /* get the module */
1245 if (module_name) {
1246 mod_len = strlen(module_name);
1247 } else {
1248 module_name = prefix;
1249 mod_len = pref_len;
1250 }
1251
Michal Vasko00cbf532020-06-15 13:58:47 +02001252 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001253 if (!value) {
1254 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001255 }
1256
Michal Vasko8d65f852021-02-17 11:28:13 +01001257 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1258 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1259
1260 if (attr) {
1261 *attr = ret;
1262 }
1263 return LY_SUCCESS;
1264}
1265
1266API LY_ERR
1267lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1268 struct lyd_attr **attr)
1269{
1270 struct lyd_attr *ret = NULL;
1271 const struct ly_ctx *ctx;
1272 const char *prefix, *tmp;
1273 size_t pref_len, name_len;
1274
1275 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1276
1277 ctx = LYD_CTX(parent);
1278
1279 /* parse the name */
1280 tmp = name;
1281 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1282 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1283 return LY_EVALID;
1284 }
1285
1286 /* set value if none */
1287 if (!value) {
1288 value = "";
1289 }
1290
1291 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1292 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001293
1294 if (attr) {
1295 *attr = ret;
1296 }
1297 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001298}
1299
1300API LY_ERR
1301lyd_change_term(struct lyd_node *term, const char *val_str)
1302{
1303 LY_ERR ret = LY_SUCCESS;
1304 struct lysc_type *type;
1305 struct lyd_node_term *t;
1306 struct lyd_node *parent;
1307 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001308 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001309
1310 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1311
1312 if (!val_str) {
1313 val_str = "";
1314 }
1315 t = (struct lyd_node_term *)term;
1316 type = ((struct lysc_node_leaf *)term->schema)->type;
1317
1318 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001319 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001320 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 +01001321 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001322 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001323 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001324
1325 /* compare original and new value */
1326 if (type->plugin->compare(&t->value, &val)) {
1327 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001328 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001329 t->value = val;
1330 memset(&val, 0, sizeof val);
1331 val_change = 1;
1332 } else {
1333 val_change = 0;
1334 }
1335
1336 /* always clear the default flag */
1337 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001338 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001339 parent->flags &= ~LYD_DEFAULT;
1340 }
1341 dflt_change = 1;
1342 } else {
1343 dflt_change = 0;
1344 }
1345
1346 if (val_change || dflt_change) {
1347 /* make the node non-validated */
1348 term->flags &= LYD_NEW;
1349 }
1350
1351 if (val_change) {
1352 if (term->schema->nodetype == LYS_LEAFLIST) {
1353 /* leaf-list needs to be hashed again and re-inserted into parent */
1354 lyd_unlink_hash(term);
1355 lyd_hash(term);
1356 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1357 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1358 /* list needs to be updated if its key was changed */
1359 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001360 lyd_unlink_hash(lyd_parent(term));
1361 lyd_hash(lyd_parent(term));
1362 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001363 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1364 }
1365
1366 /* retrun value */
1367 if (!val_change) {
1368 if (dflt_change) {
1369 /* only default flag change */
1370 ret = LY_EEXIST;
1371 } else {
1372 /* no change */
1373 ret = LY_ENOT;
1374 }
1375 } /* else value changed, LY_SUCCESS */
1376
1377cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001378 if (val.realtype) {
1379 type->plugin->free(LYD_CTX(term), &val);
1380 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001381 return ret;
1382}
1383
Michal Vasko41586352020-07-13 13:54:25 +02001384API LY_ERR
1385lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1386{
1387 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001388 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001389 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001390 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001391
1392 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1393
1394 if (!val_str) {
1395 val_str = "";
1396 }
1397
1398 /* parse the new value into a new meta structure */
1399 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 +01001400 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001401
1402 /* compare original and new value */
1403 if (lyd_compare_meta(meta, m2)) {
1404 /* values differ, switch them */
1405 val = meta->value;
1406 meta->value = m2->value;
1407 m2->value = val;
1408 val_change = 1;
1409 } else {
1410 val_change = 0;
1411 }
1412
1413 /* retrun value */
1414 if (!val_change) {
1415 /* no change */
1416 ret = LY_ENOT;
1417 } /* else value changed, LY_SUCCESS */
1418
1419cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001420 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001421 return ret;
1422}
1423
Michal Vasko3a41dff2020-07-15 14:30:28 +02001424API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001425lyd_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 +02001426 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001427{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001428 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001429}
1430
Michal Vasko6741dc62021-02-04 09:27:45 +01001431static LY_ERR
1432lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1433{
1434 LY_ERR ret = LY_SUCCESS, r;
1435 const struct lysc_node *schema;
1436 struct ly_path_predicate *pred;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001437 LY_ARRAY_COUNT_TYPE u, new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001438
1439 assert(path);
1440
1441 /* go through all the compiled nodes */
1442 LY_ARRAY_FOR(path, u) {
1443 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001444 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001445 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1446 if (schema->flags & LYS_KEYLESS) {
1447 /* creating a new keyless list instance */
1448 break;
1449 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1450 LOG_LOCSET(schema, NULL, NULL, NULL);
1451 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1452 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1453 LOG_LOCBACK(1, 0, 0, 0);
1454 return LY_EINVAL;
1455 } /* else creating an opaque list without keys */
1456 }
1457 }
1458
Michal Vasko6f9b4262021-02-04 12:09:56 +01001459 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001460 /* parse leafref value into a predicate, if not defined in the path */
1461 if (!value) {
1462 value = "";
1463 }
1464
1465 r = LY_SUCCESS;
1466 if (options & LYD_NEW_PATH_OPAQ) {
1467 r = lys_value_validate(NULL, schema, value, strlen(value));
1468 }
1469 if (!r) {
1470 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001471 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1472 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001473
1474 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1475 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1476 LY_CHECK_GOTO(ret, cleanup);
1477 ++((struct lysc_type *)pred->value.realtype)->refcount;
1478
1479 if (schema->flags & LYS_CONFIG_R) {
1480 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001481 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001482 }
1483 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1484 }
1485
1486 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001487 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001488 LY_ARRAY_DECREMENT(path);
1489 }
1490
1491cleanup:
1492 return ret;
1493}
1494
Michal Vasko00cbf532020-06-15 13:58:47 +02001495API LY_ERR
1496lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001497 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 +02001498{
1499 LY_ERR ret = LY_SUCCESS, r;
1500 struct lyxp_expr *exp = NULL;
1501 struct ly_path *p = NULL;
1502 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1503 const struct lysc_node *schema;
Michal Vasko6741dc62021-02-04 09:27:45 +01001504 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count;
Michal Vasko00cbf532020-06-15 13:58:47 +02001505
1506 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1507
1508 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001509 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001510 }
1511
1512 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001513 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 +02001514 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001515
1516 /* compile path */
1517 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 +02001518 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 +01001519 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001520
Michal Vasko6741dc62021-02-04 09:27:45 +01001521 /* check the compiled path before searching existing nodes, it may be shortened */
1522 orig_count = LY_ARRAY_COUNT(p);
1523 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001524
1525 /* try to find any existing nodes in the path */
1526 if (parent) {
1527 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1528 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001529 if (orig_count == LY_ARRAY_COUNT(p)) {
1530 /* the node exists, are we supposed to update it or is it just a default? */
1531 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1532 LOG_LOCSET(NULL, node, NULL, NULL);
1533 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1534 LOG_LOCBACK(0, 1, 0, 0);
1535 ret = LY_EEXIST;
1536 goto cleanup;
1537 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001538
Michal Vasko6741dc62021-02-04 09:27:45 +01001539 /* update the existing node */
1540 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1541 goto cleanup;
1542 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001543 } else if (ret == LY_EINCOMPLETE) {
1544 /* some nodes were found, adjust the iterator to the next segment */
1545 ++path_idx;
1546 } else if (ret == LY_ENOTFOUND) {
1547 /* 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 +01001548 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001549 node = parent;
1550 }
1551 } else {
1552 /* error */
1553 goto cleanup;
1554 }
1555 }
1556
Michal Vasko6741dc62021-02-04 09:27:45 +01001557 /* restore the full path for creating new nodes */
1558 while (orig_count > LY_ARRAY_COUNT(p)) {
1559 LY_ARRAY_INCREMENT(p);
1560 }
1561
Michal Vasko00cbf532020-06-15 13:58:47 +02001562 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001563 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001564 cur_parent = node;
1565 schema = p[path_idx].node;
1566
1567 switch (schema->nodetype) {
1568 case LYS_LIST:
1569 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001570 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001571 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001572 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1573 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1574 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001575 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001576 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1577 LOG_LOCSET(schema, NULL, NULL, NULL);
1578 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1579 lys_nodetype2str(schema->nodetype), schema->name, path);
1580 LOG_LOCBACK(1, 0, 0, 0);
1581 ret = LY_EINVAL;
1582 goto cleanup;
1583 }
1584
Michal Vasko00cbf532020-06-15 13:58:47 +02001585 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1586 }
1587 break;
1588 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001589 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001590 case LYS_CONTAINER:
1591 case LYS_NOTIF:
1592 case LYS_RPC:
1593 case LYS_ACTION:
1594 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1595 break;
1596 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001597 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001598 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001599 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1600 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1601 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001602 } else {
1603 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1604 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1605 }
1606 break;
1607 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001608 if (lysc_is_key(schema)) {
1609 /* it must have been already created or some error will occur later */
1610 assert(cur_parent);
1611 node = lyd_child(cur_parent);
1612 assert(node && (node->schema == schema));
1613 goto next_iter;
1614 }
1615
1616 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001617 if (!value) {
1618 value = "";
1619 }
1620
1621 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001622 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001623 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001624 }
1625 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001626 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1627 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001628 } else {
1629 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001630 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1631 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001632 }
1633 break;
1634 case LYS_ANYDATA:
1635 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001636 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001637 break;
1638 default:
1639 LOGINT(ctx);
1640 ret = LY_EINT;
1641 goto cleanup;
1642 }
1643
1644 if (cur_parent) {
1645 /* connect to the parent */
1646 lyd_insert_node(cur_parent, NULL, node);
1647 } else if (parent) {
1648 /* connect to top-level siblings */
1649 lyd_insert_node(NULL, &parent, node);
1650 }
1651
Michal Vasko35880332020-12-08 13:08:12 +01001652next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001653 /* update remembered nodes */
1654 if (!nparent) {
1655 nparent = node;
1656 }
1657 nnode = node;
1658 }
1659
1660cleanup:
1661 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001662 if (p) {
1663 while (orig_count > LY_ARRAY_COUNT(p)) {
1664 LY_ARRAY_INCREMENT(p);
1665 }
1666 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001667 ly_path_free(ctx, p);
1668 if (!ret) {
1669 /* set out params only on success */
1670 if (new_parent) {
1671 *new_parent = nparent;
1672 }
1673 if (new_node) {
1674 *new_node = nnode;
1675 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001676 } else {
1677 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001678 }
1679 return ret;
1680}
1681
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001682LY_ERR
1683lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001684 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 +02001685 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001686{
1687 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001688 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001689 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001690 struct lyd_value **dflts;
1691 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001692 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001693
1694 assert(first && (parent || sparent || mod));
1695
1696 if (!sparent && parent) {
1697 sparent = parent->schema;
1698 }
1699
Michal Vasko630d9892020-12-08 17:11:08 +01001700 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1701 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1702 getnext_opts |= LYS_GETNEXT_OUTPUT;
1703 }
1704
1705 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001706 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1707 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001708 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1709 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001710 }
1711
1712 switch (iter->nodetype) {
1713 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001714 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1715 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001716 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001717 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vasko69730152020-10-09 16:30:07 +02001718 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001719 } else if (node) {
1720 /* create any default data in the existing case */
1721 assert(node->schema->parent->nodetype == LYS_CASE);
1722 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_types, node_when,
1723 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001724 }
1725 break;
1726 case LYS_CONTAINER:
1727 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1728 /* create default NP container */
1729 LY_CHECK_RET(lyd_create_inner(iter, &node));
Radek Krejci9a3823e2021-01-27 20:26:46 +01001730 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001731 lyd_insert_node(parent, first, node);
1732
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001733 if (lysc_node_when(iter) && node_when) {
1734 /* remember to resolve when */
1735 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1736 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001737 if (diff) {
1738 /* add into diff */
1739 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1740 }
1741
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001742 /* create any default children */
Michal Vaskoe0665742021-02-11 11:08:44 +01001743 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 +02001744 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001745 }
1746 break;
1747 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001748 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1749 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001750 /* create default leaf */
1751 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1752 if (ret == LY_EINCOMPLETE) {
1753 if (node_types) {
1754 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001755 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001756 }
1757 } else if (ret) {
1758 return ret;
1759 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001760 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001761 lyd_insert_node(parent, first, node);
1762
Radek Krejci9a3823e2021-01-27 20:26:46 +01001763 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001764 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001765 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001766 }
1767 if (diff) {
1768 /* add into diff */
1769 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1770 }
1771 }
1772 break;
1773 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001774 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1775 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001776 /* create all default leaf-lists */
1777 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1778 LY_ARRAY_FOR(dflts, u) {
1779 ret = lyd_create_term2(iter, dflts[u], &node);
1780 if (ret == LY_EINCOMPLETE) {
1781 if (node_types) {
1782 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001783 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001784 }
1785 } else if (ret) {
1786 return ret;
1787 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001788 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001789 lyd_insert_node(parent, first, node);
1790
Radek Krejci9a3823e2021-01-27 20:26:46 +01001791 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001792 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001793 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001794 }
1795 if (diff) {
1796 /* add into diff */
1797 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1798 }
1799 }
1800 }
1801 break;
1802 default:
1803 /* without defaults */
1804 break;
1805 }
1806 }
1807
1808 return LY_SUCCESS;
1809}
1810
1811API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001812lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001813{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001814 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001815 struct lyd_node *node;
1816 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001817
1818 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1819 if (diff) {
1820 *diff = NULL;
1821 }
1822
Michal Vasko56daf732020-08-10 10:57:18 +02001823 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001824 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001825 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1826 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001827 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, NULL,
Michal Vasko3488ada2020-12-03 14:18:19 +01001828 &node_when, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001829 }
1830
Michal Vasko56daf732020-08-10 10:57:18 +02001831 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001832 }
1833
Michal Vasko3488ada2020-12-03 14:18:19 +01001834 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001835 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01001836
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001837cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001838 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001839 if (ret && diff) {
1840 lyd_free_all(*diff);
1841 *diff = NULL;
1842 }
1843 return ret;
1844}
1845
1846API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001847lyd_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 +02001848{
1849 const struct lys_module *mod;
1850 struct lyd_node *d = NULL;
1851 uint32_t i = 0;
1852 LY_ERR ret = LY_SUCCESS;
1853
1854 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1855 if (diff) {
1856 *diff = NULL;
1857 }
1858 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001859 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001860 }
1861
1862 /* add nodes for each module one-by-one */
1863 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1864 if (!mod->implemented) {
1865 continue;
1866 }
1867
1868 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1869 if (d) {
1870 /* merge into one diff */
1871 lyd_insert_sibling(*diff, d, diff);
1872
1873 d = NULL;
1874 }
1875 }
1876
1877cleanup:
1878 if (ret && diff) {
1879 lyd_free_all(*diff);
1880 *diff = NULL;
1881 }
1882 return ret;
1883}
1884
1885API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001886lyd_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 +02001887{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001888 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001889 struct lyd_node *root, *d = NULL;
1890 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001891
1892 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1893 if (diff) {
1894 *diff = NULL;
1895 }
1896
1897 /* add all top-level defaults for this module */
Michal Vasko3488ada2020-12-03 14:18:19 +01001898 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, &node_when, implicit_options, diff), cleanup);
1899
1900 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001901 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001902
1903 /* process nested nodes */
1904 LY_LIST_FOR(*tree, root) {
1905 /* skip added default nodes */
1906 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1907 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1908
1909 if (d) {
1910 /* merge into one diff */
1911 lyd_insert_sibling(*diff, d, diff);
1912
1913 d = NULL;
1914 }
1915 }
1916 }
1917
1918cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001919 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001920 if (ret && diff) {
1921 lyd_free_all(*diff);
1922 *diff = NULL;
1923 }
1924 return ret;
1925}
1926
Michal Vasko90932a92020-02-12 14:33:03 +01001927struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001928lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001929{
Michal Vaskob104f112020-07-17 09:54:54 +02001930 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001931 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001932 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01001933 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01001934
Michal Vaskob104f112020-07-17 09:54:54 +02001935 assert(new_node);
1936
1937 if (!first_sibling || !new_node->schema) {
1938 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001939 return NULL;
1940 }
1941
Michal Vasko93b16062020-12-09 18:14:18 +01001942 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001943 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01001944 getnext_opts = LYS_GETNEXT_OUTPUT;
1945 }
1946
Michal Vaskob104f112020-07-17 09:54:54 +02001947 if (first_sibling->parent && first_sibling->parent->children_ht) {
1948 /* find the anchor using hashes */
1949 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01001950 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001951 while (schema) {
1952 /* keep trying to find the first existing instance of the closest following schema sibling,
1953 * otherwise return NULL - inserting at the end */
1954 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1955 break;
1956 }
1957
Michal Vasko93b16062020-12-09 18:14:18 +01001958 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001959 }
1960 } else {
1961 /* find the anchor without hashes */
1962 match = (struct lyd_node *)first_sibling;
1963 if (!lysc_data_parent(new_node->schema)) {
1964 /* we are in top-level, skip all the data from preceding modules */
1965 LY_LIST_FOR(match, match) {
1966 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1967 break;
1968 }
1969 }
1970 }
1971
1972 /* get the first schema sibling */
1973 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01001974 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001975
1976 found = 0;
1977 LY_LIST_FOR(match, match) {
1978 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1979 /* we have found an opaque node, which must be at the end, so use it OR
1980 * modules do not match, so we must have traversed all the data from new_node module (if any),
1981 * we have found the first node of the next module, that is what we want */
1982 break;
1983 }
1984
1985 /* skip schema nodes until we find the instantiated one */
1986 while (!found) {
1987 if (new_node->schema == schema) {
1988 /* we have found the schema of the new node, continue search to find the first
1989 * data node with a different schema (after our schema) */
1990 found = 1;
1991 break;
1992 }
1993 if (match->schema == schema) {
1994 /* current node (match) is a data node still before the new node, continue search in data */
1995 break;
1996 }
Michal Vasko93b16062020-12-09 18:14:18 +01001997 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001998 assert(schema);
1999 }
2000
2001 if (found && (match->schema != new_node->schema)) {
2002 /* find the next node after we have found our node schema data instance */
2003 break;
2004 }
2005 }
Michal Vasko90932a92020-02-12 14:33:03 +01002006 }
2007
2008 return match;
2009}
2010
2011/**
2012 * @brief Insert node after a sibling.
2013 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002014 * Handles inserting into NP containers and key-less lists.
2015 *
Michal Vasko90932a92020-02-12 14:33:03 +01002016 * @param[in] sibling Sibling to insert after.
2017 * @param[in] node Node to insert.
2018 */
2019static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002020lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002021{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002022 struct lyd_node_inner *par;
2023
Michal Vasko90932a92020-02-12 14:33:03 +01002024 assert(!node->next && (node->prev == node));
2025
2026 node->next = sibling->next;
2027 node->prev = sibling;
2028 sibling->next = node;
2029 if (node->next) {
2030 /* sibling had a succeeding node */
2031 node->next->prev = node;
2032 } else {
2033 /* sibling was last, find first sibling and change its prev */
2034 if (sibling->parent) {
2035 sibling = sibling->parent->child;
2036 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002037 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002038 }
2039 sibling->prev = node;
2040 }
2041 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002042
Michal Vasko9f96a052020-03-10 09:41:45 +01002043 for (par = node->parent; par; par = par->parent) {
2044 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2045 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002046 par->flags &= ~LYD_DEFAULT;
2047 }
Michal Vaskob104f112020-07-17 09:54:54 +02002048 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002049 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002050 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002051 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002052 }
Michal Vasko90932a92020-02-12 14:33:03 +01002053}
2054
2055/**
2056 * @brief Insert node before a sibling.
2057 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002058 * Handles inserting into NP containers and key-less lists.
2059 *
Michal Vasko90932a92020-02-12 14:33:03 +01002060 * @param[in] sibling Sibling to insert before.
2061 * @param[in] node Node to insert.
2062 */
2063static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002064lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002065{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002066 struct lyd_node_inner *par;
2067
Michal Vasko90932a92020-02-12 14:33:03 +01002068 assert(!node->next && (node->prev == node));
2069
2070 node->next = sibling;
2071 /* covers situation of sibling being first */
2072 node->prev = sibling->prev;
2073 sibling->prev = node;
2074 if (node->prev->next) {
2075 /* sibling had a preceding node */
2076 node->prev->next = node;
2077 } else if (sibling->parent) {
2078 /* sibling was first and we must also change parent child pointer */
2079 sibling->parent->child = node;
2080 }
2081 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002082
Michal Vasko9f96a052020-03-10 09:41:45 +01002083 for (par = node->parent; par; par = par->parent) {
2084 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2085 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002086 par->flags &= ~LYD_DEFAULT;
2087 }
Michal Vaskob104f112020-07-17 09:54:54 +02002088 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002089 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002090 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002091 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002092 }
Michal Vasko90932a92020-02-12 14:33:03 +01002093}
2094
2095/**
Michal Vaskob104f112020-07-17 09:54:54 +02002096 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002097 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002098 * Handles inserting into NP containers and key-less lists.
2099 *
Michal Vasko90932a92020-02-12 14:33:03 +01002100 * @param[in] parent Parent to insert into.
2101 * @param[in] node Node to insert.
2102 */
2103static void
Michal Vaskob104f112020-07-17 09:54:54 +02002104lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002105{
2106 struct lyd_node_inner *par;
2107
Radek Krejcia1c1e542020-09-29 16:06:52 +02002108 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002109 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002110
2111 par = (struct lyd_node_inner *)parent;
2112
Michal Vaskob104f112020-07-17 09:54:54 +02002113 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002114 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002115
Michal Vaskod989ba02020-08-24 10:59:24 +02002116 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002117 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2118 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002119 par->flags &= ~LYD_DEFAULT;
2120 }
Michal Vasko52927e22020-03-16 17:26:14 +01002121 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002122 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002123 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002124 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002125 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002126}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002127
Michal Vasko751cb4d2020-07-14 12:25:28 +02002128/**
2129 * @brief Learn whether a list instance has all the keys.
2130 *
2131 * @param[in] list List instance to check.
2132 * @return non-zero if all the keys were found,
2133 * @return 0 otherwise.
2134 */
2135static int
2136lyd_insert_has_keys(const struct lyd_node *list)
2137{
2138 const struct lyd_node *key;
2139 const struct lysc_node *skey = NULL;
2140
2141 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002142 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002143 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2144 if (!key || (key->schema != skey)) {
2145 /* key missing */
2146 return 0;
2147 }
2148
2149 key = key->next;
2150 }
2151
2152 /* all keys found */
2153 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002154}
2155
2156void
Michal Vaskob104f112020-07-17 09:54:54 +02002157lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002158{
Michal Vaskob104f112020-07-17 09:54:54 +02002159 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002160
Michal Vaskob104f112020-07-17 09:54:54 +02002161 /* inserting list without its keys is not supported */
2162 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002163 assert(!parent || !parent->schema ||
2164 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002165
Michal Vaskob104f112020-07-17 09:54:54 +02002166 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002167 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002168 }
Michal Vasko90932a92020-02-12 14:33:03 +01002169
Michal Vaskob104f112020-07-17 09:54:54 +02002170 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002171 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002172
Michal Vaskob104f112020-07-17 09:54:54 +02002173 /* find the anchor, our next node, so we can insert before it */
2174 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2175 if (anchor) {
2176 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002177 if (!parent && (*first_sibling_p == anchor)) {
2178 /* move first sibling */
2179 *first_sibling_p = node;
2180 }
Michal Vaskob104f112020-07-17 09:54:54 +02002181 } else if (first_sibling) {
2182 lyd_insert_after_node(first_sibling->prev, node);
2183 } else if (parent) {
2184 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002185 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002186 *first_sibling_p = node;
2187 }
2188
2189 /* insert into parent HT */
2190 lyd_insert_hash(node);
2191
2192 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002193 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002194 lyd_hash(parent);
2195
2196 /* now we can insert even the list into its parent HT */
2197 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002198 }
Michal Vasko90932a92020-02-12 14:33:03 +01002199}
2200
Michal Vasko717a4c32020-12-07 09:40:10 +01002201/**
2202 * @brief Check schema place of a node to be inserted.
2203 *
2204 * @param[in] parent Schema node of the parent data node.
2205 * @param[in] sibling Schema node of a sibling data node.
2206 * @param[in] schema Schema node if the data node to be inserted.
2207 * @return LY_SUCCESS on success.
2208 * @return LY_EINVAL if the place is invalid.
2209 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002210static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002211lyd_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 +01002212{
2213 const struct lysc_node *par2;
2214
Michal Vasko62ed12d2020-05-21 10:08:25 +02002215 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002216 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2217 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002218
Michal Vasko717a4c32020-12-07 09:40:10 +01002219 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002220 /* opaque nodes can be inserted wherever */
2221 return LY_SUCCESS;
2222 }
2223
Michal Vasko717a4c32020-12-07 09:40:10 +01002224 if (!parent) {
2225 parent = lysc_data_parent(sibling);
2226 }
2227
Michal Vaskof03ed032020-03-04 13:31:44 +01002228 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002229 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002230
2231 if (parent) {
2232 /* inner node */
2233 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002234 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002235 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002236 return LY_EINVAL;
2237 }
2238 } else {
2239 /* top-level node */
2240 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002241 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002242 return LY_EINVAL;
2243 }
2244 }
2245
2246 return LY_SUCCESS;
2247}
2248
2249API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002250lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002251{
2252 struct lyd_node *iter;
2253
Michal Vasko0ab974d2021-02-24 13:18:26 +01002254 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002255
Michal Vasko717a4c32020-12-07 09:40:10 +01002256 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002257
Michal Vasko0ab974d2021-02-24 13:18:26 +01002258 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002259 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2260 return LY_EINVAL;
2261 }
2262
2263 if (node->parent || node->prev->next) {
2264 lyd_unlink_tree(node);
2265 }
2266
2267 while (node) {
2268 iter = node->next;
2269 lyd_unlink_tree(node);
2270 lyd_insert_node(parent, NULL, node);
2271 node = iter;
2272 }
2273 return LY_SUCCESS;
2274}
2275
2276API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002277lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002278{
2279 struct lyd_node *iter;
2280
Michal Vaskob104f112020-07-17 09:54:54 +02002281 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002282
Michal Vaskob104f112020-07-17 09:54:54 +02002283 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002284 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002285 }
2286
Michal Vasko553d0b52020-12-04 16:27:52 +01002287 if (sibling == node) {
2288 /* we need to keep the connection to siblings so we can insert into them */
2289 sibling = sibling->prev;
2290 }
2291
Michal Vaskob1b5c262020-03-05 14:29:47 +01002292 if (node->parent || node->prev->next) {
2293 lyd_unlink_tree(node);
2294 }
2295
2296 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002297 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002298 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002299 return LY_EINVAL;
2300 }
2301
Michal Vaskob1b5c262020-03-05 14:29:47 +01002302 iter = node->next;
2303 lyd_unlink_tree(node);
2304 lyd_insert_node(NULL, &sibling, node);
2305 node = iter;
2306 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002307
Michal Vaskob104f112020-07-17 09:54:54 +02002308 if (first) {
2309 /* find the first sibling */
2310 *first = sibling;
2311 while ((*first)->prev->next) {
2312 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002313 }
2314 }
2315
2316 return LY_SUCCESS;
2317}
2318
Michal Vaskob1b5c262020-03-05 14:29:47 +01002319API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002320lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2321{
Michal Vaskof03ed032020-03-04 13:31:44 +01002322 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2323
Michal Vasko717a4c32020-12-07 09:40:10 +01002324 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002325
Michal Vaskob104f112020-07-17 09:54:54 +02002326 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002327 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002328 return LY_EINVAL;
2329 }
2330
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002331 lyd_unlink_tree(node);
2332 lyd_insert_before_node(sibling, node);
2333 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002334
Michal Vaskof03ed032020-03-04 13:31:44 +01002335 return LY_SUCCESS;
2336}
2337
2338API LY_ERR
2339lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2340{
Michal Vaskof03ed032020-03-04 13:31:44 +01002341 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2342
Michal Vasko717a4c32020-12-07 09:40:10 +01002343 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002344
Michal Vaskob104f112020-07-17 09:54:54 +02002345 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002346 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002347 return LY_EINVAL;
2348 }
2349
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002350 lyd_unlink_tree(node);
2351 lyd_insert_after_node(sibling, node);
2352 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002353
Michal Vaskof03ed032020-03-04 13:31:44 +01002354 return LY_SUCCESS;
2355}
2356
2357API void
2358lyd_unlink_tree(struct lyd_node *node)
2359{
2360 struct lyd_node *iter;
2361
2362 if (!node) {
2363 return;
2364 }
2365
Michal Vaskob104f112020-07-17 09:54:54 +02002366 /* update hashes while still linked into the tree */
2367 lyd_unlink_hash(node);
2368
Michal Vaskof03ed032020-03-04 13:31:44 +01002369 /* unlink from siblings */
2370 if (node->prev->next) {
2371 node->prev->next = node->next;
2372 }
2373 if (node->next) {
2374 node->next->prev = node->prev;
2375 } else {
2376 /* unlinking the last node */
2377 if (node->parent) {
2378 iter = node->parent->child;
2379 } else {
2380 iter = node->prev;
2381 while (iter->prev != node) {
2382 iter = iter->prev;
2383 }
2384 }
2385 /* update the "last" pointer from the first node */
2386 iter->prev = node->prev;
2387 }
2388
2389 /* unlink from parent */
2390 if (node->parent) {
2391 if (node->parent->child == node) {
2392 /* the node is the first child */
2393 node->parent->child = node->next;
2394 }
2395
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002396 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002397 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2398 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002399 LY_LIST_FOR(node->parent->child, iter) {
2400 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2401 break;
2402 }
2403 }
2404 if (!iter) {
2405 node->parent->flags |= LYD_DEFAULT;
2406 }
2407 }
2408
Michal Vaskof03ed032020-03-04 13:31:44 +01002409 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002410 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002411 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002412 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002413 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002414 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002415 }
2416 }
2417
2418 node->parent = NULL;
2419 }
2420
2421 node->next = NULL;
2422 node->prev = node;
2423}
2424
Michal Vaskoa5da3292020-08-12 13:10:50 +02002425void
Michal Vasko871a0252020-11-11 18:35:24 +01002426lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002427{
2428 struct lyd_meta *last, *iter;
2429
2430 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002431
2432 if (!meta) {
2433 return;
2434 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002435
2436 for (iter = meta; iter; iter = iter->next) {
2437 iter->parent = parent;
2438 }
2439
2440 /* insert as the last attribute */
2441 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002442 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002443 last->next = meta;
2444 } else {
2445 parent->meta = meta;
2446 }
2447
2448 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002449 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002450 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002451 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002452 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002453}
2454
2455LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002456lyd_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 +02002457 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 +01002458 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002459{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002460 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002461 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002462 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002463 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002464
Michal Vasko9f96a052020-03-10 09:41:45 +01002465 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002466
Radek Krejciddace2c2021-01-08 11:30:56 +01002467 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002468
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002469 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002470 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002471 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002472 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002473 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002474 break;
2475 }
2476 }
2477 if (!ant) {
2478 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002479 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Michal Vasko90932a92020-02-12 14:33:03 +01002480 mod->name, name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002481 ret = LY_EINVAL;
2482 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002483 }
2484
Michal Vasko9f96a052020-03-10 09:41:45 +01002485 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002486 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002487 mt->parent = parent;
2488 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002489 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2490 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002491 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2492 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2493 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002494
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002495 /* insert as the last attribute */
2496 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002497 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002498 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002499 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002500 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002501 }
2502
Michal Vasko9f96a052020-03-10 09:41:45 +01002503 if (meta) {
2504 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002505 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002506
2507cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002508 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002509 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002510}
2511
Michal Vaskoa5da3292020-08-12 13:10:50 +02002512void
2513lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2514{
2515 struct lyd_attr *last, *iter;
2516 struct lyd_node_opaq *opaq;
2517
2518 assert(parent && !parent->schema);
2519
2520 if (!attr) {
2521 return;
2522 }
2523
2524 opaq = (struct lyd_node_opaq *)parent;
2525 for (iter = attr; iter; iter = iter->next) {
2526 iter->parent = opaq;
2527 }
2528
2529 /* insert as the last attribute */
2530 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002531 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002532 last->next = attr;
2533 } else {
2534 opaq->attr = attr;
2535 }
2536}
2537
Michal Vasko52927e22020-03-16 17:26:14 +01002538LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002539lyd_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 +01002540 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2541 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 +01002542{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002543 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002544 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002545
2546 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002547 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002548
2549 if (!value_len) {
2550 value = "";
2551 }
2552
2553 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002554 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002555
Michal Vaskoad92b672020-11-12 13:11:31 +01002556 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002557 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002558 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002559 }
2560 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002561 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002562 }
2563
Michal Vasko52927e22020-03-16 17:26:14 +01002564 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002565 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2566 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002567 *dynamic = 0;
2568 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002569 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002570 }
Michal Vasko501af032020-11-11 20:27:44 +01002571 at->format = format;
2572 at->val_prefix_data = val_prefix_data;
2573 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002574
2575 /* insert as the last attribute */
2576 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002577 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002578 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002579 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002580 last->next = at;
2581 }
2582
Radek Krejci011e4aa2020-09-04 15:22:31 +02002583finish:
2584 if (ret) {
2585 lyd_free_attr_single(ctx, at);
2586 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002587 *attr = at;
2588 }
2589 return LY_SUCCESS;
2590}
2591
Radek Krejci084289f2019-07-09 17:35:30 +02002592API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002593lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002594{
Michal Vasko004d3152020-06-11 19:59:22 +02002595 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002596
Michal Vasko004d3152020-06-11 19:59:22 +02002597 if (ly_path_eval(path, tree, &target)) {
2598 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002599 }
2600
Michal Vasko004d3152020-06-11 19:59:22 +02002601 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002602}
2603
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002604API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002605lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002606{
2607 const struct lyd_node *iter1, *iter2;
2608 struct lyd_node_term *term1, *term2;
2609 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002610 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002611 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002612
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002613 if (!node1 || !node2) {
2614 if (node1 == node2) {
2615 return LY_SUCCESS;
2616 } else {
2617 return LY_ENOT;
2618 }
2619 }
2620
Michal Vaskob7be7a82020-08-20 09:09:04 +02002621 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002622 return LY_ENOT;
2623 }
2624
2625 if (node1->hash != node2->hash) {
2626 return LY_ENOT;
2627 }
Michal Vasko52927e22020-03-16 17:26:14 +01002628 /* 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 +02002629
Michal Vasko52927e22020-03-16 17:26:14 +01002630 if (!node1->schema) {
2631 opaq1 = (struct lyd_node_opaq *)node1;
2632 opaq2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +01002633 if ((opaq1->name.name != opaq2->name.name) || (opaq1->format != opaq2->format) ||
2634 (opaq1->name.module_ns != opaq2->name.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002635 return LY_ENOT;
2636 }
Michal Vasko52927e22020-03-16 17:26:14 +01002637 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002638 case LY_PREF_XML:
2639 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 +01002640 return LY_ENOT;
2641 }
2642 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002643 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002644 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2645 if (strcmp(opaq1->value, opaq2->value)) {
2646 return LY_ENOT;
2647 }
2648 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002649 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002650 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002651 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002652 return LY_EINT;
2653 }
2654 if (options & LYD_COMPARE_FULL_RECURSION) {
2655 iter1 = opaq1->child;
2656 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002657 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002658 }
2659 return LY_SUCCESS;
2660 } else {
2661 switch (node1->schema->nodetype) {
2662 case LYS_LEAF:
2663 case LYS_LEAFLIST:
2664 if (options & LYD_COMPARE_DEFAULTS) {
2665 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2666 return LY_ENOT;
2667 }
2668 }
2669
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002670 term1 = (struct lyd_node_term *)node1;
2671 term2 = (struct lyd_node_term *)node2;
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002672 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002673 case LYS_CONTAINER:
2674 if (options & LYD_COMPARE_DEFAULTS) {
2675 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2676 return LY_ENOT;
2677 }
2678 }
2679 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002680 iter1 = lyd_child(node1);
2681 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002682 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002683 }
2684 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002685 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002686 case LYS_ACTION:
2687 if (options & LYD_COMPARE_FULL_RECURSION) {
2688 /* TODO action/RPC
2689 goto all_children_compare;
2690 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002691 }
2692 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002693 case LYS_NOTIF:
2694 if (options & LYD_COMPARE_FULL_RECURSION) {
2695 /* TODO Notification
2696 goto all_children_compare;
2697 */
2698 }
2699 return LY_SUCCESS;
2700 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01002701 iter1 = lyd_child(node1);
2702 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002703
2704 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2705 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01002706 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002707 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002708 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002709 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002710 return LY_ENOT;
2711 }
2712 iter1 = iter1->next;
2713 iter2 = iter2->next;
2714 }
2715 } else {
2716 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2717
Radek Krejci0f969882020-08-21 16:56:47 +02002718all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002719 if (!iter1 && !iter2) {
2720 /* no children, nothing to compare */
2721 return LY_SUCCESS;
2722 }
2723
Michal Vaskod989ba02020-08-24 10:59:24 +02002724 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002725 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002726 return LY_ENOT;
2727 }
2728 }
2729 if (iter1 || iter2) {
2730 return LY_ENOT;
2731 }
2732 }
2733 return LY_SUCCESS;
2734 case LYS_ANYXML:
2735 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002736 any1 = (struct lyd_node_any *)node1;
2737 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002738
2739 if (any1->value_type != any2->value_type) {
2740 return LY_ENOT;
2741 }
2742 switch (any1->value_type) {
2743 case LYD_ANYDATA_DATATREE:
2744 iter1 = any1->value.tree;
2745 iter2 = any2->value.tree;
2746 goto all_children_compare;
2747 case LYD_ANYDATA_STRING:
2748 case LYD_ANYDATA_XML:
2749 case LYD_ANYDATA_JSON:
2750 len1 = strlen(any1->value.str);
2751 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002752 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002753 return LY_ENOT;
2754 }
2755 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002756 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002757 len1 = lyd_lyb_data_length(any1->value.mem);
2758 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002759 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002760 return LY_ENOT;
2761 }
2762 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002763 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002764 }
2765 }
2766
Michal Vaskob7be7a82020-08-20 09:09:04 +02002767 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002768 return LY_EINT;
2769}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002770
Michal Vasko21725742020-06-29 11:49:25 +02002771API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002772lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002773{
Michal Vaskod989ba02020-08-24 10:59:24 +02002774 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002775 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2776 }
2777
Michal Vasko11a81432020-07-28 16:31:29 +02002778 if (node1 == node2) {
2779 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002780 }
Michal Vasko11a81432020-07-28 16:31:29 +02002781 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002782}
2783
2784API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002785lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2786{
2787 if (!meta1 || !meta2) {
2788 if (meta1 == meta2) {
2789 return LY_SUCCESS;
2790 } else {
2791 return LY_ENOT;
2792 }
2793 }
2794
Michal Vaskoa8083062020-11-06 17:22:10 +01002795 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002796 return LY_ENOT;
2797 }
2798
Michal Vasko21725742020-06-29 11:49:25 +02002799 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2800}
2801
Radek Krejci22ebdba2019-07-25 13:59:43 +02002802/**
Michal Vasko52927e22020-03-16 17:26:14 +01002803 * @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 +02002804 *
2805 * 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 +02002806 *
2807 * @param[in] node Original node to duplicate
2808 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2809 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2810 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2811 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2812 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002813 */
Michal Vasko52927e22020-03-16 17:26:14 +01002814static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002815lyd_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 +02002816 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002817{
Michal Vasko52927e22020-03-16 17:26:14 +01002818 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002819 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002820 struct lyd_meta *meta;
2821 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002822
Michal Vasko52927e22020-03-16 17:26:14 +01002823 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002824
Michal Vasko52927e22020-03-16 17:26:14 +01002825 if (!node->schema) {
2826 dup = calloc(1, sizeof(struct lyd_node_opaq));
2827 } else {
2828 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002829 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002830 case LYS_ACTION:
2831 case LYS_NOTIF:
2832 case LYS_CONTAINER:
2833 case LYS_LIST:
2834 dup = calloc(1, sizeof(struct lyd_node_inner));
2835 break;
2836 case LYS_LEAF:
2837 case LYS_LEAFLIST:
2838 dup = calloc(1, sizeof(struct lyd_node_term));
2839 break;
2840 case LYS_ANYDATA:
2841 case LYS_ANYXML:
2842 dup = calloc(1, sizeof(struct lyd_node_any));
2843 break;
2844 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002845 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002846 ret = LY_EINT;
2847 goto error;
2848 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002849 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002850 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002851
Michal Vaskof6df0a02020-06-16 13:08:34 +02002852 if (options & LYD_DUP_WITH_FLAGS) {
2853 dup->flags = node->flags;
2854 } else {
2855 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2856 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002857 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002858 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002859
Michal Vasko25a32822020-07-09 15:48:22 +02002860 /* duplicate metadata */
2861 if (!(options & LYD_DUP_NO_META)) {
2862 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002863 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002864 }
2865 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002866
2867 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002868 if (!dup->schema) {
2869 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2870 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2871 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002872
2873 if (options & LYD_DUP_RECURSIVE) {
2874 /* duplicate all the children */
2875 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002876 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002877 }
2878 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002879 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002880 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01002881 if (orig->name.prefix) {
2882 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002883 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002884 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 +01002885 if (orig->val_prefix_data) {
2886 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
2887 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002888 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002889 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002890 opaq->ctx = orig->ctx;
2891 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2892 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2893 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2894
2895 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002896 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002897 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002898 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2899 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2900 struct lyd_node *child;
2901
2902 if (options & LYD_DUP_RECURSIVE) {
2903 /* duplicate all the children */
2904 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002905 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002906 }
Michal Vasko69730152020-10-09 16:30:07 +02002907 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002908 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002909 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01002910 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002911 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002912 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002913 if (!child) {
2914 /* possibly not keys are present in filtered tree */
2915 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002916 } else if (child->schema != key) {
2917 /* possibly not all keys are present in filtered tree,
2918 * but there can be also some non-key nodes */
2919 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002920 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002921 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002922 child = child->next;
2923 }
2924 }
2925 lyd_hash(dup);
2926 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002927 dup->hash = node->hash;
2928 any = (struct lyd_node_any *)node;
2929 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002930 }
2931
Michal Vasko52927e22020-03-16 17:26:14 +01002932 /* insert */
2933 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002934
2935 if (dup_p) {
2936 *dup_p = dup;
2937 }
2938 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002939
2940error:
Michal Vasko52927e22020-03-16 17:26:14 +01002941 lyd_free_tree(dup);
2942 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002943}
2944
Michal Vasko3a41dff2020-07-15 14:30:28 +02002945static LY_ERR
2946lyd_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 +02002947 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002948{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002949 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002950 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002951
2952 *dup_parent = NULL;
2953 *local_parent = NULL;
2954
2955 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2956 if (parent && (parent->schema == orig_parent->schema)) {
2957 /* stop creating parents, connect what we have into the provided parent */
2958 iter = parent;
2959 repeat = 0;
2960 } else {
2961 iter = NULL;
2962 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002963 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002964 }
2965 if (!*local_parent) {
2966 *local_parent = (struct lyd_node_inner *)iter;
2967 }
2968 if (iter->child) {
2969 /* 1) list - add after keys
2970 * 2) provided parent with some children */
2971 iter->child->prev->next = *dup_parent;
2972 if (*dup_parent) {
2973 (*dup_parent)->prev = iter->child->prev;
2974 iter->child->prev = *dup_parent;
2975 }
2976 } else {
2977 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2978 }
2979 if (*dup_parent) {
2980 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2981 }
2982 *dup_parent = (struct lyd_node *)iter;
2983 }
2984
2985 if (repeat && parent) {
2986 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002987 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002988 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002989 return LY_EINVAL;
2990 }
2991
2992 return LY_SUCCESS;
2993}
2994
2995static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002996lyd_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 +02002997{
2998 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002999 const struct lyd_node *orig; /* original node to be duplicated */
3000 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003001 struct lyd_node *top = NULL; /* the most higher created node */
3002 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003003
Michal Vasko3a41dff2020-07-15 14:30:28 +02003004 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003005
3006 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003007 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003008 } else {
3009 local_parent = parent;
3010 }
3011
Radek Krejci22ebdba2019-07-25 13:59:43 +02003012 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003013 if (lysc_is_key(orig->schema)) {
3014 if (local_parent) {
3015 /* the key must already exist in the parent */
3016 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3017 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3018 } else {
3019 assert(!(options & LYD_DUP_WITH_PARENTS));
3020 /* duplicating a single key, okay, I suppose... */
3021 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3022 LY_CHECK_GOTO(rc, error);
3023 }
3024 } else {
3025 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003026 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003027 LY_CHECK_GOTO(rc, error);
3028 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003029 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003030 break;
3031 }
3032 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003033
3034 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003035 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003036 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003037 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003038 }
3039 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003040
3041 if (dup) {
3042 *dup = first;
3043 }
3044 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003045
3046error:
3047 if (top) {
3048 lyd_free_tree(top);
3049 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003050 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003051 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003052 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003053}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003054
Michal Vasko3a41dff2020-07-15 14:30:28 +02003055API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003056lyd_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 +02003057{
3058 return lyd_dup(node, parent, options, 1, dup);
3059}
3060
3061API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003062lyd_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 +02003063{
3064 return lyd_dup(node, parent, options, 0, dup);
3065}
3066
3067API LY_ERR
3068lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003069{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003070 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003071 struct lyd_meta *mt, *last;
3072
Michal Vasko3a41dff2020-07-15 14:30:28 +02003073 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003074
3075 /* create a copy */
3076 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003077 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003078 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003079 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003080 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3081 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003082
3083 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003084 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003085 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003086 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003087 last->next = mt;
3088 } else {
3089 node->meta = mt;
3090 }
3091
Radek Krejci011e4aa2020-09-04 15:22:31 +02003092finish:
3093 if (ret) {
3094 lyd_free_meta_single(mt);
3095 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003096 *dup = mt;
3097 }
3098 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003099}
3100
Michal Vasko4490d312020-06-16 13:08:55 +02003101/**
3102 * @brief Merge a source sibling into target siblings.
3103 *
3104 * @param[in,out] first_trg First target sibling, is updated if top-level.
3105 * @param[in] parent_trg Target parent.
3106 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3107 * @param[in] options Merge options.
3108 * @return LY_ERR value.
3109 */
3110static LY_ERR
3111lyd_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 +02003112 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003113{
3114 LY_ERR ret;
3115 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003116 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003117 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003118
3119 sibling_src = *sibling_src_p;
3120 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3121 /* try to find the exact instance */
3122 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3123 } else {
3124 /* try to simply find the node, there cannot be more instances */
3125 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3126 }
3127
3128 if (!ret) {
3129 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003130 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003131 /* since they are different, they cannot both be default */
3132 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3133
Michal Vasko3a41dff2020-07-15 14:30:28 +02003134 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3135 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003136 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003137 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3138 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003139 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003140
3141 /* copy flags and add LYD_NEW */
3142 match_trg->flags = sibling_src->flags | LYD_NEW;
3143 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003144 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003145 /* update value */
3146 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003147 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003148
3149 /* copy flags and add LYD_NEW */
3150 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003151 } else {
3152 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003153 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003154 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 +02003155 }
3156 }
3157 } else {
3158 /* node not found, merge it */
3159 if (options & LYD_MERGE_DESTRUCT) {
3160 dup_src = (struct lyd_node *)sibling_src;
3161 lyd_unlink_tree(dup_src);
3162 /* spend it */
3163 *sibling_src_p = NULL;
3164 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003165 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 +02003166 }
3167
3168 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003169 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003170 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003171 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003172 }
3173
3174 lyd_insert_node(parent_trg, first_trg, dup_src);
3175 }
3176
3177 return LY_SUCCESS;
3178}
3179
Michal Vasko3a41dff2020-07-15 14:30:28 +02003180static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003181lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003182{
3183 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003184 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003185
3186 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3187
3188 if (!source) {
3189 /* nothing to merge */
3190 return LY_SUCCESS;
3191 }
3192
Michal Vasko831422b2020-11-24 11:20:51 +01003193 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003194 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003195 return LY_EINVAL;
3196 }
3197
3198 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003199 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003200 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3201 if (first && !sibling_src) {
3202 /* source was spent (unlinked), move to the next node */
3203 source = tmp;
3204 }
3205
Michal Vasko3a41dff2020-07-15 14:30:28 +02003206 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003207 break;
3208 }
3209 }
3210
3211 if (options & LYD_MERGE_DESTRUCT) {
3212 /* free any leftover source data that were not merged */
3213 lyd_free_siblings((struct lyd_node *)source);
3214 }
3215
3216 return LY_SUCCESS;
3217}
3218
Michal Vasko3a41dff2020-07-15 14:30:28 +02003219API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003220lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003221{
3222 return lyd_merge(target, source, options, 1);
3223}
3224
3225API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003226lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003227{
3228 return lyd_merge(target, source, options, 0);
3229}
3230
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003231static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003232lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003233{
Michal Vasko14654712020-02-06 08:35:21 +01003234 /* ending \0 */
3235 ++reqlen;
3236
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003237 if (reqlen > *buflen) {
3238 if (is_static) {
3239 return LY_EINCOMPLETE;
3240 }
3241
3242 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3243 if (!*buffer) {
3244 return LY_EMEM;
3245 }
3246
3247 *buflen = reqlen;
3248 }
3249
3250 return LY_SUCCESS;
3251}
3252
Michal Vaskod59035b2020-07-08 12:00:06 +02003253LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003254lyd_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 +02003255{
3256 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003257 size_t len;
3258 const char *val;
3259 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003260
Radek Krejcia1c1e542020-09-29 16:06:52 +02003261 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003262 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003263 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003264 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003265
3266 quot = '\'';
3267 if (strchr(val, '\'')) {
3268 quot = '"';
3269 }
3270 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003271 }
3272
3273 return LY_SUCCESS;
3274}
3275
3276/**
3277 * @brief Append leaf-list value predicate to path.
3278 *
3279 * @param[in] node Node to print.
3280 * @param[in,out] buffer Buffer to print to.
3281 * @param[in,out] buflen Current buffer length.
3282 * @param[in,out] bufused Current number of characters used in @p buffer.
3283 * @param[in] is_static Whether buffer is static or can be reallocated.
3284 * @return LY_ERR
3285 */
3286static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003287lyd_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 +02003288{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003289 size_t len;
3290 const char *val;
3291 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003292
Michal Vaskob7be7a82020-08-20 09:09:04 +02003293 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003294 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003295 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003296
3297 quot = '\'';
3298 if (strchr(val, '\'')) {
3299 quot = '"';
3300 }
3301 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3302
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003303 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003304}
3305
3306/**
3307 * @brief Append node position (relative to its other instances) predicate to path.
3308 *
3309 * @param[in] node Node to print.
3310 * @param[in,out] buffer Buffer to print to.
3311 * @param[in,out] buflen Current buffer length.
3312 * @param[in,out] bufused Current number of characters used in @p buffer.
3313 * @param[in] is_static Whether buffer is static or can be reallocated.
3314 * @return LY_ERR
3315 */
3316static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003317lyd_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 +02003318{
3319 const struct lyd_node *first, *iter;
3320 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003321 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003322 char *val = NULL;
3323 LY_ERR rc;
3324
3325 if (node->parent) {
3326 first = node->parent->child;
3327 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003328 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003329 }
3330 pos = 1;
3331 for (iter = first; iter != node; iter = iter->next) {
3332 if (iter->schema == node->schema) {
3333 ++pos;
3334 }
3335 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003336 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003337 return LY_EMEM;
3338 }
3339
3340 len = 1 + strlen(val) + 1;
3341 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3342 if (rc != LY_SUCCESS) {
3343 goto cleanup;
3344 }
3345
3346 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3347
3348cleanup:
3349 free(val);
3350 return rc;
3351}
3352
3353API char *
3354lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3355{
Radek Krejci857189e2020-09-01 13:26:36 +02003356 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003357 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003358 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003359 const struct lyd_node *iter;
3360 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003361 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003362
3363 LY_CHECK_ARG_RET(NULL, node, NULL);
3364 if (buffer) {
3365 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3366 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003367 } else {
3368 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003369 }
3370
3371 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003372 case LYD_PATH_STD:
3373 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003374 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003375 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003376 ++depth;
3377 }
3378
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003379 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003380 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003381 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003382 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003383iter_print:
3384 /* print prefix and name */
3385 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003386 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003387 mod = iter->schema->module;
3388 }
3389
3390 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003391 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3392 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003393 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3394 if (rc != LY_SUCCESS) {
3395 break;
3396 }
3397
3398 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003399 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003400 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003401
Michal Vasko790b2bc2020-08-03 13:35:06 +02003402 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003403 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003404 switch (iter->schema->nodetype) {
3405 case LYS_LIST:
3406 if (iter->schema->flags & LYS_KEYLESS) {
3407 /* print its position */
3408 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3409 } else {
3410 /* print all list keys in predicates */
3411 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3412 }
3413 break;
3414 case LYS_LEAFLIST:
3415 if (iter->schema->flags & LYS_CONFIG_W) {
3416 /* print leaf-list value */
3417 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3418 } else {
3419 /* print its position */
3420 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3421 }
3422 break;
3423 default:
3424 /* nothing to print more */
3425 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003426 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003427 }
3428 if (rc != LY_SUCCESS) {
3429 break;
3430 }
3431
Michal Vasko14654712020-02-06 08:35:21 +01003432 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003433 }
3434 break;
3435 }
3436
3437 return buffer;
3438}
Michal Vaskoe444f752020-02-10 12:20:06 +01003439
Michal Vasko25a32822020-07-09 15:48:22 +02003440API struct lyd_meta *
3441lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3442{
3443 struct lyd_meta *ret = NULL;
3444 const struct ly_ctx *ctx;
3445 const char *prefix, *tmp;
3446 char *str;
3447 size_t pref_len, name_len;
3448
3449 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3450
3451 if (!first) {
3452 return NULL;
3453 }
3454
3455 ctx = first->annotation->module->ctx;
3456
3457 /* parse the name */
3458 tmp = name;
3459 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3460 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3461 return NULL;
3462 }
3463
3464 /* find the module */
3465 if (prefix) {
3466 str = strndup(prefix, pref_len);
3467 module = ly_ctx_get_module_latest(ctx, str);
3468 free(str);
3469 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3470 }
3471
3472 /* find the metadata */
3473 LY_LIST_FOR(first, first) {
3474 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3475 ret = (struct lyd_meta *)first;
3476 break;
3477 }
3478 }
3479
3480 return ret;
3481}
3482
Michal Vasko9b368d32020-02-14 13:53:31 +01003483API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003484lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3485{
3486 struct lyd_node **match_p;
3487 struct lyd_node_inner *parent;
3488
Michal Vaskof03ed032020-03-04 13:31:44 +01003489 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003490
Michal Vasko6da1e952020-12-09 18:14:38 +01003491 if (!siblings || (siblings->schema && target->schema &&
3492 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003493 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003494 if (match) {
3495 *match = NULL;
3496 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003497 return LY_ENOTFOUND;
3498 }
3499
3500 /* find first sibling */
3501 if (siblings->parent) {
3502 siblings = siblings->parent->child;
3503 } else {
3504 while (siblings->prev->next) {
3505 siblings = siblings->prev;
3506 }
3507 }
3508
Michal Vasko9e685082021-01-29 14:49:09 +01003509 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003510 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003511 assert(target->hash);
3512
3513 /* find by hash */
3514 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003515 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003516 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003517 siblings = *match_p;
3518 } else {
3519 siblings = NULL;
3520 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003521 } else {
3522 /* not found */
3523 siblings = NULL;
3524 }
3525 } else {
3526 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003527 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003528 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003529 break;
3530 }
3531 }
3532 }
3533
3534 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003535 if (match) {
3536 *match = NULL;
3537 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003538 return LY_ENOTFOUND;
3539 }
3540
Michal Vasko9b368d32020-02-14 13:53:31 +01003541 if (match) {
3542 *match = (struct lyd_node *)siblings;
3543 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003544 return LY_SUCCESS;
3545}
3546
Radek Krejci857189e2020-09-01 13:26:36 +02003547/**
3548 * @brief Comparison callback to match schema node with a schema of a data node.
3549 *
3550 * @param[in] val1_p Pointer to the schema node
3551 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003552 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003553 */
3554static ly_bool
3555lyd_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 +01003556{
3557 struct lysc_node *val1;
3558 struct lyd_node *val2;
3559
3560 val1 = *((struct lysc_node **)val1_p);
3561 val2 = *((struct lyd_node **)val2_p);
3562
Michal Vasko90932a92020-02-12 14:33:03 +01003563 if (val1 == val2->schema) {
3564 /* schema match is enough */
3565 return 1;
3566 } else {
3567 return 0;
3568 }
3569}
3570
Michal Vasko92239c72020-11-18 18:17:25 +01003571/**
3572 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3573 * Uses hashes - should be used whenever possible for best performance.
3574 *
3575 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3576 * @param[in] schema Target data node schema to find.
3577 * @param[out] match Can be NULL, otherwise the found data node.
3578 * @return LY_SUCCESS on success, @p match set.
3579 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3580 * @return LY_ERR value if another error occurred.
3581 */
Michal Vasko90932a92020-02-12 14:33:03 +01003582static LY_ERR
3583lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3584{
3585 struct lyd_node **match_p;
3586 struct lyd_node_inner *parent;
3587 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003588 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003589
Michal Vaskob104f112020-07-17 09:54:54 +02003590 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003591
Michal Vasko9e685082021-01-29 14:49:09 +01003592 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003593 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003594 /* calculate our hash */
3595 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3596 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3597 hash = dict_hash_multi(hash, NULL, 0);
3598
3599 /* use special hash table function */
3600 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3601
3602 /* find by hash */
3603 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3604 siblings = *match_p;
3605 } else {
3606 /* not found */
3607 siblings = NULL;
3608 }
3609
3610 /* set the original hash table compare function back */
3611 lyht_set_cb(parent->children_ht, ht_cb);
3612 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003613 /* find first sibling */
3614 if (siblings->parent) {
3615 siblings = siblings->parent->child;
3616 } else {
3617 while (siblings->prev->next) {
3618 siblings = siblings->prev;
3619 }
3620 }
3621
3622 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003623 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003624 if (siblings->schema == schema) {
3625 /* schema match is enough */
3626 break;
3627 }
3628 }
3629 }
3630
3631 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003632 if (match) {
3633 *match = NULL;
3634 }
Michal Vasko90932a92020-02-12 14:33:03 +01003635 return LY_ENOTFOUND;
3636 }
3637
Michal Vasko9b368d32020-02-14 13:53:31 +01003638 if (match) {
3639 *match = (struct lyd_node *)siblings;
3640 }
Michal Vasko90932a92020-02-12 14:33:03 +01003641 return LY_SUCCESS;
3642}
3643
Michal Vaskoe444f752020-02-10 12:20:06 +01003644API LY_ERR
3645lyd_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 +02003646 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003647{
3648 LY_ERR rc;
3649 struct lyd_node *target = NULL;
3650
Michal Vasko4c583e82020-07-17 12:16:14 +02003651 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003652
Michal Vasko08fd6132020-11-18 18:17:45 +01003653 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003654 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003655 if (match) {
3656 *match = NULL;
3657 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003658 return LY_ENOTFOUND;
3659 }
3660
Michal Vaskof03ed032020-03-04 13:31:44 +01003661 if (key_or_value && !val_len) {
3662 val_len = strlen(key_or_value);
3663 }
3664
Michal Vaskob104f112020-07-17 09:54:54 +02003665 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3666 /* create a data node and find the instance */
3667 if (schema->nodetype == LYS_LEAFLIST) {
3668 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003669 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3670 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003671 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003672 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003673 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003674 }
3675
3676 /* find it */
3677 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003678 } else {
3679 /* find the first schema node instance */
3680 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003681 }
3682
Michal Vaskoe444f752020-02-10 12:20:06 +01003683 lyd_free_tree(target);
3684 return rc;
3685}
Michal Vaskoccc02342020-05-21 10:09:21 +02003686
3687API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003688lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3689{
3690 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3691
3692 for ( ; first; first = first->next) {
3693 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
3694 break;
3695 }
3696 }
3697
3698 if (match) {
3699 *match = (struct lyd_node *)first;
3700 }
3701 return first ? LY_SUCCESS : LY_ENOTFOUND;
3702}
3703
3704API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02003705lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3706{
3707 LY_ERR ret = LY_SUCCESS;
3708 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003709 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003710 uint32_t i;
3711
3712 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3713
3714 memset(&xp_set, 0, sizeof xp_set);
3715
3716 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003717 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3718 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003719
3720 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01003721 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 +02003722 LY_CHECK_GOTO(ret, cleanup);
3723
3724 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003725 ret = ly_set_new(set);
3726 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003727
3728 /* transform into ly_set */
3729 if (xp_set.type == LYXP_SET_NODE_SET) {
3730 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3731 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003732 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003733 (*set)->size = xp_set.used;
3734
3735 for (i = 0; i < xp_set.used; ++i) {
3736 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003737 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003738 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003739 }
3740 }
3741 }
3742
3743cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003744 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003745 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01003746 if (ret) {
3747 ly_set_free(*set, NULL);
3748 *set = NULL;
3749 }
Michal Vaskoccc02342020-05-21 10:09:21 +02003750 return ret;
3751}
Radek Krejcica989142020-11-05 11:32:22 +01003752
Michal Vasko3e1f6552021-01-14 09:27:55 +01003753API LY_ERR
3754lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3755{
3756 LY_ERR ret = LY_SUCCESS;
3757 struct lyxp_expr *expr = NULL;
3758 struct ly_path *lypath = NULL;
3759
3760 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3761
3762 /* parse the path */
3763 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
3764 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
3765 LY_CHECK_GOTO(ret, cleanup);
3766
3767 /* compile the path */
3768 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, expr, LY_PATH_LREF_FALSE,
3769 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
3770 (void *)LYD_CTX(ctx_node), NULL, &lypath);
3771 LY_CHECK_GOTO(ret, cleanup);
3772
3773 /* evaluate the path */
3774 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
3775
3776cleanup:
3777 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3778 ly_path_free(LYD_CTX(ctx_node), lypath);
3779 return ret;
3780}
3781
Radek Krejcica989142020-11-05 11:32:22 +01003782API uint32_t
3783lyd_list_pos(const struct lyd_node *instance)
3784{
3785 const struct lyd_node *iter = NULL;
3786 uint32_t pos = 0;
3787
3788 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3789 return 0;
3790 }
3791
3792 /* data instances are ordered, so we can stop when we found instance of other schema node */
3793 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01003794 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01003795 /* overrun to the end of the siblings list */
3796 break;
3797 }
3798 ++pos;
3799 }
3800
3801 return pos;
3802}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003803
3804API struct lyd_node *
3805lyd_first_sibling(const struct lyd_node *node)
3806{
3807 struct lyd_node *start;
3808
3809 if (!node) {
3810 return NULL;
3811 }
3812
3813 /* get the first sibling */
3814 if (node->parent) {
3815 start = node->parent->child;
3816 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01003817 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003818 }
3819
3820 return start;
3821}