blob: 2047fe27718be29c27a0ee07ff003ad5b6e0f67e [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020041#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
49#include "xml.h"
50#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020051
Radek Krejci084289f2019-07-09 17:35:30 +020052LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010053lyd_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 +010054 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020055{
Michal Vasko90932a92020-02-12 14:33:03 +010056 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020057 struct ly_err_item *err = NULL;
58 struct ly_ctx *ctx;
59 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020060 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010061 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020062 assert(node);
63
64 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020065
Radek Krejci73dead22019-07-11 16:46:16 +020066 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020067 if (!second) {
68 node->value.realtype = type;
69 }
Michal Vasko90932a92020-02-12 14:33:03 +010070 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020071 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010072 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020073 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020074 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020075 if (!node->parent && lysc_data_parent(node->schema)) {
76 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
77 } else {
78 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
79 }
Radek Krejci73dead22019-07-11 16:46:16 +020080 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020081 }
Radek Krejci73dead22019-07-11 16:46:16 +020082 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010083 } else if (dynamic) {
84 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020085 }
86
87error:
88 return ret;
89}
90
Michal Vasko00cbf532020-06-15 13:58:47 +020091/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020092LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010093lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
94 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
95{
96 LY_ERR ret = LY_SUCCESS;
97 struct ly_err_item *err = NULL;
98 struct ly_ctx *ctx;
99 struct lysc_type *type;
100 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
101
102 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
103
104 ctx = schema->module->ctx;
105 type = ((struct lysc_node_leaf *)schema)->type;
106 val->realtype = type;
107 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
108 val, NULL, &err);
109 if (ret == LY_EINCOMPLETE) {
110 /* this is fine, we do not need it resolved */
111 ret = LY_SUCCESS;
112 } else if (ret && err) {
113 ly_err_print(err);
114 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
115 ly_err_free(err);
116 }
117 if (!ret && dynamic) {
118 *dynamic = 0;
119 }
120
121 return ret;
122}
123
Radek Krejci38d85362019-09-05 16:26:38 +0200124LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200125lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100126 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100127 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200128{
Michal Vasko90932a92020-02-12 14:33:03 +0100129 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200130 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 struct lyext_metadata *ant;
132 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100133 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200134
Michal Vasko9f96a052020-03-10 09:41:45 +0100135 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100136
Michal Vasko9f96a052020-03-10 09:41:45 +0100137 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200138
139 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100140 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200141 }
Michal Vasko90932a92020-02-12 14:33:03 +0100142 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100143 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100144 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200145 if (err) {
146 ly_err_print(err);
147 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
148 ly_err_free(err);
149 }
150 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100151 } else if (dynamic) {
152 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200153 }
154
155error:
156 return ret;
157}
158
Radek Krejci084289f2019-07-09 17:35:30 +0200159API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100160lys_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 +0200161 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
162{
163 LY_ERR rc = LY_SUCCESS;
164 struct ly_err_item *err = NULL;
165 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200166
167 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
168
169 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
170 LOGARG(ctx, node);
171 return LY_EINVAL;
172 }
173
174 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200175 /* just validate, no storing of enything */
176 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
177 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
178 if (rc == LY_EINCOMPLETE) {
179 /* actually success since we do not provide the context tree and call validation with
180 * LY_TYPE_OPTS_INCOMPLETE_DATA */
181 rc = LY_SUCCESS;
182 } else if (rc && err) {
183 if (ctx) {
184 /* log only in case the ctx was provided as input parameter */
185 ly_err_print(err);
186 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200187 }
Radek Krejci73dead22019-07-11 16:46:16 +0200188 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200189 }
190
191 return rc;
192}
193
194API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100195lyd_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 +0100196 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 +0200197{
198 LY_ERR rc;
199 struct ly_err_item *err = NULL;
200 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100201 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200202
203 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
204
205 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200206 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100207 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200208 NULL, NULL, &err);
209 if (rc == LY_EINCOMPLETE) {
210 return rc;
211 } else if (rc) {
212 if (err) {
213 if (ctx) {
214 ly_err_print(err);
215 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200216 }
Radek Krejci73dead22019-07-11 16:46:16 +0200217 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200218 }
Radek Krejci73dead22019-07-11 16:46:16 +0200219 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200220 }
221
222 return LY_SUCCESS;
223}
224
225API LY_ERR
226lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100227 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 +0200228{
229 LY_ERR ret = LY_SUCCESS, rc;
230 struct ly_err_item *err = NULL;
231 struct ly_ctx *ctx;
232 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200233 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100234 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200235
236 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
237
238 ctx = node->schema->module->ctx;
239 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200240 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 +0100241 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200242 if (rc == LY_EINCOMPLETE) {
243 ret = rc;
244 /* continue with comparing, just remember what to return if storing is ok */
245 } else if (rc) {
246 /* value to compare is invalid */
247 ret = LY_EINVAL;
248 if (err) {
249 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200250 }
Radek Krejci73dead22019-07-11 16:46:16 +0200251 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200252 }
253
254 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200255 if (type->plugin->compare(&node->value, &data)) {
256 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200257 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200258 }
Radek Krejci084289f2019-07-09 17:35:30 +0200259
260cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200261 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200262
263 return ret;
264}
265
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200266API const char *
267lyd_value2str(const struct lyd_node_term *node, int *dynamic)
268{
269 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
270
271 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
272}
273
274API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100275lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200276{
Michal Vasko9f96a052020-03-10 09:41:45 +0100277 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200278
Michal Vasko9f96a052020-03-10 09:41:45 +0100279 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200280}
281
Radek Krejci7931b192020-06-25 17:05:03 +0200282static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200283lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200284{
Radek Krejcie7b95092019-05-15 11:03:07 +0200285
Radek Krejci7931b192020-06-25 17:05:03 +0200286 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200288 const char *path = in->method.fpath.filepath;
289 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200290
291 /* ignore trailing whitespaces */
292 for (; len > 0 && isspace(path[len - 1]); len--);
293
294 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
295 format = LYD_XML;
296#if 0
297 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
298 format = LYD_JSON;
Radek Krejci7931b192020-06-25 17:05:03 +0200299#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200300 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
301 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200302 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 }
304
Radek Krejci7931b192020-06-25 17:05:03 +0200305 return format;
306}
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
Radek Krejci7931b192020-06-25 17:05:03 +0200308API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200309lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
310 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200311{
312 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
313 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
314 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
315
316 format = lyd_parse_get_format(in, format);
317 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
318
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 /* remember input position */
320 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200321
322 switch (format) {
323 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200324 return lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200325#if 0
326 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 return lyd_parse_json_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200328#endif
329 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 return lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200331 case LYD_SCHEMA:
332 LOGINT_RET(ctx);
333 }
334
335 /* TODO move here the top-level validation from parser_xml.c's lyd_parse_xml_data() and make
336 * it common for all the lyd_parse_*_data() functions */
337
338 LOGINT_RET(ctx);
339}
340
341API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200342lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
343 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200344{
345 LY_ERR ret;
346 struct ly_in *in;
347
348 LY_CHECK_RET(ly_in_new_memory(data, &in));
349 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
350
351 ly_in_free(in, 0);
352 return ret;
353}
354
355API LY_ERR
356lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
357{
358 LY_ERR ret;
359 struct ly_in *in;
360
361 LY_CHECK_RET(ly_in_new_fd(fd, &in));
362 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
363
364 ly_in_free(in, 0);
365 return ret;
366}
367
368API LY_ERR
369lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
370{
371 LY_ERR ret;
372 struct ly_in *in;
373
374 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
375 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
376
377 ly_in_free(in, 0);
378 return ret;
379}
380
Radek Krejci7931b192020-06-25 17:05:03 +0200381API LY_ERR
382lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
383{
384 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
385
386 format = lyd_parse_get_format(in, format);
387 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
388
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 /* remember input position */
390 in->func_start = in->current;
391
Radek Krejci7931b192020-06-25 17:05:03 +0200392 switch (format) {
393 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200394 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200395#if 0
396 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200397 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200398#endif
399 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200401 case LYD_SCHEMA:
402 LOGINT_RET(ctx);
403 }
404
405 LOGINT_RET(ctx);
406}
407
408API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200409lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
410 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200411{
412 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
413 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
414
415 format = lyd_parse_get_format(in, format);
416 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
417
Michal Vasko63f3d842020-07-08 10:10:14 +0200418 /* remember input position */
419 in->func_start = in->current;
420
Radek Krejci7931b192020-06-25 17:05:03 +0200421 switch (format) {
422 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200424#if 0
425 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200427#endif
428 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200430 case LYD_SCHEMA:
431 LOGINT_RET(LYD_NODE_CTX(request));
432 }
433
434 LOGINT_RET(LYD_NODE_CTX(request));
435}
436
437API LY_ERR
438lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
441
442 format = lyd_parse_get_format(in, format);
443 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
444
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 /* remember input position */
446 in->func_start = in->current;
447
Radek Krejci7931b192020-06-25 17:05:03 +0200448 switch (format) {
449 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200451#if 0
452 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200454#endif
455 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200457 case LYD_SCHEMA:
458 LOGINT_RET(ctx);
459 }
460
461 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200462}
Radek Krejci084289f2019-07-09 17:35:30 +0200463
Michal Vasko90932a92020-02-12 14:33:03 +0100464LY_ERR
465lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
466 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
467{
468 LY_ERR ret;
469 struct lyd_node_term *term;
470
Michal Vasko9b368d32020-02-14 13:53:31 +0100471 assert(schema->nodetype & LYD_NODE_TERM);
472
Michal Vasko90932a92020-02-12 14:33:03 +0100473 term = calloc(1, sizeof *term);
474 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
475
476 term->schema = schema;
477 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100478 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100479
480 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
481 if (ret && (ret != LY_EINCOMPLETE)) {
482 free(term);
483 return ret;
484 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100485 lyd_hash((struct lyd_node *)term);
486
487 *node = (struct lyd_node *)term;
488 return ret;
489}
490
491LY_ERR
492lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
493{
494 LY_ERR ret;
495 struct lyd_node_term *term;
496 struct lysc_type *type;
497
498 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200499 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100500
501 term = calloc(1, sizeof *term);
502 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
503
504 term->schema = schema;
505 term->prev = (struct lyd_node *)term;
506 term->flags = LYD_NEW;
507
508 type = ((struct lysc_node_leaf *)schema)->type;
509 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
510 if (ret) {
511 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
512 free(term);
513 return ret;
514 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200515 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100516 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100517
518 *node = (struct lyd_node *)term;
519 return ret;
520}
521
522LY_ERR
523lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
524{
525 struct lyd_node_inner *in;
526
Michal Vasko9b368d32020-02-14 13:53:31 +0100527 assert(schema->nodetype & LYD_NODE_INNER);
528
Michal Vasko90932a92020-02-12 14:33:03 +0100529 in = calloc(1, sizeof *in);
530 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
531
532 in->schema = schema;
533 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100534 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100535
Michal Vasko9b368d32020-02-14 13:53:31 +0100536 /* do not hash list with keys, we need them for the hash */
537 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
538 lyd_hash((struct lyd_node *)in);
539 }
Michal Vasko90932a92020-02-12 14:33:03 +0100540
541 *node = (struct lyd_node *)in;
542 return LY_SUCCESS;
543}
544
Michal Vasko90932a92020-02-12 14:33:03 +0100545LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200546lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100547{
548 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100549 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100551
Michal Vasko004d3152020-06-11 19:59:22 +0200552 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100553
554 /* create list */
555 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
556
Michal Vasko90932a92020-02-12 14:33:03 +0100557 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200558 LY_ARRAY_FOR(predicates, u) {
559 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100560 lyd_insert_node(list, NULL, key);
561 }
562
Michal Vasko9b368d32020-02-14 13:53:31 +0100563 /* hash having all the keys */
564 lyd_hash(list);
565
Michal Vasko90932a92020-02-12 14:33:03 +0100566 /* success */
567 *node = list;
568 list = NULL;
569
570cleanup:
571 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200572 return ret;
573}
574
575static LY_ERR
576lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
577{
578 LY_ERR ret = LY_SUCCESS;
579 struct lyxp_expr *expr = NULL;
580 uint16_t exp_idx = 0;
581 enum ly_path_pred_type pred_type = 0;
582 struct ly_path_predicate *predicates = NULL;
583
584 /* parse keys */
585 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
586 LY_PATH_PRED_KEYS, &expr), cleanup);
587
588 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200589 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
590 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200591
592 /* create the list node */
593 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
594
595cleanup:
596 lyxp_expr_free(schema->module->ctx, expr);
597 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100598 return ret;
599}
600
601LY_ERR
602lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
603{
604 struct lyd_node_any *any;
605
Michal Vasko9b368d32020-02-14 13:53:31 +0100606 assert(schema->nodetype & LYD_NODE_ANY);
607
Michal Vasko90932a92020-02-12 14:33:03 +0100608 any = calloc(1, sizeof *any);
609 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
610
611 any->schema = schema;
612 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100613 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100614
615 any->value.xml = value;
616 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100617 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100618
619 *node = (struct lyd_node *)any;
620 return LY_SUCCESS;
621}
622
Michal Vasko52927e22020-03-16 17:26:14 +0100623LY_ERR
624lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
625 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
626 const char *ns, struct lyd_node **node)
627{
628 struct lyd_node_opaq *opaq;
629
630 assert(ctx && name && name_len && ns);
631
632 if (!value_len) {
633 value = "";
634 }
635
636 opaq = calloc(1, sizeof *opaq);
637 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
638
639 opaq->prev = (struct lyd_node *)opaq;
640
641 opaq->name = lydict_insert(ctx, name, name_len);
642 opaq->format = format;
643 if (pref_len) {
644 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
645 }
646 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
647 opaq->val_prefs = val_prefs;
648 if (dynamic && *dynamic) {
649 opaq->value = lydict_insert_zc(ctx, (char *)value);
650 *dynamic = 0;
651 } else {
652 opaq->value = lydict_insert(ctx, value, value_len);
653 }
654 opaq->ctx = ctx;
655
656 *node = (struct lyd_node *)opaq;
657 return LY_SUCCESS;
658}
659
Michal Vasko3a41dff2020-07-15 14:30:28 +0200660API LY_ERR
661lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100662{
663 struct lyd_node *ret = NULL;
664 const struct lysc_node *schema;
665 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
666
Michal Vasko6027eb92020-07-15 16:37:30 +0200667 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100668
Michal Vaskof03ed032020-03-04 13:31:44 +0100669 if (!module) {
670 module = parent->schema->module;
671 }
672
Michal Vasko3a41dff2020-07-15 14:30:28 +0200673 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
674 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
675 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100676
Michal Vasko3a41dff2020-07-15 14:30:28 +0200677 LY_CHECK_RET(lyd_create_inner(schema, &ret));
678 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100679 lyd_insert_node(parent, NULL, ret);
680 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200681
682 if (node) {
683 *node = ret;
684 }
685 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100686}
687
Michal Vasko3a41dff2020-07-15 14:30:28 +0200688API LY_ERR
689lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100690{
691 struct lyd_node *ret = NULL, *key;
692 const struct lysc_node *schema, *key_s;
693 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
694 va_list ap;
695 const char *key_val;
696 LY_ERR rc = LY_SUCCESS;
697
Michal Vasko6027eb92020-07-15 16:37:30 +0200698 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100699
Michal Vaskof03ed032020-03-04 13:31:44 +0100700 if (!module) {
701 module = parent->schema->module;
702 }
703
Michal Vasko013a8182020-03-03 10:46:53 +0100704 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200705 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100706
707 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200708 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100709
Michal Vasko3a41dff2020-07-15 14:30:28 +0200710 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100711
712 /* create and insert all the keys */
713 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
714 key_val = va_arg(ap, const char *);
715
Michal Vaskof03ed032020-03-04 13:31:44 +0100716 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 +0200717 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
718 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100719 lyd_insert_node(ret, NULL, key);
720 }
721
Michal Vasko013a8182020-03-03 10:46:53 +0100722 if (parent) {
723 lyd_insert_node(parent, NULL, ret);
724 }
725
726cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200727 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100728 if (rc) {
729 lyd_free_tree(ret);
730 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200731 } else if (node) {
732 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100733 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200734 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100735}
736
Michal Vasko3a41dff2020-07-15 14:30:28 +0200737API LY_ERR
738lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
739 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100740{
741 struct lyd_node *ret = NULL;
742 const struct lysc_node *schema;
743 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
744
Michal Vasko6027eb92020-07-15 16:37:30 +0200745 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100746
Michal Vaskof03ed032020-03-04 13:31:44 +0100747 if (!module) {
748 module = parent->schema->module;
749 }
Michal Vasko004d3152020-06-11 19:59:22 +0200750 if (!keys) {
751 keys = "";
752 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100753
Michal Vasko004d3152020-06-11 19:59:22 +0200754 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100755 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200756 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100757
Michal Vasko004d3152020-06-11 19:59:22 +0200758 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
759 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200760 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200761 } else {
762 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200763 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200764 }
Michal Vasko004d3152020-06-11 19:59:22 +0200765 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100766 lyd_insert_node(parent, NULL, ret);
767 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200768
769 if (node) {
770 *node = ret;
771 }
772 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100773}
774
Michal Vasko3a41dff2020-07-15 14:30:28 +0200775API LY_ERR
776lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
777 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100778{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200779 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100780 struct lyd_node *ret = NULL;
781 const struct lysc_node *schema;
782 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
783
Michal Vasko6027eb92020-07-15 16:37:30 +0200784 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100785
Michal Vaskof03ed032020-03-04 13:31:44 +0100786 if (!module) {
787 module = parent->schema->module;
788 }
789
Michal Vasko013a8182020-03-03 10:46:53 +0100790 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200791 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100792
Michal Vaskocbff3e92020-05-27 12:56:41 +0200793 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200794 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200795 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100796 lyd_insert_node(parent, NULL, ret);
797 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200798
799 if (node) {
800 *node = ret;
801 }
802 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100803}
804
Michal Vasko3a41dff2020-07-15 14:30:28 +0200805API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100806lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100808{
809 struct lyd_node *ret = NULL;
810 const struct lysc_node *schema;
811 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
812
Michal Vasko6027eb92020-07-15 16:37:30 +0200813 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100814
Michal Vaskof03ed032020-03-04 13:31:44 +0100815 if (!module) {
816 module = parent->schema->module;
817 }
818
Michal Vasko013a8182020-03-03 10:46:53 +0100819 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200820 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100821
Michal Vasko3a41dff2020-07-15 14:30:28 +0200822 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
823 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100824 lyd_insert_node(parent, NULL, ret);
825 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200826
827 if (node) {
828 *node = ret;
829 }
830 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100831}
832
Michal Vasko4490d312020-06-16 13:08:55 +0200833/**
834 * @brief Update node value.
835 *
836 * @param[in] node Node to update.
837 * @param[in] value New value to set.
838 * @param[in] value_type Type of @p value for any node.
839 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
840 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
841 * @return LY_ERR value.
842 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200843static LY_ERR
844lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
845 struct lyd_node **new_parent, struct lyd_node **new_node)
846{
847 LY_ERR ret = LY_SUCCESS;
848 struct lyd_node *new_any;
849
850 switch (node->schema->nodetype) {
851 case LYS_CONTAINER:
852 case LYS_NOTIF:
853 case LYS_RPC:
854 case LYS_ACTION:
855 case LYS_LIST:
856 case LYS_LEAFLIST:
857 /* if it exists, there is nothing to update */
858 *new_parent = NULL;
859 *new_node = NULL;
860 break;
861 case LYS_LEAF:
862 ret = lyd_change_term(node, value);
863 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
864 /* there was an actual change (at least of the default flag) */
865 *new_parent = node;
866 *new_node = node;
867 ret = LY_SUCCESS;
868 } else if (ret == LY_ENOT) {
869 /* no change */
870 *new_parent = NULL;
871 *new_node = NULL;
872 ret = LY_SUCCESS;
873 } /* else error */
874 break;
875 case LYS_ANYDATA:
876 case LYS_ANYXML:
877 /* create a new any node */
878 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
879
880 /* compare with the existing one */
881 if (lyd_compare(node, new_any, 0)) {
882 /* not equal, switch values (so that we can use generic node free) */
883 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
884 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
885 ((struct lyd_node_any *)node)->value.str = value;
886 ((struct lyd_node_any *)node)->value_type = value_type;
887
888 *new_parent = node;
889 *new_node = node;
890 } else {
891 /* they are equal */
892 *new_parent = NULL;
893 *new_node = NULL;
894 }
895 lyd_free_tree(new_any);
896 break;
897 default:
898 LOGINT(LYD_NODE_CTX(node));
899 ret = LY_EINT;
900 break;
901 }
902
903 return ret;
904}
905
Michal Vasko3a41dff2020-07-15 14:30:28 +0200906API LY_ERR
907lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
908 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200909{
910 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200911 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200912 const char *prefix, *tmp;
913 char *str;
914 size_t pref_len, name_len;
915
Michal Vasko3a41dff2020-07-15 14:30:28 +0200916 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200917
918 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200919
920 /* parse the name */
921 tmp = name;
922 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
923 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200924 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200925 }
926
927 /* find the module */
928 if (prefix) {
Michal Vasko0b245382020-07-09 15:43:52 +0200929 str = strndup(prefix, pref_len);
Michal Vaskod86997b2020-05-26 15:19:54 +0200930 module = ly_ctx_get_module_implemented(ctx, str);
931 free(str);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200932 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +0200933 }
934
935 /* set value if none */
936 if (!val_str) {
937 val_str = "";
938 }
939
Michal Vasko3a41dff2020-07-15 14:30:28 +0200940 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL,
941 lydjson_resolve_prefix, NULL, LYD_JSON, parent->schema));
942
943 if (meta) {
944 *meta = ret;
945 }
946 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +0200947}
948
Michal Vasko3a41dff2020-07-15 14:30:28 +0200949API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200950lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200951 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200952{
953 struct lyd_node *ret = NULL;
954
Michal Vasko6027eb92020-07-15 16:37:30 +0200955 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200956
957 if (!ctx) {
958 ctx = LYD_NODE_CTX(parent);
959 }
960 if (!value) {
961 value = "";
962 }
963
Michal Vasko3a41dff2020-07-15 14:30:28 +0200964 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0,
965 module_name, &ret));
966 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200967 lyd_insert_node(parent, NULL, ret);
968 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969
970 if (node) {
971 *node = ret;
972 }
973 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200974}
975
Michal Vasko3a41dff2020-07-15 14:30:28 +0200976API LY_ERR
977lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
978 struct ly_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +0200979{
980 struct ly_attr *ret = NULL;
981 const struct ly_ctx *ctx;
982 const char *prefix, *tmp;
983 size_t pref_len, name_len;
984
Michal Vasko3a41dff2020-07-15 14:30:28 +0200985 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200986
987 ctx = LYD_NODE_CTX(parent);
988
989 /* parse the name */
990 tmp = name;
991 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
992 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200993 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200994 }
995
996 /* set value if none */
997 if (!val_str) {
998 val_str = "";
999 }
1000
Michal Vasko3a41dff2020-07-15 14:30:28 +02001001 LY_CHECK_RET(ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL,
1002 prefix, pref_len, module_name));
1003
1004 if (attr) {
1005 *attr = ret;
1006 }
1007 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001008}
1009
1010API LY_ERR
1011lyd_change_term(struct lyd_node *term, const char *val_str)
1012{
1013 LY_ERR ret = LY_SUCCESS;
1014 struct lysc_type *type;
1015 struct lyd_node_term *t;
1016 struct lyd_node *parent;
1017 struct lyd_value val = {0};
1018 int dflt_change, val_change;
1019
1020 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1021
1022 if (!val_str) {
1023 val_str = "";
1024 }
1025 t = (struct lyd_node_term *)term;
1026 type = ((struct lysc_node_leaf *)term->schema)->type;
1027
1028 /* parse the new value */
1029 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1030 LYD_JSON), cleanup);
1031
1032 /* compare original and new value */
1033 if (type->plugin->compare(&t->value, &val)) {
1034 /* values differ, switch them */
1035 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1036 t->value = val;
1037 memset(&val, 0, sizeof val);
1038 val_change = 1;
1039 } else {
1040 val_change = 0;
1041 }
1042
1043 /* always clear the default flag */
1044 if (term->flags & LYD_DEFAULT) {
1045 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1046 parent->flags &= ~LYD_DEFAULT;
1047 }
1048 dflt_change = 1;
1049 } else {
1050 dflt_change = 0;
1051 }
1052
1053 if (val_change || dflt_change) {
1054 /* make the node non-validated */
1055 term->flags &= LYD_NEW;
1056 }
1057
1058 if (val_change) {
1059 if (term->schema->nodetype == LYS_LEAFLIST) {
1060 /* leaf-list needs to be hashed again and re-inserted into parent */
1061 lyd_unlink_hash(term);
1062 lyd_hash(term);
1063 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1064 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1065 /* list needs to be updated if its key was changed */
1066 assert(term->parent->schema->nodetype == LYS_LIST);
1067 lyd_unlink_hash((struct lyd_node *)term->parent);
1068 lyd_hash((struct lyd_node *)term->parent);
1069 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1070 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1071 }
1072
1073 /* retrun value */
1074 if (!val_change) {
1075 if (dflt_change) {
1076 /* only default flag change */
1077 ret = LY_EEXIST;
1078 } else {
1079 /* no change */
1080 ret = LY_ENOT;
1081 }
1082 } /* else value changed, LY_SUCCESS */
1083
1084cleanup:
1085 type->plugin->free(LYD_NODE_CTX(term), &val);
1086 return ret;
1087}
1088
Michal Vasko41586352020-07-13 13:54:25 +02001089API LY_ERR
1090lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1091{
1092 LY_ERR ret = LY_SUCCESS;
1093 struct lyd_meta *m2;
1094 struct lyd_value val;
1095 int val_change;
1096
1097 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1098
1099 if (!val_str) {
1100 val_str = "";
1101 }
1102
1103 /* parse the new value into a new meta structure */
1104 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
1105 strlen(val_str), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, NULL), cleanup);
1106
1107 /* compare original and new value */
1108 if (lyd_compare_meta(meta, m2)) {
1109 /* values differ, switch them */
1110 val = meta->value;
1111 meta->value = m2->value;
1112 m2->value = val;
1113 val_change = 1;
1114 } else {
1115 val_change = 0;
1116 }
1117
1118 /* retrun value */
1119 if (!val_change) {
1120 /* no change */
1121 ret = LY_ENOT;
1122 } /* else value changed, LY_SUCCESS */
1123
1124cleanup:
1125 return ret;
1126}
1127
Michal Vasko3a41dff2020-07-15 14:30:28 +02001128API LY_ERR
1129lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1130 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001131{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001132 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001133}
1134
1135API LY_ERR
1136lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1137 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1138{
1139 LY_ERR ret = LY_SUCCESS, r;
1140 struct lyxp_expr *exp = NULL;
1141 struct ly_path *p = NULL;
1142 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1143 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001144 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001145 struct ly_path_predicate *pred;
1146
1147 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1148
1149 if (!ctx) {
1150 ctx = LYD_NODE_CTX(parent);
1151 }
1152
1153 /* parse path */
1154 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1155 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1156
1157 /* compile path */
1158 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1159 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1160 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1161
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001162 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1163 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001164 && !(options & LYD_NEWOPT_OPAQ)) {
1165 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1166 lys_nodetype2str(schema->nodetype), schema->name);
1167 ret = LY_EINVAL;
1168 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001169 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001170 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001171 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1172 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001173
1174 if (!value) {
1175 value = "";
1176 }
1177
1178 r = LY_SUCCESS;
1179 if (options & LYD_NEWOPT_OPAQ) {
1180 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1181 }
1182 if (!r) {
1183 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1184 NULL, LYD_JSON), cleanup);
1185 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1186 }
1187
1188 /* try to find any existing nodes in the path */
1189 if (parent) {
1190 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1191 if (ret == LY_SUCCESS) {
1192 /* the node exists, are we supposed to update it or is it just a default? */
1193 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1194 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1195 ret = LY_EEXIST;
1196 goto cleanup;
1197 }
1198
1199 /* update the existing node */
1200 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1201 goto cleanup;
1202 } else if (ret == LY_EINCOMPLETE) {
1203 /* some nodes were found, adjust the iterator to the next segment */
1204 ++path_idx;
1205 } else if (ret == LY_ENOTFOUND) {
1206 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001207 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001208 node = parent;
1209 }
1210 } else {
1211 /* error */
1212 goto cleanup;
1213 }
1214 }
1215
1216 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001217 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001218 cur_parent = node;
1219 schema = p[path_idx].node;
1220
1221 switch (schema->nodetype) {
1222 case LYS_LIST:
1223 if (!(schema->flags & LYS_KEYLESS)) {
1224 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1225 /* creating opaque list without keys */
1226 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1227 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1228 } else {
1229 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1230 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1231 }
1232 break;
1233 }
1234 /* fallthrough */
1235 case LYS_CONTAINER:
1236 case LYS_NOTIF:
1237 case LYS_RPC:
1238 case LYS_ACTION:
1239 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1240 break;
1241 case LYS_LEAFLIST:
1242 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1243 /* creating opaque leaf-list without value */
1244 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1245 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1246 } else {
1247 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1248 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1249 }
1250 break;
1251 case LYS_LEAF:
1252 /* make there is some value */
1253 if (!value) {
1254 value = "";
1255 }
1256
1257 r = LY_SUCCESS;
1258 if (options & LYD_NEWOPT_OPAQ) {
1259 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1260 }
1261 if (!r) {
1262 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1263 LYD_JSON, &node), cleanup);
1264 } else {
1265 /* creating opaque leaf without value */
1266 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1267 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1268 }
1269 break;
1270 case LYS_ANYDATA:
1271 case LYS_ANYXML:
1272 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1273 break;
1274 default:
1275 LOGINT(ctx);
1276 ret = LY_EINT;
1277 goto cleanup;
1278 }
1279
1280 if (cur_parent) {
1281 /* connect to the parent */
1282 lyd_insert_node(cur_parent, NULL, node);
1283 } else if (parent) {
1284 /* connect to top-level siblings */
1285 lyd_insert_node(NULL, &parent, node);
1286 }
1287
1288 /* update remembered nodes */
1289 if (!nparent) {
1290 nparent = node;
1291 }
1292 nnode = node;
1293 }
1294
1295cleanup:
1296 lyxp_expr_free(ctx, exp);
1297 ly_path_free(ctx, p);
1298 if (!ret) {
1299 /* set out params only on success */
1300 if (new_parent) {
1301 *new_parent = nparent;
1302 }
1303 if (new_node) {
1304 *new_node = nnode;
1305 }
1306 }
1307 return ret;
1308}
1309
Michal Vasko90932a92020-02-12 14:33:03 +01001310struct lyd_node *
1311lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
1312{
1313 const struct lysc_node *prev_key;
1314 struct lyd_node *match = NULL;
1315
1316 if (!first_sibling) {
1317 return NULL;
1318 }
1319
1320 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
1321 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
1322 }
1323
1324 return match;
1325}
1326
1327/**
1328 * @brief Insert node after a sibling.
1329 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001330 * Handles inserting into NP containers and key-less lists.
1331 *
Michal Vasko90932a92020-02-12 14:33:03 +01001332 * @param[in] sibling Sibling to insert after.
1333 * @param[in] node Node to insert.
1334 */
1335static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001336lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001337{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001338 struct lyd_node_inner *par;
1339
Michal Vasko90932a92020-02-12 14:33:03 +01001340 assert(!node->next && (node->prev == node));
1341
1342 node->next = sibling->next;
1343 node->prev = sibling;
1344 sibling->next = node;
1345 if (node->next) {
1346 /* sibling had a succeeding node */
1347 node->next->prev = node;
1348 } else {
1349 /* sibling was last, find first sibling and change its prev */
1350 if (sibling->parent) {
1351 sibling = sibling->parent->child;
1352 } else {
1353 for (; sibling->prev->next != node; sibling = sibling->prev);
1354 }
1355 sibling->prev = node;
1356 }
1357 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001358
Michal Vasko9f96a052020-03-10 09:41:45 +01001359 for (par = node->parent; par; par = par->parent) {
1360 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1361 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001362 par->flags &= ~LYD_DEFAULT;
1363 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001364 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1365 /* rehash key-less list */
1366 lyd_hash((struct lyd_node *)par);
1367 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001368 }
Michal Vasko90932a92020-02-12 14:33:03 +01001369}
1370
1371/**
1372 * @brief Insert node before a sibling.
1373 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001374 * Handles inserting into NP containers and key-less lists.
1375 *
Michal Vasko90932a92020-02-12 14:33:03 +01001376 * @param[in] sibling Sibling to insert before.
1377 * @param[in] node Node to insert.
1378 */
1379static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001380lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001381{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001382 struct lyd_node_inner *par;
1383
Michal Vasko90932a92020-02-12 14:33:03 +01001384 assert(!node->next && (node->prev == node));
1385
1386 node->next = sibling;
1387 /* covers situation of sibling being first */
1388 node->prev = sibling->prev;
1389 sibling->prev = node;
1390 if (node->prev->next) {
1391 /* sibling had a preceding node */
1392 node->prev->next = node;
1393 } else if (sibling->parent) {
1394 /* sibling was first and we must also change parent child pointer */
1395 sibling->parent->child = node;
1396 }
1397 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001398
Michal Vasko9f96a052020-03-10 09:41:45 +01001399 for (par = node->parent; par; par = par->parent) {
1400 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1401 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001402 par->flags &= ~LYD_DEFAULT;
1403 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001404 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1405 /* rehash key-less list */
1406 lyd_hash((struct lyd_node *)par);
1407 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001408 }
Michal Vasko90932a92020-02-12 14:33:03 +01001409}
1410
1411/**
1412 * @brief Insert node as the last child of a parent.
1413 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001414 * Handles inserting into NP containers and key-less lists.
1415 *
Michal Vasko90932a92020-02-12 14:33:03 +01001416 * @param[in] parent Parent to insert into.
1417 * @param[in] node Node to insert.
1418 */
1419static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001420lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001421{
1422 struct lyd_node_inner *par;
1423
Michal Vasko0249f7c2020-03-05 16:36:40 +01001424 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001425 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001426
1427 par = (struct lyd_node_inner *)parent;
1428
1429 if (!par->child) {
1430 par->child = node;
1431 } else {
1432 node->prev = par->child->prev;
1433 par->child->prev->next = node;
1434 par->child->prev = node;
1435 }
1436 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001437
Michal Vasko9f96a052020-03-10 09:41:45 +01001438 for (; par; par = par->parent) {
1439 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1440 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001441 par->flags &= ~LYD_DEFAULT;
1442 }
Michal Vasko52927e22020-03-16 17:26:14 +01001443 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 /* rehash key-less list */
1445 lyd_hash((struct lyd_node *)par);
1446 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001447 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001448}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001449
Michal Vasko751cb4d2020-07-14 12:25:28 +02001450/**
1451 * @brief Learn whether a list instance has all the keys.
1452 *
1453 * @param[in] list List instance to check.
1454 * @return non-zero if all the keys were found,
1455 * @return 0 otherwise.
1456 */
1457static int
1458lyd_insert_has_keys(const struct lyd_node *list)
1459{
1460 const struct lyd_node *key;
1461 const struct lysc_node *skey = NULL;
1462
1463 assert(list->schema->nodetype == LYS_LIST);
1464 key = lyd_node_children(list, 0);
1465 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1466 if (!key || (key->schema != skey)) {
1467 /* key missing */
1468 return 0;
1469 }
1470
1471 key = key->next;
1472 }
1473
1474 /* all keys found */
1475 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001476}
1477
1478void
Michal Vasko9b368d32020-02-14 13:53:31 +01001479lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001480{
Michal Vasko9b368d32020-02-14 13:53:31 +01001481 struct lyd_node *anchor;
Michal Vasko90932a92020-02-12 14:33:03 +01001482
Michal Vasko52927e22020-03-16 17:26:14 +01001483 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001484
1485 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1486 parent = (struct lyd_node *)(*first_sibling)->parent;
1487 }
Michal Vasko90932a92020-02-12 14:33:03 +01001488
1489 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001490 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001491 /* it is key and we need to insert it at the correct place */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001492 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent, 0), node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +01001493 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001494 lyd_insert_after_node(anchor, node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001495 } else if (lyd_node_children(parent, 0)) {
1496 lyd_insert_before_node(lyd_node_children(parent, 0), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001497 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001498 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001499 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001500
Michal Vasko751cb4d2020-07-14 12:25:28 +02001501 /* insert into parent HT */
1502 lyd_insert_hash(node);
Michal Vasko9f96a052020-03-10 09:41:45 +01001503
Michal Vasko751cb4d2020-07-14 12:25:28 +02001504 /* hash list if all its keys were added */
1505 if (lyd_insert_has_keys(parent)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 lyd_hash(parent);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001507
1508 /* now we can insert even the list into its parent HT */
1509 lyd_insert_hash(parent);
Michal Vasko9f96a052020-03-10 09:41:45 +01001510 }
1511
Michal Vasko90932a92020-02-12 14:33:03 +01001512 } else {
1513 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001514 lyd_insert_last_node(parent, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001515
1516 if (!node->schema || (node->schema->nodetype != LYS_LIST) || lyd_insert_has_keys(node)) {
1517 /* insert into parent HT */
1518 lyd_insert_hash(node);
1519 }
Michal Vasko90932a92020-02-12 14:33:03 +01001520 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001521 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001522 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001523 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001524 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001525 anchor = anchor->prev;
1526 }
1527
Michal Vaskoc193ce92020-03-06 11:04:48 +01001528 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001529 /* insert after last sibling from this module */
1530 lyd_insert_after_node(anchor, node);
1531 } else {
1532 /* no data from this module, insert at the last position */
1533 lyd_insert_after_node((*first_sibling)->prev, node);
1534 }
Michal Vasko90932a92020-02-12 14:33:03 +01001535 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001536 /* the only sibling */
1537 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001538 }
Michal Vasko90932a92020-02-12 14:33:03 +01001539}
1540
Michal Vaskof03ed032020-03-04 13:31:44 +01001541static LY_ERR
1542lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1543{
1544 const struct lysc_node *par2;
1545
1546 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001547 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001548
1549 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001550 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001551
1552 if (parent) {
1553 /* inner node */
1554 if (par2 != parent) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001555 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001556 return LY_EINVAL;
1557 }
1558 } else {
1559 /* top-level node */
1560 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001561 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001562 return LY_EINVAL;
1563 }
1564 }
1565
1566 return LY_SUCCESS;
1567}
1568
1569API LY_ERR
1570lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1571{
1572 struct lyd_node *iter;
1573
Michal Vasko654bc852020-06-23 13:28:06 +02001574 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001575
1576 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1577
1578 if (node->schema->flags & LYS_KEY) {
1579 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1580 return LY_EINVAL;
1581 }
1582
1583 if (node->parent || node->prev->next) {
1584 lyd_unlink_tree(node);
1585 }
1586
1587 while (node) {
1588 iter = node->next;
1589 lyd_unlink_tree(node);
1590 lyd_insert_node(parent, NULL, node);
1591 node = iter;
1592 }
1593 return LY_SUCCESS;
1594}
1595
1596API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001597lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1598{
1599 struct lyd_node *iter;
1600
1601 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1602
Michal Vasko62ed12d2020-05-21 10:08:25 +02001603 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001604
1605 if (node->schema->flags & LYS_KEY) {
1606 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1607 return LY_EINVAL;
1608 }
1609
1610 if (node->parent || node->prev->next) {
1611 lyd_unlink_tree(node);
1612 }
1613
1614 while (node) {
1615 iter = node->next;
1616 lyd_unlink_tree(node);
1617 lyd_insert_node(NULL, &sibling, node);
1618 node = iter;
1619 }
1620 return LY_SUCCESS;
1621}
1622
Michal Vasko0249f7c2020-03-05 16:36:40 +01001623static LY_ERR
1624lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1625{
1626 if (sibling->parent) {
1627 /* nested, we do not care for the order */
1628 return LY_SUCCESS;
1629 }
1630
1631 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001632 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1633 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001634 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 +01001635 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001636 return LY_EINVAL;
1637 }
1638
Michal Vaskoc193ce92020-03-06 11:04:48 +01001639 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1640 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001641 /* inserting before/after its module data */
1642 return LY_SUCCESS;
1643 }
1644 }
1645
1646 /* find first sibling */
1647 while (sibling->prev->next) {
1648 sibling = sibling->prev;
1649 }
1650
1651 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001652 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001653 /* inserting before its module data */
1654 return LY_SUCCESS;
1655 }
1656 }
1657
1658 /* check there are no data of this module */
1659 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001660 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001661 /* some data of this module found */
1662 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001663 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001664 return LY_EINVAL;
1665 }
1666 }
1667
1668 return LY_SUCCESS;
1669}
1670
Michal Vaskob1b5c262020-03-05 14:29:47 +01001671API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001672lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1673{
1674 struct lyd_node *iter;
1675
1676 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1677
Michal Vasko62ed12d2020-05-21 10:08:25 +02001678 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001679
1680 if (node->schema->flags & LYS_KEY) {
1681 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1682 return LY_EINVAL;
1683 } else if (sibling->schema->flags & LYS_KEY) {
1684 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1685 return LY_EINVAL;
1686 }
1687
Michal Vasko0249f7c2020-03-05 16:36:40 +01001688 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1689
Michal Vaskof03ed032020-03-04 13:31:44 +01001690 if (node->parent || node->prev->next) {
1691 lyd_unlink_tree(node);
1692 }
1693
1694 /* insert in reverse order to get the original order */
1695 node = node->prev;
1696 while (node) {
1697 iter = node->prev;
1698 lyd_unlink_tree(node);
1699
1700 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001701 lyd_insert_hash(node);
1702
Michal Vaskof03ed032020-03-04 13:31:44 +01001703 /* move the anchor accordingly */
1704 sibling = node;
1705
1706 node = (iter == node) ? NULL : iter;
1707 }
1708 return LY_SUCCESS;
1709}
1710
1711API LY_ERR
1712lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1713{
1714 struct lyd_node *iter;
1715
1716 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1717
Michal Vasko62ed12d2020-05-21 10:08:25 +02001718 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001719
1720 if (node->schema->flags & LYS_KEY) {
1721 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1722 return LY_EINVAL;
1723 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1724 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1725 return LY_EINVAL;
1726 }
1727
Michal Vasko0249f7c2020-03-05 16:36:40 +01001728 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1729
Michal Vaskof03ed032020-03-04 13:31:44 +01001730 if (node->parent || node->prev->next) {
1731 lyd_unlink_tree(node);
1732 }
1733
1734 while (node) {
1735 iter = node->next;
1736 lyd_unlink_tree(node);
1737
1738 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001739 lyd_insert_hash(node);
1740
Michal Vaskof03ed032020-03-04 13:31:44 +01001741 /* move the anchor accordingly */
1742 sibling = node;
1743
1744 node = iter;
1745 }
1746 return LY_SUCCESS;
1747}
1748
1749API void
1750lyd_unlink_tree(struct lyd_node *node)
1751{
1752 struct lyd_node *iter;
1753
1754 if (!node) {
1755 return;
1756 }
1757
1758 /* unlink from siblings */
1759 if (node->prev->next) {
1760 node->prev->next = node->next;
1761 }
1762 if (node->next) {
1763 node->next->prev = node->prev;
1764 } else {
1765 /* unlinking the last node */
1766 if (node->parent) {
1767 iter = node->parent->child;
1768 } else {
1769 iter = node->prev;
1770 while (iter->prev != node) {
1771 iter = iter->prev;
1772 }
1773 }
1774 /* update the "last" pointer from the first node */
1775 iter->prev = node->prev;
1776 }
1777
1778 /* unlink from parent */
1779 if (node->parent) {
1780 if (node->parent->child == node) {
1781 /* the node is the first child */
1782 node->parent->child = node->next;
1783 }
1784
1785 lyd_unlink_hash(node);
1786
1787 /* check for keyless list and update its hash */
1788 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001789 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001790 lyd_hash(iter);
1791 }
1792 }
1793
1794 node->parent = NULL;
1795 }
1796
1797 node->next = NULL;
1798 node->prev = node;
1799}
1800
Michal Vasko90932a92020-02-12 14:33:03 +01001801LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001802lyd_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 +01001803 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 +01001804 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001805{
1806 LY_ERR ret;
1807 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001808 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001809 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001810
Michal Vasko9f96a052020-03-10 09:41:45 +01001811 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001812
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001813 LY_ARRAY_FOR(mod->compiled->exts, u) {
1814 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1815 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001816 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001817 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001818 break;
1819 }
1820 }
1821 if (!ant) {
1822 /* attribute is not defined as a metadata annotation (RFC 7952) */
1823 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1824 mod->name, name_len, name);
1825 return LY_EINVAL;
1826 }
1827
Michal Vasko9f96a052020-03-10 09:41:45 +01001828 mt = calloc(1, sizeof *mt);
1829 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1830 mt->parent = parent;
1831 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001832 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 +01001833 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001834 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001835 return ret;
1836 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001837 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001838
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001839 /* insert as the last attribute */
1840 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001841 if (parent->meta) {
1842 for (last = parent->meta; last->next; last = last->next);
1843 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001844 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001845 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001846 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001847 } else if (*meta) {
1848 for (last = *meta; last->next; last = last->next);
1849 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001850 }
1851
1852 /* remove default flags from NP containers */
Michal Vasko4ba6aef2020-07-09 15:44:23 +02001853 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001854 parent->flags &= ~LYD_DEFAULT;
1855 parent = (struct lyd_node *)parent->parent;
1856 }
1857
Michal Vasko9f96a052020-03-10 09:41:45 +01001858 if (meta) {
1859 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001860 }
1861 return ret;
1862}
1863
Michal Vasko52927e22020-03-16 17:26:14 +01001864LY_ERR
1865ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1866 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1867 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1868{
1869 struct ly_attr *at, *last;
1870 struct lyd_node_opaq *opaq;
1871
1872 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1873 assert(name && name_len);
1874 assert((prefix_len && ns) || (!prefix_len && !ns));
1875
1876 if (!value_len) {
1877 value = "";
1878 }
1879
1880 at = calloc(1, sizeof *at);
1881 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1882 at->parent = (struct lyd_node_opaq *)parent;
1883 at->name = lydict_insert(ctx, name, name_len);
1884 if (dynamic && *dynamic) {
1885 at->value = lydict_insert_zc(ctx, (char *)value);
1886 *dynamic = 0;
1887 } else {
1888 at->value = lydict_insert(ctx, value, value_len);
1889 }
1890
1891 at->format = format;
1892 at->val_prefs = val_prefs;
1893 if (ns) {
1894 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1895 at->prefix.ns = lydict_insert(ctx, ns, 0);
1896 }
1897
1898 /* insert as the last attribute */
1899 if (parent) {
1900 opaq = (struct lyd_node_opaq *)parent;
1901 if (opaq->attr) {
1902 for (last = opaq->attr; last->next; last = last->next);
1903 last->next = at;
1904 } else {
1905 opaq->attr = at;
1906 }
1907 } else if (*attr) {
1908 for (last = *attr; last->next; last = last->next);
1909 last->next = at;
1910 }
1911
1912 if (attr) {
1913 *attr = at;
1914 }
1915 return LY_SUCCESS;
1916}
1917
Radek Krejci084289f2019-07-09 17:35:30 +02001918API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001919lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001920{
Michal Vasko004d3152020-06-11 19:59:22 +02001921 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001922
Michal Vasko004d3152020-06-11 19:59:22 +02001923 if (ly_path_eval(path, tree, &target)) {
1924 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001925 }
1926
Michal Vasko004d3152020-06-11 19:59:22 +02001927 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001928}
1929
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001930API LY_ERR
1931lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1932{
1933 const struct lyd_node *iter1, *iter2;
1934 struct lyd_node_term *term1, *term2;
1935 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001936 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001937 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001938
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001939 if (!node1 || !node2) {
1940 if (node1 == node2) {
1941 return LY_SUCCESS;
1942 } else {
1943 return LY_ENOT;
1944 }
1945 }
1946
Michal Vasko52927e22020-03-16 17:26:14 +01001947 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001948 return LY_ENOT;
1949 }
1950
1951 if (node1->hash != node2->hash) {
1952 return LY_ENOT;
1953 }
Michal Vasko52927e22020-03-16 17:26:14 +01001954 /* 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 +02001955
Michal Vasko52927e22020-03-16 17:26:14 +01001956 if (!node1->schema) {
1957 opaq1 = (struct lyd_node_opaq *)node1;
1958 opaq2 = (struct lyd_node_opaq *)node2;
1959 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001960 return LY_ENOT;
1961 }
Michal Vasko52927e22020-03-16 17:26:14 +01001962 switch (opaq1->format) {
1963 case LYD_XML:
1964 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1965 return LY_ENOT;
1966 }
1967 break;
1968 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001969 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001970 /* not allowed */
1971 LOGINT(LYD_NODE_CTX(node1));
1972 return LY_EINT;
1973 }
1974 if (options & LYD_COMPARE_FULL_RECURSION) {
1975 iter1 = opaq1->child;
1976 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001977 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001978 }
1979 return LY_SUCCESS;
1980 } else {
1981 switch (node1->schema->nodetype) {
1982 case LYS_LEAF:
1983 case LYS_LEAFLIST:
1984 if (options & LYD_COMPARE_DEFAULTS) {
1985 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1986 return LY_ENOT;
1987 }
1988 }
1989
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001990 term1 = (struct lyd_node_term *)node1;
1991 term2 = (struct lyd_node_term *)node2;
1992 if (term1->value.realtype != term2->value.realtype) {
1993 return LY_ENOT;
1994 }
Michal Vasko52927e22020-03-16 17:26:14 +01001995
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001996 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001997 case LYS_CONTAINER:
1998 if (options & LYD_COMPARE_DEFAULTS) {
1999 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2000 return LY_ENOT;
2001 }
2002 }
2003 if (options & LYD_COMPARE_FULL_RECURSION) {
2004 iter1 = ((struct lyd_node_inner*)node1)->child;
2005 iter2 = ((struct lyd_node_inner*)node2)->child;
2006 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002007 }
2008 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002009 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002010 case LYS_ACTION:
2011 if (options & LYD_COMPARE_FULL_RECURSION) {
2012 /* TODO action/RPC
2013 goto all_children_compare;
2014 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002015 }
2016 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002017 case LYS_NOTIF:
2018 if (options & LYD_COMPARE_FULL_RECURSION) {
2019 /* TODO Notification
2020 goto all_children_compare;
2021 */
2022 }
2023 return LY_SUCCESS;
2024 case LYS_LIST:
2025 iter1 = ((struct lyd_node_inner*)node1)->child;
2026 iter2 = ((struct lyd_node_inner*)node2)->child;
2027
2028 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2029 /* lists with keys, their equivalence is based on their keys */
2030 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002031 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002032 key = key->next) {
2033 if (lyd_compare(iter1, iter2, options)) {
2034 return LY_ENOT;
2035 }
2036 iter1 = iter1->next;
2037 iter2 = iter2->next;
2038 }
2039 } else {
2040 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2041
2042 all_children_compare:
2043 if (!iter1 && !iter2) {
2044 /* no children, nothing to compare */
2045 return LY_SUCCESS;
2046 }
2047
2048 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
2049 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
2050 return LY_ENOT;
2051 }
2052 }
2053 if (iter1 || iter2) {
2054 return LY_ENOT;
2055 }
2056 }
2057 return LY_SUCCESS;
2058 case LYS_ANYXML:
2059 case LYS_ANYDATA:
2060 any1 = (struct lyd_node_any*)node1;
2061 any2 = (struct lyd_node_any*)node2;
2062
2063 if (any1->value_type != any2->value_type) {
2064 return LY_ENOT;
2065 }
2066 switch (any1->value_type) {
2067 case LYD_ANYDATA_DATATREE:
2068 iter1 = any1->value.tree;
2069 iter2 = any2->value.tree;
2070 goto all_children_compare;
2071 case LYD_ANYDATA_STRING:
2072 case LYD_ANYDATA_XML:
2073 case LYD_ANYDATA_JSON:
2074 len1 = strlen(any1->value.str);
2075 len2 = strlen(any2->value.str);
2076 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2077 return LY_ENOT;
2078 }
2079 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002080 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002081 len1 = lyd_lyb_data_length(any1->value.mem);
2082 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002083 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2084 return LY_ENOT;
2085 }
2086 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002087 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002088 }
2089 }
2090
Michal Vasko52927e22020-03-16 17:26:14 +01002091 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002092 return LY_EINT;
2093}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002094
Michal Vasko21725742020-06-29 11:49:25 +02002095API LY_ERR
2096lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2097{
2098 if (!meta1 || !meta2) {
2099 if (meta1 == meta2) {
2100 return LY_SUCCESS;
2101 } else {
2102 return LY_ENOT;
2103 }
2104 }
2105
2106 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2107 return LY_ENOT;
2108 }
2109
2110 if (meta1->value.realtype != meta2->value.realtype) {
2111 return LY_ENOT;
2112 }
2113
2114 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2115}
2116
Radek Krejci22ebdba2019-07-25 13:59:43 +02002117/**
Michal Vasko52927e22020-03-16 17:26:14 +01002118 * @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 +02002119 *
2120 * 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 +02002121 *
2122 * @param[in] node Original node to duplicate
2123 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2124 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2125 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2126 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2127 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002128 */
Michal Vasko52927e22020-03-16 17:26:14 +01002129static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002130lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2131 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002132{
Michal Vasko52927e22020-03-16 17:26:14 +01002133 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002134 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002135 struct lyd_meta *meta;
2136 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002137 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002138
Michal Vasko52927e22020-03-16 17:26:14 +01002139 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002140
Michal Vasko52927e22020-03-16 17:26:14 +01002141 if (!node->schema) {
2142 dup = calloc(1, sizeof(struct lyd_node_opaq));
2143 } else {
2144 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002145 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002146 case LYS_ACTION:
2147 case LYS_NOTIF:
2148 case LYS_CONTAINER:
2149 case LYS_LIST:
2150 dup = calloc(1, sizeof(struct lyd_node_inner));
2151 break;
2152 case LYS_LEAF:
2153 case LYS_LEAFLIST:
2154 dup = calloc(1, sizeof(struct lyd_node_term));
2155 break;
2156 case LYS_ANYDATA:
2157 case LYS_ANYXML:
2158 dup = calloc(1, sizeof(struct lyd_node_any));
2159 break;
2160 default:
2161 LOGINT(LYD_NODE_CTX(node));
2162 ret = LY_EINT;
2163 goto error;
2164 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002165 }
Michal Vasko52927e22020-03-16 17:26:14 +01002166 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002167
Michal Vaskof6df0a02020-06-16 13:08:34 +02002168 if (options & LYD_DUP_WITH_FLAGS) {
2169 dup->flags = node->flags;
2170 } else {
2171 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2172 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002173 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002174 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002175
Michal Vasko25a32822020-07-09 15:48:22 +02002176 /* duplicate metadata */
2177 if (!(options & LYD_DUP_NO_META)) {
2178 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002179 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002180 }
2181 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002182
2183 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002184 if (!dup->schema) {
2185 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2186 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2187 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002188
2189 if (options & LYD_DUP_RECURSIVE) {
2190 /* duplicate all the children */
2191 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002192 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002193 }
2194 }
2195 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2196 opaq->format = orig->format;
2197 if (orig->prefix.pref) {
2198 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2199 }
2200 if (orig->prefix.ns) {
2201 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2202 }
2203 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002204 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002205 LY_ARRAY_FOR(orig->val_prefs, u) {
2206 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2207 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2208 LY_ARRAY_INCREMENT(opaq->val_prefs);
2209 }
2210 }
2211 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2212 opaq->ctx = orig->ctx;
2213 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2214 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2215 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2216
2217 term->hash = orig->hash;
2218 term->value.realtype = orig->value.realtype;
2219 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2220 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2221 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2222 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2223 struct lyd_node *child;
2224
2225 if (options & LYD_DUP_RECURSIVE) {
2226 /* duplicate all the children */
2227 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002228 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002229 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002230 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002231 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002232 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002233 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002234 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002235 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002236 if (!child) {
2237 /* possibly not keys are present in filtered tree */
2238 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002239 } else if (child->schema != key) {
2240 /* possibly not all keys are present in filtered tree,
2241 * but there can be also some non-key nodes */
2242 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002243 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002244 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002245 child = child->next;
2246 }
2247 }
2248 lyd_hash(dup);
2249 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002250 dup->hash = node->hash;
2251 any = (struct lyd_node_any *)node;
2252 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002253 }
2254
Michal Vasko52927e22020-03-16 17:26:14 +01002255 /* insert */
2256 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002257
2258 if (dup_p) {
2259 *dup_p = dup;
2260 }
2261 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002262
2263error:
Michal Vasko52927e22020-03-16 17:26:14 +01002264 lyd_free_tree(dup);
2265 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002266}
2267
Michal Vasko3a41dff2020-07-15 14:30:28 +02002268static LY_ERR
2269lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2270 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002271{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002272 const struct lyd_node_inner *orig_parent, *iter;
2273 int repeat = 1;
2274
2275 *dup_parent = NULL;
2276 *local_parent = NULL;
2277
2278 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2279 if (parent && (parent->schema == orig_parent->schema)) {
2280 /* stop creating parents, connect what we have into the provided parent */
2281 iter = parent;
2282 repeat = 0;
2283 } else {
2284 iter = NULL;
2285 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2286 (struct lyd_node **)&iter));
2287 }
2288 if (!*local_parent) {
2289 *local_parent = (struct lyd_node_inner *)iter;
2290 }
2291 if (iter->child) {
2292 /* 1) list - add after keys
2293 * 2) provided parent with some children */
2294 iter->child->prev->next = *dup_parent;
2295 if (*dup_parent) {
2296 (*dup_parent)->prev = iter->child->prev;
2297 iter->child->prev = *dup_parent;
2298 }
2299 } else {
2300 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2301 }
2302 if (*dup_parent) {
2303 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2304 }
2305 *dup_parent = (struct lyd_node *)iter;
2306 }
2307
2308 if (repeat && parent) {
2309 /* given parent and created parents chain actually do not interconnect */
2310 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2311 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2312 return LY_EINVAL;
2313 }
2314
2315 return LY_SUCCESS;
2316}
2317
2318static LY_ERR
2319lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2320{
2321 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002322 const struct lyd_node *orig; /* original node to be duplicated */
2323 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002324 struct lyd_node *top = NULL; /* the most higher created node */
2325 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002326
Michal Vasko3a41dff2020-07-15 14:30:28 +02002327 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002328
2329 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002330 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002331 } else {
2332 local_parent = parent;
2333 }
2334
Radek Krejci22ebdba2019-07-25 13:59:43 +02002335 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002336 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002337 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2338 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002339 break;
2340 }
2341 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002342
2343 /* rehash if needed */
2344 for (; local_parent; local_parent = local_parent->parent) {
2345 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2346 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002347 }
2348 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002349
2350 if (dup) {
2351 *dup = first;
2352 }
2353 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002354
2355error:
2356 if (top) {
2357 lyd_free_tree(top);
2358 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002359 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002360 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002361 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002362}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002363
Michal Vasko3a41dff2020-07-15 14:30:28 +02002364API LY_ERR
2365lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2366{
2367 return lyd_dup(node, parent, options, 1, dup);
2368}
2369
2370API LY_ERR
2371lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2372{
2373 return lyd_dup(node, parent, options, 0, dup);
2374}
2375
2376API LY_ERR
2377lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002378{
2379 LY_ERR ret;
2380 struct lyd_meta *mt, *last;
2381
Michal Vasko3a41dff2020-07-15 14:30:28 +02002382 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002383
2384 /* create a copy */
2385 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002386 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002387 mt->parent = node;
2388 mt->annotation = meta->annotation;
2389 mt->value.realtype = meta->value.realtype;
2390 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002391 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002392 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2393
2394 /* insert as the last attribute */
2395 if (node->meta) {
2396 for (last = node->meta; last->next; last = last->next);
2397 last->next = mt;
2398 } else {
2399 node->meta = mt;
2400 }
2401
Michal Vasko3a41dff2020-07-15 14:30:28 +02002402 if (dup) {
2403 *dup = mt;
2404 }
2405 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002406}
2407
Michal Vasko4490d312020-06-16 13:08:55 +02002408/**
2409 * @brief Merge a source sibling into target siblings.
2410 *
2411 * @param[in,out] first_trg First target sibling, is updated if top-level.
2412 * @param[in] parent_trg Target parent.
2413 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2414 * @param[in] options Merge options.
2415 * @return LY_ERR value.
2416 */
2417static LY_ERR
2418lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2419 int options)
2420{
2421 LY_ERR ret;
2422 const struct lyd_node *child_src, *tmp, *sibling_src;
2423 struct lyd_node *match_trg, *dup_src, *next, *elem;
2424 struct lysc_type *type;
2425 LYD_ANYDATA_VALUETYPE tmp_val_type;
2426 union lyd_any_value tmp_val;
2427
2428 sibling_src = *sibling_src_p;
2429 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2430 /* try to find the exact instance */
2431 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2432 } else {
2433 /* try to simply find the node, there cannot be more instances */
2434 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2435 }
2436
2437 if (!ret) {
2438 /* node found, make sure even value matches for all node types */
2439 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2440 /* since they are different, they cannot both be default */
2441 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2442
Michal Vasko3a41dff2020-07-15 14:30:28 +02002443 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2444 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002445 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2446 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2447 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2448 &((struct lyd_node_term *)match_trg)->value));
2449
2450 /* copy flags and add LYD_NEW */
2451 match_trg->flags = sibling_src->flags | LYD_NEW;
2452 }
2453 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2454 if (options & LYD_MERGE_DESTRUCT) {
2455 dup_src = (struct lyd_node *)sibling_src;
2456 lyd_unlink_tree(dup_src);
2457 /* spend it */
2458 *sibling_src_p = NULL;
2459 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002460 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, 0, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002461 }
2462 /* just switch values */
2463 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2464 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2465 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2466 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2467 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2468 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2469
2470 /* copy flags and add LYD_NEW */
2471 match_trg->flags = sibling_src->flags | LYD_NEW;
2472
2473 /* dup_src is not needed, actually */
2474 lyd_free_tree(dup_src);
2475 } else {
2476 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002477 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002478 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2479 }
2480 }
2481 } else {
2482 /* node not found, merge it */
2483 if (options & LYD_MERGE_DESTRUCT) {
2484 dup_src = (struct lyd_node *)sibling_src;
2485 lyd_unlink_tree(dup_src);
2486 /* spend it */
2487 *sibling_src_p = NULL;
2488 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002489 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002490 }
2491
2492 /* set LYD_NEW for all the new nodes, required for validation */
2493 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2494 elem->flags |= LYD_NEW;
2495 LYD_TREE_DFS_END(dup_src, next, elem);
2496 }
2497
2498 lyd_insert_node(parent_trg, first_trg, dup_src);
2499 }
2500
2501 return LY_SUCCESS;
2502}
2503
Michal Vasko3a41dff2020-07-15 14:30:28 +02002504static LY_ERR
2505lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002506{
2507 const struct lyd_node *sibling_src, *tmp;
2508 int first;
2509
2510 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2511
2512 if (!source) {
2513 /* nothing to merge */
2514 return LY_SUCCESS;
2515 }
2516
2517 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2518 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2519 return LY_EINVAL;
2520 }
2521
2522 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2523 first = sibling_src == source ? 1 : 0;
2524 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2525 if (first && !sibling_src) {
2526 /* source was spent (unlinked), move to the next node */
2527 source = tmp;
2528 }
2529
Michal Vasko3a41dff2020-07-15 14:30:28 +02002530 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002531 break;
2532 }
2533 }
2534
2535 if (options & LYD_MERGE_DESTRUCT) {
2536 /* free any leftover source data that were not merged */
2537 lyd_free_siblings((struct lyd_node *)source);
2538 }
2539
2540 return LY_SUCCESS;
2541}
2542
Michal Vasko3a41dff2020-07-15 14:30:28 +02002543API LY_ERR
2544lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2545{
2546 return lyd_merge(target, source, options, 1);
2547}
2548
2549API LY_ERR
2550lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2551{
2552 return lyd_merge(target, source, options, 0);
2553}
2554
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002555static LY_ERR
2556lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2557{
Michal Vasko14654712020-02-06 08:35:21 +01002558 /* ending \0 */
2559 ++reqlen;
2560
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002561 if (reqlen > *buflen) {
2562 if (is_static) {
2563 return LY_EINCOMPLETE;
2564 }
2565
2566 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2567 if (!*buffer) {
2568 return LY_EMEM;
2569 }
2570
2571 *buflen = reqlen;
2572 }
2573
2574 return LY_SUCCESS;
2575}
2576
Michal Vaskod59035b2020-07-08 12:00:06 +02002577LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002578lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2579{
2580 const struct lyd_node *key;
2581 int dynamic = 0;
2582 size_t len;
2583 const char *val;
2584 char quot;
2585 LY_ERR rc;
2586
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002587 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002588 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2589 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2590 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2591 if (rc != LY_SUCCESS) {
2592 if (dynamic) {
2593 free((char *)val);
2594 }
2595 return rc;
2596 }
2597
2598 quot = '\'';
2599 if (strchr(val, '\'')) {
2600 quot = '"';
2601 }
2602 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2603
2604 if (dynamic) {
2605 free((char *)val);
2606 }
2607 }
2608
2609 return LY_SUCCESS;
2610}
2611
2612/**
2613 * @brief Append leaf-list value predicate to path.
2614 *
2615 * @param[in] node Node to print.
2616 * @param[in,out] buffer Buffer to print to.
2617 * @param[in,out] buflen Current buffer length.
2618 * @param[in,out] bufused Current number of characters used in @p buffer.
2619 * @param[in] is_static Whether buffer is static or can be reallocated.
2620 * @return LY_ERR
2621 */
2622static LY_ERR
2623lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2624{
2625 int dynamic = 0;
2626 size_t len;
2627 const char *val;
2628 char quot;
2629 LY_ERR rc;
2630
2631 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2632 len = 4 + strlen(val) + 2;
2633 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2634 if (rc != LY_SUCCESS) {
2635 goto cleanup;
2636 }
2637
2638 quot = '\'';
2639 if (strchr(val, '\'')) {
2640 quot = '"';
2641 }
2642 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2643
2644cleanup:
2645 if (dynamic) {
2646 free((char *)val);
2647 }
2648 return rc;
2649}
2650
2651/**
2652 * @brief Append node position (relative to its other instances) predicate to path.
2653 *
2654 * @param[in] node Node to print.
2655 * @param[in,out] buffer Buffer to print to.
2656 * @param[in,out] buflen Current buffer length.
2657 * @param[in,out] bufused Current number of characters used in @p buffer.
2658 * @param[in] is_static Whether buffer is static or can be reallocated.
2659 * @return LY_ERR
2660 */
2661static LY_ERR
2662lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2663{
2664 const struct lyd_node *first, *iter;
2665 size_t len;
2666 int pos;
2667 char *val = NULL;
2668 LY_ERR rc;
2669
2670 if (node->parent) {
2671 first = node->parent->child;
2672 } else {
2673 for (first = node; node->prev->next; node = node->prev);
2674 }
2675 pos = 1;
2676 for (iter = first; iter != node; iter = iter->next) {
2677 if (iter->schema == node->schema) {
2678 ++pos;
2679 }
2680 }
2681 if (asprintf(&val, "%d", pos) == -1) {
2682 return LY_EMEM;
2683 }
2684
2685 len = 1 + strlen(val) + 1;
2686 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2687 if (rc != LY_SUCCESS) {
2688 goto cleanup;
2689 }
2690
2691 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2692
2693cleanup:
2694 free(val);
2695 return rc;
2696}
2697
2698API char *
2699lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2700{
Michal Vasko14654712020-02-06 08:35:21 +01002701 int is_static = 0, i, depth;
2702 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002703 const struct lyd_node *iter;
2704 const struct lys_module *mod;
2705 LY_ERR rc;
2706
2707 LY_CHECK_ARG_RET(NULL, node, NULL);
2708 if (buffer) {
2709 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2710 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002711 } else {
2712 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002713 }
2714
2715 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002716 case LYD_PATH_LOG:
2717 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002718 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2719 ++depth;
2720 }
2721
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002722 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002723 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002724 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002725 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002726iter_print:
2727 /* print prefix and name */
2728 mod = NULL;
2729 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2730 mod = iter->schema->module;
2731 }
2732
2733 /* realloc string */
2734 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2735 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2736 if (rc != LY_SUCCESS) {
2737 break;
2738 }
2739
2740 /* print next node */
2741 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2742
2743 switch (iter->schema->nodetype) {
2744 case LYS_LIST:
2745 if (iter->schema->flags & LYS_KEYLESS) {
2746 /* print its position */
2747 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2748 } else {
2749 /* print all list keys in predicates */
2750 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2751 }
2752 break;
2753 case LYS_LEAFLIST:
2754 if (iter->schema->flags & LYS_CONFIG_W) {
2755 /* print leaf-list value */
2756 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2757 } else {
2758 /* print its position */
2759 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2760 }
2761 break;
2762 default:
2763 /* nothing to print more */
2764 rc = LY_SUCCESS;
2765 break;
2766 }
2767 if (rc != LY_SUCCESS) {
2768 break;
2769 }
2770
Michal Vasko14654712020-02-06 08:35:21 +01002771 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002772 }
2773 break;
2774 }
2775
2776 return buffer;
2777}
Michal Vaskoe444f752020-02-10 12:20:06 +01002778
Michal Vasko25a32822020-07-09 15:48:22 +02002779API struct lyd_meta *
2780lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2781{
2782 struct lyd_meta *ret = NULL;
2783 const struct ly_ctx *ctx;
2784 const char *prefix, *tmp;
2785 char *str;
2786 size_t pref_len, name_len;
2787
2788 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2789
2790 if (!first) {
2791 return NULL;
2792 }
2793
2794 ctx = first->annotation->module->ctx;
2795
2796 /* parse the name */
2797 tmp = name;
2798 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2799 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2800 return NULL;
2801 }
2802
2803 /* find the module */
2804 if (prefix) {
2805 str = strndup(prefix, pref_len);
2806 module = ly_ctx_get_module_latest(ctx, str);
2807 free(str);
2808 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2809 }
2810
2811 /* find the metadata */
2812 LY_LIST_FOR(first, first) {
2813 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2814 ret = (struct lyd_meta *)first;
2815 break;
2816 }
2817 }
2818
2819 return ret;
2820}
2821
Michal Vasko9b368d32020-02-14 13:53:31 +01002822LY_ERR
2823lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2824 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002825{
2826 LY_ERR rc;
2827 const struct lyd_node *node = NULL;
2828 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02002829 struct lyxp_expr *expr = NULL;
2830 uint16_t exp_idx = 0;
2831 struct ly_path_predicate *predicates = NULL;
2832 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01002833 struct lyd_value val = {0};
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002834 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01002835
Michal Vasko9b368d32020-02-14 13:53:31 +01002836 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002837
2838 if (!first) {
2839 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002840 if (match) {
2841 *match = NULL;
2842 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002843 return LY_ENOTFOUND;
2844 }
2845
Michal Vaskoe444f752020-02-10 12:20:06 +01002846 if (key_or_value && !val_len) {
2847 val_len = strlen(key_or_value);
2848 }
2849
2850 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002851 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002852 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 +01002853 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02002854 /* parse keys */
2855 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
2856 LY_PATH_PRED_KEYS, &expr), cleanup);
2857
2858 /* compile them */
2859 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
2860 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002861 }
2862
2863 /* find first matching value */
2864 LY_LIST_FOR(first, node) {
2865 if (node->schema != schema) {
2866 continue;
2867 }
2868
Michal Vasko00cbf532020-06-15 13:58:47 +02002869 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002870 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02002871 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002872 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002873 rc = lyd_find_sibling_val(lyd_node_children(node, 0), predicates[u].key, NULL, 0, (struct lyd_node **)&term);
Michal Vaskoe444f752020-02-10 12:20:06 +01002874 if (rc == LY_ENOTFOUND) {
2875 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002876 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002877 }
2878 LY_CHECK_GOTO(rc, cleanup);
2879
2880 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02002881 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002882 break;
2883 }
2884 }
2885
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002886 if (u < LY_ARRAY_COUNT(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002887 /* not a match */
2888 continue;
2889 }
Michal Vasko90932a92020-02-12 14:33:03 +01002890 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002891 term = (struct lyd_node_term *)node;
2892
2893 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002894 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002895 /* not a match */
2896 continue;
2897 }
2898 }
2899
2900 /* all criteria passed */
2901 break;
2902 }
2903
2904 if (!node) {
2905 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002906 if (match) {
2907 *match = NULL;
2908 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002909 goto cleanup;
2910 }
2911
2912 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002913 if (match) {
2914 *match = (struct lyd_node *)node;
2915 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002916 rc = LY_SUCCESS;
2917
2918cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02002919 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
2920 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01002921 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002922 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002923 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002924 return rc;
2925}
2926
2927API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002928lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2929 const char *key_or_value, size_t val_len, struct lyd_node **match)
2930{
2931 const struct lysc_node *schema;
2932
2933 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2934
2935 if (!first) {
2936 /* no data */
2937 *match = NULL;
2938 return LY_ENOTFOUND;
2939 }
2940
2941 /* find schema */
2942 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2943 if (!schema) {
2944 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2945 return LY_EINVAL;
2946 }
2947
2948 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2949}
2950
2951API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002952lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2953{
2954 struct lyd_node **match_p;
2955 struct lyd_node_inner *parent;
2956
Michal Vaskof03ed032020-03-04 13:31:44 +01002957 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002958
Michal Vasko62ed12d2020-05-21 10:08:25 +02002959 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2960 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002961 if (match) {
2962 *match = NULL;
2963 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002964 return LY_ENOTFOUND;
2965 }
2966
2967 /* find first sibling */
2968 if (siblings->parent) {
2969 siblings = siblings->parent->child;
2970 } else {
2971 while (siblings->prev->next) {
2972 siblings = siblings->prev;
2973 }
2974 }
2975
2976 parent = (struct lyd_node_inner *)siblings->parent;
2977 if (parent && parent->children_ht) {
2978 assert(target->hash);
2979
2980 /* find by hash */
2981 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002982 /* check even value when needed */
2983 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(target, *match_p, 0)) {
2984 siblings = *match_p;
2985 } else {
2986 siblings = NULL;
2987 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002988 } else {
2989 /* not found */
2990 siblings = NULL;
2991 }
2992 } else {
2993 /* no children hash table */
2994 for (; siblings; siblings = siblings->next) {
2995 if (!lyd_compare(siblings, target, 0)) {
2996 break;
2997 }
2998 }
2999 }
3000
3001 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003002 if (match) {
3003 *match = NULL;
3004 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003005 return LY_ENOTFOUND;
3006 }
3007
Michal Vasko9b368d32020-02-14 13:53:31 +01003008 if (match) {
3009 *match = (struct lyd_node *)siblings;
3010 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003011 return LY_SUCCESS;
3012}
3013
3014API LY_ERR
3015lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3016{
3017 struct lyd_node_inner *parent;
3018 struct lyd_node *match;
3019 struct lyd_node **match_p;
3020 struct ly_set *ret;
3021
3022 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3023
Michal Vasko62ed12d2020-05-21 10:08:25 +02003024 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3025 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003026 return LY_ENOTFOUND;
3027 }
3028
3029 ret = ly_set_new();
3030 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3031
3032 /* find first sibling */
3033 if (siblings->parent) {
3034 siblings = siblings->parent->child;
3035 } else {
3036 while (siblings->prev->next) {
3037 siblings = siblings->prev;
3038 }
3039 }
3040
3041 parent = (struct lyd_node_inner *)siblings->parent;
3042 if (parent && parent->children_ht) {
3043 assert(target->hash);
3044
3045 /* find by hash */
3046 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003047 /* check even value when needed */
3048 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(target, *match_p, 0)) {
3049 match = *match_p;
3050 } else {
3051 match = NULL;
3052 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003053 } else {
3054 /* not found */
3055 match = NULL;
3056 }
3057 while (match) {
3058 /* add all found nodes into the return set */
3059 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3060 goto error;
3061 }
3062
3063 /* find next instance */
3064 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3065 match = NULL;
3066 } else {
Michal Vaskoda859032020-07-14 12:20:14 +02003067 /* check even value when needed */
3068 if (!(match->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(match, *match_p, 0)) {
3069 match = *match_p;
3070 } else {
3071 match = NULL;
3072 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003073 }
3074 }
3075 } else {
3076 /* no children hash table */
3077 for (; siblings; siblings = siblings->next) {
3078 if (!lyd_compare(siblings, target, 0)) {
3079 /* a match */
3080 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3081 goto error;
3082 }
3083 }
3084 }
3085 }
3086
3087 if (!ret->count) {
3088 ly_set_free(ret, NULL);
3089 return LY_ENOTFOUND;
3090 }
3091
3092 *set = ret;
3093 return LY_SUCCESS;
3094
3095error:
3096 ly_set_free(ret, NULL);
3097 return LY_EMEM;
3098}
3099
Michal Vasko90932a92020-02-12 14:33:03 +01003100static int
3101lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3102{
3103 struct lysc_node *val1;
3104 struct lyd_node *val2;
3105
3106 val1 = *((struct lysc_node **)val1_p);
3107 val2 = *((struct lyd_node **)val2_p);
3108
3109 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
3110
3111 if (val1 == val2->schema) {
3112 /* schema match is enough */
3113 return 1;
3114 } else {
3115 return 0;
3116 }
3117}
3118
3119static LY_ERR
3120lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3121{
3122 struct lyd_node **match_p;
3123 struct lyd_node_inner *parent;
3124 uint32_t hash;
3125 values_equal_cb ht_cb;
3126
3127 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3128
3129 /* find first sibling */
3130 if (siblings->parent) {
3131 siblings = siblings->parent->child;
3132 } else {
3133 while (siblings->prev->next) {
3134 siblings = siblings->prev;
3135 }
3136 }
3137
3138 parent = (struct lyd_node_inner *)siblings->parent;
3139 if (parent && parent->children_ht) {
3140 /* calculate our hash */
3141 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3142 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3143 hash = dict_hash_multi(hash, NULL, 0);
3144
3145 /* use special hash table function */
3146 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3147
3148 /* find by hash */
3149 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3150 siblings = *match_p;
3151 } else {
3152 /* not found */
3153 siblings = NULL;
3154 }
3155
3156 /* set the original hash table compare function back */
3157 lyht_set_cb(parent->children_ht, ht_cb);
3158 } else {
3159 /* no children hash table */
3160 for (; siblings; siblings = siblings->next) {
3161 if (siblings->schema == schema) {
3162 /* schema match is enough */
3163 break;
3164 }
3165 }
3166 }
3167
3168 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003169 if (match) {
3170 *match = NULL;
3171 }
Michal Vasko90932a92020-02-12 14:33:03 +01003172 return LY_ENOTFOUND;
3173 }
3174
Michal Vasko9b368d32020-02-14 13:53:31 +01003175 if (match) {
3176 *match = (struct lyd_node *)siblings;
3177 }
Michal Vasko90932a92020-02-12 14:33:03 +01003178 return LY_SUCCESS;
3179}
3180
Michal Vaskoe444f752020-02-10 12:20:06 +01003181API LY_ERR
3182lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3183 size_t val_len, struct lyd_node **match)
3184{
3185 LY_ERR rc;
3186 struct lyd_node *target = NULL;
3187
3188 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003189 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003190 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3191 return LY_EINVAL;
3192 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
3193 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
3194 return LY_EINVAL;
3195 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3196 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3197 lys_nodetype2str(schema->nodetype), __func__);
3198 return LY_EINVAL;
3199 }
3200
Michal Vasko62ed12d2020-05-21 10:08:25 +02003201 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3202 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003203 if (match) {
3204 *match = NULL;
3205 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003206 return LY_ENOTFOUND;
3207 }
3208
Michal Vaskof03ed032020-03-04 13:31:44 +01003209 if (key_or_value && !val_len) {
3210 val_len = strlen(key_or_value);
3211 }
3212
Michal Vasko90932a92020-02-12 14:33:03 +01003213 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01003214 switch (schema->nodetype) {
3215 case LYS_CONTAINER:
3216 case LYS_ANYXML:
3217 case LYS_ANYDATA:
3218 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01003219 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01003220 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01003221 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02003222 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01003223 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003224 break;
3225 case LYS_LEAFLIST:
3226 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02003227 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
3228 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3229 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01003230 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01003231 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01003232 if (schema->nodetype == LYS_LIST) {
3233 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003234 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003235 }
3236
3237 /* find it */
3238 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003239 break;
3240 default:
3241 /* unreachable */
3242 LOGINT(schema->module->ctx);
3243 return LY_EINT;
3244 }
3245
Michal Vaskoe444f752020-02-10 12:20:06 +01003246 lyd_free_tree(target);
3247 return rc;
3248}
Michal Vaskoccc02342020-05-21 10:09:21 +02003249
3250API LY_ERR
3251lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3252{
3253 LY_ERR ret = LY_SUCCESS;
3254 struct lyxp_set xp_set;
3255 struct lyxp_expr *exp;
3256 uint32_t i;
3257
3258 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3259
3260 memset(&xp_set, 0, sizeof xp_set);
3261
3262 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003263 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003264 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3265
3266 /* evaluate expression */
3267 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3268 LY_CHECK_GOTO(ret, cleanup);
3269
3270 /* allocate return set */
3271 *set = ly_set_new();
3272 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3273
3274 /* transform into ly_set */
3275 if (xp_set.type == LYXP_SET_NODE_SET) {
3276 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3277 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3278 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3279 (*set)->size = xp_set.used;
3280
3281 for (i = 0; i < xp_set.used; ++i) {
3282 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3283 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3284 }
3285 }
3286 }
3287
3288cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003289 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003290 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3291 return ret;
3292}