blob: eb6a5fe173c1d56f90c72a91057f1caaeb35d9cc [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>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
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"
31#include "config.h"
32#include "context.h"
33#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020036#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020038#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010039#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_types.h"
41#include "set.h"
42#include "tree.h"
43#include "tree_data_internal.h"
44#include "tree_schema.h"
45#include "tree_schema_internal.h"
46#include "xml.h"
47#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020048
Michal Vaskoe444f752020-02-10 12:20:06 +010049struct ly_keys {
50 char *str;
51 struct {
52 const struct lysc_node_leaf *schema;
53 char *value;
Michal Vasko90932a92020-02-12 14:33:03 +010054 struct lyd_value val;
Michal Vaskoe444f752020-02-10 12:20:06 +010055 } *keys;
56 size_t key_count;
57};
58
Radek Krejci084289f2019-07-09 17:35:30 +020059LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010060lyd_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 +010061 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020062{
Michal Vasko90932a92020-02-12 14:33:03 +010063 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020064 struct ly_err_item *err = NULL;
65 struct ly_ctx *ctx;
66 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020067 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010068 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020069 assert(node);
70
71 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020072
Radek Krejci73dead22019-07-11 16:46:16 +020073 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020074 if (!second) {
75 node->value.realtype = type;
76 }
Michal Vasko90932a92020-02-12 14:33:03 +010077 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020078 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010079 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020080 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020081 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020082 if (!node->parent && lysc_data_parent(node->schema)) {
83 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
84 } else {
85 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
86 }
Radek Krejci73dead22019-07-11 16:46:16 +020087 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020088 }
Radek Krejci73dead22019-07-11 16:46:16 +020089 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010090 } else if (dynamic) {
91 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020092 }
93
94error:
95 return ret;
96}
97
Michal Vasko90932a92020-02-12 14:33:03 +010098/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020099LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +0100100lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
101 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
102{
103 LY_ERR ret = LY_SUCCESS;
104 struct ly_err_item *err = NULL;
105 struct ly_ctx *ctx;
106 struct lysc_type *type;
107 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
108
109 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
110
111 ctx = schema->module->ctx;
112 type = ((struct lysc_node_leaf *)schema)->type;
113 val->realtype = type;
114 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
115 val, NULL, &err);
116 if (ret == LY_EINCOMPLETE) {
117 /* this is fine, we do not need it resolved */
118 ret = LY_SUCCESS;
119 } else if (ret && err) {
120 ly_err_print(err);
121 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
122 ly_err_free(err);
123 }
124 if (!ret && dynamic) {
125 *dynamic = 0;
126 }
127
128 return ret;
129}
130
Radek Krejci38d85362019-09-05 16:26:38 +0200131LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100132lyd_value_parse_meta(struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100133 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100134 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200135{
Michal Vasko90932a92020-02-12 14:33:03 +0100136 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200137 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200138 struct lyext_metadata *ant;
139 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100140 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200141
Michal Vasko9f96a052020-03-10 09:41:45 +0100142 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100143
Michal Vasko9f96a052020-03-10 09:41:45 +0100144 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200145
146 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100147 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200148 }
Michal Vasko90932a92020-02-12 14:33:03 +0100149 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100150 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100151 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200152 if (err) {
153 ly_err_print(err);
154 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
155 ly_err_free(err);
156 }
157 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100158 } else if (dynamic) {
159 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200160 }
161
162error:
163 return ret;
164}
165
Radek Krejci084289f2019-07-09 17:35:30 +0200166API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100167lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci084289f2019-07-09 17:35:30 +0200168 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
169{
170 LY_ERR rc = LY_SUCCESS;
171 struct ly_err_item *err = NULL;
172 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200173
174 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
175
176 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
177 LOGARG(ctx, node);
178 return LY_EINVAL;
179 }
180
181 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200182 /* just validate, no storing of enything */
183 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
184 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
185 if (rc == LY_EINCOMPLETE) {
186 /* actually success since we do not provide the context tree and call validation with
187 * LY_TYPE_OPTS_INCOMPLETE_DATA */
188 rc = LY_SUCCESS;
189 } else if (rc && err) {
190 if (ctx) {
191 /* log only in case the ctx was provided as input parameter */
192 ly_err_print(err);
193 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200194 }
Radek Krejci73dead22019-07-11 16:46:16 +0200195 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200196 }
197
198 return rc;
199}
200
201API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100202lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100203 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200204{
205 LY_ERR rc;
206 struct ly_err_item *err = NULL;
207 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100208 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200209
210 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
211
212 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200213 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100214 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200215 NULL, NULL, &err);
216 if (rc == LY_EINCOMPLETE) {
217 return rc;
218 } else if (rc) {
219 if (err) {
220 if (ctx) {
221 ly_err_print(err);
222 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200223 }
Radek Krejci73dead22019-07-11 16:46:16 +0200224 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200225 }
Radek Krejci73dead22019-07-11 16:46:16 +0200226 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200227 }
228
229 return LY_SUCCESS;
230}
231
232API LY_ERR
233lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100234 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200235{
236 LY_ERR ret = LY_SUCCESS, rc;
237 struct ly_err_item *err = NULL;
238 struct ly_ctx *ctx;
239 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200240 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100241 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200242
243 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
244
245 ctx = node->schema->module->ctx;
246 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200247 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
Michal Vaskof03ed032020-03-04 13:31:44 +0100248 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200249 if (rc == LY_EINCOMPLETE) {
250 ret = rc;
251 /* continue with comparing, just remember what to return if storing is ok */
252 } else if (rc) {
253 /* value to compare is invalid */
254 ret = LY_EINVAL;
255 if (err) {
256 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200257 }
Radek Krejci73dead22019-07-11 16:46:16 +0200258 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200259 }
260
261 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200262 if (type->plugin->compare(&node->value, &data)) {
263 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
264 ret = LY_EVALID;
265 }
Radek Krejci084289f2019-07-09 17:35:30 +0200266
267cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200268 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200269
270 return ret;
271}
272
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200273API const char *
274lyd_value2str(const struct lyd_node_term *node, int *dynamic)
275{
276 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
277
278 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
279}
280
281API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100282lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200283{
Michal Vasko9f96a052020-03-10 09:41:45 +0100284 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200285
Michal Vasko9f96a052020-03-10 09:41:45 +0100286 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200287}
288
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200289API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100290lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200291{
Radek Krejcie7b95092019-05-15 11:03:07 +0200292 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200293#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200294 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200295#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200296
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200297 LY_CHECK_ARG_RET(ctx, ctx, NULL);
298
Michal Vasko5b37a352020-03-06 13:38:33 +0100299 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
300 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
301 return NULL;
302 }
303
Michal Vaskoa3881362020-01-21 15:57:35 +0100304#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200306 /* first item in trees is mandatory - the RPC/action request */
307 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
308 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
309 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
310 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200311 return NULL;
312 }
313 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200314
Radek Krejcie7b95092019-05-15 11:03:07 +0200315 if (options & LYD_OPT_DATA_TEMPLATE) {
316 yang_data_name = va_arg(ap, const char *);
317 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200318#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200319
320 if (!format) {
321 /* TODO try to detect format from the content */
322 }
323
324 switch (format) {
325 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100326 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 break;
328#if 0
329 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200330 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200331 break;
332 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200333 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200334 break;
335#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100336 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200337 LOGINT(ctx);
338 break;
339 }
340
Radek Krejcie7b95092019-05-15 11:03:07 +0200341 return result;
342}
343
344API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100345lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200346{
347 struct lyd_node *result;
348 size_t length;
349 char *addr;
350
351 LY_CHECK_ARG_RET(ctx, ctx, NULL);
352 if (fd < 0) {
353 LOGARG(ctx, fd);
354 return NULL;
355 }
356
357 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100358 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200359 if (addr) {
360 ly_munmap(addr, length);
361 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200362
363 return result;
364}
365
366API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100367lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200368{
369 int fd;
370 struct lyd_node *result;
371 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200372
373 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
374
375 fd = open(path, O_RDONLY);
376 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
377
378 if (!format) {
379 /* unknown format - try to detect it from filename's suffix */
380 len = strlen(path);
381
382 /* ignore trailing whitespaces */
383 for (; len > 0 && isspace(path[len - 1]); len--);
384
385 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
386 format = LYD_XML;
387#if 0
388 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
389 format = LYD_JSON;
390 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
391 format = LYD_LYB;
392#endif
393 } /* else still unknown, try later to detect it from the content */
394 }
395
Michal Vaskoa3881362020-01-21 15:57:35 +0100396 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200397 close(fd);
398
399 return result;
400}
Radek Krejci084289f2019-07-09 17:35:30 +0200401
Michal Vasko90932a92020-02-12 14:33:03 +0100402LY_ERR
403lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
404 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
405{
406 LY_ERR ret;
407 struct lyd_node_term *term;
408
Michal Vasko9b368d32020-02-14 13:53:31 +0100409 assert(schema->nodetype & LYD_NODE_TERM);
410
Michal Vasko90932a92020-02-12 14:33:03 +0100411 term = calloc(1, sizeof *term);
412 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
413
414 term->schema = schema;
415 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100416 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100417
418 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
419 if (ret && (ret != LY_EINCOMPLETE)) {
420 free(term);
421 return ret;
422 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100423 lyd_hash((struct lyd_node *)term);
424
425 *node = (struct lyd_node *)term;
426 return ret;
427}
428
429LY_ERR
430lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
431{
432 LY_ERR ret;
433 struct lyd_node_term *term;
434 struct lysc_type *type;
435
436 assert(schema->nodetype & LYD_NODE_TERM);
437
438 term = calloc(1, sizeof *term);
439 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
440
441 term->schema = schema;
442 term->prev = (struct lyd_node *)term;
443 term->flags = LYD_NEW;
444
445 type = ((struct lysc_node_leaf *)schema)->type;
446 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
447 if (ret) {
448 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
449 free(term);
450 return ret;
451 }
452 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100453
454 *node = (struct lyd_node *)term;
455 return ret;
456}
457
458LY_ERR
459lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
460{
461 struct lyd_node_inner *in;
462
Michal Vasko9b368d32020-02-14 13:53:31 +0100463 assert(schema->nodetype & LYD_NODE_INNER);
464
Michal Vasko90932a92020-02-12 14:33:03 +0100465 in = calloc(1, sizeof *in);
466 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
467
468 in->schema = schema;
469 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100470 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100471
Michal Vasko9b368d32020-02-14 13:53:31 +0100472 /* do not hash list with keys, we need them for the hash */
473 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
474 lyd_hash((struct lyd_node *)in);
475 }
Michal Vasko90932a92020-02-12 14:33:03 +0100476
477 *node = (struct lyd_node *)in;
478 return LY_SUCCESS;
479}
480
481static void
482ly_keys_clean(struct ly_keys *keys)
483{
484 size_t i;
485
486 for (i = 0; i < keys->key_count; ++i) {
487 keys->keys[i].schema->type->plugin->free(keys->keys[i].schema->module->ctx, &keys->keys[i].val);
488 }
489 free(keys->str);
490 free(keys->keys);
491}
492
493static char *
494ly_keys_parse_next(char **next_key, char **key_name)
495{
496 char *ptr, *ptr2, *val, quot;
Michal Vaskoc2f11292020-05-22 16:43:47 +0200497 const char *pref;
498 size_t pref_len, key_len;
499 int have_equal = 0;
Michal Vasko90932a92020-02-12 14:33:03 +0100500
501 ptr = *next_key;
502
503 /* "[" */
504 LY_CHECK_GOTO(ptr[0] != '[', error);
505 ++ptr;
506
Michal Vaskoc2f11292020-05-22 16:43:47 +0200507 /* skip WS */
508 while (isspace(ptr[0])) {
509 ++ptr;
510 }
Michal Vasko90932a92020-02-12 14:33:03 +0100511
Michal Vaskoc2f11292020-05-22 16:43:47 +0200512 /* key name without prefix */
513 LY_CHECK_GOTO(ly_parse_nodeid((const char **)&ptr, &pref, &pref_len, (const char **)key_name, &key_len), error);
514 if (pref) {
515 goto error;
516 }
Michal Vasko90932a92020-02-12 14:33:03 +0100517
Michal Vaskoc2f11292020-05-22 16:43:47 +0200518 /* terminate it */
519 LY_CHECK_GOTO((ptr[0] != '=') && !isspace(ptr[0]), error);
520 if (ptr[0] == '=') {
521 have_equal = 1;
522 }
523 ptr[0] = '\0';
524 ++ptr;
525
526 if (!have_equal) {
527 /* skip WS */
528 while (isspace(ptr[0])) {
529 ++ptr;
530 }
531
532 /* '=' */
533 LY_CHECK_GOTO(ptr[0] != '=', error);
534 ++ptr;
535 }
536
537 /* skip WS */
538 while (isspace(ptr[0])) {
539 ++ptr;
540 }
Michal Vasko90932a92020-02-12 14:33:03 +0100541
542 /* quote */
543 LY_CHECK_GOTO((ptr[0] != '\'') && (ptr[0] != '\"'), error);
544 quot = ptr[0];
545 ++ptr;
546
547 /* value, terminate it */
548 val = ptr;
549 ptr2 = strchr(ptr, quot);
550 LY_CHECK_GOTO(!ptr2, error);
551 ptr2[0] = '\0';
552
553 /* \0, was quote */
554 ptr = ptr2 + 1;
555
Michal Vaskoc2f11292020-05-22 16:43:47 +0200556 /* skip WS */
557 while (isspace(ptr[0])) {
558 ++ptr;
559 }
560
Michal Vasko90932a92020-02-12 14:33:03 +0100561 /* "]" */
562 LY_CHECK_GOTO(ptr[0] != ']', error);
563 ++ptr;
564
565 *next_key = ptr;
566 return val;
567
568error:
569 *next_key = ptr;
570 return NULL;
571}
572
Michal Vaskod3678892020-05-21 10:06:58 +0200573/* fill keys structure that is expected to be zeroed and must always be cleaned (even on error);
574 * if store is set, fill also each val */
Michal Vasko90932a92020-02-12 14:33:03 +0100575static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +0200576ly_keys_parse(const struct lysc_node *list, const char *keys_str, size_t keys_len, int store, int log,
577 struct ly_keys *keys)
Michal Vasko90932a92020-02-12 14:33:03 +0100578{
579 LY_ERR ret = LY_SUCCESS;
580 char *next_key, *name;
581 const struct lysc_node *key;
582 size_t i;
583
584 assert(list->nodetype == LYS_LIST);
585
Michal Vaskof03ed032020-03-04 13:31:44 +0100586 if (!keys_str) {
587 /* nothing to parse */
588 return LY_SUCCESS;
589 }
590
591 keys->str = strndup(keys_str, keys_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100592 LY_CHECK_ERR_GOTO(!keys->str, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
593
594 next_key = keys->str;
595 while (next_key[0]) {
596 /* new key */
597 keys->keys = ly_realloc(keys->keys, (keys->key_count + 1) * sizeof *keys->keys);
598 LY_CHECK_ERR_GOTO(!keys->keys, LOGMEM(list->module->ctx); ret = LY_EMEM, cleanup);
599
600 /* fill */
601 keys->keys[keys->key_count].value = ly_keys_parse_next(&next_key, &name);
602 if (!keys->keys[keys->key_count].value) {
Michal Vaskod3678892020-05-21 10:06:58 +0200603 if (log) {
604 LOGERR(list->module->ctx, LY_EINVAL, "Invalid keys string (at \"%s\").", next_key);
605 }
Michal Vasko90932a92020-02-12 14:33:03 +0100606 ret = LY_EINVAL;
607 goto cleanup;
608 }
609
610 /* find schema node */
611 key = lys_find_child(list, list->module, name, 0, LYS_LEAF, 0);
612 if (!key) {
Michal Vaskod3678892020-05-21 10:06:58 +0200613 if (log) {
614 LOGERR(list->module->ctx, LY_EINVAL, "List \"%s\" has no key \"%s\".", list->name, name);
615 }
Michal Vasko90932a92020-02-12 14:33:03 +0100616 ret = LY_EINVAL;
617 goto cleanup;
618 }
619 keys->keys[keys->key_count].schema = (const struct lysc_node_leaf *)key;
620
621 /* check that we do not have it already */
622 for (i = 0; i < keys->key_count; ++i) {
623 if (keys->keys[i].schema == keys->keys[keys->key_count].schema) {
Michal Vaskod3678892020-05-21 10:06:58 +0200624 if (log) {
625 LOGERR(list->module->ctx, LY_EINVAL, "Duplicit key \"%s\" value.", name);
626 }
Michal Vasko90932a92020-02-12 14:33:03 +0100627 ret = LY_EINVAL;
628 goto cleanup;
629 }
630 }
631
632 if (store) {
633 /* store the value */
634 ret = lyd_value_store(&keys->keys[keys->key_count].val, key, keys->keys[keys->key_count].value, 0, 0,
635 lydjson_resolve_prefix, NULL, LYD_JSON);
636 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoae420e92020-05-21 10:00:40 +0200637 } else {
638 memset(&keys->keys[keys->key_count].val, 0, sizeof keys->keys[keys->key_count].val);
Michal Vasko90932a92020-02-12 14:33:03 +0100639 }
640
641 /* another valid key */
642 ++keys->key_count;
643 }
644
645cleanup:
Michal Vasko90932a92020-02-12 14:33:03 +0100646 return ret;
647}
648
649LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200650lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100651{
652 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100653 struct lyd_node *list = NULL, *key;
Michal Vasko004d3152020-06-11 19:59:22 +0200654 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100655
Michal Vasko004d3152020-06-11 19:59:22 +0200656 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100657
658 /* create list */
659 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
660
Michal Vasko90932a92020-02-12 14:33:03 +0100661 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200662 LY_ARRAY_FOR(predicates, u) {
663 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100664 lyd_insert_node(list, NULL, key);
665 }
666
Michal Vasko9b368d32020-02-14 13:53:31 +0100667 /* hash having all the keys */
668 lyd_hash(list);
669
Michal Vasko90932a92020-02-12 14:33:03 +0100670 /* success */
671 *node = list;
672 list = NULL;
673
674cleanup:
675 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200676 return ret;
677}
678
679static LY_ERR
680lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
681{
682 LY_ERR ret = LY_SUCCESS;
683 struct lyxp_expr *expr = NULL;
684 uint16_t exp_idx = 0;
685 enum ly_path_pred_type pred_type = 0;
686 struct ly_path_predicate *predicates = NULL;
687
688 /* parse keys */
689 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
690 LY_PATH_PRED_KEYS, &expr), cleanup);
691
692 /* compile them */
693 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module, schema, expr, &exp_idx, lydjson_resolve_prefix, NULL,
694 LYD_JSON, &predicates, &pred_type), cleanup);
695
696 /* create the list node */
697 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
698
699cleanup:
700 lyxp_expr_free(schema->module->ctx, expr);
701 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100702 return ret;
703}
704
705LY_ERR
706lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
707{
708 struct lyd_node_any *any;
709
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 assert(schema->nodetype & LYD_NODE_ANY);
711
Michal Vasko90932a92020-02-12 14:33:03 +0100712 any = calloc(1, sizeof *any);
713 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
714
715 any->schema = schema;
716 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100717 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100718
719 any->value.xml = value;
720 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100721 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100722
723 *node = (struct lyd_node *)any;
724 return LY_SUCCESS;
725}
726
Michal Vasko52927e22020-03-16 17:26:14 +0100727LY_ERR
728lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
729 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
730 const char *ns, struct lyd_node **node)
731{
732 struct lyd_node_opaq *opaq;
733
734 assert(ctx && name && name_len && ns);
735
736 if (!value_len) {
737 value = "";
738 }
739
740 opaq = calloc(1, sizeof *opaq);
741 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
742
743 opaq->prev = (struct lyd_node *)opaq;
744
745 opaq->name = lydict_insert(ctx, name, name_len);
746 opaq->format = format;
747 if (pref_len) {
748 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
749 }
750 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
751 opaq->val_prefs = val_prefs;
752 if (dynamic && *dynamic) {
753 opaq->value = lydict_insert_zc(ctx, (char *)value);
754 *dynamic = 0;
755 } else {
756 opaq->value = lydict_insert(ctx, value, value_len);
757 }
758 opaq->ctx = ctx;
759
760 *node = (struct lyd_node *)opaq;
761 return LY_SUCCESS;
762}
763
Michal Vasko013a8182020-03-03 10:46:53 +0100764API struct lyd_node *
765lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
766{
767 struct lyd_node *ret = NULL;
768 const struct lysc_node *schema;
769 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
770
771 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
772
Michal Vaskof03ed032020-03-04 13:31:44 +0100773 if (!module) {
774 module = parent->schema->module;
775 }
776
Michal Vasko1bf09392020-03-27 12:38:10 +0100777 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vaskob00f3cf2020-05-27 11:20:13 +0200778 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100779
780 if (!lyd_create_inner(schema, &ret) && parent) {
781 lyd_insert_node(parent, NULL, ret);
782 }
783 return ret;
784}
785
786API struct lyd_node *
787lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
788{
789 struct lyd_node *ret = NULL, *key;
790 const struct lysc_node *schema, *key_s;
791 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
792 va_list ap;
793 const char *key_val;
794 LY_ERR rc = LY_SUCCESS;
795
796 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
797
Michal Vaskof03ed032020-03-04 13:31:44 +0100798 if (!module) {
799 module = parent->schema->module;
800 }
801
Michal Vasko013a8182020-03-03 10:46:53 +0100802 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
803 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
804
805 /* create list inner node */
806 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
807
808 va_start(ap, name);
809
810 /* create and insert all the keys */
811 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
812 key_val = va_arg(ap, const char *);
813
Michal Vaskof03ed032020-03-04 13:31:44 +0100814 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 +0200815 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
816 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100817 lyd_insert_node(ret, NULL, key);
818 }
819
820 /* hash having all the keys */
821 lyd_hash(ret);
822
823 if (parent) {
824 lyd_insert_node(parent, NULL, ret);
825 }
826
827cleanup:
828 if (rc) {
829 lyd_free_tree(ret);
830 ret = NULL;
831 }
832 va_end(ap);
833 return ret;
834}
835
836API struct lyd_node *
837lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
838{
839 struct lyd_node *ret = NULL;
840 const struct lysc_node *schema;
841 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
842
843 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
844
Michal Vaskof03ed032020-03-04 13:31:44 +0100845 if (!module) {
846 module = parent->schema->module;
847 }
Michal Vasko004d3152020-06-11 19:59:22 +0200848 if (!keys) {
849 keys = "";
850 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100851
Michal Vasko004d3152020-06-11 19:59:22 +0200852 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100853 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
854 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
855
Michal Vasko004d3152020-06-11 19:59:22 +0200856 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
857 /* key-less list */
858 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
859 } else {
860 /* create the list node */
861 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret), NULL);
862 }
863
864 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100865 lyd_insert_node(parent, NULL, ret);
866 }
867 return ret;
868}
869
870API struct lyd_node *
871lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
872{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200873 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100874 struct lyd_node *ret = NULL;
875 const struct lysc_node *schema;
876 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
877
878 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
879
Michal Vaskof03ed032020-03-04 13:31:44 +0100880 if (!module) {
881 module = parent->schema->module;
882 }
883
Michal Vasko013a8182020-03-03 10:46:53 +0100884 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
885 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
886
Michal Vaskocbff3e92020-05-27 12:56:41 +0200887 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
888 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), NULL);
889
890 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100891 lyd_insert_node(parent, NULL, ret);
892 }
893 return ret;
894}
895
896API struct lyd_node *
897lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
898 LYD_ANYDATA_VALUETYPE value_type)
899{
900 struct lyd_node *ret = NULL;
901 const struct lysc_node *schema;
902 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
903
904 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
905
Michal Vaskof03ed032020-03-04 13:31:44 +0100906 if (!module) {
907 module = parent->schema->module;
908 }
909
Michal Vasko013a8182020-03-03 10:46:53 +0100910 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
911 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
912
913 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
914 lyd_insert_node(parent, NULL, ret);
915 }
916 return ret;
917}
918
Michal Vaskod86997b2020-05-26 15:19:54 +0200919API struct lyd_meta *
920lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
921{
922 struct lyd_meta *ret = NULL;
923 struct ly_ctx *ctx = parent->schema->module->ctx;
924 const char *prefix, *tmp;
925 char *str;
926 size_t pref_len, name_len;
927
928 LY_CHECK_ARG_RET(ctx, parent, name, module || strchr(name, ':'), NULL);
929
930 /* parse the name */
931 tmp = name;
932 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
933 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
934 return NULL;
935 }
936
937 /* find the module */
938 if (prefix) {
939 str = strndup(name, name_len);
940 module = ly_ctx_get_module_implemented(ctx, str);
941 free(str);
Michal Vasko004d3152020-06-11 19:59:22 +0200942 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +0200943 }
944
945 /* set value if none */
946 if (!val_str) {
947 val_str = "";
948 }
949
950 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
951 LYD_JSON, parent->schema);
952 return ret;
953}
954
Michal Vasko90932a92020-02-12 14:33:03 +0100955struct lyd_node *
956lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
957{
958 const struct lysc_node *prev_key;
959 struct lyd_node *match = NULL;
960
961 if (!first_sibling) {
962 return NULL;
963 }
964
965 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
966 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
967 }
968
969 return match;
970}
971
972/**
973 * @brief Insert node after a sibling.
974 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100975 * Handles inserting into NP containers and key-less lists.
976 *
Michal Vasko90932a92020-02-12 14:33:03 +0100977 * @param[in] sibling Sibling to insert after.
978 * @param[in] node Node to insert.
979 */
980static void
Michal Vaskof03ed032020-03-04 13:31:44 +0100981lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100982{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100983 struct lyd_node_inner *par;
984
Michal Vasko90932a92020-02-12 14:33:03 +0100985 assert(!node->next && (node->prev == node));
986
987 node->next = sibling->next;
988 node->prev = sibling;
989 sibling->next = node;
990 if (node->next) {
991 /* sibling had a succeeding node */
992 node->next->prev = node;
993 } else {
994 /* sibling was last, find first sibling and change its prev */
995 if (sibling->parent) {
996 sibling = sibling->parent->child;
997 } else {
998 for (; sibling->prev->next != node; sibling = sibling->prev);
999 }
1000 sibling->prev = node;
1001 }
1002 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001003
Michal Vasko9f96a052020-03-10 09:41:45 +01001004 for (par = node->parent; par; par = par->parent) {
1005 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1006 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001007 par->flags &= ~LYD_DEFAULT;
1008 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001009 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1010 /* rehash key-less list */
1011 lyd_hash((struct lyd_node *)par);
1012 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001013 }
1014
1015 /* insert into hash table */
1016 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001017}
1018
1019/**
1020 * @brief Insert node before a sibling.
1021 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001022 * Handles inserting into NP containers and key-less lists.
1023 *
Michal Vasko90932a92020-02-12 14:33:03 +01001024 * @param[in] sibling Sibling to insert before.
1025 * @param[in] node Node to insert.
1026 */
1027static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001028lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001029{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001030 struct lyd_node_inner *par;
1031
Michal Vasko90932a92020-02-12 14:33:03 +01001032 assert(!node->next && (node->prev == node));
1033
1034 node->next = sibling;
1035 /* covers situation of sibling being first */
1036 node->prev = sibling->prev;
1037 sibling->prev = node;
1038 if (node->prev->next) {
1039 /* sibling had a preceding node */
1040 node->prev->next = node;
1041 } else if (sibling->parent) {
1042 /* sibling was first and we must also change parent child pointer */
1043 sibling->parent->child = node;
1044 }
1045 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001046
Michal Vasko9f96a052020-03-10 09:41:45 +01001047 for (par = node->parent; par; par = par->parent) {
1048 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1049 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001050 par->flags &= ~LYD_DEFAULT;
1051 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001052 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1053 /* rehash key-less list */
1054 lyd_hash((struct lyd_node *)par);
1055 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001056 }
1057
1058 /* insert into hash table */
1059 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001060}
1061
1062/**
1063 * @brief Insert node as the last child of a parent.
1064 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001065 * Handles inserting into NP containers and key-less lists.
1066 *
Michal Vasko90932a92020-02-12 14:33:03 +01001067 * @param[in] parent Parent to insert into.
1068 * @param[in] node Node to insert.
1069 */
1070static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001071lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001072{
1073 struct lyd_node_inner *par;
1074
Michal Vasko0249f7c2020-03-05 16:36:40 +01001075 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001076 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001077
1078 par = (struct lyd_node_inner *)parent;
1079
1080 if (!par->child) {
1081 par->child = node;
1082 } else {
1083 node->prev = par->child->prev;
1084 par->child->prev->next = node;
1085 par->child->prev = node;
1086 }
1087 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001088
Michal Vasko9f96a052020-03-10 09:41:45 +01001089 for (; par; par = par->parent) {
1090 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1091 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001092 par->flags &= ~LYD_DEFAULT;
1093 }
Michal Vasko52927e22020-03-16 17:26:14 +01001094 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001095 /* rehash key-less list */
1096 lyd_hash((struct lyd_node *)par);
1097 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001098 }
1099
1100 /* insert into hash table */
1101 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001102}
1103
1104void
Michal Vasko9b368d32020-02-14 13:53:31 +01001105lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001106{
Michal Vasko9b368d32020-02-14 13:53:31 +01001107 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001108 const struct lysc_node *skey = NULL;
1109 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001110
Michal Vasko52927e22020-03-16 17:26:14 +01001111 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001112
1113 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1114 parent = (struct lyd_node *)(*first_sibling)->parent;
1115 }
Michal Vasko90932a92020-02-12 14:33:03 +01001116
1117 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001118 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001119 /* it is key and we need to insert it at the correct place */
Michal Vasko9b368d32020-02-14 13:53:31 +01001120 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent), node->schema);
1121 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001122 lyd_insert_after_node(anchor, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001123 } else if (lyd_node_children(parent)) {
Radek Krejcidae0ee82020-05-06 16:53:24 +02001124 lyd_insert_before_node(lyd_node_children(parent), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001125 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001126 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001127 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001128
1129 /* hash list if all its keys were added */
1130 assert(parent->schema->nodetype == LYS_LIST);
Radek Krejcidae0ee82020-05-06 16:53:24 +02001131 anchor = lyd_node_children(parent);
Michal Vasko9f96a052020-03-10 09:41:45 +01001132 has_keys = 1;
1133 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1134 if (!anchor || (anchor->schema != skey)) {
1135 /* key missing */
1136 has_keys = 0;
1137 break;
1138 }
1139
1140 anchor = anchor->next;
1141 }
1142 if (has_keys) {
1143 lyd_hash(parent);
1144 }
1145
Michal Vasko90932a92020-02-12 14:33:03 +01001146 } else {
1147 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001148 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001149 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001150 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001151 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001152 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001153 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001154 anchor = anchor->prev;
1155 }
1156
Michal Vaskoc193ce92020-03-06 11:04:48 +01001157 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001158 /* insert after last sibling from this module */
1159 lyd_insert_after_node(anchor, node);
1160 } else {
1161 /* no data from this module, insert at the last position */
1162 lyd_insert_after_node((*first_sibling)->prev, node);
1163 }
Michal Vasko90932a92020-02-12 14:33:03 +01001164 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001165 /* the only sibling */
1166 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001167 }
Michal Vasko90932a92020-02-12 14:33:03 +01001168}
1169
Michal Vaskof03ed032020-03-04 13:31:44 +01001170static LY_ERR
1171lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1172{
1173 const struct lysc_node *par2;
1174
1175 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001176 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001177
1178 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001179 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001180
1181 if (parent) {
1182 /* inner node */
1183 if (par2 != parent) {
1184 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1185 return LY_EINVAL;
1186 }
1187 } else {
1188 /* top-level node */
1189 if (par2) {
1190 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1191 return LY_EINVAL;
1192 }
1193 }
1194
1195 return LY_SUCCESS;
1196}
1197
1198API LY_ERR
1199lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1200{
1201 struct lyd_node *iter;
1202
1203 LY_CHECK_ARG_RET(NULL, parent, node, !(parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
1204
1205 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1206
1207 if (node->schema->flags & LYS_KEY) {
1208 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1209 return LY_EINVAL;
1210 }
1211
1212 if (node->parent || node->prev->next) {
1213 lyd_unlink_tree(node);
1214 }
1215
1216 while (node) {
1217 iter = node->next;
1218 lyd_unlink_tree(node);
1219 lyd_insert_node(parent, NULL, node);
1220 node = iter;
1221 }
1222 return LY_SUCCESS;
1223}
1224
1225API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001226lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1227{
1228 struct lyd_node *iter;
1229
1230 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1231
Michal Vasko62ed12d2020-05-21 10:08:25 +02001232 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001233
1234 if (node->schema->flags & LYS_KEY) {
1235 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1236 return LY_EINVAL;
1237 }
1238
1239 if (node->parent || node->prev->next) {
1240 lyd_unlink_tree(node);
1241 }
1242
1243 while (node) {
1244 iter = node->next;
1245 lyd_unlink_tree(node);
1246 lyd_insert_node(NULL, &sibling, node);
1247 node = iter;
1248 }
1249 return LY_SUCCESS;
1250}
1251
Michal Vasko0249f7c2020-03-05 16:36:40 +01001252static LY_ERR
1253lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1254{
1255 if (sibling->parent) {
1256 /* nested, we do not care for the order */
1257 return LY_SUCCESS;
1258 }
1259
1260 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001261 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1262 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001263 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert top-level module \"%s\" data into module \"%s\" data.",
Michal Vaskoc193ce92020-03-06 11:04:48 +01001264 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001265 return LY_EINVAL;
1266 }
1267
Michal Vaskoc193ce92020-03-06 11:04:48 +01001268 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1269 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001270 /* inserting before/after its module data */
1271 return LY_SUCCESS;
1272 }
1273 }
1274
1275 /* find first sibling */
1276 while (sibling->prev->next) {
1277 sibling = sibling->prev;
1278 }
1279
1280 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001281 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001282 /* inserting before its module data */
1283 return LY_SUCCESS;
1284 }
1285 }
1286
1287 /* check there are no data of this module */
1288 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001289 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001290 /* some data of this module found */
1291 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001292 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001293 return LY_EINVAL;
1294 }
1295 }
1296
1297 return LY_SUCCESS;
1298}
1299
Michal Vaskob1b5c262020-03-05 14:29:47 +01001300API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001301lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1302{
1303 struct lyd_node *iter;
1304
1305 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1306
Michal Vasko62ed12d2020-05-21 10:08:25 +02001307 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001308
1309 if (node->schema->flags & LYS_KEY) {
1310 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1311 return LY_EINVAL;
1312 } else if (sibling->schema->flags & LYS_KEY) {
1313 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1314 return LY_EINVAL;
1315 }
1316
Michal Vasko0249f7c2020-03-05 16:36:40 +01001317 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1318
Michal Vaskof03ed032020-03-04 13:31:44 +01001319 if (node->parent || node->prev->next) {
1320 lyd_unlink_tree(node);
1321 }
1322
1323 /* insert in reverse order to get the original order */
1324 node = node->prev;
1325 while (node) {
1326 iter = node->prev;
1327 lyd_unlink_tree(node);
1328
1329 lyd_insert_before_node(sibling, node);
1330 /* move the anchor accordingly */
1331 sibling = node;
1332
1333 node = (iter == node) ? NULL : iter;
1334 }
1335 return LY_SUCCESS;
1336}
1337
1338API LY_ERR
1339lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1340{
1341 struct lyd_node *iter;
1342
1343 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1344
Michal Vasko62ed12d2020-05-21 10:08:25 +02001345 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001346
1347 if (node->schema->flags & LYS_KEY) {
1348 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1349 return LY_EINVAL;
1350 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1351 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1352 return LY_EINVAL;
1353 }
1354
Michal Vasko0249f7c2020-03-05 16:36:40 +01001355 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1356
Michal Vaskof03ed032020-03-04 13:31:44 +01001357 if (node->parent || node->prev->next) {
1358 lyd_unlink_tree(node);
1359 }
1360
1361 while (node) {
1362 iter = node->next;
1363 lyd_unlink_tree(node);
1364
1365 lyd_insert_after_node(sibling, node);
1366 /* move the anchor accordingly */
1367 sibling = node;
1368
1369 node = iter;
1370 }
1371 return LY_SUCCESS;
1372}
1373
1374API void
1375lyd_unlink_tree(struct lyd_node *node)
1376{
1377 struct lyd_node *iter;
1378
1379 if (!node) {
1380 return;
1381 }
1382
1383 /* unlink from siblings */
1384 if (node->prev->next) {
1385 node->prev->next = node->next;
1386 }
1387 if (node->next) {
1388 node->next->prev = node->prev;
1389 } else {
1390 /* unlinking the last node */
1391 if (node->parent) {
1392 iter = node->parent->child;
1393 } else {
1394 iter = node->prev;
1395 while (iter->prev != node) {
1396 iter = iter->prev;
1397 }
1398 }
1399 /* update the "last" pointer from the first node */
1400 iter->prev = node->prev;
1401 }
1402
1403 /* unlink from parent */
1404 if (node->parent) {
1405 if (node->parent->child == node) {
1406 /* the node is the first child */
1407 node->parent->child = node->next;
1408 }
1409
1410 lyd_unlink_hash(node);
1411
1412 /* check for keyless list and update its hash */
1413 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001414 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001415 lyd_hash(iter);
1416 }
1417 }
1418
1419 node->parent = NULL;
1420 }
1421
1422 node->next = NULL;
1423 node->prev = node;
1424}
1425
Michal Vasko90932a92020-02-12 14:33:03 +01001426LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001427lyd_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 +01001428 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 +01001429 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001430{
1431 LY_ERR ret;
1432 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001434 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001437
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001438 LY_ARRAY_FOR(mod->compiled->exts, u) {
1439 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1440 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001441 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001442 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001443 break;
1444 }
1445 }
1446 if (!ant) {
1447 /* attribute is not defined as a metadata annotation (RFC 7952) */
1448 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1449 mod->name, name_len, name);
1450 return LY_EINVAL;
1451 }
1452
Michal Vasko9f96a052020-03-10 09:41:45 +01001453 mt = calloc(1, sizeof *mt);
1454 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1455 mt->parent = parent;
1456 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001457 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 +01001458 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001460 return ret;
1461 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001463
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001464 /* insert as the last attribute */
1465 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001466 if (parent->meta) {
1467 for (last = parent->meta; last->next; last = last->next);
1468 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001469 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001471 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 } else if (*meta) {
1473 for (last = *meta; last->next; last = last->next);
1474 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001475 }
1476
1477 /* remove default flags from NP containers */
1478 while (parent && (parent->flags & LYD_DEFAULT)) {
1479 parent->flags &= ~LYD_DEFAULT;
1480 parent = (struct lyd_node *)parent->parent;
1481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 if (meta) {
1484 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001485 }
1486 return ret;
1487}
1488
Michal Vasko52927e22020-03-16 17:26:14 +01001489LY_ERR
1490ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1491 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1492 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1493{
1494 struct ly_attr *at, *last;
1495 struct lyd_node_opaq *opaq;
1496
1497 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1498 assert(name && name_len);
1499 assert((prefix_len && ns) || (!prefix_len && !ns));
1500
1501 if (!value_len) {
1502 value = "";
1503 }
1504
1505 at = calloc(1, sizeof *at);
1506 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1507 at->parent = (struct lyd_node_opaq *)parent;
1508 at->name = lydict_insert(ctx, name, name_len);
1509 if (dynamic && *dynamic) {
1510 at->value = lydict_insert_zc(ctx, (char *)value);
1511 *dynamic = 0;
1512 } else {
1513 at->value = lydict_insert(ctx, value, value_len);
1514 }
1515
1516 at->format = format;
1517 at->val_prefs = val_prefs;
1518 if (ns) {
1519 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1520 at->prefix.ns = lydict_insert(ctx, ns, 0);
1521 }
1522
1523 /* insert as the last attribute */
1524 if (parent) {
1525 opaq = (struct lyd_node_opaq *)parent;
1526 if (opaq->attr) {
1527 for (last = opaq->attr; last->next; last = last->next);
1528 last->next = at;
1529 } else {
1530 opaq->attr = at;
1531 }
1532 } else if (*attr) {
1533 for (last = *attr; last->next; last = last->next);
1534 last->next = at;
1535 }
1536
1537 if (attr) {
1538 *attr = at;
1539 }
1540 return LY_SUCCESS;
1541}
1542
Radek Krejci084289f2019-07-09 17:35:30 +02001543API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001544lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001545{
Michal Vasko004d3152020-06-11 19:59:22 +02001546 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001547
Michal Vasko004d3152020-06-11 19:59:22 +02001548 if (ly_path_eval(path, tree, &target)) {
1549 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001550 }
1551
Michal Vasko004d3152020-06-11 19:59:22 +02001552 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001553}
1554
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001555API LY_ERR
1556lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1557{
1558 const struct lyd_node *iter1, *iter2;
1559 struct lyd_node_term *term1, *term2;
1560 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001561 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001562 struct lysc_type *type;
1563 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001564
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001565 if (!node1 || !node2) {
1566 if (node1 == node2) {
1567 return LY_SUCCESS;
1568 } else {
1569 return LY_ENOT;
1570 }
1571 }
1572
Michal Vasko52927e22020-03-16 17:26:14 +01001573 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001574 return LY_ENOT;
1575 }
1576
1577 if (node1->hash != node2->hash) {
1578 return LY_ENOT;
1579 }
Michal Vasko52927e22020-03-16 17:26:14 +01001580 /* 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 +02001581
Michal Vasko52927e22020-03-16 17:26:14 +01001582 if (!node1->schema) {
1583 opaq1 = (struct lyd_node_opaq *)node1;
1584 opaq2 = (struct lyd_node_opaq *)node2;
1585 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001586 return LY_ENOT;
1587 }
Michal Vasko52927e22020-03-16 17:26:14 +01001588 switch (opaq1->format) {
1589 case LYD_XML:
1590 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1591 return LY_ENOT;
1592 }
1593 break;
1594 case LYD_SCHEMA:
1595 /* not allowed */
1596 LOGINT(LYD_NODE_CTX(node1));
1597 return LY_EINT;
1598 }
1599 if (options & LYD_COMPARE_FULL_RECURSION) {
1600 iter1 = opaq1->child;
1601 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001602 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001603 }
1604 return LY_SUCCESS;
1605 } else {
1606 switch (node1->schema->nodetype) {
1607 case LYS_LEAF:
1608 case LYS_LEAFLIST:
1609 if (options & LYD_COMPARE_DEFAULTS) {
1610 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1611 return LY_ENOT;
1612 }
1613 }
1614
1615 term1 = (struct lyd_node_term*)node1;
1616 term2 = (struct lyd_node_term*)node2;
1617 type = ((struct lysc_node_leaf*)node1->schema)->type;
1618
1619 return type->plugin->compare(&term1->value, &term2->value);
1620 case LYS_CONTAINER:
1621 if (options & LYD_COMPARE_DEFAULTS) {
1622 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1623 return LY_ENOT;
1624 }
1625 }
1626 if (options & LYD_COMPARE_FULL_RECURSION) {
1627 iter1 = ((struct lyd_node_inner*)node1)->child;
1628 iter2 = ((struct lyd_node_inner*)node2)->child;
1629 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001630 }
1631 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001632 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001633 case LYS_ACTION:
1634 if (options & LYD_COMPARE_FULL_RECURSION) {
1635 /* TODO action/RPC
1636 goto all_children_compare;
1637 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001638 }
1639 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001640 case LYS_NOTIF:
1641 if (options & LYD_COMPARE_FULL_RECURSION) {
1642 /* TODO Notification
1643 goto all_children_compare;
1644 */
1645 }
1646 return LY_SUCCESS;
1647 case LYS_LIST:
1648 iter1 = ((struct lyd_node_inner*)node1)->child;
1649 iter2 = ((struct lyd_node_inner*)node2)->child;
1650
1651 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1652 /* lists with keys, their equivalence is based on their keys */
1653 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
1654 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1655 key = key->next) {
1656 if (lyd_compare(iter1, iter2, options)) {
1657 return LY_ENOT;
1658 }
1659 iter1 = iter1->next;
1660 iter2 = iter2->next;
1661 }
1662 } else {
1663 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1664
1665 all_children_compare:
1666 if (!iter1 && !iter2) {
1667 /* no children, nothing to compare */
1668 return LY_SUCCESS;
1669 }
1670
1671 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1672 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1673 return LY_ENOT;
1674 }
1675 }
1676 if (iter1 || iter2) {
1677 return LY_ENOT;
1678 }
1679 }
1680 return LY_SUCCESS;
1681 case LYS_ANYXML:
1682 case LYS_ANYDATA:
1683 any1 = (struct lyd_node_any*)node1;
1684 any2 = (struct lyd_node_any*)node2;
1685
1686 if (any1->value_type != any2->value_type) {
1687 return LY_ENOT;
1688 }
1689 switch (any1->value_type) {
1690 case LYD_ANYDATA_DATATREE:
1691 iter1 = any1->value.tree;
1692 iter2 = any2->value.tree;
1693 goto all_children_compare;
1694 case LYD_ANYDATA_STRING:
1695 case LYD_ANYDATA_XML:
1696 case LYD_ANYDATA_JSON:
1697 len1 = strlen(any1->value.str);
1698 len2 = strlen(any2->value.str);
1699 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1700 return LY_ENOT;
1701 }
1702 return LY_SUCCESS;
1703 #if 0 /* TODO LYB format */
1704 case LYD_ANYDATA_LYB:
1705 int len1 = lyd_lyb_data_length(any1->value.mem);
1706 int len2 = lyd_lyb_data_length(any2->value.mem);
1707 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1708 return LY_ENOT;
1709 }
1710 return LY_SUCCESS;
1711 #endif
1712 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001713 }
1714 }
1715
Michal Vasko52927e22020-03-16 17:26:14 +01001716 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001717 return LY_EINT;
1718}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001719
1720/**
Michal Vasko52927e22020-03-16 17:26:14 +01001721 * @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 +02001722 *
1723 * 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 +02001724 *
1725 * @param[in] node Original node to duplicate
1726 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1727 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1728 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1729 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1730 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001731 */
Michal Vasko52927e22020-03-16 17:26:14 +01001732static LY_ERR
1733lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1734 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001735{
Michal Vasko52927e22020-03-16 17:26:14 +01001736 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001737 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001738 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001739
Michal Vasko52927e22020-03-16 17:26:14 +01001740 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001741
Michal Vasko52927e22020-03-16 17:26:14 +01001742 if (!node->schema) {
1743 dup = calloc(1, sizeof(struct lyd_node_opaq));
1744 } else {
1745 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001746 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001747 case LYS_ACTION:
1748 case LYS_NOTIF:
1749 case LYS_CONTAINER:
1750 case LYS_LIST:
1751 dup = calloc(1, sizeof(struct lyd_node_inner));
1752 break;
1753 case LYS_LEAF:
1754 case LYS_LEAFLIST:
1755 dup = calloc(1, sizeof(struct lyd_node_term));
1756 break;
1757 case LYS_ANYDATA:
1758 case LYS_ANYXML:
1759 dup = calloc(1, sizeof(struct lyd_node_any));
1760 break;
1761 default:
1762 LOGINT(LYD_NODE_CTX(node));
1763 ret = LY_EINT;
1764 goto error;
1765 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001766 }
Michal Vasko52927e22020-03-16 17:26:14 +01001767 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001768
1769 /* TODO implement LYD_DUP_WITH_WHEN */
1770 dup->flags = node->flags;
1771 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01001772 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001773
1774 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
1775
1776 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001777 if (!dup->schema) {
1778 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1779 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1780 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001781
1782 if (options & LYD_DUP_RECURSIVE) {
1783 /* duplicate all the children */
1784 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01001785 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
1786 }
1787 }
1788 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
1789 opaq->format = orig->format;
1790 if (orig->prefix.pref) {
1791 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
1792 }
1793 if (orig->prefix.ns) {
1794 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
1795 }
1796 if (orig->val_prefs) {
1797 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
1798 LY_ARRAY_FOR(orig->val_prefs, u) {
1799 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
1800 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
1801 LY_ARRAY_INCREMENT(opaq->val_prefs);
1802 }
1803 }
1804 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
1805 opaq->ctx = orig->ctx;
1806 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1807 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1808 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1809
1810 term->hash = orig->hash;
1811 term->value.realtype = orig->value.realtype;
1812 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
1813 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
1814 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1815 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1816 struct lyd_node *child;
1817
1818 if (options & LYD_DUP_RECURSIVE) {
1819 /* duplicate all the children */
1820 LY_LIST_FOR(orig->child, child) {
1821 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001822 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001823 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001824 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001825 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01001826 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001827 key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY);
1828 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001829 if (!child) {
1830 /* possibly not keys are present in filtered tree */
1831 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02001832 } else if (child->schema != key) {
1833 /* possibly not all keys are present in filtered tree,
1834 * but there can be also some non-key nodes */
1835 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001836 }
Michal Vasko52927e22020-03-16 17:26:14 +01001837 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001838 child = child->next;
1839 }
1840 }
1841 lyd_hash(dup);
1842 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01001843 struct lyd_node_any *any = (struct lyd_node_any *)dup;
1844 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001845
1846 any->hash = orig->hash;
1847 any->value_type = orig->value_type;
1848 switch (any->value_type) {
1849 case LYD_ANYDATA_DATATREE:
1850 if (orig->value.tree) {
1851 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02001852 if (!any->value.tree) {
1853 /* get the last error's error code recorded by lyd_dup */
1854 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
1855 ret = ei ? ei->prev->no : LY_EOTHER;
1856 goto error;
1857 }
1858 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0 ,error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001859 }
1860 break;
1861 case LYD_ANYDATA_STRING:
1862 case LYD_ANYDATA_XML:
1863 case LYD_ANYDATA_JSON:
1864 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01001865 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02001866 }
1867 break;
1868 }
1869 }
1870
Michal Vasko52927e22020-03-16 17:26:14 +01001871 /* insert */
1872 lyd_insert_node(parent, first, dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001873 lyd_insert_hash(dup);
Michal Vasko52927e22020-03-16 17:26:14 +01001874
1875 if (dup_p) {
1876 *dup_p = dup;
1877 }
1878 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001879
1880error:
Michal Vasko52927e22020-03-16 17:26:14 +01001881 lyd_free_tree(dup);
1882 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001883}
1884
1885API struct lyd_node *
1886lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
1887{
1888 struct ly_ctx *ctx;
1889 const struct lyd_node *orig; /* original node to be duplicated */
1890 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001891 struct lyd_node *top = NULL; /* the most higher created node */
1892 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
1893 int keyless_parent_list = 0;
1894
1895 LY_CHECK_ARG_RET(NULL, node, NULL);
1896 ctx = node->schema->module->ctx;
1897
1898 if (options & LYD_DUP_WITH_PARENTS) {
1899 struct lyd_node_inner *orig_parent, *iter;
1900 int repeat = 1;
1901 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
1902 if (parent && parent->schema == orig_parent->schema) {
1903 /* stop creating parents, connect what we have into the provided parent */
1904 iter = parent;
1905 repeat = 0;
1906 /* get know if there is a keyless list which we will have to rehash */
1907 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001908 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001909 keyless_parent_list = 1;
1910 break;
1911 }
1912 }
1913 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01001914 iter = NULL;
1915 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
1916 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001917 }
1918 if (!local_parent) {
1919 local_parent = iter;
1920 }
1921 if (iter->child) {
1922 /* 1) list - add after keys
1923 * 2) provided parent with some children */
1924 iter->child->prev->next = top;
1925 if (top) {
1926 top->prev = iter->child->prev;
1927 iter->child->prev = top;
1928 }
1929 } else {
1930 iter->child = top;
1931 if (iter->schema->nodetype == LYS_LIST) {
1932 /* keyless list - we will need to rehash it since we are going to add nodes into it */
1933 keyless_parent_list = 1;
1934 }
1935 }
1936 if (top) {
1937 top->parent = iter;
1938 }
1939 top = (struct lyd_node*)iter;
1940 }
1941 if (repeat && parent) {
1942 /* given parent and created parents chain actually do not interconnect */
1943 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
1944 goto error;
1945 }
1946 } else {
1947 local_parent = parent;
1948 }
1949
Radek Krejci22ebdba2019-07-25 13:59:43 +02001950 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01001951 /* if there is no local parent, it will be inserted into first */
1952 LY_CHECK_GOTO(lyd_dup_recursive(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001953 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
1954 break;
1955 }
1956 }
1957 if (keyless_parent_list) {
1958 /* rehash */
1959 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02001960 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001961 lyd_hash((struct lyd_node*)local_parent);
1962 }
1963 }
1964 }
1965 return first;
1966
1967error:
1968 if (top) {
1969 lyd_free_tree(top);
1970 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001971 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001972 }
1973 return NULL;
1974}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001975
1976static LY_ERR
1977lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
1978{
Michal Vasko14654712020-02-06 08:35:21 +01001979 /* ending \0 */
1980 ++reqlen;
1981
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001982 if (reqlen > *buflen) {
1983 if (is_static) {
1984 return LY_EINCOMPLETE;
1985 }
1986
1987 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
1988 if (!*buffer) {
1989 return LY_EMEM;
1990 }
1991
1992 *buflen = reqlen;
1993 }
1994
1995 return LY_SUCCESS;
1996}
1997
1998/**
1999 * @brief Append all list key predicates to path.
2000 *
2001 * @param[in] node Node with keys to print.
2002 * @param[in,out] buffer Buffer to print to.
2003 * @param[in,out] buflen Current buffer length.
2004 * @param[in,out] bufused Current number of characters used in @p buffer.
2005 * @param[in] is_static Whether buffer is static or can be reallocated.
2006 * @return LY_ERR
2007 */
2008static LY_ERR
2009lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2010{
2011 const struct lyd_node *key;
2012 int dynamic = 0;
2013 size_t len;
2014 const char *val;
2015 char quot;
2016 LY_ERR rc;
2017
Michal Vasko14654712020-02-06 08:35:21 +01002018 for (key = lyd_node_children(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002019 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2020 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2021 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2022 if (rc != LY_SUCCESS) {
2023 if (dynamic) {
2024 free((char *)val);
2025 }
2026 return rc;
2027 }
2028
2029 quot = '\'';
2030 if (strchr(val, '\'')) {
2031 quot = '"';
2032 }
2033 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2034
2035 if (dynamic) {
2036 free((char *)val);
2037 }
2038 }
2039
2040 return LY_SUCCESS;
2041}
2042
2043/**
2044 * @brief Append leaf-list value predicate to path.
2045 *
2046 * @param[in] node Node to print.
2047 * @param[in,out] buffer Buffer to print to.
2048 * @param[in,out] buflen Current buffer length.
2049 * @param[in,out] bufused Current number of characters used in @p buffer.
2050 * @param[in] is_static Whether buffer is static or can be reallocated.
2051 * @return LY_ERR
2052 */
2053static LY_ERR
2054lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2055{
2056 int dynamic = 0;
2057 size_t len;
2058 const char *val;
2059 char quot;
2060 LY_ERR rc;
2061
2062 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2063 len = 4 + strlen(val) + 2;
2064 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2065 if (rc != LY_SUCCESS) {
2066 goto cleanup;
2067 }
2068
2069 quot = '\'';
2070 if (strchr(val, '\'')) {
2071 quot = '"';
2072 }
2073 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2074
2075cleanup:
2076 if (dynamic) {
2077 free((char *)val);
2078 }
2079 return rc;
2080}
2081
2082/**
2083 * @brief Append node position (relative to its other instances) predicate to path.
2084 *
2085 * @param[in] node Node to print.
2086 * @param[in,out] buffer Buffer to print to.
2087 * @param[in,out] buflen Current buffer length.
2088 * @param[in,out] bufused Current number of characters used in @p buffer.
2089 * @param[in] is_static Whether buffer is static or can be reallocated.
2090 * @return LY_ERR
2091 */
2092static LY_ERR
2093lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2094{
2095 const struct lyd_node *first, *iter;
2096 size_t len;
2097 int pos;
2098 char *val = NULL;
2099 LY_ERR rc;
2100
2101 if (node->parent) {
2102 first = node->parent->child;
2103 } else {
2104 for (first = node; node->prev->next; node = node->prev);
2105 }
2106 pos = 1;
2107 for (iter = first; iter != node; iter = iter->next) {
2108 if (iter->schema == node->schema) {
2109 ++pos;
2110 }
2111 }
2112 if (asprintf(&val, "%d", pos) == -1) {
2113 return LY_EMEM;
2114 }
2115
2116 len = 1 + strlen(val) + 1;
2117 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2118 if (rc != LY_SUCCESS) {
2119 goto cleanup;
2120 }
2121
2122 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2123
2124cleanup:
2125 free(val);
2126 return rc;
2127}
2128
2129API char *
2130lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2131{
Michal Vasko14654712020-02-06 08:35:21 +01002132 int is_static = 0, i, depth;
2133 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002134 const struct lyd_node *iter;
2135 const struct lys_module *mod;
2136 LY_ERR rc;
2137
2138 LY_CHECK_ARG_RET(NULL, node, NULL);
2139 if (buffer) {
2140 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2141 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002142 } else {
2143 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002144 }
2145
2146 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002147 case LYD_PATH_LOG:
2148 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002149 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2150 ++depth;
2151 }
2152
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002153 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002154 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002155 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002156 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002157iter_print:
2158 /* print prefix and name */
2159 mod = NULL;
2160 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2161 mod = iter->schema->module;
2162 }
2163
2164 /* realloc string */
2165 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2166 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2167 if (rc != LY_SUCCESS) {
2168 break;
2169 }
2170
2171 /* print next node */
2172 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2173
2174 switch (iter->schema->nodetype) {
2175 case LYS_LIST:
2176 if (iter->schema->flags & LYS_KEYLESS) {
2177 /* print its position */
2178 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2179 } else {
2180 /* print all list keys in predicates */
2181 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2182 }
2183 break;
2184 case LYS_LEAFLIST:
2185 if (iter->schema->flags & LYS_CONFIG_W) {
2186 /* print leaf-list value */
2187 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2188 } else {
2189 /* print its position */
2190 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2191 }
2192 break;
2193 default:
2194 /* nothing to print more */
2195 rc = LY_SUCCESS;
2196 break;
2197 }
2198 if (rc != LY_SUCCESS) {
2199 break;
2200 }
2201
Michal Vasko14654712020-02-06 08:35:21 +01002202 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002203 }
2204 break;
2205 }
2206
2207 return buffer;
2208}
Michal Vaskoe444f752020-02-10 12:20:06 +01002209
Michal Vasko9b368d32020-02-14 13:53:31 +01002210LY_ERR
2211lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2212 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002213{
2214 LY_ERR rc;
2215 const struct lyd_node *node = NULL;
2216 struct lyd_node_term *term;
Michal Vaskoe444f752020-02-10 12:20:06 +01002217 struct ly_keys keys = {0};
Michal Vasko90932a92020-02-12 14:33:03 +01002218 struct lyd_value val = {0};
Michal Vaskoe444f752020-02-10 12:20:06 +01002219 size_t i;
Michal Vaskoe444f752020-02-10 12:20:06 +01002220
Michal Vasko9b368d32020-02-14 13:53:31 +01002221 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002222
2223 if (!first) {
2224 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002225 if (match) {
2226 *match = NULL;
2227 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002228 return LY_ENOTFOUND;
2229 }
2230
Michal Vaskoe444f752020-02-10 12:20:06 +01002231 if (key_or_value && !val_len) {
2232 val_len = strlen(key_or_value);
2233 }
2234
2235 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002236 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002237 LY_CHECK_GOTO(rc = lyd_value_store(&val, schema, key_or_value, val_len, 0, lydjson_resolve_prefix, NULL, LYD_JSON), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002238 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
2239 /* parse keys into canonical values */
Michal Vaskod3678892020-05-21 10:06:58 +02002240 LY_CHECK_GOTO(rc = ly_keys_parse(schema, key_or_value, val_len, 1, 1, &keys), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002241 }
2242
2243 /* find first matching value */
2244 LY_LIST_FOR(first, node) {
2245 if (node->schema != schema) {
2246 continue;
2247 }
2248
2249 if ((schema->nodetype == LYS_LIST) && keys.str) {
2250 /* compare all set keys */
2251 for (i = 0; i < keys.key_count; ++i) {
2252 /* find key */
2253 rc = lyd_find_sibling_val(lyd_node_children(node), (struct lysc_node *)keys.keys[i].schema, NULL, 0,
2254 (struct lyd_node **)&term);
2255 if (rc == LY_ENOTFOUND) {
2256 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002257 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002258 }
2259 LY_CHECK_GOTO(rc, cleanup);
2260
2261 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002262 if (!term->value.realtype->plugin->compare(&term->value, &keys.keys[i].val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002263 break;
2264 }
2265 }
2266
2267 if (i < keys.key_count) {
2268 /* not a match */
2269 continue;
2270 }
Michal Vasko90932a92020-02-12 14:33:03 +01002271 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002272 term = (struct lyd_node_term *)node;
2273
2274 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002275 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002276 /* not a match */
2277 continue;
2278 }
2279 }
2280
2281 /* all criteria passed */
2282 break;
2283 }
2284
2285 if (!node) {
2286 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002287 if (match) {
2288 *match = NULL;
2289 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002290 goto cleanup;
2291 }
2292
2293 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002294 if (match) {
2295 *match = (struct lyd_node *)node;
2296 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002297 rc = LY_SUCCESS;
2298
2299cleanup:
2300 ly_keys_clean(&keys);
Michal Vasko90932a92020-02-12 14:33:03 +01002301 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002302 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002303 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002304 return rc;
2305}
2306
2307API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002308lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2309 const char *key_or_value, size_t val_len, struct lyd_node **match)
2310{
2311 const struct lysc_node *schema;
2312
2313 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2314
2315 if (!first) {
2316 /* no data */
2317 *match = NULL;
2318 return LY_ENOTFOUND;
2319 }
2320
2321 /* find schema */
2322 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2323 if (!schema) {
2324 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2325 return LY_EINVAL;
2326 }
2327
2328 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2329}
2330
2331API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002332lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2333{
2334 struct lyd_node **match_p;
2335 struct lyd_node_inner *parent;
2336
Michal Vaskof03ed032020-03-04 13:31:44 +01002337 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002338
Michal Vasko62ed12d2020-05-21 10:08:25 +02002339 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2340 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002341 if (match) {
2342 *match = NULL;
2343 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002344 return LY_ENOTFOUND;
2345 }
2346
2347 /* find first sibling */
2348 if (siblings->parent) {
2349 siblings = siblings->parent->child;
2350 } else {
2351 while (siblings->prev->next) {
2352 siblings = siblings->prev;
2353 }
2354 }
2355
2356 parent = (struct lyd_node_inner *)siblings->parent;
2357 if (parent && parent->children_ht) {
2358 assert(target->hash);
2359
2360 /* find by hash */
2361 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2362 siblings = *match_p;
2363 } else {
2364 /* not found */
2365 siblings = NULL;
2366 }
2367 } else {
2368 /* no children hash table */
2369 for (; siblings; siblings = siblings->next) {
2370 if (!lyd_compare(siblings, target, 0)) {
2371 break;
2372 }
2373 }
2374 }
2375
2376 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002377 if (match) {
2378 *match = NULL;
2379 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002380 return LY_ENOTFOUND;
2381 }
2382
Michal Vasko9b368d32020-02-14 13:53:31 +01002383 if (match) {
2384 *match = (struct lyd_node *)siblings;
2385 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002386 return LY_SUCCESS;
2387}
2388
2389API LY_ERR
2390lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2391{
2392 struct lyd_node_inner *parent;
2393 struct lyd_node *match;
2394 struct lyd_node **match_p;
2395 struct ly_set *ret;
2396
2397 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2398
Michal Vasko62ed12d2020-05-21 10:08:25 +02002399 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2400 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01002401 return LY_ENOTFOUND;
2402 }
2403
2404 ret = ly_set_new();
2405 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2406
2407 /* find first sibling */
2408 if (siblings->parent) {
2409 siblings = siblings->parent->child;
2410 } else {
2411 while (siblings->prev->next) {
2412 siblings = siblings->prev;
2413 }
2414 }
2415
2416 parent = (struct lyd_node_inner *)siblings->parent;
2417 if (parent && parent->children_ht) {
2418 assert(target->hash);
2419
2420 /* find by hash */
2421 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2422 match = *match_p;
2423 } else {
2424 /* not found */
2425 match = NULL;
2426 }
2427 while (match) {
2428 /* add all found nodes into the return set */
2429 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2430 goto error;
2431 }
2432
2433 /* find next instance */
2434 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2435 match = NULL;
2436 } else {
2437 match = *match_p;
2438 }
2439 }
2440 } else {
2441 /* no children hash table */
2442 for (; siblings; siblings = siblings->next) {
2443 if (!lyd_compare(siblings, target, 0)) {
2444 /* a match */
2445 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2446 goto error;
2447 }
2448 }
2449 }
2450 }
2451
2452 if (!ret->count) {
2453 ly_set_free(ret, NULL);
2454 return LY_ENOTFOUND;
2455 }
2456
2457 *set = ret;
2458 return LY_SUCCESS;
2459
2460error:
2461 ly_set_free(ret, NULL);
2462 return LY_EMEM;
2463}
2464
Michal Vasko90932a92020-02-12 14:33:03 +01002465static int
2466lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2467{
2468 struct lysc_node *val1;
2469 struct lyd_node *val2;
2470
2471 val1 = *((struct lysc_node **)val1_p);
2472 val2 = *((struct lyd_node **)val2_p);
2473
2474 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2475
2476 if (val1 == val2->schema) {
2477 /* schema match is enough */
2478 return 1;
2479 } else {
2480 return 0;
2481 }
2482}
2483
2484static LY_ERR
2485lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2486{
2487 struct lyd_node **match_p;
2488 struct lyd_node_inner *parent;
2489 uint32_t hash;
2490 values_equal_cb ht_cb;
2491
2492 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
2493
2494 /* find first sibling */
2495 if (siblings->parent) {
2496 siblings = siblings->parent->child;
2497 } else {
2498 while (siblings->prev->next) {
2499 siblings = siblings->prev;
2500 }
2501 }
2502
2503 parent = (struct lyd_node_inner *)siblings->parent;
2504 if (parent && parent->children_ht) {
2505 /* calculate our hash */
2506 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2507 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2508 hash = dict_hash_multi(hash, NULL, 0);
2509
2510 /* use special hash table function */
2511 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2512
2513 /* find by hash */
2514 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2515 siblings = *match_p;
2516 } else {
2517 /* not found */
2518 siblings = NULL;
2519 }
2520
2521 /* set the original hash table compare function back */
2522 lyht_set_cb(parent->children_ht, ht_cb);
2523 } else {
2524 /* no children hash table */
2525 for (; siblings; siblings = siblings->next) {
2526 if (siblings->schema == schema) {
2527 /* schema match is enough */
2528 break;
2529 }
2530 }
2531 }
2532
2533 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002534 if (match) {
2535 *match = NULL;
2536 }
Michal Vasko90932a92020-02-12 14:33:03 +01002537 return LY_ENOTFOUND;
2538 }
2539
Michal Vasko9b368d32020-02-14 13:53:31 +01002540 if (match) {
2541 *match = (struct lyd_node *)siblings;
2542 }
Michal Vasko90932a92020-02-12 14:33:03 +01002543 return LY_SUCCESS;
2544}
2545
Michal Vaskoe444f752020-02-10 12:20:06 +01002546API LY_ERR
2547lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2548 size_t val_len, struct lyd_node **match)
2549{
2550 LY_ERR rc;
2551 struct lyd_node *target = NULL;
2552
2553 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02002554 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002555 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
2556 return LY_EINVAL;
2557 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
2558 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
2559 return LY_EINVAL;
2560 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
2561 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
2562 lys_nodetype2str(schema->nodetype), __func__);
2563 return LY_EINVAL;
2564 }
2565
Michal Vasko62ed12d2020-05-21 10:08:25 +02002566 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2567 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002568 if (match) {
2569 *match = NULL;
2570 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002571 return LY_ENOTFOUND;
2572 }
2573
Michal Vaskof03ed032020-03-04 13:31:44 +01002574 if (key_or_value && !val_len) {
2575 val_len = strlen(key_or_value);
2576 }
2577
Michal Vasko90932a92020-02-12 14:33:03 +01002578 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01002579 switch (schema->nodetype) {
2580 case LYS_CONTAINER:
2581 case LYS_ANYXML:
2582 case LYS_ANYDATA:
2583 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01002584 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01002585 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01002586 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02002587 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01002588 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002589 break;
2590 case LYS_LEAFLIST:
2591 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02002592 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
2593 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
2594 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002595 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01002596 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002597 if (schema->nodetype == LYS_LIST) {
2598 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002599 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002600 }
2601
2602 /* find it */
2603 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002604 break;
2605 default:
2606 /* unreachable */
2607 LOGINT(schema->module->ctx);
2608 return LY_EINT;
2609 }
2610
Michal Vaskoe444f752020-02-10 12:20:06 +01002611 lyd_free_tree(target);
2612 return rc;
2613}
Michal Vaskoccc02342020-05-21 10:09:21 +02002614
2615API LY_ERR
2616lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2617{
2618 LY_ERR ret = LY_SUCCESS;
2619 struct lyxp_set xp_set;
2620 struct lyxp_expr *exp;
2621 uint32_t i;
2622
2623 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
2624
2625 memset(&xp_set, 0, sizeof xp_set);
2626
2627 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02002628 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02002629 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
2630
2631 /* evaluate expression */
2632 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
2633 LY_CHECK_GOTO(ret, cleanup);
2634
2635 /* allocate return set */
2636 *set = ly_set_new();
2637 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2638
2639 /* transform into ly_set */
2640 if (xp_set.type == LYXP_SET_NODE_SET) {
2641 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
2642 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
2643 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
2644 (*set)->size = xp_set.used;
2645
2646 for (i = 0; i < xp_set.used; ++i) {
2647 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2648 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
2649 }
2650 }
2651 }
2652
2653cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02002654 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002655 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
2656 return ret;
2657}