blob: f9887dd63de9406ef93eeac7a7e65dca06a78d1d [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 "config.h"
33#include "context.h"
34#include "dict.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"
49#include "xml.h"
50#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020051
Michal Vaskob104f112020-07-17 09:54:54 +020052static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
53 struct lyd_node **match);
54
Radek Krejci084289f2019-07-09 17:35:30 +020055LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010056lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010057 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020058{
Michal Vasko90932a92020-02-12 14:33:03 +010059 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020060 struct ly_err_item *err = NULL;
61 struct ly_ctx *ctx;
62 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020063 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010064 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020065 assert(node);
66
67 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020068
Radek Krejci73dead22019-07-11 16:46:16 +020069 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020070 if (!second) {
71 node->value.realtype = type;
72 }
Michal Vasko90932a92020-02-12 14:33:03 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020074 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010075 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020076 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020077 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020078 if (!node->parent && lysc_data_parent(node->schema)) {
79 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
80 } else {
81 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
82 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020084 }
Radek Krejci73dead22019-07-11 16:46:16 +020085 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010086 } else if (dynamic) {
87 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020088 }
89
90error:
91 return ret;
92}
93
Michal Vasko00cbf532020-06-15 13:58:47 +020094/* 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 +020095LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010096lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
97 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
98{
99 LY_ERR ret = LY_SUCCESS;
100 struct ly_err_item *err = NULL;
101 struct ly_ctx *ctx;
102 struct lysc_type *type;
103 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
104
105 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
106
107 ctx = schema->module->ctx;
108 type = ((struct lysc_node_leaf *)schema)->type;
109 val->realtype = type;
110 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
111 val, NULL, &err);
112 if (ret == LY_EINCOMPLETE) {
113 /* this is fine, we do not need it resolved */
114 ret = LY_SUCCESS;
115 } else if (ret && err) {
116 ly_err_print(err);
117 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
118 ly_err_free(err);
119 }
120 if (!ret && dynamic) {
121 *dynamic = 0;
122 }
123
124 return ret;
125}
126
Radek Krejci38d85362019-09-05 16:26:38 +0200127LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200128lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100129 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100130 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200131{
Michal Vasko90932a92020-02-12 14:33:03 +0100132 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200133 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200134 struct lyext_metadata *ant;
135 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100136 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200137
Michal Vasko9f96a052020-03-10 09:41:45 +0100138 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100139
Michal Vasko9f96a052020-03-10 09:41:45 +0100140 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200141
142 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100143 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200144 }
Michal Vasko90932a92020-02-12 14:33:03 +0100145 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100146 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100147 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200148 if (err) {
149 ly_err_print(err);
150 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
151 ly_err_free(err);
152 }
153 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100154 } else if (dynamic) {
155 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200156 }
157
158error:
159 return ret;
160}
161
Michal Vaskof937cfe2020-08-03 16:07:12 +0200162LY_ERR
163_lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
164 ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format)
Radek Krejci084289f2019-07-09 17:35:30 +0200165{
166 LY_ERR rc = LY_SUCCESS;
167 struct ly_err_item *err = NULL;
168 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200169
170 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
171
172 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
173 LOGARG(ctx, node);
174 return LY_EINVAL;
175 }
176
177 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200178 /* just validate, no storing of enything */
179 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
Michal Vaskof937cfe2020-08-03 16:07:12 +0200180 resolve_prefix, prefix_data, format, node, NULL, NULL, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200181 if (rc == LY_EINCOMPLETE) {
182 /* actually success since we do not provide the context tree and call validation with
183 * LY_TYPE_OPTS_INCOMPLETE_DATA */
184 rc = LY_SUCCESS;
185 } else if (rc && err) {
186 if (ctx) {
187 /* log only in case the ctx was provided as input parameter */
188 ly_err_print(err);
189 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200190 }
Radek Krejci73dead22019-07-11 16:46:16 +0200191 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200192 }
193
194 return rc;
195}
196
197API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200198lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
199{
200 return _lys_value_validate(ctx, node, value, value_len, lydjson_resolve_prefix, NULL, LYD_JSON);
201}
202
203API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100204lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vasko3701af52020-08-03 14:29:38 +0200205 const struct lyd_node *tree, struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200206{
207 LY_ERR rc;
208 struct ly_err_item *err = NULL;
209 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200210 struct lyd_value val = {0};
211 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA) | (realtype ? LY_TYPE_OPTS_STORE : 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200212
213 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
214
215 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200216 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof937cfe2020-08-03 16:07:12 +0200217 lydjson_resolve_prefix, NULL, LYD_JSON, tree ? (void*)node : (void*)node->schema, tree,
Michal Vasko3701af52020-08-03 14:29:38 +0200218 &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200219 if (rc == LY_EINCOMPLETE) {
220 return rc;
221 } else if (rc) {
222 if (err) {
223 if (ctx) {
224 ly_err_print(err);
225 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200226 }
Radek Krejci73dead22019-07-11 16:46:16 +0200227 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200228 }
Radek Krejci73dead22019-07-11 16:46:16 +0200229 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200230 }
231
Michal Vasko3701af52020-08-03 14:29:38 +0200232 if (realtype) {
233 *realtype = val.realtype;
234 }
235
236 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200237 return LY_SUCCESS;
238}
239
240API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200241lyd_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 +0200242{
243 LY_ERR ret = LY_SUCCESS, rc;
244 struct ly_err_item *err = NULL;
245 struct ly_ctx *ctx;
246 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200247 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100248 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200249
250 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
251
252 ctx = node->schema->module->ctx;
253 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskof937cfe2020-08-03 16:07:12 +0200254 rc = type->plugin->store(ctx, type, value, value_len, options, lydjson_resolve_prefix, NULL, LYD_JSON,
255 (struct lyd_node *)node, tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200256 if (rc == LY_EINCOMPLETE) {
257 ret = rc;
258 /* continue with comparing, just remember what to return if storing is ok */
259 } else if (rc) {
260 /* value to compare is invalid */
261 ret = LY_EINVAL;
262 if (err) {
263 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200264 }
Radek Krejci73dead22019-07-11 16:46:16 +0200265 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200266 }
267
268 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200269 if (type->plugin->compare(&node->value, &data)) {
270 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200271 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200272 }
Radek Krejci084289f2019-07-09 17:35:30 +0200273
274cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200275 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200276
277 return ret;
278}
279
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200280API const char *
281lyd_value2str(const struct lyd_node_term *node, int *dynamic)
282{
283 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
284
285 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
286}
287
288API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100289lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200290{
Michal Vasko9f96a052020-03-10 09:41:45 +0100291 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200292
Michal Vasko9f96a052020-03-10 09:41:45 +0100293 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200294}
295
Radek Krejci7931b192020-06-25 17:05:03 +0200296static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200297lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200298{
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejci7931b192020-06-25 17:05:03 +0200300 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200301 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200302 const char *path = in->method.fpath.filepath;
303 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200304
305 /* ignore trailing whitespaces */
306 for (; len > 0 && isspace(path[len - 1]); len--);
307
308 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
309 format = LYD_XML;
310#if 0
311 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
312 format = LYD_JSON;
Radek Krejci7931b192020-06-25 17:05:03 +0200313#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200314 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
315 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200316 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 }
318
Radek Krejci7931b192020-06-25 17:05:03 +0200319 return format;
320}
Radek Krejcie7b95092019-05-15 11:03:07 +0200321
Radek Krejci7931b192020-06-25 17:05:03 +0200322API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200323lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
324 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200325{
326 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
327 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
328 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
329
330 format = lyd_parse_get_format(in, format);
331 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
332
Michal Vasko63f3d842020-07-08 10:10:14 +0200333 /* remember input position */
334 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200335
336 switch (format) {
337 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200338 return lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200339#if 0
340 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200341 return lyd_parse_json_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200342#endif
343 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200344 return lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200345 case LYD_SCHEMA:
346 LOGINT_RET(ctx);
347 }
348
349 /* TODO move here the top-level validation from parser_xml.c's lyd_parse_xml_data() and make
350 * it common for all the lyd_parse_*_data() functions */
351
352 LOGINT_RET(ctx);
353}
354
355API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200356lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
357 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200358{
359 LY_ERR ret;
360 struct ly_in *in;
361
362 LY_CHECK_RET(ly_in_new_memory(data, &in));
363 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
364
365 ly_in_free(in, 0);
366 return ret;
367}
368
369API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200370lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
371 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200372{
373 LY_ERR ret;
374 struct ly_in *in;
375
376 LY_CHECK_RET(ly_in_new_fd(fd, &in));
377 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
378
379 ly_in_free(in, 0);
380 return ret;
381}
382
383API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200384lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
385 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200386{
387 LY_ERR ret;
388 struct ly_in *in;
389
390 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
391 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
392
393 ly_in_free(in, 0);
394 return ret;
395}
396
Radek Krejci7931b192020-06-25 17:05:03 +0200397API LY_ERR
398lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
399{
400 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
401
402 format = lyd_parse_get_format(in, format);
403 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
404
Michal Vasko63f3d842020-07-08 10:10:14 +0200405 /* remember input position */
406 in->func_start = in->current;
407
Radek Krejci7931b192020-06-25 17:05:03 +0200408 switch (format) {
409 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200410 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200411#if 0
412 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200413 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200414#endif
415 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200416 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200417 case LYD_SCHEMA:
418 LOGINT_RET(ctx);
419 }
420
421 LOGINT_RET(ctx);
422}
423
424API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200425lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
426 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200427{
428 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
429 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
430
431 format = lyd_parse_get_format(in, format);
432 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
433
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 /* remember input position */
435 in->func_start = in->current;
436
Radek Krejci7931b192020-06-25 17:05:03 +0200437 switch (format) {
438 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200440#if 0
441 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200443#endif
444 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200446 case LYD_SCHEMA:
447 LOGINT_RET(LYD_NODE_CTX(request));
448 }
449
450 LOGINT_RET(LYD_NODE_CTX(request));
451}
452
453API LY_ERR
454lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
455{
456 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
457
458 format = lyd_parse_get_format(in, format);
459 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
460
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 /* remember input position */
462 in->func_start = in->current;
463
Radek Krejci7931b192020-06-25 17:05:03 +0200464 switch (format) {
465 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200467#if 0
468 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200470#endif
471 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200472 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200473 case LYD_SCHEMA:
474 LOGINT_RET(ctx);
475 }
476
477 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200478}
Radek Krejci084289f2019-07-09 17:35:30 +0200479
Michal Vasko90932a92020-02-12 14:33:03 +0100480LY_ERR
481lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
482 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
483{
484 LY_ERR ret;
485 struct lyd_node_term *term;
486
Michal Vasko9b368d32020-02-14 13:53:31 +0100487 assert(schema->nodetype & LYD_NODE_TERM);
488
Michal Vasko90932a92020-02-12 14:33:03 +0100489 term = calloc(1, sizeof *term);
490 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
491
492 term->schema = schema;
493 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100494 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100495
496 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
497 if (ret && (ret != LY_EINCOMPLETE)) {
498 free(term);
499 return ret;
500 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100501 lyd_hash((struct lyd_node *)term);
502
503 *node = (struct lyd_node *)term;
504 return ret;
505}
506
507LY_ERR
508lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
509{
510 LY_ERR ret;
511 struct lyd_node_term *term;
512 struct lysc_type *type;
513
514 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200515 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100516
517 term = calloc(1, sizeof *term);
518 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
519
520 term->schema = schema;
521 term->prev = (struct lyd_node *)term;
522 term->flags = LYD_NEW;
523
524 type = ((struct lysc_node_leaf *)schema)->type;
525 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
526 if (ret) {
527 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
528 free(term);
529 return ret;
530 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200531 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100532 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100533
534 *node = (struct lyd_node *)term;
535 return ret;
536}
537
538LY_ERR
539lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
540{
541 struct lyd_node_inner *in;
542
Michal Vasko9b368d32020-02-14 13:53:31 +0100543 assert(schema->nodetype & LYD_NODE_INNER);
544
Michal Vasko90932a92020-02-12 14:33:03 +0100545 in = calloc(1, sizeof *in);
546 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
547
548 in->schema = schema;
549 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100550 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100551
Michal Vasko9b368d32020-02-14 13:53:31 +0100552 /* do not hash list with keys, we need them for the hash */
553 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
554 lyd_hash((struct lyd_node *)in);
555 }
Michal Vasko90932a92020-02-12 14:33:03 +0100556
557 *node = (struct lyd_node *)in;
558 return LY_SUCCESS;
559}
560
Michal Vasko90932a92020-02-12 14:33:03 +0100561LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200562lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100563{
564 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100565 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200566 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100567
Michal Vasko004d3152020-06-11 19:59:22 +0200568 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100569
570 /* create list */
571 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
572
Michal Vasko90932a92020-02-12 14:33:03 +0100573 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200574 LY_ARRAY_FOR(predicates, u) {
575 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100576 lyd_insert_node(list, NULL, key);
577 }
578
Michal Vasko9b368d32020-02-14 13:53:31 +0100579 /* hash having all the keys */
580 lyd_hash(list);
581
Michal Vasko90932a92020-02-12 14:33:03 +0100582 /* success */
583 *node = list;
584 list = NULL;
585
586cleanup:
587 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200588 return ret;
589}
590
591static LY_ERR
592lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
593{
594 LY_ERR ret = LY_SUCCESS;
595 struct lyxp_expr *expr = NULL;
596 uint16_t exp_idx = 0;
597 enum ly_path_pred_type pred_type = 0;
598 struct ly_path_predicate *predicates = NULL;
599
600 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200601 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 +0200602 LY_PATH_PRED_KEYS, &expr), cleanup);
603
604 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200605 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
606 lydjson_resolve_prefix, NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200607
608 /* create the list node */
609 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
610
611cleanup:
612 lyxp_expr_free(schema->module->ctx, expr);
613 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100614 return ret;
615}
616
617LY_ERR
618lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
619{
620 struct lyd_node_any *any;
621
Michal Vasko9b368d32020-02-14 13:53:31 +0100622 assert(schema->nodetype & LYD_NODE_ANY);
623
Michal Vasko90932a92020-02-12 14:33:03 +0100624 any = calloc(1, sizeof *any);
625 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
626
627 any->schema = schema;
628 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100629 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100630
631 any->value.xml = value;
632 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100633 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100634
635 *node = (struct lyd_node *)any;
636 return LY_SUCCESS;
637}
638
Michal Vasko52927e22020-03-16 17:26:14 +0100639LY_ERR
640lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
641 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
642 const char *ns, struct lyd_node **node)
643{
644 struct lyd_node_opaq *opaq;
645
646 assert(ctx && name && name_len && ns);
647
648 if (!value_len) {
649 value = "";
650 }
651
652 opaq = calloc(1, sizeof *opaq);
653 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
654
655 opaq->prev = (struct lyd_node *)opaq;
656
657 opaq->name = lydict_insert(ctx, name, name_len);
658 opaq->format = format;
659 if (pref_len) {
660 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
661 }
662 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
663 opaq->val_prefs = val_prefs;
664 if (dynamic && *dynamic) {
665 opaq->value = lydict_insert_zc(ctx, (char *)value);
666 *dynamic = 0;
667 } else {
668 opaq->value = lydict_insert(ctx, value, value_len);
669 }
670 opaq->ctx = ctx;
671
672 *node = (struct lyd_node *)opaq;
673 return LY_SUCCESS;
674}
675
Michal Vasko3a41dff2020-07-15 14:30:28 +0200676API LY_ERR
677lyd_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 +0100678{
679 struct lyd_node *ret = NULL;
680 const struct lysc_node *schema;
681 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
682
Michal Vasko6027eb92020-07-15 16:37:30 +0200683 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100684
Michal Vaskof03ed032020-03-04 13:31:44 +0100685 if (!module) {
686 module = parent->schema->module;
687 }
688
Michal Vasko3a41dff2020-07-15 14:30:28 +0200689 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
690 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
691 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 +0100692
Michal Vasko3a41dff2020-07-15 14:30:28 +0200693 LY_CHECK_RET(lyd_create_inner(schema, &ret));
694 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100695 lyd_insert_node(parent, NULL, ret);
696 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200697
698 if (node) {
699 *node = ret;
700 }
701 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100702}
703
Michal Vasko3a41dff2020-07-15 14:30:28 +0200704API LY_ERR
705lyd_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 +0100706{
707 struct lyd_node *ret = NULL, *key;
708 const struct lysc_node *schema, *key_s;
709 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
710 va_list ap;
711 const char *key_val;
712 LY_ERR rc = LY_SUCCESS;
713
Michal Vasko6027eb92020-07-15 16:37:30 +0200714 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100715
Michal Vaskof03ed032020-03-04 13:31:44 +0100716 if (!module) {
717 module = parent->schema->module;
718 }
719
Michal Vasko013a8182020-03-03 10:46:53 +0100720 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200721 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100722
723 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200724 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100725
Michal Vasko3a41dff2020-07-15 14:30:28 +0200726 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100727
728 /* create and insert all the keys */
729 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
730 key_val = va_arg(ap, const char *);
731
Michal Vaskof03ed032020-03-04 13:31:44 +0100732 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200733 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
734 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100735 lyd_insert_node(ret, NULL, key);
736 }
737
Michal Vasko013a8182020-03-03 10:46:53 +0100738 if (parent) {
739 lyd_insert_node(parent, NULL, ret);
740 }
741
742cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200743 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100744 if (rc) {
745 lyd_free_tree(ret);
746 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200747 } else if (node) {
748 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100749 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200750 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100751}
752
Michal Vasko3a41dff2020-07-15 14:30:28 +0200753API LY_ERR
754lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
755 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100756{
757 struct lyd_node *ret = NULL;
758 const struct lysc_node *schema;
759 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
760
Michal Vasko6027eb92020-07-15 16:37:30 +0200761 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100762
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 if (!module) {
764 module = parent->schema->module;
765 }
Michal Vasko004d3152020-06-11 19:59:22 +0200766 if (!keys) {
767 keys = "";
768 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100769
Michal Vasko004d3152020-06-11 19:59:22 +0200770 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100771 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200772 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100773
Michal Vasko004d3152020-06-11 19:59:22 +0200774 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
775 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200776 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200777 } else {
778 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200779 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200780 }
Michal Vasko004d3152020-06-11 19:59:22 +0200781 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100782 lyd_insert_node(parent, NULL, ret);
783 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784
785 if (node) {
786 *node = ret;
787 }
788 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100789}
790
Michal Vasko3a41dff2020-07-15 14:30:28 +0200791API LY_ERR
792lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
793 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100794{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200795 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100796 struct lyd_node *ret = NULL;
797 const struct lysc_node *schema;
798 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
799
Michal Vasko6027eb92020-07-15 16:37:30 +0200800 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100801
Michal Vaskof03ed032020-03-04 13:31:44 +0100802 if (!module) {
803 module = parent->schema->module;
804 }
805
Michal Vasko013a8182020-03-03 10:46:53 +0100806 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100808
Michal Vaskocbff3e92020-05-27 12:56:41 +0200809 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200810 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200811 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100812 lyd_insert_node(parent, NULL, ret);
813 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200814
815 if (node) {
816 *node = ret;
817 }
818 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100819}
820
Michal Vasko3a41dff2020-07-15 14:30:28 +0200821API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100822lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200823 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100824{
825 struct lyd_node *ret = NULL;
826 const struct lysc_node *schema;
827 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
828
Michal Vasko6027eb92020-07-15 16:37:30 +0200829 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100830
Michal Vaskof03ed032020-03-04 13:31:44 +0100831 if (!module) {
832 module = parent->schema->module;
833 }
834
Michal Vasko013a8182020-03-03 10:46:53 +0100835 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200836 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100837
Michal Vasko3a41dff2020-07-15 14:30:28 +0200838 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
839 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100840 lyd_insert_node(parent, NULL, ret);
841 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200842
843 if (node) {
844 *node = ret;
845 }
846 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100847}
848
Michal Vasko4490d312020-06-16 13:08:55 +0200849/**
850 * @brief Update node value.
851 *
852 * @param[in] node Node to update.
853 * @param[in] value New value to set.
854 * @param[in] value_type Type of @p value for any node.
855 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
856 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
857 * @return LY_ERR value.
858 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200859static LY_ERR
860lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
861 struct lyd_node **new_parent, struct lyd_node **new_node)
862{
863 LY_ERR ret = LY_SUCCESS;
864 struct lyd_node *new_any;
865
866 switch (node->schema->nodetype) {
867 case LYS_CONTAINER:
868 case LYS_NOTIF:
869 case LYS_RPC:
870 case LYS_ACTION:
871 case LYS_LIST:
872 case LYS_LEAFLIST:
873 /* if it exists, there is nothing to update */
874 *new_parent = NULL;
875 *new_node = NULL;
876 break;
877 case LYS_LEAF:
878 ret = lyd_change_term(node, value);
879 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
880 /* there was an actual change (at least of the default flag) */
881 *new_parent = node;
882 *new_node = node;
883 ret = LY_SUCCESS;
884 } else if (ret == LY_ENOT) {
885 /* no change */
886 *new_parent = NULL;
887 *new_node = NULL;
888 ret = LY_SUCCESS;
889 } /* else error */
890 break;
891 case LYS_ANYDATA:
892 case LYS_ANYXML:
893 /* create a new any node */
894 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
895
896 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200897 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200898 /* not equal, switch values (so that we can use generic node free) */
899 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
900 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
901 ((struct lyd_node_any *)node)->value.str = value;
902 ((struct lyd_node_any *)node)->value_type = value_type;
903
904 *new_parent = node;
905 *new_node = node;
906 } else {
907 /* they are equal */
908 *new_parent = NULL;
909 *new_node = NULL;
910 }
911 lyd_free_tree(new_any);
912 break;
913 default:
914 LOGINT(LYD_NODE_CTX(node));
915 ret = LY_EINT;
916 break;
917 }
918
919 return ret;
920}
921
Michal Vasko3a41dff2020-07-15 14:30:28 +0200922API LY_ERR
923lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
924 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200925{
926 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200927 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200928 const char *prefix, *tmp;
929 char *str;
930 size_t pref_len, name_len;
931
Michal Vasko3a41dff2020-07-15 14:30:28 +0200932 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200933
934 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200935
936 /* parse the name */
937 tmp = name;
938 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
939 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200940 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200941 }
942
943 /* find the module */
944 if (prefix) {
Michal Vasko0b245382020-07-09 15:43:52 +0200945 str = strndup(prefix, pref_len);
Michal Vaskod86997b2020-05-26 15:19:54 +0200946 module = ly_ctx_get_module_implemented(ctx, str);
947 free(str);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200948 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 +0200949 }
950
951 /* set value if none */
952 if (!val_str) {
953 val_str = "";
954 }
955
Michal Vasko3a41dff2020-07-15 14:30:28 +0200956 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL,
957 lydjson_resolve_prefix, NULL, LYD_JSON, parent->schema));
958
959 if (meta) {
960 *meta = ret;
961 }
962 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +0200963}
964
Michal Vasko3a41dff2020-07-15 14:30:28 +0200965API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200966lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200967 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200968{
969 struct lyd_node *ret = NULL;
970
Michal Vasko6027eb92020-07-15 16:37:30 +0200971 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200972
973 if (!ctx) {
974 ctx = LYD_NODE_CTX(parent);
975 }
976 if (!value) {
977 value = "";
978 }
979
Michal Vasko3a41dff2020-07-15 14:30:28 +0200980 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0,
981 module_name, &ret));
982 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200983 lyd_insert_node(parent, NULL, ret);
984 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200985
986 if (node) {
987 *node = ret;
988 }
989 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200990}
991
Michal Vasko3a41dff2020-07-15 14:30:28 +0200992API LY_ERR
993lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
994 struct ly_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +0200995{
996 struct ly_attr *ret = NULL;
997 const struct ly_ctx *ctx;
998 const char *prefix, *tmp;
999 size_t pref_len, name_len;
1000
Michal Vasko3a41dff2020-07-15 14:30:28 +02001001 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001002
1003 ctx = LYD_NODE_CTX(parent);
1004
1005 /* parse the name */
1006 tmp = name;
1007 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1008 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001009 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001010 }
1011
1012 /* set value if none */
1013 if (!val_str) {
1014 val_str = "";
1015 }
1016
Michal Vasko3a41dff2020-07-15 14:30:28 +02001017 LY_CHECK_RET(ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL,
1018 prefix, pref_len, module_name));
1019
1020 if (attr) {
1021 *attr = ret;
1022 }
1023 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001024}
1025
1026API LY_ERR
1027lyd_change_term(struct lyd_node *term, const char *val_str)
1028{
1029 LY_ERR ret = LY_SUCCESS;
1030 struct lysc_type *type;
1031 struct lyd_node_term *t;
1032 struct lyd_node *parent;
1033 struct lyd_value val = {0};
1034 int dflt_change, val_change;
1035
1036 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1037
1038 if (!val_str) {
1039 val_str = "";
1040 }
1041 t = (struct lyd_node_term *)term;
1042 type = ((struct lysc_node_leaf *)term->schema)->type;
1043
1044 /* parse the new value */
1045 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1046 LYD_JSON), cleanup);
1047
1048 /* compare original and new value */
1049 if (type->plugin->compare(&t->value, &val)) {
1050 /* values differ, switch them */
1051 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1052 t->value = val;
1053 memset(&val, 0, sizeof val);
1054 val_change = 1;
1055 } else {
1056 val_change = 0;
1057 }
1058
1059 /* always clear the default flag */
1060 if (term->flags & LYD_DEFAULT) {
1061 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1062 parent->flags &= ~LYD_DEFAULT;
1063 }
1064 dflt_change = 1;
1065 } else {
1066 dflt_change = 0;
1067 }
1068
1069 if (val_change || dflt_change) {
1070 /* make the node non-validated */
1071 term->flags &= LYD_NEW;
1072 }
1073
1074 if (val_change) {
1075 if (term->schema->nodetype == LYS_LEAFLIST) {
1076 /* leaf-list needs to be hashed again and re-inserted into parent */
1077 lyd_unlink_hash(term);
1078 lyd_hash(term);
1079 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1080 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1081 /* list needs to be updated if its key was changed */
1082 assert(term->parent->schema->nodetype == LYS_LIST);
1083 lyd_unlink_hash((struct lyd_node *)term->parent);
1084 lyd_hash((struct lyd_node *)term->parent);
1085 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1086 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1087 }
1088
1089 /* retrun value */
1090 if (!val_change) {
1091 if (dflt_change) {
1092 /* only default flag change */
1093 ret = LY_EEXIST;
1094 } else {
1095 /* no change */
1096 ret = LY_ENOT;
1097 }
1098 } /* else value changed, LY_SUCCESS */
1099
1100cleanup:
1101 type->plugin->free(LYD_NODE_CTX(term), &val);
1102 return ret;
1103}
1104
Michal Vasko41586352020-07-13 13:54:25 +02001105API LY_ERR
1106lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1107{
1108 LY_ERR ret = LY_SUCCESS;
1109 struct lyd_meta *m2;
1110 struct lyd_value val;
1111 int val_change;
1112
1113 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1114
1115 if (!val_str) {
1116 val_str = "";
1117 }
1118
1119 /* parse the new value into a new meta structure */
1120 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
1121 strlen(val_str), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, NULL), cleanup);
1122
1123 /* compare original and new value */
1124 if (lyd_compare_meta(meta, m2)) {
1125 /* values differ, switch them */
1126 val = meta->value;
1127 meta->value = m2->value;
1128 m2->value = val;
1129 val_change = 1;
1130 } else {
1131 val_change = 0;
1132 }
1133
1134 /* retrun value */
1135 if (!val_change) {
1136 /* no change */
1137 ret = LY_ENOT;
1138 } /* else value changed, LY_SUCCESS */
1139
1140cleanup:
1141 return ret;
1142}
1143
Michal Vasko3a41dff2020-07-15 14:30:28 +02001144API LY_ERR
1145lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1146 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001147{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001148 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001149}
1150
1151API LY_ERR
1152lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1153 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1154{
1155 LY_ERR ret = LY_SUCCESS, r;
1156 struct lyxp_expr *exp = NULL;
1157 struct ly_path *p = NULL;
1158 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1159 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001160 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001161 struct ly_path_predicate *pred;
1162
1163 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1164
1165 if (!ctx) {
1166 ctx = LYD_NODE_CTX(parent);
1167 }
1168
1169 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001170 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 +02001171 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1172
1173 /* compile path */
1174 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1175 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1176 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1177
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001178 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1179 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001180 && !(options & LYD_NEWOPT_OPAQ)) {
1181 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1182 lys_nodetype2str(schema->nodetype), schema->name);
1183 ret = LY_EINVAL;
1184 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001185 } 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 +02001186 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001187 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1188 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001189
1190 if (!value) {
1191 value = "";
1192 }
1193
1194 r = LY_SUCCESS;
1195 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001196 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001197 }
1198 if (!r) {
1199 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1200 NULL, LYD_JSON), cleanup);
1201 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1202 }
1203
1204 /* try to find any existing nodes in the path */
1205 if (parent) {
1206 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1207 if (ret == LY_SUCCESS) {
1208 /* the node exists, are we supposed to update it or is it just a default? */
1209 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1210 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1211 ret = LY_EEXIST;
1212 goto cleanup;
1213 }
1214
1215 /* update the existing node */
1216 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1217 goto cleanup;
1218 } else if (ret == LY_EINCOMPLETE) {
1219 /* some nodes were found, adjust the iterator to the next segment */
1220 ++path_idx;
1221 } else if (ret == LY_ENOTFOUND) {
1222 /* 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 +02001223 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001224 node = parent;
1225 }
1226 } else {
1227 /* error */
1228 goto cleanup;
1229 }
1230 }
1231
1232 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001233 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001234 cur_parent = node;
1235 schema = p[path_idx].node;
1236
1237 switch (schema->nodetype) {
1238 case LYS_LIST:
1239 if (!(schema->flags & LYS_KEYLESS)) {
1240 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1241 /* creating opaque list without keys */
1242 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1243 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1244 } else {
1245 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1246 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1247 }
1248 break;
1249 }
1250 /* fallthrough */
1251 case LYS_CONTAINER:
1252 case LYS_NOTIF:
1253 case LYS_RPC:
1254 case LYS_ACTION:
1255 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1256 break;
1257 case LYS_LEAFLIST:
1258 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1259 /* creating opaque leaf-list without value */
1260 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1261 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1262 } else {
1263 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1264 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1265 }
1266 break;
1267 case LYS_LEAF:
1268 /* make there is some value */
1269 if (!value) {
1270 value = "";
1271 }
1272
1273 r = LY_SUCCESS;
1274 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001275 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001276 }
1277 if (!r) {
1278 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1279 LYD_JSON, &node), cleanup);
1280 } else {
1281 /* creating opaque leaf without value */
1282 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1283 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1284 }
1285 break;
1286 case LYS_ANYDATA:
1287 case LYS_ANYXML:
1288 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1289 break;
1290 default:
1291 LOGINT(ctx);
1292 ret = LY_EINT;
1293 goto cleanup;
1294 }
1295
1296 if (cur_parent) {
1297 /* connect to the parent */
1298 lyd_insert_node(cur_parent, NULL, node);
1299 } else if (parent) {
1300 /* connect to top-level siblings */
1301 lyd_insert_node(NULL, &parent, node);
1302 }
1303
1304 /* update remembered nodes */
1305 if (!nparent) {
1306 nparent = node;
1307 }
1308 nnode = node;
1309 }
1310
1311cleanup:
1312 lyxp_expr_free(ctx, exp);
1313 ly_path_free(ctx, p);
1314 if (!ret) {
1315 /* set out params only on success */
1316 if (new_parent) {
1317 *new_parent = nparent;
1318 }
1319 if (new_node) {
1320 *new_node = nnode;
1321 }
1322 }
1323 return ret;
1324}
1325
Michal Vasko90932a92020-02-12 14:33:03 +01001326struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001327lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001328{
Michal Vaskob104f112020-07-17 09:54:54 +02001329 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001330 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001331 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001332
Michal Vaskob104f112020-07-17 09:54:54 +02001333 assert(new_node);
1334
1335 if (!first_sibling || !new_node->schema) {
1336 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001337 return NULL;
1338 }
1339
Michal Vaskob104f112020-07-17 09:54:54 +02001340 if (first_sibling->parent && first_sibling->parent->children_ht) {
1341 /* find the anchor using hashes */
1342 sparent = first_sibling->parent->schema;
1343 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1344 while (schema) {
1345 /* keep trying to find the first existing instance of the closest following schema sibling,
1346 * otherwise return NULL - inserting at the end */
1347 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1348 break;
1349 }
1350
1351 schema = lys_getnext(schema, sparent, NULL, 0);
1352 }
1353 } else {
1354 /* find the anchor without hashes */
1355 match = (struct lyd_node *)first_sibling;
1356 if (!lysc_data_parent(new_node->schema)) {
1357 /* we are in top-level, skip all the data from preceding modules */
1358 LY_LIST_FOR(match, match) {
1359 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1360 break;
1361 }
1362 }
1363 }
1364
1365 /* get the first schema sibling */
1366 sparent = lysc_data_parent(new_node->schema);
1367 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1368
1369 found = 0;
1370 LY_LIST_FOR(match, match) {
1371 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1372 /* we have found an opaque node, which must be at the end, so use it OR
1373 * modules do not match, so we must have traversed all the data from new_node module (if any),
1374 * we have found the first node of the next module, that is what we want */
1375 break;
1376 }
1377
1378 /* skip schema nodes until we find the instantiated one */
1379 while (!found) {
1380 if (new_node->schema == schema) {
1381 /* we have found the schema of the new node, continue search to find the first
1382 * data node with a different schema (after our schema) */
1383 found = 1;
1384 break;
1385 }
1386 if (match->schema == schema) {
1387 /* current node (match) is a data node still before the new node, continue search in data */
1388 break;
1389 }
1390 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1391 assert(schema);
1392 }
1393
1394 if (found && (match->schema != new_node->schema)) {
1395 /* find the next node after we have found our node schema data instance */
1396 break;
1397 }
1398 }
Michal Vasko90932a92020-02-12 14:33:03 +01001399 }
1400
1401 return match;
1402}
1403
1404/**
1405 * @brief Insert node after a sibling.
1406 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 * Handles inserting into NP containers and key-less lists.
1408 *
Michal Vasko90932a92020-02-12 14:33:03 +01001409 * @param[in] sibling Sibling to insert after.
1410 * @param[in] node Node to insert.
1411 */
1412static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001413lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001414{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001415 struct lyd_node_inner *par;
1416
Michal Vasko90932a92020-02-12 14:33:03 +01001417 assert(!node->next && (node->prev == node));
1418
1419 node->next = sibling->next;
1420 node->prev = sibling;
1421 sibling->next = node;
1422 if (node->next) {
1423 /* sibling had a succeeding node */
1424 node->next->prev = node;
1425 } else {
1426 /* sibling was last, find first sibling and change its prev */
1427 if (sibling->parent) {
1428 sibling = sibling->parent->child;
1429 } else {
1430 for (; sibling->prev->next != node; sibling = sibling->prev);
1431 }
1432 sibling->prev = node;
1433 }
1434 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 for (par = node->parent; par; par = par->parent) {
1437 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1438 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001439 par->flags &= ~LYD_DEFAULT;
1440 }
Michal Vaskob104f112020-07-17 09:54:54 +02001441 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 /* rehash key-less list */
1443 lyd_hash((struct lyd_node *)par);
1444 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001445 }
Michal Vasko90932a92020-02-12 14:33:03 +01001446}
1447
1448/**
1449 * @brief Insert node before a sibling.
1450 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 * Handles inserting into NP containers and key-less lists.
1452 *
Michal Vasko90932a92020-02-12 14:33:03 +01001453 * @param[in] sibling Sibling to insert before.
1454 * @param[in] node Node to insert.
1455 */
1456static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001457lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001458{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001459 struct lyd_node_inner *par;
1460
Michal Vasko90932a92020-02-12 14:33:03 +01001461 assert(!node->next && (node->prev == node));
1462
1463 node->next = sibling;
1464 /* covers situation of sibling being first */
1465 node->prev = sibling->prev;
1466 sibling->prev = node;
1467 if (node->prev->next) {
1468 /* sibling had a preceding node */
1469 node->prev->next = node;
1470 } else if (sibling->parent) {
1471 /* sibling was first and we must also change parent child pointer */
1472 sibling->parent->child = node;
1473 }
1474 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001475
Michal Vasko9f96a052020-03-10 09:41:45 +01001476 for (par = node->parent; par; par = par->parent) {
1477 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1478 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001479 par->flags &= ~LYD_DEFAULT;
1480 }
Michal Vaskob104f112020-07-17 09:54:54 +02001481 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* rehash key-less list */
1483 lyd_hash((struct lyd_node *)par);
1484 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001485 }
Michal Vasko90932a92020-02-12 14:33:03 +01001486}
1487
1488/**
Michal Vaskob104f112020-07-17 09:54:54 +02001489 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001490 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001491 * Handles inserting into NP containers and key-less lists.
1492 *
Michal Vasko90932a92020-02-12 14:33:03 +01001493 * @param[in] parent Parent to insert into.
1494 * @param[in] node Node to insert.
1495 */
1496static void
Michal Vaskob104f112020-07-17 09:54:54 +02001497lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001498{
1499 struct lyd_node_inner *par;
1500
Michal Vaskob104f112020-07-17 09:54:54 +02001501 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001502 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001503
1504 par = (struct lyd_node_inner *)parent;
1505
Michal Vaskob104f112020-07-17 09:54:54 +02001506 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001507 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001508
Michal Vasko9f96a052020-03-10 09:41:45 +01001509 for (; par; par = par->parent) {
1510 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1511 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001512 par->flags &= ~LYD_DEFAULT;
1513 }
Michal Vasko52927e22020-03-16 17:26:14 +01001514 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 /* rehash key-less list */
1516 lyd_hash((struct lyd_node *)par);
1517 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001518 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001519}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001520
Michal Vasko751cb4d2020-07-14 12:25:28 +02001521/**
1522 * @brief Learn whether a list instance has all the keys.
1523 *
1524 * @param[in] list List instance to check.
1525 * @return non-zero if all the keys were found,
1526 * @return 0 otherwise.
1527 */
1528static int
1529lyd_insert_has_keys(const struct lyd_node *list)
1530{
1531 const struct lyd_node *key;
1532 const struct lysc_node *skey = NULL;
1533
1534 assert(list->schema->nodetype == LYS_LIST);
1535 key = lyd_node_children(list, 0);
1536 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1537 if (!key || (key->schema != skey)) {
1538 /* key missing */
1539 return 0;
1540 }
1541
1542 key = key->next;
1543 }
1544
1545 /* all keys found */
1546 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001547}
1548
1549void
Michal Vaskob104f112020-07-17 09:54:54 +02001550lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001551{
Michal Vaskob104f112020-07-17 09:54:54 +02001552 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001553
Michal Vaskob104f112020-07-17 09:54:54 +02001554 /* inserting list without its keys is not supported */
1555 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001556
Michal Vaskob104f112020-07-17 09:54:54 +02001557 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1558 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001559 }
Michal Vasko90932a92020-02-12 14:33:03 +01001560
Michal Vaskob104f112020-07-17 09:54:54 +02001561 /* get first sibling */
1562 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001563
Michal Vaskob104f112020-07-17 09:54:54 +02001564 /* find the anchor, our next node, so we can insert before it */
1565 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1566 if (anchor) {
1567 lyd_insert_before_node(anchor, node);
1568 } else if (first_sibling) {
1569 lyd_insert_after_node(first_sibling->prev, node);
1570 } else if (parent) {
1571 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001572 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001573 *first_sibling_p = node;
1574 }
1575
1576 /* insert into parent HT */
1577 lyd_insert_hash(node);
1578
1579 /* finish hashes for our parent, if needed and possible */
1580 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1581 lyd_hash(parent);
1582
1583 /* now we can insert even the list into its parent HT */
1584 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001585 }
Michal Vasko90932a92020-02-12 14:33:03 +01001586}
1587
Michal Vaskof03ed032020-03-04 13:31:44 +01001588static LY_ERR
1589lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1590{
1591 const struct lysc_node *par2;
1592
1593 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001594 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001595
1596 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001597 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001598
1599 if (parent) {
1600 /* inner node */
1601 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001602 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1603 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001604 return LY_EINVAL;
1605 }
1606 } else {
1607 /* top-level node */
1608 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001609 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001610 return LY_EINVAL;
1611 }
1612 }
1613
1614 return LY_SUCCESS;
1615}
1616
1617API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001618lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001619{
1620 struct lyd_node *iter;
1621
Michal Vasko654bc852020-06-23 13:28:06 +02001622 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001623
1624 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1625
1626 if (node->schema->flags & LYS_KEY) {
1627 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1628 return LY_EINVAL;
1629 }
1630
1631 if (node->parent || node->prev->next) {
1632 lyd_unlink_tree(node);
1633 }
1634
1635 while (node) {
1636 iter = node->next;
1637 lyd_unlink_tree(node);
1638 lyd_insert_node(parent, NULL, node);
1639 node = iter;
1640 }
1641 return LY_SUCCESS;
1642}
1643
1644API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001645lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001646{
1647 struct lyd_node *iter;
1648
Michal Vaskob104f112020-07-17 09:54:54 +02001649 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001650
Michal Vaskob104f112020-07-17 09:54:54 +02001651 if (sibling) {
1652 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001653 }
1654
1655 if (node->parent || node->prev->next) {
1656 lyd_unlink_tree(node);
1657 }
1658
1659 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001660 if (node->schema->flags & LYS_KEY) {
1661 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1662 return LY_EINVAL;
1663 }
1664
Michal Vaskob1b5c262020-03-05 14:29:47 +01001665 iter = node->next;
1666 lyd_unlink_tree(node);
1667 lyd_insert_node(NULL, &sibling, node);
1668 node = iter;
1669 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001670
Michal Vaskob104f112020-07-17 09:54:54 +02001671 if (first) {
1672 /* find the first sibling */
1673 *first = sibling;
1674 while ((*first)->prev->next) {
1675 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001676 }
1677 }
1678
1679 return LY_SUCCESS;
1680}
1681
Michal Vaskob1b5c262020-03-05 14:29:47 +01001682API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001683lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1684{
1685 struct lyd_node *iter;
1686
1687 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1688
Michal Vasko62ed12d2020-05-21 10:08:25 +02001689 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001690
Michal Vaskob104f112020-07-17 09:54:54 +02001691 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1692 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001693 return LY_EINVAL;
1694 }
1695
1696 if (node->parent || node->prev->next) {
1697 lyd_unlink_tree(node);
1698 }
1699
1700 /* insert in reverse order to get the original order */
1701 node = node->prev;
1702 while (node) {
1703 iter = node->prev;
1704 lyd_unlink_tree(node);
1705
1706 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001707 lyd_insert_hash(node);
1708
Michal Vaskof03ed032020-03-04 13:31:44 +01001709 /* move the anchor accordingly */
1710 sibling = node;
1711
1712 node = (iter == node) ? NULL : iter;
1713 }
1714 return LY_SUCCESS;
1715}
1716
1717API LY_ERR
1718lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1719{
1720 struct lyd_node *iter;
1721
1722 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1723
Michal Vasko62ed12d2020-05-21 10:08:25 +02001724 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001725
Michal Vaskob104f112020-07-17 09:54:54 +02001726 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1727 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001728 return LY_EINVAL;
1729 }
1730
1731 if (node->parent || node->prev->next) {
1732 lyd_unlink_tree(node);
1733 }
1734
1735 while (node) {
1736 iter = node->next;
1737 lyd_unlink_tree(node);
1738
1739 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001740 lyd_insert_hash(node);
1741
Michal Vaskof03ed032020-03-04 13:31:44 +01001742 /* move the anchor accordingly */
1743 sibling = node;
1744
1745 node = iter;
1746 }
1747 return LY_SUCCESS;
1748}
1749
1750API void
1751lyd_unlink_tree(struct lyd_node *node)
1752{
1753 struct lyd_node *iter;
1754
1755 if (!node) {
1756 return;
1757 }
1758
Michal Vaskob104f112020-07-17 09:54:54 +02001759 /* update hashes while still linked into the tree */
1760 lyd_unlink_hash(node);
1761
Michal Vaskof03ed032020-03-04 13:31:44 +01001762 /* unlink from siblings */
1763 if (node->prev->next) {
1764 node->prev->next = node->next;
1765 }
1766 if (node->next) {
1767 node->next->prev = node->prev;
1768 } else {
1769 /* unlinking the last node */
1770 if (node->parent) {
1771 iter = node->parent->child;
1772 } else {
1773 iter = node->prev;
1774 while (iter->prev != node) {
1775 iter = iter->prev;
1776 }
1777 }
1778 /* update the "last" pointer from the first node */
1779 iter->prev = node->prev;
1780 }
1781
1782 /* unlink from parent */
1783 if (node->parent) {
1784 if (node->parent->child == node) {
1785 /* the node is the first child */
1786 node->parent->child = node->next;
1787 }
1788
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001789 /* check for NP container whether its last non-default node is not being unlinked */
1790 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
1791 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
1792 LY_LIST_FOR(node->parent->child, iter) {
1793 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
1794 break;
1795 }
1796 }
1797 if (!iter) {
1798 node->parent->flags |= LYD_DEFAULT;
1799 }
1800 }
1801
Michal Vaskof03ed032020-03-04 13:31:44 +01001802 /* check for keyless list and update its hash */
1803 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001804 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001805 lyd_hash(iter);
1806 }
1807 }
1808
1809 node->parent = NULL;
1810 }
1811
1812 node->next = NULL;
1813 node->prev = node;
1814}
1815
Michal Vasko90932a92020-02-12 14:33:03 +01001816LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001817lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko52927e22020-03-16 17:26:14 +01001818 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix resolve_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01001819 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001820{
1821 LY_ERR ret;
1822 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001823 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001824 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001825
Michal Vasko9f96a052020-03-10 09:41:45 +01001826 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001827
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001828 LY_ARRAY_FOR(mod->compiled->exts, u) {
1829 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1830 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001831 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001832 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001833 break;
1834 }
1835 }
1836 if (!ant) {
1837 /* attribute is not defined as a metadata annotation (RFC 7952) */
1838 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1839 mod->name, name_len, name);
1840 return LY_EINVAL;
1841 }
1842
Michal Vasko9f96a052020-03-10 09:41:45 +01001843 mt = calloc(1, sizeof *mt);
1844 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1845 mt->parent = parent;
1846 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001847 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, resolve_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01001848 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001849 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001850 return ret;
1851 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001852 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001853
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001854 /* insert as the last attribute */
1855 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001856 if (parent->meta) {
1857 for (last = parent->meta; last->next; last = last->next);
1858 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001859 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001860 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001861 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001862 } else if (*meta) {
1863 for (last = *meta; last->next; last = last->next);
1864 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001865 }
1866
1867 /* remove default flags from NP containers */
Michal Vasko4ba6aef2020-07-09 15:44:23 +02001868 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001869 parent->flags &= ~LYD_DEFAULT;
1870 parent = (struct lyd_node *)parent->parent;
1871 }
1872
Michal Vasko9f96a052020-03-10 09:41:45 +01001873 if (meta) {
1874 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001875 }
1876 return ret;
1877}
1878
Michal Vasko52927e22020-03-16 17:26:14 +01001879LY_ERR
1880ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1881 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1882 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1883{
1884 struct ly_attr *at, *last;
1885 struct lyd_node_opaq *opaq;
1886
1887 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1888 assert(name && name_len);
1889 assert((prefix_len && ns) || (!prefix_len && !ns));
1890
1891 if (!value_len) {
1892 value = "";
1893 }
1894
1895 at = calloc(1, sizeof *at);
1896 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1897 at->parent = (struct lyd_node_opaq *)parent;
1898 at->name = lydict_insert(ctx, name, name_len);
1899 if (dynamic && *dynamic) {
1900 at->value = lydict_insert_zc(ctx, (char *)value);
1901 *dynamic = 0;
1902 } else {
1903 at->value = lydict_insert(ctx, value, value_len);
1904 }
1905
1906 at->format = format;
1907 at->val_prefs = val_prefs;
1908 if (ns) {
1909 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1910 at->prefix.ns = lydict_insert(ctx, ns, 0);
1911 }
1912
1913 /* insert as the last attribute */
1914 if (parent) {
1915 opaq = (struct lyd_node_opaq *)parent;
1916 if (opaq->attr) {
1917 for (last = opaq->attr; last->next; last = last->next);
1918 last->next = at;
1919 } else {
1920 opaq->attr = at;
1921 }
1922 } else if (*attr) {
1923 for (last = *attr; last->next; last = last->next);
1924 last->next = at;
1925 }
1926
1927 if (attr) {
1928 *attr = at;
1929 }
1930 return LY_SUCCESS;
1931}
1932
Radek Krejci084289f2019-07-09 17:35:30 +02001933API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001934lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001935{
Michal Vasko004d3152020-06-11 19:59:22 +02001936 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 if (ly_path_eval(path, tree, &target)) {
1939 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001940 }
1941
Michal Vasko004d3152020-06-11 19:59:22 +02001942 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001943}
1944
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001945API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02001946lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001947{
1948 const struct lyd_node *iter1, *iter2;
1949 struct lyd_node_term *term1, *term2;
1950 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001951 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001952 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001953
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001954 if (!node1 || !node2) {
1955 if (node1 == node2) {
1956 return LY_SUCCESS;
1957 } else {
1958 return LY_ENOT;
1959 }
1960 }
1961
Michal Vasko52927e22020-03-16 17:26:14 +01001962 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001963 return LY_ENOT;
1964 }
1965
1966 if (node1->hash != node2->hash) {
1967 return LY_ENOT;
1968 }
Michal Vasko52927e22020-03-16 17:26:14 +01001969 /* 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 +02001970
Michal Vasko52927e22020-03-16 17:26:14 +01001971 if (!node1->schema) {
1972 opaq1 = (struct lyd_node_opaq *)node1;
1973 opaq2 = (struct lyd_node_opaq *)node2;
1974 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001975 return LY_ENOT;
1976 }
Michal Vasko52927e22020-03-16 17:26:14 +01001977 switch (opaq1->format) {
1978 case LYD_XML:
1979 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1980 return LY_ENOT;
1981 }
1982 break;
1983 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001984 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001985 /* not allowed */
1986 LOGINT(LYD_NODE_CTX(node1));
1987 return LY_EINT;
1988 }
1989 if (options & LYD_COMPARE_FULL_RECURSION) {
1990 iter1 = opaq1->child;
1991 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001992 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001993 }
1994 return LY_SUCCESS;
1995 } else {
1996 switch (node1->schema->nodetype) {
1997 case LYS_LEAF:
1998 case LYS_LEAFLIST:
1999 if (options & LYD_COMPARE_DEFAULTS) {
2000 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2001 return LY_ENOT;
2002 }
2003 }
2004
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002005 term1 = (struct lyd_node_term *)node1;
2006 term2 = (struct lyd_node_term *)node2;
2007 if (term1->value.realtype != term2->value.realtype) {
2008 return LY_ENOT;
2009 }
Michal Vasko52927e22020-03-16 17:26:14 +01002010
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002011 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002012 case LYS_CONTAINER:
2013 if (options & LYD_COMPARE_DEFAULTS) {
2014 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2015 return LY_ENOT;
2016 }
2017 }
2018 if (options & LYD_COMPARE_FULL_RECURSION) {
2019 iter1 = ((struct lyd_node_inner*)node1)->child;
2020 iter2 = ((struct lyd_node_inner*)node2)->child;
2021 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002022 }
2023 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002024 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002025 case LYS_ACTION:
2026 if (options & LYD_COMPARE_FULL_RECURSION) {
2027 /* TODO action/RPC
2028 goto all_children_compare;
2029 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002030 }
2031 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002032 case LYS_NOTIF:
2033 if (options & LYD_COMPARE_FULL_RECURSION) {
2034 /* TODO Notification
2035 goto all_children_compare;
2036 */
2037 }
2038 return LY_SUCCESS;
2039 case LYS_LIST:
2040 iter1 = ((struct lyd_node_inner*)node1)->child;
2041 iter2 = ((struct lyd_node_inner*)node2)->child;
2042
2043 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2044 /* lists with keys, their equivalence is based on their keys */
2045 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002046 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002047 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002048 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002049 return LY_ENOT;
2050 }
2051 iter1 = iter1->next;
2052 iter2 = iter2->next;
2053 }
2054 } else {
2055 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2056
2057 all_children_compare:
2058 if (!iter1 && !iter2) {
2059 /* no children, nothing to compare */
2060 return LY_SUCCESS;
2061 }
2062
2063 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002064 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002065 return LY_ENOT;
2066 }
2067 }
2068 if (iter1 || iter2) {
2069 return LY_ENOT;
2070 }
2071 }
2072 return LY_SUCCESS;
2073 case LYS_ANYXML:
2074 case LYS_ANYDATA:
2075 any1 = (struct lyd_node_any*)node1;
2076 any2 = (struct lyd_node_any*)node2;
2077
2078 if (any1->value_type != any2->value_type) {
2079 return LY_ENOT;
2080 }
2081 switch (any1->value_type) {
2082 case LYD_ANYDATA_DATATREE:
2083 iter1 = any1->value.tree;
2084 iter2 = any2->value.tree;
2085 goto all_children_compare;
2086 case LYD_ANYDATA_STRING:
2087 case LYD_ANYDATA_XML:
2088 case LYD_ANYDATA_JSON:
2089 len1 = strlen(any1->value.str);
2090 len2 = strlen(any2->value.str);
2091 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2092 return LY_ENOT;
2093 }
2094 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002095 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002096 len1 = lyd_lyb_data_length(any1->value.mem);
2097 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002098 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2099 return LY_ENOT;
2100 }
2101 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002102 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002103 }
2104 }
2105
Michal Vasko52927e22020-03-16 17:26:14 +01002106 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002107 return LY_EINT;
2108}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002109
Michal Vasko21725742020-06-29 11:49:25 +02002110API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002111lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2112{
2113 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2114 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2115 }
2116
Michal Vasko11a81432020-07-28 16:31:29 +02002117 if (node1 == node2) {
2118 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002119 }
Michal Vasko11a81432020-07-28 16:31:29 +02002120 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002121}
2122
2123API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002124lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2125{
2126 if (!meta1 || !meta2) {
2127 if (meta1 == meta2) {
2128 return LY_SUCCESS;
2129 } else {
2130 return LY_ENOT;
2131 }
2132 }
2133
2134 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2135 return LY_ENOT;
2136 }
2137
2138 if (meta1->value.realtype != meta2->value.realtype) {
2139 return LY_ENOT;
2140 }
2141
2142 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2143}
2144
Radek Krejci22ebdba2019-07-25 13:59:43 +02002145/**
Michal Vasko52927e22020-03-16 17:26:14 +01002146 * @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 +02002147 *
2148 * 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 +02002149 *
2150 * @param[in] node Original node to duplicate
2151 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2152 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2153 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2154 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2155 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002156 */
Michal Vasko52927e22020-03-16 17:26:14 +01002157static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002158lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2159 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002160{
Michal Vasko52927e22020-03-16 17:26:14 +01002161 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002162 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002163 struct lyd_meta *meta;
2164 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002165 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002166
Michal Vasko52927e22020-03-16 17:26:14 +01002167 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002168
Michal Vasko52927e22020-03-16 17:26:14 +01002169 if (!node->schema) {
2170 dup = calloc(1, sizeof(struct lyd_node_opaq));
2171 } else {
2172 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002173 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002174 case LYS_ACTION:
2175 case LYS_NOTIF:
2176 case LYS_CONTAINER:
2177 case LYS_LIST:
2178 dup = calloc(1, sizeof(struct lyd_node_inner));
2179 break;
2180 case LYS_LEAF:
2181 case LYS_LEAFLIST:
2182 dup = calloc(1, sizeof(struct lyd_node_term));
2183 break;
2184 case LYS_ANYDATA:
2185 case LYS_ANYXML:
2186 dup = calloc(1, sizeof(struct lyd_node_any));
2187 break;
2188 default:
2189 LOGINT(LYD_NODE_CTX(node));
2190 ret = LY_EINT;
2191 goto error;
2192 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002193 }
Michal Vasko52927e22020-03-16 17:26:14 +01002194 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002195
Michal Vaskof6df0a02020-06-16 13:08:34 +02002196 if (options & LYD_DUP_WITH_FLAGS) {
2197 dup->flags = node->flags;
2198 } else {
2199 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2200 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002201 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002202 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002203
Michal Vasko25a32822020-07-09 15:48:22 +02002204 /* duplicate metadata */
2205 if (!(options & LYD_DUP_NO_META)) {
2206 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002207 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002208 }
2209 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002210
2211 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002212 if (!dup->schema) {
2213 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2214 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2215 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002216
2217 if (options & LYD_DUP_RECURSIVE) {
2218 /* duplicate all the children */
2219 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002220 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002221 }
2222 }
2223 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2224 opaq->format = orig->format;
2225 if (orig->prefix.pref) {
2226 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2227 }
2228 if (orig->prefix.ns) {
2229 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2230 }
2231 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002232 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002233 LY_ARRAY_FOR(orig->val_prefs, u) {
2234 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2235 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2236 LY_ARRAY_INCREMENT(opaq->val_prefs);
2237 }
2238 }
2239 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2240 opaq->ctx = orig->ctx;
2241 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2242 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2243 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2244
2245 term->hash = orig->hash;
2246 term->value.realtype = orig->value.realtype;
2247 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2248 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2249 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2250 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2251 struct lyd_node *child;
2252
2253 if (options & LYD_DUP_RECURSIVE) {
2254 /* duplicate all the children */
2255 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002256 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002257 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002258 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002259 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002260 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002261 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002262 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002263 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002264 if (!child) {
2265 /* possibly not keys are present in filtered tree */
2266 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002267 } else if (child->schema != key) {
2268 /* possibly not all keys are present in filtered tree,
2269 * but there can be also some non-key nodes */
2270 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002271 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002272 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002273 child = child->next;
2274 }
2275 }
2276 lyd_hash(dup);
2277 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002278 dup->hash = node->hash;
2279 any = (struct lyd_node_any *)node;
2280 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002281 }
2282
Michal Vasko52927e22020-03-16 17:26:14 +01002283 /* insert */
2284 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002285
2286 if (dup_p) {
2287 *dup_p = dup;
2288 }
2289 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002290
2291error:
Michal Vasko52927e22020-03-16 17:26:14 +01002292 lyd_free_tree(dup);
2293 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002294}
2295
Michal Vasko3a41dff2020-07-15 14:30:28 +02002296static LY_ERR
2297lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2298 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002299{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002300 const struct lyd_node_inner *orig_parent, *iter;
2301 int repeat = 1;
2302
2303 *dup_parent = NULL;
2304 *local_parent = NULL;
2305
2306 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2307 if (parent && (parent->schema == orig_parent->schema)) {
2308 /* stop creating parents, connect what we have into the provided parent */
2309 iter = parent;
2310 repeat = 0;
2311 } else {
2312 iter = NULL;
2313 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2314 (struct lyd_node **)&iter));
2315 }
2316 if (!*local_parent) {
2317 *local_parent = (struct lyd_node_inner *)iter;
2318 }
2319 if (iter->child) {
2320 /* 1) list - add after keys
2321 * 2) provided parent with some children */
2322 iter->child->prev->next = *dup_parent;
2323 if (*dup_parent) {
2324 (*dup_parent)->prev = iter->child->prev;
2325 iter->child->prev = *dup_parent;
2326 }
2327 } else {
2328 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2329 }
2330 if (*dup_parent) {
2331 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2332 }
2333 *dup_parent = (struct lyd_node *)iter;
2334 }
2335
2336 if (repeat && parent) {
2337 /* given parent and created parents chain actually do not interconnect */
2338 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2339 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2340 return LY_EINVAL;
2341 }
2342
2343 return LY_SUCCESS;
2344}
2345
2346static LY_ERR
2347lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2348{
2349 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002350 const struct lyd_node *orig; /* original node to be duplicated */
2351 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002352 struct lyd_node *top = NULL; /* the most higher created node */
2353 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002354
Michal Vasko3a41dff2020-07-15 14:30:28 +02002355 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002356
2357 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002358 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002359 } else {
2360 local_parent = parent;
2361 }
2362
Radek Krejci22ebdba2019-07-25 13:59:43 +02002363 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002364 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002365 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2366 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002367 break;
2368 }
2369 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002370
2371 /* rehash if needed */
2372 for (; local_parent; local_parent = local_parent->parent) {
2373 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2374 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002375 }
2376 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002377
2378 if (dup) {
2379 *dup = first;
2380 }
2381 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002382
2383error:
2384 if (top) {
2385 lyd_free_tree(top);
2386 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002387 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002388 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002389 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002390}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002391
Michal Vasko3a41dff2020-07-15 14:30:28 +02002392API LY_ERR
2393lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2394{
2395 return lyd_dup(node, parent, options, 1, dup);
2396}
2397
2398API LY_ERR
2399lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2400{
2401 return lyd_dup(node, parent, options, 0, dup);
2402}
2403
2404API LY_ERR
2405lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002406{
2407 LY_ERR ret;
2408 struct lyd_meta *mt, *last;
2409
Michal Vasko3a41dff2020-07-15 14:30:28 +02002410 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002411
2412 /* create a copy */
2413 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002414 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002415 mt->parent = node;
2416 mt->annotation = meta->annotation;
2417 mt->value.realtype = meta->value.realtype;
2418 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002419 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002420 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2421
2422 /* insert as the last attribute */
2423 if (node->meta) {
2424 for (last = node->meta; last->next; last = last->next);
2425 last->next = mt;
2426 } else {
2427 node->meta = mt;
2428 }
2429
Michal Vasko3a41dff2020-07-15 14:30:28 +02002430 if (dup) {
2431 *dup = mt;
2432 }
2433 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002434}
2435
Michal Vasko4490d312020-06-16 13:08:55 +02002436/**
2437 * @brief Merge a source sibling into target siblings.
2438 *
2439 * @param[in,out] first_trg First target sibling, is updated if top-level.
2440 * @param[in] parent_trg Target parent.
2441 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2442 * @param[in] options Merge options.
2443 * @return LY_ERR value.
2444 */
2445static LY_ERR
2446lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2447 int options)
2448{
2449 LY_ERR ret;
2450 const struct lyd_node *child_src, *tmp, *sibling_src;
2451 struct lyd_node *match_trg, *dup_src, *next, *elem;
2452 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002453
2454 sibling_src = *sibling_src_p;
2455 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2456 /* try to find the exact instance */
2457 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2458 } else {
2459 /* try to simply find the node, there cannot be more instances */
2460 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2461 }
2462
2463 if (!ret) {
2464 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002465 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002466 /* since they are different, they cannot both be default */
2467 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2468
Michal Vasko3a41dff2020-07-15 14:30:28 +02002469 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2470 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002471 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2472 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2473 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2474 &((struct lyd_node_term *)match_trg)->value));
2475
2476 /* copy flags and add LYD_NEW */
2477 match_trg->flags = sibling_src->flags | LYD_NEW;
2478 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002479 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002480 /* update value */
2481 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2482 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002483
2484 /* copy flags and add LYD_NEW */
2485 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002486 } else {
2487 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002488 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002489 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2490 }
2491 }
2492 } else {
2493 /* node not found, merge it */
2494 if (options & LYD_MERGE_DESTRUCT) {
2495 dup_src = (struct lyd_node *)sibling_src;
2496 lyd_unlink_tree(dup_src);
2497 /* spend it */
2498 *sibling_src_p = NULL;
2499 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002500 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 +02002501 }
2502
2503 /* set LYD_NEW for all the new nodes, required for validation */
2504 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2505 elem->flags |= LYD_NEW;
2506 LYD_TREE_DFS_END(dup_src, next, elem);
2507 }
2508
2509 lyd_insert_node(parent_trg, first_trg, dup_src);
2510 }
2511
2512 return LY_SUCCESS;
2513}
2514
Michal Vasko3a41dff2020-07-15 14:30:28 +02002515static LY_ERR
2516lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002517{
2518 const struct lyd_node *sibling_src, *tmp;
2519 int first;
2520
2521 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2522
2523 if (!source) {
2524 /* nothing to merge */
2525 return LY_SUCCESS;
2526 }
2527
2528 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2529 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2530 return LY_EINVAL;
2531 }
2532
2533 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2534 first = sibling_src == source ? 1 : 0;
2535 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2536 if (first && !sibling_src) {
2537 /* source was spent (unlinked), move to the next node */
2538 source = tmp;
2539 }
2540
Michal Vasko3a41dff2020-07-15 14:30:28 +02002541 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002542 break;
2543 }
2544 }
2545
2546 if (options & LYD_MERGE_DESTRUCT) {
2547 /* free any leftover source data that were not merged */
2548 lyd_free_siblings((struct lyd_node *)source);
2549 }
2550
2551 return LY_SUCCESS;
2552}
2553
Michal Vasko3a41dff2020-07-15 14:30:28 +02002554API LY_ERR
2555lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2556{
2557 return lyd_merge(target, source, options, 1);
2558}
2559
2560API LY_ERR
2561lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2562{
2563 return lyd_merge(target, source, options, 0);
2564}
2565
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002566static LY_ERR
2567lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2568{
Michal Vasko14654712020-02-06 08:35:21 +01002569 /* ending \0 */
2570 ++reqlen;
2571
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002572 if (reqlen > *buflen) {
2573 if (is_static) {
2574 return LY_EINCOMPLETE;
2575 }
2576
2577 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2578 if (!*buffer) {
2579 return LY_EMEM;
2580 }
2581
2582 *buflen = reqlen;
2583 }
2584
2585 return LY_SUCCESS;
2586}
2587
Michal Vaskod59035b2020-07-08 12:00:06 +02002588LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002589lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2590{
2591 const struct lyd_node *key;
2592 int dynamic = 0;
2593 size_t len;
2594 const char *val;
2595 char quot;
2596 LY_ERR rc;
2597
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002598 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002599 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2600 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2601 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2602 if (rc != LY_SUCCESS) {
2603 if (dynamic) {
2604 free((char *)val);
2605 }
2606 return rc;
2607 }
2608
2609 quot = '\'';
2610 if (strchr(val, '\'')) {
2611 quot = '"';
2612 }
2613 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2614
2615 if (dynamic) {
2616 free((char *)val);
2617 }
2618 }
2619
2620 return LY_SUCCESS;
2621}
2622
2623/**
2624 * @brief Append leaf-list value predicate to path.
2625 *
2626 * @param[in] node Node to print.
2627 * @param[in,out] buffer Buffer to print to.
2628 * @param[in,out] buflen Current buffer length.
2629 * @param[in,out] bufused Current number of characters used in @p buffer.
2630 * @param[in] is_static Whether buffer is static or can be reallocated.
2631 * @return LY_ERR
2632 */
2633static LY_ERR
2634lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2635{
2636 int dynamic = 0;
2637 size_t len;
2638 const char *val;
2639 char quot;
2640 LY_ERR rc;
2641
2642 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2643 len = 4 + strlen(val) + 2;
2644 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2645 if (rc != LY_SUCCESS) {
2646 goto cleanup;
2647 }
2648
2649 quot = '\'';
2650 if (strchr(val, '\'')) {
2651 quot = '"';
2652 }
2653 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2654
2655cleanup:
2656 if (dynamic) {
2657 free((char *)val);
2658 }
2659 return rc;
2660}
2661
2662/**
2663 * @brief Append node position (relative to its other instances) predicate to path.
2664 *
2665 * @param[in] node Node to print.
2666 * @param[in,out] buffer Buffer to print to.
2667 * @param[in,out] buflen Current buffer length.
2668 * @param[in,out] bufused Current number of characters used in @p buffer.
2669 * @param[in] is_static Whether buffer is static or can be reallocated.
2670 * @return LY_ERR
2671 */
2672static LY_ERR
2673lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2674{
2675 const struct lyd_node *first, *iter;
2676 size_t len;
2677 int pos;
2678 char *val = NULL;
2679 LY_ERR rc;
2680
2681 if (node->parent) {
2682 first = node->parent->child;
2683 } else {
2684 for (first = node; node->prev->next; node = node->prev);
2685 }
2686 pos = 1;
2687 for (iter = first; iter != node; iter = iter->next) {
2688 if (iter->schema == node->schema) {
2689 ++pos;
2690 }
2691 }
2692 if (asprintf(&val, "%d", pos) == -1) {
2693 return LY_EMEM;
2694 }
2695
2696 len = 1 + strlen(val) + 1;
2697 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2698 if (rc != LY_SUCCESS) {
2699 goto cleanup;
2700 }
2701
2702 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2703
2704cleanup:
2705 free(val);
2706 return rc;
2707}
2708
2709API char *
2710lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2711{
Michal Vasko14654712020-02-06 08:35:21 +01002712 int is_static = 0, i, depth;
2713 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002714 const struct lyd_node *iter;
2715 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002716 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002717
2718 LY_CHECK_ARG_RET(NULL, node, NULL);
2719 if (buffer) {
2720 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2721 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002722 } else {
2723 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002724 }
2725
2726 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002727 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02002728 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002729 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002730 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2731 ++depth;
2732 }
2733
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002734 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002735 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002736 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002737 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002738iter_print:
2739 /* print prefix and name */
2740 mod = NULL;
2741 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2742 mod = iter->schema->module;
2743 }
2744
2745 /* realloc string */
2746 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2747 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2748 if (rc != LY_SUCCESS) {
2749 break;
2750 }
2751
2752 /* print next node */
2753 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2754
Michal Vasko790b2bc2020-08-03 13:35:06 +02002755 /* do not always print the last (first) predicate */
2756 if (bufused || (pathtype == LYD_PATH_LOG)) {
2757 switch (iter->schema->nodetype) {
2758 case LYS_LIST:
2759 if (iter->schema->flags & LYS_KEYLESS) {
2760 /* print its position */
2761 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2762 } else {
2763 /* print all list keys in predicates */
2764 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2765 }
2766 break;
2767 case LYS_LEAFLIST:
2768 if (iter->schema->flags & LYS_CONFIG_W) {
2769 /* print leaf-list value */
2770 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2771 } else {
2772 /* print its position */
2773 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2774 }
2775 break;
2776 default:
2777 /* nothing to print more */
2778 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002779 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002780 }
2781 if (rc != LY_SUCCESS) {
2782 break;
2783 }
2784
Michal Vasko14654712020-02-06 08:35:21 +01002785 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002786 }
2787 break;
2788 }
2789
2790 return buffer;
2791}
Michal Vaskoe444f752020-02-10 12:20:06 +01002792
Michal Vasko25a32822020-07-09 15:48:22 +02002793API struct lyd_meta *
2794lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2795{
2796 struct lyd_meta *ret = NULL;
2797 const struct ly_ctx *ctx;
2798 const char *prefix, *tmp;
2799 char *str;
2800 size_t pref_len, name_len;
2801
2802 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2803
2804 if (!first) {
2805 return NULL;
2806 }
2807
2808 ctx = first->annotation->module->ctx;
2809
2810 /* parse the name */
2811 tmp = name;
2812 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2813 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2814 return NULL;
2815 }
2816
2817 /* find the module */
2818 if (prefix) {
2819 str = strndup(prefix, pref_len);
2820 module = ly_ctx_get_module_latest(ctx, str);
2821 free(str);
2822 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2823 }
2824
2825 /* find the metadata */
2826 LY_LIST_FOR(first, first) {
2827 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2828 ret = (struct lyd_meta *)first;
2829 break;
2830 }
2831 }
2832
2833 return ret;
2834}
2835
Michal Vasko9b368d32020-02-14 13:53:31 +01002836API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002837lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2838{
2839 struct lyd_node **match_p;
2840 struct lyd_node_inner *parent;
2841
Michal Vaskof03ed032020-03-04 13:31:44 +01002842 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002843
Michal Vasko62ed12d2020-05-21 10:08:25 +02002844 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2845 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002846 if (match) {
2847 *match = NULL;
2848 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002849 return LY_ENOTFOUND;
2850 }
2851
2852 /* find first sibling */
2853 if (siblings->parent) {
2854 siblings = siblings->parent->child;
2855 } else {
2856 while (siblings->prev->next) {
2857 siblings = siblings->prev;
2858 }
2859 }
2860
2861 parent = (struct lyd_node_inner *)siblings->parent;
2862 if (parent && parent->children_ht) {
2863 assert(target->hash);
2864
2865 /* find by hash */
2866 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002867 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002868 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002869 siblings = *match_p;
2870 } else {
2871 siblings = NULL;
2872 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002873 } else {
2874 /* not found */
2875 siblings = NULL;
2876 }
2877 } else {
2878 /* no children hash table */
2879 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002880 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002881 break;
2882 }
2883 }
2884 }
2885
2886 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002887 if (match) {
2888 *match = NULL;
2889 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002890 return LY_ENOTFOUND;
2891 }
2892
Michal Vasko9b368d32020-02-14 13:53:31 +01002893 if (match) {
2894 *match = (struct lyd_node *)siblings;
2895 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002896 return LY_SUCCESS;
2897}
2898
Michal Vasko90932a92020-02-12 14:33:03 +01002899static int
2900lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2901{
2902 struct lysc_node *val1;
2903 struct lyd_node *val2;
2904
2905 val1 = *((struct lysc_node **)val1_p);
2906 val2 = *((struct lyd_node **)val2_p);
2907
Michal Vasko90932a92020-02-12 14:33:03 +01002908 if (val1 == val2->schema) {
2909 /* schema match is enough */
2910 return 1;
2911 } else {
2912 return 0;
2913 }
2914}
2915
2916static LY_ERR
2917lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2918{
2919 struct lyd_node **match_p;
2920 struct lyd_node_inner *parent;
2921 uint32_t hash;
2922 values_equal_cb ht_cb;
2923
Michal Vaskob104f112020-07-17 09:54:54 +02002924 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01002925
2926 parent = (struct lyd_node_inner *)siblings->parent;
2927 if (parent && parent->children_ht) {
2928 /* calculate our hash */
2929 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2930 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2931 hash = dict_hash_multi(hash, NULL, 0);
2932
2933 /* use special hash table function */
2934 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2935
2936 /* find by hash */
2937 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2938 siblings = *match_p;
2939 } else {
2940 /* not found */
2941 siblings = NULL;
2942 }
2943
2944 /* set the original hash table compare function back */
2945 lyht_set_cb(parent->children_ht, ht_cb);
2946 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002947 /* find first sibling */
2948 if (siblings->parent) {
2949 siblings = siblings->parent->child;
2950 } else {
2951 while (siblings->prev->next) {
2952 siblings = siblings->prev;
2953 }
2954 }
2955
2956 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01002957 for (; siblings; siblings = siblings->next) {
2958 if (siblings->schema == schema) {
2959 /* schema match is enough */
2960 break;
2961 }
2962 }
2963 }
2964
2965 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002966 if (match) {
2967 *match = NULL;
2968 }
Michal Vasko90932a92020-02-12 14:33:03 +01002969 return LY_ENOTFOUND;
2970 }
2971
Michal Vasko9b368d32020-02-14 13:53:31 +01002972 if (match) {
2973 *match = (struct lyd_node *)siblings;
2974 }
Michal Vasko90932a92020-02-12 14:33:03 +01002975 return LY_SUCCESS;
2976}
2977
Michal Vaskoe444f752020-02-10 12:20:06 +01002978API LY_ERR
2979lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2980 size_t val_len, struct lyd_node **match)
2981{
2982 LY_ERR rc;
2983 struct lyd_node *target = NULL;
2984
Michal Vasko4c583e82020-07-17 12:16:14 +02002985 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002986
Michal Vasko62ed12d2020-05-21 10:08:25 +02002987 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2988 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002989 if (match) {
2990 *match = NULL;
2991 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002992 return LY_ENOTFOUND;
2993 }
2994
Michal Vaskof03ed032020-03-04 13:31:44 +01002995 if (key_or_value && !val_len) {
2996 val_len = strlen(key_or_value);
2997 }
2998
Michal Vaskob104f112020-07-17 09:54:54 +02002999 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3000 /* create a data node and find the instance */
3001 if (schema->nodetype == LYS_LEAFLIST) {
3002 /* target used attributes: schema, hash, value */
3003 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
3004 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3005 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003006 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003007 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003008 }
3009
3010 /* find it */
3011 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003012 } else {
3013 /* find the first schema node instance */
3014 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003015 }
3016
Michal Vaskoe444f752020-02-10 12:20:06 +01003017 lyd_free_tree(target);
3018 return rc;
3019}
Michal Vaskoccc02342020-05-21 10:09:21 +02003020
3021API LY_ERR
3022lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3023{
3024 LY_ERR ret = LY_SUCCESS;
3025 struct lyxp_set xp_set;
3026 struct lyxp_expr *exp;
3027 uint32_t i;
3028
3029 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3030
3031 memset(&xp_set, 0, sizeof xp_set);
3032
3033 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003034 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003035 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3036
3037 /* evaluate expression */
3038 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3039 LY_CHECK_GOTO(ret, cleanup);
3040
3041 /* allocate return set */
3042 *set = ly_set_new();
3043 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3044
3045 /* transform into ly_set */
3046 if (xp_set.type == LYXP_SET_NODE_SET) {
3047 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3048 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3049 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3050 (*set)->size = xp_set.used;
3051
3052 for (i = 0; i < xp_set.used; ++i) {
3053 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3054 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3055 }
3056 }
3057 }
3058
3059cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003060 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003061 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3062 return ret;
3063}