blob: 7b3ae217988c4123bd56718b1e38c55aedbb8127 [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>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "context.h"
33#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020034#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "plugins_exts.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Michal Vasko69730152020-10-09 16:30:07 +020043#include "plugins_exts_metadata.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
48#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
59 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
60 const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Michal Vasko25d6ad02020-10-22 12:20:22 +020064 uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vaskofeca4fb2020-10-05 08:58:40 +020066 if (incomplete) {
67 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020068 }
69
Michal Vaskofeca4fb2020-10-05 08:58:40 +020070 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010071 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020072 if (incomplete) {
73 *incomplete = 1;
74 }
75 } else if (ret) {
76 if (err) {
77 LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg);
78 ly_err_free(err);
79 } else {
80 LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
81 }
82 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010083 }
84
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 if (dynamic) {
86 *dynamic = 0;
87 }
88 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,
93 const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem)
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) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200103 LOGVAL(ctx, log_elem_type, log_elem, 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 {
106 LOGVAL(ctx, log_elem_type, log_elem, 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,
132 LYD_HINT_SCHEMA, node, &storage, &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 Krejci73dead22019-07-11 16:46:16 +0200140 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200141 }
Radek Krejci73dead22019-07-11 16:46:16 +0200142 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200143 }
144
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200145 if (!rc) {
146 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
147 }
Radek Krejci084289f2019-07-09 17:35:30 +0200148 return rc;
149}
150
151API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200152lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
153{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200154 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200155}
156
157API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100158lyd_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 +0200159 const struct lyd_node *tree, const struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200160{
161 LY_ERR rc;
162 struct ly_err_item *err = NULL;
163 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200164 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200165 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200166
167 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
168
Michal Vasko22df3f02020-08-24 13:29:22 +0200169 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200170 /* store */
171 rc = type->plugin->store(ctx ? ctx : LYD_CTX(node), type, value, value_len, 0, LY_PREF_JSON, NULL,
172 LYD_HINT_DATA, node->schema, &val, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200173 if (rc == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200174 stored = 1;
175
176 /* resolve */
177 rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, (struct lyd_node *)node, tree, &val, &err);
178 }
179
180 if (rc) {
Radek Krejci73dead22019-07-11 16:46:16 +0200181 if (err) {
182 if (ctx) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200183 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200184 }
Radek Krejci73dead22019-07-11 16:46:16 +0200185 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200186 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200187 if (stored) {
188 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
189 }
Radek Krejci73dead22019-07-11 16:46:16 +0200190 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200191 }
192
Michal Vasko3701af52020-08-03 14:29:38 +0200193 if (realtype) {
194 *realtype = val.realtype;
195 }
196
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200197 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200198 return LY_SUCCESS;
199}
200
201API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200202lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200203{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200204 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200205 struct ly_ctx *ctx;
206 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200207 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200208
209 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
210
211 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200212 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200213
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200214 /* store the value */
215 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema,
216 NULL, LY_VLOG_LYSC, node->schema);
217 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200218
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200219 /* compare values */
220 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200221
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200222 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200223 return ret;
224}
225
Radek Krejci19611252020-10-04 13:54:53 +0200226API ly_bool
227lyd_is_default(const struct lyd_node *node)
228{
229 const struct lysc_node_leaf *leaf;
230 const struct lysc_node_leaflist *llist;
231 const struct lyd_node_term *term;
232 LY_ARRAY_COUNT_TYPE u;
233
234 assert(node->schema->nodetype & LYD_NODE_TERM);
235 term = (const struct lyd_node_term *)node;
236
237 if (node->schema->nodetype == LYS_LEAF) {
238 leaf = (const struct lysc_node_leaf *)node->schema;
239 if (!leaf->dflt) {
240 return 0;
241 }
242
243 /* compare with the default value */
244 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
245 return 0;
246 }
247 } else {
248 llist = (const struct lysc_node_leaflist *)node->schema;
249 if (!llist->dflts) {
250 return 0;
251 }
252
253 LY_ARRAY_FOR(llist->dflts, u) {
254 /* compare with each possible default value */
255 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
256 return 0;
257 }
258 }
259 }
260
261 return 1;
262}
263
Radek Krejci7931b192020-06-25 17:05:03 +0200264static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200265lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200266{
Michal Vasko69730152020-10-09 16:30:07 +0200267 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200268 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200269 const char *path = in->method.fpath.filepath;
270 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200271
272 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200273 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200274
Michal Vasko69730152020-10-09 16:30:07 +0200275 if ((len >= 5) && !strncmp(&path[len - 4], ".xml", 4)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200276 format = LYD_XML;
Michal Vasko69730152020-10-09 16:30:07 +0200277 } else if ((len >= 6) && !strncmp(&path[len - 5], ".json", 5)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200278 format = LYD_JSON;
Michal Vasko69730152020-10-09 16:30:07 +0200279 } else if ((len >= 5) && !strncmp(&path[len - 4], ".lyb", 4)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200280 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200281 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200282 }
283
Radek Krejci7931b192020-06-25 17:05:03 +0200284 return format;
285}
Radek Krejcie7b95092019-05-15 11:03:07 +0200286
Radek Krejci7931b192020-06-25 17:05:03 +0200287API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200288lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200289 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200290{
Radek Krejci1798aae2020-07-14 13:26:06 +0200291 LY_ERR ret = LY_SUCCESS;
292 struct lyd_ctx *lydctx = NULL;
293
Radek Krejci7931b192020-06-25 17:05:03 +0200294 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
295 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
296 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
297
298 format = lyd_parse_get_format(in, format);
299 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
300
Radek Krejci1798aae2020-07-14 13:26:06 +0200301 /* init */
302 *tree = NULL;
303
Michal Vasko63f3d842020-07-08 10:10:14 +0200304 /* remember input position */
305 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200306
307 switch (format) {
308 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200309 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
310 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200311 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200312 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
313 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200314 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200315 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
316 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200317 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200318 LOGINT_RET(ctx);
319 }
320
Radek Krejci1798aae2020-07-14 13:26:06 +0200321 if (!(parse_options & LYD_PARSE_ONLY)) {
322 uint32_t i = 0;
323 const struct lys_module *mod;
324 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200325
Radek Krejci1798aae2020-07-14 13:26:06 +0200326 next = *tree;
327 while (1) {
328 if (validate_options & LYD_VALIDATE_PRESENT) {
329 mod = lyd_data_next_module(&next, &first);
330 } else {
331 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
332 }
333 if (!mod) {
334 break;
335 }
336 if (first == *tree) {
337 /* make sure first2 changes are carried to tree */
338 first2 = tree;
339 } else {
340 first2 = &first;
341 }
342
343 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
344 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
345
346 /* add all top-level defaults for this module */
347 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
Michal Vasko69730152020-10-09 16:30:07 +0200348 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200349 LY_CHECK_GOTO(ret, cleanup);
350
351 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200352 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200353 LY_CHECK_GOTO(ret, cleanup);
354
355 /* perform final validation that assumes the data tree is final */
356 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
357 }
358 }
359
360cleanup:
Michal Vaskoafac7822020-10-20 14:22:26 +0200361 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 if (ret) {
363 lyd_free_all(*tree);
364 *tree = NULL;
365 }
366 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200367}
368
369API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200370lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200371 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200372{
373 LY_ERR ret;
374 struct ly_in *in;
375
376 LY_CHECK_RET(ly_in_new_memory(data, &in));
377 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
378
379 ly_in_free(in, 0);
380 return ret;
381}
382
383API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200384lyd_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 +0200385 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200386{
387 LY_ERR ret;
388 struct ly_in *in;
389
390 LY_CHECK_RET(ly_in_new_fd(fd, &in));
391 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
392
393 ly_in_free(in, 0);
394 return ret;
395}
396
397API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200398lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
399 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200400{
401 LY_ERR ret;
402 struct ly_in *in;
403
404 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
405 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
406
407 ly_in_free(in, 0);
408 return ret;
409}
410
Radek Krejci7931b192020-06-25 17:05:03 +0200411API LY_ERR
412lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
413{
414 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
415
416 format = lyd_parse_get_format(in, format);
417 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
418
Radek Krejci1798aae2020-07-14 13:26:06 +0200419 /* init */
420 *tree = NULL;
421 if (op) {
422 *op = NULL;
423 }
424
Michal Vasko63f3d842020-07-08 10:10:14 +0200425 /* remember input position */
426 in->func_start = in->current;
427
Radek Krejci7931b192020-06-25 17:05:03 +0200428 switch (format) {
429 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200430 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200431 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200432 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200433 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200435 case LYD_UNKNOWN:
436 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200437 }
438
439 LOGINT_RET(ctx);
440}
441
442API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200443lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
Radek Krejci0f969882020-08-21 16:56:47 +0200444 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200445{
446 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200447 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200448
449 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200450 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200451
Radek Krejci1798aae2020-07-14 13:26:06 +0200452 /* init */
453 if (tree) {
454 *tree = NULL;
455 }
456 if (op) {
457 *op = NULL;
458 }
459
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 /* remember input position */
461 in->func_start = in->current;
462
Radek Krejci7931b192020-06-25 17:05:03 +0200463 switch (format) {
464 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200465 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200466 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200468 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200470 case LYD_UNKNOWN:
471 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200472 }
473
Michal Vaskob7be7a82020-08-20 09:09:04 +0200474 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200475}
476
477API LY_ERR
478lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
479{
Radek Krejci1798aae2020-07-14 13:26:06 +0200480 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200481
482 format = lyd_parse_get_format(in, format);
483 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
484
Radek Krejci1798aae2020-07-14 13:26:06 +0200485 /* init */
486 if (tree) {
487 *tree = NULL;
488 }
489 if (ntf) {
490 *ntf = NULL;
491 }
492
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 /* remember input position */
494 in->func_start = in->current;
495
Radek Krejci7931b192020-06-25 17:05:03 +0200496 switch (format) {
497 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200498 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200499 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200500 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200501 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200503 case LYD_UNKNOWN:
504 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200505 }
506
507 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200508}
Radek Krejci084289f2019-07-09 17:35:30 +0200509
Michal Vasko90932a92020-02-12 14:33:03 +0100510LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200511lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
512 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100513{
514 LY_ERR ret;
515 struct lyd_node_term *term;
516
Michal Vasko9b368d32020-02-14 13:53:31 +0100517 assert(schema->nodetype & LYD_NODE_TERM);
518
Michal Vasko90932a92020-02-12 14:33:03 +0100519 term = calloc(1, sizeof *term);
520 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
521
522 term->schema = schema;
523 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100524 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100525
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200526 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
527 value_len, dynamic, format, prefix_data, hints, schema, incomplete, LY_VLOG_LYSC, schema);
528 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9b368d32020-02-14 13:53:31 +0100529 lyd_hash((struct lyd_node *)term);
530
531 *node = (struct lyd_node *)term;
532 return ret;
533}
534
535LY_ERR
536lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
537{
538 LY_ERR ret;
539 struct lyd_node_term *term;
540 struct lysc_type *type;
541
542 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200543 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100544
545 term = calloc(1, sizeof *term);
546 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
547
548 term->schema = schema;
549 term->prev = (struct lyd_node *)term;
550 term->flags = LYD_NEW;
551
552 type = ((struct lysc_node_leaf *)schema)->type;
553 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
554 if (ret) {
555 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
556 free(term);
557 return ret;
558 }
559 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100560
561 *node = (struct lyd_node *)term;
562 return ret;
563}
564
565LY_ERR
566lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
567{
568 struct lyd_node_inner *in;
569
Michal Vasko9b368d32020-02-14 13:53:31 +0100570 assert(schema->nodetype & LYD_NODE_INNER);
571
Michal Vasko90932a92020-02-12 14:33:03 +0100572 in = calloc(1, sizeof *in);
573 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
574
575 in->schema = schema;
576 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100577 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100578 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
579 in->flags |= LYD_DEFAULT;
580 }
Michal Vasko90932a92020-02-12 14:33:03 +0100581
Michal Vasko9b368d32020-02-14 13:53:31 +0100582 /* do not hash list with keys, we need them for the hash */
583 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
584 lyd_hash((struct lyd_node *)in);
585 }
Michal Vasko90932a92020-02-12 14:33:03 +0100586
587 *node = (struct lyd_node *)in;
588 return LY_SUCCESS;
589}
590
Michal Vasko90932a92020-02-12 14:33:03 +0100591LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200592lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100593{
594 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100595 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200596 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100597
Michal Vasko004d3152020-06-11 19:59:22 +0200598 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100599
600 /* create list */
601 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
602
Michal Vasko90932a92020-02-12 14:33:03 +0100603 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200604 LY_ARRAY_FOR(predicates, u) {
605 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100606 lyd_insert_node(list, NULL, key);
607 }
608
Michal Vasko9b368d32020-02-14 13:53:31 +0100609 /* hash having all the keys */
610 lyd_hash(list);
611
Michal Vasko90932a92020-02-12 14:33:03 +0100612 /* success */
613 *node = list;
614 list = NULL;
615
616cleanup:
617 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200618 return ret;
619}
620
621static LY_ERR
622lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
623{
624 LY_ERR ret = LY_SUCCESS;
625 struct lyxp_expr *expr = NULL;
626 uint16_t exp_idx = 0;
627 enum ly_path_pred_type pred_type = 0;
628 struct ly_path_predicate *predicates = NULL;
629
630 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200631 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 +0200632 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200633
634 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200635 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
636 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200637
638 /* create the list node */
639 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
640
641cleanup:
642 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200643 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100644 return ret;
645}
646
647LY_ERR
648lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
649{
650 struct lyd_node_any *any;
651
Michal Vasko9b368d32020-02-14 13:53:31 +0100652 assert(schema->nodetype & LYD_NODE_ANY);
653
Michal Vasko90932a92020-02-12 14:33:03 +0100654 any = calloc(1, sizeof *any);
655 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
656
657 any->schema = schema;
658 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100659 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100660
Radek Krejci1798aae2020-07-14 13:26:06 +0200661 /* TODO: convert XML/JSON strings into a opaq data tree */
662 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100663 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100664 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100665
666 *node = (struct lyd_node *)any;
667 return LY_SUCCESS;
668}
669
Michal Vasko52927e22020-03-16 17:26:14 +0100670LY_ERR
671lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200672 ly_bool *dynamic, LYD_FORMAT format, uint32_t hints, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200673 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100674{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200675 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100676 struct lyd_node_opaq *opaq;
677
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200678 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100679
680 if (!value_len) {
681 value = "";
682 }
683
684 opaq = calloc(1, sizeof *opaq);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200685 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vasko52927e22020-03-16 17:26:14 +0100686
687 opaq->prev = (struct lyd_node *)opaq;
Radek Krejcid46e46a2020-09-15 14:22:42 +0200688 opaq->val_prefs = val_prefs;
689 opaq->format = format;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200690 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish);
691
Michal Vasko52927e22020-03-16 17:26:14 +0100692 if (pref_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200693 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100694 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200695 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200696 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200697 }
698
Michal Vasko52927e22020-03-16 17:26:14 +0100699 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200700 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100701 *dynamic = 0;
702 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200703 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100704 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200705 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100706 opaq->ctx = ctx;
707
Radek Krejci011e4aa2020-09-04 15:22:31 +0200708finish:
709 if (ret) {
710 lyd_free_tree((struct lyd_node *)opaq);
711 } else {
712 *node = (struct lyd_node *)opaq;
713 }
714 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100715}
716
Michal Vasko3a41dff2020-07-15 14:30:28 +0200717API LY_ERR
Radek Krejci41ac9942020-11-02 14:47:56 +0100718lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100719{
720 struct lyd_node *ret = NULL;
721 const struct lysc_node *schema;
722 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
723
Michal Vasko6027eb92020-07-15 16:37:30 +0200724 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100725
Michal Vaskof03ed032020-03-04 13:31:44 +0100726 if (!module) {
727 module = parent->schema->module;
728 }
729
Michal Vasko3a41dff2020-07-15 14:30:28 +0200730 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100731 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200732 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 +0100733
Michal Vasko3a41dff2020-07-15 14:30:28 +0200734 LY_CHECK_RET(lyd_create_inner(schema, &ret));
735 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100736 lyd_insert_node(parent, NULL, ret);
737 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200738
739 if (node) {
740 *node = ret;
741 }
742 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100743}
744
Michal Vasko3a41dff2020-07-15 14:30:28 +0200745API LY_ERR
Radek Krejci41ac9942020-11-02 14:47:56 +0100746lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100747{
748 struct lyd_node *ret = NULL, *key;
749 const struct lysc_node *schema, *key_s;
750 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
751 va_list ap;
752 const char *key_val;
753 LY_ERR rc = LY_SUCCESS;
754
Michal Vasko6027eb92020-07-15 16:37:30 +0200755 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100756
Michal Vaskof03ed032020-03-04 13:31:44 +0100757 if (!module) {
758 module = parent->schema->module;
759 }
760
Radek Krejci41ac9942020-11-02 14:47:56 +0100761 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 +0200762 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100763
764 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200765 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100766
Michal Vasko3a41dff2020-07-15 14:30:28 +0200767 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100768
769 /* create and insert all the keys */
770 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
771 key_val = va_arg(ap, const char *);
772
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200773 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
774 NULL, &key);
775 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100776 lyd_insert_node(ret, NULL, key);
777 }
778
Michal Vasko013a8182020-03-03 10:46:53 +0100779 if (parent) {
780 lyd_insert_node(parent, NULL, ret);
781 }
782
783cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100785 if (rc) {
786 lyd_free_tree(ret);
787 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200788 } else if (node) {
789 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100790 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200791 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100792}
793
Michal Vasko3a41dff2020-07-15 14:30:28 +0200794API LY_ERR
795lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +0100796 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100797{
798 struct lyd_node *ret = NULL;
799 const struct lysc_node *schema;
800 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
801
Michal Vasko6027eb92020-07-15 16:37:30 +0200802 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100803
Michal Vaskof03ed032020-03-04 13:31:44 +0100804 if (!module) {
805 module = parent->schema->module;
806 }
Michal Vasko004d3152020-06-11 19:59:22 +0200807 if (!keys) {
808 keys = "";
809 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100810
Michal Vasko004d3152020-06-11 19:59:22 +0200811 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +0100812 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 +0200813 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100814
Michal Vasko004d3152020-06-11 19:59:22 +0200815 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
816 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200817 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200818 } else {
819 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200820 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200821 }
Michal Vasko004d3152020-06-11 19:59:22 +0200822 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100823 lyd_insert_node(parent, NULL, ret);
824 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200825
826 if (node) {
827 *node = ret;
828 }
829 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100830}
831
Michal Vasko3a41dff2020-07-15 14:30:28 +0200832API LY_ERR
833lyd_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 +0100834 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100835{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200836 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100837 struct lyd_node *ret = NULL;
838 const struct lysc_node *schema;
839 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
840
Michal Vasko6027eb92020-07-15 16:37:30 +0200841 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100842
Michal Vaskof03ed032020-03-04 13:31:44 +0100843 if (!module) {
844 module = parent->schema->module;
845 }
846
Radek Krejci41ac9942020-11-02 14:47:56 +0100847 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 +0200848 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100849
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200850 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL,
851 &ret);
852 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200853 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100854 lyd_insert_node(parent, NULL, ret);
855 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200856
857 if (node) {
858 *node = ret;
859 }
860 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100861}
862
Michal Vasko3a41dff2020-07-15 14:30:28 +0200863API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100864lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci41ac9942020-11-02 14:47:56 +0100865 LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100866{
867 struct lyd_node *ret = NULL;
868 const struct lysc_node *schema;
869 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
870
Michal Vasko6027eb92020-07-15 16:37:30 +0200871 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100872
Michal Vaskof03ed032020-03-04 13:31:44 +0100873 if (!module) {
874 module = parent->schema->module;
875 }
876
Radek Krejci41ac9942020-11-02 14:47:56 +0100877 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 +0200878 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100879
Michal Vasko3a41dff2020-07-15 14:30:28 +0200880 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
881 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100882 lyd_insert_node(parent, NULL, ret);
883 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200884
885 if (node) {
886 *node = ret;
887 }
888 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100889}
890
Michal Vasko4490d312020-06-16 13:08:55 +0200891/**
892 * @brief Update node value.
893 *
894 * @param[in] node Node to update.
895 * @param[in] value New value to set.
896 * @param[in] value_type Type of @p value for any node.
897 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
898 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
899 * @return LY_ERR value.
900 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200901static LY_ERR
902lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200903 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200904{
905 LY_ERR ret = LY_SUCCESS;
906 struct lyd_node *new_any;
907
908 switch (node->schema->nodetype) {
909 case LYS_CONTAINER:
910 case LYS_NOTIF:
911 case LYS_RPC:
912 case LYS_ACTION:
913 case LYS_LIST:
914 case LYS_LEAFLIST:
915 /* if it exists, there is nothing to update */
916 *new_parent = NULL;
917 *new_node = NULL;
918 break;
919 case LYS_LEAF:
920 ret = lyd_change_term(node, value);
921 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
922 /* there was an actual change (at least of the default flag) */
923 *new_parent = node;
924 *new_node = node;
925 ret = LY_SUCCESS;
926 } else if (ret == LY_ENOT) {
927 /* no change */
928 *new_parent = NULL;
929 *new_node = NULL;
930 ret = LY_SUCCESS;
931 } /* else error */
932 break;
933 case LYS_ANYDATA:
934 case LYS_ANYXML:
935 /* create a new any node */
936 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
937
938 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200939 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200940 /* not equal, switch values (so that we can use generic node free) */
941 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
942 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
943 ((struct lyd_node_any *)node)->value.str = value;
944 ((struct lyd_node_any *)node)->value_type = value_type;
945
946 *new_parent = node;
947 *new_node = node;
948 } else {
949 /* they are equal */
950 *new_parent = NULL;
951 *new_node = NULL;
952 }
953 lyd_free_tree(new_any);
954 break;
955 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200956 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200957 ret = LY_EINT;
958 break;
959 }
960
961 return ret;
962}
963
Michal Vasko3a41dff2020-07-15 14:30:28 +0200964API LY_ERR
965lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200966 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200967{
968 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200969 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200970 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200971 size_t pref_len, name_len;
972
Michal Vasko3a41dff2020-07-15 14:30:28 +0200973 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200974
Michal Vaskob7be7a82020-08-20 09:09:04 +0200975 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200976
977 /* parse the name */
978 tmp = name;
979 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
980 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200982 }
983
984 /* find the module */
985 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +0200986 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200987 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 +0200988 }
989
990 /* set value if none */
991 if (!val_str) {
992 val_str = "";
993 }
994
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200995 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
996 NULL, LYD_HINT_DATA, NULL));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200997
998 if (meta) {
999 *meta = ret;
1000 }
1001 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001002}
1003
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001005lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001006 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001007{
1008 struct lyd_node *ret = NULL;
1009
Michal Vasko6027eb92020-07-15 16:37:30 +02001010 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001011
1012 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001013 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001014 }
1015 if (!value) {
1016 value = "";
1017 }
1018
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001019 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, 0, NULL, NULL, 0,
1020 module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001022 lyd_insert_node(parent, NULL, ret);
1023 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001024
1025 if (node) {
1026 *node = ret;
1027 }
1028 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001029}
1030
Michal Vasko3a41dff2020-07-15 14:30:28 +02001031API LY_ERR
1032lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001033 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001034{
Radek Krejci1798aae2020-07-14 13:26:06 +02001035 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001036 const struct ly_ctx *ctx;
1037 const char *prefix, *tmp;
1038 size_t pref_len, name_len;
1039
Michal Vasko3a41dff2020-07-15 14:30:28 +02001040 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001041
Michal Vaskob7be7a82020-08-20 09:09:04 +02001042 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001043
1044 /* parse the name */
1045 tmp = name;
1046 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1047 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001049 }
1050
1051 /* set value if none */
1052 if (!val_str) {
1053 val_str = "";
1054 }
1055
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001056 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, 0, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001057 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001058
1059 if (attr) {
1060 *attr = ret;
1061 }
1062 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001063}
1064
1065API LY_ERR
1066lyd_change_term(struct lyd_node *term, const char *val_str)
1067{
1068 LY_ERR ret = LY_SUCCESS;
1069 struct lysc_type *type;
1070 struct lyd_node_term *t;
1071 struct lyd_node *parent;
1072 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001073 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001074
1075 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1076
1077 if (!val_str) {
1078 val_str = "";
1079 }
1080 t = (struct lyd_node_term *)term;
1081 type = ((struct lysc_node_leaf *)term->schema)->type;
1082
1083 /* parse the new value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001084 ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
1085 term->schema, NULL, LY_VLOG_LYD, term);
1086 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001087
1088 /* compare original and new value */
1089 if (type->plugin->compare(&t->value, &val)) {
1090 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001091 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001092 t->value = val;
1093 memset(&val, 0, sizeof val);
1094 val_change = 1;
1095 } else {
1096 val_change = 0;
1097 }
1098
1099 /* always clear the default flag */
1100 if (term->flags & LYD_DEFAULT) {
1101 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1102 parent->flags &= ~LYD_DEFAULT;
1103 }
1104 dflt_change = 1;
1105 } else {
1106 dflt_change = 0;
1107 }
1108
1109 if (val_change || dflt_change) {
1110 /* make the node non-validated */
1111 term->flags &= LYD_NEW;
1112 }
1113
1114 if (val_change) {
1115 if (term->schema->nodetype == LYS_LEAFLIST) {
1116 /* leaf-list needs to be hashed again and re-inserted into parent */
1117 lyd_unlink_hash(term);
1118 lyd_hash(term);
1119 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1120 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1121 /* list needs to be updated if its key was changed */
1122 assert(term->parent->schema->nodetype == LYS_LIST);
1123 lyd_unlink_hash((struct lyd_node *)term->parent);
1124 lyd_hash((struct lyd_node *)term->parent);
1125 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1126 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1127 }
1128
1129 /* retrun value */
1130 if (!val_change) {
1131 if (dflt_change) {
1132 /* only default flag change */
1133 ret = LY_EEXIST;
1134 } else {
1135 /* no change */
1136 ret = LY_ENOT;
1137 }
1138 } /* else value changed, LY_SUCCESS */
1139
1140cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001141 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001142 return ret;
1143}
1144
Michal Vasko41586352020-07-13 13:54:25 +02001145API LY_ERR
1146lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1147{
1148 LY_ERR ret = LY_SUCCESS;
1149 struct lyd_meta *m2;
1150 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001151 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001152
1153 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1154
1155 if (!val_str) {
1156 val_str = "";
1157 }
1158
1159 /* parse the new value into a new meta structure */
1160 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001161 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001162
1163 /* compare original and new value */
1164 if (lyd_compare_meta(meta, m2)) {
1165 /* values differ, switch them */
1166 val = meta->value;
1167 meta->value = m2->value;
1168 m2->value = val;
1169 val_change = 1;
1170 } else {
1171 val_change = 0;
1172 }
1173
1174 /* retrun value */
1175 if (!val_change) {
1176 /* no change */
1177 ret = LY_ENOT;
1178 } /* else value changed, LY_SUCCESS */
1179
1180cleanup:
1181 return ret;
1182}
1183
Michal Vasko3a41dff2020-07-15 14:30:28 +02001184API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001185lyd_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 +02001186 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001187{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001188 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001189}
1190
1191API LY_ERR
1192lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001193 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 +02001194{
1195 LY_ERR ret = LY_SUCCESS, r;
1196 struct lyxp_expr *exp = NULL;
1197 struct ly_path *p = NULL;
1198 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1199 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001200 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001201 struct ly_path_predicate *pred;
1202
1203 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1204
1205 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001206 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001207 }
1208
1209 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001210 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 +02001211 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001212
1213 /* compile path */
1214 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 +02001215 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON,
1216 NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001217
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001218 schema = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko69730152020-10-09 16:30:07 +02001219 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE) &&
Radek Krejci092e33c2020-10-12 15:33:10 +02001220 !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001221 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +02001222 lys_nodetype2str(schema->nodetype), schema->name);
Michal Vasko00cbf532020-06-15 13:58:47 +02001223 ret = LY_EINVAL;
1224 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001225 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001226 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001227 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1228 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001229
1230 if (!value) {
1231 value = "";
1232 }
1233
1234 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001235 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001236 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001237 }
1238 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001239 ret = lyd_value_store(ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value, strlen(value),
1240 NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL, LY_VLOG_LYSC, schema);
1241 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae875662020-10-21 10:33:17 +02001242 ++((struct lysc_type *)pred->value.realtype)->refcount;
Michal Vasko00cbf532020-06-15 13:58:47 +02001243 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1244 }
1245
1246 /* try to find any existing nodes in the path */
1247 if (parent) {
1248 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1249 if (ret == LY_SUCCESS) {
1250 /* the node exists, are we supposed to update it or is it just a default? */
Radek Krejci092e33c2020-10-12 15:33:10 +02001251 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001252 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1253 ret = LY_EEXIST;
1254 goto cleanup;
1255 }
1256
1257 /* update the existing node */
1258 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1259 goto cleanup;
1260 } else if (ret == LY_EINCOMPLETE) {
1261 /* some nodes were found, adjust the iterator to the next segment */
1262 ++path_idx;
1263 } else if (ret == LY_ENOTFOUND) {
1264 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001265 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001266 node = parent;
1267 }
1268 } else {
1269 /* error */
1270 goto cleanup;
1271 }
1272 }
1273
1274 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001275 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001276 cur_parent = node;
1277 schema = p[path_idx].node;
1278
1279 switch (schema->nodetype) {
1280 case LYS_LIST:
1281 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001282 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001283 /* creating opaque list without keys */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001284 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1285 LYD_NODEHINT_LIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
Michal Vasko69730152020-10-09 16:30:07 +02001286 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001287 } else {
1288 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1289 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1290 }
1291 break;
1292 }
Radek Krejci0f969882020-08-21 16:56:47 +02001293 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001294 case LYS_CONTAINER:
1295 case LYS_NOTIF:
1296 case LYS_RPC:
1297 case LYS_ACTION:
1298 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1299 break;
1300 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001301 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001302 /* creating opaque leaf-list without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001303 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1304 LYD_NODEHINT_LEAFLIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1305 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001306 } else {
1307 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1308 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1309 }
1310 break;
1311 case LYS_LEAF:
1312 /* make there is some value */
1313 if (!value) {
1314 value = "";
1315 }
1316
1317 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001318 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001319 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001320 }
1321 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001322 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1323 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001324 } else {
1325 /* creating opaque leaf without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001326 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001327 0, NULL, NULL, 0, schema->module->name,
1328 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001329 }
1330 break;
1331 case LYS_ANYDATA:
1332 case LYS_ANYXML:
1333 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1334 break;
1335 default:
1336 LOGINT(ctx);
1337 ret = LY_EINT;
1338 goto cleanup;
1339 }
1340
1341 if (cur_parent) {
1342 /* connect to the parent */
1343 lyd_insert_node(cur_parent, NULL, node);
1344 } else if (parent) {
1345 /* connect to top-level siblings */
1346 lyd_insert_node(NULL, &parent, node);
1347 }
1348
1349 /* update remembered nodes */
1350 if (!nparent) {
1351 nparent = node;
1352 }
1353 nnode = node;
1354 }
1355
1356cleanup:
1357 lyxp_expr_free(ctx, exp);
1358 ly_path_free(ctx, p);
1359 if (!ret) {
1360 /* set out params only on success */
1361 if (new_parent) {
1362 *new_parent = nparent;
1363 }
1364 if (new_node) {
1365 *new_node = nnode;
1366 }
1367 }
1368 return ret;
1369}
1370
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001371LY_ERR
1372lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001373 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 +02001374 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001375{
1376 LY_ERR ret;
1377 const struct lysc_node *iter = NULL;
1378 struct lyd_node *node;
1379 struct lyd_value **dflts;
1380 LY_ARRAY_COUNT_TYPE u;
1381
1382 assert(first && (parent || sparent || mod));
1383
1384 if (!sparent && parent) {
1385 sparent = parent->schema;
1386 }
1387
1388 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1389 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1390 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001391 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1392 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001393 }
1394
1395 switch (iter->nodetype) {
1396 case LYS_CHOICE:
1397 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1398 /* create default case data */
1399 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
Michal Vasko69730152020-10-09 16:30:07 +02001400 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001401 }
1402 break;
1403 case LYS_CONTAINER:
1404 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1405 /* create default NP container */
1406 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vasko0cadfcb2020-11-04 17:17:05 +01001407 node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001408 lyd_insert_node(parent, first, node);
1409
1410 /* cannot be a NP container with when */
1411 assert(!iter->when);
1412
Michal Vaskoe49b6322020-11-05 17:38:36 +01001413 if (diff) {
1414 /* add into diff */
1415 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1416 }
1417
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001418 /* create any default children */
1419 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
Michal Vasko69730152020-10-09 16:30:07 +02001420 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001421 }
1422 break;
1423 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001424 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1425 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001426 /* create default leaf */
1427 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1428 if (ret == LY_EINCOMPLETE) {
1429 if (node_types) {
1430 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001431 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001432 }
1433 } else if (ret) {
1434 return ret;
1435 }
Michal Vasko0cadfcb2020-11-04 17:17:05 +01001436 node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001437 lyd_insert_node(parent, first, node);
1438
1439 if (iter->when && node_when) {
1440 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001441 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001442 }
1443 if (diff) {
1444 /* add into diff */
1445 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1446 }
1447 }
1448 break;
1449 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001450 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1451 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001452 /* create all default leaf-lists */
1453 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1454 LY_ARRAY_FOR(dflts, u) {
1455 ret = lyd_create_term2(iter, dflts[u], &node);
1456 if (ret == LY_EINCOMPLETE) {
1457 if (node_types) {
1458 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001459 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001460 }
1461 } else if (ret) {
1462 return ret;
1463 }
Radek Krejcic5b54a02020-11-05 17:13:18 +01001464 node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001465 lyd_insert_node(parent, first, node);
1466
1467 if (iter->when && node_when) {
1468 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001469 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001470 }
1471 if (diff) {
1472 /* add into diff */
1473 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1474 }
1475 }
1476 }
1477 break;
1478 default:
1479 /* without defaults */
1480 break;
1481 }
1482 }
1483
1484 return LY_SUCCESS;
1485}
1486
1487API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001488lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001489{
Michal Vasko56daf732020-08-10 10:57:18 +02001490 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001491 LY_ERR ret = LY_SUCCESS;
1492
1493 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1494 if (diff) {
1495 *diff = NULL;
1496 }
1497
Michal Vasko56daf732020-08-10 10:57:18 +02001498 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001499 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001500 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1501 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001502 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001503 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001504 }
1505
Michal Vasko56daf732020-08-10 10:57:18 +02001506 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001507 }
1508
1509cleanup:
1510 if (ret && diff) {
1511 lyd_free_all(*diff);
1512 *diff = NULL;
1513 }
1514 return ret;
1515}
1516
1517API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001518lyd_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 +02001519{
1520 const struct lys_module *mod;
1521 struct lyd_node *d = NULL;
1522 uint32_t i = 0;
1523 LY_ERR ret = LY_SUCCESS;
1524
1525 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1526 if (diff) {
1527 *diff = NULL;
1528 }
1529 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001530 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001531 }
1532
1533 /* add nodes for each module one-by-one */
1534 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1535 if (!mod->implemented) {
1536 continue;
1537 }
1538
1539 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1540 if (d) {
1541 /* merge into one diff */
1542 lyd_insert_sibling(*diff, d, diff);
1543
1544 d = NULL;
1545 }
1546 }
1547
1548cleanup:
1549 if (ret && diff) {
1550 lyd_free_all(*diff);
1551 *diff = NULL;
1552 }
1553 return ret;
1554}
1555
1556API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001557lyd_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 +02001558{
1559 struct lyd_node *root, *d = NULL;
1560 LY_ERR ret = LY_SUCCESS;
1561
1562 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1563 if (diff) {
1564 *diff = NULL;
1565 }
1566
1567 /* add all top-level defaults for this module */
1568 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1569
1570 /* process nested nodes */
1571 LY_LIST_FOR(*tree, root) {
1572 /* skip added default nodes */
1573 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1574 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1575
1576 if (d) {
1577 /* merge into one diff */
1578 lyd_insert_sibling(*diff, d, diff);
1579
1580 d = NULL;
1581 }
1582 }
1583 }
1584
1585cleanup:
1586 if (ret && diff) {
1587 lyd_free_all(*diff);
1588 *diff = NULL;
1589 }
1590 return ret;
1591}
1592
Michal Vasko90932a92020-02-12 14:33:03 +01001593struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001594lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001595{
Michal Vaskob104f112020-07-17 09:54:54 +02001596 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001597 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001598 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001599
Michal Vaskob104f112020-07-17 09:54:54 +02001600 assert(new_node);
1601
1602 if (!first_sibling || !new_node->schema) {
1603 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001604 return NULL;
1605 }
1606
Michal Vaskob104f112020-07-17 09:54:54 +02001607 if (first_sibling->parent && first_sibling->parent->children_ht) {
1608 /* find the anchor using hashes */
1609 sparent = first_sibling->parent->schema;
1610 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1611 while (schema) {
1612 /* keep trying to find the first existing instance of the closest following schema sibling,
1613 * otherwise return NULL - inserting at the end */
1614 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1615 break;
1616 }
1617
1618 schema = lys_getnext(schema, sparent, NULL, 0);
1619 }
1620 } else {
1621 /* find the anchor without hashes */
1622 match = (struct lyd_node *)first_sibling;
1623 if (!lysc_data_parent(new_node->schema)) {
1624 /* we are in top-level, skip all the data from preceding modules */
1625 LY_LIST_FOR(match, match) {
1626 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1627 break;
1628 }
1629 }
1630 }
1631
1632 /* get the first schema sibling */
1633 sparent = lysc_data_parent(new_node->schema);
1634 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1635
1636 found = 0;
1637 LY_LIST_FOR(match, match) {
1638 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1639 /* we have found an opaque node, which must be at the end, so use it OR
1640 * modules do not match, so we must have traversed all the data from new_node module (if any),
1641 * we have found the first node of the next module, that is what we want */
1642 break;
1643 }
1644
1645 /* skip schema nodes until we find the instantiated one */
1646 while (!found) {
1647 if (new_node->schema == schema) {
1648 /* we have found the schema of the new node, continue search to find the first
1649 * data node with a different schema (after our schema) */
1650 found = 1;
1651 break;
1652 }
1653 if (match->schema == schema) {
1654 /* current node (match) is a data node still before the new node, continue search in data */
1655 break;
1656 }
1657 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1658 assert(schema);
1659 }
1660
1661 if (found && (match->schema != new_node->schema)) {
1662 /* find the next node after we have found our node schema data instance */
1663 break;
1664 }
1665 }
Michal Vasko90932a92020-02-12 14:33:03 +01001666 }
1667
1668 return match;
1669}
1670
1671/**
1672 * @brief Insert node after a sibling.
1673 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001674 * Handles inserting into NP containers and key-less lists.
1675 *
Michal Vasko90932a92020-02-12 14:33:03 +01001676 * @param[in] sibling Sibling to insert after.
1677 * @param[in] node Node to insert.
1678 */
1679static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001680lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001681{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001682 struct lyd_node_inner *par;
1683
Michal Vasko90932a92020-02-12 14:33:03 +01001684 assert(!node->next && (node->prev == node));
1685
1686 node->next = sibling->next;
1687 node->prev = sibling;
1688 sibling->next = node;
1689 if (node->next) {
1690 /* sibling had a succeeding node */
1691 node->next->prev = node;
1692 } else {
1693 /* sibling was last, find first sibling and change its prev */
1694 if (sibling->parent) {
1695 sibling = sibling->parent->child;
1696 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001697 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001698 }
1699 sibling->prev = node;
1700 }
1701 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001702
Michal Vasko9f96a052020-03-10 09:41:45 +01001703 for (par = node->parent; par; par = par->parent) {
1704 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1705 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001706 par->flags &= ~LYD_DEFAULT;
1707 }
Michal Vaskob104f112020-07-17 09:54:54 +02001708 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001709 /* rehash key-less list */
1710 lyd_hash((struct lyd_node *)par);
1711 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001712 }
Michal Vasko90932a92020-02-12 14:33:03 +01001713}
1714
1715/**
1716 * @brief Insert node before a sibling.
1717 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001718 * Handles inserting into NP containers and key-less lists.
1719 *
Michal Vasko90932a92020-02-12 14:33:03 +01001720 * @param[in] sibling Sibling to insert before.
1721 * @param[in] node Node to insert.
1722 */
1723static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001724lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001725{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001726 struct lyd_node_inner *par;
1727
Michal Vasko90932a92020-02-12 14:33:03 +01001728 assert(!node->next && (node->prev == node));
1729
1730 node->next = sibling;
1731 /* covers situation of sibling being first */
1732 node->prev = sibling->prev;
1733 sibling->prev = node;
1734 if (node->prev->next) {
1735 /* sibling had a preceding node */
1736 node->prev->next = node;
1737 } else if (sibling->parent) {
1738 /* sibling was first and we must also change parent child pointer */
1739 sibling->parent->child = node;
1740 }
1741 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001742
Michal Vasko9f96a052020-03-10 09:41:45 +01001743 for (par = node->parent; par; par = par->parent) {
1744 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1745 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001746 par->flags &= ~LYD_DEFAULT;
1747 }
Michal Vaskob104f112020-07-17 09:54:54 +02001748 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001749 /* rehash key-less list */
1750 lyd_hash((struct lyd_node *)par);
1751 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001752 }
Michal Vasko90932a92020-02-12 14:33:03 +01001753}
1754
1755/**
Michal Vaskob104f112020-07-17 09:54:54 +02001756 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001757 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001758 * Handles inserting into NP containers and key-less lists.
1759 *
Michal Vasko90932a92020-02-12 14:33:03 +01001760 * @param[in] parent Parent to insert into.
1761 * @param[in] node Node to insert.
1762 */
1763static void
Michal Vaskob104f112020-07-17 09:54:54 +02001764lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001765{
1766 struct lyd_node_inner *par;
1767
Radek Krejcia1c1e542020-09-29 16:06:52 +02001768 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001769 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001770
1771 par = (struct lyd_node_inner *)parent;
1772
Michal Vaskob104f112020-07-17 09:54:54 +02001773 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001774 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001775
Michal Vaskod989ba02020-08-24 10:59:24 +02001776 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001777 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1778 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001779 par->flags &= ~LYD_DEFAULT;
1780 }
Michal Vasko52927e22020-03-16 17:26:14 +01001781 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001782 /* rehash key-less list */
1783 lyd_hash((struct lyd_node *)par);
1784 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001785 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001786}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001787
Michal Vasko751cb4d2020-07-14 12:25:28 +02001788/**
1789 * @brief Learn whether a list instance has all the keys.
1790 *
1791 * @param[in] list List instance to check.
1792 * @return non-zero if all the keys were found,
1793 * @return 0 otherwise.
1794 */
1795static int
1796lyd_insert_has_keys(const struct lyd_node *list)
1797{
1798 const struct lyd_node *key;
1799 const struct lysc_node *skey = NULL;
1800
1801 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02001802 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001803 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1804 if (!key || (key->schema != skey)) {
1805 /* key missing */
1806 return 0;
1807 }
1808
1809 key = key->next;
1810 }
1811
1812 /* all keys found */
1813 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001814}
1815
1816void
Michal Vaskob104f112020-07-17 09:54:54 +02001817lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001818{
Michal Vaskob104f112020-07-17 09:54:54 +02001819 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001820
Michal Vaskob104f112020-07-17 09:54:54 +02001821 /* inserting list without its keys is not supported */
1822 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001823
Michal Vaskob104f112020-07-17 09:54:54 +02001824 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1825 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001826 }
Michal Vasko90932a92020-02-12 14:33:03 +01001827
Michal Vaskob104f112020-07-17 09:54:54 +02001828 /* get first sibling */
1829 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001830
Michal Vaskob104f112020-07-17 09:54:54 +02001831 /* find the anchor, our next node, so we can insert before it */
1832 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1833 if (anchor) {
1834 lyd_insert_before_node(anchor, node);
1835 } else if (first_sibling) {
1836 lyd_insert_after_node(first_sibling->prev, node);
1837 } else if (parent) {
1838 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001839 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001840 *first_sibling_p = node;
1841 }
1842
1843 /* insert into parent HT */
1844 lyd_insert_hash(node);
1845
1846 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001847 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001848 lyd_hash(parent);
1849
1850 /* now we can insert even the list into its parent HT */
1851 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001852 }
Michal Vasko90932a92020-02-12 14:33:03 +01001853}
1854
Michal Vaskof03ed032020-03-04 13:31:44 +01001855static LY_ERR
1856lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1857{
1858 const struct lysc_node *par2;
1859
1860 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001861 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001862
1863 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001864 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001865
1866 if (parent) {
1867 /* inner node */
1868 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001869 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02001870 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001871 return LY_EINVAL;
1872 }
1873 } else {
1874 /* top-level node */
1875 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001876 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001877 return LY_EINVAL;
1878 }
1879 }
1880
1881 return LY_SUCCESS;
1882}
1883
1884API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001885lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001886{
1887 struct lyd_node *iter;
1888
Michal Vasko654bc852020-06-23 13:28:06 +02001889 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001890
1891 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1892
1893 if (node->schema->flags & LYS_KEY) {
1894 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1895 return LY_EINVAL;
1896 }
1897
1898 if (node->parent || node->prev->next) {
1899 lyd_unlink_tree(node);
1900 }
1901
1902 while (node) {
1903 iter = node->next;
1904 lyd_unlink_tree(node);
1905 lyd_insert_node(parent, NULL, node);
1906 node = iter;
1907 }
1908 return LY_SUCCESS;
1909}
1910
1911API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001912lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001913{
1914 struct lyd_node *iter;
1915
Michal Vaskob104f112020-07-17 09:54:54 +02001916 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001917
Michal Vaskob104f112020-07-17 09:54:54 +02001918 if (sibling) {
1919 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001920 }
1921
1922 if (node->parent || node->prev->next) {
1923 lyd_unlink_tree(node);
1924 }
1925
1926 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001927 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001928 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001929 return LY_EINVAL;
1930 }
1931
Michal Vaskob1b5c262020-03-05 14:29:47 +01001932 iter = node->next;
1933 lyd_unlink_tree(node);
1934 lyd_insert_node(NULL, &sibling, node);
1935 node = iter;
1936 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001937
Michal Vaskob104f112020-07-17 09:54:54 +02001938 if (first) {
1939 /* find the first sibling */
1940 *first = sibling;
1941 while ((*first)->prev->next) {
1942 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001943 }
1944 }
1945
1946 return LY_SUCCESS;
1947}
1948
Michal Vaskob1b5c262020-03-05 14:29:47 +01001949API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001950lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1951{
1952 struct lyd_node *iter;
1953
1954 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1955
Michal Vasko62ed12d2020-05-21 10:08:25 +02001956 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001957
Michal Vaskob104f112020-07-17 09:54:54 +02001958 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001959 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001960 return LY_EINVAL;
1961 }
1962
1963 if (node->parent || node->prev->next) {
1964 lyd_unlink_tree(node);
1965 }
1966
1967 /* insert in reverse order to get the original order */
1968 node = node->prev;
1969 while (node) {
1970 iter = node->prev;
1971 lyd_unlink_tree(node);
1972
1973 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001974 lyd_insert_hash(node);
1975
Michal Vaskof03ed032020-03-04 13:31:44 +01001976 /* move the anchor accordingly */
1977 sibling = node;
1978
1979 node = (iter == node) ? NULL : iter;
1980 }
1981 return LY_SUCCESS;
1982}
1983
1984API LY_ERR
1985lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1986{
1987 struct lyd_node *iter;
1988
1989 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1990
Michal Vasko62ed12d2020-05-21 10:08:25 +02001991 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001992
Michal Vaskob104f112020-07-17 09:54:54 +02001993 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001994 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001995 return LY_EINVAL;
1996 }
1997
1998 if (node->parent || node->prev->next) {
1999 lyd_unlink_tree(node);
2000 }
2001
2002 while (node) {
2003 iter = node->next;
2004 lyd_unlink_tree(node);
2005
2006 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002007 lyd_insert_hash(node);
2008
Michal Vaskof03ed032020-03-04 13:31:44 +01002009 /* move the anchor accordingly */
2010 sibling = node;
2011
2012 node = iter;
2013 }
2014 return LY_SUCCESS;
2015}
2016
2017API void
2018lyd_unlink_tree(struct lyd_node *node)
2019{
2020 struct lyd_node *iter;
2021
2022 if (!node) {
2023 return;
2024 }
2025
Michal Vaskob104f112020-07-17 09:54:54 +02002026 /* update hashes while still linked into the tree */
2027 lyd_unlink_hash(node);
2028
Michal Vaskof03ed032020-03-04 13:31:44 +01002029 /* unlink from siblings */
2030 if (node->prev->next) {
2031 node->prev->next = node->next;
2032 }
2033 if (node->next) {
2034 node->next->prev = node->prev;
2035 } else {
2036 /* unlinking the last node */
2037 if (node->parent) {
2038 iter = node->parent->child;
2039 } else {
2040 iter = node->prev;
2041 while (iter->prev != node) {
2042 iter = iter->prev;
2043 }
2044 }
2045 /* update the "last" pointer from the first node */
2046 iter->prev = node->prev;
2047 }
2048
2049 /* unlink from parent */
2050 if (node->parent) {
2051 if (node->parent->child == node) {
2052 /* the node is the first child */
2053 node->parent->child = node->next;
2054 }
2055
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002056 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002057 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2058 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002059 LY_LIST_FOR(node->parent->child, iter) {
2060 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2061 break;
2062 }
2063 }
2064 if (!iter) {
2065 node->parent->flags |= LYD_DEFAULT;
2066 }
2067 }
2068
Michal Vaskof03ed032020-03-04 13:31:44 +01002069 /* check for keyless list and update its hash */
2070 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002071 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002072 lyd_hash(iter);
2073 }
2074 }
2075
2076 node->parent = NULL;
2077 }
2078
2079 node->next = NULL;
2080 node->prev = node;
2081}
2082
Michal Vaskoa5da3292020-08-12 13:10:50 +02002083void
Radek Krejci1798aae2020-07-14 13:26:06 +02002084lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2085{
2086 struct lyd_meta *last, *iter;
2087
2088 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002089
2090 if (!meta) {
2091 return;
2092 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002093
2094 for (iter = meta; iter; iter = iter->next) {
2095 iter->parent = parent;
2096 }
2097
2098 /* insert as the last attribute */
2099 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002100 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002101 last->next = meta;
2102 } else {
2103 parent->meta = meta;
2104 }
2105
2106 /* remove default flags from NP containers */
2107 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2108 parent->flags &= ~LYD_DEFAULT;
2109 parent = (struct lyd_node *)parent->parent;
2110 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002111}
2112
2113LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002114lyd_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 +02002115 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
2116 void *prefix_data, uint32_t hints, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002117{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002118 LY_ERR rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002119 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002120 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002121 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002122
Michal Vasko9f96a052020-03-10 09:41:45 +01002123 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002124
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002125 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002126 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002127 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002128 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002129 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002130 break;
2131 }
2132 }
2133 if (!ant) {
2134 /* attribute is not defined as a metadata annotation (RFC 7952) */
2135 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2136 mod->name, name_len, name);
2137 return LY_EINVAL;
2138 }
2139
Michal Vasko9f96a052020-03-10 09:41:45 +01002140 mt = calloc(1, sizeof *mt);
2141 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2142 mt->parent = parent;
2143 mt->annotation = ant;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002144 rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic,
2145 format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL);
2146 LY_CHECK_ERR_RET(rc, free(mt), rc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002147 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2148 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002149
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002150 /* insert as the last attribute */
2151 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002152 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002153 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002154 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002155 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002156 }
2157
Michal Vasko9f96a052020-03-10 09:41:45 +01002158 if (meta) {
2159 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002160 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002161 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002162}
2163
Michal Vaskoa5da3292020-08-12 13:10:50 +02002164void
2165lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2166{
2167 struct lyd_attr *last, *iter;
2168 struct lyd_node_opaq *opaq;
2169
2170 assert(parent && !parent->schema);
2171
2172 if (!attr) {
2173 return;
2174 }
2175
2176 opaq = (struct lyd_node_opaq *)parent;
2177 for (iter = attr; iter; iter = iter->next) {
2178 iter->parent = opaq;
2179 }
2180
2181 /* insert as the last attribute */
2182 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002183 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002184 last->next = attr;
2185 } else {
2186 opaq->attr = attr;
2187 }
2188}
2189
Michal Vasko52927e22020-03-16 17:26:14 +01002190LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002191lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len,
2192 const char *value, size_t value_len, ly_bool *dynamic, LYD_FORMAT format, uint32_t hints,
Radek Krejci0f969882020-08-21 16:56:47 +02002193 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len)
Michal Vasko52927e22020-03-16 17:26:14 +01002194{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002195 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002196 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002197
2198 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002199 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002200
2201 if (!value_len) {
2202 value = "";
2203 }
2204
2205 at = calloc(1, sizeof *at);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002206 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002207 at->hints = hints;
Radek Krejcid46e46a2020-09-15 14:22:42 +02002208 at->format = format;
2209 at->val_prefs = val_prefs;
2210
Radek Krejci011e4aa2020-09-04 15:22:31 +02002211 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002212 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002213 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2214 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002215 *dynamic = 0;
2216 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002217 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002218 }
2219
Radek Krejci1798aae2020-07-14 13:26:06 +02002220 if (prefix_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002221 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +02002222 }
2223 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002224 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002225 }
2226
2227 /* insert as the last attribute */
2228 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002229 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002230 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002231 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002232 last->next = at;
2233 }
2234
Radek Krejci011e4aa2020-09-04 15:22:31 +02002235finish:
2236 if (ret) {
2237 lyd_free_attr_single(ctx, at);
2238 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002239 *attr = at;
2240 }
2241 return LY_SUCCESS;
2242}
2243
Radek Krejci084289f2019-07-09 17:35:30 +02002244API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002245lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002246{
Michal Vasko004d3152020-06-11 19:59:22 +02002247 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002248
Michal Vasko004d3152020-06-11 19:59:22 +02002249 if (ly_path_eval(path, tree, &target)) {
2250 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002251 }
2252
Michal Vasko004d3152020-06-11 19:59:22 +02002253 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002254}
2255
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002256API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002257lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002258{
2259 const struct lyd_node *iter1, *iter2;
2260 struct lyd_node_term *term1, *term2;
2261 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002262 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002263 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002264
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002265 if (!node1 || !node2) {
2266 if (node1 == node2) {
2267 return LY_SUCCESS;
2268 } else {
2269 return LY_ENOT;
2270 }
2271 }
2272
Michal Vaskob7be7a82020-08-20 09:09:04 +02002273 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002274 return LY_ENOT;
2275 }
2276
2277 if (node1->hash != node2->hash) {
2278 return LY_ENOT;
2279 }
Michal Vasko52927e22020-03-16 17:26:14 +01002280 /* 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 +02002281
Michal Vasko52927e22020-03-16 17:26:14 +01002282 if (!node1->schema) {
2283 opaq1 = (struct lyd_node_opaq *)node1;
2284 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002285 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002286 return LY_ENOT;
2287 }
Michal Vasko52927e22020-03-16 17:26:14 +01002288 switch (opaq1->format) {
2289 case LYD_XML:
2290 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2291 return LY_ENOT;
2292 }
2293 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002294 case LYD_JSON:
2295 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2296 if (strcmp(opaq1->value, opaq2->value)) {
2297 return LY_ENOT;
2298 }
2299 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002300 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002301 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002302 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002303 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002304 return LY_EINT;
2305 }
2306 if (options & LYD_COMPARE_FULL_RECURSION) {
2307 iter1 = opaq1->child;
2308 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002309 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002310 }
2311 return LY_SUCCESS;
2312 } else {
2313 switch (node1->schema->nodetype) {
2314 case LYS_LEAF:
2315 case LYS_LEAFLIST:
2316 if (options & LYD_COMPARE_DEFAULTS) {
2317 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2318 return LY_ENOT;
2319 }
2320 }
2321
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002322 term1 = (struct lyd_node_term *)node1;
2323 term2 = (struct lyd_node_term *)node2;
2324 if (term1->value.realtype != term2->value.realtype) {
2325 return LY_ENOT;
2326 }
Michal Vasko52927e22020-03-16 17:26:14 +01002327
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002328 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002329 case LYS_CONTAINER:
2330 if (options & LYD_COMPARE_DEFAULTS) {
2331 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2332 return LY_ENOT;
2333 }
2334 }
2335 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002336 iter1 = ((struct lyd_node_inner *)node1)->child;
2337 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002338 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002339 }
2340 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002341 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002342 case LYS_ACTION:
2343 if (options & LYD_COMPARE_FULL_RECURSION) {
2344 /* TODO action/RPC
2345 goto all_children_compare;
2346 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002347 }
2348 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002349 case LYS_NOTIF:
2350 if (options & LYD_COMPARE_FULL_RECURSION) {
2351 /* TODO Notification
2352 goto all_children_compare;
2353 */
2354 }
2355 return LY_SUCCESS;
2356 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002357 iter1 = ((struct lyd_node_inner *)node1)->child;
2358 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002359
2360 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2361 /* lists with keys, their equivalence is based on their keys */
Michal Vasko22df3f02020-08-24 13:29:22 +02002362 for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002363 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002364 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002365 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002366 return LY_ENOT;
2367 }
2368 iter1 = iter1->next;
2369 iter2 = iter2->next;
2370 }
2371 } else {
2372 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2373
Radek Krejci0f969882020-08-21 16:56:47 +02002374all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002375 if (!iter1 && !iter2) {
2376 /* no children, nothing to compare */
2377 return LY_SUCCESS;
2378 }
2379
Michal Vaskod989ba02020-08-24 10:59:24 +02002380 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002381 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002382 return LY_ENOT;
2383 }
2384 }
2385 if (iter1 || iter2) {
2386 return LY_ENOT;
2387 }
2388 }
2389 return LY_SUCCESS;
2390 case LYS_ANYXML:
2391 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002392 any1 = (struct lyd_node_any *)node1;
2393 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002394
2395 if (any1->value_type != any2->value_type) {
2396 return LY_ENOT;
2397 }
2398 switch (any1->value_type) {
2399 case LYD_ANYDATA_DATATREE:
2400 iter1 = any1->value.tree;
2401 iter2 = any2->value.tree;
2402 goto all_children_compare;
2403 case LYD_ANYDATA_STRING:
2404 case LYD_ANYDATA_XML:
2405 case LYD_ANYDATA_JSON:
2406 len1 = strlen(any1->value.str);
2407 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002408 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002409 return LY_ENOT;
2410 }
2411 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002412 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002413 len1 = lyd_lyb_data_length(any1->value.mem);
2414 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002415 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002416 return LY_ENOT;
2417 }
2418 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002419 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002420 }
2421 }
2422
Michal Vaskob7be7a82020-08-20 09:09:04 +02002423 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002424 return LY_EINT;
2425}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002426
Michal Vasko21725742020-06-29 11:49:25 +02002427API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002428lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002429{
Michal Vaskod989ba02020-08-24 10:59:24 +02002430 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002431 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2432 }
2433
Michal Vasko11a81432020-07-28 16:31:29 +02002434 if (node1 == node2) {
2435 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002436 }
Michal Vasko11a81432020-07-28 16:31:29 +02002437 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002438}
2439
2440API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002441lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2442{
2443 if (!meta1 || !meta2) {
2444 if (meta1 == meta2) {
2445 return LY_SUCCESS;
2446 } else {
2447 return LY_ENOT;
2448 }
2449 }
2450
Michal Vaskob7be7a82020-08-20 09:09:04 +02002451 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002452 return LY_ENOT;
2453 }
2454
2455 if (meta1->value.realtype != meta2->value.realtype) {
2456 return LY_ENOT;
2457 }
2458
2459 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2460}
2461
Radek Krejci22ebdba2019-07-25 13:59:43 +02002462/**
Michal Vasko52927e22020-03-16 17:26:14 +01002463 * @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 +02002464 *
2465 * 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 +02002466 *
2467 * @param[in] node Original node to duplicate
2468 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2469 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2470 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2471 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2472 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002473 */
Michal Vasko52927e22020-03-16 17:26:14 +01002474static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002475lyd_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 +02002476 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002477{
Michal Vasko52927e22020-03-16 17:26:14 +01002478 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002479 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002480 struct lyd_meta *meta;
2481 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002482 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002483
Michal Vasko52927e22020-03-16 17:26:14 +01002484 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002485
Michal Vasko52927e22020-03-16 17:26:14 +01002486 if (!node->schema) {
2487 dup = calloc(1, sizeof(struct lyd_node_opaq));
2488 } else {
2489 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002490 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002491 case LYS_ACTION:
2492 case LYS_NOTIF:
2493 case LYS_CONTAINER:
2494 case LYS_LIST:
2495 dup = calloc(1, sizeof(struct lyd_node_inner));
2496 break;
2497 case LYS_LEAF:
2498 case LYS_LEAFLIST:
2499 dup = calloc(1, sizeof(struct lyd_node_term));
2500 break;
2501 case LYS_ANYDATA:
2502 case LYS_ANYXML:
2503 dup = calloc(1, sizeof(struct lyd_node_any));
2504 break;
2505 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002506 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002507 ret = LY_EINT;
2508 goto error;
2509 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002510 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002511 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002512
Michal Vaskof6df0a02020-06-16 13:08:34 +02002513 if (options & LYD_DUP_WITH_FLAGS) {
2514 dup->flags = node->flags;
2515 } else {
2516 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2517 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002518 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002519 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002520
Michal Vasko25a32822020-07-09 15:48:22 +02002521 /* duplicate metadata */
2522 if (!(options & LYD_DUP_NO_META)) {
2523 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002524 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002525 }
2526 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002527
2528 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002529 if (!dup->schema) {
2530 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2531 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2532 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002533
2534 if (options & LYD_DUP_RECURSIVE) {
2535 /* duplicate all the children */
2536 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002537 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002538 }
2539 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002540 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002541 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002542 if (orig->prefix.id) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002543 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002544 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002545 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0, &opaq->prefix.module_ns), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002546 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002547 LY_ARRAY_CREATE_GOTO(LYD_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002548 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002549 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
2550 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0, &opaq->val_prefs[u].module_ns), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002551 LY_ARRAY_INCREMENT(opaq->val_prefs);
2552 }
2553 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002554 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002555 opaq->ctx = orig->ctx;
2556 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2557 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2558 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2559
2560 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002561 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002562 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002563 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2564 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2565 struct lyd_node *child;
2566
2567 if (options & LYD_DUP_RECURSIVE) {
2568 /* duplicate all the children */
2569 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002570 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002571 }
Michal Vasko69730152020-10-09 16:30:07 +02002572 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002573 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002574 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002575 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002576 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002577 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002578 if (!child) {
2579 /* possibly not keys are present in filtered tree */
2580 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002581 } else if (child->schema != key) {
2582 /* possibly not all keys are present in filtered tree,
2583 * but there can be also some non-key nodes */
2584 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002585 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002586 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002587 child = child->next;
2588 }
2589 }
2590 lyd_hash(dup);
2591 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002592 dup->hash = node->hash;
2593 any = (struct lyd_node_any *)node;
2594 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002595 }
2596
Michal Vasko52927e22020-03-16 17:26:14 +01002597 /* insert */
2598 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002599
2600 if (dup_p) {
2601 *dup_p = dup;
2602 }
2603 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002604
2605error:
Michal Vasko52927e22020-03-16 17:26:14 +01002606 lyd_free_tree(dup);
2607 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002608}
2609
Michal Vasko3a41dff2020-07-15 14:30:28 +02002610static LY_ERR
2611lyd_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 +02002612 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002613{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002614 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002615 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002616
2617 *dup_parent = NULL;
2618 *local_parent = NULL;
2619
2620 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2621 if (parent && (parent->schema == orig_parent->schema)) {
2622 /* stop creating parents, connect what we have into the provided parent */
2623 iter = parent;
2624 repeat = 0;
2625 } else {
2626 iter = NULL;
2627 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002628 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002629 }
2630 if (!*local_parent) {
2631 *local_parent = (struct lyd_node_inner *)iter;
2632 }
2633 if (iter->child) {
2634 /* 1) list - add after keys
2635 * 2) provided parent with some children */
2636 iter->child->prev->next = *dup_parent;
2637 if (*dup_parent) {
2638 (*dup_parent)->prev = iter->child->prev;
2639 iter->child->prev = *dup_parent;
2640 }
2641 } else {
2642 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2643 }
2644 if (*dup_parent) {
2645 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2646 }
2647 *dup_parent = (struct lyd_node *)iter;
2648 }
2649
2650 if (repeat && parent) {
2651 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002652 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002653 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002654 return LY_EINVAL;
2655 }
2656
2657 return LY_SUCCESS;
2658}
2659
2660static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002661lyd_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 +02002662{
2663 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002664 const struct lyd_node *orig; /* original node to be duplicated */
2665 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002666 struct lyd_node *top = NULL; /* the most higher created node */
2667 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002668
Michal Vasko3a41dff2020-07-15 14:30:28 +02002669 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002670
2671 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002672 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002673 } else {
2674 local_parent = parent;
2675 }
2676
Radek Krejci22ebdba2019-07-25 13:59:43 +02002677 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002678 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002679 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2680 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002681 break;
2682 }
2683 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002684
2685 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02002686 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02002687 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002688 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002689 }
2690 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002691
2692 if (dup) {
2693 *dup = first;
2694 }
2695 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002696
2697error:
2698 if (top) {
2699 lyd_free_tree(top);
2700 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002701 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002702 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002703 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002704}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002705
Michal Vasko3a41dff2020-07-15 14:30:28 +02002706API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002707lyd_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 +02002708{
2709 return lyd_dup(node, parent, options, 1, dup);
2710}
2711
2712API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002713lyd_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 +02002714{
2715 return lyd_dup(node, parent, options, 0, dup);
2716}
2717
2718API LY_ERR
2719lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002720{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002721 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002722 struct lyd_meta *mt, *last;
2723
Michal Vasko3a41dff2020-07-15 14:30:28 +02002724 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002725
2726 /* create a copy */
2727 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002728 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002729 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002730 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002731 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
2732 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002733
2734 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002735 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002736 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002737 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002738 last->next = mt;
2739 } else {
2740 node->meta = mt;
2741 }
2742
Radek Krejci011e4aa2020-09-04 15:22:31 +02002743finish:
2744 if (ret) {
2745 lyd_free_meta_single(mt);
2746 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002747 *dup = mt;
2748 }
2749 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002750}
2751
Michal Vasko4490d312020-06-16 13:08:55 +02002752/**
2753 * @brief Merge a source sibling into target siblings.
2754 *
2755 * @param[in,out] first_trg First target sibling, is updated if top-level.
2756 * @param[in] parent_trg Target parent.
2757 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2758 * @param[in] options Merge options.
2759 * @return LY_ERR value.
2760 */
2761static LY_ERR
2762lyd_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 +02002763 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02002764{
2765 LY_ERR ret;
2766 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002767 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002768 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002769
2770 sibling_src = *sibling_src_p;
2771 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2772 /* try to find the exact instance */
2773 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2774 } else {
2775 /* try to simply find the node, there cannot be more instances */
2776 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2777 }
2778
2779 if (!ret) {
2780 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002781 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002782 /* since they are different, they cannot both be default */
2783 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2784
Michal Vasko3a41dff2020-07-15 14:30:28 +02002785 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2786 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002787 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002788 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2789 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002790 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002791
2792 /* copy flags and add LYD_NEW */
2793 match_trg->flags = sibling_src->flags | LYD_NEW;
2794 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002795 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002796 /* update value */
2797 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002798 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002799
2800 /* copy flags and add LYD_NEW */
2801 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002802 } else {
2803 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02002804 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002805 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2806 }
2807 }
2808 } else {
2809 /* node not found, merge it */
2810 if (options & LYD_MERGE_DESTRUCT) {
2811 dup_src = (struct lyd_node *)sibling_src;
2812 lyd_unlink_tree(dup_src);
2813 /* spend it */
2814 *sibling_src_p = NULL;
2815 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002816 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 +02002817 }
2818
2819 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002820 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002821 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002822 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002823 }
2824
2825 lyd_insert_node(parent_trg, first_trg, dup_src);
2826 }
2827
2828 return LY_SUCCESS;
2829}
2830
Michal Vasko3a41dff2020-07-15 14:30:28 +02002831static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002832lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002833{
2834 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02002835 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02002836
2837 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2838
2839 if (!source) {
2840 /* nothing to merge */
2841 return LY_SUCCESS;
2842 }
2843
2844 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002845 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002846 return LY_EINVAL;
2847 }
2848
2849 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02002850 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002851 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2852 if (first && !sibling_src) {
2853 /* source was spent (unlinked), move to the next node */
2854 source = tmp;
2855 }
2856
Michal Vasko3a41dff2020-07-15 14:30:28 +02002857 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002858 break;
2859 }
2860 }
2861
2862 if (options & LYD_MERGE_DESTRUCT) {
2863 /* free any leftover source data that were not merged */
2864 lyd_free_siblings((struct lyd_node *)source);
2865 }
2866
2867 return LY_SUCCESS;
2868}
2869
Michal Vasko3a41dff2020-07-15 14:30:28 +02002870API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002871lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002872{
2873 return lyd_merge(target, source, options, 1);
2874}
2875
2876API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002877lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002878{
2879 return lyd_merge(target, source, options, 0);
2880}
2881
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002882static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002883lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002884{
Michal Vasko14654712020-02-06 08:35:21 +01002885 /* ending \0 */
2886 ++reqlen;
2887
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002888 if (reqlen > *buflen) {
2889 if (is_static) {
2890 return LY_EINCOMPLETE;
2891 }
2892
2893 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2894 if (!*buffer) {
2895 return LY_EMEM;
2896 }
2897
2898 *buflen = reqlen;
2899 }
2900
2901 return LY_SUCCESS;
2902}
2903
Michal Vaskod59035b2020-07-08 12:00:06 +02002904LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002905lyd_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 +02002906{
2907 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002908 size_t len;
2909 const char *val;
2910 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002911
Radek Krejcia1c1e542020-09-29 16:06:52 +02002912 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002913 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002914 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002915 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002916
2917 quot = '\'';
2918 if (strchr(val, '\'')) {
2919 quot = '"';
2920 }
2921 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002922 }
2923
2924 return LY_SUCCESS;
2925}
2926
2927/**
2928 * @brief Append leaf-list value predicate to path.
2929 *
2930 * @param[in] node Node to print.
2931 * @param[in,out] buffer Buffer to print to.
2932 * @param[in,out] buflen Current buffer length.
2933 * @param[in,out] bufused Current number of characters used in @p buffer.
2934 * @param[in] is_static Whether buffer is static or can be reallocated.
2935 * @return LY_ERR
2936 */
2937static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002938lyd_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 +02002939{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002940 size_t len;
2941 const char *val;
2942 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002943
Michal Vaskob7be7a82020-08-20 09:09:04 +02002944 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002945 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002946 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002947
2948 quot = '\'';
2949 if (strchr(val, '\'')) {
2950 quot = '"';
2951 }
2952 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2953
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002954 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002955}
2956
2957/**
2958 * @brief Append node position (relative to its other instances) predicate to path.
2959 *
2960 * @param[in] node Node to print.
2961 * @param[in,out] buffer Buffer to print to.
2962 * @param[in,out] buflen Current buffer length.
2963 * @param[in,out] bufused Current number of characters used in @p buffer.
2964 * @param[in] is_static Whether buffer is static or can be reallocated.
2965 * @return LY_ERR
2966 */
2967static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002968lyd_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 +02002969{
2970 const struct lyd_node *first, *iter;
2971 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002972 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002973 char *val = NULL;
2974 LY_ERR rc;
2975
2976 if (node->parent) {
2977 first = node->parent->child;
2978 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02002979 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002980 }
2981 pos = 1;
2982 for (iter = first; iter != node; iter = iter->next) {
2983 if (iter->schema == node->schema) {
2984 ++pos;
2985 }
2986 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002987 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002988 return LY_EMEM;
2989 }
2990
2991 len = 1 + strlen(val) + 1;
2992 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2993 if (rc != LY_SUCCESS) {
2994 goto cleanup;
2995 }
2996
2997 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2998
2999cleanup:
3000 free(val);
3001 return rc;
3002}
3003
3004API char *
3005lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3006{
Radek Krejci857189e2020-09-01 13:26:36 +02003007 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003008 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003009 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003010 const struct lyd_node *iter;
3011 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003012 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003013
3014 LY_CHECK_ARG_RET(NULL, node, NULL);
3015 if (buffer) {
3016 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3017 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003018 } else {
3019 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003020 }
3021
3022 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003023 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003024 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003025 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003026 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3027 ++depth;
3028 }
3029
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003030 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003031 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003032 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003033 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003034iter_print:
3035 /* print prefix and name */
3036 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003037 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003038 mod = iter->schema->module;
3039 }
3040
3041 /* realloc string */
Michal Vasko22df3f02020-08-24 13:29:22 +02003042 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : strlen(((struct lyd_node_opaq *)iter)->name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003043 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3044 if (rc != LY_SUCCESS) {
3045 break;
3046 }
3047
3048 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003049 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vasko69730152020-10-09 16:30:07 +02003050 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003051
Michal Vasko790b2bc2020-08-03 13:35:06 +02003052 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003053 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003054 switch (iter->schema->nodetype) {
3055 case LYS_LIST:
3056 if (iter->schema->flags & LYS_KEYLESS) {
3057 /* print its position */
3058 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3059 } else {
3060 /* print all list keys in predicates */
3061 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3062 }
3063 break;
3064 case LYS_LEAFLIST:
3065 if (iter->schema->flags & LYS_CONFIG_W) {
3066 /* print leaf-list value */
3067 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3068 } else {
3069 /* print its position */
3070 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3071 }
3072 break;
3073 default:
3074 /* nothing to print more */
3075 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003076 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003077 }
3078 if (rc != LY_SUCCESS) {
3079 break;
3080 }
3081
Michal Vasko14654712020-02-06 08:35:21 +01003082 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003083 }
3084 break;
3085 }
3086
3087 return buffer;
3088}
Michal Vaskoe444f752020-02-10 12:20:06 +01003089
Michal Vasko25a32822020-07-09 15:48:22 +02003090API struct lyd_meta *
3091lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3092{
3093 struct lyd_meta *ret = NULL;
3094 const struct ly_ctx *ctx;
3095 const char *prefix, *tmp;
3096 char *str;
3097 size_t pref_len, name_len;
3098
3099 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3100
3101 if (!first) {
3102 return NULL;
3103 }
3104
3105 ctx = first->annotation->module->ctx;
3106
3107 /* parse the name */
3108 tmp = name;
3109 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3110 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3111 return NULL;
3112 }
3113
3114 /* find the module */
3115 if (prefix) {
3116 str = strndup(prefix, pref_len);
3117 module = ly_ctx_get_module_latest(ctx, str);
3118 free(str);
3119 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3120 }
3121
3122 /* find the metadata */
3123 LY_LIST_FOR(first, first) {
3124 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3125 ret = (struct lyd_meta *)first;
3126 break;
3127 }
3128 }
3129
3130 return ret;
3131}
3132
Michal Vasko9b368d32020-02-14 13:53:31 +01003133API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003134lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3135{
3136 struct lyd_node **match_p;
3137 struct lyd_node_inner *parent;
3138
Michal Vaskof03ed032020-03-04 13:31:44 +01003139 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003140
Michal Vasko62ed12d2020-05-21 10:08:25 +02003141 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3142 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003143 if (match) {
3144 *match = NULL;
3145 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003146 return LY_ENOTFOUND;
3147 }
3148
3149 /* find first sibling */
3150 if (siblings->parent) {
3151 siblings = siblings->parent->child;
3152 } else {
3153 while (siblings->prev->next) {
3154 siblings = siblings->prev;
3155 }
3156 }
3157
3158 parent = (struct lyd_node_inner *)siblings->parent;
3159 if (parent && parent->children_ht) {
3160 assert(target->hash);
3161
3162 /* find by hash */
3163 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003164 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003165 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003166 siblings = *match_p;
3167 } else {
3168 siblings = NULL;
3169 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003170 } else {
3171 /* not found */
3172 siblings = NULL;
3173 }
3174 } else {
3175 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003176 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003177 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003178 break;
3179 }
3180 }
3181 }
3182
3183 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003184 if (match) {
3185 *match = NULL;
3186 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003187 return LY_ENOTFOUND;
3188 }
3189
Michal Vasko9b368d32020-02-14 13:53:31 +01003190 if (match) {
3191 *match = (struct lyd_node *)siblings;
3192 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003193 return LY_SUCCESS;
3194}
3195
Radek Krejci857189e2020-09-01 13:26:36 +02003196/**
3197 * @brief Comparison callback to match schema node with a schema of a data node.
3198 *
3199 * @param[in] val1_p Pointer to the schema node
3200 * @param[in] val2_p Pointer to the data node
3201 * Implementation of ::values_equal_cb.
3202 */
3203static ly_bool
3204lyd_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 +01003205{
3206 struct lysc_node *val1;
3207 struct lyd_node *val2;
3208
3209 val1 = *((struct lysc_node **)val1_p);
3210 val2 = *((struct lyd_node **)val2_p);
3211
Michal Vasko90932a92020-02-12 14:33:03 +01003212 if (val1 == val2->schema) {
3213 /* schema match is enough */
3214 return 1;
3215 } else {
3216 return 0;
3217 }
3218}
3219
3220static LY_ERR
3221lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3222{
3223 struct lyd_node **match_p;
3224 struct lyd_node_inner *parent;
3225 uint32_t hash;
3226 values_equal_cb ht_cb;
3227
Michal Vaskob104f112020-07-17 09:54:54 +02003228 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003229
3230 parent = (struct lyd_node_inner *)siblings->parent;
3231 if (parent && parent->children_ht) {
3232 /* calculate our hash */
3233 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3234 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3235 hash = dict_hash_multi(hash, NULL, 0);
3236
3237 /* use special hash table function */
3238 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3239
3240 /* find by hash */
3241 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3242 siblings = *match_p;
3243 } else {
3244 /* not found */
3245 siblings = NULL;
3246 }
3247
3248 /* set the original hash table compare function back */
3249 lyht_set_cb(parent->children_ht, ht_cb);
3250 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003251 /* find first sibling */
3252 if (siblings->parent) {
3253 siblings = siblings->parent->child;
3254 } else {
3255 while (siblings->prev->next) {
3256 siblings = siblings->prev;
3257 }
3258 }
3259
3260 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003261 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003262 if (siblings->schema == schema) {
3263 /* schema match is enough */
3264 break;
3265 }
3266 }
3267 }
3268
3269 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003270 if (match) {
3271 *match = NULL;
3272 }
Michal Vasko90932a92020-02-12 14:33:03 +01003273 return LY_ENOTFOUND;
3274 }
3275
Michal Vasko9b368d32020-02-14 13:53:31 +01003276 if (match) {
3277 *match = (struct lyd_node *)siblings;
3278 }
Michal Vasko90932a92020-02-12 14:33:03 +01003279 return LY_SUCCESS;
3280}
3281
Michal Vaskoe444f752020-02-10 12:20:06 +01003282API LY_ERR
3283lyd_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 +02003284 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003285{
3286 LY_ERR rc;
3287 struct lyd_node *target = NULL;
3288
Michal Vasko4c583e82020-07-17 12:16:14 +02003289 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003290
Michal Vasko62ed12d2020-05-21 10:08:25 +02003291 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3292 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003293 if (match) {
3294 *match = NULL;
3295 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003296 return LY_ENOTFOUND;
3297 }
3298
Michal Vaskof03ed032020-03-04 13:31:44 +01003299 if (key_or_value && !val_len) {
3300 val_len = strlen(key_or_value);
3301 }
3302
Michal Vaskob104f112020-07-17 09:54:54 +02003303 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3304 /* create a data node and find the instance */
3305 if (schema->nodetype == LYS_LEAFLIST) {
3306 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003307 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3308 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003309 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003310 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003311 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003312 }
3313
3314 /* find it */
3315 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003316 } else {
3317 /* find the first schema node instance */
3318 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003319 }
3320
Michal Vaskoe444f752020-02-10 12:20:06 +01003321 lyd_free_tree(target);
3322 return rc;
3323}
Michal Vaskoccc02342020-05-21 10:09:21 +02003324
3325API LY_ERR
3326lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3327{
3328 LY_ERR ret = LY_SUCCESS;
3329 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003330 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003331 uint32_t i;
3332
3333 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3334
3335 memset(&xp_set, 0, sizeof xp_set);
3336
3337 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003338 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3339 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003340
3341 /* evaluate expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003342 ret = lyxp_eval(exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003343 LY_CHECK_GOTO(ret, cleanup);
3344
3345 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003346 ret = ly_set_new(set);
3347 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003348
3349 /* transform into ly_set */
3350 if (xp_set.type == LYXP_SET_NODE_SET) {
3351 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3352 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003353 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003354 (*set)->size = xp_set.used;
3355
3356 for (i = 0; i < xp_set.used; ++i) {
3357 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003358 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003359 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003360 }
3361 }
3362 }
3363
3364cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003365 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003366 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003367 return ret;
3368}
Radek Krejcica989142020-11-05 11:32:22 +01003369
3370API uint32_t
3371lyd_list_pos(const struct lyd_node *instance)
3372{
3373 const struct lyd_node *iter = NULL;
3374 uint32_t pos = 0;
3375
3376 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3377 return 0;
3378 }
3379
3380 /* data instances are ordered, so we can stop when we found instance of other schema node */
3381 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
3382 if (pos && iter->next == NULL) {
3383 /* overrun to the end of the siblings list */
3384 break;
3385 }
3386 ++pos;
3387 }
3388
3389 return pos;
3390}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003391
3392API struct lyd_node *
3393lyd_first_sibling(const struct lyd_node *node)
3394{
3395 struct lyd_node *start;
3396
3397 if (!node) {
3398 return NULL;
3399 }
3400
3401 /* get the first sibling */
3402 if (node->parent) {
3403 start = node->parent->child;
3404 } else {
3405 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev);
3406 }
3407
3408 return start;
3409}