blob: 7f2efa2b129bfb30d8666c4d9944f5b1aff1f0de [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"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020041#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020049#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "xml.h"
51#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020052
Michal Vaskob104f112020-07-17 09:54:54 +020053static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020054 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020055
Radek Krejci084289f2019-07-09 17:35:30 +020056LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +020057lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, int value_hint,
Radek Krejci0f969882020-08-21 16:56:47 +020058 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020059{
Michal Vasko90932a92020-02-12 14:33:03 +010060 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020061 struct ly_err_item *err = NULL;
62 struct ly_ctx *ctx;
63 struct lysc_type *type;
Michal Vaskoba99a3e2020-08-18 15:50:05 +020064 int options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010065 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020066 assert(node);
67
68 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020069
Radek Krejci73dead22019-07-11 16:46:16 +020070 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020071 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +020072 tree ? (void *)node : (void *)node->schema, tree, &node->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010073 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020074 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020075 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020076 if (!node->parent && lysc_data_parent(node->schema)) {
77 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
78 } else {
79 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
80 }
Radek Krejci73dead22019-07-11 16:46:16 +020081 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020082 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010084 } else if (dynamic) {
85 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020086 }
87
88error:
89 return ret;
90}
91
Michal Vasko00cbf532020-06-15 13:58:47 +020092/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020093LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010094lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
Radek Krejci0f969882020-08-21 16:56:47 +020095 LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko90932a92020-02-12 14:33:03 +010096{
97 LY_ERR ret = LY_SUCCESS;
98 struct ly_err_item *err = NULL;
99 struct ly_ctx *ctx;
100 struct lysc_type *type;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200101 int options = LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100102
103 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
104
105 ctx = schema->module->ctx;
106 type = ((struct lysc_node_leaf *)schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200107 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, (void *)schema, NULL,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200108 val, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100109 if (ret == LY_EINCOMPLETE) {
110 /* this is fine, we do not need it resolved */
111 ret = LY_SUCCESS;
112 } else if (ret && err) {
113 ly_err_print(err);
114 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
115 ly_err_free(err);
116 }
117 if (!ret && dynamic) {
118 *dynamic = 0;
119 }
120
121 return ret;
122}
123
Radek Krejci38d85362019-09-05 16:26:38 +0200124LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200125lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Radek Krejci0f969882020-08-21 16:56:47 +0200126 int second, int value_hint, LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_snode,
127 const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200128{
Michal Vasko90932a92020-02-12 14:33:03 +0100129 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200130 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 struct lyext_metadata *ant;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200132 int options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100133 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200134
Michal Vasko9f96a052020-03-10 09:41:45 +0100135 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100136
Michal Vasko9f96a052020-03-10 09:41:45 +0100137 ant = meta->annotation->data;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200138 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200139 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100140 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200141 if (err) {
142 ly_err_print(err);
143 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
144 ly_err_free(err);
145 }
146 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100147 } else if (dynamic) {
148 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200149 }
150
151error:
152 return ret;
153}
154
Michal Vaskof937cfe2020-08-03 16:07:12 +0200155LY_ERR
156_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 +0200157 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200158{
159 LY_ERR rc = LY_SUCCESS;
160 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200161 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200162 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200163
164 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
165
166 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
167 LOGARG(ctx, node);
168 return LY_EINVAL;
169 }
170
171 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200172 /* just validate, no storing of enything */
173 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200174 format, prefix_data, node, NULL, &storage, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200175 if (rc == LY_EINCOMPLETE) {
176 /* actually success since we do not provide the context tree and call validation with
177 * LY_TYPE_OPTS_INCOMPLETE_DATA */
178 rc = LY_SUCCESS;
179 } else if (rc && err) {
180 if (ctx) {
181 /* log only in case the ctx was provided as input parameter */
182 ly_err_print(err);
183 LOGVAL(ctx, LY_VLOG_STR, err->path, 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 }
187
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200188 if (!rc) {
189 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
190 }
Radek Krejci084289f2019-07-09 17:35:30 +0200191 return rc;
192}
193
194API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200195lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
196{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200197 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200198}
199
200API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100201lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200202 const struct lyd_node *tree, struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200203{
204 LY_ERR rc;
205 struct ly_err_item *err = NULL;
206 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200207 struct lyd_value val = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200208 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200209
210 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
211
212 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200213 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, LY_PREF_JSON, NULL,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200214 tree ? (void*)node : (void*)node->schema, tree, &val, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200215 if (rc == LY_EINCOMPLETE) {
216 return rc;
217 } else if (rc) {
218 if (err) {
219 if (ctx) {
220 ly_err_print(err);
221 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200222 }
Radek Krejci73dead22019-07-11 16:46:16 +0200223 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200224 }
Radek Krejci73dead22019-07-11 16:46:16 +0200225 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200226 }
227
Michal Vasko3701af52020-08-03 14:29:38 +0200228 if (realtype) {
229 *realtype = val.realtype;
230 }
231
232 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200233 return LY_SUCCESS;
234}
235
236API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200237lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200238{
239 LY_ERR ret = LY_SUCCESS, rc;
240 struct ly_err_item *err = NULL;
241 struct ly_ctx *ctx;
242 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200243 struct lyd_value data = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200244 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200245
246 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
247
248 ctx = node->schema->module->ctx;
249 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200250 rc = type->plugin->store(ctx, type, value, value_len, options, LY_PREF_JSON, NULL, (struct lyd_node *)node, tree, &data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200251 &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200252 if (rc == LY_EINCOMPLETE) {
253 ret = rc;
254 /* continue with comparing, just remember what to return if storing is ok */
255 } else if (rc) {
256 /* value to compare is invalid */
257 ret = LY_EINVAL;
258 if (err) {
259 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200260 }
Radek Krejci73dead22019-07-11 16:46:16 +0200261 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200262 }
263
264 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200265 if (type->plugin->compare(&node->value, &data)) {
266 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200267 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200268 }
Radek Krejci084289f2019-07-09 17:35:30 +0200269
270cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200271 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200272
273 return ret;
274}
275
Radek Krejci7931b192020-06-25 17:05:03 +0200276static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200277lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200278{
Radek Krejcie7b95092019-05-15 11:03:07 +0200279
Radek Krejci7931b192020-06-25 17:05:03 +0200280 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200282 const char *path = in->method.fpath.filepath;
283 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200284
285 /* ignore trailing whitespaces */
Radek Krejci1e008d22020-08-17 11:37:37 +0200286 for (; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200287
288 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
289 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
291 format = LYD_JSON;
292 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
293 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200294 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200295 }
296
Radek Krejci7931b192020-06-25 17:05:03 +0200297 return format;
298}
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejci7931b192020-06-25 17:05:03 +0200300API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200301lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200302 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200303{
Radek Krejci1798aae2020-07-14 13:26:06 +0200304 LY_ERR ret = LY_SUCCESS;
305 struct lyd_ctx *lydctx = NULL;
306
Radek Krejci7931b192020-06-25 17:05:03 +0200307 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
308 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
309 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
310
311 format = lyd_parse_get_format(in, format);
312 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
313
Radek Krejci1798aae2020-07-14 13:26:06 +0200314 /* init */
315 *tree = NULL;
316
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 /* remember input position */
318 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200319
320 switch (format) {
321 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
323 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200324 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200325 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
326 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200327 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200328 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
329 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200330 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200331 LOGINT_RET(ctx);
332 }
333
Radek Krejci1798aae2020-07-14 13:26:06 +0200334 if (!(parse_options & LYD_PARSE_ONLY)) {
335 uint32_t i = 0;
336 const struct lys_module *mod;
337 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200338
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 next = *tree;
340 while (1) {
341 if (validate_options & LYD_VALIDATE_PRESENT) {
342 mod = lyd_data_next_module(&next, &first);
343 } else {
344 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
345 }
346 if (!mod) {
347 break;
348 }
349 if (first == *tree) {
350 /* make sure first2 changes are carried to tree */
351 first2 = tree;
352 } else {
353 first2 = &first;
354 }
355
356 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
357 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
358
359 /* add all top-level defaults for this module */
360 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
Radek Krejci0f969882020-08-21 16:56:47 +0200361 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 LY_CHECK_GOTO(ret, cleanup);
363
364 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200365 switch (format) {
366 case LYD_XML:
367 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
368 LY_PREF_XML, &((struct lyxml_ctx *)lydctx->data_ctx)->ns, NULL);
369 break;
370 case LYD_JSON:
371 case LYD_LYB:
372 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
373 LY_PREF_JSON, NULL, NULL);
374 break;
375 case LYD_UNKNOWN:
376 LOGINT_RET(ctx);
377 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200378 LY_CHECK_GOTO(ret, cleanup);
379
380 /* perform final validation that assumes the data tree is final */
381 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
382 }
383 }
384
385cleanup:
386 lydctx->free((struct lyd_ctx *)lydctx);
387 if (ret) {
388 lyd_free_all(*tree);
389 *tree = NULL;
390 }
391 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200392}
393
394API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200395lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200396 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200397{
398 LY_ERR ret;
399 struct ly_in *in;
400
401 LY_CHECK_RET(ly_in_new_memory(data, &in));
402 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
403
404 ly_in_free(in, 0);
405 return ret;
406}
407
408API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200409lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200410 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200411{
412 LY_ERR ret;
413 struct ly_in *in;
414
415 LY_CHECK_RET(ly_in_new_fd(fd, &in));
416 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
417
418 ly_in_free(in, 0);
419 return ret;
420}
421
422API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200423lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200424 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200425{
426 LY_ERR ret;
427 struct ly_in *in;
428
429 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
430 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
431
432 ly_in_free(in, 0);
433 return ret;
434}
435
Radek Krejci7931b192020-06-25 17:05:03 +0200436API LY_ERR
437lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
438{
439 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
440
441 format = lyd_parse_get_format(in, format);
442 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
443
Radek Krejci1798aae2020-07-14 13:26:06 +0200444 /* init */
445 *tree = NULL;
446 if (op) {
447 *op = NULL;
448 }
449
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 /* remember input position */
451 in->func_start = in->current;
452
Radek Krejci7931b192020-06-25 17:05:03 +0200453 switch (format) {
454 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200456 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200458 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200460 case LYD_UNKNOWN:
461 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200462 }
463
464 LOGINT_RET(ctx);
465}
466
467API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200468lyd_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 +0200469 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200470{
471 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200472 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200473
474 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200476
Radek Krejci1798aae2020-07-14 13:26:06 +0200477 /* init */
478 if (tree) {
479 *tree = NULL;
480 }
481 if (op) {
482 *op = NULL;
483 }
484
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 /* remember input position */
486 in->func_start = in->current;
487
Radek Krejci7931b192020-06-25 17:05:03 +0200488 switch (format) {
489 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200491 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200493 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200495 case LYD_UNKNOWN:
496 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200497 }
498
Michal Vaskob7be7a82020-08-20 09:09:04 +0200499 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200500}
501
502API LY_ERR
503lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
504{
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200506
507 format = lyd_parse_get_format(in, format);
508 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
509
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 /* init */
511 if (tree) {
512 *tree = NULL;
513 }
514 if (ntf) {
515 *ntf = NULL;
516 }
517
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 /* remember input position */
519 in->func_start = in->current;
520
Radek Krejci7931b192020-06-25 17:05:03 +0200521 switch (format) {
522 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200524 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200525 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200526 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200528 case LYD_UNKNOWN:
529 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200530 }
531
532 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200533}
Radek Krejci084289f2019-07-09 17:35:30 +0200534
Michal Vasko90932a92020-02-12 14:33:03 +0100535LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200536lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
Radek Krejci0f969882020-08-21 16:56:47 +0200537 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100538{
539 LY_ERR ret;
540 struct lyd_node_term *term;
541
Michal Vasko9b368d32020-02-14 13:53:31 +0100542 assert(schema->nodetype & LYD_NODE_TERM);
543
Michal Vasko90932a92020-02-12 14:33:03 +0100544 term = calloc(1, sizeof *term);
545 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
546
547 term->schema = schema;
548 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100549 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100550
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200551 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, format, prefix_data, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100552 if (ret && (ret != LY_EINCOMPLETE)) {
553 free(term);
554 return ret;
555 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100556 lyd_hash((struct lyd_node *)term);
557
558 *node = (struct lyd_node *)term;
559 return ret;
560}
561
562LY_ERR
563lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
564{
565 LY_ERR ret;
566 struct lyd_node_term *term;
567 struct lysc_type *type;
568
569 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200570 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100571
572 term = calloc(1, sizeof *term);
573 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
574
575 term->schema = schema;
576 term->prev = (struct lyd_node *)term;
577 term->flags = LYD_NEW;
578
579 type = ((struct lysc_node_leaf *)schema)->type;
580 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
581 if (ret) {
582 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
583 free(term);
584 return ret;
585 }
586 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100587
588 *node = (struct lyd_node *)term;
589 return ret;
590}
591
592LY_ERR
593lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
594{
595 struct lyd_node_inner *in;
596
Michal Vasko9b368d32020-02-14 13:53:31 +0100597 assert(schema->nodetype & LYD_NODE_INNER);
598
Michal Vasko90932a92020-02-12 14:33:03 +0100599 in = calloc(1, sizeof *in);
600 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
601
602 in->schema = schema;
603 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100604 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100605
Michal Vasko9b368d32020-02-14 13:53:31 +0100606 /* do not hash list with keys, we need them for the hash */
607 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
608 lyd_hash((struct lyd_node *)in);
609 }
Michal Vasko90932a92020-02-12 14:33:03 +0100610
611 *node = (struct lyd_node *)in;
612 return LY_SUCCESS;
613}
614
Michal Vasko90932a92020-02-12 14:33:03 +0100615LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200616lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100617{
618 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100619 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200620 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100621
Michal Vasko004d3152020-06-11 19:59:22 +0200622 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100623
624 /* create list */
625 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
626
Michal Vasko90932a92020-02-12 14:33:03 +0100627 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200628 LY_ARRAY_FOR(predicates, u) {
629 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100630 lyd_insert_node(list, NULL, key);
631 }
632
Michal Vasko9b368d32020-02-14 13:53:31 +0100633 /* hash having all the keys */
634 lyd_hash(list);
635
Michal Vasko90932a92020-02-12 14:33:03 +0100636 /* success */
637 *node = list;
638 list = NULL;
639
640cleanup:
641 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200642 return ret;
643}
644
645static LY_ERR
646lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
647{
648 LY_ERR ret = LY_SUCCESS;
649 struct lyxp_expr *expr = NULL;
650 uint16_t exp_idx = 0;
651 enum ly_path_pred_type pred_type = 0;
652 struct ly_path_predicate *predicates = NULL;
653
654 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200655 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko004d3152020-06-11 19:59:22 +0200656 LY_PATH_PRED_KEYS, &expr), cleanup);
657
658 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200659 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200660 LY_PREF_JSON, NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200661
662 /* create the list node */
663 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
664
665cleanup:
666 lyxp_expr_free(schema->module->ctx, expr);
667 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100668 return ret;
669}
670
671LY_ERR
672lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
673{
674 struct lyd_node_any *any;
675
Michal Vasko9b368d32020-02-14 13:53:31 +0100676 assert(schema->nodetype & LYD_NODE_ANY);
677
Michal Vasko90932a92020-02-12 14:33:03 +0100678 any = calloc(1, sizeof *any);
679 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
680
681 any->schema = schema;
682 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100683 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100684
Radek Krejci1798aae2020-07-14 13:26:06 +0200685 /* TODO: convert XML/JSON strings into a opaq data tree */
686 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100687 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100688 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100689
690 *node = (struct lyd_node *)any;
691 return LY_SUCCESS;
692}
693
Michal Vasko52927e22020-03-16 17:26:14 +0100694LY_ERR
695lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200696 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
697 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100698{
699 struct lyd_node_opaq *opaq;
700
Radek Krejci1798aae2020-07-14 13:26:06 +0200701 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100702
703 if (!value_len) {
704 value = "";
705 }
706
707 opaq = calloc(1, sizeof *opaq);
708 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
709
710 opaq->prev = (struct lyd_node *)opaq;
711
712 opaq->name = lydict_insert(ctx, name, name_len);
713 opaq->format = format;
714 if (pref_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 opaq->prefix.id = lydict_insert(ctx, prefix, pref_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100716 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200717 if (module_key_len) {
718 opaq->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
719 }
720
Michal Vasko52927e22020-03-16 17:26:14 +0100721 opaq->val_prefs = val_prefs;
722 if (dynamic && *dynamic) {
723 opaq->value = lydict_insert_zc(ctx, (char *)value);
724 *dynamic = 0;
725 } else {
726 opaq->value = lydict_insert(ctx, value, value_len);
727 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100729 opaq->ctx = ctx;
730
731 *node = (struct lyd_node *)opaq;
732 return LY_SUCCESS;
733}
734
Michal Vasko3a41dff2020-07-15 14:30:28 +0200735API LY_ERR
736lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100737{
738 struct lyd_node *ret = NULL;
739 const struct lysc_node *schema;
740 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
741
Michal Vasko6027eb92020-07-15 16:37:30 +0200742 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100743
Michal Vaskof03ed032020-03-04 13:31:44 +0100744 if (!module) {
745 module = parent->schema->module;
746 }
747
Michal Vasko3a41dff2020-07-15 14:30:28 +0200748 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
749 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
750 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 +0100751
Michal Vasko3a41dff2020-07-15 14:30:28 +0200752 LY_CHECK_RET(lyd_create_inner(schema, &ret));
753 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100754 lyd_insert_node(parent, NULL, ret);
755 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200756
757 if (node) {
758 *node = ret;
759 }
760 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100761}
762
Michal Vasko3a41dff2020-07-15 14:30:28 +0200763API LY_ERR
764lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100765{
766 struct lyd_node *ret = NULL, *key;
767 const struct lysc_node *schema, *key_s;
768 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
769 va_list ap;
770 const char *key_val;
771 LY_ERR rc = LY_SUCCESS;
772
Michal Vasko6027eb92020-07-15 16:37:30 +0200773 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100774
Michal Vaskof03ed032020-03-04 13:31:44 +0100775 if (!module) {
776 module = parent->schema->module;
777 }
778
Michal Vasko013a8182020-03-03 10:46:53 +0100779 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200780 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100781
782 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200783 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100784
Michal Vasko3a41dff2020-07-15 14:30:28 +0200785 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100786
787 /* create and insert all the keys */
788 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
789 key_val = va_arg(ap, const char *);
790
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200791 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, 0, LY_PREF_JSON, NULL, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200792 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
793 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100794 lyd_insert_node(ret, NULL, key);
795 }
796
Michal Vasko013a8182020-03-03 10:46:53 +0100797 if (parent) {
798 lyd_insert_node(parent, NULL, ret);
799 }
800
801cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200802 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100803 if (rc) {
804 lyd_free_tree(ret);
805 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200806 } else if (node) {
807 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100808 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200809 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100810}
811
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812API LY_ERR
813lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200814 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100815{
816 struct lyd_node *ret = NULL;
817 const struct lysc_node *schema;
818 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
819
Michal Vasko6027eb92020-07-15 16:37:30 +0200820 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100821
Michal Vaskof03ed032020-03-04 13:31:44 +0100822 if (!module) {
823 module = parent->schema->module;
824 }
Michal Vasko004d3152020-06-11 19:59:22 +0200825 if (!keys) {
826 keys = "";
827 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100828
Michal Vasko004d3152020-06-11 19:59:22 +0200829 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100830 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100832
Michal Vasko004d3152020-06-11 19:59:22 +0200833 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
834 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200835 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200836 } else {
837 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200838 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200839 }
Michal Vasko004d3152020-06-11 19:59:22 +0200840 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100841 lyd_insert_node(parent, NULL, ret);
842 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200843
844 if (node) {
845 *node = ret;
846 }
847 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100848}
849
Michal Vasko3a41dff2020-07-15 14:30:28 +0200850API LY_ERR
851lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200852 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100853{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200854 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100855 struct lyd_node *ret = NULL;
856 const struct lysc_node *schema;
857 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
858
Michal Vasko6027eb92020-07-15 16:37:30 +0200859 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100860
Michal Vaskof03ed032020-03-04 13:31:44 +0100861 if (!module) {
862 module = parent->schema->module;
863 }
864
Michal Vasko013a8182020-03-03 10:46:53 +0100865 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200866 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100867
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200868 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, 0, LY_PREF_JSON, NULL, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200869 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200870 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100871 lyd_insert_node(parent, NULL, ret);
872 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200873
874 if (node) {
875 *node = ret;
876 }
877 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100878}
879
Michal Vasko3a41dff2020-07-15 14:30:28 +0200880API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100881lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200882 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100883{
884 struct lyd_node *ret = NULL;
885 const struct lysc_node *schema;
886 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
887
Michal Vasko6027eb92020-07-15 16:37:30 +0200888 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100889
Michal Vaskof03ed032020-03-04 13:31:44 +0100890 if (!module) {
891 module = parent->schema->module;
892 }
893
Michal Vasko013a8182020-03-03 10:46:53 +0100894 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200895 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100896
Michal Vasko3a41dff2020-07-15 14:30:28 +0200897 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
898 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100899 lyd_insert_node(parent, NULL, ret);
900 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200901
902 if (node) {
903 *node = ret;
904 }
905 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100906}
907
Michal Vasko4490d312020-06-16 13:08:55 +0200908/**
909 * @brief Update node value.
910 *
911 * @param[in] node Node to update.
912 * @param[in] value New value to set.
913 * @param[in] value_type Type of @p value for any node.
914 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
915 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
916 * @return LY_ERR value.
917 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200918static LY_ERR
919lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200920 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200921{
922 LY_ERR ret = LY_SUCCESS;
923 struct lyd_node *new_any;
924
925 switch (node->schema->nodetype) {
926 case LYS_CONTAINER:
927 case LYS_NOTIF:
928 case LYS_RPC:
929 case LYS_ACTION:
930 case LYS_LIST:
931 case LYS_LEAFLIST:
932 /* if it exists, there is nothing to update */
933 *new_parent = NULL;
934 *new_node = NULL;
935 break;
936 case LYS_LEAF:
937 ret = lyd_change_term(node, value);
938 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
939 /* there was an actual change (at least of the default flag) */
940 *new_parent = node;
941 *new_node = node;
942 ret = LY_SUCCESS;
943 } else if (ret == LY_ENOT) {
944 /* no change */
945 *new_parent = NULL;
946 *new_node = NULL;
947 ret = LY_SUCCESS;
948 } /* else error */
949 break;
950 case LYS_ANYDATA:
951 case LYS_ANYXML:
952 /* create a new any node */
953 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
954
955 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200956 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200957 /* not equal, switch values (so that we can use generic node free) */
958 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
959 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
960 ((struct lyd_node_any *)node)->value.str = value;
961 ((struct lyd_node_any *)node)->value_type = value_type;
962
963 *new_parent = node;
964 *new_node = node;
965 } else {
966 /* they are equal */
967 *new_parent = NULL;
968 *new_node = NULL;
969 }
970 lyd_free_tree(new_any);
971 break;
972 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200973 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200974 ret = LY_EINT;
975 break;
976 }
977
978 return ret;
979}
980
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981API LY_ERR
982lyd_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 +0200983 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200984{
985 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200986 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200987 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200988 size_t pref_len, name_len;
989
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200991
Michal Vaskob7be7a82020-08-20 09:09:04 +0200992 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200993
994 /* parse the name */
995 tmp = name;
996 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
997 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200998 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200999 }
1000
1001 /* find the module */
1002 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001003 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004 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 +02001005 }
1006
1007 /* set value if none */
1008 if (!val_str) {
1009 val_str = "";
1010 }
1011
Radek Krejci1798aae2020-07-14 13:26:06 +02001012 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001013 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001014
1015 if (meta) {
1016 *meta = ret;
1017 }
1018 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001019}
1020
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001022lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001023 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001024{
1025 struct lyd_node *ret = NULL;
1026
Michal Vasko6027eb92020-07-15 16:37:30 +02001027 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001028
1029 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001030 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001031 }
1032 if (!value) {
1033 value = "";
1034 }
1035
Radek Krejci1798aae2020-07-14 13:26:06 +02001036 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1037 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001039 lyd_insert_node(parent, NULL, ret);
1040 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001041
1042 if (node) {
1043 *node = ret;
1044 }
1045 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001046}
1047
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048API LY_ERR
1049lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +02001050 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001051{
Radek Krejci1798aae2020-07-14 13:26:06 +02001052 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001053 const struct ly_ctx *ctx;
1054 const char *prefix, *tmp;
1055 size_t pref_len, name_len;
1056
Michal Vasko3a41dff2020-07-15 14:30:28 +02001057 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001058
Michal Vaskob7be7a82020-08-20 09:09:04 +02001059 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001060
1061 /* parse the name */
1062 tmp = name;
1063 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1064 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001066 }
1067
1068 /* set value if none */
1069 if (!val_str) {
1070 val_str = "";
1071 }
1072
Radek Krejci1798aae2020-07-14 13:26:06 +02001073 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1074 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001075
1076 if (attr) {
1077 *attr = ret;
1078 }
1079 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001080}
1081
1082API LY_ERR
1083lyd_change_term(struct lyd_node *term, const char *val_str)
1084{
1085 LY_ERR ret = LY_SUCCESS;
1086 struct lysc_type *type;
1087 struct lyd_node_term *t;
1088 struct lyd_node *parent;
1089 struct lyd_value val = {0};
1090 int dflt_change, val_change;
1091
1092 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1093
1094 if (!val_str) {
1095 val_str = "";
1096 }
1097 t = (struct lyd_node_term *)term;
1098 type = ((struct lysc_node_leaf *)term->schema)->type;
1099
1100 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001101 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001102
1103 /* compare original and new value */
1104 if (type->plugin->compare(&t->value, &val)) {
1105 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001106 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001107 t->value = val;
1108 memset(&val, 0, sizeof val);
1109 val_change = 1;
1110 } else {
1111 val_change = 0;
1112 }
1113
1114 /* always clear the default flag */
1115 if (term->flags & LYD_DEFAULT) {
1116 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1117 parent->flags &= ~LYD_DEFAULT;
1118 }
1119 dflt_change = 1;
1120 } else {
1121 dflt_change = 0;
1122 }
1123
1124 if (val_change || dflt_change) {
1125 /* make the node non-validated */
1126 term->flags &= LYD_NEW;
1127 }
1128
1129 if (val_change) {
1130 if (term->schema->nodetype == LYS_LEAFLIST) {
1131 /* leaf-list needs to be hashed again and re-inserted into parent */
1132 lyd_unlink_hash(term);
1133 lyd_hash(term);
1134 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1135 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1136 /* list needs to be updated if its key was changed */
1137 assert(term->parent->schema->nodetype == LYS_LIST);
1138 lyd_unlink_hash((struct lyd_node *)term->parent);
1139 lyd_hash((struct lyd_node *)term->parent);
1140 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1141 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1142 }
1143
1144 /* retrun value */
1145 if (!val_change) {
1146 if (dflt_change) {
1147 /* only default flag change */
1148 ret = LY_EEXIST;
1149 } else {
1150 /* no change */
1151 ret = LY_ENOT;
1152 }
1153 } /* else value changed, LY_SUCCESS */
1154
1155cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001156 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001157 return ret;
1158}
1159
Michal Vasko41586352020-07-13 13:54:25 +02001160API LY_ERR
1161lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1162{
1163 LY_ERR ret = LY_SUCCESS;
1164 struct lyd_meta *m2;
1165 struct lyd_value val;
1166 int val_change;
1167
1168 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1169
1170 if (!val_str) {
1171 val_str = "";
1172 }
1173
1174 /* parse the new value into a new meta structure */
1175 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001176 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001177
1178 /* compare original and new value */
1179 if (lyd_compare_meta(meta, m2)) {
1180 /* values differ, switch them */
1181 val = meta->value;
1182 meta->value = m2->value;
1183 m2->value = val;
1184 val_change = 1;
1185 } else {
1186 val_change = 0;
1187 }
1188
1189 /* retrun value */
1190 if (!val_change) {
1191 /* no change */
1192 ret = LY_ENOT;
1193 } /* else value changed, LY_SUCCESS */
1194
1195cleanup:
1196 return ret;
1197}
1198
Michal Vasko3a41dff2020-07-15 14:30:28 +02001199API LY_ERR
1200lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
Radek Krejci0f969882020-08-21 16:56:47 +02001201 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001202{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001203 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001204}
1205
1206API LY_ERR
1207lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001208 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001209{
1210 LY_ERR ret = LY_SUCCESS, r;
1211 struct lyxp_expr *exp = NULL;
1212 struct ly_path *p = NULL;
1213 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1214 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001215 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001216 struct ly_path_predicate *pred;
1217
1218 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1219
1220 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001221 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001222 }
1223
1224 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001225 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko00cbf532020-06-15 13:58:47 +02001226 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1227
1228 /* compile path */
1229 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1230 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001231 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001232
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001233 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1234 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001235 && !(options & LYD_NEWOPT_OPAQ)) {
1236 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1237 lys_nodetype2str(schema->nodetype), schema->name);
1238 ret = LY_EINVAL;
1239 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001240 } 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 +02001241 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001242 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1243 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001244
1245 if (!value) {
1246 value = "";
1247 }
1248
1249 r = LY_SUCCESS;
1250 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001251 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001252 }
1253 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001254 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001255 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1256 }
1257
1258 /* try to find any existing nodes in the path */
1259 if (parent) {
1260 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1261 if (ret == LY_SUCCESS) {
1262 /* the node exists, are we supposed to update it or is it just a default? */
1263 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1264 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1265 ret = LY_EEXIST;
1266 goto cleanup;
1267 }
1268
1269 /* update the existing node */
1270 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1271 goto cleanup;
1272 } else if (ret == LY_EINCOMPLETE) {
1273 /* some nodes were found, adjust the iterator to the next segment */
1274 ++path_idx;
1275 } else if (ret == LY_ENOTFOUND) {
1276 /* 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 +02001277 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001278 node = parent;
1279 }
1280 } else {
1281 /* error */
1282 goto cleanup;
1283 }
1284 }
1285
1286 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001287 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001288 cur_parent = node;
1289 schema = p[path_idx].node;
1290
1291 switch (schema->nodetype) {
1292 case LYS_LIST:
1293 if (!(schema->flags & LYS_KEYLESS)) {
1294 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1295 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001296 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1297 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1298 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001299 } else {
1300 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1301 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1302 }
1303 break;
1304 }
Radek Krejci0f969882020-08-21 16:56:47 +02001305 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001306 case LYS_CONTAINER:
1307 case LYS_NOTIF:
1308 case LYS_RPC:
1309 case LYS_ACTION:
1310 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1311 break;
1312 case LYS_LEAFLIST:
1313 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1314 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001315 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1316 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1317 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001318 } else {
1319 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1320 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1321 }
1322 break;
1323 case LYS_LEAF:
1324 /* make there is some value */
1325 if (!value) {
1326 value = "";
1327 }
1328
1329 r = LY_SUCCESS;
1330 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001331 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001332 }
1333 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001334 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, 0, LY_PREF_JSON, NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001335 } else {
1336 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001337 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001338 LYD_JSON, NULL, NULL, 0, schema->module->name,
1339 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001340 }
1341 break;
1342 case LYS_ANYDATA:
1343 case LYS_ANYXML:
1344 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1345 break;
1346 default:
1347 LOGINT(ctx);
1348 ret = LY_EINT;
1349 goto cleanup;
1350 }
1351
1352 if (cur_parent) {
1353 /* connect to the parent */
1354 lyd_insert_node(cur_parent, NULL, node);
1355 } else if (parent) {
1356 /* connect to top-level siblings */
1357 lyd_insert_node(NULL, &parent, node);
1358 }
1359
1360 /* update remembered nodes */
1361 if (!nparent) {
1362 nparent = node;
1363 }
1364 nnode = node;
1365 }
1366
1367cleanup:
1368 lyxp_expr_free(ctx, exp);
1369 ly_path_free(ctx, p);
1370 if (!ret) {
1371 /* set out params only on success */
1372 if (new_parent) {
1373 *new_parent = nparent;
1374 }
1375 if (new_node) {
1376 *new_node = nnode;
1377 }
1378 }
1379 return ret;
1380}
1381
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001382LY_ERR
1383lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci0f969882020-08-21 16:56:47 +02001384 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int impl_opts,
1385 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001386{
1387 LY_ERR ret;
1388 const struct lysc_node *iter = NULL;
1389 struct lyd_node *node;
1390 struct lyd_value **dflts;
1391 LY_ARRAY_COUNT_TYPE u;
1392
1393 assert(first && (parent || sparent || mod));
1394
1395 if (!sparent && parent) {
1396 sparent = parent->schema;
1397 }
1398
1399 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1400 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1401 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001402 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1403 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001404 }
1405
1406 switch (iter->nodetype) {
1407 case LYS_CHOICE:
1408 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1409 /* create default case data */
1410 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1411 NULL, node_types, node_when, impl_opts, diff));
1412 }
1413 break;
1414 case LYS_CONTAINER:
1415 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1416 /* create default NP container */
1417 LY_CHECK_RET(lyd_create_inner(iter, &node));
1418 node->flags = LYD_DEFAULT;
1419 lyd_insert_node(parent, first, node);
1420
1421 /* cannot be a NP container with when */
1422 assert(!iter->when);
1423
1424 /* create any default children */
1425 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1426 impl_opts, diff));
1427 }
1428 break;
1429 case LYS_LEAF:
1430 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1431 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1432 /* create default leaf */
1433 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1434 if (ret == LY_EINCOMPLETE) {
1435 if (node_types) {
1436 /* remember to resolve type */
1437 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1438 }
1439 } else if (ret) {
1440 return ret;
1441 }
1442 node->flags = LYD_DEFAULT;
1443 lyd_insert_node(parent, first, node);
1444
1445 if (iter->when && node_when) {
1446 /* remember to resolve when */
1447 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1448 }
1449 if (diff) {
1450 /* add into diff */
1451 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1452 }
1453 }
1454 break;
1455 case LYS_LEAFLIST:
1456 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1457 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1458 /* create all default leaf-lists */
1459 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1460 LY_ARRAY_FOR(dflts, u) {
1461 ret = lyd_create_term2(iter, dflts[u], &node);
1462 if (ret == LY_EINCOMPLETE) {
1463 if (node_types) {
1464 /* remember to resolve type */
1465 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1466 }
1467 } else if (ret) {
1468 return ret;
1469 }
1470 node->flags = LYD_DEFAULT;
1471 lyd_insert_node(parent, first, node);
1472
1473 if (iter->when && node_when) {
1474 /* remember to resolve when */
1475 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1476 }
1477 if (diff) {
1478 /* add into diff */
1479 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1480 }
1481 }
1482 }
1483 break;
1484 default:
1485 /* without defaults */
1486 break;
1487 }
1488 }
1489
1490 return LY_SUCCESS;
1491}
1492
1493API LY_ERR
1494lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff)
1495{
Michal Vasko56daf732020-08-10 10:57:18 +02001496 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001497 LY_ERR ret = LY_SUCCESS;
1498
1499 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1500 if (diff) {
1501 *diff = NULL;
1502 }
1503
Michal Vasko56daf732020-08-10 10:57:18 +02001504 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001505 /* skip added default nodes */
1506 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1507 && (node->schema->nodetype & LYD_NODE_INNER)) {
1508 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1509 NULL, implicit_options, diff), cleanup);
1510 }
1511
Michal Vasko56daf732020-08-10 10:57:18 +02001512 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001513 }
1514
1515cleanup:
1516 if (ret && diff) {
1517 lyd_free_all(*diff);
1518 *diff = NULL;
1519 }
1520 return ret;
1521}
1522
1523API LY_ERR
1524lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff)
1525{
1526 const struct lys_module *mod;
1527 struct lyd_node *d = NULL;
1528 uint32_t i = 0;
1529 LY_ERR ret = LY_SUCCESS;
1530
1531 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1532 if (diff) {
1533 *diff = NULL;
1534 }
1535 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001536 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001537 }
1538
1539 /* add nodes for each module one-by-one */
1540 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1541 if (!mod->implemented) {
1542 continue;
1543 }
1544
1545 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1546 if (d) {
1547 /* merge into one diff */
1548 lyd_insert_sibling(*diff, d, diff);
1549
1550 d = NULL;
1551 }
1552 }
1553
1554cleanup:
1555 if (ret && diff) {
1556 lyd_free_all(*diff);
1557 *diff = NULL;
1558 }
1559 return ret;
1560}
1561
1562API LY_ERR
1563lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options, struct lyd_node **diff)
1564{
1565 struct lyd_node *root, *d = NULL;
1566 LY_ERR ret = LY_SUCCESS;
1567
1568 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1569 if (diff) {
1570 *diff = NULL;
1571 }
1572
1573 /* add all top-level defaults for this module */
1574 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1575
1576 /* process nested nodes */
1577 LY_LIST_FOR(*tree, root) {
1578 /* skip added default nodes */
1579 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1580 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1581
1582 if (d) {
1583 /* merge into one diff */
1584 lyd_insert_sibling(*diff, d, diff);
1585
1586 d = NULL;
1587 }
1588 }
1589 }
1590
1591cleanup:
1592 if (ret && diff) {
1593 lyd_free_all(*diff);
1594 *diff = NULL;
1595 }
1596 return ret;
1597}
1598
Michal Vasko90932a92020-02-12 14:33:03 +01001599struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001600lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001601{
Michal Vaskob104f112020-07-17 09:54:54 +02001602 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001603 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001604 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001605
Michal Vaskob104f112020-07-17 09:54:54 +02001606 assert(new_node);
1607
1608 if (!first_sibling || !new_node->schema) {
1609 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001610 return NULL;
1611 }
1612
Michal Vaskob104f112020-07-17 09:54:54 +02001613 if (first_sibling->parent && first_sibling->parent->children_ht) {
1614 /* find the anchor using hashes */
1615 sparent = first_sibling->parent->schema;
1616 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1617 while (schema) {
1618 /* keep trying to find the first existing instance of the closest following schema sibling,
1619 * otherwise return NULL - inserting at the end */
1620 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1621 break;
1622 }
1623
1624 schema = lys_getnext(schema, sparent, NULL, 0);
1625 }
1626 } else {
1627 /* find the anchor without hashes */
1628 match = (struct lyd_node *)first_sibling;
1629 if (!lysc_data_parent(new_node->schema)) {
1630 /* we are in top-level, skip all the data from preceding modules */
1631 LY_LIST_FOR(match, match) {
1632 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1633 break;
1634 }
1635 }
1636 }
1637
1638 /* get the first schema sibling */
1639 sparent = lysc_data_parent(new_node->schema);
1640 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1641
1642 found = 0;
1643 LY_LIST_FOR(match, match) {
1644 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1645 /* we have found an opaque node, which must be at the end, so use it OR
1646 * modules do not match, so we must have traversed all the data from new_node module (if any),
1647 * we have found the first node of the next module, that is what we want */
1648 break;
1649 }
1650
1651 /* skip schema nodes until we find the instantiated one */
1652 while (!found) {
1653 if (new_node->schema == schema) {
1654 /* we have found the schema of the new node, continue search to find the first
1655 * data node with a different schema (after our schema) */
1656 found = 1;
1657 break;
1658 }
1659 if (match->schema == schema) {
1660 /* current node (match) is a data node still before the new node, continue search in data */
1661 break;
1662 }
1663 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1664 assert(schema);
1665 }
1666
1667 if (found && (match->schema != new_node->schema)) {
1668 /* find the next node after we have found our node schema data instance */
1669 break;
1670 }
1671 }
Michal Vasko90932a92020-02-12 14:33:03 +01001672 }
1673
1674 return match;
1675}
1676
1677/**
1678 * @brief Insert node after a sibling.
1679 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001680 * Handles inserting into NP containers and key-less lists.
1681 *
Michal Vasko90932a92020-02-12 14:33:03 +01001682 * @param[in] sibling Sibling to insert after.
1683 * @param[in] node Node to insert.
1684 */
1685static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001686lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001687{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001688 struct lyd_node_inner *par;
1689
Michal Vasko90932a92020-02-12 14:33:03 +01001690 assert(!node->next && (node->prev == node));
1691
1692 node->next = sibling->next;
1693 node->prev = sibling;
1694 sibling->next = node;
1695 if (node->next) {
1696 /* sibling had a succeeding node */
1697 node->next->prev = node;
1698 } else {
1699 /* sibling was last, find first sibling and change its prev */
1700 if (sibling->parent) {
1701 sibling = sibling->parent->child;
1702 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02001703 for (; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001704 }
1705 sibling->prev = node;
1706 }
1707 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001708
Michal Vasko9f96a052020-03-10 09:41:45 +01001709 for (par = node->parent; par; par = par->parent) {
1710 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1711 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001712 par->flags &= ~LYD_DEFAULT;
1713 }
Michal Vaskob104f112020-07-17 09:54:54 +02001714 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001715 /* rehash key-less list */
1716 lyd_hash((struct lyd_node *)par);
1717 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001718 }
Michal Vasko90932a92020-02-12 14:33:03 +01001719}
1720
1721/**
1722 * @brief Insert node before a sibling.
1723 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001724 * Handles inserting into NP containers and key-less lists.
1725 *
Michal Vasko90932a92020-02-12 14:33:03 +01001726 * @param[in] sibling Sibling to insert before.
1727 * @param[in] node Node to insert.
1728 */
1729static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001730lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001731{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001732 struct lyd_node_inner *par;
1733
Michal Vasko90932a92020-02-12 14:33:03 +01001734 assert(!node->next && (node->prev == node));
1735
1736 node->next = sibling;
1737 /* covers situation of sibling being first */
1738 node->prev = sibling->prev;
1739 sibling->prev = node;
1740 if (node->prev->next) {
1741 /* sibling had a preceding node */
1742 node->prev->next = node;
1743 } else if (sibling->parent) {
1744 /* sibling was first and we must also change parent child pointer */
1745 sibling->parent->child = node;
1746 }
1747 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001748
Michal Vasko9f96a052020-03-10 09:41:45 +01001749 for (par = node->parent; par; par = par->parent) {
1750 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1751 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001752 par->flags &= ~LYD_DEFAULT;
1753 }
Michal Vaskob104f112020-07-17 09:54:54 +02001754 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001755 /* rehash key-less list */
1756 lyd_hash((struct lyd_node *)par);
1757 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001758 }
Michal Vasko90932a92020-02-12 14:33:03 +01001759}
1760
1761/**
Michal Vaskob104f112020-07-17 09:54:54 +02001762 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001763 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001764 * Handles inserting into NP containers and key-less lists.
1765 *
Michal Vasko90932a92020-02-12 14:33:03 +01001766 * @param[in] parent Parent to insert into.
1767 * @param[in] node Node to insert.
1768 */
1769static void
Michal Vaskob104f112020-07-17 09:54:54 +02001770lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001771{
1772 struct lyd_node_inner *par;
1773
Michal Vaskob104f112020-07-17 09:54:54 +02001774 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001775 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001776
1777 par = (struct lyd_node_inner *)parent;
1778
Michal Vaskob104f112020-07-17 09:54:54 +02001779 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001780 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001781
Michal Vasko9f96a052020-03-10 09:41:45 +01001782 for (; par; par = par->parent) {
1783 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1784 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001785 par->flags &= ~LYD_DEFAULT;
1786 }
Michal Vasko52927e22020-03-16 17:26:14 +01001787 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001788 /* rehash key-less list */
1789 lyd_hash((struct lyd_node *)par);
1790 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001791 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001792}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001793
Michal Vasko751cb4d2020-07-14 12:25:28 +02001794/**
1795 * @brief Learn whether a list instance has all the keys.
1796 *
1797 * @param[in] list List instance to check.
1798 * @return non-zero if all the keys were found,
1799 * @return 0 otherwise.
1800 */
1801static int
1802lyd_insert_has_keys(const struct lyd_node *list)
1803{
1804 const struct lyd_node *key;
1805 const struct lysc_node *skey = NULL;
1806
1807 assert(list->schema->nodetype == LYS_LIST);
1808 key = lyd_node_children(list, 0);
1809 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1810 if (!key || (key->schema != skey)) {
1811 /* key missing */
1812 return 0;
1813 }
1814
1815 key = key->next;
1816 }
1817
1818 /* all keys found */
1819 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001820}
1821
1822void
Michal Vaskob104f112020-07-17 09:54:54 +02001823lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001824{
Michal Vaskob104f112020-07-17 09:54:54 +02001825 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001826
Michal Vaskob104f112020-07-17 09:54:54 +02001827 /* inserting list without its keys is not supported */
1828 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001829
Michal Vaskob104f112020-07-17 09:54:54 +02001830 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1831 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001832 }
Michal Vasko90932a92020-02-12 14:33:03 +01001833
Michal Vaskob104f112020-07-17 09:54:54 +02001834 /* get first sibling */
1835 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001836
Michal Vaskob104f112020-07-17 09:54:54 +02001837 /* find the anchor, our next node, so we can insert before it */
1838 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1839 if (anchor) {
1840 lyd_insert_before_node(anchor, node);
1841 } else if (first_sibling) {
1842 lyd_insert_after_node(first_sibling->prev, node);
1843 } else if (parent) {
1844 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001845 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001846 *first_sibling_p = node;
1847 }
1848
1849 /* insert into parent HT */
1850 lyd_insert_hash(node);
1851
1852 /* finish hashes for our parent, if needed and possible */
1853 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1854 lyd_hash(parent);
1855
1856 /* now we can insert even the list into its parent HT */
1857 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001858 }
Michal Vasko90932a92020-02-12 14:33:03 +01001859}
1860
Michal Vaskof03ed032020-03-04 13:31:44 +01001861static LY_ERR
1862lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1863{
1864 const struct lysc_node *par2;
1865
1866 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001867 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001868
1869 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001870 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001871
1872 if (parent) {
1873 /* inner node */
1874 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001875 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1876 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001877 return LY_EINVAL;
1878 }
1879 } else {
1880 /* top-level node */
1881 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001882 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001883 return LY_EINVAL;
1884 }
1885 }
1886
1887 return LY_SUCCESS;
1888}
1889
1890API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001891lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001892{
1893 struct lyd_node *iter;
1894
Michal Vasko654bc852020-06-23 13:28:06 +02001895 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001896
1897 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1898
1899 if (node->schema->flags & LYS_KEY) {
1900 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1901 return LY_EINVAL;
1902 }
1903
1904 if (node->parent || node->prev->next) {
1905 lyd_unlink_tree(node);
1906 }
1907
1908 while (node) {
1909 iter = node->next;
1910 lyd_unlink_tree(node);
1911 lyd_insert_node(parent, NULL, node);
1912 node = iter;
1913 }
1914 return LY_SUCCESS;
1915}
1916
1917API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001918lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001919{
1920 struct lyd_node *iter;
1921
Michal Vaskob104f112020-07-17 09:54:54 +02001922 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001923
Michal Vaskob104f112020-07-17 09:54:54 +02001924 if (sibling) {
1925 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001926 }
1927
1928 if (node->parent || node->prev->next) {
1929 lyd_unlink_tree(node);
1930 }
1931
1932 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001933 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001934 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001935 return LY_EINVAL;
1936 }
1937
Michal Vaskob1b5c262020-03-05 14:29:47 +01001938 iter = node->next;
1939 lyd_unlink_tree(node);
1940 lyd_insert_node(NULL, &sibling, node);
1941 node = iter;
1942 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001943
Michal Vaskob104f112020-07-17 09:54:54 +02001944 if (first) {
1945 /* find the first sibling */
1946 *first = sibling;
1947 while ((*first)->prev->next) {
1948 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001949 }
1950 }
1951
1952 return LY_SUCCESS;
1953}
1954
Michal Vaskob1b5c262020-03-05 14:29:47 +01001955API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001956lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1957{
1958 struct lyd_node *iter;
1959
1960 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1961
Michal Vasko62ed12d2020-05-21 10:08:25 +02001962 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001963
Michal Vaskob104f112020-07-17 09:54:54 +02001964 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001965 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001966 return LY_EINVAL;
1967 }
1968
1969 if (node->parent || node->prev->next) {
1970 lyd_unlink_tree(node);
1971 }
1972
1973 /* insert in reverse order to get the original order */
1974 node = node->prev;
1975 while (node) {
1976 iter = node->prev;
1977 lyd_unlink_tree(node);
1978
1979 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001980 lyd_insert_hash(node);
1981
Michal Vaskof03ed032020-03-04 13:31:44 +01001982 /* move the anchor accordingly */
1983 sibling = node;
1984
1985 node = (iter == node) ? NULL : iter;
1986 }
1987 return LY_SUCCESS;
1988}
1989
1990API LY_ERR
1991lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1992{
1993 struct lyd_node *iter;
1994
1995 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1996
Michal Vasko62ed12d2020-05-21 10:08:25 +02001997 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001998
Michal Vaskob104f112020-07-17 09:54:54 +02001999 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002000 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002001 return LY_EINVAL;
2002 }
2003
2004 if (node->parent || node->prev->next) {
2005 lyd_unlink_tree(node);
2006 }
2007
2008 while (node) {
2009 iter = node->next;
2010 lyd_unlink_tree(node);
2011
2012 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002013 lyd_insert_hash(node);
2014
Michal Vaskof03ed032020-03-04 13:31:44 +01002015 /* move the anchor accordingly */
2016 sibling = node;
2017
2018 node = iter;
2019 }
2020 return LY_SUCCESS;
2021}
2022
2023API void
2024lyd_unlink_tree(struct lyd_node *node)
2025{
2026 struct lyd_node *iter;
2027
2028 if (!node) {
2029 return;
2030 }
2031
Michal Vaskob104f112020-07-17 09:54:54 +02002032 /* update hashes while still linked into the tree */
2033 lyd_unlink_hash(node);
2034
Michal Vaskof03ed032020-03-04 13:31:44 +01002035 /* unlink from siblings */
2036 if (node->prev->next) {
2037 node->prev->next = node->next;
2038 }
2039 if (node->next) {
2040 node->next->prev = node->prev;
2041 } else {
2042 /* unlinking the last node */
2043 if (node->parent) {
2044 iter = node->parent->child;
2045 } else {
2046 iter = node->prev;
2047 while (iter->prev != node) {
2048 iter = iter->prev;
2049 }
2050 }
2051 /* update the "last" pointer from the first node */
2052 iter->prev = node->prev;
2053 }
2054
2055 /* unlink from parent */
2056 if (node->parent) {
2057 if (node->parent->child == node) {
2058 /* the node is the first child */
2059 node->parent->child = node->next;
2060 }
2061
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002062 /* check for NP container whether its last non-default node is not being unlinked */
2063 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2064 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2065 LY_LIST_FOR(node->parent->child, iter) {
2066 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2067 break;
2068 }
2069 }
2070 if (!iter) {
2071 node->parent->flags |= LYD_DEFAULT;
2072 }
2073 }
2074
Michal Vaskof03ed032020-03-04 13:31:44 +01002075 /* check for keyless list and update its hash */
2076 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002077 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002078 lyd_hash(iter);
2079 }
2080 }
2081
2082 node->parent = NULL;
2083 }
2084
2085 node->next = NULL;
2086 node->prev = node;
2087}
2088
Michal Vaskoa5da3292020-08-12 13:10:50 +02002089void
Radek Krejci1798aae2020-07-14 13:26:06 +02002090lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2091{
2092 struct lyd_meta *last, *iter;
2093
2094 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002095
2096 if (!meta) {
2097 return;
2098 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002099
2100 for (iter = meta; iter; iter = iter->next) {
2101 iter->parent = parent;
2102 }
2103
2104 /* insert as the last attribute */
2105 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002106 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002107 last->next = meta;
2108 } else {
2109 parent->meta = meta;
2110 }
2111
2112 /* remove default flags from NP containers */
2113 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2114 parent->flags &= ~LYD_DEFAULT;
2115 parent = (struct lyd_node *)parent->parent;
2116 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002117}
2118
2119LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002120lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +02002121 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LY_PREFIX_FORMAT format,
2122 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002123{
2124 LY_ERR ret;
2125 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002126 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002127 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002128
Michal Vasko9f96a052020-03-10 09:41:45 +01002129 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002130
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002131 LY_ARRAY_FOR(mod->compiled->exts, u) {
2132 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2133 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002134 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002135 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002136 break;
2137 }
2138 }
2139 if (!ant) {
2140 /* attribute is not defined as a metadata annotation (RFC 7952) */
2141 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2142 mod->name, name_len, name);
2143 return LY_EINVAL;
2144 }
2145
Michal Vasko9f96a052020-03-10 09:41:45 +01002146 mt = calloc(1, sizeof *mt);
2147 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2148 mt->parent = parent;
2149 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002150 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, value_hint, format, prefix_data, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01002151 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002152 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002153 return ret;
2154 }
Michal Vasko9f96a052020-03-10 09:41:45 +01002155 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01002156
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002157 /* insert as the last attribute */
2158 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002159 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002160 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002161 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002162 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002163 }
2164
Michal Vasko9f96a052020-03-10 09:41:45 +01002165 if (meta) {
2166 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002167 }
2168 return ret;
2169}
2170
Michal Vaskoa5da3292020-08-12 13:10:50 +02002171void
2172lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2173{
2174 struct lyd_attr *last, *iter;
2175 struct lyd_node_opaq *opaq;
2176
2177 assert(parent && !parent->schema);
2178
2179 if (!attr) {
2180 return;
2181 }
2182
2183 opaq = (struct lyd_node_opaq *)parent;
2184 for (iter = attr; iter; iter = iter->next) {
2185 iter->parent = opaq;
2186 }
2187
2188 /* insert as the last attribute */
2189 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002190 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002191 last->next = attr;
2192 } else {
2193 opaq->attr = attr;
2194 }
2195}
2196
Michal Vasko52927e22020-03-16 17:26:14 +01002197LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002198lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +02002199 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
2200 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 +01002201{
Radek Krejci1798aae2020-07-14 13:26:06 +02002202 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002203
2204 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2205 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002206
2207 if (!value_len) {
2208 value = "";
2209 }
2210
2211 at = calloc(1, sizeof *at);
2212 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
2213 at->parent = (struct lyd_node_opaq *)parent;
2214 at->name = lydict_insert(ctx, name, name_len);
2215 if (dynamic && *dynamic) {
2216 at->value = lydict_insert_zc(ctx, (char *)value);
2217 *dynamic = 0;
2218 } else {
2219 at->value = lydict_insert(ctx, value, value_len);
2220 }
2221
Radek Krejci1798aae2020-07-14 13:26:06 +02002222 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002223 at->format = format;
2224 at->val_prefs = val_prefs;
Radek Krejci1798aae2020-07-14 13:26:06 +02002225 if (prefix_len) {
2226 at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
2227 }
2228 if (module_key_len) {
2229 at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002230 }
2231
2232 /* insert as the last attribute */
2233 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002234 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002235 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002236 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002237 last->next = at;
2238 }
2239
2240 if (attr) {
2241 *attr = at;
2242 }
2243 return LY_SUCCESS;
2244}
2245
Radek Krejci084289f2019-07-09 17:35:30 +02002246API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002247lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002248{
Michal Vasko004d3152020-06-11 19:59:22 +02002249 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002250
Michal Vasko004d3152020-06-11 19:59:22 +02002251 if (ly_path_eval(path, tree, &target)) {
2252 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002253 }
2254
Michal Vasko004d3152020-06-11 19:59:22 +02002255 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002256}
2257
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002258API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002259lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002260{
2261 const struct lyd_node *iter1, *iter2;
2262 struct lyd_node_term *term1, *term2;
2263 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002264 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002265 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002266
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002267 if (!node1 || !node2) {
2268 if (node1 == node2) {
2269 return LY_SUCCESS;
2270 } else {
2271 return LY_ENOT;
2272 }
2273 }
2274
Michal Vaskob7be7a82020-08-20 09:09:04 +02002275 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002276 return LY_ENOT;
2277 }
2278
2279 if (node1->hash != node2->hash) {
2280 return LY_ENOT;
2281 }
Michal Vasko52927e22020-03-16 17:26:14 +01002282 /* 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 +02002283
Michal Vasko52927e22020-03-16 17:26:14 +01002284 if (!node1->schema) {
2285 opaq1 = (struct lyd_node_opaq *)node1;
2286 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002287 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002288 return LY_ENOT;
2289 }
Michal Vasko52927e22020-03-16 17:26:14 +01002290 switch (opaq1->format) {
2291 case LYD_XML:
2292 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2293 return LY_ENOT;
2294 }
2295 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002296 case LYD_JSON:
2297 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2298 if (strcmp(opaq1->value, opaq2->value)) {
2299 return LY_ENOT;
2300 }
2301 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002302 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002303 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002304 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002305 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002306 return LY_EINT;
2307 }
2308 if (options & LYD_COMPARE_FULL_RECURSION) {
2309 iter1 = opaq1->child;
2310 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002311 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002312 }
2313 return LY_SUCCESS;
2314 } else {
2315 switch (node1->schema->nodetype) {
2316 case LYS_LEAF:
2317 case LYS_LEAFLIST:
2318 if (options & LYD_COMPARE_DEFAULTS) {
2319 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2320 return LY_ENOT;
2321 }
2322 }
2323
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002324 term1 = (struct lyd_node_term *)node1;
2325 term2 = (struct lyd_node_term *)node2;
2326 if (term1->value.realtype != term2->value.realtype) {
2327 return LY_ENOT;
2328 }
Michal Vasko52927e22020-03-16 17:26:14 +01002329
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002330 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002331 case LYS_CONTAINER:
2332 if (options & LYD_COMPARE_DEFAULTS) {
2333 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2334 return LY_ENOT;
2335 }
2336 }
2337 if (options & LYD_COMPARE_FULL_RECURSION) {
2338 iter1 = ((struct lyd_node_inner*)node1)->child;
2339 iter2 = ((struct lyd_node_inner*)node2)->child;
2340 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002341 }
2342 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002343 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002344 case LYS_ACTION:
2345 if (options & LYD_COMPARE_FULL_RECURSION) {
2346 /* TODO action/RPC
2347 goto all_children_compare;
2348 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002349 }
2350 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002351 case LYS_NOTIF:
2352 if (options & LYD_COMPARE_FULL_RECURSION) {
2353 /* TODO Notification
2354 goto all_children_compare;
2355 */
2356 }
2357 return LY_SUCCESS;
2358 case LYS_LIST:
2359 iter1 = ((struct lyd_node_inner*)node1)->child;
2360 iter2 = ((struct lyd_node_inner*)node2)->child;
2361
2362 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2363 /* lists with keys, their equivalence is based on their keys */
2364 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002365 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002366 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002367 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002368 return LY_ENOT;
2369 }
2370 iter1 = iter1->next;
2371 iter2 = iter2->next;
2372 }
2373 } else {
2374 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2375
Radek Krejci0f969882020-08-21 16:56:47 +02002376all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002377 if (!iter1 && !iter2) {
2378 /* no children, nothing to compare */
2379 return LY_SUCCESS;
2380 }
2381
2382 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002383 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002384 return LY_ENOT;
2385 }
2386 }
2387 if (iter1 || iter2) {
2388 return LY_ENOT;
2389 }
2390 }
2391 return LY_SUCCESS;
2392 case LYS_ANYXML:
2393 case LYS_ANYDATA:
2394 any1 = (struct lyd_node_any*)node1;
2395 any2 = (struct lyd_node_any*)node2;
2396
2397 if (any1->value_type != any2->value_type) {
2398 return LY_ENOT;
2399 }
2400 switch (any1->value_type) {
2401 case LYD_ANYDATA_DATATREE:
2402 iter1 = any1->value.tree;
2403 iter2 = any2->value.tree;
2404 goto all_children_compare;
2405 case LYD_ANYDATA_STRING:
2406 case LYD_ANYDATA_XML:
2407 case LYD_ANYDATA_JSON:
2408 len1 = strlen(any1->value.str);
2409 len2 = strlen(any2->value.str);
2410 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2411 return LY_ENOT;
2412 }
2413 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002414 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002415 len1 = lyd_lyb_data_length(any1->value.mem);
2416 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002417 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2418 return LY_ENOT;
2419 }
2420 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002421 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002422 }
2423 }
2424
Michal Vaskob7be7a82020-08-20 09:09:04 +02002425 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002426 return LY_EINT;
2427}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002428
Michal Vasko21725742020-06-29 11:49:25 +02002429API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002430lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2431{
2432 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2433 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2434 }
2435
Michal Vasko11a81432020-07-28 16:31:29 +02002436 if (node1 == node2) {
2437 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002438 }
Michal Vasko11a81432020-07-28 16:31:29 +02002439 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002440}
2441
2442API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002443lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2444{
2445 if (!meta1 || !meta2) {
2446 if (meta1 == meta2) {
2447 return LY_SUCCESS;
2448 } else {
2449 return LY_ENOT;
2450 }
2451 }
2452
Michal Vaskob7be7a82020-08-20 09:09:04 +02002453 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002454 return LY_ENOT;
2455 }
2456
2457 if (meta1->value.realtype != meta2->value.realtype) {
2458 return LY_ENOT;
2459 }
2460
2461 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2462}
2463
Radek Krejci22ebdba2019-07-25 13:59:43 +02002464/**
Michal Vasko52927e22020-03-16 17:26:14 +01002465 * @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 +02002466 *
2467 * 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 +02002468 *
2469 * @param[in] node Original node to duplicate
2470 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2471 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2472 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2473 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2474 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002475 */
Michal Vasko52927e22020-03-16 17:26:14 +01002476static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002477lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
Radek Krejci0f969882020-08-21 16:56:47 +02002478 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002479{
Michal Vasko52927e22020-03-16 17:26:14 +01002480 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002481 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002482 struct lyd_meta *meta;
2483 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002484 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002485
Michal Vasko52927e22020-03-16 17:26:14 +01002486 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002487
Michal Vasko52927e22020-03-16 17:26:14 +01002488 if (!node->schema) {
2489 dup = calloc(1, sizeof(struct lyd_node_opaq));
2490 } else {
2491 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002492 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002493 case LYS_ACTION:
2494 case LYS_NOTIF:
2495 case LYS_CONTAINER:
2496 case LYS_LIST:
2497 dup = calloc(1, sizeof(struct lyd_node_inner));
2498 break;
2499 case LYS_LEAF:
2500 case LYS_LEAFLIST:
2501 dup = calloc(1, sizeof(struct lyd_node_term));
2502 break;
2503 case LYS_ANYDATA:
2504 case LYS_ANYXML:
2505 dup = calloc(1, sizeof(struct lyd_node_any));
2506 break;
2507 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002508 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002509 ret = LY_EINT;
2510 goto error;
2511 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002512 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002513 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002514
Michal Vaskof6df0a02020-06-16 13:08:34 +02002515 if (options & LYD_DUP_WITH_FLAGS) {
2516 dup->flags = node->flags;
2517 } else {
2518 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2519 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002520 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002521 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002522
Michal Vasko25a32822020-07-09 15:48:22 +02002523 /* duplicate metadata */
2524 if (!(options & LYD_DUP_NO_META)) {
2525 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002526 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002527 }
2528 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002529
2530 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002531 if (!dup->schema) {
2532 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2533 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2534 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002535
2536 if (options & LYD_DUP_RECURSIVE) {
2537 /* duplicate all the children */
2538 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002539 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002540 }
2541 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002542 opaq->name = lydict_insert(LYD_CTX(node), orig->name, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002543 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002544 if (orig->prefix.id) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002545 opaq->prefix.id = lydict_insert(LYD_CTX(node), orig->prefix.id, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002546 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002547 opaq->prefix.module_ns = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002548 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002549 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 +01002550 LY_ARRAY_FOR(orig->val_prefs, u) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002551 opaq->val_prefs[u].id = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0);
2552 opaq->val_prefs[u].module_ns = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002553 LY_ARRAY_INCREMENT(opaq->val_prefs);
2554 }
2555 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002556 opaq->value = lydict_insert(LYD_CTX(node), orig->value, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002557 opaq->ctx = orig->ctx;
2558 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2559 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2560 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2561
2562 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002563 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
2564 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002565 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2566 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2567 struct lyd_node *child;
2568
2569 if (options & LYD_DUP_RECURSIVE) {
2570 /* duplicate all the children */
2571 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002572 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002573 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002574 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002575 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002576 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002577 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002578 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002579 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002580 if (!child) {
2581 /* possibly not keys are present in filtered tree */
2582 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002583 } else if (child->schema != key) {
2584 /* possibly not all keys are present in filtered tree,
2585 * but there can be also some non-key nodes */
2586 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002587 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002588 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002589 child = child->next;
2590 }
2591 }
2592 lyd_hash(dup);
2593 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002594 dup->hash = node->hash;
2595 any = (struct lyd_node_any *)node;
2596 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002597 }
2598
Michal Vasko52927e22020-03-16 17:26:14 +01002599 /* insert */
2600 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002601
2602 if (dup_p) {
2603 *dup_p = dup;
2604 }
2605 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002606
2607error:
Michal Vasko52927e22020-03-16 17:26:14 +01002608 lyd_free_tree(dup);
2609 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002610}
2611
Michal Vasko3a41dff2020-07-15 14:30:28 +02002612static LY_ERR
2613lyd_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 +02002614 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002615{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002616 const struct lyd_node_inner *orig_parent, *iter;
2617 int repeat = 1;
2618
2619 *dup_parent = NULL;
2620 *local_parent = NULL;
2621
2622 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2623 if (parent && (parent->schema == orig_parent->schema)) {
2624 /* stop creating parents, connect what we have into the provided parent */
2625 iter = parent;
2626 repeat = 0;
2627 } else {
2628 iter = NULL;
2629 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002630 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002631 }
2632 if (!*local_parent) {
2633 *local_parent = (struct lyd_node_inner *)iter;
2634 }
2635 if (iter->child) {
2636 /* 1) list - add after keys
2637 * 2) provided parent with some children */
2638 iter->child->prev->next = *dup_parent;
2639 if (*dup_parent) {
2640 (*dup_parent)->prev = iter->child->prev;
2641 iter->child->prev = *dup_parent;
2642 }
2643 } else {
2644 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2645 }
2646 if (*dup_parent) {
2647 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2648 }
2649 *dup_parent = (struct lyd_node *)iter;
2650 }
2651
2652 if (repeat && parent) {
2653 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002654 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko3a41dff2020-07-15 14:30:28 +02002655 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2656 return LY_EINVAL;
2657 }
2658
2659 return LY_SUCCESS;
2660}
2661
2662static LY_ERR
2663lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2664{
2665 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002666 const struct lyd_node *orig; /* original node to be duplicated */
2667 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002668 struct lyd_node *top = NULL; /* the most higher created node */
2669 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002670
Michal Vasko3a41dff2020-07-15 14:30:28 +02002671 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002672
2673 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002674 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002675 } else {
2676 local_parent = parent;
2677 }
2678
Radek Krejci22ebdba2019-07-25 13:59:43 +02002679 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002680 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002681 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2682 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002683 break;
2684 }
2685 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002686
2687 /* rehash if needed */
2688 for (; local_parent; local_parent = local_parent->parent) {
2689 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2690 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002691 }
2692 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002693
2694 if (dup) {
2695 *dup = first;
2696 }
2697 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002698
2699error:
2700 if (top) {
2701 lyd_free_tree(top);
2702 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002703 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002704 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002705 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002706}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002707
Michal Vasko3a41dff2020-07-15 14:30:28 +02002708API LY_ERR
2709lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2710{
2711 return lyd_dup(node, parent, options, 1, dup);
2712}
2713
2714API LY_ERR
2715lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2716{
2717 return lyd_dup(node, parent, options, 0, dup);
2718}
2719
2720API LY_ERR
2721lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002722{
2723 LY_ERR ret;
2724 struct lyd_meta *mt, *last;
2725
Michal Vasko3a41dff2020-07-15 14:30:28 +02002726 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002727
2728 /* create a copy */
2729 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002730 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002731 mt->parent = node;
2732 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002733 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
2734 LY_CHECK_ERR_RET(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), ret);
2735 mt->name = lydict_insert(LYD_CTX(node), meta->name, 0);
Michal Vasko25a32822020-07-09 15:48:22 +02002736
2737 /* insert as the last attribute */
2738 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002739 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002740 last->next = mt;
2741 } else {
2742 node->meta = mt;
2743 }
2744
Michal Vasko3a41dff2020-07-15 14:30:28 +02002745 if (dup) {
2746 *dup = mt;
2747 }
2748 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002749}
2750
Michal Vasko4490d312020-06-16 13:08:55 +02002751/**
2752 * @brief Merge a source sibling into target siblings.
2753 *
2754 * @param[in,out] first_trg First target sibling, is updated if top-level.
2755 * @param[in] parent_trg Target parent.
2756 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2757 * @param[in] options Merge options.
2758 * @return LY_ERR value.
2759 */
2760static LY_ERR
2761lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002762 int options)
Michal Vasko4490d312020-06-16 13:08:55 +02002763{
2764 LY_ERR ret;
2765 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002766 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002767 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002768
2769 sibling_src = *sibling_src_p;
2770 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2771 /* try to find the exact instance */
2772 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2773 } else {
2774 /* try to simply find the node, there cannot be more instances */
2775 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2776 }
2777
2778 if (!ret) {
2779 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002780 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002781 /* since they are different, they cannot both be default */
2782 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2783
Michal Vasko3a41dff2020-07-15 14:30:28 +02002784 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2785 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002786 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002787 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2788 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko4490d312020-06-16 13:08:55 +02002789 &((struct lyd_node_term *)match_trg)->value));
2790
2791 /* copy flags and add LYD_NEW */
2792 match_trg->flags = sibling_src->flags | LYD_NEW;
2793 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002794 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002795 /* update value */
2796 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002797 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002798
2799 /* copy flags and add LYD_NEW */
2800 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002801 } else {
2802 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002803 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002804 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2805 }
2806 }
2807 } else {
2808 /* node not found, merge it */
2809 if (options & LYD_MERGE_DESTRUCT) {
2810 dup_src = (struct lyd_node *)sibling_src;
2811 lyd_unlink_tree(dup_src);
2812 /* spend it */
2813 *sibling_src_p = NULL;
2814 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002815 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 +02002816 }
2817
2818 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002819 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002820 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002821 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002822 }
2823
2824 lyd_insert_node(parent_trg, first_trg, dup_src);
2825 }
2826
2827 return LY_SUCCESS;
2828}
2829
Michal Vasko3a41dff2020-07-15 14:30:28 +02002830static LY_ERR
2831lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002832{
2833 const struct lyd_node *sibling_src, *tmp;
2834 int first;
2835
2836 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2837
2838 if (!source) {
2839 /* nothing to merge */
2840 return LY_SUCCESS;
2841 }
2842
2843 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002844 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002845 return LY_EINVAL;
2846 }
2847
2848 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2849 first = sibling_src == source ? 1 : 0;
2850 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2851 if (first && !sibling_src) {
2852 /* source was spent (unlinked), move to the next node */
2853 source = tmp;
2854 }
2855
Michal Vasko3a41dff2020-07-15 14:30:28 +02002856 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002857 break;
2858 }
2859 }
2860
2861 if (options & LYD_MERGE_DESTRUCT) {
2862 /* free any leftover source data that were not merged */
2863 lyd_free_siblings((struct lyd_node *)source);
2864 }
2865
2866 return LY_SUCCESS;
2867}
2868
Michal Vasko3a41dff2020-07-15 14:30:28 +02002869API LY_ERR
2870lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2871{
2872 return lyd_merge(target, source, options, 1);
2873}
2874
2875API LY_ERR
2876lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2877{
2878 return lyd_merge(target, source, options, 0);
2879}
2880
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002881static LY_ERR
2882lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2883{
Michal Vasko14654712020-02-06 08:35:21 +01002884 /* ending \0 */
2885 ++reqlen;
2886
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002887 if (reqlen > *buflen) {
2888 if (is_static) {
2889 return LY_EINCOMPLETE;
2890 }
2891
2892 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2893 if (!*buffer) {
2894 return LY_EMEM;
2895 }
2896
2897 *buflen = reqlen;
2898 }
2899
2900 return LY_SUCCESS;
2901}
2902
Michal Vaskod59035b2020-07-08 12:00:06 +02002903LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002904lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2905{
2906 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002907 size_t len;
2908 const char *val;
2909 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002910
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002911 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002912 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002913 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002914 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002915
2916 quot = '\'';
2917 if (strchr(val, '\'')) {
2918 quot = '"';
2919 }
2920 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002921 }
2922
2923 return LY_SUCCESS;
2924}
2925
2926/**
2927 * @brief Append leaf-list value predicate to path.
2928 *
2929 * @param[in] node Node to print.
2930 * @param[in,out] buffer Buffer to print to.
2931 * @param[in,out] buflen Current buffer length.
2932 * @param[in,out] bufused Current number of characters used in @p buffer.
2933 * @param[in] is_static Whether buffer is static or can be reallocated.
2934 * @return LY_ERR
2935 */
2936static LY_ERR
2937lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2938{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002939 size_t len;
2940 const char *val;
2941 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002942
Michal Vaskob7be7a82020-08-20 09:09:04 +02002943 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002944 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002945 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002946
2947 quot = '\'';
2948 if (strchr(val, '\'')) {
2949 quot = '"';
2950 }
2951 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2952
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002953 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002954}
2955
2956/**
2957 * @brief Append node position (relative to its other instances) predicate to path.
2958 *
2959 * @param[in] node Node to print.
2960 * @param[in,out] buffer Buffer to print to.
2961 * @param[in,out] buflen Current buffer length.
2962 * @param[in,out] bufused Current number of characters used in @p buffer.
2963 * @param[in] is_static Whether buffer is static or can be reallocated.
2964 * @return LY_ERR
2965 */
2966static LY_ERR
2967lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2968{
2969 const struct lyd_node *first, *iter;
2970 size_t len;
2971 int pos;
2972 char *val = NULL;
2973 LY_ERR rc;
2974
2975 if (node->parent) {
2976 first = node->parent->child;
2977 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002978 for (first = node; node->prev->next; node = node->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002979 }
2980 pos = 1;
2981 for (iter = first; iter != node; iter = iter->next) {
2982 if (iter->schema == node->schema) {
2983 ++pos;
2984 }
2985 }
2986 if (asprintf(&val, "%d", pos) == -1) {
2987 return LY_EMEM;
2988 }
2989
2990 len = 1 + strlen(val) + 1;
2991 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2992 if (rc != LY_SUCCESS) {
2993 goto cleanup;
2994 }
2995
2996 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2997
2998cleanup:
2999 free(val);
3000 return rc;
3001}
3002
3003API char *
3004lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3005{
Michal Vasko14654712020-02-06 08:35:21 +01003006 int is_static = 0, i, depth;
3007 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003008 const struct lyd_node *iter;
3009 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003010 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003011
3012 LY_CHECK_ARG_RET(NULL, node, NULL);
3013 if (buffer) {
3014 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3015 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003016 } else {
3017 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003018 }
3019
3020 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003021 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003022 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003023 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003024 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3025 ++depth;
3026 }
3027
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003028 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003029 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003030 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003031 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003032iter_print:
3033 /* print prefix and name */
3034 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003035 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003036 mod = iter->schema->module;
3037 }
3038
3039 /* realloc string */
Radek Krejci1798aae2020-07-14 13:26:06 +02003040 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 +02003041 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3042 if (rc != LY_SUCCESS) {
3043 break;
3044 }
3045
3046 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003047 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
3048 iter->schema ? iter->schema->name : ((struct lyd_node_opaq*)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003049
Michal Vasko790b2bc2020-08-03 13:35:06 +02003050 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003051 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003052 switch (iter->schema->nodetype) {
3053 case LYS_LIST:
3054 if (iter->schema->flags & LYS_KEYLESS) {
3055 /* print its position */
3056 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3057 } else {
3058 /* print all list keys in predicates */
3059 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3060 }
3061 break;
3062 case LYS_LEAFLIST:
3063 if (iter->schema->flags & LYS_CONFIG_W) {
3064 /* print leaf-list value */
3065 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3066 } else {
3067 /* print its position */
3068 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3069 }
3070 break;
3071 default:
3072 /* nothing to print more */
3073 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003074 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003075 }
3076 if (rc != LY_SUCCESS) {
3077 break;
3078 }
3079
Michal Vasko14654712020-02-06 08:35:21 +01003080 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003081 }
3082 break;
3083 }
3084
3085 return buffer;
3086}
Michal Vaskoe444f752020-02-10 12:20:06 +01003087
Michal Vasko25a32822020-07-09 15:48:22 +02003088API struct lyd_meta *
3089lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3090{
3091 struct lyd_meta *ret = NULL;
3092 const struct ly_ctx *ctx;
3093 const char *prefix, *tmp;
3094 char *str;
3095 size_t pref_len, name_len;
3096
3097 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3098
3099 if (!first) {
3100 return NULL;
3101 }
3102
3103 ctx = first->annotation->module->ctx;
3104
3105 /* parse the name */
3106 tmp = name;
3107 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3108 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3109 return NULL;
3110 }
3111
3112 /* find the module */
3113 if (prefix) {
3114 str = strndup(prefix, pref_len);
3115 module = ly_ctx_get_module_latest(ctx, str);
3116 free(str);
3117 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3118 }
3119
3120 /* find the metadata */
3121 LY_LIST_FOR(first, first) {
3122 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3123 ret = (struct lyd_meta *)first;
3124 break;
3125 }
3126 }
3127
3128 return ret;
3129}
3130
Michal Vasko9b368d32020-02-14 13:53:31 +01003131API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003132lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3133{
3134 struct lyd_node **match_p;
3135 struct lyd_node_inner *parent;
3136
Michal Vaskof03ed032020-03-04 13:31:44 +01003137 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003138
Michal Vasko62ed12d2020-05-21 10:08:25 +02003139 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3140 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003141 if (match) {
3142 *match = NULL;
3143 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003144 return LY_ENOTFOUND;
3145 }
3146
3147 /* find first sibling */
3148 if (siblings->parent) {
3149 siblings = siblings->parent->child;
3150 } else {
3151 while (siblings->prev->next) {
3152 siblings = siblings->prev;
3153 }
3154 }
3155
3156 parent = (struct lyd_node_inner *)siblings->parent;
3157 if (parent && parent->children_ht) {
3158 assert(target->hash);
3159
3160 /* find by hash */
3161 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003162 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003163 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003164 siblings = *match_p;
3165 } else {
3166 siblings = NULL;
3167 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003168 } else {
3169 /* not found */
3170 siblings = NULL;
3171 }
3172 } else {
3173 /* no children hash table */
3174 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003175 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003176 break;
3177 }
3178 }
3179 }
3180
3181 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003182 if (match) {
3183 *match = NULL;
3184 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003185 return LY_ENOTFOUND;
3186 }
3187
Michal Vasko9b368d32020-02-14 13:53:31 +01003188 if (match) {
3189 *match = (struct lyd_node *)siblings;
3190 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003191 return LY_SUCCESS;
3192}
3193
Michal Vasko90932a92020-02-12 14:33:03 +01003194static int
3195lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3196{
3197 struct lysc_node *val1;
3198 struct lyd_node *val2;
3199
3200 val1 = *((struct lysc_node **)val1_p);
3201 val2 = *((struct lyd_node **)val2_p);
3202
Michal Vasko90932a92020-02-12 14:33:03 +01003203 if (val1 == val2->schema) {
3204 /* schema match is enough */
3205 return 1;
3206 } else {
3207 return 0;
3208 }
3209}
3210
3211static LY_ERR
3212lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3213{
3214 struct lyd_node **match_p;
3215 struct lyd_node_inner *parent;
3216 uint32_t hash;
3217 values_equal_cb ht_cb;
3218
Michal Vaskob104f112020-07-17 09:54:54 +02003219 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003220
3221 parent = (struct lyd_node_inner *)siblings->parent;
3222 if (parent && parent->children_ht) {
3223 /* calculate our hash */
3224 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3225 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3226 hash = dict_hash_multi(hash, NULL, 0);
3227
3228 /* use special hash table function */
3229 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3230
3231 /* find by hash */
3232 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3233 siblings = *match_p;
3234 } else {
3235 /* not found */
3236 siblings = NULL;
3237 }
3238
3239 /* set the original hash table compare function back */
3240 lyht_set_cb(parent->children_ht, ht_cb);
3241 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003242 /* find first sibling */
3243 if (siblings->parent) {
3244 siblings = siblings->parent->child;
3245 } else {
3246 while (siblings->prev->next) {
3247 siblings = siblings->prev;
3248 }
3249 }
3250
3251 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003252 for (; siblings; siblings = siblings->next) {
3253 if (siblings->schema == schema) {
3254 /* schema match is enough */
3255 break;
3256 }
3257 }
3258 }
3259
3260 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003261 if (match) {
3262 *match = NULL;
3263 }
Michal Vasko90932a92020-02-12 14:33:03 +01003264 return LY_ENOTFOUND;
3265 }
3266
Michal Vasko9b368d32020-02-14 13:53:31 +01003267 if (match) {
3268 *match = (struct lyd_node *)siblings;
3269 }
Michal Vasko90932a92020-02-12 14:33:03 +01003270 return LY_SUCCESS;
3271}
3272
Michal Vaskoe444f752020-02-10 12:20:06 +01003273API LY_ERR
3274lyd_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 +02003275 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003276{
3277 LY_ERR rc;
3278 struct lyd_node *target = NULL;
3279
Michal Vasko4c583e82020-07-17 12:16:14 +02003280 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003281
Michal Vasko62ed12d2020-05-21 10:08:25 +02003282 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3283 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003284 if (match) {
3285 *match = NULL;
3286 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003287 return LY_ENOTFOUND;
3288 }
3289
Michal Vaskof03ed032020-03-04 13:31:44 +01003290 if (key_or_value && !val_len) {
3291 val_len = strlen(key_or_value);
3292 }
3293
Michal Vaskob104f112020-07-17 09:54:54 +02003294 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3295 /* create a data node and find the instance */
3296 if (schema->nodetype == LYS_LEAFLIST) {
3297 /* target used attributes: schema, hash, value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003298 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, LY_PREF_JSON, NULL, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003299 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3300 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003301 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003302 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003303 }
3304
3305 /* find it */
3306 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003307 } else {
3308 /* find the first schema node instance */
3309 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003310 }
3311
Michal Vaskoe444f752020-02-10 12:20:06 +01003312 lyd_free_tree(target);
3313 return rc;
3314}
Michal Vaskoccc02342020-05-21 10:09:21 +02003315
3316API LY_ERR
3317lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3318{
3319 LY_ERR ret = LY_SUCCESS;
3320 struct lyxp_set xp_set;
3321 struct lyxp_expr *exp;
3322 uint32_t i;
3323
3324 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3325
3326 memset(&xp_set, 0, sizeof xp_set);
3327
3328 /* compile expression */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003329 exp = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003330 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3331
3332 /* evaluate expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003333 ret = lyxp_eval(exp, LY_PREF_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003334 LY_CHECK_GOTO(ret, cleanup);
3335
3336 /* allocate return set */
3337 *set = ly_set_new();
Michal Vaskob7be7a82020-08-20 09:09:04 +02003338 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003339
3340 /* transform into ly_set */
3341 if (xp_set.type == LYXP_SET_NODE_SET) {
3342 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3343 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003344 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003345 (*set)->size = xp_set.used;
3346
3347 for (i = 0; i < xp_set.used; ++i) {
3348 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3349 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3350 }
3351 }
3352 }
3353
3354cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003355 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003356 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003357 return ret;
3358}