blob: 21b8573921876ea96564069aca3ac1eb8876c355 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020041#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
49#include "xml.h"
50#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020051
Radek Krejci084289f2019-07-09 17:35:30 +020052LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010053lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010054 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020055{
Michal Vasko90932a92020-02-12 14:33:03 +010056 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020057 struct ly_err_item *err = NULL;
58 struct ly_ctx *ctx;
59 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020060 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010061 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020062 assert(node);
63
64 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020065
Radek Krejci73dead22019-07-11 16:46:16 +020066 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020067 if (!second) {
68 node->value.realtype = type;
69 }
Michal Vasko90932a92020-02-12 14:33:03 +010070 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020071 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010072 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020073 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020074 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020075 if (!node->parent && lysc_data_parent(node->schema)) {
76 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
77 } else {
78 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
79 }
Radek Krejci73dead22019-07-11 16:46:16 +020080 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020081 }
Radek Krejci73dead22019-07-11 16:46:16 +020082 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010083 } else if (dynamic) {
84 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020085 }
86
87error:
88 return ret;
89}
90
Michal Vasko00cbf532020-06-15 13:58:47 +020091/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020092LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010093lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
94 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
95{
96 LY_ERR ret = LY_SUCCESS;
97 struct ly_err_item *err = NULL;
98 struct ly_ctx *ctx;
99 struct lysc_type *type;
100 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
101
102 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
103
104 ctx = schema->module->ctx;
105 type = ((struct lysc_node_leaf *)schema)->type;
106 val->realtype = type;
107 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
108 val, NULL, &err);
109 if (ret == LY_EINCOMPLETE) {
110 /* this is fine, we do not need it resolved */
111 ret = LY_SUCCESS;
112 } else if (ret && err) {
113 ly_err_print(err);
114 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
115 ly_err_free(err);
116 }
117 if (!ret && dynamic) {
118 *dynamic = 0;
119 }
120
121 return ret;
122}
123
Radek Krejci38d85362019-09-05 16:26:38 +0200124LY_ERR
Michal 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 */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200257 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200258 }
Radek Krejci084289f2019-07-09 17:35:30 +0200259
260cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200261 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200262
263 return ret;
264}
265
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200266API const char *
267lyd_value2str(const struct lyd_node_term *node, int *dynamic)
268{
269 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
270
271 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
272}
273
274API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100275lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200276{
Michal Vasko9f96a052020-03-10 09:41:45 +0100277 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200278
Michal Vasko9f96a052020-03-10 09:41:45 +0100279 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200280}
281
Radek Krejci7931b192020-06-25 17:05:03 +0200282static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200283lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200284{
Radek Krejcie7b95092019-05-15 11:03:07 +0200285
Radek Krejci7931b192020-06-25 17:05:03 +0200286 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200288 const char *path = in->method.fpath.filepath;
289 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200290
291 /* ignore trailing whitespaces */
292 for (; len > 0 && isspace(path[len - 1]); len--);
293
294 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
295 format = LYD_XML;
296#if 0
297 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
298 format = LYD_JSON;
Radek Krejci7931b192020-06-25 17:05:03 +0200299#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200300 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
301 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200302 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 }
304
Radek Krejci7931b192020-06-25 17:05:03 +0200305 return format;
306}
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
Radek Krejci7931b192020-06-25 17:05:03 +0200308API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200309lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
310 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200311{
312 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
313 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
314 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
315
316 format = lyd_parse_get_format(in, format);
317 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
318
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 /* remember input position */
320 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200321
322 switch (format) {
323 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200324 return lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200325#if 0
326 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 return lyd_parse_json_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200328#endif
329 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 return lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200331 case LYD_SCHEMA:
332 LOGINT_RET(ctx);
333 }
334
335 /* TODO move here the top-level validation from parser_xml.c's lyd_parse_xml_data() and make
336 * it common for all the lyd_parse_*_data() functions */
337
338 LOGINT_RET(ctx);
339}
340
341API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200342lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
343 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200344{
345 LY_ERR ret;
346 struct ly_in *in;
347
348 LY_CHECK_RET(ly_in_new_memory(data, &in));
349 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
350
351 ly_in_free(in, 0);
352 return ret;
353}
354
355API LY_ERR
356lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
357{
358 LY_ERR ret;
359 struct ly_in *in;
360
361 LY_CHECK_RET(ly_in_new_fd(fd, &in));
362 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
363
364 ly_in_free(in, 0);
365 return ret;
366}
367
368API LY_ERR
369lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
370{
371 LY_ERR ret;
372 struct ly_in *in;
373
374 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
375 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
376
377 ly_in_free(in, 0);
378 return ret;
379}
380
Radek Krejci7931b192020-06-25 17:05:03 +0200381API LY_ERR
382lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
383{
384 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
385
386 format = lyd_parse_get_format(in, format);
387 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
388
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 /* remember input position */
390 in->func_start = in->current;
391
Radek Krejci7931b192020-06-25 17:05:03 +0200392 switch (format) {
393 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200394 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200395#if 0
396 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200397 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200398#endif
399 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200401 case LYD_SCHEMA:
402 LOGINT_RET(ctx);
403 }
404
405 LOGINT_RET(ctx);
406}
407
408API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200409lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
410 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200411{
412 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
413 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
414
415 format = lyd_parse_get_format(in, format);
416 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
417
Michal Vasko63f3d842020-07-08 10:10:14 +0200418 /* remember input position */
419 in->func_start = in->current;
420
Radek Krejci7931b192020-06-25 17:05:03 +0200421 switch (format) {
422 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200424#if 0
425 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200427#endif
428 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200430 case LYD_SCHEMA:
431 LOGINT_RET(LYD_NODE_CTX(request));
432 }
433
434 LOGINT_RET(LYD_NODE_CTX(request));
435}
436
437API LY_ERR
438lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
441
442 format = lyd_parse_get_format(in, format);
443 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
444
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 /* remember input position */
446 in->func_start = in->current;
447
Radek Krejci7931b192020-06-25 17:05:03 +0200448 switch (format) {
449 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200451#if 0
452 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200454#endif
455 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200457 case LYD_SCHEMA:
458 LOGINT_RET(ctx);
459 }
460
461 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200462}
Radek Krejci084289f2019-07-09 17:35:30 +0200463
Michal Vasko90932a92020-02-12 14:33:03 +0100464LY_ERR
465lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
466 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
467{
468 LY_ERR ret;
469 struct lyd_node_term *term;
470
Michal Vasko9b368d32020-02-14 13:53:31 +0100471 assert(schema->nodetype & LYD_NODE_TERM);
472
Michal Vasko90932a92020-02-12 14:33:03 +0100473 term = calloc(1, sizeof *term);
474 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
475
476 term->schema = schema;
477 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100478 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100479
480 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
481 if (ret && (ret != LY_EINCOMPLETE)) {
482 free(term);
483 return ret;
484 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100485 lyd_hash((struct lyd_node *)term);
486
487 *node = (struct lyd_node *)term;
488 return ret;
489}
490
491LY_ERR
492lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
493{
494 LY_ERR ret;
495 struct lyd_node_term *term;
496 struct lysc_type *type;
497
498 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200499 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100500
501 term = calloc(1, sizeof *term);
502 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
503
504 term->schema = schema;
505 term->prev = (struct lyd_node *)term;
506 term->flags = LYD_NEW;
507
508 type = ((struct lysc_node_leaf *)schema)->type;
509 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
510 if (ret) {
511 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
512 free(term);
513 return ret;
514 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200515 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100516 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100517
518 *node = (struct lyd_node *)term;
519 return ret;
520}
521
522LY_ERR
523lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
524{
525 struct lyd_node_inner *in;
526
Michal Vasko9b368d32020-02-14 13:53:31 +0100527 assert(schema->nodetype & LYD_NODE_INNER);
528
Michal Vasko90932a92020-02-12 14:33:03 +0100529 in = calloc(1, sizeof *in);
530 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
531
532 in->schema = schema;
533 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100534 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100535
Michal Vasko9b368d32020-02-14 13:53:31 +0100536 /* do not hash list with keys, we need them for the hash */
537 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
538 lyd_hash((struct lyd_node *)in);
539 }
Michal Vasko90932a92020-02-12 14:33:03 +0100540
541 *node = (struct lyd_node *)in;
542 return LY_SUCCESS;
543}
544
Michal Vasko90932a92020-02-12 14:33:03 +0100545LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200546lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100547{
548 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100549 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100551
Michal Vasko004d3152020-06-11 19:59:22 +0200552 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100553
554 /* create list */
555 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
556
Michal Vasko90932a92020-02-12 14:33:03 +0100557 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200558 LY_ARRAY_FOR(predicates, u) {
559 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100560 lyd_insert_node(list, NULL, key);
561 }
562
Michal Vasko9b368d32020-02-14 13:53:31 +0100563 /* hash having all the keys */
564 lyd_hash(list);
565
Michal Vasko90932a92020-02-12 14:33:03 +0100566 /* success */
567 *node = list;
568 list = NULL;
569
570cleanup:
571 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200572 return ret;
573}
574
575static LY_ERR
576lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
577{
578 LY_ERR ret = LY_SUCCESS;
579 struct lyxp_expr *expr = NULL;
580 uint16_t exp_idx = 0;
581 enum ly_path_pred_type pred_type = 0;
582 struct ly_path_predicate *predicates = NULL;
583
584 /* parse keys */
585 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
586 LY_PATH_PRED_KEYS, &expr), cleanup);
587
588 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200589 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
590 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200591
592 /* create the list node */
593 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
594
595cleanup:
596 lyxp_expr_free(schema->module->ctx, expr);
597 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100598 return ret;
599}
600
601LY_ERR
602lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
603{
604 struct lyd_node_any *any;
605
Michal Vasko9b368d32020-02-14 13:53:31 +0100606 assert(schema->nodetype & LYD_NODE_ANY);
607
Michal Vasko90932a92020-02-12 14:33:03 +0100608 any = calloc(1, sizeof *any);
609 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
610
611 any->schema = schema;
612 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100613 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100614
615 any->value.xml = value;
616 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100617 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100618
619 *node = (struct lyd_node *)any;
620 return LY_SUCCESS;
621}
622
Michal Vasko52927e22020-03-16 17:26:14 +0100623LY_ERR
624lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
625 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
626 const char *ns, struct lyd_node **node)
627{
628 struct lyd_node_opaq *opaq;
629
630 assert(ctx && name && name_len && ns);
631
632 if (!value_len) {
633 value = "";
634 }
635
636 opaq = calloc(1, sizeof *opaq);
637 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
638
639 opaq->prev = (struct lyd_node *)opaq;
640
641 opaq->name = lydict_insert(ctx, name, name_len);
642 opaq->format = format;
643 if (pref_len) {
644 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
645 }
646 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
647 opaq->val_prefs = val_prefs;
648 if (dynamic && *dynamic) {
649 opaq->value = lydict_insert_zc(ctx, (char *)value);
650 *dynamic = 0;
651 } else {
652 opaq->value = lydict_insert(ctx, value, value_len);
653 }
654 opaq->ctx = ctx;
655
656 *node = (struct lyd_node *)opaq;
657 return LY_SUCCESS;
658}
659
Michal Vasko013a8182020-03-03 10:46:53 +0100660API struct lyd_node *
661lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
662{
663 struct lyd_node *ret = NULL;
664 const struct lysc_node *schema;
665 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
666
667 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
668
Michal Vaskof03ed032020-03-04 13:31:44 +0100669 if (!module) {
670 module = parent->schema->module;
671 }
672
Michal Vasko1bf09392020-03-27 12:38:10 +0100673 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 +0200674 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 +0100675
676 if (!lyd_create_inner(schema, &ret) && parent) {
677 lyd_insert_node(parent, NULL, ret);
678 }
679 return ret;
680}
681
682API struct lyd_node *
683lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
684{
685 struct lyd_node *ret = NULL, *key;
686 const struct lysc_node *schema, *key_s;
687 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
688 va_list ap;
689 const char *key_val;
690 LY_ERR rc = LY_SUCCESS;
691
692 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
693
Michal Vaskof03ed032020-03-04 13:31:44 +0100694 if (!module) {
695 module = parent->schema->module;
696 }
697
Michal Vasko013a8182020-03-03 10:46:53 +0100698 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
699 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
700
701 /* create list inner node */
702 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
703
704 va_start(ap, name);
705
706 /* create and insert all the keys */
707 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
708 key_val = va_arg(ap, const char *);
709
Michal Vaskof03ed032020-03-04 13:31:44 +0100710 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 +0200711 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
712 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100713 lyd_insert_node(ret, NULL, key);
714 }
715
716 /* hash having all the keys */
717 lyd_hash(ret);
718
719 if (parent) {
720 lyd_insert_node(parent, NULL, ret);
721 }
722
723cleanup:
724 if (rc) {
725 lyd_free_tree(ret);
726 ret = NULL;
727 }
728 va_end(ap);
729 return ret;
730}
731
732API struct lyd_node *
733lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
734{
735 struct lyd_node *ret = NULL;
736 const struct lysc_node *schema;
737 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
738
739 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
740
Michal Vaskof03ed032020-03-04 13:31:44 +0100741 if (!module) {
742 module = parent->schema->module;
743 }
Michal Vasko004d3152020-06-11 19:59:22 +0200744 if (!keys) {
745 keys = "";
746 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100747
Michal Vasko004d3152020-06-11 19:59:22 +0200748 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100749 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
750 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
751
Michal Vasko004d3152020-06-11 19:59:22 +0200752 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
753 /* key-less list */
754 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
755 } else {
756 /* create the list node */
757 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret), NULL);
758 }
759
760 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100761 lyd_insert_node(parent, NULL, ret);
762 }
763 return ret;
764}
765
766API struct lyd_node *
767lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
768{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200769 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100770 struct lyd_node *ret = NULL;
771 const struct lysc_node *schema;
772 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
773
774 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
775
Michal Vaskof03ed032020-03-04 13:31:44 +0100776 if (!module) {
777 module = parent->schema->module;
778 }
779
Michal Vasko013a8182020-03-03 10:46:53 +0100780 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
781 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
782
Michal Vaskocbff3e92020-05-27 12:56:41 +0200783 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
784 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), NULL);
785
786 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100787 lyd_insert_node(parent, NULL, ret);
788 }
789 return ret;
790}
791
792API struct lyd_node *
793lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
794 LYD_ANYDATA_VALUETYPE value_type)
795{
796 struct lyd_node *ret = NULL;
797 const struct lysc_node *schema;
798 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
799
800 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
801
Michal Vaskof03ed032020-03-04 13:31:44 +0100802 if (!module) {
803 module = parent->schema->module;
804 }
805
Michal Vasko013a8182020-03-03 10:46:53 +0100806 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
807 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
808
809 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
810 lyd_insert_node(parent, NULL, ret);
811 }
812 return ret;
813}
814
Michal Vasko4490d312020-06-16 13:08:55 +0200815/**
816 * @brief Update node value.
817 *
818 * @param[in] node Node to update.
819 * @param[in] value New value to set.
820 * @param[in] value_type Type of @p value for any node.
821 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
822 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
823 * @return LY_ERR value.
824 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200825static LY_ERR
826lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
827 struct lyd_node **new_parent, struct lyd_node **new_node)
828{
829 LY_ERR ret = LY_SUCCESS;
830 struct lyd_node *new_any;
831
832 switch (node->schema->nodetype) {
833 case LYS_CONTAINER:
834 case LYS_NOTIF:
835 case LYS_RPC:
836 case LYS_ACTION:
837 case LYS_LIST:
838 case LYS_LEAFLIST:
839 /* if it exists, there is nothing to update */
840 *new_parent = NULL;
841 *new_node = NULL;
842 break;
843 case LYS_LEAF:
844 ret = lyd_change_term(node, value);
845 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
846 /* there was an actual change (at least of the default flag) */
847 *new_parent = node;
848 *new_node = node;
849 ret = LY_SUCCESS;
850 } else if (ret == LY_ENOT) {
851 /* no change */
852 *new_parent = NULL;
853 *new_node = NULL;
854 ret = LY_SUCCESS;
855 } /* else error */
856 break;
857 case LYS_ANYDATA:
858 case LYS_ANYXML:
859 /* create a new any node */
860 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
861
862 /* compare with the existing one */
863 if (lyd_compare(node, new_any, 0)) {
864 /* not equal, switch values (so that we can use generic node free) */
865 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
866 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
867 ((struct lyd_node_any *)node)->value.str = value;
868 ((struct lyd_node_any *)node)->value_type = value_type;
869
870 *new_parent = node;
871 *new_node = node;
872 } else {
873 /* they are equal */
874 *new_parent = NULL;
875 *new_node = NULL;
876 }
877 lyd_free_tree(new_any);
878 break;
879 default:
880 LOGINT(LYD_NODE_CTX(node));
881 ret = LY_EINT;
882 break;
883 }
884
885 return ret;
886}
887
Michal Vaskod86997b2020-05-26 15:19:54 +0200888API struct lyd_meta *
889lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
890{
891 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200892 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200893 const char *prefix, *tmp;
894 char *str;
895 size_t pref_len, name_len;
896
Michal Vasko00cbf532020-06-15 13:58:47 +0200897 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), NULL);
898
899 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200900
901 /* parse the name */
902 tmp = name;
903 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
904 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
905 return NULL;
906 }
907
908 /* find the module */
909 if (prefix) {
Michal Vasko0b245382020-07-09 15:43:52 +0200910 str = strndup(prefix, pref_len);
Michal Vaskod86997b2020-05-26 15:19:54 +0200911 module = ly_ctx_get_module_implemented(ctx, str);
912 free(str);
Michal Vasko004d3152020-06-11 19:59:22 +0200913 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +0200914 }
915
916 /* set value if none */
917 if (!val_str) {
918 val_str = "";
919 }
920
921 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
922 LYD_JSON, parent->schema);
923 return ret;
924}
925
Michal Vasko00cbf532020-06-15 13:58:47 +0200926API struct lyd_node *
927lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
928 const char *module_name)
929{
930 struct lyd_node *ret = NULL;
931
932 LY_CHECK_ARG_RET(ctx, parent || ctx, name, module_name, NULL);
933
934 if (!ctx) {
935 ctx = LYD_NODE_CTX(parent);
936 }
937 if (!value) {
938 value = "";
939 }
940
941 if (!lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0, module_name, &ret)
942 && parent) {
943 lyd_insert_node(parent, NULL, ret);
944 }
945 return ret;
946}
947
948API struct ly_attr *
949lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str)
950{
951 struct ly_attr *ret = NULL;
952 const struct ly_ctx *ctx;
953 const char *prefix, *tmp;
954 size_t pref_len, name_len;
955
956 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, NULL);
957
958 ctx = LYD_NODE_CTX(parent);
959
960 /* parse the name */
961 tmp = name;
962 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
963 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
964 return NULL;
965 }
966
967 /* set value if none */
968 if (!val_str) {
969 val_str = "";
970 }
971
972 ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL, prefix,
973 pref_len, module_name);
974 return ret;
975}
976
977API LY_ERR
978lyd_change_term(struct lyd_node *term, const char *val_str)
979{
980 LY_ERR ret = LY_SUCCESS;
981 struct lysc_type *type;
982 struct lyd_node_term *t;
983 struct lyd_node *parent;
984 struct lyd_value val = {0};
985 int dflt_change, val_change;
986
987 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
988
989 if (!val_str) {
990 val_str = "";
991 }
992 t = (struct lyd_node_term *)term;
993 type = ((struct lysc_node_leaf *)term->schema)->type;
994
995 /* parse the new value */
996 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
997 LYD_JSON), cleanup);
998
999 /* compare original and new value */
1000 if (type->plugin->compare(&t->value, &val)) {
1001 /* values differ, switch them */
1002 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1003 t->value = val;
1004 memset(&val, 0, sizeof val);
1005 val_change = 1;
1006 } else {
1007 val_change = 0;
1008 }
1009
1010 /* always clear the default flag */
1011 if (term->flags & LYD_DEFAULT) {
1012 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1013 parent->flags &= ~LYD_DEFAULT;
1014 }
1015 dflt_change = 1;
1016 } else {
1017 dflt_change = 0;
1018 }
1019
1020 if (val_change || dflt_change) {
1021 /* make the node non-validated */
1022 term->flags &= LYD_NEW;
1023 }
1024
1025 if (val_change) {
1026 if (term->schema->nodetype == LYS_LEAFLIST) {
1027 /* leaf-list needs to be hashed again and re-inserted into parent */
1028 lyd_unlink_hash(term);
1029 lyd_hash(term);
1030 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1031 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1032 /* list needs to be updated if its key was changed */
1033 assert(term->parent->schema->nodetype == LYS_LIST);
1034 lyd_unlink_hash((struct lyd_node *)term->parent);
1035 lyd_hash((struct lyd_node *)term->parent);
1036 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1037 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1038 }
1039
1040 /* retrun value */
1041 if (!val_change) {
1042 if (dflt_change) {
1043 /* only default flag change */
1044 ret = LY_EEXIST;
1045 } else {
1046 /* no change */
1047 ret = LY_ENOT;
1048 }
1049 } /* else value changed, LY_SUCCESS */
1050
1051cleanup:
1052 type->plugin->free(LYD_NODE_CTX(term), &val);
1053 return ret;
1054}
1055
1056API struct lyd_node *
1057lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options)
1058{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001059 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001060
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001061 lyd_new_path2(parent, ctx, path, value, 0, options, &new_parent, NULL);
1062 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001063}
1064
1065API struct lyd_node *
1066lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1067 LYD_ANYDATA_VALUETYPE value_type, int options)
1068{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001069 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001070
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001071 lyd_new_path2(parent, ctx, path, value, value_type, options, &new_parent, NULL);
1072 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001073}
1074
1075API LY_ERR
1076lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1077 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1078{
1079 LY_ERR ret = LY_SUCCESS, r;
1080 struct lyxp_expr *exp = NULL;
1081 struct ly_path *p = NULL;
1082 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1083 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001084 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001085 struct ly_path_predicate *pred;
1086
1087 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1088
1089 if (!ctx) {
1090 ctx = LYD_NODE_CTX(parent);
1091 }
1092
1093 /* parse path */
1094 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1095 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1096
1097 /* compile path */
1098 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1099 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1100 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1101
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001102 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1103 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001104 && !(options & LYD_NEWOPT_OPAQ)) {
1105 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1106 lys_nodetype2str(schema->nodetype), schema->name);
1107 ret = LY_EINVAL;
1108 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001109 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001110 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001111 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1112 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001113
1114 if (!value) {
1115 value = "";
1116 }
1117
1118 r = LY_SUCCESS;
1119 if (options & LYD_NEWOPT_OPAQ) {
1120 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1121 }
1122 if (!r) {
1123 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1124 NULL, LYD_JSON), cleanup);
1125 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1126 }
1127
1128 /* try to find any existing nodes in the path */
1129 if (parent) {
1130 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1131 if (ret == LY_SUCCESS) {
1132 /* the node exists, are we supposed to update it or is it just a default? */
1133 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1134 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1135 ret = LY_EEXIST;
1136 goto cleanup;
1137 }
1138
1139 /* update the existing node */
1140 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1141 goto cleanup;
1142 } else if (ret == LY_EINCOMPLETE) {
1143 /* some nodes were found, adjust the iterator to the next segment */
1144 ++path_idx;
1145 } else if (ret == LY_ENOTFOUND) {
1146 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001147 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001148 node = parent;
1149 }
1150 } else {
1151 /* error */
1152 goto cleanup;
1153 }
1154 }
1155
1156 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001157 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001158 cur_parent = node;
1159 schema = p[path_idx].node;
1160
1161 switch (schema->nodetype) {
1162 case LYS_LIST:
1163 if (!(schema->flags & LYS_KEYLESS)) {
1164 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1165 /* creating opaque list without keys */
1166 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1167 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1168 } else {
1169 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1170 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1171 }
1172 break;
1173 }
1174 /* fallthrough */
1175 case LYS_CONTAINER:
1176 case LYS_NOTIF:
1177 case LYS_RPC:
1178 case LYS_ACTION:
1179 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1180 break;
1181 case LYS_LEAFLIST:
1182 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1183 /* creating opaque leaf-list without value */
1184 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1185 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1186 } else {
1187 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1188 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1189 }
1190 break;
1191 case LYS_LEAF:
1192 /* make there is some value */
1193 if (!value) {
1194 value = "";
1195 }
1196
1197 r = LY_SUCCESS;
1198 if (options & LYD_NEWOPT_OPAQ) {
1199 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1200 }
1201 if (!r) {
1202 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1203 LYD_JSON, &node), cleanup);
1204 } else {
1205 /* creating opaque leaf without value */
1206 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1207 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1208 }
1209 break;
1210 case LYS_ANYDATA:
1211 case LYS_ANYXML:
1212 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1213 break;
1214 default:
1215 LOGINT(ctx);
1216 ret = LY_EINT;
1217 goto cleanup;
1218 }
1219
1220 if (cur_parent) {
1221 /* connect to the parent */
1222 lyd_insert_node(cur_parent, NULL, node);
1223 } else if (parent) {
1224 /* connect to top-level siblings */
1225 lyd_insert_node(NULL, &parent, node);
1226 }
1227
1228 /* update remembered nodes */
1229 if (!nparent) {
1230 nparent = node;
1231 }
1232 nnode = node;
1233 }
1234
1235cleanup:
1236 lyxp_expr_free(ctx, exp);
1237 ly_path_free(ctx, p);
1238 if (!ret) {
1239 /* set out params only on success */
1240 if (new_parent) {
1241 *new_parent = nparent;
1242 }
1243 if (new_node) {
1244 *new_node = nnode;
1245 }
1246 }
1247 return ret;
1248}
1249
Michal Vasko90932a92020-02-12 14:33:03 +01001250struct lyd_node *
1251lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
1252{
1253 const struct lysc_node *prev_key;
1254 struct lyd_node *match = NULL;
1255
1256 if (!first_sibling) {
1257 return NULL;
1258 }
1259
1260 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
1261 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
1262 }
1263
1264 return match;
1265}
1266
1267/**
1268 * @brief Insert node after a sibling.
1269 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001270 * Handles inserting into NP containers and key-less lists.
1271 *
Michal Vasko90932a92020-02-12 14:33:03 +01001272 * @param[in] sibling Sibling to insert after.
1273 * @param[in] node Node to insert.
1274 */
1275static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001276lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001277{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001278 struct lyd_node_inner *par;
1279
Michal Vasko90932a92020-02-12 14:33:03 +01001280 assert(!node->next && (node->prev == node));
1281
1282 node->next = sibling->next;
1283 node->prev = sibling;
1284 sibling->next = node;
1285 if (node->next) {
1286 /* sibling had a succeeding node */
1287 node->next->prev = node;
1288 } else {
1289 /* sibling was last, find first sibling and change its prev */
1290 if (sibling->parent) {
1291 sibling = sibling->parent->child;
1292 } else {
1293 for (; sibling->prev->next != node; sibling = sibling->prev);
1294 }
1295 sibling->prev = node;
1296 }
1297 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001298
Michal Vasko9f96a052020-03-10 09:41:45 +01001299 for (par = node->parent; par; par = par->parent) {
1300 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1301 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001302 par->flags &= ~LYD_DEFAULT;
1303 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001304 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1305 /* rehash key-less list */
1306 lyd_hash((struct lyd_node *)par);
1307 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001308 }
1309
1310 /* insert into hash table */
1311 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001312}
1313
1314/**
1315 * @brief Insert node before a sibling.
1316 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001317 * Handles inserting into NP containers and key-less lists.
1318 *
Michal Vasko90932a92020-02-12 14:33:03 +01001319 * @param[in] sibling Sibling to insert before.
1320 * @param[in] node Node to insert.
1321 */
1322static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001323lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001324{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001325 struct lyd_node_inner *par;
1326
Michal Vasko90932a92020-02-12 14:33:03 +01001327 assert(!node->next && (node->prev == node));
1328
1329 node->next = sibling;
1330 /* covers situation of sibling being first */
1331 node->prev = sibling->prev;
1332 sibling->prev = node;
1333 if (node->prev->next) {
1334 /* sibling had a preceding node */
1335 node->prev->next = node;
1336 } else if (sibling->parent) {
1337 /* sibling was first and we must also change parent child pointer */
1338 sibling->parent->child = node;
1339 }
1340 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001341
Michal Vasko9f96a052020-03-10 09:41:45 +01001342 for (par = node->parent; par; par = par->parent) {
1343 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1344 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001345 par->flags &= ~LYD_DEFAULT;
1346 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001347 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1348 /* rehash key-less list */
1349 lyd_hash((struct lyd_node *)par);
1350 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001351 }
1352
1353 /* insert into hash table */
1354 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001355}
1356
1357/**
1358 * @brief Insert node as the last child of a parent.
1359 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001360 * Handles inserting into NP containers and key-less lists.
1361 *
Michal Vasko90932a92020-02-12 14:33:03 +01001362 * @param[in] parent Parent to insert into.
1363 * @param[in] node Node to insert.
1364 */
1365static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001366lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001367{
1368 struct lyd_node_inner *par;
1369
Michal Vasko0249f7c2020-03-05 16:36:40 +01001370 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001371 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001372
1373 par = (struct lyd_node_inner *)parent;
1374
1375 if (!par->child) {
1376 par->child = node;
1377 } else {
1378 node->prev = par->child->prev;
1379 par->child->prev->next = node;
1380 par->child->prev = node;
1381 }
1382 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001383
Michal Vasko9f96a052020-03-10 09:41:45 +01001384 for (; par; par = par->parent) {
1385 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1386 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001387 par->flags &= ~LYD_DEFAULT;
1388 }
Michal Vasko52927e22020-03-16 17:26:14 +01001389 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001390 /* rehash key-less list */
1391 lyd_hash((struct lyd_node *)par);
1392 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001393 }
1394
1395 /* insert into hash table */
1396 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001397}
1398
1399void
Michal Vasko9b368d32020-02-14 13:53:31 +01001400lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001401{
Michal Vasko9b368d32020-02-14 13:53:31 +01001402 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001403 const struct lysc_node *skey = NULL;
1404 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001405
Michal Vasko52927e22020-03-16 17:26:14 +01001406 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001407
1408 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1409 parent = (struct lyd_node *)(*first_sibling)->parent;
1410 }
Michal Vasko90932a92020-02-12 14:33:03 +01001411
1412 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001413 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001414 /* it is key and we need to insert it at the correct place */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001415 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent, 0), node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +01001416 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001417 lyd_insert_after_node(anchor, node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001418 } else if (lyd_node_children(parent, 0)) {
1419 lyd_insert_before_node(lyd_node_children(parent, 0), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001420 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001421 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001422 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001423
1424 /* hash list if all its keys were added */
1425 assert(parent->schema->nodetype == LYS_LIST);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001426 anchor = lyd_node_children(parent, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +01001427 has_keys = 1;
1428 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1429 if (!anchor || (anchor->schema != skey)) {
1430 /* key missing */
1431 has_keys = 0;
1432 break;
1433 }
1434
1435 anchor = anchor->next;
1436 }
1437 if (has_keys) {
1438 lyd_hash(parent);
1439 }
1440
Michal Vasko90932a92020-02-12 14:33:03 +01001441 } else {
1442 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001443 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001444 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001445 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001446 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001447 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001448 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001449 anchor = anchor->prev;
1450 }
1451
Michal Vaskoc193ce92020-03-06 11:04:48 +01001452 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001453 /* insert after last sibling from this module */
1454 lyd_insert_after_node(anchor, node);
1455 } else {
1456 /* no data from this module, insert at the last position */
1457 lyd_insert_after_node((*first_sibling)->prev, node);
1458 }
Michal Vasko90932a92020-02-12 14:33:03 +01001459 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001460 /* the only sibling */
1461 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001462 }
Michal Vasko90932a92020-02-12 14:33:03 +01001463}
1464
Michal Vaskof03ed032020-03-04 13:31:44 +01001465static LY_ERR
1466lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1467{
1468 const struct lysc_node *par2;
1469
1470 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001471 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001472
1473 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001474 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001475
1476 if (parent) {
1477 /* inner node */
1478 if (par2 != parent) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001479 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001480 return LY_EINVAL;
1481 }
1482 } else {
1483 /* top-level node */
1484 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001485 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001486 return LY_EINVAL;
1487 }
1488 }
1489
1490 return LY_SUCCESS;
1491}
1492
1493API LY_ERR
1494lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1495{
1496 struct lyd_node *iter;
1497
Michal Vasko654bc852020-06-23 13:28:06 +02001498 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001499
1500 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1501
1502 if (node->schema->flags & LYS_KEY) {
1503 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1504 return LY_EINVAL;
1505 }
1506
1507 if (node->parent || node->prev->next) {
1508 lyd_unlink_tree(node);
1509 }
1510
1511 while (node) {
1512 iter = node->next;
1513 lyd_unlink_tree(node);
1514 lyd_insert_node(parent, NULL, node);
1515 node = iter;
1516 }
1517 return LY_SUCCESS;
1518}
1519
1520API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001521lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1522{
1523 struct lyd_node *iter;
1524
1525 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1526
Michal Vasko62ed12d2020-05-21 10:08:25 +02001527 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001528
1529 if (node->schema->flags & LYS_KEY) {
1530 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1531 return LY_EINVAL;
1532 }
1533
1534 if (node->parent || node->prev->next) {
1535 lyd_unlink_tree(node);
1536 }
1537
1538 while (node) {
1539 iter = node->next;
1540 lyd_unlink_tree(node);
1541 lyd_insert_node(NULL, &sibling, node);
1542 node = iter;
1543 }
1544 return LY_SUCCESS;
1545}
1546
Michal Vasko0249f7c2020-03-05 16:36:40 +01001547static LY_ERR
1548lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1549{
1550 if (sibling->parent) {
1551 /* nested, we do not care for the order */
1552 return LY_SUCCESS;
1553 }
1554
1555 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001556 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1557 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001558 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 +01001559 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001560 return LY_EINVAL;
1561 }
1562
Michal Vaskoc193ce92020-03-06 11:04:48 +01001563 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1564 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001565 /* inserting before/after its module data */
1566 return LY_SUCCESS;
1567 }
1568 }
1569
1570 /* find first sibling */
1571 while (sibling->prev->next) {
1572 sibling = sibling->prev;
1573 }
1574
1575 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001576 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001577 /* inserting before its module data */
1578 return LY_SUCCESS;
1579 }
1580 }
1581
1582 /* check there are no data of this module */
1583 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001584 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001585 /* some data of this module found */
1586 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001587 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001588 return LY_EINVAL;
1589 }
1590 }
1591
1592 return LY_SUCCESS;
1593}
1594
Michal Vaskob1b5c262020-03-05 14:29:47 +01001595API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001596lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1597{
1598 struct lyd_node *iter;
1599
1600 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1601
Michal Vasko62ed12d2020-05-21 10:08:25 +02001602 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001603
1604 if (node->schema->flags & LYS_KEY) {
1605 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1606 return LY_EINVAL;
1607 } else if (sibling->schema->flags & LYS_KEY) {
1608 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1609 return LY_EINVAL;
1610 }
1611
Michal Vasko0249f7c2020-03-05 16:36:40 +01001612 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1613
Michal Vaskof03ed032020-03-04 13:31:44 +01001614 if (node->parent || node->prev->next) {
1615 lyd_unlink_tree(node);
1616 }
1617
1618 /* insert in reverse order to get the original order */
1619 node = node->prev;
1620 while (node) {
1621 iter = node->prev;
1622 lyd_unlink_tree(node);
1623
1624 lyd_insert_before_node(sibling, node);
1625 /* move the anchor accordingly */
1626 sibling = node;
1627
1628 node = (iter == node) ? NULL : iter;
1629 }
1630 return LY_SUCCESS;
1631}
1632
1633API LY_ERR
1634lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1635{
1636 struct lyd_node *iter;
1637
1638 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1639
Michal Vasko62ed12d2020-05-21 10:08:25 +02001640 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001641
1642 if (node->schema->flags & LYS_KEY) {
1643 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1644 return LY_EINVAL;
1645 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1646 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1647 return LY_EINVAL;
1648 }
1649
Michal Vasko0249f7c2020-03-05 16:36:40 +01001650 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1651
Michal Vaskof03ed032020-03-04 13:31:44 +01001652 if (node->parent || node->prev->next) {
1653 lyd_unlink_tree(node);
1654 }
1655
1656 while (node) {
1657 iter = node->next;
1658 lyd_unlink_tree(node);
1659
1660 lyd_insert_after_node(sibling, node);
1661 /* move the anchor accordingly */
1662 sibling = node;
1663
1664 node = iter;
1665 }
1666 return LY_SUCCESS;
1667}
1668
1669API void
1670lyd_unlink_tree(struct lyd_node *node)
1671{
1672 struct lyd_node *iter;
1673
1674 if (!node) {
1675 return;
1676 }
1677
1678 /* unlink from siblings */
1679 if (node->prev->next) {
1680 node->prev->next = node->next;
1681 }
1682 if (node->next) {
1683 node->next->prev = node->prev;
1684 } else {
1685 /* unlinking the last node */
1686 if (node->parent) {
1687 iter = node->parent->child;
1688 } else {
1689 iter = node->prev;
1690 while (iter->prev != node) {
1691 iter = iter->prev;
1692 }
1693 }
1694 /* update the "last" pointer from the first node */
1695 iter->prev = node->prev;
1696 }
1697
1698 /* unlink from parent */
1699 if (node->parent) {
1700 if (node->parent->child == node) {
1701 /* the node is the first child */
1702 node->parent->child = node->next;
1703 }
1704
1705 lyd_unlink_hash(node);
1706
1707 /* check for keyless list and update its hash */
1708 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001709 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001710 lyd_hash(iter);
1711 }
1712 }
1713
1714 node->parent = NULL;
1715 }
1716
1717 node->next = NULL;
1718 node->prev = node;
1719}
1720
Michal Vasko90932a92020-02-12 14:33:03 +01001721LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001722lyd_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 +01001723 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 +01001724 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001725{
1726 LY_ERR ret;
1727 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001728 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001729 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001730
Michal Vasko9f96a052020-03-10 09:41:45 +01001731 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001732
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001733 LY_ARRAY_FOR(mod->compiled->exts, u) {
1734 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1735 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001736 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001737 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001738 break;
1739 }
1740 }
1741 if (!ant) {
1742 /* attribute is not defined as a metadata annotation (RFC 7952) */
1743 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1744 mod->name, name_len, name);
1745 return LY_EINVAL;
1746 }
1747
Michal Vasko9f96a052020-03-10 09:41:45 +01001748 mt = calloc(1, sizeof *mt);
1749 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1750 mt->parent = parent;
1751 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001752 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 +01001753 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001754 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001755 return ret;
1756 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001757 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001758
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001759 /* insert as the last attribute */
1760 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001761 if (parent->meta) {
1762 for (last = parent->meta; last->next; last = last->next);
1763 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001764 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001765 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001766 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001767 } else if (*meta) {
1768 for (last = *meta; last->next; last = last->next);
1769 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001770 }
1771
1772 /* remove default flags from NP containers */
Michal Vasko4ba6aef2020-07-09 15:44:23 +02001773 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001774 parent->flags &= ~LYD_DEFAULT;
1775 parent = (struct lyd_node *)parent->parent;
1776 }
1777
Michal Vasko9f96a052020-03-10 09:41:45 +01001778 if (meta) {
1779 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001780 }
1781 return ret;
1782}
1783
Michal Vasko52927e22020-03-16 17:26:14 +01001784LY_ERR
1785ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1786 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1787 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1788{
1789 struct ly_attr *at, *last;
1790 struct lyd_node_opaq *opaq;
1791
1792 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1793 assert(name && name_len);
1794 assert((prefix_len && ns) || (!prefix_len && !ns));
1795
1796 if (!value_len) {
1797 value = "";
1798 }
1799
1800 at = calloc(1, sizeof *at);
1801 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1802 at->parent = (struct lyd_node_opaq *)parent;
1803 at->name = lydict_insert(ctx, name, name_len);
1804 if (dynamic && *dynamic) {
1805 at->value = lydict_insert_zc(ctx, (char *)value);
1806 *dynamic = 0;
1807 } else {
1808 at->value = lydict_insert(ctx, value, value_len);
1809 }
1810
1811 at->format = format;
1812 at->val_prefs = val_prefs;
1813 if (ns) {
1814 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1815 at->prefix.ns = lydict_insert(ctx, ns, 0);
1816 }
1817
1818 /* insert as the last attribute */
1819 if (parent) {
1820 opaq = (struct lyd_node_opaq *)parent;
1821 if (opaq->attr) {
1822 for (last = opaq->attr; last->next; last = last->next);
1823 last->next = at;
1824 } else {
1825 opaq->attr = at;
1826 }
1827 } else if (*attr) {
1828 for (last = *attr; last->next; last = last->next);
1829 last->next = at;
1830 }
1831
1832 if (attr) {
1833 *attr = at;
1834 }
1835 return LY_SUCCESS;
1836}
1837
Radek Krejci084289f2019-07-09 17:35:30 +02001838API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001839lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001840{
Michal Vasko004d3152020-06-11 19:59:22 +02001841 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001842
Michal Vasko004d3152020-06-11 19:59:22 +02001843 if (ly_path_eval(path, tree, &target)) {
1844 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001845 }
1846
Michal Vasko004d3152020-06-11 19:59:22 +02001847 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001848}
1849
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001850API LY_ERR
1851lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1852{
1853 const struct lyd_node *iter1, *iter2;
1854 struct lyd_node_term *term1, *term2;
1855 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001856 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001857 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001858
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001859 if (!node1 || !node2) {
1860 if (node1 == node2) {
1861 return LY_SUCCESS;
1862 } else {
1863 return LY_ENOT;
1864 }
1865 }
1866
Michal Vasko52927e22020-03-16 17:26:14 +01001867 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001868 return LY_ENOT;
1869 }
1870
1871 if (node1->hash != node2->hash) {
1872 return LY_ENOT;
1873 }
Michal Vasko52927e22020-03-16 17:26:14 +01001874 /* 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 +02001875
Michal Vasko52927e22020-03-16 17:26:14 +01001876 if (!node1->schema) {
1877 opaq1 = (struct lyd_node_opaq *)node1;
1878 opaq2 = (struct lyd_node_opaq *)node2;
1879 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001880 return LY_ENOT;
1881 }
Michal Vasko52927e22020-03-16 17:26:14 +01001882 switch (opaq1->format) {
1883 case LYD_XML:
1884 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1885 return LY_ENOT;
1886 }
1887 break;
1888 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001889 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001890 /* not allowed */
1891 LOGINT(LYD_NODE_CTX(node1));
1892 return LY_EINT;
1893 }
1894 if (options & LYD_COMPARE_FULL_RECURSION) {
1895 iter1 = opaq1->child;
1896 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001897 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001898 }
1899 return LY_SUCCESS;
1900 } else {
1901 switch (node1->schema->nodetype) {
1902 case LYS_LEAF:
1903 case LYS_LEAFLIST:
1904 if (options & LYD_COMPARE_DEFAULTS) {
1905 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1906 return LY_ENOT;
1907 }
1908 }
1909
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001910 term1 = (struct lyd_node_term *)node1;
1911 term2 = (struct lyd_node_term *)node2;
1912 if (term1->value.realtype != term2->value.realtype) {
1913 return LY_ENOT;
1914 }
Michal Vasko52927e22020-03-16 17:26:14 +01001915
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001916 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001917 case LYS_CONTAINER:
1918 if (options & LYD_COMPARE_DEFAULTS) {
1919 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1920 return LY_ENOT;
1921 }
1922 }
1923 if (options & LYD_COMPARE_FULL_RECURSION) {
1924 iter1 = ((struct lyd_node_inner*)node1)->child;
1925 iter2 = ((struct lyd_node_inner*)node2)->child;
1926 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001927 }
1928 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001929 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001930 case LYS_ACTION:
1931 if (options & LYD_COMPARE_FULL_RECURSION) {
1932 /* TODO action/RPC
1933 goto all_children_compare;
1934 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001935 }
1936 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001937 case LYS_NOTIF:
1938 if (options & LYD_COMPARE_FULL_RECURSION) {
1939 /* TODO Notification
1940 goto all_children_compare;
1941 */
1942 }
1943 return LY_SUCCESS;
1944 case LYS_LIST:
1945 iter1 = ((struct lyd_node_inner*)node1)->child;
1946 iter2 = ((struct lyd_node_inner*)node2)->child;
1947
1948 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1949 /* lists with keys, their equivalence is based on their keys */
1950 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001951 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001952 key = key->next) {
1953 if (lyd_compare(iter1, iter2, options)) {
1954 return LY_ENOT;
1955 }
1956 iter1 = iter1->next;
1957 iter2 = iter2->next;
1958 }
1959 } else {
1960 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1961
1962 all_children_compare:
1963 if (!iter1 && !iter2) {
1964 /* no children, nothing to compare */
1965 return LY_SUCCESS;
1966 }
1967
1968 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1969 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1970 return LY_ENOT;
1971 }
1972 }
1973 if (iter1 || iter2) {
1974 return LY_ENOT;
1975 }
1976 }
1977 return LY_SUCCESS;
1978 case LYS_ANYXML:
1979 case LYS_ANYDATA:
1980 any1 = (struct lyd_node_any*)node1;
1981 any2 = (struct lyd_node_any*)node2;
1982
1983 if (any1->value_type != any2->value_type) {
1984 return LY_ENOT;
1985 }
1986 switch (any1->value_type) {
1987 case LYD_ANYDATA_DATATREE:
1988 iter1 = any1->value.tree;
1989 iter2 = any2->value.tree;
1990 goto all_children_compare;
1991 case LYD_ANYDATA_STRING:
1992 case LYD_ANYDATA_XML:
1993 case LYD_ANYDATA_JSON:
1994 len1 = strlen(any1->value.str);
1995 len2 = strlen(any2->value.str);
1996 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
1997 return LY_ENOT;
1998 }
1999 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002000 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002001 len1 = lyd_lyb_data_length(any1->value.mem);
2002 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002003 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2004 return LY_ENOT;
2005 }
2006 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002007 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002008 }
2009 }
2010
Michal Vasko52927e22020-03-16 17:26:14 +01002011 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002012 return LY_EINT;
2013}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002014
Michal Vasko21725742020-06-29 11:49:25 +02002015API LY_ERR
2016lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2017{
2018 if (!meta1 || !meta2) {
2019 if (meta1 == meta2) {
2020 return LY_SUCCESS;
2021 } else {
2022 return LY_ENOT;
2023 }
2024 }
2025
2026 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2027 return LY_ENOT;
2028 }
2029
2030 if (meta1->value.realtype != meta2->value.realtype) {
2031 return LY_ENOT;
2032 }
2033
2034 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2035}
2036
Radek Krejci22ebdba2019-07-25 13:59:43 +02002037/**
Michal Vasko52927e22020-03-16 17:26:14 +01002038 * @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 +02002039 *
2040 * 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 +02002041 *
2042 * @param[in] node Original node to duplicate
2043 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2044 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2045 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2046 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2047 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002048 */
Michal Vasko52927e22020-03-16 17:26:14 +01002049static LY_ERR
2050lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2051 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002052{
Michal Vasko52927e22020-03-16 17:26:14 +01002053 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002054 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002055 struct lyd_meta *meta;
2056 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002057 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002058
Michal Vasko52927e22020-03-16 17:26:14 +01002059 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002060
Michal Vasko52927e22020-03-16 17:26:14 +01002061 if (!node->schema) {
2062 dup = calloc(1, sizeof(struct lyd_node_opaq));
2063 } else {
2064 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002065 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002066 case LYS_ACTION:
2067 case LYS_NOTIF:
2068 case LYS_CONTAINER:
2069 case LYS_LIST:
2070 dup = calloc(1, sizeof(struct lyd_node_inner));
2071 break;
2072 case LYS_LEAF:
2073 case LYS_LEAFLIST:
2074 dup = calloc(1, sizeof(struct lyd_node_term));
2075 break;
2076 case LYS_ANYDATA:
2077 case LYS_ANYXML:
2078 dup = calloc(1, sizeof(struct lyd_node_any));
2079 break;
2080 default:
2081 LOGINT(LYD_NODE_CTX(node));
2082 ret = LY_EINT;
2083 goto error;
2084 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002085 }
Michal Vasko52927e22020-03-16 17:26:14 +01002086 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002087
Michal Vaskof6df0a02020-06-16 13:08:34 +02002088 if (options & LYD_DUP_WITH_FLAGS) {
2089 dup->flags = node->flags;
2090 } else {
2091 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2092 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002093 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002094 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002095
Michal Vasko25a32822020-07-09 15:48:22 +02002096 /* duplicate metadata */
2097 if (!(options & LYD_DUP_NO_META)) {
2098 LY_LIST_FOR(node->meta, meta) {
2099 if (!lyd_dup_meta(meta, dup)) {
2100 ret = LY_EINT;
2101 goto error;
2102 }
2103 }
2104 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002105
2106 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002107 if (!dup->schema) {
2108 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2109 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2110 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002111
2112 if (options & LYD_DUP_RECURSIVE) {
2113 /* duplicate all the children */
2114 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01002115 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
2116 }
2117 }
2118 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2119 opaq->format = orig->format;
2120 if (orig->prefix.pref) {
2121 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2122 }
2123 if (orig->prefix.ns) {
2124 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2125 }
2126 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002127 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002128 LY_ARRAY_FOR(orig->val_prefs, u) {
2129 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2130 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2131 LY_ARRAY_INCREMENT(opaq->val_prefs);
2132 }
2133 }
2134 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2135 opaq->ctx = orig->ctx;
2136 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2137 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2138 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2139
2140 term->hash = orig->hash;
2141 term->value.realtype = orig->value.realtype;
2142 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2143 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2144 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2145 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2146 struct lyd_node *child;
2147
2148 if (options & LYD_DUP_RECURSIVE) {
2149 /* duplicate all the children */
2150 LY_LIST_FOR(orig->child, child) {
2151 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002152 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002153 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002154 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002155 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002156 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002157 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002158 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002159 if (!child) {
2160 /* possibly not keys are present in filtered tree */
2161 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002162 } else if (child->schema != key) {
2163 /* possibly not all keys are present in filtered tree,
2164 * but there can be also some non-key nodes */
2165 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002166 }
Michal Vasko52927e22020-03-16 17:26:14 +01002167 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002168 child = child->next;
2169 }
2170 }
2171 lyd_hash(dup);
2172 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002173 dup->hash = node->hash;
2174 any = (struct lyd_node_any *)node;
2175 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002176 }
2177
Michal Vasko52927e22020-03-16 17:26:14 +01002178 /* insert */
2179 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002180
2181 if (dup_p) {
2182 *dup_p = dup;
2183 }
2184 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002185
2186error:
Michal Vasko52927e22020-03-16 17:26:14 +01002187 lyd_free_tree(dup);
2188 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002189}
2190
2191API struct lyd_node *
2192lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
2193{
Radek Krejci22ebdba2019-07-25 13:59:43 +02002194 const struct lyd_node *orig; /* original node to be duplicated */
2195 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002196 struct lyd_node *top = NULL; /* the most higher created node */
2197 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
2198 int keyless_parent_list = 0;
2199
2200 LY_CHECK_ARG_RET(NULL, node, NULL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002201
2202 if (options & LYD_DUP_WITH_PARENTS) {
2203 struct lyd_node_inner *orig_parent, *iter;
2204 int repeat = 1;
2205 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2206 if (parent && parent->schema == orig_parent->schema) {
2207 /* stop creating parents, connect what we have into the provided parent */
2208 iter = parent;
2209 repeat = 0;
2210 /* get know if there is a keyless list which we will have to rehash */
2211 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002212 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002213 keyless_parent_list = 1;
2214 break;
2215 }
2216 }
2217 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01002218 iter = NULL;
2219 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2220 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002221 }
2222 if (!local_parent) {
2223 local_parent = iter;
2224 }
2225 if (iter->child) {
2226 /* 1) list - add after keys
2227 * 2) provided parent with some children */
2228 iter->child->prev->next = top;
2229 if (top) {
2230 top->prev = iter->child->prev;
2231 iter->child->prev = top;
2232 }
2233 } else {
2234 iter->child = top;
2235 if (iter->schema->nodetype == LYS_LIST) {
2236 /* keyless list - we will need to rehash it since we are going to add nodes into it */
2237 keyless_parent_list = 1;
2238 }
2239 }
2240 if (top) {
2241 top->parent = iter;
2242 }
2243 top = (struct lyd_node*)iter;
2244 }
2245 if (repeat && parent) {
2246 /* given parent and created parents chain actually do not interconnect */
Michal Vasko25a32822020-07-09 15:48:22 +02002247 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2248 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002249 goto error;
2250 }
2251 } else {
2252 local_parent = parent;
2253 }
2254
Radek Krejci22ebdba2019-07-25 13:59:43 +02002255 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002256 /* if there is no local parent, it will be inserted into first */
2257 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 +02002258 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
2259 break;
2260 }
2261 }
2262 if (keyless_parent_list) {
2263 /* rehash */
2264 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002265 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002266 lyd_hash((struct lyd_node*)local_parent);
2267 }
2268 }
2269 }
2270 return first;
2271
2272error:
2273 if (top) {
2274 lyd_free_tree(top);
2275 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002276 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002277 }
2278 return NULL;
2279}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002280
Michal Vasko25a32822020-07-09 15:48:22 +02002281API struct lyd_meta *
2282lyd_dup_meta(const struct lyd_meta *meta, struct lyd_node *node)
2283{
2284 LY_ERR ret;
2285 struct lyd_meta *mt, *last;
2286
2287 LY_CHECK_ARG_RET(NULL, meta, node, NULL);
2288
2289 /* create a copy */
2290 mt = calloc(1, sizeof *mt);
2291 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), NULL);
2292 mt->parent = node;
2293 mt->annotation = meta->annotation;
2294 mt->value.realtype = meta->value.realtype;
2295 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
2296 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), NULL);
2297 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2298
2299 /* insert as the last attribute */
2300 if (node->meta) {
2301 for (last = node->meta; last->next; last = last->next);
2302 last->next = mt;
2303 } else {
2304 node->meta = mt;
2305 }
2306
2307 return mt;
2308}
2309
Michal Vasko4490d312020-06-16 13:08:55 +02002310/**
2311 * @brief Merge a source sibling into target siblings.
2312 *
2313 * @param[in,out] first_trg First target sibling, is updated if top-level.
2314 * @param[in] parent_trg Target parent.
2315 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2316 * @param[in] options Merge options.
2317 * @return LY_ERR value.
2318 */
2319static LY_ERR
2320lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2321 int options)
2322{
2323 LY_ERR ret;
2324 const struct lyd_node *child_src, *tmp, *sibling_src;
2325 struct lyd_node *match_trg, *dup_src, *next, *elem;
2326 struct lysc_type *type;
2327 LYD_ANYDATA_VALUETYPE tmp_val_type;
2328 union lyd_any_value tmp_val;
2329
2330 sibling_src = *sibling_src_p;
2331 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2332 /* try to find the exact instance */
2333 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2334 } else {
2335 /* try to simply find the node, there cannot be more instances */
2336 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2337 }
2338
2339 if (!ret) {
2340 /* node found, make sure even value matches for all node types */
2341 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2342 /* since they are different, they cannot both be default */
2343 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2344
2345 /* update value (or only LYD_DEFAULT flag) only if no flag set or the source node is not default */
2346 if (!(options & LYD_MERGE_EXPLICIT) || !(sibling_src->flags & LYD_DEFAULT)) {
2347 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2348 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2349 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2350 &((struct lyd_node_term *)match_trg)->value));
2351
2352 /* copy flags and add LYD_NEW */
2353 match_trg->flags = sibling_src->flags | LYD_NEW;
2354 }
2355 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2356 if (options & LYD_MERGE_DESTRUCT) {
2357 dup_src = (struct lyd_node *)sibling_src;
2358 lyd_unlink_tree(dup_src);
2359 /* spend it */
2360 *sibling_src_p = NULL;
2361 } else {
2362 dup_src = lyd_dup(sibling_src, NULL, 0);
2363 LY_CHECK_RET(!dup_src, LY_EMEM);
2364 }
2365 /* just switch values */
2366 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2367 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2368 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2369 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2370 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2371 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2372
2373 /* copy flags and add LYD_NEW */
2374 match_trg->flags = sibling_src->flags | LYD_NEW;
2375
2376 /* dup_src is not needed, actually */
2377 lyd_free_tree(dup_src);
2378 } else {
2379 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002380 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002381 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2382 }
2383 }
2384 } else {
2385 /* node not found, merge it */
2386 if (options & LYD_MERGE_DESTRUCT) {
2387 dup_src = (struct lyd_node *)sibling_src;
2388 lyd_unlink_tree(dup_src);
2389 /* spend it */
2390 *sibling_src_p = NULL;
2391 } else {
2392 dup_src = lyd_dup(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS);
2393 LY_CHECK_RET(!dup_src, LY_EMEM);
2394 }
2395
2396 /* set LYD_NEW for all the new nodes, required for validation */
2397 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2398 elem->flags |= LYD_NEW;
2399 LYD_TREE_DFS_END(dup_src, next, elem);
2400 }
2401
2402 lyd_insert_node(parent_trg, first_trg, dup_src);
2403 }
2404
2405 return LY_SUCCESS;
2406}
2407
2408API LY_ERR
2409lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options)
2410{
2411 const struct lyd_node *sibling_src, *tmp;
2412 int first;
2413
2414 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2415
2416 if (!source) {
2417 /* nothing to merge */
2418 return LY_SUCCESS;
2419 }
2420
2421 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2422 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2423 return LY_EINVAL;
2424 }
2425
2426 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2427 first = sibling_src == source ? 1 : 0;
2428 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2429 if (first && !sibling_src) {
2430 /* source was spent (unlinked), move to the next node */
2431 source = tmp;
2432 }
2433
2434 if (options & LYD_MERGE_NOSIBLINGS) {
2435 break;
2436 }
2437 }
2438
2439 if (options & LYD_MERGE_DESTRUCT) {
2440 /* free any leftover source data that were not merged */
2441 lyd_free_siblings((struct lyd_node *)source);
2442 }
2443
2444 return LY_SUCCESS;
2445}
2446
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002447static LY_ERR
2448lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2449{
Michal Vasko14654712020-02-06 08:35:21 +01002450 /* ending \0 */
2451 ++reqlen;
2452
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002453 if (reqlen > *buflen) {
2454 if (is_static) {
2455 return LY_EINCOMPLETE;
2456 }
2457
2458 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2459 if (!*buffer) {
2460 return LY_EMEM;
2461 }
2462
2463 *buflen = reqlen;
2464 }
2465
2466 return LY_SUCCESS;
2467}
2468
Michal Vaskod59035b2020-07-08 12:00:06 +02002469LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002470lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2471{
2472 const struct lyd_node *key;
2473 int dynamic = 0;
2474 size_t len;
2475 const char *val;
2476 char quot;
2477 LY_ERR rc;
2478
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002479 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002480 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2481 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2482 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2483 if (rc != LY_SUCCESS) {
2484 if (dynamic) {
2485 free((char *)val);
2486 }
2487 return rc;
2488 }
2489
2490 quot = '\'';
2491 if (strchr(val, '\'')) {
2492 quot = '"';
2493 }
2494 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2495
2496 if (dynamic) {
2497 free((char *)val);
2498 }
2499 }
2500
2501 return LY_SUCCESS;
2502}
2503
2504/**
2505 * @brief Append leaf-list value predicate to path.
2506 *
2507 * @param[in] node Node to print.
2508 * @param[in,out] buffer Buffer to print to.
2509 * @param[in,out] buflen Current buffer length.
2510 * @param[in,out] bufused Current number of characters used in @p buffer.
2511 * @param[in] is_static Whether buffer is static or can be reallocated.
2512 * @return LY_ERR
2513 */
2514static LY_ERR
2515lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2516{
2517 int dynamic = 0;
2518 size_t len;
2519 const char *val;
2520 char quot;
2521 LY_ERR rc;
2522
2523 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2524 len = 4 + strlen(val) + 2;
2525 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2526 if (rc != LY_SUCCESS) {
2527 goto cleanup;
2528 }
2529
2530 quot = '\'';
2531 if (strchr(val, '\'')) {
2532 quot = '"';
2533 }
2534 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2535
2536cleanup:
2537 if (dynamic) {
2538 free((char *)val);
2539 }
2540 return rc;
2541}
2542
2543/**
2544 * @brief Append node position (relative to its other instances) predicate to path.
2545 *
2546 * @param[in] node Node to print.
2547 * @param[in,out] buffer Buffer to print to.
2548 * @param[in,out] buflen Current buffer length.
2549 * @param[in,out] bufused Current number of characters used in @p buffer.
2550 * @param[in] is_static Whether buffer is static or can be reallocated.
2551 * @return LY_ERR
2552 */
2553static LY_ERR
2554lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2555{
2556 const struct lyd_node *first, *iter;
2557 size_t len;
2558 int pos;
2559 char *val = NULL;
2560 LY_ERR rc;
2561
2562 if (node->parent) {
2563 first = node->parent->child;
2564 } else {
2565 for (first = node; node->prev->next; node = node->prev);
2566 }
2567 pos = 1;
2568 for (iter = first; iter != node; iter = iter->next) {
2569 if (iter->schema == node->schema) {
2570 ++pos;
2571 }
2572 }
2573 if (asprintf(&val, "%d", pos) == -1) {
2574 return LY_EMEM;
2575 }
2576
2577 len = 1 + strlen(val) + 1;
2578 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2579 if (rc != LY_SUCCESS) {
2580 goto cleanup;
2581 }
2582
2583 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2584
2585cleanup:
2586 free(val);
2587 return rc;
2588}
2589
2590API char *
2591lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2592{
Michal Vasko14654712020-02-06 08:35:21 +01002593 int is_static = 0, i, depth;
2594 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002595 const struct lyd_node *iter;
2596 const struct lys_module *mod;
2597 LY_ERR rc;
2598
2599 LY_CHECK_ARG_RET(NULL, node, NULL);
2600 if (buffer) {
2601 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2602 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002603 } else {
2604 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002605 }
2606
2607 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002608 case LYD_PATH_LOG:
2609 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002610 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2611 ++depth;
2612 }
2613
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002614 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002615 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002616 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002617 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002618iter_print:
2619 /* print prefix and name */
2620 mod = NULL;
2621 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2622 mod = iter->schema->module;
2623 }
2624
2625 /* realloc string */
2626 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2627 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2628 if (rc != LY_SUCCESS) {
2629 break;
2630 }
2631
2632 /* print next node */
2633 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2634
2635 switch (iter->schema->nodetype) {
2636 case LYS_LIST:
2637 if (iter->schema->flags & LYS_KEYLESS) {
2638 /* print its position */
2639 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2640 } else {
2641 /* print all list keys in predicates */
2642 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2643 }
2644 break;
2645 case LYS_LEAFLIST:
2646 if (iter->schema->flags & LYS_CONFIG_W) {
2647 /* print leaf-list value */
2648 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2649 } else {
2650 /* print its position */
2651 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2652 }
2653 break;
2654 default:
2655 /* nothing to print more */
2656 rc = LY_SUCCESS;
2657 break;
2658 }
2659 if (rc != LY_SUCCESS) {
2660 break;
2661 }
2662
Michal Vasko14654712020-02-06 08:35:21 +01002663 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002664 }
2665 break;
2666 }
2667
2668 return buffer;
2669}
Michal Vaskoe444f752020-02-10 12:20:06 +01002670
Michal Vasko25a32822020-07-09 15:48:22 +02002671API struct lyd_meta *
2672lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2673{
2674 struct lyd_meta *ret = NULL;
2675 const struct ly_ctx *ctx;
2676 const char *prefix, *tmp;
2677 char *str;
2678 size_t pref_len, name_len;
2679
2680 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2681
2682 if (!first) {
2683 return NULL;
2684 }
2685
2686 ctx = first->annotation->module->ctx;
2687
2688 /* parse the name */
2689 tmp = name;
2690 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2691 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2692 return NULL;
2693 }
2694
2695 /* find the module */
2696 if (prefix) {
2697 str = strndup(prefix, pref_len);
2698 module = ly_ctx_get_module_latest(ctx, str);
2699 free(str);
2700 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2701 }
2702
2703 /* find the metadata */
2704 LY_LIST_FOR(first, first) {
2705 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2706 ret = (struct lyd_meta *)first;
2707 break;
2708 }
2709 }
2710
2711 return ret;
2712}
2713
Michal Vasko9b368d32020-02-14 13:53:31 +01002714LY_ERR
2715lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
2716 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002717{
2718 LY_ERR rc;
2719 const struct lyd_node *node = NULL;
2720 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02002721 struct lyxp_expr *expr = NULL;
2722 uint16_t exp_idx = 0;
2723 struct ly_path_predicate *predicates = NULL;
2724 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01002725 struct lyd_value val = {0};
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002726 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01002727
Michal Vasko9b368d32020-02-14 13:53:31 +01002728 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002729
2730 if (!first) {
2731 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01002732 if (match) {
2733 *match = NULL;
2734 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002735 return LY_ENOTFOUND;
2736 }
2737
Michal Vaskoe444f752020-02-10 12:20:06 +01002738 if (key_or_value && !val_len) {
2739 val_len = strlen(key_or_value);
2740 }
2741
2742 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01002743 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01002744 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 +01002745 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02002746 /* parse keys */
2747 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
2748 LY_PATH_PRED_KEYS, &expr), cleanup);
2749
2750 /* compile them */
2751 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
2752 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002753 }
2754
2755 /* find first matching value */
2756 LY_LIST_FOR(first, node) {
2757 if (node->schema != schema) {
2758 continue;
2759 }
2760
Michal Vasko00cbf532020-06-15 13:58:47 +02002761 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002762 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02002763 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002764 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002765 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 +01002766 if (rc == LY_ENOTFOUND) {
2767 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01002768 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01002769 }
2770 LY_CHECK_GOTO(rc, cleanup);
2771
2772 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02002773 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002774 break;
2775 }
2776 }
2777
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002778 if (u < LY_ARRAY_COUNT(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002779 /* not a match */
2780 continue;
2781 }
Michal Vasko90932a92020-02-12 14:33:03 +01002782 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002783 term = (struct lyd_node_term *)node;
2784
2785 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01002786 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002787 /* not a match */
2788 continue;
2789 }
2790 }
2791
2792 /* all criteria passed */
2793 break;
2794 }
2795
2796 if (!node) {
2797 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01002798 if (match) {
2799 *match = NULL;
2800 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002801 goto cleanup;
2802 }
2803
2804 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01002805 if (match) {
2806 *match = (struct lyd_node *)node;
2807 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002808 rc = LY_SUCCESS;
2809
2810cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02002811 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
2812 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01002813 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002814 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01002815 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002816 return rc;
2817}
2818
2819API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01002820lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
2821 const char *key_or_value, size_t val_len, struct lyd_node **match)
2822{
2823 const struct lysc_node *schema;
2824
2825 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
2826
2827 if (!first) {
2828 /* no data */
2829 *match = NULL;
2830 return LY_ENOTFOUND;
2831 }
2832
2833 /* find schema */
2834 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
2835 if (!schema) {
2836 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
2837 return LY_EINVAL;
2838 }
2839
2840 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
2841}
2842
2843API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002844lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2845{
2846 struct lyd_node **match_p;
2847 struct lyd_node_inner *parent;
2848
Michal Vaskof03ed032020-03-04 13:31:44 +01002849 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002850
Michal Vasko62ed12d2020-05-21 10:08:25 +02002851 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2852 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002853 if (match) {
2854 *match = NULL;
2855 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002856 return LY_ENOTFOUND;
2857 }
2858
2859 /* find first sibling */
2860 if (siblings->parent) {
2861 siblings = siblings->parent->child;
2862 } else {
2863 while (siblings->prev->next) {
2864 siblings = siblings->prev;
2865 }
2866 }
2867
2868 parent = (struct lyd_node_inner *)siblings->parent;
2869 if (parent && parent->children_ht) {
2870 assert(target->hash);
2871
2872 /* find by hash */
2873 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2874 siblings = *match_p;
2875 } else {
2876 /* not found */
2877 siblings = NULL;
2878 }
2879 } else {
2880 /* no children hash table */
2881 for (; siblings; siblings = siblings->next) {
2882 if (!lyd_compare(siblings, target, 0)) {
2883 break;
2884 }
2885 }
2886 }
2887
2888 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002889 if (match) {
2890 *match = NULL;
2891 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002892 return LY_ENOTFOUND;
2893 }
2894
Michal Vasko9b368d32020-02-14 13:53:31 +01002895 if (match) {
2896 *match = (struct lyd_node *)siblings;
2897 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002898 return LY_SUCCESS;
2899}
2900
2901API LY_ERR
2902lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2903{
2904 struct lyd_node_inner *parent;
2905 struct lyd_node *match;
2906 struct lyd_node **match_p;
2907 struct ly_set *ret;
2908
2909 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
2910
Michal Vasko62ed12d2020-05-21 10:08:25 +02002911 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2912 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01002913 return LY_ENOTFOUND;
2914 }
2915
2916 ret = ly_set_new();
2917 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
2918
2919 /* find first sibling */
2920 if (siblings->parent) {
2921 siblings = siblings->parent->child;
2922 } else {
2923 while (siblings->prev->next) {
2924 siblings = siblings->prev;
2925 }
2926 }
2927
2928 parent = (struct lyd_node_inner *)siblings->parent;
2929 if (parent && parent->children_ht) {
2930 assert(target->hash);
2931
2932 /* find by hash */
2933 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2934 match = *match_p;
2935 } else {
2936 /* not found */
2937 match = NULL;
2938 }
2939 while (match) {
2940 /* add all found nodes into the return set */
2941 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
2942 goto error;
2943 }
2944
2945 /* find next instance */
2946 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
2947 match = NULL;
2948 } else {
2949 match = *match_p;
2950 }
2951 }
2952 } else {
2953 /* no children hash table */
2954 for (; siblings; siblings = siblings->next) {
2955 if (!lyd_compare(siblings, target, 0)) {
2956 /* a match */
2957 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
2958 goto error;
2959 }
2960 }
2961 }
2962 }
2963
2964 if (!ret->count) {
2965 ly_set_free(ret, NULL);
2966 return LY_ENOTFOUND;
2967 }
2968
2969 *set = ret;
2970 return LY_SUCCESS;
2971
2972error:
2973 ly_set_free(ret, NULL);
2974 return LY_EMEM;
2975}
2976
Michal Vasko90932a92020-02-12 14:33:03 +01002977static int
2978lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2979{
2980 struct lysc_node *val1;
2981 struct lyd_node *val2;
2982
2983 val1 = *((struct lysc_node **)val1_p);
2984 val2 = *((struct lyd_node **)val2_p);
2985
2986 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
2987
2988 if (val1 == val2->schema) {
2989 /* schema match is enough */
2990 return 1;
2991 } else {
2992 return 0;
2993 }
2994}
2995
2996static LY_ERR
2997lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2998{
2999 struct lyd_node **match_p;
3000 struct lyd_node_inner *parent;
3001 uint32_t hash;
3002 values_equal_cb ht_cb;
3003
3004 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3005
3006 /* find first sibling */
3007 if (siblings->parent) {
3008 siblings = siblings->parent->child;
3009 } else {
3010 while (siblings->prev->next) {
3011 siblings = siblings->prev;
3012 }
3013 }
3014
3015 parent = (struct lyd_node_inner *)siblings->parent;
3016 if (parent && parent->children_ht) {
3017 /* calculate our hash */
3018 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3019 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3020 hash = dict_hash_multi(hash, NULL, 0);
3021
3022 /* use special hash table function */
3023 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3024
3025 /* find by hash */
3026 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3027 siblings = *match_p;
3028 } else {
3029 /* not found */
3030 siblings = NULL;
3031 }
3032
3033 /* set the original hash table compare function back */
3034 lyht_set_cb(parent->children_ht, ht_cb);
3035 } else {
3036 /* no children hash table */
3037 for (; siblings; siblings = siblings->next) {
3038 if (siblings->schema == schema) {
3039 /* schema match is enough */
3040 break;
3041 }
3042 }
3043 }
3044
3045 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003046 if (match) {
3047 *match = NULL;
3048 }
Michal Vasko90932a92020-02-12 14:33:03 +01003049 return LY_ENOTFOUND;
3050 }
3051
Michal Vasko9b368d32020-02-14 13:53:31 +01003052 if (match) {
3053 *match = (struct lyd_node *)siblings;
3054 }
Michal Vasko90932a92020-02-12 14:33:03 +01003055 return LY_SUCCESS;
3056}
3057
Michal Vaskoe444f752020-02-10 12:20:06 +01003058API LY_ERR
3059lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3060 size_t val_len, struct lyd_node **match)
3061{
3062 LY_ERR rc;
3063 struct lyd_node *target = NULL;
3064
3065 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02003066 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003067 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
3068 return LY_EINVAL;
3069 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
3070 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
3071 return LY_EINVAL;
3072 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
3073 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
3074 lys_nodetype2str(schema->nodetype), __func__);
3075 return LY_EINVAL;
3076 }
3077
Michal Vasko62ed12d2020-05-21 10:08:25 +02003078 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3079 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003080 if (match) {
3081 *match = NULL;
3082 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003083 return LY_ENOTFOUND;
3084 }
3085
Michal Vaskof03ed032020-03-04 13:31:44 +01003086 if (key_or_value && !val_len) {
3087 val_len = strlen(key_or_value);
3088 }
3089
Michal Vasko90932a92020-02-12 14:33:03 +01003090 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01003091 switch (schema->nodetype) {
3092 case LYS_CONTAINER:
3093 case LYS_ANYXML:
3094 case LYS_ANYDATA:
3095 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01003096 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01003097 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01003098 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02003099 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01003100 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003101 break;
3102 case LYS_LEAFLIST:
3103 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02003104 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
3105 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3106 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01003107 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01003108 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01003109 if (schema->nodetype == LYS_LIST) {
3110 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003111 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003112 }
3113
3114 /* find it */
3115 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003116 break;
3117 default:
3118 /* unreachable */
3119 LOGINT(schema->module->ctx);
3120 return LY_EINT;
3121 }
3122
Michal Vaskoe444f752020-02-10 12:20:06 +01003123 lyd_free_tree(target);
3124 return rc;
3125}
Michal Vaskoccc02342020-05-21 10:09:21 +02003126
3127API LY_ERR
3128lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3129{
3130 LY_ERR ret = LY_SUCCESS;
3131 struct lyxp_set xp_set;
3132 struct lyxp_expr *exp;
3133 uint32_t i;
3134
3135 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3136
3137 memset(&xp_set, 0, sizeof xp_set);
3138
3139 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003140 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003141 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3142
3143 /* evaluate expression */
3144 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3145 LY_CHECK_GOTO(ret, cleanup);
3146
3147 /* allocate return set */
3148 *set = ly_set_new();
3149 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3150
3151 /* transform into ly_set */
3152 if (xp_set.type == LYXP_SET_NODE_SET) {
3153 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3154 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3155 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3156 (*set)->size = xp_set.used;
3157
3158 for (i = 0; i < xp_set.used; ++i) {
3159 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3160 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3161 }
3162 }
3163 }
3164
3165cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003166 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003167 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3168 return ret;
3169}