blob: 5942b09dfeb9f0fa306b475a079a4e5e5d092b76 [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"
31#include "config.h"
32#include "context.h"
33#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020036#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020038#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010039#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_types.h"
41#include "set.h"
42#include "tree.h"
43#include "tree_data_internal.h"
44#include "tree_schema.h"
45#include "tree_schema_internal.h"
46#include "xml.h"
47#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020048
Michal Vaskoe893ddd2020-06-23 13:35:20 +020049static LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused,
50 int is_static);
51
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 Vasko9f96a052020-03-10 09:41:45 +0100125lyd_value_parse_meta(struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +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 */
257 ret = LY_EVALID;
258 }
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 Krejcif3b6fec2019-07-24 15:53:11 +0200282API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100283lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200284{
Radek Krejcie7b95092019-05-15 11:03:07 +0200285 struct lyd_node *result = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200286#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 const char *yang_data_name = NULL;
Radek Krejcie92210c2019-05-17 15:53:35 +0200288#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200289
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200290 LY_CHECK_ARG_RET(ctx, ctx, NULL);
291
Michal Vasko5b37a352020-03-06 13:38:33 +0100292 if ((options & LYD_OPT_PARSE_ONLY) && (options & LYD_VALOPT_MASK)) {
293 LOGERR(ctx, LY_EINVAL, "Passing validation flags with LYD_OPT_PARSE_ONLY is not allowed.");
294 return NULL;
295 }
296
Michal Vaskoa3881362020-01-21 15:57:35 +0100297#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200298 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200299 /* first item in trees is mandatory - the RPC/action request */
300 LY_CHECK_ARG_RET(ctx, trees, LY_ARRAY_SIZE(trees) >= 1, NULL);
301 if (!trees[0] || trees[0]->parent || !(trees[0]->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
302 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
303 lyd_parse_options_type2str(options));
Radek Krejcie7b95092019-05-15 11:03:07 +0200304 return NULL;
305 }
306 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
Radek Krejcie7b95092019-05-15 11:03:07 +0200308 if (options & LYD_OPT_DATA_TEMPLATE) {
309 yang_data_name = va_arg(ap, const char *);
310 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200311#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200312
313 if (!format) {
314 /* TODO try to detect format from the content */
315 }
316
317 switch (format) {
318 case LYD_XML:
Michal Vasko9f96a052020-03-10 09:41:45 +0100319 lyd_parse_xml_data(ctx, data, options, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 break;
321#if 0
322 case LYD_JSON:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200323 lyd_parse_json(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200324 break;
325 case LYD_LYB:
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200326 lyd_parse_lyb(ctx, data, options, trees, &result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 break;
328#endif
Michal Vasko52927e22020-03-16 17:26:14 +0100329 case LYD_SCHEMA:
Radek Krejcie7b95092019-05-15 11:03:07 +0200330 LOGINT(ctx);
331 break;
332 }
333
Radek Krejcie7b95092019-05-15 11:03:07 +0200334 return result;
335}
336
337API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100338lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200339{
340 struct lyd_node *result;
341 size_t length;
342 char *addr;
343
344 LY_CHECK_ARG_RET(ctx, ctx, NULL);
345 if (fd < 0) {
346 LOGARG(ctx, fd);
347 return NULL;
348 }
349
350 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
Michal Vaskoa3881362020-01-21 15:57:35 +0100351 result = lyd_parse_mem(ctx, addr ? addr : "", format, options);
Radek Krejcidf3da792019-05-17 10:32:24 +0200352 if (addr) {
353 ly_munmap(addr, length);
354 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200355
356 return result;
357}
358
359API struct lyd_node *
Michal Vaskoa3881362020-01-21 15:57:35 +0100360lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +0200361{
362 int fd;
363 struct lyd_node *result;
364 size_t len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200365
366 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
367
368 fd = open(path, O_RDONLY);
369 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
370
371 if (!format) {
372 /* unknown format - try to detect it from filename's suffix */
373 len = strlen(path);
374
375 /* ignore trailing whitespaces */
376 for (; len > 0 && isspace(path[len - 1]); len--);
377
378 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
379 format = LYD_XML;
380#if 0
381 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
382 format = LYD_JSON;
383 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
384 format = LYD_LYB;
385#endif
386 } /* else still unknown, try later to detect it from the content */
387 }
388
Michal Vaskoa3881362020-01-21 15:57:35 +0100389 result = lyd_parse_fd(ctx, fd, format, options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200390 close(fd);
391
392 return result;
393}
Radek Krejci084289f2019-07-09 17:35:30 +0200394
Michal Vasko90932a92020-02-12 14:33:03 +0100395LY_ERR
396lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
397 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
398{
399 LY_ERR ret;
400 struct lyd_node_term *term;
401
Michal Vasko9b368d32020-02-14 13:53:31 +0100402 assert(schema->nodetype & LYD_NODE_TERM);
403
Michal Vasko90932a92020-02-12 14:33:03 +0100404 term = calloc(1, sizeof *term);
405 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
406
407 term->schema = schema;
408 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100409 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100410
411 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
412 if (ret && (ret != LY_EINCOMPLETE)) {
413 free(term);
414 return ret;
415 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100416 lyd_hash((struct lyd_node *)term);
417
418 *node = (struct lyd_node *)term;
419 return ret;
420}
421
422LY_ERR
423lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
424{
425 LY_ERR ret;
426 struct lyd_node_term *term;
427 struct lysc_type *type;
428
429 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200430 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100431
432 term = calloc(1, sizeof *term);
433 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
434
435 term->schema = schema;
436 term->prev = (struct lyd_node *)term;
437 term->flags = LYD_NEW;
438
439 type = ((struct lysc_node_leaf *)schema)->type;
440 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
441 if (ret) {
442 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
443 free(term);
444 return ret;
445 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200446 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100447 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100448
449 *node = (struct lyd_node *)term;
450 return ret;
451}
452
453LY_ERR
454lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
455{
456 struct lyd_node_inner *in;
457
Michal Vasko9b368d32020-02-14 13:53:31 +0100458 assert(schema->nodetype & LYD_NODE_INNER);
459
Michal Vasko90932a92020-02-12 14:33:03 +0100460 in = calloc(1, sizeof *in);
461 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
462
463 in->schema = schema;
464 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100465 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100466
Michal Vasko9b368d32020-02-14 13:53:31 +0100467 /* do not hash list with keys, we need them for the hash */
468 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
469 lyd_hash((struct lyd_node *)in);
470 }
Michal Vasko90932a92020-02-12 14:33:03 +0100471
472 *node = (struct lyd_node *)in;
473 return LY_SUCCESS;
474}
475
Michal Vasko90932a92020-02-12 14:33:03 +0100476LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200477lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100478{
479 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100480 struct lyd_node *list = NULL, *key;
Michal Vasko004d3152020-06-11 19:59:22 +0200481 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100482
Michal Vasko004d3152020-06-11 19:59:22 +0200483 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100484
485 /* create list */
486 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
487
Michal Vasko90932a92020-02-12 14:33:03 +0100488 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200489 LY_ARRAY_FOR(predicates, u) {
490 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100491 lyd_insert_node(list, NULL, key);
492 }
493
Michal Vasko9b368d32020-02-14 13:53:31 +0100494 /* hash having all the keys */
495 lyd_hash(list);
496
Michal Vasko90932a92020-02-12 14:33:03 +0100497 /* success */
498 *node = list;
499 list = NULL;
500
501cleanup:
502 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200503 return ret;
504}
505
506static LY_ERR
507lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
508{
509 LY_ERR ret = LY_SUCCESS;
510 struct lyxp_expr *expr = NULL;
511 uint16_t exp_idx = 0;
512 enum ly_path_pred_type pred_type = 0;
513 struct ly_path_predicate *predicates = NULL;
514
515 /* parse keys */
516 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
517 LY_PATH_PRED_KEYS, &expr), cleanup);
518
519 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200520 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
521 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200522
523 /* create the list node */
524 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
525
526cleanup:
527 lyxp_expr_free(schema->module->ctx, expr);
528 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100529 return ret;
530}
531
532LY_ERR
533lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
534{
535 struct lyd_node_any *any;
536
Michal Vasko9b368d32020-02-14 13:53:31 +0100537 assert(schema->nodetype & LYD_NODE_ANY);
538
Michal Vasko90932a92020-02-12 14:33:03 +0100539 any = calloc(1, sizeof *any);
540 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
541
542 any->schema = schema;
543 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100544 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100545
546 any->value.xml = value;
547 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100548 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100549
550 *node = (struct lyd_node *)any;
551 return LY_SUCCESS;
552}
553
Michal Vasko52927e22020-03-16 17:26:14 +0100554LY_ERR
555lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
556 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
557 const char *ns, struct lyd_node **node)
558{
559 struct lyd_node_opaq *opaq;
560
561 assert(ctx && name && name_len && ns);
562
563 if (!value_len) {
564 value = "";
565 }
566
567 opaq = calloc(1, sizeof *opaq);
568 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
569
570 opaq->prev = (struct lyd_node *)opaq;
571
572 opaq->name = lydict_insert(ctx, name, name_len);
573 opaq->format = format;
574 if (pref_len) {
575 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
576 }
577 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
578 opaq->val_prefs = val_prefs;
579 if (dynamic && *dynamic) {
580 opaq->value = lydict_insert_zc(ctx, (char *)value);
581 *dynamic = 0;
582 } else {
583 opaq->value = lydict_insert(ctx, value, value_len);
584 }
585 opaq->ctx = ctx;
586
587 *node = (struct lyd_node *)opaq;
588 return LY_SUCCESS;
589}
590
Michal Vasko013a8182020-03-03 10:46:53 +0100591API struct lyd_node *
592lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
593{
594 struct lyd_node *ret = NULL;
595 const struct lysc_node *schema;
596 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
597
598 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
599
Michal Vaskof03ed032020-03-04 13:31:44 +0100600 if (!module) {
601 module = parent->schema->module;
602 }
603
Michal Vasko1bf09392020-03-27 12:38:10 +0100604 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vaskob00f3cf2020-05-27 11:20:13 +0200605 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100606
607 if (!lyd_create_inner(schema, &ret) && parent) {
608 lyd_insert_node(parent, NULL, ret);
609 }
610 return ret;
611}
612
613API struct lyd_node *
614lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
615{
616 struct lyd_node *ret = NULL, *key;
617 const struct lysc_node *schema, *key_s;
618 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
619 va_list ap;
620 const char *key_val;
621 LY_ERR rc = LY_SUCCESS;
622
623 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
624
Michal Vaskof03ed032020-03-04 13:31:44 +0100625 if (!module) {
626 module = parent->schema->module;
627 }
628
Michal Vasko013a8182020-03-03 10:46:53 +0100629 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
630 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
631
632 /* create list inner node */
633 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
634
635 va_start(ap, name);
636
637 /* create and insert all the keys */
638 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
639 key_val = va_arg(ap, const char *);
640
Michal Vaskof03ed032020-03-04 13:31:44 +0100641 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 +0200642 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
643 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100644 lyd_insert_node(ret, NULL, key);
645 }
646
647 /* hash having all the keys */
648 lyd_hash(ret);
649
650 if (parent) {
651 lyd_insert_node(parent, NULL, ret);
652 }
653
654cleanup:
655 if (rc) {
656 lyd_free_tree(ret);
657 ret = NULL;
658 }
659 va_end(ap);
660 return ret;
661}
662
663API struct lyd_node *
664lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
665{
666 struct lyd_node *ret = NULL;
667 const struct lysc_node *schema;
668 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
669
670 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
671
Michal Vaskof03ed032020-03-04 13:31:44 +0100672 if (!module) {
673 module = parent->schema->module;
674 }
Michal Vasko004d3152020-06-11 19:59:22 +0200675 if (!keys) {
676 keys = "";
677 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100678
Michal Vasko004d3152020-06-11 19:59:22 +0200679 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100680 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
681 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
682
Michal Vasko004d3152020-06-11 19:59:22 +0200683 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
684 /* key-less list */
685 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
686 } else {
687 /* create the list node */
688 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret), NULL);
689 }
690
691 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100692 lyd_insert_node(parent, NULL, ret);
693 }
694 return ret;
695}
696
697API struct lyd_node *
698lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
699{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200700 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100701 struct lyd_node *ret = NULL;
702 const struct lysc_node *schema;
703 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
704
705 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
706
Michal Vaskof03ed032020-03-04 13:31:44 +0100707 if (!module) {
708 module = parent->schema->module;
709 }
710
Michal Vasko013a8182020-03-03 10:46:53 +0100711 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
712 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
713
Michal Vaskocbff3e92020-05-27 12:56:41 +0200714 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
715 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), NULL);
716
717 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100718 lyd_insert_node(parent, NULL, ret);
719 }
720 return ret;
721}
722
723API struct lyd_node *
724lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
725 LYD_ANYDATA_VALUETYPE value_type)
726{
727 struct lyd_node *ret = NULL;
728 const struct lysc_node *schema;
729 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
730
731 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
732
Michal Vaskof03ed032020-03-04 13:31:44 +0100733 if (!module) {
734 module = parent->schema->module;
735 }
736
Michal Vasko013a8182020-03-03 10:46:53 +0100737 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
738 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
739
740 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
741 lyd_insert_node(parent, NULL, ret);
742 }
743 return ret;
744}
745
Michal Vasko4490d312020-06-16 13:08:55 +0200746/**
747 * @brief Update node value.
748 *
749 * @param[in] node Node to update.
750 * @param[in] value New value to set.
751 * @param[in] value_type Type of @p value for any node.
752 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
753 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
754 * @return LY_ERR value.
755 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200756static LY_ERR
757lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
758 struct lyd_node **new_parent, struct lyd_node **new_node)
759{
760 LY_ERR ret = LY_SUCCESS;
761 struct lyd_node *new_any;
762
763 switch (node->schema->nodetype) {
764 case LYS_CONTAINER:
765 case LYS_NOTIF:
766 case LYS_RPC:
767 case LYS_ACTION:
768 case LYS_LIST:
769 case LYS_LEAFLIST:
770 /* if it exists, there is nothing to update */
771 *new_parent = NULL;
772 *new_node = NULL;
773 break;
774 case LYS_LEAF:
775 ret = lyd_change_term(node, value);
776 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
777 /* there was an actual change (at least of the default flag) */
778 *new_parent = node;
779 *new_node = node;
780 ret = LY_SUCCESS;
781 } else if (ret == LY_ENOT) {
782 /* no change */
783 *new_parent = NULL;
784 *new_node = NULL;
785 ret = LY_SUCCESS;
786 } /* else error */
787 break;
788 case LYS_ANYDATA:
789 case LYS_ANYXML:
790 /* create a new any node */
791 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
792
793 /* compare with the existing one */
794 if (lyd_compare(node, new_any, 0)) {
795 /* not equal, switch values (so that we can use generic node free) */
796 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
797 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
798 ((struct lyd_node_any *)node)->value.str = value;
799 ((struct lyd_node_any *)node)->value_type = value_type;
800
801 *new_parent = node;
802 *new_node = node;
803 } else {
804 /* they are equal */
805 *new_parent = NULL;
806 *new_node = NULL;
807 }
808 lyd_free_tree(new_any);
809 break;
810 default:
811 LOGINT(LYD_NODE_CTX(node));
812 ret = LY_EINT;
813 break;
814 }
815
816 return ret;
817}
818
Michal Vaskod86997b2020-05-26 15:19:54 +0200819API struct lyd_meta *
820lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
821{
822 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200823 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200824 const char *prefix, *tmp;
825 char *str;
826 size_t pref_len, name_len;
827
Michal Vasko00cbf532020-06-15 13:58:47 +0200828 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), NULL);
829
830 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200831
832 /* parse the name */
833 tmp = name;
834 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
835 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
836 return NULL;
837 }
838
839 /* find the module */
840 if (prefix) {
841 str = strndup(name, name_len);
842 module = ly_ctx_get_module_implemented(ctx, str);
843 free(str);
Michal Vasko004d3152020-06-11 19:59:22 +0200844 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +0200845 }
846
847 /* set value if none */
848 if (!val_str) {
849 val_str = "";
850 }
851
852 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
853 LYD_JSON, parent->schema);
854 return ret;
855}
856
Michal Vasko00cbf532020-06-15 13:58:47 +0200857API struct lyd_node *
858lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
859 const char *module_name)
860{
861 struct lyd_node *ret = NULL;
862
863 LY_CHECK_ARG_RET(ctx, parent || ctx, name, module_name, NULL);
864
865 if (!ctx) {
866 ctx = LYD_NODE_CTX(parent);
867 }
868 if (!value) {
869 value = "";
870 }
871
872 if (!lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0, module_name, &ret)
873 && parent) {
874 lyd_insert_node(parent, NULL, ret);
875 }
876 return ret;
877}
878
879API struct ly_attr *
880lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str)
881{
882 struct ly_attr *ret = NULL;
883 const struct ly_ctx *ctx;
884 const char *prefix, *tmp;
885 size_t pref_len, name_len;
886
887 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, NULL);
888
889 ctx = LYD_NODE_CTX(parent);
890
891 /* parse the name */
892 tmp = name;
893 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
894 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
895 return NULL;
896 }
897
898 /* set value if none */
899 if (!val_str) {
900 val_str = "";
901 }
902
903 ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL, prefix,
904 pref_len, module_name);
905 return ret;
906}
907
908API LY_ERR
909lyd_change_term(struct lyd_node *term, const char *val_str)
910{
911 LY_ERR ret = LY_SUCCESS;
912 struct lysc_type *type;
913 struct lyd_node_term *t;
914 struct lyd_node *parent;
915 struct lyd_value val = {0};
916 int dflt_change, val_change;
917
918 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
919
920 if (!val_str) {
921 val_str = "";
922 }
923 t = (struct lyd_node_term *)term;
924 type = ((struct lysc_node_leaf *)term->schema)->type;
925
926 /* parse the new value */
927 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
928 LYD_JSON), cleanup);
929
930 /* compare original and new value */
931 if (type->plugin->compare(&t->value, &val)) {
932 /* values differ, switch them */
933 type->plugin->free(LYD_NODE_CTX(term), &t->value);
934 t->value = val;
935 memset(&val, 0, sizeof val);
936 val_change = 1;
937 } else {
938 val_change = 0;
939 }
940
941 /* always clear the default flag */
942 if (term->flags & LYD_DEFAULT) {
943 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
944 parent->flags &= ~LYD_DEFAULT;
945 }
946 dflt_change = 1;
947 } else {
948 dflt_change = 0;
949 }
950
951 if (val_change || dflt_change) {
952 /* make the node non-validated */
953 term->flags &= LYD_NEW;
954 }
955
956 if (val_change) {
957 if (term->schema->nodetype == LYS_LEAFLIST) {
958 /* leaf-list needs to be hashed again and re-inserted into parent */
959 lyd_unlink_hash(term);
960 lyd_hash(term);
961 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
962 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
963 /* list needs to be updated if its key was changed */
964 assert(term->parent->schema->nodetype == LYS_LIST);
965 lyd_unlink_hash((struct lyd_node *)term->parent);
966 lyd_hash((struct lyd_node *)term->parent);
967 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
968 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
969 }
970
971 /* retrun value */
972 if (!val_change) {
973 if (dflt_change) {
974 /* only default flag change */
975 ret = LY_EEXIST;
976 } else {
977 /* no change */
978 ret = LY_ENOT;
979 }
980 } /* else value changed, LY_SUCCESS */
981
982cleanup:
983 type->plugin->free(LYD_NODE_CTX(term), &val);
984 return ret;
985}
986
987API struct lyd_node *
988lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options)
989{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200990 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200991
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200992 lyd_new_path2(parent, ctx, path, value, 0, options, &new_parent, NULL);
993 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200994}
995
996API struct lyd_node *
997lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
998 LYD_ANYDATA_VALUETYPE value_type, int options)
999{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001000 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001001
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001002 lyd_new_path2(parent, ctx, path, value, value_type, options, &new_parent, NULL);
1003 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001004}
1005
1006API LY_ERR
1007lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1008 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1009{
1010 LY_ERR ret = LY_SUCCESS, r;
1011 struct lyxp_expr *exp = NULL;
1012 struct ly_path *p = NULL;
1013 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1014 const struct lysc_node *schema;
1015 LY_ARRAY_SIZE_TYPE path_idx = 0;
1016 struct ly_path_predicate *pred;
1017
1018 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1019
1020 if (!ctx) {
1021 ctx = LYD_NODE_CTX(parent);
1022 }
1023
1024 /* parse path */
1025 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1026 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1027
1028 /* compile path */
1029 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1030 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1031 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1032
1033 schema = p[LY_ARRAY_SIZE(p) - 1].node;
1034 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_SIZE(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
1035 && !(options & LYD_NEWOPT_OPAQ)) {
1036 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1037 lys_nodetype2str(schema->nodetype), schema->name);
1038 ret = LY_EINVAL;
1039 goto cleanup;
1040 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_SIZE(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
1041 /* parse leafref value into a predicate, if not defined in the path */
1042 p[LY_ARRAY_SIZE(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1043 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_SIZE(p) - 1].predicates, pred, ret, cleanup);
1044
1045 if (!value) {
1046 value = "";
1047 }
1048
1049 r = LY_SUCCESS;
1050 if (options & LYD_NEWOPT_OPAQ) {
1051 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1052 }
1053 if (!r) {
1054 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1055 NULL, LYD_JSON), cleanup);
1056 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1057 }
1058
1059 /* try to find any existing nodes in the path */
1060 if (parent) {
1061 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1062 if (ret == LY_SUCCESS) {
1063 /* the node exists, are we supposed to update it or is it just a default? */
1064 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1065 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1066 ret = LY_EEXIST;
1067 goto cleanup;
1068 }
1069
1070 /* update the existing node */
1071 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1072 goto cleanup;
1073 } else if (ret == LY_EINCOMPLETE) {
1074 /* some nodes were found, adjust the iterator to the next segment */
1075 ++path_idx;
1076 } else if (ret == LY_ENOTFOUND) {
1077 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1078 if (lysc_data_parent(p[LY_ARRAY_SIZE(p) - 1].node)) {
1079 node = parent;
1080 }
1081 } else {
1082 /* error */
1083 goto cleanup;
1084 }
1085 }
1086
1087 /* create all the non-existing nodes in a loop */
1088 for (; path_idx < LY_ARRAY_SIZE(p); ++path_idx) {
1089 cur_parent = node;
1090 schema = p[path_idx].node;
1091
1092 switch (schema->nodetype) {
1093 case LYS_LIST:
1094 if (!(schema->flags & LYS_KEYLESS)) {
1095 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1096 /* creating opaque list without keys */
1097 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1098 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1099 } else {
1100 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1101 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1102 }
1103 break;
1104 }
1105 /* fallthrough */
1106 case LYS_CONTAINER:
1107 case LYS_NOTIF:
1108 case LYS_RPC:
1109 case LYS_ACTION:
1110 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1111 break;
1112 case LYS_LEAFLIST:
1113 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1114 /* creating opaque leaf-list without value */
1115 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1116 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1117 } else {
1118 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1119 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1120 }
1121 break;
1122 case LYS_LEAF:
1123 /* make there is some value */
1124 if (!value) {
1125 value = "";
1126 }
1127
1128 r = LY_SUCCESS;
1129 if (options & LYD_NEWOPT_OPAQ) {
1130 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1131 }
1132 if (!r) {
1133 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1134 LYD_JSON, &node), cleanup);
1135 } else {
1136 /* creating opaque leaf without value */
1137 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1138 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1139 }
1140 break;
1141 case LYS_ANYDATA:
1142 case LYS_ANYXML:
1143 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1144 break;
1145 default:
1146 LOGINT(ctx);
1147 ret = LY_EINT;
1148 goto cleanup;
1149 }
1150
1151 if (cur_parent) {
1152 /* connect to the parent */
1153 lyd_insert_node(cur_parent, NULL, node);
1154 } else if (parent) {
1155 /* connect to top-level siblings */
1156 lyd_insert_node(NULL, &parent, node);
1157 }
1158
1159 /* update remembered nodes */
1160 if (!nparent) {
1161 nparent = node;
1162 }
1163 nnode = node;
1164 }
1165
1166cleanup:
1167 lyxp_expr_free(ctx, exp);
1168 ly_path_free(ctx, p);
1169 if (!ret) {
1170 /* set out params only on success */
1171 if (new_parent) {
1172 *new_parent = nparent;
1173 }
1174 if (new_node) {
1175 *new_node = nnode;
1176 }
1177 }
1178 return ret;
1179}
1180
Michal Vasko90932a92020-02-12 14:33:03 +01001181struct lyd_node *
1182lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
1183{
1184 const struct lysc_node *prev_key;
1185 struct lyd_node *match = NULL;
1186
1187 if (!first_sibling) {
1188 return NULL;
1189 }
1190
1191 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
1192 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
1193 }
1194
1195 return match;
1196}
1197
1198/**
1199 * @brief Insert node after a sibling.
1200 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001201 * Handles inserting into NP containers and key-less lists.
1202 *
Michal Vasko90932a92020-02-12 14:33:03 +01001203 * @param[in] sibling Sibling to insert after.
1204 * @param[in] node Node to insert.
1205 */
1206static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001207lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001208{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001209 struct lyd_node_inner *par;
1210
Michal Vasko90932a92020-02-12 14:33:03 +01001211 assert(!node->next && (node->prev == node));
1212
1213 node->next = sibling->next;
1214 node->prev = sibling;
1215 sibling->next = node;
1216 if (node->next) {
1217 /* sibling had a succeeding node */
1218 node->next->prev = node;
1219 } else {
1220 /* sibling was last, find first sibling and change its prev */
1221 if (sibling->parent) {
1222 sibling = sibling->parent->child;
1223 } else {
1224 for (; sibling->prev->next != node; sibling = sibling->prev);
1225 }
1226 sibling->prev = node;
1227 }
1228 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001229
Michal Vasko9f96a052020-03-10 09:41:45 +01001230 for (par = node->parent; par; par = par->parent) {
1231 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1232 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001233 par->flags &= ~LYD_DEFAULT;
1234 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001235 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1236 /* rehash key-less list */
1237 lyd_hash((struct lyd_node *)par);
1238 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001239 }
1240
1241 /* insert into hash table */
1242 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001243}
1244
1245/**
1246 * @brief Insert node before a sibling.
1247 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001248 * Handles inserting into NP containers and key-less lists.
1249 *
Michal Vasko90932a92020-02-12 14:33:03 +01001250 * @param[in] sibling Sibling to insert before.
1251 * @param[in] node Node to insert.
1252 */
1253static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001254lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001255{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001256 struct lyd_node_inner *par;
1257
Michal Vasko90932a92020-02-12 14:33:03 +01001258 assert(!node->next && (node->prev == node));
1259
1260 node->next = sibling;
1261 /* covers situation of sibling being first */
1262 node->prev = sibling->prev;
1263 sibling->prev = node;
1264 if (node->prev->next) {
1265 /* sibling had a preceding node */
1266 node->prev->next = node;
1267 } else if (sibling->parent) {
1268 /* sibling was first and we must also change parent child pointer */
1269 sibling->parent->child = node;
1270 }
1271 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001272
Michal Vasko9f96a052020-03-10 09:41:45 +01001273 for (par = node->parent; par; par = par->parent) {
1274 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1275 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001276 par->flags &= ~LYD_DEFAULT;
1277 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001278 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1279 /* rehash key-less list */
1280 lyd_hash((struct lyd_node *)par);
1281 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001282 }
1283
1284 /* insert into hash table */
1285 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001286}
1287
1288/**
1289 * @brief Insert node as the last child of a parent.
1290 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001291 * Handles inserting into NP containers and key-less lists.
1292 *
Michal Vasko90932a92020-02-12 14:33:03 +01001293 * @param[in] parent Parent to insert into.
1294 * @param[in] node Node to insert.
1295 */
1296static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001297lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001298{
1299 struct lyd_node_inner *par;
1300
Michal Vasko0249f7c2020-03-05 16:36:40 +01001301 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001302 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001303
1304 par = (struct lyd_node_inner *)parent;
1305
1306 if (!par->child) {
1307 par->child = node;
1308 } else {
1309 node->prev = par->child->prev;
1310 par->child->prev->next = node;
1311 par->child->prev = node;
1312 }
1313 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001314
Michal Vasko9f96a052020-03-10 09:41:45 +01001315 for (; par; par = par->parent) {
1316 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1317 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001318 par->flags &= ~LYD_DEFAULT;
1319 }
Michal Vasko52927e22020-03-16 17:26:14 +01001320 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001321 /* rehash key-less list */
1322 lyd_hash((struct lyd_node *)par);
1323 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001324 }
1325
1326 /* insert into hash table */
1327 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001328}
1329
1330void
Michal Vasko9b368d32020-02-14 13:53:31 +01001331lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001332{
Michal Vasko9b368d32020-02-14 13:53:31 +01001333 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001334 const struct lysc_node *skey = NULL;
1335 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001336
Michal Vasko52927e22020-03-16 17:26:14 +01001337 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001338
1339 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1340 parent = (struct lyd_node *)(*first_sibling)->parent;
1341 }
Michal Vasko90932a92020-02-12 14:33:03 +01001342
1343 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001344 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001345 /* it is key and we need to insert it at the correct place */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001346 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent, 0), node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +01001347 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001348 lyd_insert_after_node(anchor, node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001349 } else if (lyd_node_children(parent, 0)) {
1350 lyd_insert_before_node(lyd_node_children(parent, 0), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001351 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001352 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001353 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001354
1355 /* hash list if all its keys were added */
1356 assert(parent->schema->nodetype == LYS_LIST);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001357 anchor = lyd_node_children(parent, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +01001358 has_keys = 1;
1359 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1360 if (!anchor || (anchor->schema != skey)) {
1361 /* key missing */
1362 has_keys = 0;
1363 break;
1364 }
1365
1366 anchor = anchor->next;
1367 }
1368 if (has_keys) {
1369 lyd_hash(parent);
1370 }
1371
Michal Vasko90932a92020-02-12 14:33:03 +01001372 } else {
1373 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001374 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001375 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001376 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001377 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001378 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001379 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001380 anchor = anchor->prev;
1381 }
1382
Michal Vaskoc193ce92020-03-06 11:04:48 +01001383 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001384 /* insert after last sibling from this module */
1385 lyd_insert_after_node(anchor, node);
1386 } else {
1387 /* no data from this module, insert at the last position */
1388 lyd_insert_after_node((*first_sibling)->prev, node);
1389 }
Michal Vasko90932a92020-02-12 14:33:03 +01001390 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001391 /* the only sibling */
1392 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001393 }
Michal Vasko90932a92020-02-12 14:33:03 +01001394}
1395
Michal Vaskof03ed032020-03-04 13:31:44 +01001396static LY_ERR
1397lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1398{
1399 const struct lysc_node *par2;
1400
1401 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001402 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001403
1404 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001405 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001406
1407 if (parent) {
1408 /* inner node */
1409 if (par2 != parent) {
1410 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
1411 return LY_EINVAL;
1412 }
1413 } else {
1414 /* top-level node */
1415 if (par2) {
1416 LOGERR(parent->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
1417 return LY_EINVAL;
1418 }
1419 }
1420
1421 return LY_SUCCESS;
1422}
1423
1424API LY_ERR
1425lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1426{
1427 struct lyd_node *iter;
1428
Michal Vasko654bc852020-06-23 13:28:06 +02001429 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001430
1431 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1432
1433 if (node->schema->flags & LYS_KEY) {
1434 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1435 return LY_EINVAL;
1436 }
1437
1438 if (node->parent || node->prev->next) {
1439 lyd_unlink_tree(node);
1440 }
1441
1442 while (node) {
1443 iter = node->next;
1444 lyd_unlink_tree(node);
1445 lyd_insert_node(parent, NULL, node);
1446 node = iter;
1447 }
1448 return LY_SUCCESS;
1449}
1450
1451API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001452lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1453{
1454 struct lyd_node *iter;
1455
1456 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1457
Michal Vasko62ed12d2020-05-21 10:08:25 +02001458 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001459
1460 if (node->schema->flags & LYS_KEY) {
1461 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1462 return LY_EINVAL;
1463 }
1464
1465 if (node->parent || node->prev->next) {
1466 lyd_unlink_tree(node);
1467 }
1468
1469 while (node) {
1470 iter = node->next;
1471 lyd_unlink_tree(node);
1472 lyd_insert_node(NULL, &sibling, node);
1473 node = iter;
1474 }
1475 return LY_SUCCESS;
1476}
1477
Michal Vasko0249f7c2020-03-05 16:36:40 +01001478static LY_ERR
1479lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1480{
1481 if (sibling->parent) {
1482 /* nested, we do not care for the order */
1483 return LY_SUCCESS;
1484 }
1485
1486 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001487 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1488 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001489 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 +01001490 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001491 return LY_EINVAL;
1492 }
1493
Michal Vaskoc193ce92020-03-06 11:04:48 +01001494 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1495 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001496 /* inserting before/after its module data */
1497 return LY_SUCCESS;
1498 }
1499 }
1500
1501 /* find first sibling */
1502 while (sibling->prev->next) {
1503 sibling = sibling->prev;
1504 }
1505
1506 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001507 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001508 /* inserting before its module data */
1509 return LY_SUCCESS;
1510 }
1511 }
1512
1513 /* check there are no data of this module */
1514 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001515 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001516 /* some data of this module found */
1517 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001518 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001519 return LY_EINVAL;
1520 }
1521 }
1522
1523 return LY_SUCCESS;
1524}
1525
Michal Vaskob1b5c262020-03-05 14:29:47 +01001526API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001527lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1528{
1529 struct lyd_node *iter;
1530
1531 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1532
Michal Vasko62ed12d2020-05-21 10:08:25 +02001533 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001534
1535 if (node->schema->flags & LYS_KEY) {
1536 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1537 return LY_EINVAL;
1538 } else if (sibling->schema->flags & LYS_KEY) {
1539 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1540 return LY_EINVAL;
1541 }
1542
Michal Vasko0249f7c2020-03-05 16:36:40 +01001543 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1544
Michal Vaskof03ed032020-03-04 13:31:44 +01001545 if (node->parent || node->prev->next) {
1546 lyd_unlink_tree(node);
1547 }
1548
1549 /* insert in reverse order to get the original order */
1550 node = node->prev;
1551 while (node) {
1552 iter = node->prev;
1553 lyd_unlink_tree(node);
1554
1555 lyd_insert_before_node(sibling, node);
1556 /* move the anchor accordingly */
1557 sibling = node;
1558
1559 node = (iter == node) ? NULL : iter;
1560 }
1561 return LY_SUCCESS;
1562}
1563
1564API LY_ERR
1565lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1566{
1567 struct lyd_node *iter;
1568
1569 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1570
Michal Vasko62ed12d2020-05-21 10:08:25 +02001571 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001572
1573 if (node->schema->flags & LYS_KEY) {
1574 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1575 return LY_EINVAL;
1576 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1577 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1578 return LY_EINVAL;
1579 }
1580
Michal Vasko0249f7c2020-03-05 16:36:40 +01001581 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1582
Michal Vaskof03ed032020-03-04 13:31:44 +01001583 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
1591 lyd_insert_after_node(sibling, node);
1592 /* move the anchor accordingly */
1593 sibling = node;
1594
1595 node = iter;
1596 }
1597 return LY_SUCCESS;
1598}
1599
1600API void
1601lyd_unlink_tree(struct lyd_node *node)
1602{
1603 struct lyd_node *iter;
1604
1605 if (!node) {
1606 return;
1607 }
1608
1609 /* unlink from siblings */
1610 if (node->prev->next) {
1611 node->prev->next = node->next;
1612 }
1613 if (node->next) {
1614 node->next->prev = node->prev;
1615 } else {
1616 /* unlinking the last node */
1617 if (node->parent) {
1618 iter = node->parent->child;
1619 } else {
1620 iter = node->prev;
1621 while (iter->prev != node) {
1622 iter = iter->prev;
1623 }
1624 }
1625 /* update the "last" pointer from the first node */
1626 iter->prev = node->prev;
1627 }
1628
1629 /* unlink from parent */
1630 if (node->parent) {
1631 if (node->parent->child == node) {
1632 /* the node is the first child */
1633 node->parent->child = node->next;
1634 }
1635
1636 lyd_unlink_hash(node);
1637
1638 /* check for keyless list and update its hash */
1639 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001640 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001641 lyd_hash(iter);
1642 }
1643 }
1644
1645 node->parent = NULL;
1646 }
1647
1648 node->next = NULL;
1649 node->prev = node;
1650}
1651
Michal Vasko90932a92020-02-12 14:33:03 +01001652LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001653lyd_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 +01001654 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 +01001655 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001656{
1657 LY_ERR ret;
1658 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001659 struct lyd_meta *mt, *last;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001660 LY_ARRAY_SIZE_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001661
Michal Vasko9f96a052020-03-10 09:41:45 +01001662 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001663
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001664 LY_ARRAY_FOR(mod->compiled->exts, u) {
1665 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1666 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001667 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001668 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001669 break;
1670 }
1671 }
1672 if (!ant) {
1673 /* attribute is not defined as a metadata annotation (RFC 7952) */
1674 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1675 mod->name, name_len, name);
1676 return LY_EINVAL;
1677 }
1678
Michal Vasko9f96a052020-03-10 09:41:45 +01001679 mt = calloc(1, sizeof *mt);
1680 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1681 mt->parent = parent;
1682 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001683 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 +01001684 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001685 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001686 return ret;
1687 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001688 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001689
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001690 /* insert as the last attribute */
1691 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001692 if (parent->meta) {
1693 for (last = parent->meta; last->next; last = last->next);
1694 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001695 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001696 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001697 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001698 } else if (*meta) {
1699 for (last = *meta; last->next; last = last->next);
1700 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001701 }
1702
1703 /* remove default flags from NP containers */
1704 while (parent && (parent->flags & LYD_DEFAULT)) {
1705 parent->flags &= ~LYD_DEFAULT;
1706 parent = (struct lyd_node *)parent->parent;
1707 }
1708
Michal Vasko9f96a052020-03-10 09:41:45 +01001709 if (meta) {
1710 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001711 }
1712 return ret;
1713}
1714
Michal Vasko52927e22020-03-16 17:26:14 +01001715LY_ERR
1716ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1717 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1718 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1719{
1720 struct ly_attr *at, *last;
1721 struct lyd_node_opaq *opaq;
1722
1723 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1724 assert(name && name_len);
1725 assert((prefix_len && ns) || (!prefix_len && !ns));
1726
1727 if (!value_len) {
1728 value = "";
1729 }
1730
1731 at = calloc(1, sizeof *at);
1732 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1733 at->parent = (struct lyd_node_opaq *)parent;
1734 at->name = lydict_insert(ctx, name, name_len);
1735 if (dynamic && *dynamic) {
1736 at->value = lydict_insert_zc(ctx, (char *)value);
1737 *dynamic = 0;
1738 } else {
1739 at->value = lydict_insert(ctx, value, value_len);
1740 }
1741
1742 at->format = format;
1743 at->val_prefs = val_prefs;
1744 if (ns) {
1745 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1746 at->prefix.ns = lydict_insert(ctx, ns, 0);
1747 }
1748
1749 /* insert as the last attribute */
1750 if (parent) {
1751 opaq = (struct lyd_node_opaq *)parent;
1752 if (opaq->attr) {
1753 for (last = opaq->attr; last->next; last = last->next);
1754 last->next = at;
1755 } else {
1756 opaq->attr = at;
1757 }
1758 } else if (*attr) {
1759 for (last = *attr; last->next; last = last->next);
1760 last->next = at;
1761 }
1762
1763 if (attr) {
1764 *attr = at;
1765 }
1766 return LY_SUCCESS;
1767}
1768
Radek Krejci084289f2019-07-09 17:35:30 +02001769API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001770lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001771{
Michal Vasko004d3152020-06-11 19:59:22 +02001772 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001773
Michal Vasko004d3152020-06-11 19:59:22 +02001774 if (ly_path_eval(path, tree, &target)) {
1775 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001776 }
1777
Michal Vasko004d3152020-06-11 19:59:22 +02001778 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001779}
1780
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001781API LY_ERR
1782lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1783{
1784 const struct lyd_node *iter1, *iter2;
1785 struct lyd_node_term *term1, *term2;
1786 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001787 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001788 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001789
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001790 if (!node1 || !node2) {
1791 if (node1 == node2) {
1792 return LY_SUCCESS;
1793 } else {
1794 return LY_ENOT;
1795 }
1796 }
1797
Michal Vasko52927e22020-03-16 17:26:14 +01001798 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001799 return LY_ENOT;
1800 }
1801
1802 if (node1->hash != node2->hash) {
1803 return LY_ENOT;
1804 }
Michal Vasko52927e22020-03-16 17:26:14 +01001805 /* 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 +02001806
Michal Vasko52927e22020-03-16 17:26:14 +01001807 if (!node1->schema) {
1808 opaq1 = (struct lyd_node_opaq *)node1;
1809 opaq2 = (struct lyd_node_opaq *)node2;
1810 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001811 return LY_ENOT;
1812 }
Michal Vasko52927e22020-03-16 17:26:14 +01001813 switch (opaq1->format) {
1814 case LYD_XML:
1815 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1816 return LY_ENOT;
1817 }
1818 break;
1819 case LYD_SCHEMA:
1820 /* not allowed */
1821 LOGINT(LYD_NODE_CTX(node1));
1822 return LY_EINT;
1823 }
1824 if (options & LYD_COMPARE_FULL_RECURSION) {
1825 iter1 = opaq1->child;
1826 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001827 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001828 }
1829 return LY_SUCCESS;
1830 } else {
1831 switch (node1->schema->nodetype) {
1832 case LYS_LEAF:
1833 case LYS_LEAFLIST:
1834 if (options & LYD_COMPARE_DEFAULTS) {
1835 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1836 return LY_ENOT;
1837 }
1838 }
1839
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001840 term1 = (struct lyd_node_term *)node1;
1841 term2 = (struct lyd_node_term *)node2;
1842 if (term1->value.realtype != term2->value.realtype) {
1843 return LY_ENOT;
1844 }
Michal Vasko52927e22020-03-16 17:26:14 +01001845
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001846 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001847 case LYS_CONTAINER:
1848 if (options & LYD_COMPARE_DEFAULTS) {
1849 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1850 return LY_ENOT;
1851 }
1852 }
1853 if (options & LYD_COMPARE_FULL_RECURSION) {
1854 iter1 = ((struct lyd_node_inner*)node1)->child;
1855 iter2 = ((struct lyd_node_inner*)node2)->child;
1856 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001857 }
1858 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001859 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001860 case LYS_ACTION:
1861 if (options & LYD_COMPARE_FULL_RECURSION) {
1862 /* TODO action/RPC
1863 goto all_children_compare;
1864 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001865 }
1866 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001867 case LYS_NOTIF:
1868 if (options & LYD_COMPARE_FULL_RECURSION) {
1869 /* TODO Notification
1870 goto all_children_compare;
1871 */
1872 }
1873 return LY_SUCCESS;
1874 case LYS_LIST:
1875 iter1 = ((struct lyd_node_inner*)node1)->child;
1876 iter2 = ((struct lyd_node_inner*)node2)->child;
1877
1878 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1879 /* lists with keys, their equivalence is based on their keys */
1880 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001881 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001882 key = key->next) {
1883 if (lyd_compare(iter1, iter2, options)) {
1884 return LY_ENOT;
1885 }
1886 iter1 = iter1->next;
1887 iter2 = iter2->next;
1888 }
1889 } else {
1890 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1891
1892 all_children_compare:
1893 if (!iter1 && !iter2) {
1894 /* no children, nothing to compare */
1895 return LY_SUCCESS;
1896 }
1897
1898 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1899 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1900 return LY_ENOT;
1901 }
1902 }
1903 if (iter1 || iter2) {
1904 return LY_ENOT;
1905 }
1906 }
1907 return LY_SUCCESS;
1908 case LYS_ANYXML:
1909 case LYS_ANYDATA:
1910 any1 = (struct lyd_node_any*)node1;
1911 any2 = (struct lyd_node_any*)node2;
1912
1913 if (any1->value_type != any2->value_type) {
1914 return LY_ENOT;
1915 }
1916 switch (any1->value_type) {
1917 case LYD_ANYDATA_DATATREE:
1918 iter1 = any1->value.tree;
1919 iter2 = any2->value.tree;
1920 goto all_children_compare;
1921 case LYD_ANYDATA_STRING:
1922 case LYD_ANYDATA_XML:
1923 case LYD_ANYDATA_JSON:
1924 len1 = strlen(any1->value.str);
1925 len2 = strlen(any2->value.str);
1926 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1927 return LY_ENOT;
1928 }
1929 return LY_SUCCESS;
1930 #if 0 /* TODO LYB format */
1931 case LYD_ANYDATA_LYB:
1932 int len1 = lyd_lyb_data_length(any1->value.mem);
1933 int len2 = lyd_lyb_data_length(any2->value.mem);
1934 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
1935 return LY_ENOT;
1936 }
1937 return LY_SUCCESS;
1938 #endif
1939 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001940 }
1941 }
1942
Michal Vasko52927e22020-03-16 17:26:14 +01001943 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001944 return LY_EINT;
1945}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001946
1947/**
Michal Vasko52927e22020-03-16 17:26:14 +01001948 * @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 +02001949 *
1950 * 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 +02001951 *
1952 * @param[in] node Original node to duplicate
1953 * @param[in] parent Parent to insert into, NULL for top-level sibling.
1954 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1955 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1956 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
1957 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02001958 */
Michal Vasko52927e22020-03-16 17:26:14 +01001959static LY_ERR
1960lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
1961 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001962{
Michal Vasko52927e22020-03-16 17:26:14 +01001963 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001964 struct lyd_node *dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001965 LY_ARRAY_SIZE_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001966
Michal Vasko52927e22020-03-16 17:26:14 +01001967 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001968
Michal Vasko52927e22020-03-16 17:26:14 +01001969 if (!node->schema) {
1970 dup = calloc(1, sizeof(struct lyd_node_opaq));
1971 } else {
1972 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001973 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001974 case LYS_ACTION:
1975 case LYS_NOTIF:
1976 case LYS_CONTAINER:
1977 case LYS_LIST:
1978 dup = calloc(1, sizeof(struct lyd_node_inner));
1979 break;
1980 case LYS_LEAF:
1981 case LYS_LEAFLIST:
1982 dup = calloc(1, sizeof(struct lyd_node_term));
1983 break;
1984 case LYS_ANYDATA:
1985 case LYS_ANYXML:
1986 dup = calloc(1, sizeof(struct lyd_node_any));
1987 break;
1988 default:
1989 LOGINT(LYD_NODE_CTX(node));
1990 ret = LY_EINT;
1991 goto error;
1992 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001993 }
Michal Vasko52927e22020-03-16 17:26:14 +01001994 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001995
Michal Vaskof6df0a02020-06-16 13:08:34 +02001996 if (options & LYD_DUP_WITH_FLAGS) {
1997 dup->flags = node->flags;
1998 } else {
1999 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2000 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002001 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002002 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002003
2004 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
2005
2006 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002007 if (!dup->schema) {
2008 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2009 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2010 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002011
2012 if (options & LYD_DUP_RECURSIVE) {
2013 /* duplicate all the children */
2014 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01002015 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
2016 }
2017 }
2018 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2019 opaq->format = orig->format;
2020 if (orig->prefix.pref) {
2021 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2022 }
2023 if (orig->prefix.ns) {
2024 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2025 }
2026 if (orig->val_prefs) {
2027 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_SIZE(orig->val_prefs), ret, error);
2028 LY_ARRAY_FOR(orig->val_prefs, u) {
2029 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2030 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2031 LY_ARRAY_INCREMENT(opaq->val_prefs);
2032 }
2033 }
2034 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2035 opaq->ctx = orig->ctx;
2036 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2037 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2038 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2039
2040 term->hash = orig->hash;
2041 term->value.realtype = orig->value.realtype;
2042 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2043 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2044 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2045 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2046 struct lyd_node *child;
2047
2048 if (options & LYD_DUP_RECURSIVE) {
2049 /* duplicate all the children */
2050 LY_LIST_FOR(orig->child, child) {
2051 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002052 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002053 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002054 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002055 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002056 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002057 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002058 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002059 if (!child) {
2060 /* possibly not keys are present in filtered tree */
2061 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002062 } else if (child->schema != key) {
2063 /* possibly not all keys are present in filtered tree,
2064 * but there can be also some non-key nodes */
2065 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002066 }
Michal Vasko52927e22020-03-16 17:26:14 +01002067 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002068 child = child->next;
2069 }
2070 }
2071 lyd_hash(dup);
2072 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01002073 struct lyd_node_any *any = (struct lyd_node_any *)dup;
2074 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002075
2076 any->hash = orig->hash;
2077 any->value_type = orig->value_type;
2078 switch (any->value_type) {
2079 case LYD_ANYDATA_DATATREE:
2080 if (orig->value.tree) {
2081 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02002082 if (!any->value.tree) {
2083 /* get the last error's error code recorded by lyd_dup */
2084 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
2085 ret = ei ? ei->prev->no : LY_EOTHER;
2086 goto error;
2087 }
2088 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0 ,error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002089 }
2090 break;
2091 case LYD_ANYDATA_STRING:
2092 case LYD_ANYDATA_XML:
2093 case LYD_ANYDATA_JSON:
2094 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01002095 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02002096 }
2097 break;
2098 }
2099 }
2100
Michal Vasko52927e22020-03-16 17:26:14 +01002101 /* insert */
2102 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002103
2104 if (dup_p) {
2105 *dup_p = dup;
2106 }
2107 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002108
2109error:
Michal Vasko52927e22020-03-16 17:26:14 +01002110 lyd_free_tree(dup);
2111 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002112}
2113
2114API struct lyd_node *
2115lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
2116{
2117 struct ly_ctx *ctx;
2118 const struct lyd_node *orig; /* original node to be duplicated */
2119 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002120 struct lyd_node *top = NULL; /* the most higher created node */
2121 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
2122 int keyless_parent_list = 0;
2123
2124 LY_CHECK_ARG_RET(NULL, node, NULL);
2125 ctx = node->schema->module->ctx;
2126
2127 if (options & LYD_DUP_WITH_PARENTS) {
2128 struct lyd_node_inner *orig_parent, *iter;
2129 int repeat = 1;
2130 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2131 if (parent && parent->schema == orig_parent->schema) {
2132 /* stop creating parents, connect what we have into the provided parent */
2133 iter = parent;
2134 repeat = 0;
2135 /* get know if there is a keyless list which we will have to rehash */
2136 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002137 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002138 keyless_parent_list = 1;
2139 break;
2140 }
2141 }
2142 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01002143 iter = NULL;
2144 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2145 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002146 }
2147 if (!local_parent) {
2148 local_parent = iter;
2149 }
2150 if (iter->child) {
2151 /* 1) list - add after keys
2152 * 2) provided parent with some children */
2153 iter->child->prev->next = top;
2154 if (top) {
2155 top->prev = iter->child->prev;
2156 iter->child->prev = top;
2157 }
2158 } else {
2159 iter->child = top;
2160 if (iter->schema->nodetype == LYS_LIST) {
2161 /* keyless list - we will need to rehash it since we are going to add nodes into it */
2162 keyless_parent_list = 1;
2163 }
2164 }
2165 if (top) {
2166 top->parent = iter;
2167 }
2168 top = (struct lyd_node*)iter;
2169 }
2170 if (repeat && parent) {
2171 /* given parent and created parents chain actually do not interconnect */
2172 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2173 goto error;
2174 }
2175 } else {
2176 local_parent = parent;
2177 }
2178
Radek Krejci22ebdba2019-07-25 13:59:43 +02002179 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002180 /* if there is no local parent, it will be inserted into first */
2181 LY_CHECK_GOTO(lyd_dup_recursive(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002182 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
2183 break;
2184 }
2185 }
2186 if (keyless_parent_list) {
2187 /* rehash */
2188 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002189 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002190 lyd_hash((struct lyd_node*)local_parent);
2191 }
2192 }
2193 }
2194 return first;
2195
2196error:
2197 if (top) {
2198 lyd_free_tree(top);
2199 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002200 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002201 }
2202 return NULL;
2203}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002204
Michal Vasko4490d312020-06-16 13:08:55 +02002205/**
2206 * @brief Merge a source sibling into target siblings.
2207 *
2208 * @param[in,out] first_trg First target sibling, is updated if top-level.
2209 * @param[in] parent_trg Target parent.
2210 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2211 * @param[in] options Merge options.
2212 * @return LY_ERR value.
2213 */
2214static LY_ERR
2215lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2216 int options)
2217{
2218 LY_ERR ret;
2219 const struct lyd_node *child_src, *tmp, *sibling_src;
2220 struct lyd_node *match_trg, *dup_src, *next, *elem;
2221 struct lysc_type *type;
2222 LYD_ANYDATA_VALUETYPE tmp_val_type;
2223 union lyd_any_value tmp_val;
2224
2225 sibling_src = *sibling_src_p;
2226 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2227 /* try to find the exact instance */
2228 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2229 } else {
2230 /* try to simply find the node, there cannot be more instances */
2231 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2232 }
2233
2234 if (!ret) {
2235 /* node found, make sure even value matches for all node types */
2236 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2237 /* since they are different, they cannot both be default */
2238 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2239
2240 /* update value (or only LYD_DEFAULT flag) only if no flag set or the source node is not default */
2241 if (!(options & LYD_MERGE_EXPLICIT) || !(sibling_src->flags & LYD_DEFAULT)) {
2242 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2243 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2244 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2245 &((struct lyd_node_term *)match_trg)->value));
2246
2247 /* copy flags and add LYD_NEW */
2248 match_trg->flags = sibling_src->flags | LYD_NEW;
2249 }
2250 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2251 if (options & LYD_MERGE_DESTRUCT) {
2252 dup_src = (struct lyd_node *)sibling_src;
2253 lyd_unlink_tree(dup_src);
2254 /* spend it */
2255 *sibling_src_p = NULL;
2256 } else {
2257 dup_src = lyd_dup(sibling_src, NULL, 0);
2258 LY_CHECK_RET(!dup_src, LY_EMEM);
2259 }
2260 /* just switch values */
2261 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2262 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2263 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2264 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2265 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2266 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2267
2268 /* copy flags and add LYD_NEW */
2269 match_trg->flags = sibling_src->flags | LYD_NEW;
2270
2271 /* dup_src is not needed, actually */
2272 lyd_free_tree(dup_src);
2273 } else {
2274 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002275 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002276 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2277 }
2278 }
2279 } else {
2280 /* node not found, merge it */
2281 if (options & LYD_MERGE_DESTRUCT) {
2282 dup_src = (struct lyd_node *)sibling_src;
2283 lyd_unlink_tree(dup_src);
2284 /* spend it */
2285 *sibling_src_p = NULL;
2286 } else {
2287 dup_src = lyd_dup(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS);
2288 LY_CHECK_RET(!dup_src, LY_EMEM);
2289 }
2290
2291 /* set LYD_NEW for all the new nodes, required for validation */
2292 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2293 elem->flags |= LYD_NEW;
2294 LYD_TREE_DFS_END(dup_src, next, elem);
2295 }
2296
2297 lyd_insert_node(parent_trg, first_trg, dup_src);
2298 }
2299
2300 return LY_SUCCESS;
2301}
2302
2303API LY_ERR
2304lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options)
2305{
2306 const struct lyd_node *sibling_src, *tmp;
2307 int first;
2308
2309 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2310
2311 if (!source) {
2312 /* nothing to merge */
2313 return LY_SUCCESS;
2314 }
2315
2316 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2317 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2318 return LY_EINVAL;
2319 }
2320
2321 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2322 first = sibling_src == source ? 1 : 0;
2323 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2324 if (first && !sibling_src) {
2325 /* source was spent (unlinked), move to the next node */
2326 source = tmp;
2327 }
2328
2329 if (options & LYD_MERGE_NOSIBLINGS) {
2330 break;
2331 }
2332 }
2333
2334 if (options & LYD_MERGE_DESTRUCT) {
2335 /* free any leftover source data that were not merged */
2336 lyd_free_siblings((struct lyd_node *)source);
2337 }
2338
2339 return LY_SUCCESS;
2340}
2341
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002342struct lyd_diff_userord {
2343 const struct lysc_node *schema; /**< User-ordered list/leaf-list schema node. */
2344 uint64_t pos; /**< Current position in the second tree. */
2345 const struct lyd_node **inst; /**< Sized array of current instance order. */
2346};
2347
2348enum lyd_diff_op {
2349 LYD_DIFF_OP_CREATE, /**< Subtree created. */
2350 LYD_DIFF_OP_DELETE, /**< Subtree deleted. */
2351 LYD_DIFF_OP_REPLACE, /**< Node value changed or (leaf-)list instance moved. */
2352 LYD_DIFF_OP_NONE, /**< No change of an existing inner node or default flag change of a term node. */
2353};
2354
2355static const char *
2356lyd_diff_op2str(enum lyd_diff_op op)
2357{
2358 switch (op) {
2359 case LYD_DIFF_OP_CREATE:
2360 return "create";
2361 case LYD_DIFF_OP_DELETE:
2362 return "delete";
2363 case LYD_DIFF_OP_REPLACE:
2364 return "replace";
2365 case LYD_DIFF_OP_NONE:
2366 return "none";
2367 }
2368
2369 LOGINT(NULL);
2370 return NULL;
2371}
2372
2373/**
2374 * @brief Add a new change into diff.
2375 *
2376 * @param[in] node Node (subtree) to add into diff.
2377 * @param[in] op Operation to set.
2378 * @param[in] orig_default Original default metadata to set.
2379 * @param[in] orig_value Original value metadata to set.
2380 * @param[in] key Key metadata to set.
2381 * @param[in] value Value metadata to set.
2382 * @param[in] orig_key Original key metadata to set.
2383 * @param[in,out] diff Diff to append to.
2384 * @return LY_ERR value.
2385 */
2386static LY_ERR
2387lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
2388 const char *key, const char *value, const char *orig_key, struct lyd_node **diff)
2389{
2390 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
2391 const struct lyd_node *parent = NULL;
2392 const struct lys_module *yang_mod;
2393
2394 /* find the first existing parent */
2395 siblings = *diff;
2396 while (1) {
2397 /* find next node parent */
2398 parent = node;
2399 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
2400 parent = (struct lyd_node *)parent->parent;
2401 }
2402 if (parent == node) {
2403 /* no more parents to find */
2404 break;
2405 }
2406
2407 /* check whether it exists in the diff */
2408 if (lyd_find_sibling_first(siblings, parent, &match)) {
2409 break;
2410 }
2411
2412 /* another parent found */
2413 diff_parent = match;
2414
2415 /* move down in the diff */
2416 siblings = LYD_CHILD(match);
2417 }
2418
2419 /* duplicate the subtree (and connect to the diff if possible) */
2420 dup = lyd_dup(node, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS);
2421 LY_CHECK_RET(!dup, LY_EMEM);
2422
2423 /* find the first duplicated parent */
2424 if (!diff_parent) {
2425 diff_parent = (struct lyd_node *)dup->parent;
2426 while (diff_parent && diff_parent->parent) {
2427 diff_parent = (struct lyd_node *)diff_parent->parent;
2428 }
2429 } else {
2430 diff_parent = (struct lyd_node *)dup;
2431 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
2432 diff_parent = (struct lyd_node *)diff_parent->parent;
2433 }
2434 }
2435
2436 /* no parent existed, must be manually connected */
2437 if (!diff_parent) {
2438 /* there actually was no parent to duplicate */
2439 if (*diff) {
2440 lyd_insert_sibling(*diff, dup);
2441 } else {
2442 *diff = dup;
2443 }
2444 } else if (!diff_parent->parent) {
2445 if (*diff) {
2446 lyd_insert_sibling(*diff, diff_parent);
2447 } else {
2448 *diff = diff_parent;
2449 }
2450 }
2451
2452 /* get module with the operation metadata */
2453 yang_mod = LYD_NODE_CTX(node)->list.objs[1];
2454 assert(!strcmp(yang_mod->name, "yang"));
2455
2456 /* add parent operation, if any */
2457 if (diff_parent && (diff_parent != dup) && !lyd_new_meta(diff_parent, yang_mod, "operation", "none")) {
2458 return LY_EMEM;
2459 }
2460
2461 /* add subtree operation */
2462 if (!lyd_new_meta(dup, yang_mod, "operation", lyd_diff_op2str(op))) {
2463 return LY_EMEM;
2464 }
2465
2466 /* orig-default */
2467 if (orig_default && !lyd_new_meta(dup, yang_mod, "orig-default", orig_default)) {
2468 return LY_EMEM;
2469 }
2470
2471 /* orig-value */
2472 if (orig_value && !lyd_new_meta(dup, yang_mod, "orig-value", orig_value)) {
2473 return LY_EMEM;
2474 }
2475
2476 /* key */
2477 if (key && !lyd_new_meta(dup, yang_mod, "key", key)) {
2478 return LY_EMEM;
2479 }
2480
2481 /* value */
2482 if (value && !lyd_new_meta(dup, yang_mod, "value", value)) {
2483 return LY_EMEM;
2484 }
2485
2486 /* orig-key */
2487 if (orig_key && !lyd_new_meta(dup, yang_mod, "orig-key", orig_key)) {
2488 return LY_EMEM;
2489 }
2490
2491 return LY_SUCCESS;
2492}
2493
2494/**
2495 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
2496 *
2497 * @param[in] first
2498 * @param[in] schema Schema node of the list/leaf-list.
2499 * @param[in,out] userord Sized array of userord items.
2500 * @return Userord item for all the user-ordered list/leaf-list instances.
2501 */
2502static struct lyd_diff_userord *
2503lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
2504{
2505 struct lyd_diff_userord *item;
2506 const struct lyd_node *iter, **node;
2507 LY_ARRAY_SIZE_TYPE u;
2508
2509 LY_ARRAY_FOR(*userord, u) {
2510 if ((*userord)[u].schema == schema) {
2511 return &(*userord)[u];
2512 }
2513 }
2514
2515 /* it was not added yet, add it now */
2516 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
2517
2518 item->schema = schema;
2519 item->pos = 0;
2520 item->inst = NULL;
2521
2522 /* store all the instance pointers in the current order */
2523 if (first) {
2524 if (first->parent) {
2525 iter = first->parent->child;
2526 } else {
2527 for (iter = first; iter->prev->next; iter = iter->prev);
2528 }
2529 for (; iter; iter = iter->next) {
2530 if (iter->schema == first->schema) {
2531 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
2532 *node = iter;
2533 }
2534 }
2535 }
2536
2537 return item;
2538}
2539
2540/**
2541 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
2542 * lists/leaf-lists.
2543 *
2544 * @param[in] first Node from the first tree, can be NULL (on create).
2545 * @param[in] second Node from the second tree, can be NULL (on delete).
2546 * @param[in] options Diff options.
2547 * @param[in,out] userord Sized array of userord items for keeping the current node order.
2548 * @param[out] op Operation.
2549 * @param[out] orig_default Original default metadata.
2550 * @param[out] value Value metadata.
2551 * @param[out] orig_value Original value metadata
2552 * @param[out] key Key metadata.
2553 * @param[out] orig_key Original key metadata.
2554 * @return LY_SUCCESS on success,
2555 * @return LY_ENOT if there is no change to be added into diff,
2556 * @return LY_ERR value on other errors.
2557 */
2558static LY_ERR
2559lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, int options,
2560 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
2561 char **orig_value, char **key, char **orig_key)
2562{
2563 const struct lysc_node *schema;
2564 int dynamic;
2565 size_t buflen, bufused, first_pos, second_pos;
2566 struct lyd_diff_userord *userord_item;
2567
2568 assert(first || second);
2569
2570 *orig_default = NULL;
2571 *value = NULL;
2572 *orig_value = NULL;
2573 *key = NULL;
2574 *orig_key = NULL;
2575
2576 schema = first ? first->schema : second->schema;
2577 assert(lysc_is_userordered(schema));
2578
2579 /* get userord entry */
2580 userord_item = lyd_diff_userord_get(first, schema, userord);
2581 LY_CHECK_RET(!userord_item, LY_EMEM);
2582
2583 /* prepare position of the next instance */
2584 second_pos = userord_item->pos++;
2585
2586 /* find user-ordered first position */
2587 if (first) {
2588 for (first_pos = second_pos; first_pos < LY_ARRAY_SIZE(userord_item->inst); ++first_pos) {
2589 if (userord_item->inst[first_pos] == first) {
2590 break;
2591 }
2592 }
2593 assert(first_pos < LY_ARRAY_SIZE(userord_item->inst));
2594 }
2595
2596 /* learn operation first */
2597 if (!second) {
2598 *op = LYD_DIFF_OP_DELETE;
2599 } else if (!first) {
2600 *op = LYD_DIFF_OP_CREATE;
2601 } else {
2602 assert(schema->nodetype & (LYS_LIST | LYS_LEAFLIST));
2603 if (lyd_compare(second, userord_item->inst[second_pos], 0)) {
2604 /* in first, there is a different instance on the second position, we are going to move 'first' node */
2605 *op = LYD_DIFF_OP_REPLACE;
2606 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2607 /* default flag change */
2608 *op = LYD_DIFF_OP_NONE;
2609 } else {
2610 /* no changes */
2611 return LY_ENOT;
2612 }
2613 }
2614
2615 /*
2616 * set each attribute correctly based on the operation and node type
2617 */
2618
2619 /* orig-default */
2620 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype == LYS_LEAFLIST)
2621 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2622 if (first->flags & LYD_DEFAULT) {
2623 *orig_default = "true";
2624 } else {
2625 *orig_default = "false";
2626 }
2627 }
2628
2629 /* value */
2630 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
2631 if (second_pos) {
2632 *value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[second_pos - 1], &dynamic);
2633 if (!dynamic) {
2634 *value = strdup(*value);
2635 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2636 }
2637 } else {
2638 *value = strdup("");
2639 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2640 }
2641 }
2642
2643 /* orig-value */
2644 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2645 if (first_pos) {
2646 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[first_pos - 1], &dynamic);
2647 if (!dynamic) {
2648 *orig_value = strdup(*orig_value);
2649 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2650 }
2651 } else {
2652 *orig_value = strdup("");
2653 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2654 }
2655 }
2656
2657 /* key */
2658 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op ==LYD_DIFF_OP_CREATE))) {
2659 if (second_pos) {
2660 buflen = bufused = 0;
2661 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
2662 } else {
2663 *key = strdup("");
2664 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
2665 }
2666 }
2667
2668 /* orig-key */
2669 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2670 if (first_pos) {
2671 buflen = bufused = 0;
2672 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
2673 } else {
2674 *orig_key = strdup("");
2675 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
2676 }
2677 }
2678
2679 /*
2680 * update our instances - apply the change
2681 */
2682 if (*op == LYD_DIFF_OP_CREATE) {
2683 /* insert the instance */
2684 LY_ARRAY_RESIZE_ERR_RET(schema->module->ctx, userord_item->inst, LY_ARRAY_SIZE(userord_item->inst) + 1,
2685 ;, LY_EMEM);
2686 if (second_pos < LY_ARRAY_SIZE(userord_item->inst)) {
2687 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2688 (LY_ARRAY_SIZE(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
2689 }
2690 LY_ARRAY_INCREMENT(userord_item->inst);
2691 userord_item->inst[second_pos] = second;
2692
2693 } else if (*op == LYD_DIFF_OP_DELETE) {
2694 /* remove the instance */
2695 if (first_pos + 1 < LY_ARRAY_SIZE(userord_item->inst)) {
2696 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
2697 (LY_ARRAY_SIZE(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
2698 }
2699 LY_ARRAY_DECREMENT(userord_item->inst);
2700
2701 } else if (*op == LYD_DIFF_OP_REPLACE) {
2702 /* move the instances */
2703 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2704 (first_pos - second_pos) * sizeof *userord_item->inst);
2705 userord_item->inst[second_pos] = first;
2706 }
2707
2708 return LY_SUCCESS;
2709}
2710
2711/**
2712 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
2713 * lists/leaf-lists.
2714 *
2715 * @param[in] first Node from the first tree, can be NULL (on create).
2716 * @param[in] second Node from the second tree, can be NULL (on delete).
2717 * @param[in] options Diff options.
2718 * @param[out] op Operation.
2719 * @param[out] orig_default Original default metadata.
2720 * @param[out] orig_value Original value metadata.
2721 * @return LY_SUCCESS on success,
2722 * @return LY_ENOT if there is no change to be added into diff,
2723 * @return LY_ERR value on other errors.
2724 */
2725static LY_ERR
2726lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, int options, enum lyd_diff_op *op,
2727 const char **orig_default, char **orig_value)
2728{
2729 const struct lysc_node *schema;
2730 int dynamic;
2731
2732 assert(first || second);
2733
2734 *orig_default = NULL;
2735 *orig_value = NULL;
2736
2737 schema = first ? first->schema : second->schema;
2738 assert(!lysc_is_userordered(schema));
2739
2740 /* learn operation first */
2741 if (!second) {
2742 *op = LYD_DIFF_OP_DELETE;
2743 } else if (!first) {
2744 *op = LYD_DIFF_OP_CREATE;
2745 } else {
2746 switch (schema->nodetype) {
2747 case LYS_CONTAINER:
2748 case LYS_RPC:
2749 case LYS_ACTION:
2750 case LYS_NOTIF:
2751 /* no changes */
2752 return LY_ENOT;
2753 case LYS_LIST:
2754 case LYS_LEAFLIST:
2755 if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2756 /* default flag change */
2757 *op = LYD_DIFF_OP_NONE;
2758 } else {
2759 /* no changes */
2760 return LY_ENOT;
2761 }
2762 break;
2763 case LYS_LEAF:
2764 case LYS_ANYXML:
2765 case LYS_ANYDATA:
2766 if (lyd_compare(first, second, 0)) {
2767 /* different values */
2768 *op = LYD_DIFF_OP_REPLACE;
2769 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2770 /* default flag change */
2771 *op = LYD_DIFF_OP_NONE;
2772 } else {
2773 /* no changes */
2774 return LY_ENOT;
2775 }
2776 break;
2777 default:
2778 LOGINT_RET(schema->module->ctx);
2779 }
2780 }
2781
2782 /*
2783 * set each attribute correctly based on the operation and node type
2784 */
2785
2786 /* orig-default */
2787 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype & LYD_NODE_TERM)
2788 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2789 if (first->flags & LYD_DEFAULT) {
2790 *orig_default = "true";
2791 } else {
2792 *orig_default = "false";
2793 }
2794 }
2795
2796 /* orig-value */
2797 if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
2798 /* leaf */
2799 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)first, &dynamic);
2800 if (!dynamic) {
2801 *orig_value = strdup(*orig_value);
2802 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2803 }
2804 }
2805
2806 return LY_SUCCESS;
2807}
2808
2809/**
2810 * @brief Perform diff for all siblings at certain depth, recursively.
2811 *
2812 * For user-ordered lists/leaf-lists a specific structure is used for storing
2813 * the current order. The idea is to apply all the generated diff changes
2814 * virtually on the first tree so that we can continue to generate correct
2815 * changes after some were already generated.
2816 *
2817 * The algorithm then uses second tree position-based changes with a before
2818 * (preceding) item anchor.
2819 *
2820 * Example:
2821 *
2822 * Virtual first tree leaf-list order:
2823 * 1 2 [3] 4 5
2824 *
2825 * Second tree leaf-list order:
2826 * 1 2 [5] 3 4
2827 *
2828 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
2829 * match - they do not - move nodes so that the 3rd position node is final ->
2830 * -> move node 5 to the 3rd position -> move node 5 after node 2.
2831 *
2832 * Required properties:
2833 * Stored operations (move) should not be affected by later operations -
2834 * - would cause a redundantly long list of operations, possibly inifinite.
2835 *
2836 * Implemenation justification:
2837 * First, all delete operations and only then move/create operations are stored.
2838 * Also, preceding anchor is used and after each iteration another node is
2839 * at its final position. That results in the invariant that all preceding
2840 * nodes are final and will not be changed by the later operations, meaning
2841 * they can safely be used as anchors for the later operations.
2842 *
2843 * @param[in] first First tree first sibling.
2844 * @param[in] second Second tree first sibling.
2845 * @param[in] options Diff options.
2846 * @param[in,out] diff Diff to append to.
2847 * @return LY_ERR value.
2848 */
2849static LY_ERR
2850lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
2851{
2852 LY_ERR ret = LY_SUCCESS;
2853 const struct lyd_node *iter_first, *iter_second;
2854 struct lyd_node *match_second, *match_first;
2855 int nosiblings = 0;
2856 struct lyd_diff_userord *userord = NULL;
2857 LY_ARRAY_SIZE_TYPE u;
2858 enum lyd_diff_op op;
2859 const char *orig_default;
2860 char *orig_value, *key, *value, *orig_key;
2861
2862 if (options & LYD_DIFF_NOSIBLINGS) {
2863 /* remember it for this function call only, should not be passed recursively */
2864 nosiblings = 1;
2865 options &= ~LYD_DIFF_NOSIBLINGS;
2866 }
2867
2868 /* compare first tree to the second tree - delete, replace, none */
2869 LY_LIST_FOR(first, iter_first) {
2870 assert(!(iter_first->schema->flags & LYS_KEY));
2871 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2872 /* skip default nodes */
2873 continue;
2874 }
2875
2876 if (iter_first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2877 /* try to find the exact instance */
2878 lyd_find_sibling_first(second, iter_first, &match_second);
2879 } else {
2880 /* try to simply find the node, there cannot be more instances */
2881 lyd_find_sibling_val(second, iter_first->schema, NULL, 0, &match_second);
2882 }
2883
2884 if (match_second && (match_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2885 /* ignore default nodes */
2886 match_second = NULL;
2887 }
2888
2889 if (lysc_is_userordered(iter_first->schema)) {
2890 if (match_second) {
2891 /* we are handling only user-ordered node delete now */
2892 continue;
2893 }
2894
2895 /* get all the attributes */
2896 LY_CHECK_GOTO(lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
2897 &value, &orig_value, &key, &orig_key), cleanup);
2898
2899 /* there must be changes, it is deleted */
2900 assert(op == LYD_DIFF_OP_DELETE);
2901 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, orig_key, diff);
2902
2903 free(orig_value);
2904 free(key);
2905 free(value);
2906 free(orig_key);
2907 LY_CHECK_GOTO(ret, cleanup);
2908 } else {
2909 /* get all the attributes */
2910 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
2911
2912 /* add into diff if there are any changes */
2913 if (!ret) {
2914 if (op == LYD_DIFF_OP_DELETE) {
2915 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2916 } else {
2917 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2918 }
2919
2920 free(orig_value);
2921 LY_CHECK_GOTO(ret, cleanup);
2922 } else if (ret == LY_ENOT) {
2923 ret = LY_SUCCESS;
2924 } else {
2925 goto cleanup;
2926 }
2927 }
2928
2929 /* check descendants, if any, recursively */
2930 if (match_second) {
2931 LY_CHECK_GOTO(lyd_diff_siblings_r(LYD_CHILD(iter_first), LYD_CHILD(match_second), options, diff), cleanup);
2932 }
2933
2934 if (nosiblings) {
2935 break;
2936 }
2937 }
2938
2939 /* reset all cached positions */
2940 LY_ARRAY_FOR(userord, u) {
2941 userord[u].pos = 0;
2942 }
2943
2944 /* compare second tree to the first tree - create, user-ordered move */
2945 LY_LIST_FOR(second, iter_second) {
2946 assert(!(iter_second->schema->flags & LYS_KEY));
2947 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2948 /* skip default nodes */
2949 continue;
2950 }
2951
2952 if (iter_second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2953 lyd_find_sibling_first(first, iter_second, &match_first);
2954 } else {
2955 lyd_find_sibling_val(first, iter_second->schema, NULL, 0, &match_first);
2956 }
2957
2958 if (match_first && (match_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2959 /* ignore default nodes */
2960 match_first = NULL;
2961 }
2962
2963 if (lysc_is_userordered(iter_second->schema)) {
2964 /* get all the attributes */
2965 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
2966 &value, &orig_value, &key, &orig_key);
2967
2968 /* add into diff if there are any changes */
2969 if (!ret) {
2970 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, orig_key, diff);
2971
2972 free(orig_value);
2973 free(key);
2974 free(value);
2975 free(orig_key);
2976 LY_CHECK_GOTO(ret, cleanup);
2977 } else if (ret == LY_ENOT) {
2978 ret = LY_SUCCESS;
2979 } else {
2980 goto cleanup;
2981 }
2982 } else if (!match_first) {
2983 /* get all the attributes */
2984 LY_CHECK_GOTO(lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
2985
2986 /* there must be changes, it is created */
2987 assert(op == LYD_DIFF_OP_CREATE);
2988 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
2989
2990 free(orig_value);
2991 LY_CHECK_GOTO(ret, cleanup);
2992 } /* else was handled */
2993
2994 if (nosiblings) {
2995 break;
2996 }
2997 }
2998
2999cleanup:
3000 LY_ARRAY_FOR(userord, u) {
3001 LY_ARRAY_FREE(userord[u].inst);
3002 }
3003 LY_ARRAY_FREE(userord);
3004 return ret;
3005}
3006
3007API LY_ERR
3008lyd_diff(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
3009{
3010 const struct ly_ctx *ctx;
3011
3012 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
3013
3014 if (first) {
3015 ctx = LYD_NODE_CTX(first);
3016 } else if (second) {
3017 ctx = LYD_NODE_CTX(second);
3018 } else {
3019 ctx = NULL;
3020 }
3021
3022 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
3023 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
3024 return LY_EINVAL;
3025 }
3026
3027 *diff = NULL;
3028
3029 return lyd_diff_siblings_r(first, second, options, diff);
3030}
3031
3032/**
3033 * @brief Find a matching node in data tree for a diff node.
3034 *
3035 * @param[in] first_node First sibling in the data tree.
3036 * @param[in] diff_node Diff node to match.
3037 * @param[out] match_p Matching node.
3038 * @return LY_ERR value.
3039 */
3040static LY_ERR
3041lyd_diff_find_node(const struct lyd_node *first_node, const struct lyd_node *diff_node, struct lyd_node **match_p)
3042{
3043 if (diff_node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3044 /* try to find the exact instance */
3045 lyd_find_sibling_first(first_node, diff_node, match_p);
3046 } else {
3047 /* try to simply find the node, there cannot be more instances */
3048 lyd_find_sibling_val(first_node, diff_node->schema, NULL, 0, match_p);
3049 }
3050 LY_CHECK_ERR_RET(!*match_p, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3051
3052 return LY_SUCCESS;
3053}
3054
3055/**
3056 * @brief Learn operation of a diff node.
3057 *
3058 * @param[in] diff_node Diff node.
3059 * @param[out] op Operation.
3060 * @param[out] key_or_value Optional list instance keys predicate or leaf-list value for move operation.
3061 * @return LY_ERR value.
3062 */
3063static LY_ERR
3064lyd_diff_get_op(const struct lyd_node *diff_node, const char **op, const char **key_or_value)
3065{
3066 struct lyd_meta *meta = NULL;
3067 const struct lyd_node *diff_parent;
3068 const char *meta_name, *str;
3069 int dynamic;
3070
3071 for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
3072 LY_LIST_FOR(diff_parent->meta, meta) {
3073 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
3074 str = lyd_meta2str(meta, &dynamic);
3075 assert(!dynamic);
3076 if ((str[0] == 'r') && (diff_parent != diff_node)) {
3077 /* we do not care about this operation if it's in our parent */
3078 continue;
3079 }
3080 *op = str;
3081 break;
3082 }
3083 }
3084 if (meta) {
3085 break;
3086 }
3087 }
3088 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3089
3090 *key_or_value = NULL;
3091 if (lysc_is_userordered(diff_node->schema) && (((*op)[0] == 'c') || ((*op)[0] == 'r'))) {
3092 if (diff_node->schema->nodetype == LYS_LIST) {
3093 meta_name = "key";
3094 } else {
3095 meta_name = "value";
3096 }
3097
3098 LY_LIST_FOR(diff_node->meta, meta) {
3099 if (!strcmp(meta->name, meta_name) && !strcmp(meta->annotation->module->name, "yang")) {
3100 str = lyd_meta2str(meta, &dynamic);
3101 assert(!dynamic);
3102 *key_or_value = str;
3103 break;
3104 }
3105 }
3106 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3107 }
3108
3109 return LY_SUCCESS;
3110}
3111
3112/**
3113 * @brief Insert a diff node into a data tree.
3114 *
3115 * @param[in,out] first_node First sibling of the data tree.
3116 * @param[in] parent_node Data tree sibling parent node.
3117 * @param[in] new_node Node to insert.
3118 * @param[in] keys_or_value Optional predicate of relative (leaf-)list instance. If not set, the user-ordered
3119 * instance will be inserted at the first position.
3120 * @return err_info, NULL on success.
3121 */
3122static LY_ERR
3123lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
3124 const char *key_or_value)
3125{
3126 LY_ERR ret;
3127 struct lyd_node *anchor;
3128
3129 assert(new_node);
3130
3131 if (!*first_node) {
3132 if (!parent_node) {
3133 /* no parent or siblings */
3134 *first_node = new_node;
3135 return LY_SUCCESS;
3136 }
3137
3138 /* simply insert into parent, no other children */
3139 if (key_or_value) {
3140 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3141 new_node->schema->name);
3142 return LY_EINVAL;
3143 }
3144 return lyd_insert(parent_node, new_node);
3145 }
3146
3147 assert(!(*first_node)->parent || ((struct lyd_node *)(*first_node)->parent == parent_node));
3148
3149 /* simple insert */
3150 if (!lysc_is_userordered(new_node->schema)) {
3151 /* insert at the end */
3152 return lyd_insert_sibling(*first_node, new_node);
3153 }
3154
3155 if (key_or_value) {
3156 /* find the anchor sibling */
3157 ret = lyd_find_sibling_val(*first_node, new_node->schema, key_or_value, 0, &anchor);
3158 if (ret == LY_ENOTFOUND) {
3159 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3160 new_node->schema->name);
3161 return LY_EINVAL;
3162 } else if (ret) {
3163 return ret;
3164 }
3165
3166 /* insert after */
3167 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3168 assert(new_node->prev == anchor);
3169 if (*first_node == new_node) {
3170 *first_node = anchor;
3171 }
3172 } else {
3173 if ((*first_node)->schema->flags & LYS_KEY) {
3174 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
3175
3176 /* find last key */
3177 anchor = *first_node;
3178 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
3179 anchor = anchor->next;
3180 }
3181 /* insert after the last key */
3182 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3183 } else {
3184 /* insert at the beginning */
3185 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
3186 *first_node = new_node;
3187 }
3188 }
3189
3190 return LY_SUCCESS;
3191}
3192
3193/**
3194 * @brief Apply diff subtree on data tree nodes, recursively.
3195 *
3196 * @param[in,out] first_node First sibling of the data tree.
3197 * @param[in] parent_node Parent of the first sibling.
3198 * @param[in] diff_node Current diff node.
3199 * @return LY_ERR value.
3200 */
3201static LY_ERR
3202lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
3203 lyd_diff_cb diff_cb, void *cb_data)
3204{
3205 LY_ERR ret;
3206 struct lyd_node *match, *diff_child;
3207 const char *op, *key_or_value, *str_val;
3208 int dynamic;
3209 const struct ly_ctx *ctx = LYD_NODE_CTX(diff_node);
3210
3211 /* read all the valid attributes */
3212 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op, &key_or_value));
3213
3214 /* handle user-ordered (leaf-)lists separately */
3215 if (key_or_value) {
3216 assert((op[0] == 'c') || (op[0] == 'r'));
3217 if (op[0] == 'r') {
3218 /* find the node (we must have some siblings because the node was only moved) */
3219 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3220 } else {
3221 /* duplicate the node(s) */
3222 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3223 LY_CHECK_RET(!match, LY_EMEM);
3224 }
3225
3226 /* insert/move the node */
3227 if (key_or_value[0]) {
3228 ret = lyd_diff_insert(first_node, parent_node, match, key_or_value);
3229 } else {
3230 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
3231 }
3232 if (ret) {
3233 if (op[0] == 'c') {
3234 lyd_free_tree(match);
3235 }
3236 return ret;
3237 }
3238
3239 goto next_iter_r;
3240 }
3241
3242 /* apply operation */
3243 switch (op[0]) {
3244 case 'n':
3245 /* find the node */
3246 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3247
3248 if (match->schema->nodetype & LYD_NODE_TERM) {
3249 /* special case of only dflt flag change */
3250 if (diff_node->flags & LYD_DEFAULT) {
3251 match->flags |= LYD_DEFAULT;
3252 } else {
3253 match->flags &= ~LYD_DEFAULT;
3254 }
3255 } else {
3256 /* none operation on nodes without children is redundant and hence forbidden */
3257 if (!LYD_CHILD(diff_node)) {
3258 LOGINT_RET(ctx);
3259 }
3260 }
3261 break;
3262 case 'c':
3263 /* duplicate the node */
3264 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3265 LY_CHECK_RET(!match, LY_EMEM);
3266
3267 /* insert it at the end */
3268 ret = 0;
3269 if (*first_node) {
3270 ret = lyd_insert_after((*first_node)->prev, match);
3271 } else if (parent_node) {
3272 ret = lyd_insert(parent_node, match);
3273 } else {
3274 *first_node = match;
3275 }
3276 if (ret) {
3277 lyd_free_tree(match);
3278 return ret;
3279 }
3280
3281 break;
3282 case 'd':
3283 /* find the node */
3284 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3285
3286 /* remove it */
3287 if ((match == *first_node) && !match->parent) {
3288 assert(!parent_node);
3289 /* we have removed the top-level node */
3290 *first_node = (*first_node)->next;
3291 }
3292 lyd_free_tree(match);
3293
3294 /* we are not going recursively in this case, the whole subtree was already deleted */
3295 return LY_SUCCESS;
3296 case 'r':
3297 LY_CHECK_ERR_RET(diff_node->schema->nodetype != LYS_LEAF, LOGINT(ctx), LY_EINT);
3298
3299 /* find the node */
3300 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3301
3302 /* update its value */
3303 str_val = lyd_value2str((struct lyd_node_term *)diff_node, &dynamic);
3304 ret = lyd_change_term(match, str_val);
3305 if (dynamic) {
3306 free((char *)str_val);
3307 }
3308 if (ret && (ret != LY_EEXIST)) {
3309 LOGINT_RET(ctx);
3310 }
3311
3312 /* with flags */
3313 match->flags = diff_node->flags;
3314 break;
3315 default:
3316 LOGINT_RET(ctx);
3317 }
3318
3319next_iter_r:
3320 if (diff_cb) {
3321 /* call callback */
3322 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
3323 }
3324
3325 /* apply diff recursively */
3326 LY_LIST_FOR(LYD_CHILD(diff_node), diff_child) {
3327 LY_CHECK_RET(lyd_diff_apply_r(lyd_node_children_p(match), match, diff_child, diff_cb, cb_data));
3328 }
3329
3330 return LY_SUCCESS;
3331}
3332
3333API LY_ERR
3334lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
3335 lyd_diff_cb diff_cb, void *cb_data)
3336{
3337 const struct lyd_node *root;
3338
3339 LY_LIST_FOR(diff, root) {
3340 if (mod && (lyd_owner_module(root) != mod)) {
3341 /* skip data nodes from different modules */
3342 continue;
3343 }
3344
3345 /* apply relevant nodes from the diff datatree */
3346 LY_CHECK_RET(lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data));
3347 }
3348
3349 return LY_SUCCESS;
3350}
3351
3352API LY_ERR
3353lyd_diff_apply(struct lyd_node **data, const struct lyd_node *diff)
3354{
3355 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
3356}
3357
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003358static LY_ERR
3359lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
3360{
Michal Vasko14654712020-02-06 08:35:21 +01003361 /* ending \0 */
3362 ++reqlen;
3363
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003364 if (reqlen > *buflen) {
3365 if (is_static) {
3366 return LY_EINCOMPLETE;
3367 }
3368
3369 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3370 if (!*buffer) {
3371 return LY_EMEM;
3372 }
3373
3374 *buflen = reqlen;
3375 }
3376
3377 return LY_SUCCESS;
3378}
3379
3380/**
3381 * @brief Append all list key predicates to path.
3382 *
3383 * @param[in] node Node with keys to print.
3384 * @param[in,out] buffer Buffer to print to.
3385 * @param[in,out] buflen Current buffer length.
3386 * @param[in,out] bufused Current number of characters used in @p buffer.
3387 * @param[in] is_static Whether buffer is static or can be reallocated.
3388 * @return LY_ERR
3389 */
3390static LY_ERR
3391lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3392{
3393 const struct lyd_node *key;
3394 int dynamic = 0;
3395 size_t len;
3396 const char *val;
3397 char quot;
3398 LY_ERR rc;
3399
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003400 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003401 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
3402 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
3403 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3404 if (rc != LY_SUCCESS) {
3405 if (dynamic) {
3406 free((char *)val);
3407 }
3408 return rc;
3409 }
3410
3411 quot = '\'';
3412 if (strchr(val, '\'')) {
3413 quot = '"';
3414 }
3415 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
3416
3417 if (dynamic) {
3418 free((char *)val);
3419 }
3420 }
3421
3422 return LY_SUCCESS;
3423}
3424
3425/**
3426 * @brief Append leaf-list value predicate to path.
3427 *
3428 * @param[in] node Node to print.
3429 * @param[in,out] buffer Buffer to print to.
3430 * @param[in,out] buflen Current buffer length.
3431 * @param[in,out] bufused Current number of characters used in @p buffer.
3432 * @param[in] is_static Whether buffer is static or can be reallocated.
3433 * @return LY_ERR
3434 */
3435static LY_ERR
3436lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3437{
3438 int dynamic = 0;
3439 size_t len;
3440 const char *val;
3441 char quot;
3442 LY_ERR rc;
3443
3444 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
3445 len = 4 + strlen(val) + 2;
3446 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3447 if (rc != LY_SUCCESS) {
3448 goto cleanup;
3449 }
3450
3451 quot = '\'';
3452 if (strchr(val, '\'')) {
3453 quot = '"';
3454 }
3455 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3456
3457cleanup:
3458 if (dynamic) {
3459 free((char *)val);
3460 }
3461 return rc;
3462}
3463
3464/**
3465 * @brief Append node position (relative to its other instances) predicate to path.
3466 *
3467 * @param[in] node Node to print.
3468 * @param[in,out] buffer Buffer to print to.
3469 * @param[in,out] buflen Current buffer length.
3470 * @param[in,out] bufused Current number of characters used in @p buffer.
3471 * @param[in] is_static Whether buffer is static or can be reallocated.
3472 * @return LY_ERR
3473 */
3474static LY_ERR
3475lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3476{
3477 const struct lyd_node *first, *iter;
3478 size_t len;
3479 int pos;
3480 char *val = NULL;
3481 LY_ERR rc;
3482
3483 if (node->parent) {
3484 first = node->parent->child;
3485 } else {
3486 for (first = node; node->prev->next; node = node->prev);
3487 }
3488 pos = 1;
3489 for (iter = first; iter != node; iter = iter->next) {
3490 if (iter->schema == node->schema) {
3491 ++pos;
3492 }
3493 }
3494 if (asprintf(&val, "%d", pos) == -1) {
3495 return LY_EMEM;
3496 }
3497
3498 len = 1 + strlen(val) + 1;
3499 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3500 if (rc != LY_SUCCESS) {
3501 goto cleanup;
3502 }
3503
3504 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3505
3506cleanup:
3507 free(val);
3508 return rc;
3509}
3510
3511API char *
3512lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3513{
Michal Vasko14654712020-02-06 08:35:21 +01003514 int is_static = 0, i, depth;
3515 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003516 const struct lyd_node *iter;
3517 const struct lys_module *mod;
3518 LY_ERR rc;
3519
3520 LY_CHECK_ARG_RET(NULL, node, NULL);
3521 if (buffer) {
3522 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3523 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003524 } else {
3525 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003526 }
3527
3528 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003529 case LYD_PATH_LOG:
3530 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003531 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3532 ++depth;
3533 }
3534
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003535 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003536 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003537 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003538 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003539iter_print:
3540 /* print prefix and name */
3541 mod = NULL;
3542 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
3543 mod = iter->schema->module;
3544 }
3545
3546 /* realloc string */
3547 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
3548 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3549 if (rc != LY_SUCCESS) {
3550 break;
3551 }
3552
3553 /* print next node */
3554 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
3555
3556 switch (iter->schema->nodetype) {
3557 case LYS_LIST:
3558 if (iter->schema->flags & LYS_KEYLESS) {
3559 /* print its position */
3560 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3561 } else {
3562 /* print all list keys in predicates */
3563 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3564 }
3565 break;
3566 case LYS_LEAFLIST:
3567 if (iter->schema->flags & LYS_CONFIG_W) {
3568 /* print leaf-list value */
3569 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3570 } else {
3571 /* print its position */
3572 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3573 }
3574 break;
3575 default:
3576 /* nothing to print more */
3577 rc = LY_SUCCESS;
3578 break;
3579 }
3580 if (rc != LY_SUCCESS) {
3581 break;
3582 }
3583
Michal Vasko14654712020-02-06 08:35:21 +01003584 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003585 }
3586 break;
3587 }
3588
3589 return buffer;
3590}
Michal Vaskoe444f752020-02-10 12:20:06 +01003591
Michal Vasko9b368d32020-02-14 13:53:31 +01003592LY_ERR
3593lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
3594 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003595{
3596 LY_ERR rc;
3597 const struct lyd_node *node = NULL;
3598 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02003599 struct lyxp_expr *expr = NULL;
3600 uint16_t exp_idx = 0;
3601 struct ly_path_predicate *predicates = NULL;
3602 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01003603 struct lyd_value val = {0};
Michal Vasko00cbf532020-06-15 13:58:47 +02003604 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01003605
Michal Vasko9b368d32020-02-14 13:53:31 +01003606 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003607
3608 if (!first) {
3609 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01003610 if (match) {
3611 *match = NULL;
3612 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003613 return LY_ENOTFOUND;
3614 }
3615
Michal Vaskoe444f752020-02-10 12:20:06 +01003616 if (key_or_value && !val_len) {
3617 val_len = strlen(key_or_value);
3618 }
3619
3620 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01003621 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01003622 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 +01003623 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003624 /* parse keys */
3625 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
3626 LY_PATH_PRED_KEYS, &expr), cleanup);
3627
3628 /* compile them */
3629 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
3630 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003631 }
3632
3633 /* find first matching value */
3634 LY_LIST_FOR(first, node) {
3635 if (node->schema != schema) {
3636 continue;
3637 }
3638
Michal Vasko00cbf532020-06-15 13:58:47 +02003639 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003640 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02003641 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003642 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003643 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 +01003644 if (rc == LY_ENOTFOUND) {
3645 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01003646 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01003647 }
3648 LY_CHECK_GOTO(rc, cleanup);
3649
3650 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02003651 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003652 break;
3653 }
3654 }
3655
Michal Vasko00cbf532020-06-15 13:58:47 +02003656 if (u < LY_ARRAY_SIZE(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003657 /* not a match */
3658 continue;
3659 }
Michal Vasko90932a92020-02-12 14:33:03 +01003660 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003661 term = (struct lyd_node_term *)node;
3662
3663 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01003664 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003665 /* not a match */
3666 continue;
3667 }
3668 }
3669
3670 /* all criteria passed */
3671 break;
3672 }
3673
3674 if (!node) {
3675 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01003676 if (match) {
3677 *match = NULL;
3678 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003679 goto cleanup;
3680 }
3681
3682 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01003683 if (match) {
3684 *match = (struct lyd_node *)node;
3685 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003686 rc = LY_SUCCESS;
3687
3688cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02003689 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
3690 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01003691 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003692 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01003693 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003694 return rc;
3695}
3696
3697API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01003698lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
3699 const char *key_or_value, size_t val_len, struct lyd_node **match)
3700{
3701 const struct lysc_node *schema;
3702
3703 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
3704
3705 if (!first) {
3706 /* no data */
3707 *match = NULL;
3708 return LY_ENOTFOUND;
3709 }
3710
3711 /* find schema */
3712 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
3713 if (!schema) {
3714 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
3715 return LY_EINVAL;
3716 }
3717
3718 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
3719}
3720
3721API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003722lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3723{
3724 struct lyd_node **match_p;
3725 struct lyd_node_inner *parent;
3726
Michal Vaskof03ed032020-03-04 13:31:44 +01003727 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003728
Michal Vasko62ed12d2020-05-21 10:08:25 +02003729 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3730 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003731 if (match) {
3732 *match = NULL;
3733 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003734 return LY_ENOTFOUND;
3735 }
3736
3737 /* find first sibling */
3738 if (siblings->parent) {
3739 siblings = siblings->parent->child;
3740 } else {
3741 while (siblings->prev->next) {
3742 siblings = siblings->prev;
3743 }
3744 }
3745
3746 parent = (struct lyd_node_inner *)siblings->parent;
3747 if (parent && parent->children_ht) {
3748 assert(target->hash);
3749
3750 /* find by hash */
3751 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3752 siblings = *match_p;
3753 } else {
3754 /* not found */
3755 siblings = NULL;
3756 }
3757 } else {
3758 /* no children hash table */
3759 for (; siblings; siblings = siblings->next) {
3760 if (!lyd_compare(siblings, target, 0)) {
3761 break;
3762 }
3763 }
3764 }
3765
3766 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003767 if (match) {
3768 *match = NULL;
3769 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003770 return LY_ENOTFOUND;
3771 }
3772
Michal Vasko9b368d32020-02-14 13:53:31 +01003773 if (match) {
3774 *match = (struct lyd_node *)siblings;
3775 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003776 return LY_SUCCESS;
3777}
3778
3779API LY_ERR
3780lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3781{
3782 struct lyd_node_inner *parent;
3783 struct lyd_node *match;
3784 struct lyd_node **match_p;
3785 struct ly_set *ret;
3786
3787 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3788
Michal Vasko62ed12d2020-05-21 10:08:25 +02003789 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3790 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003791 return LY_ENOTFOUND;
3792 }
3793
3794 ret = ly_set_new();
3795 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3796
3797 /* find first sibling */
3798 if (siblings->parent) {
3799 siblings = siblings->parent->child;
3800 } else {
3801 while (siblings->prev->next) {
3802 siblings = siblings->prev;
3803 }
3804 }
3805
3806 parent = (struct lyd_node_inner *)siblings->parent;
3807 if (parent && parent->children_ht) {
3808 assert(target->hash);
3809
3810 /* find by hash */
3811 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3812 match = *match_p;
3813 } else {
3814 /* not found */
3815 match = NULL;
3816 }
3817 while (match) {
3818 /* add all found nodes into the return set */
3819 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3820 goto error;
3821 }
3822
3823 /* find next instance */
3824 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3825 match = NULL;
3826 } else {
3827 match = *match_p;
3828 }
3829 }
3830 } else {
3831 /* no children hash table */
3832 for (; siblings; siblings = siblings->next) {
3833 if (!lyd_compare(siblings, target, 0)) {
3834 /* a match */
3835 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3836 goto error;
3837 }
3838 }
3839 }
3840 }
3841
3842 if (!ret->count) {
3843 ly_set_free(ret, NULL);
3844 return LY_ENOTFOUND;
3845 }
3846
3847 *set = ret;
3848 return LY_SUCCESS;
3849
3850error:
3851 ly_set_free(ret, NULL);
3852 return LY_EMEM;
3853}
3854
Michal Vasko90932a92020-02-12 14:33:03 +01003855static int
3856lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3857{
3858 struct lysc_node *val1;
3859 struct lyd_node *val2;
3860
3861 val1 = *((struct lysc_node **)val1_p);
3862 val2 = *((struct lyd_node **)val2_p);
3863
3864 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
3865
3866 if (val1 == val2->schema) {
3867 /* schema match is enough */
3868 return 1;
3869 } else {
3870 return 0;
3871 }
3872}
3873
3874static LY_ERR
3875lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3876{
3877 struct lyd_node **match_p;
3878 struct lyd_node_inner *parent;
3879 uint32_t hash;
3880 values_equal_cb ht_cb;
3881
3882 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3883
3884 /* find first sibling */
3885 if (siblings->parent) {
3886 siblings = siblings->parent->child;
3887 } else {
3888 while (siblings->prev->next) {
3889 siblings = siblings->prev;
3890 }
3891 }
3892
3893 parent = (struct lyd_node_inner *)siblings->parent;
3894 if (parent && parent->children_ht) {
3895 /* calculate our hash */
3896 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3897 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3898 hash = dict_hash_multi(hash, NULL, 0);
3899
3900 /* use special hash table function */
3901 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3902
3903 /* find by hash */
3904 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3905 siblings = *match_p;
3906 } else {
3907 /* not found */
3908 siblings = NULL;
3909 }
3910
3911 /* set the original hash table compare function back */
3912 lyht_set_cb(parent->children_ht, ht_cb);
3913 } else {
3914 /* no children hash table */
3915 for (; siblings; siblings = siblings->next) {
3916 if (siblings->schema == schema) {
3917 /* schema match is enough */
3918 break;
3919 }
3920 }
3921 }
3922
3923 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003924 if (match) {
3925 *match = NULL;
3926 }
Michal Vasko90932a92020-02-12 14:33:03 +01003927 return LY_ENOTFOUND;
3928 }
3929
Michal Vasko9b368d32020-02-14 13:53:31 +01003930 if (match) {
3931 *match = (struct lyd_node *)siblings;
3932 }
Michal Vasko90932a92020-02-12 14:33:03 +01003933 return LY_SUCCESS;
3934}
3935
Michal Vaskoe444f752020-02-10 12:20:06 +01003936API LY_ERR
3937lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3938 size_t val_len, struct lyd_node **match)
3939{
3940 LY_ERR rc;
3941 struct lyd_node *target = NULL;
3942
3943 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003944 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003945 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3946 return LY_EINVAL;
3947 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
3948 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
3949 return LY_EINVAL;
3950 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3951 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3952 lys_nodetype2str(schema->nodetype), __func__);
3953 return LY_EINVAL;
3954 }
3955
Michal Vasko62ed12d2020-05-21 10:08:25 +02003956 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3957 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003958 if (match) {
3959 *match = NULL;
3960 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003961 return LY_ENOTFOUND;
3962 }
3963
Michal Vaskof03ed032020-03-04 13:31:44 +01003964 if (key_or_value && !val_len) {
3965 val_len = strlen(key_or_value);
3966 }
3967
Michal Vasko90932a92020-02-12 14:33:03 +01003968 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01003969 switch (schema->nodetype) {
3970 case LYS_CONTAINER:
3971 case LYS_ANYXML:
3972 case LYS_ANYDATA:
3973 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01003974 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01003975 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01003976 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02003977 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01003978 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003979 break;
3980 case LYS_LEAFLIST:
3981 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02003982 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
3983 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3984 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01003985 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01003986 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01003987 if (schema->nodetype == LYS_LIST) {
3988 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003989 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003990 }
3991
3992 /* find it */
3993 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003994 break;
3995 default:
3996 /* unreachable */
3997 LOGINT(schema->module->ctx);
3998 return LY_EINT;
3999 }
4000
Michal Vaskoe444f752020-02-10 12:20:06 +01004001 lyd_free_tree(target);
4002 return rc;
4003}
Michal Vaskoccc02342020-05-21 10:09:21 +02004004
4005API LY_ERR
4006lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4007{
4008 LY_ERR ret = LY_SUCCESS;
4009 struct lyxp_set xp_set;
4010 struct lyxp_expr *exp;
4011 uint32_t i;
4012
4013 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4014
4015 memset(&xp_set, 0, sizeof xp_set);
4016
4017 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02004018 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02004019 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
4020
4021 /* evaluate expression */
4022 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
4023 LY_CHECK_GOTO(ret, cleanup);
4024
4025 /* allocate return set */
4026 *set = ly_set_new();
4027 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4028
4029 /* transform into ly_set */
4030 if (xp_set.type == LYXP_SET_NODE_SET) {
4031 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4032 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
4033 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4034 (*set)->size = xp_set.used;
4035
4036 for (i = 0; i < xp_set.used; ++i) {
4037 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
4038 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
4039 }
4040 }
4041 }
4042
4043cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004044 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02004045 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
4046 return ret;
4047}